public function actionCreate($id) { if (!($category = Category::findOne($id))) { return $this->redirect(['/admin/' . $this->module->id]); } $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->image = UploadedFile::getInstance($model, 'image'); if ($model->image && $model->validate(['image'])) { $model->image = Image::upload($model->image, 'pharmacies'); } else { $model->image = ''; } } if ($model->save()) { $this->flash('success', Yii::t('easyii/pharmacies', 'Item created')); return $this->redirect(['/admin/' . $this->module->id . '/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 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/pharmacies', 'Category updated')); } else { $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors())); } return $this->refresh(); } else { return $this->render('fields', ['model' => $model]); } }
private function findCategory($id_slug) { $category = Category::find()->where(['or', 'category_id=:id_slug', 'slug=:id_slug'], [':id_slug' => $id_slug])->status(Item::STATUS_ON)->one(); return $category ? new CategoryObject($category) : null; }
public function getCategory() { return $this->hasOne(Category::className(), ['category_id' => 'category_id']); }