Exemplo n.º 1
0
 /**
  * Add message
  *
  * @access public
  * @param void
  * @return null
  */
 function add()
 {
     $this->setTemplate('add_message');
     if (!ProjectMessage::canAdd(logged_user(), active_project())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('message'));
     }
     // if
     $message = new ProjectMessage();
     tpl_assign('message', $message);
     $message_data = array_var($_POST, 'message');
     if (!is_array($message_data)) {
         $message_data = array('milestone_id' => array_var($_GET, 'milestone_id'));
         // array
     }
     // if
     tpl_assign('message_data', $message_data);
     if (is_array(array_var($_POST, 'message'))) {
         try {
             $uploaded_files = ProjectFiles::handleHelperUploads(active_project());
         } catch (Exception $e) {
             $uploaded_files = null;
         }
         // try
         try {
             $message->setFromAttributes($message_data);
             $message->setProjectId(active_project()->getId());
             // Options are reserved only for members of owner company
             if (!logged_user()->isMemberOfOwnerCompany()) {
                 $message->setIsPrivate(false);
                 $message->setIsImportant(false);
                 $message->setCommentsEnabled(true);
                 $message->setAnonymousCommentsEnabled(false);
             }
             // if
             DB::beginWork();
             $message->save();
             $message->subscribeUser(logged_user());
             $message->setTagsFromCSV(array_var($message_data, 'tags'));
             if (is_array($uploaded_files)) {
                 foreach ($uploaded_files as $uploaded_file) {
                     $message->attachFile($uploaded_file);
                     $uploaded_file->setIsPrivate($message->isPrivate());
                     $uploaded_file->setIsVisible(true);
                     $uploaded_file->setExpirationTime(EMPTY_DATETIME);
                     $uploaded_file->save();
                 }
                 // if
             }
             // if
             ApplicationLogs::createLog($message, active_project(), ApplicationLogs::ACTION_ADD);
             DB::commit();
             // Try to send notifications but don't break submission in case of an error
             try {
                 $notify_people = array();
                 $project_companies = active_project()->getCompanies();
                 foreach ($project_companies as $project_company) {
                     $company_users = $project_company->getUsersOnProject(active_project());
                     if (is_array($company_users)) {
                         foreach ($company_users as $company_user) {
                             if (array_var($message_data, 'notify_company_' . $project_company->getId()) == 'checked' || array_var($message_data, 'notify_user_' . $company_user->getId())) {
                                 $message->subscribeUser($company_user);
                                 // subscribe
                                 $notify_people[] = $company_user;
                             }
                             // if
                         }
                         // if
                     }
                     // if
                 }
                 // if
                 Notifier::newMessage($message, $notify_people);
                 // send notification email...
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success add message', $message->getTitle()));
             $this->redirectTo('message');
             // Error...
         } catch (Exception $e) {
             DB::rollback();
             if (is_array($uploaded_files)) {
                 foreach ($uploaded_files as $uploaded_file) {
                     $uploaded_file->delete();
                 }
                 // foreach
             }
             // if
             $message->setNew(true);
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }