/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/helpers/GtpHelper.php
244 строки
6 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\helpers; use common\components\crud\FormCrud; use common\components\LanguageSelector; use common\kdf\KApiKey; use common\search\base\BaseSearch; use common\validators\ErpValidator; use Yii; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; /** * Расширение функций для работы с проектами * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019 */ class GtpHelper { /** * Получить из входного параметра массив с полями GTP_ID и ERP_ID * @param $id * @param bool $throw * @return array|bool * @throws BadRequestHttpException */ public static function getId($id, $throw = true) { $id = trim($id); $gtp_id = null; $erp_id = null; if (strlen($id) == 36) { $validator = new ErpValidator(); if (!$validator->validate($id)) { if ($throw) { throw new BadRequestHttpException($validator->message); } else { return false; } } $erp_id = $id; } else { if (!is_numeric($id)) { if ($throw) { throw new BadRequestHttpException(Yii::t('app', 'Invalid number «{0}»', [$id])); } else { return false; } } $gtp_id = $id; } return [ 'erp_id' => $erp_id, 'gtp_id' => $gtp_id, ]; } /** * @param $now * @return string * @throws InvalidConfigException */ public static function refDate($now) { $time = strtotime($now); return ($time > DateHelper::MONTH) ? Yii::$app->formatter->asRelativeTime($time) : Yii::$app->formatter->asDate($time); } /** * Текущее приложение * * @param int $app_id * @return bool * @throws InvalidConfigException */ public static function isApp($app_id = KApiKey::ERP) { /** @var KApiKey $api */ $api = Yii::$app->get('api', false); return $api and $api->id == $app_id; } /** * Преобразовать массив результатов - индексировать по ключу * * @param array $array Массив результатов * @param string $column Название колонки * @param bool $is_array * @return array */ public static function indexBy($array, $column, $is_array = false) { $result = []; foreach ($array as $item) { if ($is_array) { $result[$item[$column]][$item['id']] = $item; } else { $result[$item->{$column}][$item->getId()] = $item; } } return $result; } /** * Преобразовать массив результатов - индексировать по ключу * * @param array $array Массив результатов * @param string $column Название колонки * @return array */ public static function indexByArray($array, $column) { $result = []; foreach ($array as $item) { $result[$item[$column]][] = $item; } return $result; } /** * Получить набор фильтров * * @param $query * @param $filters * @return array * @throws InvalidConfigException */ public static function getFilters($query, $filters) { $result = []; foreach ($filters as $filter) { /** @var FormCrud $crud */ $crud = new $filter(); /** @var BaseSearch $model */ $model = $crud->createModel(); $model->setQuery($query); $model->execute(); $result[] = [ 'crud' => $crud, 'model' => $model, ]; } return $result; } /** * Преобразовать массив ИД в GET параметр для select2 * @param array $ids * @return string */ public static function idToUrl($ids = []) { return urlencode(base64_encode(implode(',', $ids))); } /** * Возвращает метод TRUE - если работаем в административной части * @return bool */ public static function isDashboard() { return Yii::$app->id == 'app-backend'; } /** * Возвращает метод TRUE - если работаем на лицевой части * @return bool */ public static function isFrontend() { return Yii::$app->id == 'app-frontend'; } /** * Возвращает метод TRUE - если работаем в API части * @return bool */ public static function isApi() { return Yii::$app->id == 'api'; } /** * Обновление языка * @throws InvalidConfigException */ public static function updateLang() { /** @var LanguageSelector $languageSelector */ $languageSelector = Yii::$app->get('languageSelector'); $languageSelector->bootstrap(Yii::$app); } /** * Получить идентификатор языка * @return mixed|null * @throws InvalidConfigException */ public static function langId() { /** @var LanguageSelector $languageSelector */ $languageSelector = Yii::$app->get('languageSelector'); return ArrayHelper::getValue(array_flip($languageSelector->supportedLanguages), Yii::$app->language); } /** * Является ли текущая установка Box Version * @return bool * @throws \Exception */ public static function isBox() { return ArrayHelper::getValue(Yii::$app->params, 'gtp_type') != 'gtp'; } /** * @return bool * @throws \Exception */ public static function isSupportRus() { return ( ( Yii::$app->user->can('admin') or ClientHelper::isSupportRus()) and !GtpHelper::isBox() ); } }