/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $this->scenario = 'search'; $query = Field::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 100]]); $this->load($params); if ($this->document_id) { $query->joinWith(['document' => function ($q) { $q->where(['like', 'document.name', $this->document_id])->orWhere(['like', 'document.id', $this->document_id]); }]); } if ($this->option_id) { $query->joinWith(['option' => function ($q) { $q->where(['like', 'option.name', $this->option_id])->orWhere(['like', 'option.id', $this->option_id]); }]); } if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'position' => $this->position]); $query->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }
/** * @return bool */ public function beforeDelete() { if (parent::beforeDelete()) { //Удаляем дополнительные поля документа Field::deleteAll('document_id = :document_id', [':document_id' => $this->id]); //Удаляем папку со связанными файлами документа CFF::RemoveDir(Document::FROM_ADM_PATH . Document::FILES_PATH . $this->id); return true; } else { return false; } }
/** * @return bool * @throws \yii\db\Exception */ public function savePhoto() { if ($this->file) { $this->deletePhoto(); $ext = "." . end(explode(".", $this->file)); if ($ext === ".") { $ext = '.jpg'; } $path = Document::FROM_ADM_PATH . Document::FILES_PATH . $this->document_id . '/'; if (!file_exists($path)) { mkdir($path, 0777, true); } $name = $this->document->alias . "-" . $this->id; $fullname = $path . $name . $ext; $this->file->saveAs($fullname); $this->file = $fullname; Image::thumbnail($fullname, 300, 200, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND)->save($path . $name . '_thumb' . $ext, ['quality' => 100]); $this->value = Document::FILES_PATH . $this->document_id . '/' . $name . $ext; if (!$this->isNewRecord) { $db = Field::getDb(); $db->createCommand()->update('field', ['value' => Document::FILES_PATH . $this->document_id . '/' . $name . $ext], ['id' => $this->id])->execute(); } else { $this->save(); } } return true; }
/** * Удаление значения мультиполя * @return mixed */ public function actionDeletefield() { $field_id = Yii::$app->request->get('id'); /** @var \common\models\Field $field */ $field = Field::findOne($field_id); if ($field && isset($field->option)) { //необходимое кол-во значений мультиполя $count_require = $field->option->require; if ($count_require) { $all_fields = Field::find()->where(['option_id' => $field->option_id, 'document_id' => $field->document_id])->all(); if (count($all_fields) <= $count_require) { return $this->redirect(['update', 'id' => $field->document_id]); } } $field->deletePhoto(); $document_id = $field->document_id; $field->delete(); Yii::$app->getSession()->setFlash('field-delete-success'); return $this->redirect(['update', 'id' => $document_id]); } throw new NotFoundHttpException('Страница не найдена.'); }
/** * Finds the Field model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Field the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Field::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<?php echo Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'method' => 'post']]); ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'name', 'price', 'product_type', 'created_at', 'updated_at', 'created_by', 'updated_by']]); ?> </div> <div class="col-lg-6"> <h1>Fields</h1> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($productField, 'field_id')->dropDownList(ArrayHelper::map(Field::find()->all(), 'id', 'name'), ['prompt' => 'Please select Field']); ?> <?php echo $form->field($productField, 'product_id')->hiddenInput(['value' => $model->id])->label(false); ?> <?php echo $form->field($productField, 'value')->textInput(); ?> <?php echo $form->field($productField, 'rules')->textarea(['row' => '3']); ?> <div class="form-group"> <?php echo Html::submitButton($productField->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?>