function afterSend($sender, $opt, $mailsOptions) { if (!empty($opt['sending']['NewsletterSending']['status']) && $opt['sending']['NewsletterSending']['status'] == 'test') { } else { //$this->log($mailsOptions,'newsletter'); $emails = array(); foreach ($mailsOptions as $opt) { $emails[] = $opt['email']['email']; } //$this->log($emails,'newsletter'); App::import('Lib', 'Newsletter.Sendlist'); Sendlist::disable_email($emails); } }
function updateBounces() { App::import('Lib', 'Newsletter.NewsletterConfig'); $bounceLimit = NewsletterConfig::load('bounceLimit'); $NewsletterEvent = ClassRegistry::init('Newsletter.NewsletterEvent'); $findOpt = array('fields' => array('count(NewsletterSended.id) as count', 'count(NewsletterSended.id)>=' . $bounceLimit . ' as to_drop', 'NewsletterEvent.*', 'NewsletterSended.email_id', 'NewsletterSended.tabledlist_id', 'NewsletterSended.email'), 'conditions' => array('NewsletterEvent.action' => 'bounce', 'NewsletterEvent.processed' => 0, 'or' => array('NewsletterSended.tabledlist_id IS NOT NULL', 'NewsletterEmail.active' => 1)), 'joins' => array(array('alias' => $NewsletterEvent->NewsletterSended->alias, 'table' => $NewsletterEvent->NewsletterSended->useTable, 'type' => 'LEFT', 'conditions' => array('NewsletterSended.id = NewsletterEvent.sended_id')), array('alias' => 'OtherEvent', 'table' => $NewsletterEvent->useTable, 'type' => 'LEFT', 'conditions' => array('OtherEvent.sended_id = NewsletterSended.id')), array('alias' => $NewsletterEvent->NewsletterSended->NewsletterEmail->alias, 'table' => $NewsletterEvent->NewsletterSended->NewsletterEmail->useTable, 'type' => 'LEFT', 'conditions' => array('NewsletterEmail.id = NewsletterSended.email_id', 'NewsletterSended.tabledlist_id IS NULL'))), 'group' => 'NewsletterSended.id', 'order' => array('count(NewsletterSended.id) DESC', 'NewsletterEvent.date DESC'), 'recursive' => -1, 'limit' => 100); $data = $NewsletterEvent->find('all', $findOpt); //debug($data[0]); $proccessed = array(); $drops = array(); foreach ($data as $r) { $proccessed[] = $r['NewsletterEvent']['id']; if ($r[0]['to_drop']) { $drops[$r['NewsletterSended']['tabledlist_id'] ? $r['NewsletterSended']['tabledlist_id'] : 0][$r['NewsletterSended']['email_id']] = true; } } //debug($drops); foreach ($drops as $list => $ids) { $Model = $activeField = $idField = null; if ($list) { App::import('Lib', 'Newsletter.Sendlist'); $list = Sendlist::getSendlist($list); $fields = $list->emailFields(); //debug($fields); if (!empty($fields['active'])) { $Model = $NewsletterEvent->NewsletterSended->NewsletterEmail; $activeField = $fields['active']; $idField = $fields['id']; } } else { $Model = $NewsletterEvent->NewsletterSended->NewsletterEmail; $activeField = 'NewsletterEmail.active'; $idField = 'NewsletterEmail.id'; } if (!empty($Model)) { $Model->updateAll(array($activeField => 0), array($idField => array_keys($ids))); } } $NewsletterEvent->updateAll(array('processed' => 1), array('NewsletterEvent.id' => $proccessed)); }
function alterEmailQuery($opt = array(), $reset = true) { $opt = Sendlist::addSendlistsEmailCond($this->id, $opt, $reset); if (!empty($opt['active'])) { $opt['conditions'][$this->EmailModel->alias . '.active'] = 1; } return $opt; }
function admin_edit($id = null) { if (!$id && empty($this->data)) { $this->Session->setFlash(__d('newsletter', 'Invalid NewsletterSendlist', true)); $this->redirect(array('action' => 'index')); } if (!empty($this->data)) { if ($this->NewsletterSendlist->save($this->data)) { $this->Session->setFlash(__d('newsletter', 'The NewsletterSendlist has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__d('newsletter', 'The NewsletterSendlist could not be saved. Please, try again.', true)); } } if (empty($this->data)) { $this->data = $this->NewsletterSendlist->read(null, $id); } App::import('Lib', 'Newsletter.Sendlist'); $this->set('tabled', Sendlist::isTabled($id)); }
function _build($sending) { App::import('Lib', 'Newsletter.Sendlist'); if (is_numeric($sending)) { $sending = $this->NewsletterSending->read(null, $sending); } if (empty($sending)) { $this->_consoleOut(false, __d('newsletter', 'Invalid Newsletter Sending', true), array('exit' => true)); } $id = $sending['NewsletterSending']['id']; $db =& ConnectionManager::getDataSource($this->NewsletterSended->useDbConfig); $this->_updateProcessTime($id); $this->_consoleOut($id, __d('newsletter', 'Start Building Sending', true)); //=========================== Close Cancelled Sending =========================== $toClose = $this->NewsletterSending->find('list', array('fields' => array('id', 'id'), 'conditions' => array('active' => 0, 'newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'not' => array('status' => array('done', 'cancelled'))), 'recursive' => -1)); if (!empty($toClose)) { $this->NewsletterSending->cancel($toClose, false); $this->_consoleOut($id, sprintf(__d('newsletter', '%s old sending cancelled', true), count($toClose))); } //=========================== Data =========================== $basicInfo = array('active' => 1, 'status' => "ready", 'sending_id' => $id, 'newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'date' => date('Y-m-d H:i:s'), 'email' => null); //=========================== Split Dynamic sendlists =========================== $sendlists = array(); $dynSendlists = array(); if (!empty($sending['NewsletterSending']['selected_lists'])) { foreach ($sending['NewsletterSending']['selected_lists'] as $newsletterSendlist) { if (Sendlist::isTabled($newsletterSendlist)) { $dynSendlists[] = $newsletterSendlist; } else { $sendlists[] = $newsletterSendlist; } } } //=========================== Grouping =========================== $grouping = $sending['Newsletter']['TemplateConfig']->getGrouping($sending); if (!empty($grouping)) { $this->_consoleOut($id, __d('newsletter', 'Calculating variants', true)); $code = sha1(serialize($grouping['fields'])); $groups = array($code => array('active' => 1, 'conditions' => $grouping['fields'], 'newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'code' => $code)); $listsGroups = array(); if (!empty($sendlists)) { //--------- normal sendlists --------- $findOpt = Sendlist::addSendlistsEmailCond($sendlists, array('fields' => array('NewsletterEmail.id', 'NewsletterEmail.name', 'NewsletterEmail.email'), 'conditions' => array('NewsletterEmail.active' => 1), 'group' => 'NewsletterEmail.id', 'recursive' => -1)); $findOpt = $this->_groupingBaseFindOpt($grouping, array_keys($this->NewsletterSendlist->NewsletterEmail->schema())); if (!empty($findOpt)) { $lgroups = $this->NewsletterSendlist->NewsletterEmail->find('all', $findOpt); foreach ($lgroups as $group) { $listsGroups['Basic'][] = array('cond' => $group[$this->NewsletterSendlist->NewsletterEmail->alias]); } } } if (!empty($dynSendlists)) { //--------- dynamic sendlists --------- foreach ($dynSendlists as $newsletterSendlist) { $sendlist = Sendlist::getSendlist($newsletterSendlist); $cgrouping = $grouping; if ($cgrouping['bySendlist']) { $cgrouping = $sending['Newsletter']['TemplateConfig']->getGrouping($sending, $sendlist); } $findOpt = $this->_groupingBaseFindOpt($cgrouping, array_keys($sendlist->emailFields())); if (!empty($findOpt)) { $lgroups = $sendlist->findEmail('all', $findOpt); foreach ($lgroups as $group) { $listsGroups[$newsletterSendlist][] = array('cond' => $group[$sendlist->EmailModel->alias]); } } } } foreach ($listsGroups as &$lgroup) { foreach ($lgroup as &$group) { $cond = array_merge($cgrouping['fields'], $group['cond']); $code = sha1(serialize($cond)); if (!isset($groups[$code])) { $groups[$code] = array('active' => 1, 'conditions' => $cond, 'newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'code' => $code); } $group['variant'] =& $groups[$code]; } } unset($lgroup); unset($group); $this->_consoleOut($id, __d('newsletter', 'Save variants', true)); $existing = $this->NewsletterVariant->find('list', array('fields' => array('code', 'id'), 'conditions' => array('newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'code' => array_keys($groups)), 'recursive' => -1)); foreach ($groups as &$group) { if (isset($existing[$group['code']])) { $group['id'] = $existing[$group['code']]; } else { $this->NewsletterVariant->create(); $this->NewsletterVariant->save($group); $group['id'] = $this->NewsletterVariant->id; } } unset($group); //debug($listsGroups); } //debug($sending); $queries = array(); //=========================== Build normal sendlists =========================== $this->_consoleOut($id, sprintf(__d('newsletter', '%s normal Sendlists found', true), count($sendlists))); if (!empty($sendlists)) { $lGroups = !empty($listsGroups['Basic']) ? $listsGroups['Basic'] : array(null); foreach ($lGroups as $group) { //If any sendeded has been added in this same group we assume it the group was allready added by an interucpted build or a parralel process and we skip the query $findOpt = array('conditions' => array('tabledlist_id IS NULL', 'email_id IS NOT NULL', 'sending_id' => $id), 'recursive' => -1); if (!empty($group)) { $findOpt['conditions']['newsletter_variant_id'] = $group['variant']['id']; } if (!$this->NewsletterSended->find('count', $findOpt)) { $findOpt = Sendlist::addSendlistsEmailCond($sendlists, array('fields' => array('NewsletterEmail.id', 'NewsletterEmail.name', 'NewsletterEmail.email'), 'conditions' => array('NewsletterEmail.active' => 1), 'group' => 'NewsletterEmail.id', 'recursive' => -1, 'msg' => '')); if ($sending['NewsletterSending']['check_sended']) { $findOpt['joins'][] = array('table' => $this->NewsletterSended->useTable, 'alias' => $this->NewsletterSended->alias, 'type' => 'left', 'foreignKey' => false, 'conditions' => array($this->NewsletterSendlist->NewsletterEmail->alias . '.id = ' . $this->NewsletterSended->alias . '.email_id', $this->NewsletterSended->alias . '.newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'not' => array($this->NewsletterSended->alias . '.status' => 'cancelled'))); $findOpt['conditions'][] = $this->NewsletterSended->alias . '.id IS NULL'; } if (!empty($group)) { $findOpt['conditions'][] = $group['cond']; $findOpt['fields']['newsletter_variant_id'] = $group['variant']['id']; $findOpt['msg'] .= sprintf(__d('newsletter', 'Variant id : %s', true), $group['variant']['id']); } else { $findOpt['msg'] .= __d('newsletter', 'Normal sendlists', true); } //debug($findOpt); $findOpt['model'] = $this->NewsletterSendlist->NewsletterEmail; $queries[] = $findOpt; } $this->_updateProcessTime($id, true); } } /* foreach($sendlists as $list){ $this->_consoleOut($id,sprintf(__d('newsletter','Get query for sendlist id : %s', true),$list)); $this->NewsletterSendlist->NewsletterEmail->bindModel(array( 'hasOne' => array( 'NewsletterSendlistsEmail' => array( 'className' => 'Newsletter.NewsletterSendlistsEmail' ) ) ),false); $mailsFindOptions = array( 'fields'=>array('NewsletterEmail.id','NewsletterEmail.name','NewsletterEmail.email'), 'conditions'=>array('NewsletterSendlistsEmail.newsletter_sendlist_id'=>$list,'NewsletterEmail.active'=>1) ); $lGroups = !empty($listsGroups[$list])?$listsGroups[$list]:array(null); foreach($lGroups as $group){ $finalFindOptions = Set::merge($mailsFindOptions,(array)$findOptions); if(!empty($group)){ $finalFindOptions['conditions'][] = $group['cond']; $finalFindOptions['fields']['newsletter_variant_id'] = $group['variant']['id']; } $finalFindOptions['fields']['sendlist_id'] = $list; $finalFindOptions['model'] = $this->NewsletterSendlist->NewsletterEmail; $queries[] = $finalFindOptions; } }*/ //=========================== Build Dynamic sendlists =========================== $this->_consoleOut($id, sprintf(__d('newsletter', '%s dynamic Sendlists found', true), count($dynSendlists))); if (!empty($dynSendlists)) { $findOptions = array(); $join = array('table' => $this->NewsletterSended->useTable, 'alias' => $this->NewsletterSended->alias, 'type' => 'left', 'foreignKey' => false, 'conditions' => array($this->NewsletterSendlist->NewsletterEmail->alias . '.email = ' . $this->NewsletterSended->alias . '.email', $this->NewsletterSended->alias . '.newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'not' => array($this->NewsletterSended->alias . '.status' => 'cancelled'))); if (!$sending['NewsletterSending']['check_sended']) { $join['conditions'][$this->NewsletterSended->alias . '.sending_id'] = $id; } $opt = array('joins' => array($join), 'conditions' => array($this->NewsletterSended->alias . '.email IS NULL')); $findOptions = Set::merge($findOptions, (array) $opt); foreach ($dynSendlists as $list) { $this->_consoleOut($id, sprintf(__d('newsletter', 'Get query for Dynamic sendlist id : %s', true), $list)); $tableSendlist = $this->NewsletterFunct->getTableSendlistID($list, true); $lGroups = !empty($listsGroups[$list]) ? $listsGroups[$list] : array(null); foreach ($lGroups as $group) { $finalFindOptions = $findOptions; $finalFindOptions['group'] = 'NewsletterEmail.email'; if (!empty($group)) { $finalFindOptions['conditions'][] = $group['cond']; } if ($tableSendlist['modelClass']->useDbConfig != $this->NewsletterSended->useDbConfig) { $finalFindOptions = array(); if (!empty($group)) { $finalFindOptions['conditions'][] = $group['cond']; } $finalFindOptions = $this->NewsletterFunct->tabledEmailGetFindOptions($list, true); $finalFindOptions['tableSendlist'] = $tableSendlist; } else { $finalFindOptions = $this->NewsletterFunct->tabledEmailGetFindOptions($list, true, $finalFindOptions); } $finalFindOptions['msg'] = sprintf(__d('newsletter', 'Dynamic sendlist id : %s', true), $list); if (!empty($group)) { $finalFindOptions['msg'] .= '; ' . sprintf(__d('newsletter', 'Variant id : %s', true), $group['variant']['id']); $finalFindOptions['fields']['newsletter_variant_id'] = $group['variant']['id']; } $finalFindOptions['fields']['tabledlist_id'] = $list; $queries[] = $finalFindOptions; } $this->_updateProcessTime($id, true); } } /*if(!empty($grouping)){ debug($grouping); }*/ /*foreach ($queries as $q) { $q['model'] = $q['model']->alias; debug($q); }*/ //exit(); //=========================== Save Queries =========================== foreach ($queries as $query) { //--- normalize Queries --- $fields = $this->NewsletterFunct->fieldsAddAlias($query['fields']); $insertFields = $this->NewsletterSended->tcheckSaveFields(array_keys($fields)); //debug($insertFields); $fields = array_intersect_key($fields, array_flip($insertFields)); $query['fields'] = $fields; if ($query['model']->useDbConfig != $this->NewsletterSended->useDbConfig) { //--------------- external database --------------- $this->_consoleOut($id, sprintf(__d('newsletter', 'The sendlist id : %s Is using an external Database', true), $query['fields']['tabledlist_id'])); $this->_consoleOut($id, sprintf(__d('newsletter', 'Retrieving data', true), $query['fields']['tabledlist_id'])); $tableSendlist = $query['tableSendlist']; //debug($tableSendlist); unset($query['tableSendlist']); $query['limit'] = 200; $i = 0; do { $query['page'] = $i + 1; App::import('Lib', 'Newsletter.QueryUtil'); $emails = $query['model']->find('all', QueryUtil::standardizeFindOptions($query)); if (!empty($emails)) { $this->_consoleOut($id, sprintf(__d('newsletter', '%s Email read', true), count($emails))); //debug($emails); App::import('Lib', 'Newsletter.SetMulti'); $adresses = Set::extract('{n}.' . $query['model']->alias . '.email', $emails); //debug($adresses); //--- get duplicata --- $dupFindOpt = array('fields' => array('id', 'email'), 'conditions' => array('email' => $adresses, 'newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'not' => array($this->NewsletterSended->alias . '.status' => 'cancelled')), 'recursive' => -1); if (!$sending['NewsletterSending']['check_sended']) { $dupFindOpt['conditions']['sending_id'] = $id; } $duplicata = $this->NewsletterSended->find('list', $dupFindOpt); if (!empty($duplicata)) { $this->_consoleOut($id, sprintf(__d('newsletter', '%s dupliqued email Ignored', true), count($duplicata))); //debug($duplicata); } //--- format data --- $toSave = array(); foreach ($emails as $mail) { $mailData = $this->NewsletterFunct->tabledEmailGetFields($mail, $tableSendlist); if (!in_array($mailData['email'], $duplicata)) { $mailData = array_intersect_key($mailData, array_flip($insertFields)); $mailData['email_id'] = $mailData['id']; unset($mailData['id']); $mailData = array_merge($basicInfo, $mailData); $toSave[] = $mailData; } } //debug($toSave); //--- save --- if (!empty($toSave)) { $toSaveSql = array(); foreach ($toSave as $d) { $toSaveSql[] = "(" . implode(",", $this->NewsletterFunct->valFields($d)) . ")"; } $insertStatement = 'INSERT INTO ' . $db->fullTableName($this->NewsletterSended) . ' (`' . implode("`,`", array_keys($toSave[0])) . '`) VALUES ' . implode(",", $toSaveSql); //debug($insertStatement); if ($db->execute($insertStatement)) { $this->_consoleOut($id, sprintf(__d('newsletter', '%s saved Emails', true), count($toSave))); } /*if($this->NewsletterSended->saveAll($toSave)){ $this->_consoleOut($id,sprintf(__d('newsletter','%s saved Emails', true),count($toSave))); }*/ } $this->_updateProcessTime($id, true); } $i++; /*if($i>=3){ $viewClass = $this->view; if ($viewClass != 'View') { list($plugin, $viewClass) = pluginSplit($viewClass); $viewClass = $viewClass . 'View'; App::import('View', $this->view); } $View = new $viewClass($this, false); echo $View->element('sql_dump'); exit(); }*/ } while (!empty($emails)); /*$this->_consoleOut($id, __d('newsletter','External Database lists are not supported yet', true), array('exit'=>true) );*/ } else { $query['fields']['email_id'] = $query['fields']['id']; unset($query['fields']['id']); $query['fields'] = array_merge($this->NewsletterFunct->valFields($basicInfo), $query['fields']); App::import('Lib', 'Newsletter.QueryUtil'); $selectStatement = $db->buildStatement(QueryUtil::standardizeFindOptions($query), $query['model']); //--- make insert Queries --- $fields = $this->NewsletterFunct->fieldsAddAlias($query['fields']); $insertFields = $this->NewsletterSended->tcheckSaveFields(array_keys($fields)); $insertQuery = array('table' => $db->fullTableName($this->NewsletterSended), 'fields' => array(), 'select' => $selectStatement); foreach ($insertFields as $f) { $insertQuery['fields'][] = $db->name($f); } $insertQuery['fields'] = implode(', ', $insertQuery['fields']); $insertStatement = 'INSERT INTO ' . $insertQuery['table'] . ' (' . $insertQuery['fields'] . ') (' . $insertQuery['select'] . ')'; $msg = str_replace('%msg%', $query['msg'], __d('newsletter', 'Execute query : %msg%', true)); $this->_consoleOut($id, $msg); if ($db->execute($insertStatement)) { $this->_consoleOut($id, sprintf(__d('newsletter', '%s saved Emails', true), $this->NewsletterSended->getAffectedRows())); } else { $this->_consoleOut($id, __d('newsletter', 'Could not save emails', true), array('exit' => true)); } $this->_updateProcessTime($id, true); } } //=========================== Build Aditionnal Emails =========================== if (!empty($sending['NewsletterSending']['additional_emails'])) { $this->_consoleOut($id, __d('newsletter', 'Build Aditionnal Emails', true)); //--- format emails --- $emails = explode(',', $sending['NewsletterSending']['additional_emails']); $named = "/^<([^>]*)>(.*)\$/"; $add_emails = array(); foreach ($emails as $key => $email) { $email = array('email' => trim($email)); if (preg_match($named, $email['email'], $match)) { $email['name'] = $match[1]; $email['email'] = $match[2]; } $email = array_merge($basicInfo, $email); $add_emails[$email['email']] = $email; } //--- tcheck for duplicate --- $findOpt = array('fields' => array('DISTINCT email'), 'conditions' => array('email' => array_keys($add_emails), 'newsletter_id' => $sending['NewsletterSending']['newsletter_id'], 'not' => array($this->NewsletterSended->alias . '.status' => 'cancelled'))); if (!$sending['NewsletterSending']['check_sended']) { $findOpt['conditions']['sending_id'] = $id; } $this->NewsletterSended->recursive = -1; $dub = $this->NewsletterSended->find('all', $findOpt); if (!empty($dub)) { $this->_consoleOut($id, sprintf(__d('newsletter', '%s dupliqued Email found, they will be ignored', true), count($dub))); foreach ($dub as $key => $val) { $dub[$key] = $val['NewsletterSended']['email']; } $add_emails = array_diff_key($add_emails, array_flip($dub)); } //--- save --- if (!empty($add_emails)) { if ($this->NewsletterSended->createMany(array_keys($basicInfo), array_values($add_emails))) { $this->_consoleOut($id, sprintf(__d('newsletter', '%s saved Emails', true), $this->NewsletterSended->getAffectedRows())); } else { $this->_consoleOut($id, __d('newsletter', 'Could not save emails', true), array('exit' => true)); } } else { $this->_consoleOut($id, __d('newsletter', 'No Emails saved', true)); } } //=========================== Done =========================== $this->NewsletterSending->create(); $data = array('NewsletterSending' => array('id' => $id, 'status' => 'render')); if ($this->NewsletterSending->save($data)) { $this->_consoleOut($id, __('Building Complete.', true)); } else { $this->_consoleOut($id, __('Could not start Sending', true), array('exit' => true)); } return true; }
function admin_index($listId = null) { $q = null; if (isset($this->data['q']) && !empty($this->data['q'])) { $q = $this->data['q']; $this->params['named']['q'] = $this->data['q']; } elseif (isset($this->params['named']['q']) && !empty($this->params['named']['q'])) { $q = $this->params['named']['q']; } if (!$listId && !empty($this->params['named']['id'])) { $listId = $this->params['named']['id']; } if ($listId) { App::import('Lib', 'Newsletter.Sendlist'); $sendlist = Sendlist::getSendlist($listId); $findOptions = array('search' => $q); $findOptions = $sendlist->emailQuery($findOptions, false); //debug($findOptions); $this->paginate = $findOptions; $mails = $this->paginate($sendlist->EmailModel); //debug($mails); $newsletterEmails = $sendlist->parseResult($mails, array('alias' => 'NewsletterEmail', 'local' => true)); //debug($newsletterEmails); if ($sendlist->type == 'tabled') { $this->set('fields', $sendlist->emailFields()); $toRender = 'tabled_email'; } $this->set('sendlist', $sendlist->getInfo()); } else { if ($q != null) { $this->paginate['conditions'] = array('OR' => array(Inflector::singularize($this->name) . '.email LIKE' => '%' . $q . '%', Inflector::singularize($this->name) . '.name LIKE' => '%' . $q . '%')); } $this->NewsletterEmail->recursive = 0; $newsletterEmails = $this->paginate(); } $this->set('newsletterEmails', $newsletterEmails); if (isset($toRender)) { $this->render('admin_' . $toRender); } }
function disable_email($email, $user_action = false) { App::import('Lib', 'Newsletter.Sendlist'); return Sendlist::disable_email($email, $user_action); }