Example #1
0
 /**
  * Email the task log to assignees, task contacts, project contacts, and others
  * based upon the information supplied by the user.
  */
 public function email_log(&$log, $assignees, $task_contacts, $project_contacts, $others, $extras, $specific_user = 0)
 {
     global $AppUI, $locale_char_set, $w2Pconfig;
     $mail_recipients = array();
     $q = new w2p_Database_Query();
     if ((int) $this->task_id > 0 && (int) $this->task_project > 0) {
         $q->addTable('users', 'u');
         $q->leftJoin('contacts', 'c', 'c.contact_id = u.user_contact');
         $q->addQuery('c.contact_first_name, c.contact_last_name');
         $q->addWhere('u.user_id=' . (int) $log->task_log_creator);
         $row = $q->loadHash();
         $creatorname = htmlspecialchars_decode($row['contact_first_name']) . ' ' . htmlspecialchars_decode($row['contact_last_name']);
         $q->clear();
         if (isset($assignees) && $assignees == 'on') {
             $q->addTable('user_tasks', 'ut');
             $q->leftJoin('users', 'ua', 'ua.user_id = ut.user_id');
             $q->leftJoin('contacts', 'c', 'c.contact_id = ua.user_contact');
             $q->addQuery('c.contact_first_name, c.contact_last_name, c.contact_email');
             $q->addWhere('ut.task_id = ' . $this->task_id);
             if (!$AppUI->getPref('MAILALL')) {
                 $q->addWhere('ua.user_id <>' . (int) $AppUI->user_id);
             }
             $assigneeList = $q->loadList();
             $q->clear();
             foreach ($assigneeList as $myContact) {
                 $mail_recipients[$myContact['contact_email']] = mb_trim($myContact['contact_first_name'] . ' ' . $myContact['contact_last_name']);
             }
         }
         if (isset($task_contacts) && $task_contacts == 'on') {
             $q->addTable('task_contacts', 'tc');
             $q->leftJoin('contacts', 'c', 'c.contact_id = tc.contact_id');
             $q->addQuery('c.contact_first_name, c.contact_last_name, c.contact_email');
             $q->addWhere('tc.task_id = ' . $this->task_id);
             $contactList = $q->loadList();
             $q->clear();
             foreach ($contactList as $myContact) {
                 $mail_recipients[$myContact['contact_email']] = mb_trim($myContact['contact_first_name'] . ' ' . $myContact['contact_last_name']);
             }
         }
         if (isset($project_contacts) && $project_contacts == 'on') {
             $q->addTable('project_contacts', 'pc');
             $q->leftJoin('contacts', 'c', 'c.contact_id = pc.contact_id');
             $q->addQuery('c.contact_first_name, c.contact_last_name, c.contact_email');
             $q->addWhere('pc.project_id = ' . $this->task_project);
             $projectContactList = $q->loadList();
             $q->clear();
             foreach ($projectContactList as $myContact) {
                 $mail_recipients[$myContact['contact_email']] = mb_trim($myContact['contact_first_name'] . ' ' . $myContact['contact_last_name']);
             }
         }
         if (isset($others)) {
             $others = trim($others, " \r\n\t,");
             // get rid of empty elements.
             if (strlen($others) > 0) {
                 $q->addTable('contacts', 'c');
                 $q->addQuery('c.contact_first_name, c.contact_last_name, c.contact_email');
                 $q->addWhere('c.contact_id IN (' . $others . ')');
                 $otherContacts = $q->loadList();
                 $q->clear();
                 foreach ($otherContacts as $myContact) {
                     $mail_recipients[$myContact['contact_email']] = mb_trim($myContact['contact_first_name'] . ' ' . $myContact['contact_last_name']);
                 }
             }
         }
         if (isset($extras) && $extras) {
             // Search for semi-colons, commas or spaces and allow any to be separators
             $extra_list = preg_split('/[\\s,;]+/', $extras);
             foreach ($extra_list as $email) {
                 if ($email && !isset($mail_recipients[$email])) {
                     $mail_recipients[$email] = trim($email);
                 }
             }
         }
         $q->clear();
         // Reset to the default state.
         // If this should be sent to a specific user, add their contact details here
         if (isset($specific_user) && $specific_user) {
             $q->addTable('users', 'u');
             $q->leftJoin('contacts', 'c', 'c.contact_id = u.user_contact');
             $q->addQuery('c.contact_first_name, c.contact_last_name');
             $q->leftJoin('contacts_methods', 'cm', 'cm.contact_id = c.contact_id');
             $q->addWhere("cm.method_name = 'email_primary'");
             $q->addQuery('cm.method_value AS contact_email');
             $q->addWhere('u.user_id = ' . $specific_user);
             $su_list = $q->loadList();
             foreach ($su_list as $su_contact) {
                 $mail_recipients[$su_contact['contact_email']] = mb_trim($su_contact['contact_first_name'] . ' ' . $su_contact['contact_last_name']);
             }
         }
         if (count($mail_recipients) == 0) {
             return false;
         }
         // Build the email and send it out.
         $char_set = isset($locale_char_set) ? $locale_char_set : '';
         $mail = new w2p_Utilities_Mail();
         // Grab the subject from user preferences
         $prefix = $AppUI->getPref('TASKLOGSUBJ');
         $mail->Subject($prefix . ' ' . $log->task_log_name, $char_set);
         $q->addTable('projects');
         $q->addQuery('project_name');
         $q->addWhere('project_id=' . (int) $this->task_project);
         $projname = htmlspecialchars_decode($q->loadResult());
         $q->clear();
         $body = $AppUI->_('Project', UI_OUTPUT_RAW) . ': ' . $projname . "\n";
         if ($this->task_parent != $this->task_id) {
             $q->addTable('tasks');
             $q->addQuery('task_name');
             $q->addWhere('task_id = ' . (int) $this->task_parent);
             $req =& $q->exec(QUERY_STYLE_NUM);
             if ($req) {
                 $body .= $AppUI->_('Parent Task', UI_OUTPUT_RAW) . ': ' . htmlspecialchars_decode($req->fields[0]) . "\n";
             }
             $q->clear();
         }
         $body .= $AppUI->_('Task', UI_OUTPUT_RAW) . ': ' . $this->task_name . "\n";
         $task_types = w2PgetSysVal('TaskType');
         $body .= $AppUI->_('Task Type', UI_OUTPUT_RAW) . ':' . $task_types[$this->task_type] . "\n";
         $body .= $AppUI->_('URL', UI_OUTPUT_RAW) . ': ' . W2P_BASE_URL . '/index.php?m=tasks&a=view&task_id=' . $this->task_id . "\n\n";
         $body .= "------------------------\n\n";
         $body .= $AppUI->_('User', UI_OUTPUT_RAW) . ': ' . $creatorname . "\n";
         $body .= $AppUI->_('Hours', UI_OUTPUT_RAW) . ': ' . $log->task_log_hours . "\n";
         $body .= $AppUI->_('Summary', UI_OUTPUT_RAW) . ': ' . $log->task_log_name . "\n\n";
         $body .= $log->task_log_description;
         // Append the user signature to the email - if it exists.
         $q->addTable('users');
         $q->addQuery('user_signature');
         $q->addWhere('user_id = ' . (int) $AppUI->user_id);
         if ($res = $q->exec()) {
             if ($res->fields['user_signature']) {
                 $body .= "\n--\n" . $res->fields['user_signature'];
             }
         }
         $q->clear();
         $mail->Body($body, $char_set);
         $recipient_list = '';
         $toList = array();
         foreach ($mail_recipients as $email => $name) {
             if ($mail->ValidEmail($email)) {
                 $toList[$email] = $email;
                 $recipient_list .= $email . ' (' . $name . ")\n";
             } else {
                 $recipient_list .= "Invalid email address '{$email}' for '{$name}' not sent \n";
             }
         }
         $sendToList = array_keys($mail_recipients);
         $mail->SendSeparatelyTo($sendToList);
         // Now update the log
         $save_email = $AppUI->getPref('TASKLOGNOTE');
         if ($save_email) {
             //TODO: This is where #38 - http://bugs.web2project.net/view.php?id=38 - should be applied if a change is necessary.
             $log->task_log_description .= "\n" . 'Emailed ' . date('l F j, Y H:i:s') . ' to:' . "\n" . $recipient_list;
             return true;
         }
     }
     return false;
     // No update needed.
 }
