예제 #1
0
파일: Contacts.php 프로젝트: dsyman2/X2CRM
 /**
  * Responds when {@link X2Model::afterUpdate()} is called (record saved, but
  * not a new record). Sends a notification to anyone subscribed to this contact.
  *
  * Before executing this, the model must check whether the contact has the
  * "changelog" behavior. That is because the behavior is disabled
  * when checking for duplicates in {@link ContactsController}
  */
 public function afterUpdate()
 {
     if (!Yii::app()->params->noSession && $this->asa('changelog') && $this->asa('changelog')->enabled) {
         //$this->scenario != 'noChangelog') {
         // send subscribe emails if anyone has subscribed to this contact
         $result = Yii::app()->db->createCommand()->select('user_id')->from('x2_subscribe_contacts')->where('contact_id=:id', array(':id' => $this->id))->queryColumn();
         $datetime = Formatter::formatLongDateTime(time());
         $modelLink = CHtml::link($this->name, Yii::app()->controller->createAbsoluteUrl('/contacts/' . $this->id));
         $subject = 'X2Engine: ' . $this->name . ' updated';
         $message = "Hello,<br>\n<br>\n";
         $message .= 'You are receiving this email because you are subscribed to changes made to the contact ' . $modelLink . ' in X2Engine. ';
         $message .= 'The following changes were made on ' . $datetime . ":<br>\n<br>\n";
         foreach ($this->getChanges() as $attribute => $change) {
             if ($attribute != 'lastActivity') {
                 $old = $change[0] == '' ? '-----' : $change[0];
                 $new = $change[1] == '' ? '-----' : $change[1];
                 $label = $this->getAttributeLabel($attribute);
                 $message .= "{$label}: {$old} => {$new}<br>\n";
             }
         }
         $message .= "<br>\nYou can unsubscribe to these messages by going to {$modelLink} and clicking Unsubscribe.<br>\n<br>\n";
         $adminProfile = Yii::app()->params->adminProfile;
         foreach ($result as $subscription) {
             $subscription = array();
             if (isset($subscription['user_id'])) {
                 $profile = X2Model::model('Profile')->findByPk($subscription['user_id']);
                 if ($profile && $profile->emailAddress && $adminProfile && $adminProfile->emailAddress) {
                     $to = array('to' => array(array($profile->fullName, $profile->emailAddress)));
                     Yii::app()->controller->sendUserEmail($to, $subject, $message, null, Credentials::$sysUseId['systemNotificationEmail']);
                 }
             }
         }
     }
     parent::afterUpdate();
 }