From 8b3687cb81121003ac7805a7aa3a8eb67aaeb136 Mon Sep 17 00:00:00 2001 From: vladrigos Date: Tue, 4 Aug 2020 11:14:55 +0300 Subject: [PATCH 1/2] added upload files in additional fields --- .../modules/balance/views/balance/_form.php | 13 ++++++-- .../modules/balance/views/balance/view.php | 6 +++- backend/modules/company/models/Company.php | 30 ++++++++++++------- .../modules/company/views/company/_form.php | 29 ++++++++++++------ .../modules/company/views/company/view.php | 6 +++- backend/modules/project/models/Project.php | 27 +++++++++++------ .../modules/project/views/project/_form.php | 13 ++++++-- .../modules/project/views/project/view.php | 9 +++++- common/models/Balance.php | 27 +++++++++++------ 9 files changed, 115 insertions(+), 45 deletions(-) diff --git a/backend/modules/balance/views/balance/_form.php b/backend/modules/balance/views/balance/_form.php index efa92da..b862877 100755 --- a/backend/modules/balance/views/balance/_form.php +++ b/backend/modules/balance/views/balance/_form.php @@ -6,6 +6,7 @@ use yii\helpers\Html; use yii\jui\DatePicker; use yii\web\View; use yii\widgets\ActiveForm; +use mihaildev\elfinder\InputFile; /* @var $this yii\web\View */ /* @var $model backend\modules\company\models\Balance */ @@ -35,7 +36,7 @@ use yii\widgets\ActiveForm;
field($model, 'fields')->widget(MultipleInput::class, [ - + 'cloneButton' => true, 'columns' => [ [ 'name' => 'field_id', @@ -53,8 +54,16 @@ use yii\widgets\ActiveForm; 'name' => 'value', 'title' => 'Значение', 'enableError' => true, + 'type' => InputFile::class, 'options' => [ - 'class' => 'input-priority' + 'language' => 'ru', + 'controller' => 'elfinder', + 'name' => 'fields[value]', + 'id' => 'fields-value', + 'options' => ['class' => 'form-control itemImg', 'maxlength' => '255'], + 'buttonOptions' => ['class' => 'btn btn-primary'], + 'value' => $model->fields[0]['value'], + 'buttonName' => 'Выбрать файл', ] ], [ diff --git a/backend/modules/balance/views/balance/view.php b/backend/modules/balance/views/balance/view.php index daf144a..9d60018 100755 --- a/backend/modules/balance/views/balance/view.php +++ b/backend/modules/balance/views/balance/view.php @@ -49,7 +49,11 @@ $this->params['breadcrumbs'][] = $this->title; 'field.name:text:Поле', [ 'attribute' => 'value', - 'label' => 'Значение' + 'format' => 'raw', + 'label' => 'Значение', + 'value' => function ($model) { + return $model->getValue(); + } ], ], ]); ?> diff --git a/backend/modules/company/models/Company.php b/backend/modules/company/models/Company.php index 19f6d7f..37d9f62 100755 --- a/backend/modules/company/models/Company.php +++ b/backend/modules/company/models/Company.php @@ -4,6 +4,7 @@ namespace backend\modules\company\models; use common\models\FieldsValue; use common\models\FieldsValueNew; +use Yii; class Company extends \common\models\Company { @@ -15,10 +16,10 @@ class Company extends \common\models\Company $fieldValue = FieldsValueNew::find() ->where( - [ - 'item_id' => \Yii::$app->request->get('id'), - 'item_type' => FieldsValueNew::TYPE_COMPANY, - ]) + [ + 'item_id' => \Yii::$app->request->get('id'), + 'item_type' => FieldsValueNew::TYPE_COMPANY, + ]) ->all(); $array = []; if(!empty($fieldValue)){ @@ -27,6 +28,7 @@ class Company extends \common\models\Company ['field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order, + 'type_file' => $item->type_file, 'field_name' => $item->field->name]); } $this->fields = $array; @@ -38,6 +40,7 @@ class Company extends \common\models\Company 'value' => null, 'order' => null, 'field_name' => null, + 'type_file' => null, ], ]; } @@ -50,14 +53,19 @@ class Company extends \common\models\Company FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_COMPANY]); foreach ( $post['fields'] as $item) { - $fildsValue = new FieldsValueNew(); - $fildsValue->field_id = $item['field_id']; - $fildsValue->value = $item['value']; - $fildsValue->order = $item['order']; - $fildsValue->item_id = $this->id; - $fildsValue->item_type = FieldsValueNew::TYPE_COMPANY; + $fieldsValue = new FieldsValueNew(); + $fieldsValue->field_id = $item['field_id']; + $fieldsValue->value = $item['value']; + $fieldsValue->order = $item['order']; + $fieldsValue->item_id = $this->id; + $fieldsValue->item_type = FieldsValueNew::TYPE_COMPANY; + if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){ + $fieldsValue->type_file = 'file'; + }else{ + $fieldsValue->type_file = 'text'; + } - $fildsValue->save(); + $fieldsValue->save(); } parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub diff --git a/backend/modules/company/views/company/_form.php b/backend/modules/company/views/company/_form.php index 7cff194..a389fad 100755 --- a/backend/modules/company/views/company/_form.php +++ b/backend/modules/company/views/company/_form.php @@ -4,6 +4,7 @@ use backend\modules\settings\models\AdditionalFields; use unclead\multipleinput\MultipleInput; use yii\helpers\Html; use yii\widgets\ActiveForm; +use mihaildev\elfinder\InputFile; /* @var $this yii\web\View */ /* @var $model backend\modules\company\models\Company */ @@ -31,12 +32,12 @@ use yii\widgets\ActiveForm;
- field($model, 'fields')->widget(MultipleInput::class, [ - + field($model, 'fields')->widget(MultipleInput::class,[ + 'cloneButton' => true, 'columns' => [ [ - 'name' => 'field_id', - 'type' => 'dropDownList', + 'name' => 'field_id', + 'type' => 'dropDownList', 'title' => 'Поле', 'defaultValue' => null, 'items' => \yii\helpers\ArrayHelper::map(AdditionalFields::find() @@ -47,21 +48,31 @@ use yii\widgets\ActiveForm; 'options' => ['prompt' => 'Выберите'] ], [ - 'name' => 'value', + 'name' => 'value', 'title' => 'Значение', 'enableError' => true, + 'type' => InputFile::class, 'options' => [ - 'class' => 'input-priority' - ] + 'language' => 'ru', + 'controller' => 'elfinder', + // вставляем название контроллера, по умолчанию равен elfinder + // фильтр файлов, можно задать массив фильтров https://github.com/Studio-42/elFinder/wiki/Client-con.. + 'name' => 'fields[value]', + 'id' => 'fields-value', + 'options' => ['class' => 'form-control itemImg', 'maxlength' => '255'], + 'buttonOptions' => ['class' => 'btn btn-primary'], + 'value' => $model->fields[0]['value'], + 'buttonName' => 'Выбрать файл', + ], ], [ - 'name' => 'order', + 'name' => 'order', 'title' => 'Приоритет', 'enableError' => true, 'options' => [ 'class' => 'input-priority' ] - ] + ], ] ])->label('Дополнительно'); ?> diff --git a/backend/modules/company/views/company/view.php b/backend/modules/company/views/company/view.php index df256a4..8ddbaaa 100755 --- a/backend/modules/company/views/company/view.php +++ b/backend/modules/company/views/company/view.php @@ -45,7 +45,11 @@ $this->params['breadcrumbs'][] = $this->title; 'field.name:text:Поле', [ 'attribute' => 'value', - 'label' => 'Значение' + 'format' => 'raw', + 'label' => 'Значение', + 'value' => function ($model) { + return $model->getValue(); + } ], ], ]); ?> diff --git a/backend/modules/project/models/Project.php b/backend/modules/project/models/Project.php index 57fc08a..098a4ed 100755 --- a/backend/modules/project/models/Project.php +++ b/backend/modules/project/models/Project.php @@ -7,6 +7,7 @@ use common\models\FieldsValue; use common\models\FieldsValueNew; use common\models\ProjectUser; use yii\helpers\ArrayHelper; +use Yii; class Project extends \common\models\Project { @@ -31,15 +32,17 @@ class Project extends \common\models\Project 'field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order, + 'type_file' => $item->type_file, 'field_name' => $item->field->name]); } $this->fields = $array; } else { $this->fields = [ [ - 'field_id' => null, - 'value' => null, + 'field_id' => null, + 'value' => null, 'order' => null, + 'type_file' => null, 'field_name' => null, ], ]; @@ -61,14 +64,20 @@ class Project extends \common\models\Project FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROJECT]); foreach ($post['fields'] as $item) { - $fildsValue = new FieldsValueNew(); - $fildsValue->field_id = $item['field_id']; - $fildsValue->value = $item['value']; - $fildsValue->order = $item['order']; - $fildsValue->item_id = $this->id; - $fildsValue->item_type = FieldsValueNew::TYPE_PROJECT; + $fieldsValue = new FieldsValueNew(); + $fieldsValue->field_id = $item['field_id']; + $fieldsValue->value = $item['value']; + $fieldsValue->order = $item['order']; + $fieldsValue->item_id = $this->id; + $fieldsValue->item_type = FieldsValueNew::TYPE_PROJECT; - $fildsValue->save(); + if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){ + $fieldsValue->type_file = 'file'; + }else{ + $fieldsValue->type_file = 'text'; + } + + $fieldsValue->save(); } ProjectUser::deleteAll(['project_id' => $this->id]); diff --git a/backend/modules/project/views/project/_form.php b/backend/modules/project/views/project/_form.php index 4ed2289..fd75953 100755 --- a/backend/modules/project/views/project/_form.php +++ b/backend/modules/project/views/project/_form.php @@ -4,6 +4,7 @@ use kartik\select2\Select2; use unclead\multipleinput\MultipleInput; use yii\helpers\Html; use yii\widgets\ActiveForm; +use mihaildev\elfinder\InputFile; /* @var $this yii\web\View */ /* @var $model backend\modules\project\models\Project */ @@ -53,7 +54,7 @@ use yii\widgets\ActiveForm;
field($model, 'fields')->widget(MultipleInput::class, [ - + 'cloneButton' => true, 'columns' => [ [ 'name' => 'field_id', @@ -71,8 +72,16 @@ use yii\widgets\ActiveForm; 'name' => 'value', 'title' => 'Значение', 'enableError' => true, + 'type' => InputFile::class, 'options' => [ - 'class' => 'input-priority' + 'language' => 'ru', + 'controller' => 'elfinder', + 'name' => 'fields[value]', + 'id' => 'fields-value', + 'options' => ['class' => 'form-control itemImg', 'maxlength' => '255'], + 'buttonOptions' => ['class' => 'btn btn-primary'], + 'value' => $model->fields[0]['value'], + 'buttonName' => 'Выбрать файл', ] ], [ diff --git a/backend/modules/project/views/project/view.php b/backend/modules/project/views/project/view.php index fbaabb4..217a39d 100755 --- a/backend/modules/project/views/project/view.php +++ b/backend/modules/project/views/project/view.php @@ -47,7 +47,14 @@ $this->params['breadcrumbs'][] = $this->title; 'layout' => "{items}", 'columns' => [ 'field.name:text:Поле', - 'value', + [ + 'attribute' => 'value', + 'format' => 'raw', + 'label' => 'Значение', + 'value' => function ($model) { + return $model->getValue(); + } + ], ], ]); ?> diff --git a/common/models/Balance.php b/common/models/Balance.php index 3f4465c..cdf2edb 100755 --- a/common/models/Balance.php +++ b/common/models/Balance.php @@ -7,6 +7,7 @@ use common\classes\Debug; use PHPUnit\Framework\MockObject\Matcher\DeferredError; use yii\db\Query; use yii\helpers\ArrayHelper; +use yii; /** * This is the model class for table "company". @@ -42,6 +43,7 @@ class Balance extends \yii\db\ActiveRecord ['field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order, + 'type_file' => $item->type_file, 'field_name' => $item->field->name]); } $this->fields = $array; @@ -51,6 +53,7 @@ class Balance extends \yii\db\ActiveRecord 'field_id' => null, 'value' => null, 'order' => null, + 'type_file' => null, 'field_name' => null, ], ]; @@ -82,8 +85,8 @@ class Balance extends \yii\db\ActiveRecord return ArrayHelper::map( AdditionalFields::find() - ->leftJoin('use_field', 'additional_fields.id=use_field.field_id') - ->where(['use_field.use' => $type])->all(), 'id', 'name' + ->leftJoin('use_field', 'additional_fields.id=use_field.field_id') + ->where(['use_field.use' => $type])->all(), 'id', 'name' ); } @@ -131,14 +134,20 @@ class Balance extends \yii\db\ActiveRecord FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_BALANCE]); foreach ($post['fields'] as $item) { - $fildsValue = new FieldsValueNew(); - $fildsValue->field_id = $item['field_id']; - $fildsValue->value = $item['value']; - $fildsValue->order = $item['order']; - $fildsValue->item_id = $this->id; - $fildsValue->item_type = FieldsValueNew::TYPE_BALANCE; + $fieldsValue = new FieldsValueNew(); + $fieldsValue->field_id = $item['field_id']; + $fieldsValue->value = $item['value']; + $fieldsValue->order = $item['order']; + $fieldsValue->item_id = $this->id; + $fieldsValue->item_type = FieldsValueNew::TYPE_BALANCE; - $fildsValue->save(); + if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){ + $fieldsValue->type_file = 'file'; + }else{ + $fieldsValue->type_file = 'text'; + } + + $fieldsValue->save(); } parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub From fa731a80fe65c6fa4352b69cf34b564690683204 Mon Sep 17 00:00:00 2001 From: vladrigos Date: Tue, 4 Aug 2020 11:16:14 +0300 Subject: [PATCH 2/2] fix exception for project without status, fix character set in company table --- common/models/Project.php | 2 +- ...837_add_table_options_to_company_table.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 console/migrations/m200803_135837_add_table_options_to_company_table.php diff --git a/common/models/Project.php b/common/models/Project.php index 1f471de..9ec01d3 100755 --- a/common/models/Project.php +++ b/common/models/Project.php @@ -52,7 +52,7 @@ class Project extends \yii\db\ActiveRecord public function rules() { return [ - [['name'], 'required'], + [['name', 'status'], 'required'], [['description'], 'string'], [['created_at', 'updated_at'], 'safe'], [['status'], 'exist', 'skipOnError' => true, 'targetClass' => Status::class, 'targetAttribute' => ['status' => 'id']], diff --git a/console/migrations/m200803_135837_add_table_options_to_company_table.php b/console/migrations/m200803_135837_add_table_options_to_company_table.php new file mode 100644 index 0000000..630bdb3 --- /dev/null +++ b/console/migrations/m200803_135837_add_table_options_to_company_table.php @@ -0,0 +1,30 @@ +db->driverName === 'mysql') { + $this->execute('ALTER TABLE company DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + if ($this->db->driverName === 'mysql') { + $this->execute('ALTER TABLE company DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ENGINE=InnoDB'); + } + } +} +