render('templates'); } public function actionCategories() { \Yii::$app->response->format = Response::FORMAT_JSON; $categories = ( new Query() )->select( [ 'id', 'title', ] ) ->from('category') ->leftJoin('category_lang', 'category_lang.category_id = category.id') ->where( [ 'language_id' => Language::getCurrent()->id, ] ) ->all(); $categories[] = [ 'id' => 0, 'title' => \Yii::t('app', 'No category'), ]; ArrayHelper::multisort($categories, 'id'); return $categories; } public function actionGroups(int $id = 0) { \Yii::$app->response->format = Response::FORMAT_JSON; $query = ( new Query() )->select( [ 'id', 'title', ] ) ->from('group') ->leftJoin('group_lang', 'group_lang.group_id = "group".id') ->where( [ 'language_id' => Language::getCurrent()->id, ] ); if ($id !== 0) { $query->andWhere( [ 'id' => ( new Query() )->select('group_id') ->from('group_to_category') ->where( [ 'category_id' => $id, ] ), ] ); } $groups = $query->all(); ArrayHelper::multisort($groups, 'id'); return $groups; } public function actionGetTemplates($categoryId, $groupId) { \Yii::$app->response->format = Response::FORMAT_JSON; $templates = CatalogSeo::find() ->where( [ 'category_id' => $categoryId, ] ) ->andWhere( [ 'group_id' => $groupId, ] ) ->asArray() ->all(); if (empty($templates)) { $activeLanguages = Language::getActive(); $return = []; foreach ($activeLanguages as $activeLanguage) { $return[] = [ 'title' => '', 'language_id' => $activeLanguage->id, 'description' => '', 'h1' => '', 'seo_text' => '', 'robots' => '', ]; } return $return; } else { return $templates; } } public function actionSaveTemplates($categoryId, $groupId) { \Yii::$app->response->format = Response::FORMAT_JSON; /** * @var \yii\web\RequestParserInterface $parser */ $parser = \Yii::createObject('yii\web\JsonParser'); $data = $parser->parse(\Yii::$app->request->rawBody, 'application/json'); $templates = CatalogSeo::find() ->where( [ 'category_id' => $categoryId, ] ) ->andWhere( [ 'group_id' => $groupId, ] ) ->indexBy('language_id') ->all(); if (empty($templates)) { $errors = []; foreach ($data as $item) { $template = new CatalogSeo( [ 'category_id' => $categoryId, 'group_id' => $groupId, ] ); $template->load($item, ''); $template->save(); $errors[] = $template->errors; } return $errors; } else { foreach ($data as $item) { $template = $templates[ $item[ 'language_id' ] ]; $template->load($item, ''); $template->save(); } return []; } } public function actionLanguages() { \Yii::$app->response->format = Response::FORMAT_JSON; $languages = Language::find() ->where( [ 'status' => true, ] ) ->asArray() ->all(); return $languages; } }