Example #2
0
 /**
  * Email the task log to assignees, task contacts, project contacts, and others
  * based upon the information supplied by the user.
  */
 public function email_log(&$log, $assignees, $task_contacts, $project_contacts, $others, $extras, $specific_user = 0)
 {
     $mail_recipients = array();
     $q = $this->_getQuery();
     if ((int) $this->task_id > 0 && (int) $this->task_project > 0) {
         if ('on' == $assignees) {
             $q->addTable('user_tasks', 'ut');
             $q->leftJoin('users', 'ua', 'ua.user_id = ut.user_id');
             $q->leftJoin('contacts', 'c', 'c.contact_id = ua.user_contact');
             $q->addQuery('c.contact_email, c.contact_display_name as contact_name');
             $q->addWhere('ut.task_id = ' . $this->task_id);
             if (!$this->_AppUI->getPref('MAILALL')) {
                 $q->addWhere('ua.user_id <>' . (int) $this->_AppUI->user_id);
             }
             $mail_recipients += $q->loadHashList();
             $q->clear();
         }
         if ('on' == $task_contacts) {
             $q->addTable('task_contacts', 'tc');
             $q->leftJoin('contacts', 'c', 'c.contact_id = tc.contact_id');
             $q->addQuery('c.contact_email, c.contact_display_name as contact_name');
             $q->addWhere('tc.task_id = ' . $this->task_id);
             $mail_recipients += $q->loadHashList();
             $q->clear();
         }
         if ('on' == $project_contacts) {
             $q->addTable('project_contacts', 'pc');
             $q->leftJoin('contacts', 'c', 'c.contact_id = pc.contact_id');
             $q->addQuery('c.contact_email, c.contact_display_name as contact_name');
             $q->addWhere('pc.project_id = ' . $this->task_project);
             $mail_recipients += $q->loadHashList();
             $q->clear();
         }
         if (isset($others)) {
             $others = trim($others, " \r\n\t,");
             // get rid of empty elements.
             if (strlen($others) > 0) {
                 $q->addTable('contacts', 'c');
                 $q->addQuery('c.contact_email, c.contact_display_name as contact_name');
                 $q->addWhere('c.contact_id IN (' . $others . ')');
                 $otherContacts = $q->loadList();
                 $q->clear();
                 foreach ($otherContacts as $myContact) {
                     $mail_recipients[$myContact['contact_email']] = $myContact['contact_name'];
                 }
             }
         }
         if (isset($extras) && $extras) {
             // Search for semi-colons, commas or spaces and allow any to be separators
             $extra_list = preg_split('/[\\s,;]+/', $extras);
             foreach ($extra_list as $email) {
                 if ($email && !isset($mail_recipients[$email])) {
                     $mail_recipients[$email] = trim($email);
                 }
             }
         }
         $q->clear();
         // Reset to the default state.
         // If this should be sent to a specific user, add their contact details here
         if ((int) $specific_user) {
             $q->addTable('users', 'u');
             $q->leftJoin('contacts', 'c', 'c.contact_id = u.user_contact');
             $q->addQuery('c.contact_email, c.contact_display_name as contact_name');
             $q->addWhere('u.user_id = ' . $specific_user);
             $su_list = $q->loadList();
             foreach ($su_list as $su_contact) {
                 $mail_recipients[$su_contact['contact_email']] = $su_contact['contact_name'];
             }
         }
         if (count($mail_recipients) == 0) {
             return false;
         }
         // Build the email and send it out.
         $char_set = isset($this->_locale_char_set) ? $this->_locale_char_set : '';
         // Grab the subject from user preferences
         $prefix = $this->_AppUI->getPref('TASKLOGSUBJ');
         $emailManager = new w2p_Output_EmailManager($this->_AppUI);
         $body = $emailManager->getTaskEmailLog($this, $log);
         $mail = new w2p_Utilities_Mail();
         $mail->Subject($prefix . ' ' . $log->task_log_name);
         $mail->Body($body, $char_set);
         $recipient_list = '';
         $toList = array();
         foreach ($mail_recipients as $email => $name) {
             $toList[$email] = $email;
             $recipient_list .= $email . ' (' . $name . ")\n";
         }
         $sendToList = array_keys($mail_recipients);
         $mail->SendSeparatelyTo($sendToList);
         // Now update the log
         $save_email = $this->_AppUI->getPref('TASKLOGNOTE');
         if ($save_email) {
             //TODO: This is where #38 - http://bugs.web2project.net/view.php?id=38 - should be applied if a change is necessary.
             $log->task_log_description .= "\n" . 'Emailed ' . date('l F j, Y H:i:s') . ' GMT to:' . "\n" . $recipient_list;
             return true;
         }
     }
     return false;
     // No update needed.
 }