示例#1
0
 public function add()
 {
     $this->view = null;
     if (isset($_POST['api_key'])) {
         Utils::validateAPIKey();
         $user = User::find($_POST['creator']);
         $userId = $user->getId();
     } else {
         Utils::checkLogin();
         $userId = Session::uid();
     }
     if (!$userId) {
         header('HTTP/1.1 401 Unauthorized', true, 401);
         echo json_encode(array('error' => "Invalid parameters !"));
         return;
     }
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->view = new AddJobView();
         parent::run();
         return;
     }
     $this->view = null;
     $journal_message = '';
     $workitem_added = false;
     $nick = '';
     $workitem = new WorkItem();
     Utils::initUserById($userId);
     $user = new User();
     $user->findUserById($userId);
     $nick = $user->getNickname();
     $runner_id = Project::isAllowedRunnerForProject($user->getId(), $_REQUEST['project_id']) ? $userId : '';
     $itemid = $_REQUEST['itemid'];
     $summary = $_REQUEST['summary'];
     $project_id = $_REQUEST['project_id'];
     $labels = $_REQUEST['labels'];
     $status = Project::isAllowedRunnerForProject($user->getId(), $_REQUEST['project_id']) || $user->getIs_admin() == 1 && $user->getIs_runner() ? $_REQUEST['status'] : 'Suggestion';
     $notes = $_REQUEST['notes'];
     $is_expense = $_REQUEST['is_expense'];
     $is_rewarder = $_REQUEST['is_rewarder'];
     $is_internal = $_REQUEST['is_internal'];
     $fileUpload = $_REQUEST['fileUpload'];
     $assigned_id = 0;
     if ((int) $_REQUEST['assigned']) {
         $assignedUser = User::find($_REQUEST['assigned']);
         if ($assignedUser->isInternal()) {
             $assigned_id = $assignedUser->getId();
         }
     }
     if (!empty($_POST['itemid'])) {
         $workitem->loadById($_POST['itemid']);
     } else {
         $workitem->setCreatorId($userId);
         $workitem_added = true;
     }
     $workitem->setSummary($summary);
     $labelsArr = explode(',', $labels);
     $workitem->setRunnerId($runner_id);
     $workitem->setProjectId($project_id);
     $workitem->setStatus($status);
     $workitem->setNotes($notes);
     $workitem->setWorkitemLabels($labelsArr);
     $workitem->setIs_internal($is_internal);
     $workitem->setAssigned_id($assigned_id);
     $workitem->save();
     $related = $this->getRelated($notes);
     Notification::massStatusNotify($workitem);
     if ($assigned_id) {
         $emailTemplate = 'job-assigned';
         $data = array('job_id' => $workitem->getId(), 'summary' => $workitem->getSummary(), 'assigner' => $user->getNickname(), 'assigned' => $assignedUser->getNickname());
         $senderEmail = 'Worklist - ' . $user->getNickname() . ' <*****@*****.**> ';
         Utils::sendTemplateEmail($assignedUser->getUsername(), $emailTemplate, $data, $senderEmail);
     }
     // if files were uploaded, update their workitem id
     $file = new File();
     // update images first
     if (isset($fileUpload['uploads'])) {
         foreach ($fileUpload['uploads'] as $image) {
             $file->findFileById($image);
             $file->setWorkitem($workitem->getId());
             $file->save();
         }
     }
     if (empty($_POST['itemid'])) {
         $bid_fee_itemid = $workitem->getId();
         $journal_message .= "\\\\#" . $bid_fee_itemid . ' created by @' . $nick . ' Status set to ' . $status;
         if (!empty($_POST['files'])) {
             $files = explode(',', $_POST['files']);
             foreach ($files as $file) {
                 $sql = 'UPDATE `' . FILES . '` SET `workitem` = ' . $bid_fee_itemid . ' WHERE `id` = ' . (int) $file;
                 mysql_query($sql);
             }
         }
     } else {
         $bid_fee_itemid = $itemid;
         $journal_message .= '\\#' . $bid_fee_itemid . ' updated by ' . $nick . 'Status set to ' . $status;
     }
     $journal_message .= "{$related}. ";
     // don't send any journal notifications for DRAFTS
     if (!empty($journal_message) && $status != 'Draft') {
         Utils::systemNotification(stripslashes($journal_message));
         if ($workitem_added) {
             $options = array('type' => 'workitem-add', 'workitem' => $workitem);
             $data = array('notes' => $notes, 'nick' => $nick, 'status' => $status);
             Notification::workitemNotifyHipchat($options, $data);
         }
         // workitem mentions
         $matches = array();
         if (preg_match_all('/@(\\w+)/', $workitem->getNotes(), $matches, PREG_SET_ORDER)) {
             foreach ($matches as $mention) {
                 // validate the username actually exists
                 if ($recipient = User::find($mention[1])) {
                     // exclude creator, designer, developer and followers
                     if ($recipient->getId() != $workitem->getRunnerId() && $recipient->getId() != $workitem->getMechanicId() && $recipient->getId() != $workitem->getCreatorId() && !$workitem->isUserFollowing($recipient->getId())) {
                         $emailTemplate = 'workitem-mention';
                         $data = array('job_id' => $workitem->getId(), 'summary' => $workitem->getSummary(), 'author' => $_SESSION['nickname'], 'text' => $workitem->getNotes(), 'link' => '<a href="' . WORKLIST_URL . $workitem->getId() . '">See the workitem</a>');
                         $senderEmail = 'Worklist - ' . $_SESSION['nickname'] . ' <*****@*****.**> ';
                         Utils::sendTemplateEmail($recipient->getUsername(), $emailTemplate, $data, $senderEmail);
                     }
                 }
             }
         }
     }
     // Notify Runners of new suggested task
     if ($status == 'Suggestion' && $project_id != '') {
         $options = array('type' => 'suggested', 'workitem' => $workitem, 'recipients' => array('projectRunners'));
         $data = array('notes' => $notes, 'nick' => $nick, 'status' => $status);
         Notification::workitemNotify($options, $data);
     }
     echo json_encode(array('return' => "Done!", 'workitem' => $workitem->getId()));
 }