Пример #1
0
 public function actionCreate($id)
 {
     if (!($category = Category::findOne($id))) {
         return $this->redirect(['/admin/catalog']);
     }
     $model = new Item();
     if ($model->load(Yii::$app->request->post())) {
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         } else {
             $model->category_id = $category->primaryKey;
             $model->data = Yii::$app->request->post('Data');
             if (isset($_FILES) && $this->module->settings['itemThumb']) {
                 $model->thumb = UploadedFile::getInstance($model, 'thumb');
                 if ($model->thumb && $model->validate(['thumb'])) {
                     $model->thumb = Image::upload($model->thumb, 'catalog', $this->module->settings['itemThumbWidth'], $this->module->settings['itemThumbHeight'], $this->module->settings['itemThumbCrop']);
                 } else {
                     $model->thumb = '';
                 }
             }
             if ($model->save()) {
                 $this->flash('success', Yii::t('easyii/catalog', 'Item created'));
                 return $this->redirect(['/admin/catalog/items/edit/', 'id' => $model->primaryKey]);
             } else {
                 $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
                 return $this->refresh();
             }
         }
     } else {
         return $this->render('create', ['model' => $model, 'category' => $category, 'dataForm' => $this->generateForm($category->fields)]);
     }
 }
 public function actionGenerate($id)
 {
     $category = Category::findOne($id);
     /** @var StaticUrl $class */
     $class = AdminModule::getInstance()->getCatalog()->staticUrlClass;
     $class::generate($category);
 }
Пример #3
0
 public function init()
 {
     parent::init();
     require_once Yii::$app->basePath . '/../vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
     $this->_category = Category::findOne($this->categoryId);
     if ($this->_category === null) {
         throw new \Exception(Yii::t('app', 'Категория {categoryId} не найдена.', ['categoryId' => $this->categoryId]));
     }
     if (!$this->exportConfig) {
         throw new \Exception('Не настроено.');
     }
     if ($this->db === null) {
         $this->db = Yii::$app->getDb();
     }
 }
Пример #4
0
 public function actionFields($id)
 {
     if (!($model = Category::findOne($id))) {
         return $this->redirect(['/admin/' . $this->module->id]);
     }
     if (Yii::$app->request->post('save')) {
         $fields = Yii::$app->request->post('Field') ?: [];
         $result = [];
         foreach ($fields as $field) {
             $temp = json_decode($field);
             if ($temp === null && json_last_error() !== JSON_ERROR_NONE || empty($temp->name) || empty($temp->title) || empty($temp->type) || !($temp->name = trim($temp->name)) || !($temp->title = trim($temp->title)) || !array_key_exists($temp->type, Category::$fieldTypes)) {
                 continue;
             }
             $options = '';
             if ($temp->type == 'select' || $temp->type == 'checkbox') {
                 if (empty($temp->options) || !($temp->options = trim($temp->options))) {
                     continue;
                 }
                 $options = [];
                 foreach (explode(',', $temp->options) as $option) {
                     $options[] = trim($option);
                 }
             }
             $result[] = ['name' => \yii\helpers\Inflector::slug($temp->name), 'title' => $temp->title, 'type' => $temp->type, 'options' => $options];
         }
         $model->fields = $result;
         if ($model->save()) {
             $ids = [];
             foreach ($model->children()->all() as $child) {
                 $ids[] = $child->primaryKey;
             }
             if (count($ids)) {
                 Category::updateAll(['fields' => json_encode($model->fields)], ['in', 'category_id', $ids]);
             }
             $this->flash('success', Yii::t('easyii/catalog', 'Category updated'));
         } else {
             $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors()));
         }
         return $this->refresh();
     } else {
         return $this->render('fields', ['model' => $model]);
     }
 }
Пример #5
0
 public function actionDelete($id)
 {
     if ($model = Category::findOne($id)) {
         $model->delete();
     } else {
         $this->error = Yii::t('easyii', 'Not found');
     }
     return $this->formatResponse(Yii::t('easyii/catalog', 'Category deleted'));
 }
Пример #6
0
 public function actionGroups($id)
 {
     if (!($model = Category::findOne($id))) {
         return $this->redirect(['/admin/' . $this->module->id]);
     }
     if (Yii::$app->request->post('save')) {
         $images = Yii::$app->request->post('image');
         $names = Yii::$app->request->post('name');
         $result = [];
         foreach ($names as $key => $name) {
             $r = new \stdClass();
             $r->name = $name;
             $r->file = $images[$key];
             $result[] = $r;
         }
         $model->groups = $result;
         if ($model->save()) {
             $ids = [];
             foreach ($model->children()->all() as $child) {
                 $ids[] = $child->primaryKey;
             }
             if (count($ids)) {
                 Category::updateAll(['groups' => json_encode($model->groups)], ['in', 'category_id', $ids]);
             }
             $this->flash('success', Yii::t('easyii/catalog', 'Category updated'));
         } else {
             $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors()));
         }
         return $this->refresh();
     } else {
         return $this->render('groups', ['model' => $model]);
     }
 }