notification 流程记录

刚写完的不小心回退了结果全都没有了 - =。。 坑爹啊

 

1.调用action_trigger_notification 方法

$this->email_notifications->action_trigger_notification( $action_id, $action_info );

从voucher_lkup_action 里获取到trigger_notification的ids,

然后循环用call_user_func调用这些id对应在voucher_lkup_notification表内notification_name对应的notification_function

public function action_trigger_notification( $action_id, $action_info )
 {
  $details = $this->voucher_lkup->get_action_by_id($action_id);
  //ilog('Action Trigger Notification: <br/> Action Details: ');
  //ilog( array_merge($action_info, $details)  );
  if($details['trigger_notification'] == 'Y' && !empty($details['notification_id']))
  {
   $notification_ids = explode(',', $details['notification_id']);
   foreach($notification_ids as $notification_id)
   {
    $notification_details = $this->_notification_get( $notification_id );
    if($notification_details && $notification_details['is_active'] == 'Y' && !empty($notification_details['notification_function']))
    {
     if(method_exists ($this, $notification_details['notification_function']))
     {
      call_user_func( array($this, $notification_details['notification_function']), $notification_details, $action_info );
     }
    }
   }
  }
 }

比如说我们现在做的是replacement 那action对应的notification的function 是action_replacement_notification_report

在这个function 里会有2个主要的function

1.$this->_notification_get_email_content( 'replacement', $action_info );

2.$this->_notification_send( $notification_details, $notification, $action_info );

private function action_replacement_notification_report( $notification_details, $action_info )
 {
  $date = isset($action_info['action_time']) ? date('Y-m-d', strtotime($action_info['action_time'])) : date('Y-m-d');
  
  $reports = site_url("vreports/view/24/$date/$date");
  $action_info['report_url'] = $reports;
  
  $notification = array();
  $notification['email_content'] = $this->_notification_get_email_content( 'replacement', $action_info );
  
  $this->_notification_send( $notification_details, $notification, $action_info );
 }

 

_notification_get_email_content( $notification_content, $content_data ) 这个function就是来获取email的内容,把定义action_trigger_notification时传进来的action_content也传递进来 传到模版里 然后用view加上true返回邮件内容。

private function _notification_get_email_content( $notification_content, $content_data )
 {

  $folder = $this->config->slash_item('template_module_folder');
  $template_view = $folder . 'notification';
  
  $notification_content = 'notifications/' . $notification_content;

  //set user_name into it.
  $this->load->model('acl/acl_aro');

        $content_data['user_id'] = isset($content_data['user_id']) ? $content_data['user_id'] : $this->_user_id;

        $user_details = $this->acl_aro->get_user_info('', $content_data['user_id']);

        $content_data['user_name'] = isset($content_data['user_name']) ? $content_data['user_name'] : $user_details['user_name'];

        if(empty($user_details))
  {
   $user_details['user_name'] = ''; //cso user.
  }




  $this->load->library('wick');
  $data = array('notification_content' => $this->load->view($notification_content, array_merge($content_data, array('page_data'=>$content_data)), TRUE));
  return $this->load->view($template_view, $data, TRUE);
 }

 

这个function 就时发送邮件啦,根据voucher_lkup_notification 里的target,target_cc等信息来进行发送

private function _notification_send( $notification_details, $notification, $action_info )
 {
  $this->load->helper('email');
  
  $notification['target'] = isset($notification['target']) ? $notification['target'] : $this->_notification_get_target( $notification_details, 'target' );
  $notification['target_cc'] = isset($notification['target_cc']) ? $notification['target_cc'] : $this->_notification_get_target( $notification_details, 'target_cc' );
  $notification['target_bcc'] = isset($notification['target_bcc']) ? $notification['target_bcc'] : $this->_notification_get_target( $notification_details, 'target_bcc' );
  $notification['email_subject'] = isset($notification['email_subject']) ? $notification['email_subject'] : $this->_notification_get_email_subject( $notification_details, $action_info );
  
  if(IS_LOCALHOST)
  {
   ilog( $notification );
  }
  else
  {
   if( empty($notification['target']) )
   {
    //evo_email_admin('No target email found for the notification.' . '<BR />Notification Definition: <BR />' .  var_export($notification_details, TRUE) . '<BR />Notification: <BR />' .  var_export($notification, TRUE) );
    //return FALSE;
   }
   
   if( empty($notification['email_content']) )
   {
    evo_email_admin('Notification content is empty. Kindly check. ' . '<BR />Notification Definition: <BR />' .  var_export($notification_details, TRUE) . '<BR />Notification: <BR />' .  var_export($notification, TRUE));
    return FALSE;
   }
   
   $this->load->library('email');
   
   $config['mailtype'] = 'html';
   $this->email->initialize($config);
   
   $this->email->subject( $notification['email_subject'] );
   $this->email->from( $this->notification_from_email, $this->notification_from_name );
   
   
   $this->email->to( $notification['target'] );
   
   if(!empty( $notification['target_cc'] ))
   {
    $this->email->cc( $notification['target_cc'] );
   }
   
   if(!empty( $notification['target_bcc'] ))
   {
    $this->email->bcc( $notification['target_bcc'] );
   }
   
   $this->email->message( $notification['email_content'] );
   $this->email->send();
  }
  
  return TRUE;
 }

 

获取用户组的方法已经在之前记录过了

http://www.jinleistudio.cn/index.php/posts/92