From b7ed0dab993df42fd04dfec8eac7479640637af6 Mon Sep 17 00:00:00 2001 From: Korzinkayablok Date: Thu, 9 Sep 2021 13:12:37 +0300 Subject: [PATCH] - Achievement input at user card form. - Achievement at user view with img --- .../card/controllers/UserCardController.php | 8 +++++ backend/modules/card/models/UserCard.php | 29 +++++++++++++++++++ .../modules/card/views/user-card/_form.php | 14 +++++++++ backend/modules/card/views/user-card/view.php | 15 +++++++++- common/models/AchievementUserCard.php | 2 +- common/models/UserCard.php | 9 ++++++ 6 files changed, 75 insertions(+), 2 deletions(-) diff --git a/backend/modules/card/controllers/UserCardController.php b/backend/modules/card/controllers/UserCardController.php index d618438..4524d46 100755 --- a/backend/modules/card/controllers/UserCardController.php +++ b/backend/modules/card/controllers/UserCardController.php @@ -3,6 +3,7 @@ namespace backend\modules\card\controllers; use common\classes\Debug; +use common\models\AchievementUserCard; use common\models\AdditionalFields; use common\models\CardSkill; use common\Models\ChangeHistory; @@ -104,6 +105,12 @@ class UserCardController extends Controller ]); $skills = CardSkill::find()->where(['card_id' => $id])->with('skill')->all(); + $achievements = + AchievementUserCard::find()->where(['user_card_id' => $id]) + ->innerJoinWith(['achievement' => function($query) { + $query->andWhere(['status' => \common\models\Achievement::STATUS_ACTIVE]); + }]) + ->all(); $id_current_user = $this->findModel($id)->id_user; $changeDataProvider = new ActiveDataProvider([ @@ -117,6 +124,7 @@ class UserCardController extends Controller 'model' => $this->findModel($id), 'modelFieldValue' => $dataProvider, 'skills' => $skills, + 'achievements' => $achievements, 'userData' => User::findOne($id_current_user), 'changeDataProvider' => $changeDataProvider, ]); diff --git a/backend/modules/card/models/UserCard.php b/backend/modules/card/models/UserCard.php index fed5613..997a554 100755 --- a/backend/modules/card/models/UserCard.php +++ b/backend/modules/card/models/UserCard.php @@ -2,7 +2,9 @@ namespace backend\modules\card\models; +use backend\modules\achievements\models\Achievement; use Common\Behaviors\LogBehavior; +use common\models\AchievementUserCard; use Yii; use backend\modules\settings\models\Skill; use common\classes\Debug; @@ -16,6 +18,7 @@ class UserCard extends \common\models\UserCard { public $fields; public $skill; + public $achievements; public function init() { @@ -63,6 +66,19 @@ class UserCard extends \common\models\UserCard if (!empty($skill)) { $this->skill = $skill; } + + $achievements = ArrayHelper::getColumn(AchievementUserCard::find() + ->where(['user_card_id' => \Yii::$app->request->get('id')]) + ->innerJoinWith(['achievement' => function($query) { + $query->andWhere(['status' => \common\models\Achievement::STATUS_ACTIVE]); + }]) + ->all(), + 'achievement_id' + ); + + if (!empty($achievements)) { + $this->achievements = $achievements; + } } public function behaviors() @@ -118,6 +134,19 @@ class UserCard extends \common\models\UserCard $skill->save(); } } + + if(is_array($post['achievements'])){ + AchievementUserCard::deleteAll(['user_card_id' => $this->id]); + + foreach ($post['achievements'] as $item) { + $achCard = new AchievementUserCard(); + $achCard->user_card_id = $this->id; + $achCard->achievement_id = $item; + + $achCard->save(); + } + } + parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub } } diff --git a/backend/modules/card/views/user-card/_form.php b/backend/modules/card/views/user-card/_form.php index 897bb98..5bbcecb 100755 --- a/backend/modules/card/views/user-card/_form.php +++ b/backend/modules/card/views/user-card/_form.php @@ -2,6 +2,7 @@ use asmoday74\ckeditor5\EditorClassic; use common\classes\Debug; +use common\models\Achievement; use kartik\select2\Select2; use mihaildev\elfinder\InputFile; use unclead\multipleinput\MultipleInput; @@ -175,6 +176,19 @@ use yii\widgets\ActiveForm; 'language' => 'ru', ] ]); ?> +
+
+ field($model, 'achievements')->widget(Select2::class, + [ + 'data' => \yii\helpers\ArrayHelper::map(Achievement::find()->where(['status' => Achievement::STATUS_ACTIVE])->all(),'id', 'title'), + 'options' => ['placeholder' => '...','class' => 'form-control', 'multiple' => true], + 'pluginOptions' => [ + 'allowClear' => true + ], + ] + ); ?> +
+
diff --git a/backend/modules/card/views/user-card/view.php b/backend/modules/card/views/user-card/view.php index 06d5385..5a89d5f 100755 --- a/backend/modules/card/views/user-card/view.php +++ b/backend/modules/card/views/user-card/view.php @@ -8,7 +8,9 @@ use yii\widgets\DetailView; /* @var $model backend\modules\card\models\UserCard */ /* @var $userData common\models\User */ /* @var $skills \common\models\CardSkill */ +/* @var $achievements \common\models\AchievementUserCard */ /* @var $skill \common\models\Skill */ +/* @var $achievement \common\models\Achievement */ /* @var $modelFieldValue yii\data\ActiveDataProvider */ /* @var $changeDataProvider yii\data\ActiveDataProvider */ @@ -91,11 +93,22 @@ $this->params['breadcrumbs'][] = $this->title; ]) ?>

Навыки

- name; ?> +

Достижения

+ + + $achievement['achievement']->img, 'height' => '50px', 'alt'=> $achievement['achievement']->title] + ) ?> + title; ?> + + +

Дополнительные сведения

hasOne(Status::class, ['id' => 'status']); } + /** + * @return \yii\db\ActiveQuery + */ + public function getAchievements(): \yii\db\ActiveQuery + { + return $this->hasMany(AchievementUserCard::class, ['user_card_id' => 'id'])->with('achievement'); + } + public function getGenders() { return [