From d204fdc13522157ccb507df980b9f4a251ba7349 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Thu, 31 May 2018 15:15:40 +0300 Subject: [PATCH] - doctors - add doctor to question --- backend/controllers/DoctorController.php | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/controllers/QuestionController.php | 7 ++++++- backend/views/layouts/menu_items.php | 5 +++++ backend/views/question/_form.php | 5 +++++ backend/views/question/update.php | 2 ++ common/models/Doctor.php | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/models/DoctorLang.php | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/models/Question.php | 9 ++++++++- console/migrations/m180531_104933_create_doctor_table.php | 39 +++++++++++++++++++++++++++++++++++++++ console/migrations/m180531_111128_create_doctor_lang_table.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console/migrations/m180531_115620_alter_table_question.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ frontend/controllers/SiteController.php | 2 +- frontend/views/service/view.php | 8 +++++--- frontend/views/site/_question.php | 4 +++- 14 files changed, 590 insertions(+), 7 deletions(-) create mode 100644 backend/controllers/DoctorController.php create mode 100644 common/models/Doctor.php create mode 100644 common/models/DoctorLang.php create mode 100644 console/migrations/m180531_104933_create_doctor_table.php create mode 100644 console/migrations/m180531_111128_create_doctor_lang_table.php create mode 100644 console/migrations/m180531_115620_alter_table_question.php diff --git a/backend/controllers/DoctorController.php b/backend/controllers/DoctorController.php new file mode 100644 index 0000000..87a06b6 --- /dev/null +++ b/backend/controllers/DoctorController.php @@ -0,0 +1,195 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], + ], + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + ]; + } + + public function actions() + { + return [ + 'index' => [ + 'class' => Index::className(), + 'columns' => [ + 'name' => [ + 'type' => Index::ACTION_COL, + ], + + 'position' => [ + 'type' => Index::STRING_COL, + ], + 'sort' => [ + 'type' => Index::POSITION_COL, + ], + 'status' => [ + 'type' => Index::STATUS_COL, + ], + ], + 'model' => Doctor::className(), + 'hasLanguage' => true, + 'enableMassDelete' => true, + 'modelPrimaryKey' => 'id', + ], + 'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()), + 'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()), + 'view' => [ + 'class' => View::className(), + 'model' => Doctor::className(), + 'hasAlias' => true, + 'languageFields' => [ + [ + 'name' => 'name', + 'type' => Form::STRING, + ], + [ + 'name' => 'position', + 'type' => Form::STRING, + ], + [ + 'name' => 'description', + 'type' => Form::WYSIWYG, + ], + ], + 'fields' => [ + [ + 'name' => 'service_id', + 'type' => Form::RELATION, + 'relationAttribute' => 'title', + 'relationName' => 'service', + 'multiple' => false, + ], + ], + ], + 'delete' => [ + 'class' => Delete::className(), + ], + ]; + } + + public function findModel($id) + { + $model = Doctor::find() + ->with('languages') + ->where([ 'id' => $id ]) + ->one(); + if ($model !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } + + public function newModel() + { + return new Doctor(); + } + + public function deleteModel($id) + { + $page = Doctor::find() + ->with('languages.alias') + ->where( + [ + 'id' => $id, + ] + ) + ->one(); + $langs = call_user_func( + [ + $page, + 'getVariationModels', + ] + ); + foreach ($langs as $lang) { + if ($lang->alias !== null) { + $lang->alias->delete(); + } + } + + return $page->delete(); + } + + protected static function fieldsConfig() + { + return [ + 'model' => Doctor::className(), + 'hasAlias' => true, + 'languageFields' => [ + [ + 'name' => 'name', + 'type' => Form::STRING, + 'decorate' => true + ], + [ + 'name' => 'description', + 'type' => Form::WYSIWYG, + ], + [ + 'name' => 'position', + 'type' => Form::STRING, + ], + ], + 'fields' => [ + [ + 'name' => 'image_id', + 'type' => Form::IMAGE + ], + [ + 'name' => 'service_id', + 'type' => Form::RELATION, + 'relationAttribute' => 'title', + 'relationName' => 'service', + 'multiple' => false, + ], + [ + 'name' => 'status', + 'type' => Form::BOOL, + ], + [ + 'name' => 'sort', + 'type' => Form::NUMBER, + ], + ], + ]; + } + + } \ No newline at end of file diff --git a/backend/controllers/QuestionController.php b/backend/controllers/QuestionController.php index 795b6dd..23dbce5 100644 --- a/backend/controllers/QuestionController.php +++ b/backend/controllers/QuestionController.php @@ -12,6 +12,7 @@ use artbox\core\admin\actions\Index; use artbox\core\admin\actions\View; use artbox\core\admin\widgets\Form; + use common\models\Doctor; use common\models\Question; use common\models\Service; use yii\filters\AccessControl; @@ -133,6 +134,9 @@ $model = $this->findModel($id); $services = Service::find()->all(); $data = ArrayHelper::map($services, 'id', 'title'); + + $doctors = Doctor::find()->all(); + $dataDoctors = ArrayHelper::map($doctors, 'id', 'name'); $model->scenario = Question::SCENARIO_ANSWER; if ($model->load(\Yii::$app->request->post()) && $model->save()) { return $this->redirect('index'); @@ -141,7 +145,8 @@ 'update', [ 'model' => $model, - 'services' => $data + 'services' => $data, + 'doctors' => $dataDoctors ] ); } diff --git a/backend/views/layouts/menu_items.php b/backend/views/layouts/menu_items.php index abb5f8f..b7d42f1 100755 --- a/backend/views/layouts/menu_items.php +++ b/backend/views/layouts/menu_items.php @@ -65,6 +65,11 @@ 'url' => [ '/package/index' ], ], + [ + 'label' => \Yii::t('app', 'Doctors'), + 'url' => [ '/doctor/index' ], + + ], ], ], diff --git a/backend/views/question/_form.php b/backend/views/question/_form.php index df363e7..5b8b135 100644 --- a/backend/views/question/_form.php +++ b/backend/views/question/_form.php @@ -8,6 +8,7 @@ /* @var $this yii\web\View */ /* @var $model \common\models\Question */ /* @var $form yii\widgets\ActiveForm */ + /* @var $doctors \common\models\Doctor[]*/ Switchery::register($this); $js = <<< JS $('.switchery').each(function(idx, elem) { @@ -18,6 +19,7 @@ $('.switchery').each(function(idx, elem) { }); $(".select_service").select2(); +$(".select_doctors").select2(); JS; @@ -30,6 +32,9 @@ JS; field($model, 'service_id')->dropDownList($services, [ 'class' => 'select_service' ])?> + field($model, 'doctor_id')->dropDownList($doctors, [ + 'class' => 'select_doctors' + ])?> field($model, 'name') ->textInput([ 'maxlength' => true ]) ?> diff --git a/backend/views/question/update.php b/backend/views/question/update.php index eb743e9..f43c89a 100644 --- a/backend/views/question/update.php +++ b/backend/views/question/update.php @@ -4,6 +4,7 @@ /* @var $this yii\web\View */ /* @var $model \common\models\Question */ + /* @var $doctors \common\models\Doctor[]*/ $this->title = Yii::t( 'app', @@ -40,6 +41,7 @@ [ 'model' => $model, 'services' => $services, + 'doctors' => $doctors ] ) ?> diff --git a/common/models/Doctor.php b/common/models/Doctor.php new file mode 100644 index 0000000..e63aa05 --- /dev/null +++ b/common/models/Doctor.php @@ -0,0 +1,126 @@ + [ + 'class' => VariationBehavior::className(), + 'variationsRelation' => 'languages', + 'defaultVariationRelation' => 'language', + 'variationOptionReferenceAttribute' => 'language_id', + 'optionModelClass' => Language::className(), + 'defaultVariationOptionReference' => function () { + return Language::getCurrent()->id; + }, + 'optionQueryFilter' => function (ActiveQuery $query) { + $query->where( + [ + 'status' => true, + ] + ); + }, + ], + 'positionBehavior' => [ + 'class' => PositionBehavior::className(), + 'positionAttribute' => 'sort', + ], + ]; + } + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['service_id', 'sort'], 'default', 'value' => null], + [['service_id', 'sort'], 'integer'], + [['status'], 'boolean'], + [['service_id'], 'exist', 'skipOnError' => true, 'targetClass' => Service::className(), 'targetAttribute' => ['service_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'service_id' => Yii::t('app', 'Service ID'), + 'status' => Yii::t('app', 'Status'), + 'sort' => Yii::t('app', 'Sort'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getService() + { + return $this->hasOne(Service::className(), ['id' => 'service_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguages() + { + return $this->hasMany(DoctorLang::className(), ['doctor_id' => 'id']); + } + + public function getLanguage(){ + return $this->hasDefaultVariationRelation(); + } + + /** + * @return string + */ + public function getRoute() + { + return Json::encode( + [ + 'doctor/view', + 'id' => $this->id, + ] + ); + } + + public function getImage(){ + return $this->hasOne(Image::className(), ['id' => 'image_id']); + } +} diff --git a/common/models/DoctorLang.php b/common/models/DoctorLang.php new file mode 100644 index 0000000..b55f89c --- /dev/null +++ b/common/models/DoctorLang.php @@ -0,0 +1,88 @@ + null], + [['language_id', 'alias_id', 'doctor_id'], 'integer'], + [['description'], 'string'], + [['name', 'position'], 'string', 'max' => 255], + [['language_id', 'doctor_id'], 'unique', 'targetAttribute' => ['language_id', 'doctor_id']], + [['alias_id'], 'exist', 'skipOnError' => true, 'targetClass' => Alias::className(), 'targetAttribute' => ['alias_id' => 'id']], + [['doctor_id'], 'exist', 'skipOnError' => true, 'targetClass' => Doctor::className(), 'targetAttribute' => ['doctor_id' => 'id']], + [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::className(), 'targetAttribute' => ['language_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'language_id' => Yii::t('app', 'Language ID'), + 'alias_id' => Yii::t('app', 'Alias ID'), + 'doctor_id' => Yii::t('app', 'Doctor ID'), + 'name' => Yii::t('app', 'Name'), + 'position' => Yii::t('app', 'Position'), + 'description' => Yii::t('app', 'Description'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAlias() + { + return $this->hasOne(Alias::className(), ['id' => 'alias_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getDoctor() + { + return $this->hasOne(Doctor::className(), ['id' => 'doctor_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), ['id' => 'language_id']); + } +} diff --git a/common/models/Question.php b/common/models/Question.php index e1c059c..3446e88 100644 --- a/common/models/Question.php +++ b/common/models/Question.php @@ -47,7 +47,8 @@ ], self::SCENARIO_ANSWER => [ 'answer', - 'status' + 'status', + 'doctor_id' ], ] ); @@ -91,6 +92,7 @@ 'service_id', 'created_at', 'updated_at', + 'doctor_id' ], 'integer', ], @@ -143,4 +145,9 @@ { return $this->hasOne(Service::className(), [ 'id' => 'service_id' ]); } + + public function getDoctor() + { + return $this->hasOne(Doctor::className(), [ 'id' => 'doctor_id' ]); + } } diff --git a/console/migrations/m180531_104933_create_doctor_table.php b/console/migrations/m180531_104933_create_doctor_table.php new file mode 100644 index 0000000..64a137c --- /dev/null +++ b/console/migrations/m180531_104933_create_doctor_table.php @@ -0,0 +1,39 @@ +createTable('doctor', [ + 'id' => $this->primaryKey(), + 'service_id' => $this->integer(), + 'status' => $this->boolean(), + 'sort' => $this->integer(), + 'image_id' => $this->integer() + ]); + + $this->addForeignKey('doctor_service_fk', + 'doctor', + 'service_id', + 'service', + 'id', + 'SET NULL', + 'CASCADE'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('doctor'); + } +} diff --git a/console/migrations/m180531_111128_create_doctor_lang_table.php b/console/migrations/m180531_111128_create_doctor_lang_table.php new file mode 100644 index 0000000..15b2192 --- /dev/null +++ b/console/migrations/m180531_111128_create_doctor_lang_table.php @@ -0,0 +1,58 @@ +createTable('doctor_lang', [ + 'language_id' => $this->integer(), + 'alias_id' => $this->integer(), + 'doctor_id' => $this->integer(), + 'name' => $this->string(), + 'position' => $this->string(), + 'description' => $this->text() + ]); + + $this->addPrimaryKey('doctor_lang_pk', 'doctor_lang', ['language_id', 'doctor_id']); + + $this->addForeignKey('doctor_lang_language_fk', + 'doctor_lang', + 'language_id', + 'language', + 'id'); + $this->addForeignKey('doctor_lang_doctor_fk', + 'doctor_lang', + 'doctor_id', + 'doctor', + 'id', + 'CASCADE', + 'CASCADE'); + + $this->addForeignKey('doctor_lang_alias_fk', + 'doctor_lang', + 'alias_id', + 'alias', + 'id', + 'CASCADE', + 'CASCADE'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('doctor_lang_language_fk', 'doctor_lang'); + $this->dropForeignKey('doctor_lang_doctor_fk', 'doctor_lang'); + $this->dropForeignKey('doctor_lang_alias_fk', 'doctor_lang'); + $this->dropTable('doctor_lang'); + } +} diff --git a/console/migrations/m180531_115620_alter_table_question.php b/console/migrations/m180531_115620_alter_table_question.php new file mode 100644 index 0000000..eaa9d08 --- /dev/null +++ b/console/migrations/m180531_115620_alter_table_question.php @@ -0,0 +1,49 @@ +addColumn('question', 'doctor_id', $this->integer()); + + $this->addForeignKey('question_doctor_fk', + 'question', + 'doctor_id', + 'doctor', + 'id', + 'SET NULL', + 'CASCADE'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('question_doctor_fk', 'question'); + $this->dropColumn('coctor_id', 'question'); + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m180531_115620_alter_table_question cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 6761d25..4b9d403 100755 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -239,7 +239,7 @@ } } $dataProvider = new ActiveDataProvider([ - 'query' => Question::find()->where(['status' => true])->andFilterWhere(['service_id' => $service_id]), + 'query' => Question::find()->where(['status' => true])->andFilterWhere(['service_id' => $service_id])->with(['doctor.language']), 'pagination' => [ 'pageSize' => 10, ], diff --git a/frontend/views/service/view.php b/frontend/views/service/view.php index c1b36e4..82d284e 100644 --- a/frontend/views/service/view.php +++ b/frontend/views/service/view.php @@ -71,7 +71,7 @@ - +
Оставить отзыв
@@ -91,7 +91,9 @@
name?>
question?>
-
врач-гинеколог Алексей нестеренко
+ doctor !== null){?> +
doctor->position?>doctor->name?>
+
answer?>
@@ -101,7 +103,7 @@
- +
Задать вопрос
diff --git a/frontend/views/site/_question.php b/frontend/views/site/_question.php index b6cabc7..13cbe88 100644 --- a/frontend/views/site/_question.php +++ b/frontend/views/site/_question.php @@ -14,7 +14,9 @@
-
врач-гинеколог Алексей нестеренко
+ doctor !== null){?> +
doctor->position?> doctor->name?>
+
answer?>
-- libgit2 0.21.4