public static function process($action = 'list', $id = null, $filters = array()) { switch ($action) { case 'init': if ($_SERVER['REQUEST_METHOD'] == 'POST') { // plantilla $template = $_POST['template']; // destinatarios if ($_POST['test']) { $users = Boletin::getTesters(); } elseif ($template == 33) { // los destinatarios de newsletter $users = Boletin::getReceivers(); } elseif ($template == 35) { // los destinatarios para testear a subscriptores $users = Boletin::getReceivers(); } // sin idiomas $nolang = $_POST['nolang']; if ($nolang) { $receivers[LANG] = $users; } else { // separamos destinatarios en idiomas $receivers = array(); foreach ($users as $usr) { if (empty($usr->lang) || $usr->lang == LANG) { $receivers[LANG][] = $usr; } else { $receivers[$usr->lang][] = $usr; } } } // idiomas que vamos a enviar $langs = array_keys($receivers); // para cada idioma foreach ($langs as $lang) { // destinatarios $recipients = $receivers[$lang]; // datos de la plantilla $tpl = Template::get($template, $lang); // contenido de newsletter $content = $template == 33 ? Boletin::getContent($tpl->text, $lang) : ($content = $tpl->text); // asunto $subject = $tpl->title; // creamos instancia $sql = "INSERT INTO mail (id, email, html, template) VALUES ('', :email, :html, :template)"; $values = array(':email' => 'any', ':html' => $content, ':template' => $template); $query = \Goteo\Core\Model::query($sql, $values); $mailId = \Goteo\Core\Model::insertId(); // inicializamos el envío if (Sender::initiateSending($mailId, $subject, $recipients, true)) { // ok... } else { Message::Error('No se ha podido iniciar el mailing con asunto "' . $subject . '"'); } } } throw new Redirection('/admin/newsletter'); break; case 'activate': if (Sender::activateSending($id)) { Message::Info('Se ha activado un nuevo envío automático'); } else { Message::Error('No se pudo activar el envío. Iniciar de nuevo'); } throw new Redirection('/admin/newsletter'); break; case 'detail': $mailing = Sender::getSending($id); $list = Sender::getDetail($id, $filters['show']); return new View('view/admin/index.html.php', array('folder' => 'newsletter', 'file' => 'detail', 'detail' => $filters['show'], 'mailing' => $mailing, 'list' => $list)); break; default: $list = Sender::getMailings(); return new View('view/admin/index.html.php', array('folder' => 'newsletter', 'file' => 'list', 'list' => $list)); } }
$file = __DIR__ . '/library/' . strtolower($cls) . '.php'; } if ($file !== false) { include $file; } }); $debug = true; $fail = false; // check the limit /*if (!Mail::checkLimit(null, false, $LIMIT)) { die("LIMIT REACHED\n"); }*/ $itime = microtime(true); $total_users = 0; // cogemos el siguiente envío a tratar $mailing = Sender::getSending(); // si no está activa fin if (!$mailing->active) { if ($debug) { echo "INACTIVE\n"; } die; } if ($mailing->blocked) { if ($debug) { echo "dbg: BLOQUEADO!\n"; } $fail = true; } // voy a parar aquí, antes del bloqueo if ($debug) {
die("Se necesita un identificador de sender como argumento del script!"); } $list = array(); $sql = "SELECT\n mailer_send.id,\n mailer_send.user,\n mailer_send.name,\n mailer_send.email,\n mailer_content.id as mailing_id\n FROM mailer_send\n RIGHT JOIN mailer_content ON mailer_content.id=mailer_send.mailing AND mailer_content.active=1\n WHERE mailer_send.id = ?\n AND mailer_send.sended IS NULL\n AND mailer_send.blocked IS NULL\n "; if ($query = Model::query($sql, array($id))) { $user = $query->fetchObject(); } if (!is_object($user)) { die("No se ha encontrado un usuario válido para el mailer_send.id={$id}\n"); } //si estamos aqui sabemos que el usuari es valido i el mailing tambien if ($debug) { echo "dbg: Fecha inicio " . date("Y-m-d H:i:s") . "\n"; } // cogemos el siguiente envío a tratar $mailing = Sender::getSending($user->mailing_id); // print_r($mailing); // si no está activa fin if (!$mailing->active) { die("Mailing {$user->mailing_id} inactivo!\n"); } // cogemos el contenido y la plantilla desde el historial $query = Model::query('SELECT html, template FROM mail WHERE id = ?', array($mailing->mail)); $data = $query->fetch(\PDO::FETCH_ASSOC); $content = $data['html']; $template = $data['template']; if (empty($content)) { die("Mailing {$user->mailing_id} sin contenido!\n"); } if ($debug) { echo "dbg: Bloqueando registro {$user->id} ({$user->email}) mailing: {$user->mailing_id}\n";