Example #1
0
File: event.php Project: rair/yacs
 /**
  * remember an action once it's done
  *
  * To be overloaded into derived class
  *
  * @param string the action 'insert', 'update' or 'delete'
  * @param array the hosting record
  * @param string reference of the hosting record (e.g., 'article:123')
  * @return FALSE on error, TRUE otherwise
  */
 function remember($action, $host, $reference)
 {
     global $context;
     // remember the id of the master record
     $id = $host['id'];
     // set default values for this editor
     Surfer::check_default_editor($this->attributes);
     // we use the existing back-end for dates
     include_once $context['path_to_root'] . 'dates/dates.php';
     // build the update query
     switch ($action) {
         case 'delete':
             // no need to notify participants after the date planned for the event, nor if the event has been initiated
             if (isset($this->attributes['date_stamp']) && $this->attributes['date_stamp'] > gmstrftime('%Y-%m-%d %H:%M') && isset($this->attributes['status']) && $this->attributes['status'] != 'started' && $this->attributes['status'] != 'stopped') {
                 // send a cancellation message to participants
                 $query = "SELECT user_email FROM " . SQL::table_name('enrolments') . " WHERE (anchor LIKE '" . $reference . "') AND (approved LIKE 'Y')";
                 $result = SQL::query($query);
                 while ($item = SQL::fetch($result)) {
                     // sanity check
                     if (!preg_match(VALID_RECIPIENT, $item['user_email'])) {
                         continue;
                     }
                     // message title
                     $subject = sprintf('%s: %s', i18n::c('Cancellation'), strip_tags($this->anchor->get_title()));
                     // headline
                     $headline = sprintf(i18n::c('%s has cancelled %s'), Surfer::get_link(), $this->anchor->get_title());
                     // message to reader
                     $message = $this->get_invite_default_message('CANCEL');
                     // assemble main content of this message
                     $message = Skin::build_mail_content($headline, $message);
                     // threads messages
                     $headers = Mailer::set_thread($this->anchor->get_reference());
                     // get attachment from the overlay
                     $attachments = $this->get_invite_attachments('CANCEL');
                     // post it
                     Mailer::notify(Surfer::from(), $item['user_email'], $subject, $message, $headers, $attachments);
                 }
             }
             // delete dates for this anchor
             Dates::delete_for_anchor($reference);
             // also delete related enrolment records
             $query = "DELETE FROM " . SQL::table_name('enrolments') . " WHERE anchor LIKE '" . $reference . "'";
             SQL::query($query);
             break;
         case 'insert':
             // bind one date to this record
             if (isset($this->attributes['date_stamp']) && $this->attributes['date_stamp']) {
                 $fields = array();
                 $fields['anchor'] = $reference;
                 $fields['date_stamp'] = $this->attributes['date_stamp'];
                 // update the database
                 if (!($fields['id'] = Dates::post($fields))) {
                     Logger::error(i18n::s('Impossible to add an item.'));
                     return FALSE;
                 }
             }
             // enroll page creator
             include_once $context['path_to_root'] . 'shared/enrolments.php';
             enrolments::confirm($reference);
             // reload the anchor through the cache to reflect the update
             if ($reference) {
                 $this->anchor = Anchors::get($reference, TRUE);
             }
             // send a confirmation message to event creator
             $query = "SELECT * FROM " . SQL::table_name('enrolments') . " WHERE (anchor LIKE '" . $reference . "')";
             $result = SQL::query($query);
             while ($item = SQL::fetch($result)) {
                 // a user registered on this server
                 if ($item['user_id'] && ($watcher = Users::get($item['user_id']))) {
                     // sanity check
                     if (!preg_match(VALID_RECIPIENT, $item['user_email'])) {
                         continue;
                     }
                     // use this email address
                     if ($watcher['full_name']) {
                         $recipient = Mailer::encode_recipient($watcher['email'], $watcher['full_name']);
                     } else {
                         $recipient = Mailer::encode_recipient($watcher['email'], $watcher['nick_name']);
                     }
                     // message title
                     $subject = sprintf(i18n::c('Meeting: %s'), strip_tags($this->anchor->get_title()));
                     // headline
                     $headline = sprintf(i18n::c('you have arranged %s'), '<a href="' . $context['url_to_home'] . $context['url_to_root'] . $this->anchor->get_url() . '">' . $this->anchor->get_title() . '</a>');
                     // message to reader
                     $message = $this->get_invite_default_message('PUBLISH');
                     // assemble main content of this message
                     $message = Skin::build_mail_content($headline, $message);
                     // a set of links
                     $menu = array();
                     // call for action
                     $link = $context['url_to_home'] . $context['url_to_root'] . $this->anchor->get_url();
                     $menu[] = Skin::build_mail_button($link, i18n::c('View event details'), TRUE);
                     // finalize links
                     $message .= Skin::build_mail_menu($menu);
                     // threads messages
                     $headers = Mailer::set_thread($this->anchor->get_reference());
                     // get attachment from the overlay
                     $attachments = $this->get_invite_attachments('PUBLISH');
                     // post it
                     Mailer::notify(Surfer::from(), $recipient, $subject, $message, $headers, $attachments);
                 }
             }
             break;
         case 'update':
             // reload the anchor through the cache to reflect the update
             if ($reference) {
                 $this->anchor = Anchors::get($reference, TRUE);
             }
             // no need to notify watchers after the date planned for the event, nor if the event has been initiated
             if (isset($this->attributes['date_stamp']) && $this->attributes['date_stamp'] > gmstrftime('%Y-%m-%d %H:%M') && isset($this->attributes['status']) && $this->attributes['status'] != 'started' && $this->attributes['status'] != 'stopped' && isset($_REQUEST['notify_watchers']) && $_REQUEST['notify_watchers'] == 'Y') {
                 // send a confirmation message to participants
                 $query = "SELECT * FROM " . SQL::table_name('enrolments') . " WHERE (anchor LIKE '" . $reference . "')";
                 $result = SQL::query($query);
                 while ($item = SQL::fetch($result)) {
                     // skip current surfer
                     if (Surfer::get_id() && Surfer::get_id() == $item['user_id']) {
                         continue;
                     }
                     // a user registered on this server
                     if ($item['user_id'] && ($watcher = Users::get($item['user_id']))) {
                         // skip banned users
                         if ($watcher['capability'] == '?') {
                             continue;
                         }
                         // ensure this surfer wants to be alerted
                         if ($watcher['without_alerts'] == 'Y') {
                             continue;
                         }
                         // sanity check
                         if (!preg_match(VALID_RECIPIENT, $item['user_email'])) {
                             continue;
                         }
                         // use this email address
                         if ($watcher['full_name']) {
                             $recipient = Mailer::encode_recipient($watcher['email'], $watcher['full_name']);
                         } else {
                             $recipient = Mailer::encode_recipient($watcher['email'], $watcher['nick_name']);
                         }
                         // message title
                         $subject = sprintf(i18n::c('Updated: %s'), strip_tags($this->anchor->get_title()));
                         // headline
                         $headline = sprintf(i18n::c('%s has updated %s'), Surfer::get_link(), '<a href="' . $context['url_to_home'] . $context['url_to_root'] . $this->anchor->get_url() . '">' . $this->anchor->get_title() . '</a>');
                         // message to reader
                         $message = $this->get_invite_default_message('PUBLISH');
                         // assemble main content of this message
                         $message = Skin::build_mail_content($headline, $message);
                         // a set of links
                         $menu = array();
                         // call for action
                         $link = $context['url_to_home'] . $context['url_to_root'] . $this->anchor->get_url();
                         $menu[] = Skin::build_mail_button($link, i18n::c('View event details'), TRUE);
                         // finalize links
                         $message .= Skin::build_mail_menu($menu);
                         // threads messages
                         $headers = Mailer::set_thread($this->anchor->get_reference());
                         // get attachment from the overlay
                         $attachments = $this->get_invite_attachments('PUBLISH');
                         // post it
                         Mailer::notify(Surfer::from(), $recipient, $subject, $message, $headers, $attachments);
                     }
                 }
             }
             // bind one date to this record
             if (isset($this->attributes['date_stamp']) && $this->attributes['date_stamp']) {
                 $fields = array();
                 $fields['anchor'] = $reference;
                 $fields['date_stamp'] = $this->attributes['date_stamp'];
                 // there is an existing record
                 if ($date =& Dates::get_for_anchor($reference)) {
                     // update the record
                     $fields['id'] = $date['id'];
                     if (!($id = Dates::post($fields))) {
                         Logger::error(sprintf(i18n::s('Impossible to update date %s'), $this->attributes['date_stamp']));
                         return FALSE;
                     }
                     // create a record instead of raising an error, we are smart y'a'know
                 } else {
                     if (!($fields['id'] = Dates::post($fields))) {
                         Logger::error(i18n::s('Impossible to add an item.'));
                         return FALSE;
                     }
                 }
             }
             break;
     }
     // job done
     return TRUE;
 }