Редактирование skills

This commit is contained in:
Glambertessa 2019-11-18 16:14:06 +03:00
parent 6037c69b85
commit 028cce3bef
9 changed files with 134 additions and 8 deletions

View File

@ -2,6 +2,8 @@
use yii\grid\GridView;
$this->title = 'Доступы';
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [

View File

@ -3,11 +3,10 @@
namespace frontend\modules\card\controllers;
use common\classes\Debug;
use common\models\CardSkill;
use common\models\FieldsValueNew;
use Yii;
use common\models\UserCard;
use frontend\modules\card\models\UserCard;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
@ -26,7 +25,7 @@ class UserCardController extends Controller
$id_user = Yii::$app->user->id;
$result = UserCard::find()->where(['id_user' => $id_user])->asArray()->all();
if($result){
if($result) {
$id = $result[0]['id'];
$dataProvider = new ActiveDataProvider([
'query' => FieldsValueNew::find()
@ -49,6 +48,25 @@ class UserCardController extends Controller
}
}
/**
* Updates an existing UserCard model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Finds the Product model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.

View File

@ -0,0 +1,45 @@
<?php
namespace frontend\modules\card\models;
use common\models\CardSkill;
use yii\helpers\ArrayHelper;
class UserCard extends \common\models\UserCard
{
public $fields;
public $skill;
public function init()
{
parent::init();
$skill = ArrayHelper::getColumn(
CardSkill::find()->where(['card_id' => \Yii::$app->request->get('id')])->all(),
'skill_id'
);
if (!empty($skill)) {
$this->skill = $skill;
}
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('UserCard');
if ($post['skill']) {
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
}
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}

View File

@ -0,0 +1,39 @@
<?php
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\Skill;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $model frontend\modules\card\models\UserCard */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="user-card-form">
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-xs-12">
<?= $form->field($model, 'skill')->widget(
Select2::class,
[
'data' => ArrayHelper::map(Skill::find()->all(), 'id', 'name'),
'options' => ['placeholder' => '...', 'class' => 'form-control', 'multiple' => true],
'pluginOptions' => [
'allowClear' => true
],
]
)->label('Навыки'); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -1 +1,7 @@
<?= $info ?>
<?php
/* @var $info */
$this->title = 'Профиль';
echo $info;

View File

@ -0,0 +1,14 @@
<?php
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\UserCard */
$this->title = "Редактировать профиль";
?>
<div class="user-card-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -9,7 +9,7 @@ use yii\widgets\DetailView;
/* @var $skill \common\models\Skill */
/* @var $modelFildValue yii\data\ActiveDataProvider */
$this->title = 'Профиль';
?>
<div class="user-card-view">
@ -48,7 +48,6 @@ use yii\widgets\DetailView;
}
],
['label' => 'Добвлен', 'attribute' => 'created_at',],
['label' => 'Изменен', 'attribute' => 'updated_at',],
],
]);
?>
@ -58,6 +57,9 @@ use yii\widgets\DetailView;
<?php foreach ($skills as $skill) : ?>
<span class="btn btn-default btn-sm"><?= $skill['skill']->name; ?></span>
<?php endforeach; ?>
<?= Html::a('Добавить', ['/card/user-card/update', 'id' => $model->id], ['class' => 'btn btn-success']); ?>
<h2>Дополнительные сведения</h2>
<?= GridView::widget([