WYSIJA_model_email

The MailPoet Newsletters WYSIJA model email class.

Defined (1)

The class is defined in the following location(s).

/models/email.php  
  1. class WYSIJA_model_email extends WYSIJA_model{ 
  2.  
  3. var $pk='email_id'; 
  4. var $table_name='email'; 
  5. var $columns=array( 
  6. 'email_id'=>array('auto'=>true),  
  7. 'campaign_id' => array('type'=>'integer'),  
  8. 'subject' => array('req'=>true),  
  9. 'body' => array('req'=>true, 'html'=>1),  
  10. 'from_email' => array('req'=>true),  
  11. 'from_name' => array('req'=>true),  
  12. 'replyto_email' => array(),  
  13. 'replyto_name' => array(),  
  14. 'attachments' => array(),  
  15. 'status' => array('type'=>'integer'),  
  16. /**draft :0 
  17. sending:1&3 
  18. sent:2 
  19. paused:-1*/ 
  20. 'type' => array('type'=>'integer'),  
  21. /** 
  22. * 0 : confirmation email 
  23. * 1 : standard nl 
  24. * 2 : autonewsletter 
  25. */ 
  26. 'number_sent'=>array('type'=>'integer'),  
  27. 'number_opened'=>array('type'=>'integer'),  
  28. 'number_clicked'=>array('type'=>'integer'),  
  29. 'number_unsub'=>array('type'=>'integer'),  
  30. 'number_bounce'=>array('type'=>'integer'),  
  31. 'number_forward'=>array('type'=>'integer'),  
  32. 'sent_at' => array('type'=>'date'),  
  33. 'created_at' => array('type'=>'date', 'autoins'=>1),  
  34. 'modified_at' => array('type'=>'date'),  
  35. 'params' => array(),  
  36. 'wj_data' => array(),  
  37. 'wj_styles' => array() 
  38. ); 
  39.  
  40. var $retro_active_autoresponders=true; 
  41.  
  42. function __construct() { 
  43. parent::__construct(); 
  44.  
  45. /** 
  46. * validation before insertion 
  47. * @return type 
  48. */ 
  49. function beforeInsert() { 
  50. $this->checkParams(); 
  51. $model_config = WYSIJA::get('config', 'model'); 
  52. if(!isset($this->values['from_email'])) $this->values['from_email'] = $model_config->getValue('from_email'); 
  53. if(!isset($this->values['from_name'])) $this->values['from_name'] = $model_config->getValue('from_name'); 
  54. if(!isset($this->values['replyto_email'])) $this->values['replyto_email'] = $model_config->getValue('replyto_email'); 
  55. if(!isset($this->values['replyto_name'])) $this->values['replyto_name'] = $model_config->getValue('replyto_name'); 
  56. if(!isset($this->values['modified_at'])) $this->values['modified_at'] = time(); 
  57.  
  58. return true; 
  59.  
  60. /** 
  61. * validation before deletion 
  62. * @param type $conditions 
  63. * @return type 
  64. */ 
  65. function beforeDelete($conditions) { 
  66. if(!isset($conditions['email_id'])) { 
  67. return true; 
  68. }else $emailid=$conditions['email_id']; 
  69.  
  70. $modelQ=WYSIJA::get('queue', 'model'); 
  71. $modelQ->delete(array('email_id'=>$conditions['email_id'])); 
  72. return true; 
  73.  
  74. /** 
  75. * validation before updating the data 
  76. * @return boolean 
  77. */ 
  78. function beforeUpdate($id = null) { 
  79.  
  80. if(isset($this->values['params']) && is_array($this->values['params'])) { 
  81.  
  82. //update the nextSend value 
  83. if(!isset($this->values['params']['autonl']['nextSend']) && isset($this->values['type']) && $this->values['type']=='2') { 
  84. $auton=WYSIJA::get('autonews', 'helper'); 
  85. $this->values['params']['autonl']['nextSend']=$auton->getNextSend($this->values); 
  86.  
  87. //get the params from the db and update them 
  88. $this->checkParams(); 
  89.  
  90.  
  91. return true; 
  92.  
  93. /** 
  94. * trigger on Update 
  95. */ 
  96. function afterUpdate($result_save_id) { 
  97. //First reply-to address (name + email) is a default value for next newsletter 
  98. $model_config=WYSIJA::get('config', 'model'); 
  99. if(!$model_config->getValue('replyto_name') || !$model_config->getValue('replyto_email')) { 
  100. $email = $this->getOne(false, array('email_id'=>$result_save_id)); 
  101. $model_config->save(array( 
  102. 'replyto_name' => $email['replyto_name'],  
  103. 'replyto_email' => $email['replyto_email'] 
  104. )); 
  105. return true; 
  106.  
  107.  
  108. /** 
  109. * return the online version link of a newsletter 
  110. * @param int $email_id 
  111. * @param string $text 
  112. * @param string $urlOnly 
  113. * @return string 
  114. */ 
  115. function getPreviewLink($email_id, $text=false, $urlOnly=true) { 
  116. if(!$text) $text=__('View', WYSIJA); 
  117.  
  118. $this->reset(); 
  119. $modelConf=WYSIJA::get('config', 'model'); 
  120.  
  121. $params=array( 
  122. 'wysija-page'=>1,  
  123. 'controller'=>'email',  
  124. 'action'=>'view',  
  125. 'email_id'=>$email_id,  
  126. ); 
  127.  
  128.  
  129. $fullurl=WYSIJA::get_permalink($modelConf->getValue('confirm_email_link'), $params); 
  130. if($urlOnly) return $fullurl; 
  131. return '<a href="'.$fullurl.'" target="_blank">'.$text.'</a>'; 
  132.  
  133.  
  134.  
  135. /** 
  136. * what to do when starting to send a newsletter based on the type and other parameters 
  137. * @param mixed $email 
  138. * @return boolean 
  139. */ 
  140. function send_activate($email) { 
  141. if(!is_array($email)) { 
  142. if(is_numeric($email)) { 
  143. $email=$this->getOne(false, array('email_id'=>$email)); 
  144. }else return false; 
  145.  
  146. // we go through that queuing function which will check if it is necessary to queue the email 
  147. // depending on the type of email we're dealing with there will be no queuing 
  148. if($this->retro_active_autoresponders) { 
  149. $model_queue = WYSIJA::get('queue', 'model'); 
  150. $emails_have_been_queued = $model_queue->queue_email($email); 
  151.  
  152. //set the email status based on parameters and also return a message 
  153. $email_status=99; 
  154. $sent_status = array(); 
  155.  
  156. if((int)$email['type']===1) { 
  157. if(isset($email['params']['schedule']['isscheduled']) && !$emails_have_been_queued) { 
  158. $email_status=4; 
  159. $this->notice(__('Newsletter has been scheduled.', WYSIJA)); 
  160. } else $this->notice(__('Your latest newsletter is being sent.', WYSIJA)); 
  161. $sent_status['sent_at'] = time(); 
  162. } else $this->notice(__('Your auto newsletter has been activated.', WYSIJA)); 
  163. $sent_status['status'] = $email_status; 
  164.  
  165.  
  166. $this->reset(); 
  167. $this->update($sent_status, array('email_id'=>$email['email_id'])); 
  168. return true; 
  169.  
  170.  
  171. /** 
  172. * used in post notification scheme, it uses the model email to create a child to send when ready 
  173. * @param array $email 
  174. * @param boolean $immediatePostNotif 
  175. * @return int next send value 
  176. */ 
  177. function give_birth($email, $immediatePostNotif=false) { 
  178. WYSIJA::log('give_birth_starts', $email['params']['autonl']['nextSend'], 'post_notif'); 
  179. //duplicate email with the right body and title set it as type 1 
  180. if(isset($email['params']) && !is_array($email['params'])) $this->getParams($email); 
  181. $emailChild=$email; 
  182. $paramsVal=$email['params']; 
  183.  
  184. if(!isset($paramsVal['autonl']['total_child'])) $paramsVal['autonl']['total_child'] = 0; 
  185. $paramsVal['autonl']['total_child']++; 
  186.  
  187. unset($emailChild['email_id']); 
  188. unset($emailChild['created_at']); 
  189. $emailChild['type']=1; 
  190. $emailChild['status']=99; 
  191. $emailChild['sent_at']=time(); 
  192.  
  193. $this->reset(); 
  194. unset($emailChild['params']['autonl']); 
  195.  
  196. // get articles ids used in previously sent childs 
  197. $ids = (!empty($paramsVal['autonl']['articles']['ids'])) ? $paramsVal['autonl']['articles']['ids'] : array(); 
  198.  
  199. // build autonl articles params for child 
  200. $emailChild['params']['autonl']['articles'] = array('ids' => $ids, 'count' => 0, 'first_subject' => ''); 
  201. if(isset($email['params']['autonl']['firstSend'])) $emailChild['params']['autonl']['firstSend'] = $email['params']['autonl']['firstSend']; 
  202. if(isset($email['params']['autonl']['lastSend'])) $emailChild['params']['autonl']['lastSend'] = $email['params']['autonl']['lastSend']; 
  203.  
  204. //if it's an immediate post notif let know the render email 
  205. if($immediatePostNotif) { 
  206. $emailChild['params']['autonl']['articles']['immediatepostid'] = (int)$immediatePostNotif; 
  207. //if this article is already set there is no reason to give birth to a child email 
  208. if(isset($email['params']['autonl']['articles']['ids']) && in_array($immediatePostNotif, $email['params']['autonl']['articles']['ids'])) return false; 
  209.  
  210. $wjEngine = WYSIJA::get('wj_engine', 'helper'); 
  211. // set data & styles 
  212. if(isset($emailChild['wj_data'])) { $wjEngine->setData($emailChild['wj_data'], true); } else { $wjEngine->setData(); } 
  213. if(isset($emailChild['wj_styles'])) { $wjEngine->setStyles($emailChild['wj_styles'], true); } else { $wjEngine->setStyles(); } 
  214.  
  215. // generate email html body 
  216. $body = $wjEngine->renderEmail($emailChild); 
  217.  
  218. // get back email data as it will be updated during the rendering (articles ids + articles count) 
  219. $emailChild = $wjEngine->getEmailData(); 
  220. $emailChild['body'] = $body; 
  221.  
  222. // update parent email articles' ids to reflect the ones added in the child email 
  223. $paramsVal['autonl']['articles']['ids'] = $emailChild['params']['autonl']['articles']['ids']; 
  224.  
  225. // added new_ids parameter in order to know which articles are being used in that newsletter for instance with shortcodes 
  226. if(!empty($email['params']['autonl']['articles']['ids'])) { 
  227. $emailChild['params']['autonl']['articles']['new_ids'] = array_diff($emailChild['params']['autonl']['articles']['ids'], $email['params']['autonl']['articles']['ids']); 
  228. } else { 
  229. $emailChild['params']['autonl']['articles']['new_ids'] = $emailChild['params']['autonl']['articles']['ids']; 
  230.  
  231. $donotsend=false; 
  232. // if there's no article, do not send 
  233. if((int)$emailChild['params']['autonl']['articles']['count'] === 0) { 
  234. $donotsend = true; 
  235.  
  236. // we send if not told to not do it 
  237. if(!$donotsend) { 
  238.  
  239. // Parse old subject shortcodes. 
  240. $emailChild['subject'] = str_replace( 
  241. array('[total]', '[number]', '[post_title]'),  
  242. array((int)$emailChild['params']['autonl']['articles']['count'],  
  243. (int)$paramsVal['autonl']['total_child'],  
  244. $emailChild['params']['autonl']['articles']['first_subject']),  
  245. $emailChild['subject']); 
  246. $emailChild['params']['autonl']['total_child']=(int)$paramsVal['autonl']['total_child']; 
  247. // Get the email object, needed for the shortcode class. 
  248. $current_email_object = (object) $emailChild; 
  249.  
  250. // Parse subject shortcodes. 
  251. $helper_mailer = WYSIJA::get('mailer', 'helper'); 
  252. $helper_mailer->parseSubjectUserTags($current_email_object); 
  253.  
  254. // Replace subject shortcodes. 
  255. $helper_shortcodes = WYSIJA::get('shortcodes', 'helper'); 
  256. $emailChild['subject'] = $helper_shortcodes->replace_subject($current_email_object); 
  257.  
  258. // save the child email 
  259. $emailChild['params']['autonl']['parent']=$email['email_id']; 
  260.  
  261. $this->dbg=false;//this line is to correct the crazy color so that it doesn't use the keepQry function. 
  262. $emailChild['email_id']=$this->insert($emailChild); 
  263. $this->reset(); 
  264. WYSIJA::log('check_post_notif_give_birth_before_send', $emailChild, 'post_notif'); 
  265. $this->send_activate($emailChild); 
  266. WYSIJA::log('prev_send_value_give_birth', $email['params']['autonl']['nextSend'], 'post_notif'); 
  267. // update the parent with the new nextSend date 
  268. $auton=WYSIJA::get('autonews', 'helper'); 
  269. $nextSendValue=$auton->getNextSend($email); 
  270.  
  271. WYSIJA::log('next_send_value_give_birth', $nextSendValue, 'post_notif'); 
  272. //update the parent email only it has been sent 
  273. if(!$donotsend) { 
  274. $paramsVal['autonl']['nextSend']=$nextSendValue; 
  275. if(isset($paramsVal['autonl']['late_send'])) unset($paramsVal['autonl']['late_send']); 
  276. $this->reset(); 
  277.  
  278. //we use to have a filter compared to the first send date, but we should have a filter to the lastSend date instead 
  279. if(!isset($email['params']['autonl']['firstSend'])) $paramsVal['autonl']['firstSend']=time(); 
  280. $paramsVal['autonl']['lastSend']=time(); 
  281.  
  282.  
  283. $this->update(array('params'=>$paramsVal), array('email_id'=>$email['email_id'])); 
  284.  
  285. return $nextSendValue; 
  286.  
  287. /** 
  288. * special get overriding the model class in order to load the params of the email 
  289. * @param array $columns 
  290. * @param array $conditions 
  291. * @return type 
  292. */ 
  293. function get($columns=array(), $conditions=array()) { 
  294.  
  295. $results=parent::get($columns, $conditions); 
  296.  
  297. if(is_array($results) && (!isset($results['params']))) { 
  298. foreach($results as &$result) { 
  299. $this->getParams($result); 
  300. }else $this->getParams($results); 
  301.  
  302. return $results; 
  303.  
  304. /** 
  305. * convert the base64 serialized param into an array directly on email load 
  306. * @param type $object 
  307. */ 
  308. function getParams(&$object=false) { 
  309. if(!$object) $object=&$this->values; 
  310.  
  311. if(is_array($object)) { 
  312. if(isset($object['params']) && is_string($object['params'])) { 
  313. $object['params']=unserialize(base64_decode($object['params'])); 
  314. }else{ 
  315. if(isset($object->params) && is_string($object->params)) { 
  316. $object->params=unserialize(base64_decode($object->params)); 
  317.  
  318.  
  319.  
  320. /** 
  321. * encode the param array before saving the email data 
  322. * @param object reference $object 
  323. */ 
  324. function checkParams(&$object=false) { 
  325.  
  326. if(!$object) $object=&$this->values; 
  327. //else dbg($object, 0); 
  328. if(is_array($object)) { 
  329.  
  330. if(isset($object['params']) && is_array($object['params'])) { 
  331. if(isset($object['email_id'])) { 
  332. $newEmailModel=new WYSIJA_model_email(); 
  333. $recentData=$newEmailModel->getOne(false, array('email_id'=>$object['email_id'])); 
  334. if(is_string($recentData['params'])) $recentData['params']=unserialize(base64_decode($recentData['params'])); 
  335.  
  336.  
  337. foreach($object['params'] as $pk => $pv) { 
  338. if($pk=='autonl') { 
  339. foreach($pv as $pvk => $pvv) { 
  340. $recentData['params'][$pk][$pvk]=$pvv; 
  341. }else $recentData['params'][$pk]=$pv; 
  342.  
  343. $object['params']=base64_encode(serialize($recentData['params'])); 
  344. }else{ 
  345. if(isset($object->params) && is_array($object->params)) { 
  346. if(isset($object->email_id)) { 
  347. $newEmailModel=new WYSIJA_model_email(); 
  348. $recentData=$newEmailModel->getOne(false, array('email_id'=>$object->email_id)); 
  349. $recentData['params']=unserialize(base64_decode($recentData['params'])); 
  350.  
  351. foreach($object->params as $pk => $pv) { 
  352. if($pk=='autonl') { 
  353. foreach($pv as $pvk => $pvv) { 
  354. $recentData['params'][$pk][$pvk]=$pvv; 
  355. }else $recentData['params'][$pk]=$pv; 
  356.  
  357. $object->params=base64_encode(serialize($recentData['params']));