Example #1
0
 function mail_reminder()
 {
     // check for a reminder.reminderHash that links off to a token.tokenHash
     // this lets us trigger reminders on complex actions, for example create
     // a reminder that sends when a task status changes from pending to open
     // Note this->reminderTime is going to always be null for the token that
     // link to task->moved_from_pending_to_open().
     // Whereas the task->reopen_pending_task() will have a reminderTime set.
     $ok = true;
     if ($this->get_value("reminderHash")) {
         $token = new token();
         if ($token->set_hash($this->get_value("reminderHash"))) {
             list($entity, $method) = $token->execute();
             if (is_object($entity) && $entity->get_id()) {
                 if (!$entity->{$method}()) {
                     $token->decrement_tokenUsed();
                     // next time, gadget
                     $ok = false;
                 }
             }
         }
     }
     if ($ok) {
         $recipients = $this->get_all_recipients();
         # Reminders can be clients, tasks, projects or "general" - comment threads don't exist for general
         if ($this->get_value('reminderType') != 'general') {
             # Nowhere to put the subject?
             $commentID = comment::add_comment($this->get_value('reminderType'), $this->get_value('reminderLinkID'), $this->get_value('reminderContent'), $this->get_value('reminderType'), $this->get_value('reminderLinkID'));
             # Repackage the recipients to become IPs of the new comment
             $ips = array();
             foreach ((array) $recipients as $id => $person) {
                 $ip = array();
                 $ip['name'] = $person['name'];
                 $ip['addIP'] = true;
                 $ip['addContact'] = false;
                 $ip['internal'] = true;
                 $ips[$person['emailAddress']] = $ip;
             }
             comment::add_interested_parties($commentID, false, $ips);
             # email_receive false or true? false for now... maybe true is better?
             comment::send_comment($commentID, array("interested"));
         } else {
             foreach ((array) $recipients as $person) {
                 if ($person['emailAddress']) {
                     $email = sprintf("%s %s <%s>", $person['firstName'], $person['surname'], $person['emailAddress']);
                     $subject = $this->get_value('reminderSubject');
                     $content = $this->get_value('reminderContent');
                     $e = new email_send($email, $subject, $content, "reminder");
                     $e->send();
                 }
             }
         }
         // Update reminder (reminderTime can be blank for task->moved_from_pending_to_open)
         if ($this->get_value('reminderRecuringInterval') == "No") {
             $this->deactivate();
         } else {
             if ($this->get_value('reminderRecuringValue') != 0) {
                 $interval = $this->get_value('reminderRecuringValue');
                 $intervalUnit = $this->get_value('reminderRecuringInterval');
                 $newtime = $this->get_next_reminder_time(strtotime($this->get_value('reminderTime')), $interval, $intervalUnit);
                 $this->set_value('reminderTime', date("Y-m-d H:i:s", $newtime));
                 $this->set_value('reminderAdvNoticeSent', 0);
                 $this->save();
             }
         }
     }
 }