'', 'response' => [], 'error' => []]; /** * @param $action * @return bool * @throws \yii\web\HttpException */ public function beforeAction($action) { parent::beforeAction($action); $key = Yii::$app->request->headers->get('uuid-key'); $this->checkAuth($key); Yii::$app->response->format = Response::FORMAT_JSON; $login = Yii::$app->request->post('login'); $password = Yii::$app->request->post('password'); $this->response['login'] = $login; $this->user = $this->getAccount($login, $password); return true; } /** * Получение данных * * @return array */ public function actionGetardata() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } //objects /** @var TargetObject $object */ foreach(TargetObject::findAll(['status' => 1]) as $object) { $this->response['response']['objects'][] = $object->asArray(); } //scenarios $scenarios = []; foreach (Scenario::find()->all() as $scenario) { $scenarioArr = $scenario->asArray(); $stages = []; /** @var Scenario $scenario */ foreach ($scenario->stages as $stage) { $stages[] = $stage->asArray(true); } $scenarioArr['stages'] = $stages; $scenarios[] = $scenarioArr; } $this->response['response']['scenarios'] = $scenarios; //all_types $typesArr = Tasktypes::find() ->select(['id', 'name', 'scenario_id']) ->where(['company' => $this->user->company]) ->all() ; $this->response['response']['all_types'] = $typesArr; return $this->response; } /** * Добавление объекта. * * @return array */ public function actionAddobject() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $object = new TargetObject(); $object->fillFromArray($params); if ($object->save()) { $this->response['response'] = $object->id; } else { $this->response['error'] = self::SAVE_ERROR; } return $this->response; } /** * Редактирование объекта. * * @return array */ public function actionEditobject() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $this->update($params, TargetObject::class); return $this->response; } /** * Удаление объекта. * * @throws \Throwable * @return array */ public function actionRemoveobject() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $objectId = (int)Yii::$app->request->post('id'); //Ищем все задачи с присланным objectId $tasks = Tasks::find()->where(['object_id' => $objectId])->all(); //Обнуляем все эти objectId foreach ($tasks as $task) { $task->object_id = null; $task->save(); } //Делаем объект не активным $object = TargetObject::findOne($objectId); if (!$object) { $this->response['error'] = 'Объект не найден'; } else { $object->status = 0; $object->save(); $this->response['response'] = 'Объект удалён'; } return $this->response; } /** * Добавление этапа. * * @return array */ public function actionAddstage() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $stage = new Stage(); $stage->fillFromArray($params); if ($stage->save()) { $this->response['response'] = $stage->id; } else { $this->response['error'] = self::SAVE_ERROR; } return $this->response; } /** * Редактирование этапа. * * @return array */ public function actionEditstage() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $this->update($params, Stage::class); return $this->response; } /** * Удаление этапа. * * @throws \Throwable * @return array */ public function actionRemovestage() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $stageId = (int)Yii::$app->request->post('id'); $stage = Stage::findOne($stageId); if ($stage) { $stage->delete(); $this->response['response'] = 'Этап удалён'; } else { $this->response['error'] = 'Этап не найден'; } return $this->response; } /** * Добавление сценария. * * @return array */ public function actionAddscenario() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $scenario = new Scenario(); $scenario->fillFromArray($params); if ($scenario->save()) { $this->response['response'] = $scenario->id; } else { $this->response['error'] = self::SAVE_ERROR; } return $this->response; } /** * Редактирование сценария. * * @return array */ public function actionEditscenario() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $this->update($params, Scenario::class); return $this->response; } /** * Удаление сценария. * * @throws \Throwable * @return array */ public function actionRemovescenario() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $scenarioId = (int)Yii::$app->request->post('id'); $scenario = Scenario::findOne($scenarioId); if ($scenario) { $scenario->delete(); $this->response['response'] = 'Сценарий удалён'; } else { $this->response['error'] = 'Сценарий не найден'; } return $this->response; } /** * Добавление медиа. * * @return array */ public function actionAddmedia() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); if ($params) { $media = new Media(); $media->fillFromArray($params); if ($media->save()) { $this->response['response'] = $media->id; } else { $this->response['error'] = self::SAVE_ERROR; } } else { $this->response['error'] = self::INVALID_JSON; } return $this->response; } /** * Редактирование медиа. * * @return array */ public function actionEditmedia() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $this->update($params, Media::class); return $this->response; } /** * Удаление медиа. * * @throws \Throwable * @return array */ public function actionRemovemedia() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $mediaId = (int)Yii::$app->request->post('id'); $media = Media::findOne($mediaId); if ($media) { $media->delete(); $this->response['response'] = 'Медиаданные удалёны'; } else { $this->response['error'] = 'Медиаданные не найдены'; } return $this->response; } /** * Получение списка команд\задач, относящихся к компании пользователя и имеющие принадлежность к сценарию * * @return array */ public function actionCommands() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } //Запросы 30 секунд выполняется. // $tasks = Tasks::find()->all(); // // /** @var Tasks $task */ // foreach ($tasks as $task) { // $companyId = $task->accounts->company_id; // $scenarioId = $task->tasktypes->scenario_id; // if ($companyId == $this->user->company && $scenarioId) { // $this->response['response'][] = [ // 'command_id' => $task->id, // 'name_command' => $task->tasktypes->name, // 'scenario_id' => $task->tasktypes->scenario_id, // ]; // } // } // // return $this->response; $query = new Query(); $query->select('tt.id as command_id, ttt.name as name_command, tt.scenario_id') ->from(Accounts::tableName() . ' ta') ->leftJoin(Tasks::tableName() . ' tt', 'ta.id = tt.assignees_arr') ->innerJoin(Tasktypes::tableName() . ' ttt', 'ttt.id = tt.type') ->where(['ta.company' => $this->user->company]) ->andWhere(['>', 'tt.scenario_id', 0]) ; $this->response['response'] = $query->all(); return $this->response; } /** * Присваивает задаче сценарий * * @return array * @throws BadRequestHttpException */ public function actionSetscenario() { if (!$this->user->isAdmin()) { $this->response['error'] = self::ADMIN_ONLY_ERROR; return $this->response; } $params = json_decode(Yii::$app->request->post('params'), true); $taskTypeIds = $params['tasktypes']; $scenarioId = (int)$params['id_scenario']; if (!($params && $taskTypeIds && $scenarioId)) { throw new BadRequestHttpException('', 400); } $taskTypes = Tasktypes::findAll(['scenario_id' => $scenarioId]); foreach ($taskTypes as $type) { $type->scenario_id = null; $type->save(); } $taskTypes = Tasktypes::findAll($taskTypeIds); foreach ($taskTypes as $taskType) { $taskType->scenario_id = $scenarioId; $taskType->save(); $this->response['response']['tasktypes'][] = $taskType->id; } return $this->response; } public function getAccount($login, $password) { return Accounts::findOne(['login' => $login, 'password' => md5($password)]); } /** * Получение команд\задач, по их ID * * @return array */ public function actionCommand() { $id = Yii::$app->request->post('id'); $task = Tasks::findOne($id); if ($task) { $result = []; $result['command_id'] = $task->id; $result['name_command'] = $task->tasktypes->name; $result['scenario_id'] = $task->tasktypes->scenario_id ?? 0; if ($scenario = Scenario::findOne($result['scenario_id'])) { $result['name'] = $scenario->name; $result['stages'] = []; foreach ($scenario->stages as $stage) { $stageArr = $stage->asArray(true); if ($stage->object) { $stageArr['object']['id'] = $stage->object->id; $stageArr['object']['object_name'] = $stage->object->name; $stageArr['object']['name_display'] = $stage->object->name_display; $stageArr['object']['target_name_xml'] = $stage->object->target_name_xml; $stageArr['object']['type'] = $stage->object->type; } $result['stages'][] = $stageArr; } } $this->response['response'] = $result; } else { $this->response['error'] = self::OBJECT_NOT_FOUND; } return $this->response; } /** * Обновляет один из объектов (TargetObject, Scenario, Stage, Media). Если объект не существует * заполняет $this->response['error']. В случае успеха записывает в $this->response['response'] id объекта. * Ксасс указанный в $type содлжен содержать метод fillFromArray($params) * * @param array $params * @param string $type * @return bool */ private function update($params, $type = '') { switch ($type) { case TargetObject::class: $object = TargetObject::findOne($params['id']); break; case Scenario::class: $object = Scenario::findOne($params['id']); break; case Stage::class: $object = Stage::findOne($params['id']); break; default: $object = Media::findOne($params['id']); break; } if (!$object) { $this->response['error'] = self::OBJECT_NOT_FOUND; return false; } $object->fillFromArray($params); if ($object->save()) { $this->response['response'] = $object->id; } else { $this->response['error'] = self::SAVE_ERROR; } return true; } }