guild/frontend/modules/api/models/ProfileSearchForm.php

106 lines
2.4 KiB
PHP
Raw Normal View History

2021-06-25 18:11:30 +03:00
<?php
namespace frontend\modules\api\models;
use backend\modules\card\models\UserCard;
2021-06-25 18:19:02 +03:00
use common\classes\Debug;
2021-06-25 18:11:30 +03:00
use yii\base\Model;
/**
* Class ProfileSearchForm
* @property integer $limit
* @property integer $offset
* @property integer $id
* @package frontend\modules\api\models
*/
class ProfileSearchForm extends Model
{
public $limit = 10;
public $offset = 0;
public $skills;
2021-08-12 12:34:37 +03:00
public $position_id;
2021-06-25 18:11:30 +03:00
public $id;
public function rules()
{
return [
2021-08-12 12:34:37 +03:00
[['id', 'limit', 'offset', 'position_id'], 'safe'],
2021-06-25 18:11:30 +03:00
[['skills'], 'checkIsArray'],
];
}
2021-11-30 16:54:04 +03:00
public function exclude($arr)
{
2022-12-23 17:42:35 +03:00
$ex = ['passport', 'resume', 'link_vk', 'link_telegram', 'email', 'salary'];
2021-11-30 16:54:04 +03:00
foreach ($ex as $remove) {
if (isset($arr[$remove])) {
unset($arr[$remove]);
}
}
return $arr;
}
2021-06-25 18:11:30 +03:00
public function checkIsArray()
{
if (!is_array($this->_task)) {
$this->addError('_task', 'X is not array!');
}
}
public function byId()
{
if ($this->id) {
2021-11-30 16:54:04 +03:00
return $this->exclude(UserCard::find()
2021-06-25 18:11:30 +03:00
->where(['id' => $this->id])
->with(['skillValues'])
->with(['achievements'])
2021-06-25 18:11:30 +03:00
->asArray()
2021-11-30 16:54:04 +03:00
->one());
2021-06-25 18:11:30 +03:00
}
return null;
}
public function byParams()
{
2021-08-12 12:35:59 +03:00
$model = UserCard::find();
2021-06-25 18:19:02 +03:00
2021-11-30 16:54:04 +03:00
if ($this->skills) {
2021-06-25 18:19:02 +03:00
$model->joinWith(['skillValues']);
2021-06-25 18:11:30 +03:00
$this->skills = explode(',', $this->skills);
$model->where(['card_skill.skill_id' => $this->skills]);
2021-07-13 13:45:44 +03:00
$model->having('COUNT(DISTINCT skill_id) = ' . count($this->skills));
2021-11-30 16:54:04 +03:00
} else {
2021-07-13 13:45:44 +03:00
$model->joinWith('skillValues');
2021-06-25 18:19:02 +03:00
}
2021-06-25 18:11:30 +03:00
$model->joinWith('achievements');
2021-08-12 12:34:37 +03:00
2021-09-15 16:43:14 +03:00
$model->andFilterWhere(['position_id' => $this->position_id]);
$model->andWhere(['status' => [4, 12]]);
$model->andWhere(['deleted_at' => null]);
2021-07-07 16:30:01 +03:00
//$model->groupBy('card_skill.card_id');
2021-07-13 13:45:44 +03:00
2021-11-30 16:54:04 +03:00
$res = $model->limit($this->limit)
2021-08-05 14:08:00 +03:00
->offset($this->offset)->orderBy('updated_at DESC')->asArray()->all();
2021-11-30 16:54:04 +03:00
if(!$res){
return [];
}
$resArr = [];
foreach ($res as $re){
$resArr[] = $this->exclude($re);
}
return $resArr;
2021-06-25 18:11:30 +03:00
}
}