/modules/entity/controllers/ItemsController.php
PHP | 229 lines | 208 code | 21 blank | 0 comment | 30 complexity | fe4eefadfcf4cd631fbbc76f4dc3ff35 MD5 | raw file
- <?php
- namespace nitm\cms\modules\entity\controllers;
- use Yii;
- use \nitm\cms\actions\ChangeStatusAction;
- use \nitm\cms\actions\DeleteAction;
- use \nitm\cms\actions\SortByNumAction;
- use \nitm\cms\helpers\Image;
- use \nitm\cms\helpers\Upload;
- use \nitm\cms\modules\entity\EntityModule;
- use \nitm\cms\widgets\DateTimePicker;
- use yii\validators\FileValidator;
- use yii\web\UploadedFile;
- use yii\helpers\Html;
- use \nitm\cms\components\Controller;
- use \nitm\cms\modules\entity\models\Category;
- use \nitm\cms\modules\entity\models\Item;
- use yii\widgets\ActiveForm;
- class ItemsController extends Controller
- {
- public $modelClass = 'nitm\cms\modules\entity\models\Item';
- public $categoryClass = 'nitm\cms\modules\entity\models\Category';
- public function actions()
- {
- return [
- 'delete' => [
- 'class' => DeleteAction::className(),
- 'successMessage' => Yii::t('nitm/cms', 'Item deleted')
- ],
- 'up' => SortByNumAction::className(),
- 'down' => SortByNumAction::className(),
- 'on' => ChangeStatusAction::className(),
- 'off' => ChangeStatusAction::className(),
- ];
- }
- public function actionIndex($id)
- {
- return $this->render('index', [
- 'category' => $this->findCategory($id)
- ]);
- }
- public function actionCreate($id)
- {
- $category = $this->findCategory($id);
- $model = new Item(['category_id' => $id]);
- 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 {
- $this->parseData($model);
- if ($model->save()) {
- $this->flash('success', Yii::t('nitm/cms', 'Item created'));
- return $this->redirect(['/admin/'.$this->module->id.'/items/form/update/', 'id' => $model->primaryKey]);
- } else {
- $this->flash('error', Yii::t('nitm/cms', 'Create error. {0}', $model->formatErrors()));
- return $this->refresh();
- }
- }
- }
- else {
- return $this->render('create', [
- 'model' => $model,
- 'category' => $category,
- 'dataForm' => $this->generateForm($category->fields),
- 'cats' => $this->getSameCats($category)
- ]);
- }
- }
- public function actionUpdate($id, $modelClass=null, $with=[], $viewOptions=[])
- {
- $model = $this->findModel($id);
- 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 {
- $this->parseData($model);
- if ($model->save()) {
- $this->flash('success', Yii::t('nitm/cms', 'Item updated'));
- return $this->redirect(['/admin/'.$this->module->id.'/items/form/update', 'id' => $model->primaryKey]);
- } else {
- $this->flash('error', Yii::t('nitm/cms', 'Update error. {0}', $model->formatErrors()));
- return $this->refresh();
- }
- }
- }
- else {
- return $this->render('update', [
- 'model' => $model,
- 'dataForm' => $this->generateForm($model->category->fields, $model->data),
- 'cats' => $this->getSameCats($model->category)
- ]);
- }
- }
- public function actionPhotos($id)
- {
- return $this->render('photos', [
- 'model' => $this->findModel($id),
- ]);
- }
- public function actionDeleteDataFile($file)
- {
- foreach(Item::find()->where(['like', 'data', $file])->all() as $model) {
- foreach ($model->data as $name => $value) {
- if (!is_array($value) && strpos($value, '/' . $file) !== false) {
- Upload::delete($value);
- $model->data->{$name} = '';
- }
- }
- $model->update();
- }
- return $this->formatResponse(Yii::t('nitm/cms', 'Deleted'));
- }
- private function generateForm($fields, $data = null)
- {
- $result = '';
- foreach($fields as $field)
- {
- $value = !empty($data->{$field->name}) ? $data->{$field->name} : null;
- if ($field->type === 'string') {
- $result .= '<div class="form-group"><label>'. $field->title .'</label>'. Html::input('text', "Data[{$field->name}]", $value, ['class' => 'form-control']) .'</div>';
- }
- elseif ($field->type === 'text') {
- $result .= '<div class="form-group"><label>'. $field->title .'</label>'. Html::textarea("Data[{$field->name}]", $value, ['class' => 'form-control']) .'</div>';
- }
- elseif ($field->type === 'html') {
- $result .= '<div class="form-group"><label>'. $field->title .'</label>';
- $result .= \nitm\cms\widgets\Redactor::widget([
- 'name' => "Data[{$field->name}]",
- 'value' => $value,
- ]);
- $result .= '</div>';
- }
- elseif ($field->type === 'boolean') {
- $result .= '<div class="checkbox"><label>'. Html::checkbox("Data[{$field->name}]", $value, ['uncheck' => 0]) .' '. $field->title .'</label></div>';
- }
- elseif ($field->type === 'select') {
- $options = ['' => Yii::t('nitm/cms', 'Select')];
- foreach($field->options as $option){
- $options[$option] = $option;
- }
- $result .= '<div class="form-group"><label>'. $field->title .'</label><select name="Data['.$field->name.']" class="form-control">'. Html::renderSelectOptions($value, $options) .'</select></div>';
- }
- elseif ($field->type === 'checkbox') {
- $options = '';
- foreach($field->options as $option){
- $checked = $value && in_array($option, $value);
- $options .= '<br><label>'. Html::checkbox("Data[{$field->name}][]", $checked, ['value' => $option]) .' '. $option .'</label>';
- }
- $result .= '<div class="checkbox well well-sm"><b>'. $field->title .'</b>'. $options .'</div>';
- }
- elseif ($field->type === 'file') {
- $result .= '<div class="form-group"><label>'. $field->title .'</label><p>';
- if($value != ''){
- $basename = basename($value);
- $isImage = preg_match('/\.(jpg|jpeg|png|gif|bmp)$/', $basename);
- if($isImage) {
- $result .= Html::a(Html::img(Image::thumb($value, 240, 180)), Upload::getFileUrl($value), ['class' => 'fancybox']);
- } else {
- $result .= Html::a($basename, [$value], ['target' => 'blank']);
- }
- $result .= ' ' . Html::a($isImage ? 'Удалить' : '<i class="fa fa-remove"></i>', ['/admin/' . $this->module->id . '/items/delete-data-file', 'file' => $basename], ['class' => 'confirm-delete', 'data-reload' => 1, 'title' => Yii::t('nitm/cms', 'Delete')]);
- }
- $result .= '</p>' . Html::fileInput("Data[{$field->name}]"). '</div>';
- }
- elseif ($field->type === 'date') {
- $result .= '<div class="form-group"><label>'. $field->title .'</label>';
- $result .= DateTimePicker::widget(['name' => "Data[{$field->name}]", 'value' => $value]);
- $result .= '</div>';
- }
- }
- return $result;
- }
- private function parseData(&$model)
- {
- $data = Yii::$app->request->post('Data');
- if(isset($_FILES['Data']))
- {
- foreach($_FILES['Data']['name'] as $fieldName => $sourceName){
- $field = $model->category->getFieldByName($fieldName);
- $validator = new FileValidator(['extensions' => $field->options ? $field->options : null]);
- $uploadInstance = UploadedFile::getInstanceByName('Data['.$fieldName.']');
- if($uploadInstance && $validator->validate($uploadInstance) && ($result = Upload::file($uploadInstance, 'entity', false))) {
- if(!empty($model->data->{$fieldName})){
- Upload::delete($model->data->{$fieldName});
- }
- $data[$fieldName] = $result;
- } else {
- $data[$fieldName] = !empty($model->data->{$fieldName}) ? $model->data->{$fieldName} : '';
- }
- }
- }
- $model->data = $data;
- }
- private function getSameCats($cat)
- {
- $result = [];
- $fieldsHash = md5(json_encode($cat->fields));
- foreach(Category::cats() as $cat){
- if(md5(json_encode($cat->fields)) == $fieldsHash && (!count($cat->children) || EntityModule::setting('itemsInFolder'))) {
- $result[$cat->id] = $cat->title;
- }
- }
- return $result;
- }
- }