示例#1
0
 /**
  * Sends notifications *later*, so stores them in the database
  * @param mixed $to string or array...the type of address (email, task ID, user ID) is specified below
  * @param integer $to_type type of $to address
  * @param integer $type type of notification
  * @param array $data additional info needed for notification
  * @access public
  * @return bool
  */
 function send_later($to, $to_type, $type, $data = array())
 {
     global $db, $user;
     // we only "send later" to registered users
     if ($to_type == ADDRESS_EMAIL) {
         return false;
     }
     if ($to_type == ADDRESS_TASK) {
         $data['task_id'] = $to;
         list(, , $to) = Notifications::task_notifications($to, $type, ADDRESS_USER);
     }
     // otherwise we already have a list of users
     $to = (array) $to;
     if (isset($data['task_id'])) {
         $data['task'] = Flyspray::getTaskDetails($data['task_id']);
         // we have project specific options
         $pid = $db->x->GetOne('SELECT project_id FROM {tasks} WHERE task_id = ?', null, $data['task_id']);
         $data['project'] = new Project($pid);
     }
     list($data['subject'], $data['body']) = Notifications::generate_message($type, $data);
     $time = time();
     // on a sidenote: never do strange things like $date = time() or $time = date('U');
     // just in case
     $res = $db->x->autoExecute('{notification_messages}', array('message_data' => serialize($data), 'time_created' => $time));
     if (PEAR::isError($res)) {
         return false;
     }
     $message_id = $db->lastInsertID();
     if (count($to)) {
         $stmt = $db->x->autoPrepare('{notification_recipients}', array('message_id', 'user_id'));
         foreach ($to as $user_id) {
             if ($user_id == $user->id && !$user->infos['notify_own']) {
                 continue;
             }
             $stmt->execute(array($message_id, $user_id));
         }
         $stmt->free();
     }
     return true;
 }