2022-01-14 10:09:02 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\services;
|
|
|
|
|
|
|
|
use common\models\Manager;
|
|
|
|
use common\models\ManagerEmployee;
|
2023-04-13 01:09:35 +03:00
|
|
|
use common\models\Position;
|
2022-03-21 14:56:21 +03:00
|
|
|
use common\models\UserCard;
|
2022-12-27 11:45:28 +03:00
|
|
|
use common\models\UserCardPortfolioProjects;
|
2022-03-21 14:56:21 +03:00
|
|
|
use frontend\modules\api\models\ProfileSearchForm;
|
|
|
|
use Yii;
|
2022-03-21 19:08:39 +03:00
|
|
|
use yii\web\ServerErrorHttpException;
|
2022-01-14 10:09:02 +03:00
|
|
|
|
|
|
|
class ProfileService
|
|
|
|
{
|
2022-12-27 11:45:28 +03:00
|
|
|
public static function getPortfolioProjects($card_id)
|
|
|
|
{
|
|
|
|
/** @var UserCardPortfolioProjects[] $portfolioProjects */
|
|
|
|
$portfolioProjects = UserCardPortfolioProjects::find()
|
|
|
|
->where(['card_id' => $card_id])
|
|
|
|
->all();
|
|
|
|
|
|
|
|
$array = [];
|
|
|
|
if (!empty($portfolioProjects)) {
|
|
|
|
foreach ($portfolioProjects as $project) {
|
|
|
|
array_push(
|
|
|
|
$array,
|
|
|
|
[
|
|
|
|
'id' => $project->id,
|
|
|
|
'title' => $project->title,
|
|
|
|
'description' => $project->description,
|
|
|
|
'main_stack' => $project->skill->name,
|
|
|
|
'additional_stack' => $project->additional_stack,
|
|
|
|
'link' => $project->link
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2022-03-21 19:08:39 +03:00
|
|
|
/**
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
2022-10-19 15:28:09 +03:00
|
|
|
public static function getMainData($user_id): array
|
2022-03-21 19:08:39 +03:00
|
|
|
{
|
|
|
|
$userCard = UserCard::findOne(['id_user' => $user_id]);
|
|
|
|
if (empty($userCard)) {
|
2022-10-19 15:28:09 +03:00
|
|
|
throw new ServerErrorHttpException('Profile not found!');
|
2022-03-21 19:08:39 +03:00
|
|
|
}
|
|
|
|
return array('fio' => $userCard->fio,
|
|
|
|
'photo' => $userCard->photo,
|
|
|
|
'gender' => $userCard->gender,
|
|
|
|
'level' => $userCard->level,
|
|
|
|
'years_of_exp' => $userCard->years_of_exp,
|
|
|
|
'specification' => $userCard->specification,
|
|
|
|
'position_name' => $userCard->position->name);
|
|
|
|
}
|
|
|
|
|
2022-10-19 15:28:09 +03:00
|
|
|
public static function getProfile($id, $request): ?array
|
2022-03-21 14:56:21 +03:00
|
|
|
{
|
|
|
|
$searchModel = new ProfileSearchForm();
|
|
|
|
$searchModel->attributes = $request;
|
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
return $searchModel->byId();
|
|
|
|
}
|
|
|
|
return $searchModel->byParams();
|
|
|
|
}
|
|
|
|
|
2023-10-11 23:19:30 +03:00
|
|
|
public static function getProfileById($id): ?array
|
|
|
|
{
|
|
|
|
$searchModel = new ProfileSearchForm();
|
|
|
|
$searchModel->id = $id;
|
|
|
|
return $searchModel->byId();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
/**
|
2022-10-19 15:28:09 +03:00
|
|
|
* @throws ServerErrorHttpException
|
2022-03-21 14:56:21 +03:00
|
|
|
*/
|
|
|
|
public static function getProfileWithReportPermission($user_card_id): ?array
|
|
|
|
{
|
|
|
|
if (UserCard::find()->where(['id' => $user_card_id])->exists()) {
|
|
|
|
|
|
|
|
$searchModel = new ProfileSearchForm();
|
|
|
|
$searchModel->id = $user_card_id;
|
|
|
|
$profile = $searchModel->byId();
|
|
|
|
|
|
|
|
self::addPermission($profile, $user_card_id);
|
|
|
|
return $profile;
|
|
|
|
}
|
2022-10-19 15:28:09 +03:00
|
|
|
throw new ServerErrorHttpException('There is no user with this id');
|
2022-03-21 14:56:21 +03:00
|
|
|
}
|
|
|
|
|
2023-04-13 01:09:35 +03:00
|
|
|
/**
|
|
|
|
* @return array|\yii\db\ActiveRecord[]
|
|
|
|
*/
|
|
|
|
public static function getPositionsList()
|
|
|
|
{
|
|
|
|
return Position::find()->all();
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function addPermission(&$profile, $user_card_id)
|
|
|
|
{
|
|
|
|
$searcherCardID = self::getSearcherCardID(Yii::$app->user->getId());
|
|
|
|
if (self::checkReportPermission($user_card_id, $searcherCardID)) {
|
|
|
|
$profile += ['report_permission' => '1'];
|
|
|
|
} else {
|
|
|
|
$profile += ['report_permission' => '0'];
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 10:09:02 +03:00
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function getSearcherCardID($user_id): int
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
2022-03-21 14:56:21 +03:00
|
|
|
return UserCard::findOne(['id_user' => $user_id])->id;
|
2022-01-14 10:09:02 +03:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function checkReportPermission($user_card_id, $searcherCardID): bool
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
2022-03-21 14:56:21 +03:00
|
|
|
if (self::isMyProfile($user_card_id, $searcherCardID)
|
|
|
|
or self::isMyEmployee($user_card_id, $searcherCardID)) {
|
2022-01-14 10:09:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function isMyProfile($user_card_id, $searcherCardID): bool
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
2022-03-21 14:56:21 +03:00
|
|
|
if ($user_card_id == $searcherCardID) {
|
2022-01-14 10:09:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function isMyEmployee($user_card_id, $searcherCardID): bool
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
2022-03-21 14:56:21 +03:00
|
|
|
if (!self::amIManager($searcherCardID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::isMyEmployer($user_card_id, $searcherCardID)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-01-14 10:09:02 +03:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function amIManager($searcherCardID): bool
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
2022-03-21 14:56:21 +03:00
|
|
|
if (Manager::find()->where(['user_card_id' => $searcherCardID])->exists()) {
|
2022-01-14 10:09:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:56:21 +03:00
|
|
|
private static function isMyEmployer($user_card_id, $searcherCardID): bool
|
2022-01-14 10:09:02 +03:00
|
|
|
{
|
2022-03-21 14:56:21 +03:00
|
|
|
$manager = Manager::find()->where(['user_card_id' => $searcherCardID])->one();
|
2022-01-14 10:09:02 +03:00
|
|
|
$exist = ManagerEmployee::find()
|
2022-03-21 14:56:21 +03:00
|
|
|
->where(['manager_id' => $manager->id, 'user_card_id' => $user_card_id])
|
2022-01-14 10:09:02 +03:00
|
|
|
->exists();
|
|
|
|
|
|
|
|
if ($exist) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|