guild/backend/modules/card/models/UserCard.php
2020-08-04 10:47:53 +03:00

134 lines
4.0 KiB
PHP
Executable File

<?php
namespace backend\modules\card\models;
use Common\Behaviors\LogBehavior;
use Yii;
use backend\modules\settings\models\Skill;
use common\classes\Debug;
use common\models\CardSkill;
use common\models\User;
use common\models\FieldsValue;
use common\models\FieldsValueNew;
use yii\helpers\ArrayHelper;
class UserCard extends \common\models\UserCard
{
public $fields;
public $skill;
public function init()
{
parent::init();
$fieldValue = FieldsValueNew::find()->where(
[
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_PROFILE,
]
)
->all();
$array = [];
if (!empty($fieldValue)) {
foreach ($fieldValue as $item) {
array_push(
$array,
[
'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,
'order' => null,
'field_name' => null,
'type_file' => null,
],
];
}
$skill = ArrayHelper::getColumn(
CardSkill::find()->where(['card_id' => \Yii::$app->request->get('id')])->all(),
'skill_id'
);
if (!empty($skill)) {
$this->skill = $skill;
}
}
public function behaviors()
{
return [
'log' => [
'class' => \common\behaviors\LogBehavior::class,
]
];
}
public function beforeSave($insert)
{
if(is_array(\Yii::$app->request->post('UserCard')))
{
$this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']);
}
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('UserCard');
if($post) {
if (isset($post['fields'])) {
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
foreach ($post['fields'] as $item) {
$item['value'] = urldecode($item['value']);
$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_PROFILE;
if (is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])) {
$fieldsValue->type_file = 'file';
} else {
$fieldsValue->type_file = 'text';
}
$fieldsValue->save();
}
}
if (is_array($post['fields'])) {
CardSkill::deleteAll(['card_id' => $this->id]);
if (is_array($post['skill']))
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
}
}
public static function getParameter($params, $key)
{
try {
return $params[$key];
} catch (\Exception $e) {
return '';
}
}
}