예제 #1
0
 public function saveAction()
 {
     if (!$this->isXmlHttpRequest()) {
         throw new \Exception('Неверный тип запроса!');
     }
     $user = $this->session->getCurrentUser();
     $data = $this->request->getPost();
     if (!isset($data['id']) || !isset($data['projects_id']) || !isset($data['name']) || !isset($data['description']) || !isset($data['deadline']) || !isset($data['task_priority_id'])) {
         throw new \Exception('Неверный тип запроса!');
     }
     $data['id'] = (int) $data['id'];
     $data['projects_id'] = (int) $data['projects_id'];
     $data['name'] = trim(strip_tags($data['name']));
     $data['description'] = trim(strip_tags($data['description']));
     $data['deadline'] = trim($data['deadline']);
     $data['task_priority_id'] = (int) $data['task_priority_id'];
     if ($data['name'] == '' || $data['description'] == '' || $data['deadline'] == '') {
         throw new \Exception('Не заполнены обязательные поля!');
     }
     $tasksModel = new Tasks();
     $oldTask = array();
     if ($data['id'] > 0) {
         $oldTask = $tasksModel->getTasksDataById((int) $data['id']);
         if ($user->roles_id == 2) {
             if ($oldTask['task_status_id'] != 1) {
                 throw new Exception('Доступ запрещен!');
             }
             if (isset($data['task_status_id'])) {
                 unset($data['task_status_id']);
             }
             if (isset($data['initiator'])) {
                 unset($data['initiator']);
             }
             if (isset($data['date_added'])) {
                 unset($data['date_added']);
             }
         }
     }
     if (isset($data['add_file'])) {
         unset($data['add_file']);
     }
     $tasks_id = $tasksModel->save($data);
     $task = $tasksModel->getTasksDataById($tasks_id);
     if (!is_null($this->session->getUploadedFiles())) {
         $filesModel = new Files();
         foreach ($this->session->getUploadedFiles() as $filesId) {
             $filesModel->update(array('projects_id' => $task['projects_id'], 'tasks_id' => $task['id']), array('id' => (int) $filesId));
         }
         $this->session->unsetUploadedFiles();
     }
     if ($data['id'] > 0) {
         $this->view->task = $task;
     } else {
         if ($data['projects_id'] > 0) {
             $projectsModel = new Projects();
             $this->view->project = $projectsModel->getProjectsDataForEdit($data['projects_id']);
         } else {
             $this->view->task = $task;
         }
     }
     if ($data['id'] == 0) {
         $usersModel = new Users();
         $usersModel->setWhere(array('id' => 1));
         $result = $usersModel->select();
         $superAdmin = $result[0];
         $to = $superAdmin['user'];
         $subject = 'Поступила новая задача!';
         $message = 'Новая задача : ' . $task['name'] . "\n";
         $message .= 'По проекту: ' . ($task['p_name'] ? $task['p_name'] : 'Без проекта') . "\n";
         $message .= 'Приоритет: ' . $task['tp_name'] . "\n";
         $message .= 'Закончить до: ' . $task['deadline'] . "\n";
         $message .= 'Описание: ' . "\n";
         $message .= $task['description'];
         Mail::instance()->send($to, $subject, $message);
     }
     if ($data['id'] > 0 && isset($data['task_status_id']) && $data['task_status_id'] != $oldTask['task_status_id']) {
         $to = array();
         $usersModel = new Users();
         $usersModel->setWhere(array('id' => 1));
         $result1 = $usersModel->select();
         $to[] = $result1[0]['user'];
         $usersModel->setWhere(array('id' => (int) $task['executors_id']));
         $result2 = $usersModel->select();
         if ($result2 && $result2[0]['user'] != $result1[0]['user']) {
             $to[] = $result2[0]['user'];
         }
         $usersModel->setWhere(array('id' => (int) $task['initiator']));
         $result3 = $usersModel->select();
         $to[] = $result3[0]['user'];
         $subject = 'Изменение статуса задачи';
         $message = 'По задаче "' . $task['name'];
         $message .= '" сменился статус на ' . $task['ts_name'] . "\n";
         $message .= 'По проекту: ' . ($task['p_name'] ? $task['p_name'] : 'Без проекта');
         Mail::instance()->send($to, $subject, $message);
     }
     $this->view->user = $user;
 }