public static function initiateSending($mailId, $subject, $receivers, $autoactive = 0, $reply = null, $reply_name = null) { /* * Grabar el contenido para el sinoves en la tabla mail, obtener el id y el codigo para sinoves * */ try { Model::query("START TRANSACTION"); $sql = "INSERT INTO `mailer_content` (`id`, `active`, `mail`, `subject`, `blocked`, `reply`, `reply_name`)\n VALUES ('' , '{$autoactive}', :mail, :subject, 0, :reply, :reply_name)"; Model::query($sql, array(':subject' => $subject, ':mail' => $mailId, ':reply' => $reply, ':reply_name' => $reply_name)); $mailing = Model::insertId(); // destinatarios $sql = "INSERT INTO `mailer_send` (`id`, `mailing`, `user`, `email`, `name`)\n VALUES ('', :mailing, :user, :email, :name)"; foreach ($receivers as $user) { Model::query($sql, array(':mailing' => $mailing, ':user' => $user->user, ':email' => $user->email, ':name' => $user->name)); } Model::query("COMMIT"); return true; } catch (\PDOException $e) { echo "HA FALLADO!!" . $e->getMessage(); die; return false; } }
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)); } }
/** * Cuerpo del texto en HTML para los clientes de correo con formato habilitado. * * Se mete el contenido alrededor del diseño de email de Diego * */ private function bodyHTML($plain = false) { $viewData = array('content' => $this->content); // grabamos el contenido en la tabla de envios // especial para newsletter, solo grabamos un sinoves if ($this->template == 33) { if (!empty($_SESSION['NEWSLETTER_SENDID'])) { $sendId = $_SESSION['NEWSLETTER_SENDID']; } else { $sql = "INSERT INTO mail (id, email, html, template, node) VALUES ('', :email, :html, :template, :node)"; $values = array(':email' => 'any', ':html' => $this->content, ':template' => $this->template, ':node' => $_SESSION['admin_node']); $query = Model::query($sql, $values); $sendId = Model::insertId(); $_SESSION['NEWSLETTER_SENDID'] = $sendId; } $the_mail = 'any'; } else { $sql = "INSERT INTO mail (id, email, html, template) VALUES ('', :email, :html, :template)"; $values = array(':email' => $this->to, ':html' => str_replace('cid:logo', SITE_URL . '/goteo_logo.png', $this->content), ':template' => $this->template); $query = Model::query($sql, $values); $sendId = Model::insertId(); $the_mail = $this->to; } if (!empty($sendId)) { // token para el sinoves $token = md5(uniqid()) . '¬' . $the_mail . '¬' . $sendId; $viewData['sinoves'] = \SITE_URL . '/mail/' . base64_encode($token) . '/?email=' . $this->to; } else { $viewData['sinoves'] = \SITE_URL . '/contact'; } $_SESSION['MAILING_TOKEN'] = $viewData['sinoves']; $viewData['baja'] = \SITE_URL . '/user/leave/?email=' . $this->to; if ($plain) { return strip_tags($this->content) . ' ' . $viewData['sinoves']; } else { // para plantilla boletin if ($this->template == 33) { $viewData['baja'] = \SITE_URL . '/user/leave/?unsuscribe=newsletter&email=' . $this->to; return new View(GOTEO_PATH . '/view/email/newsletter.html.php', $viewData); } else { return new View('view/email/goteo.html.php', $viewData); } } }
/** * Realiza el envio masivo a participantees o cofinanciadores * * @param type $option 'messegers' || 'rewards' * @param type $project Instancia del proyecto de trabajo * @return boolean */ public static function process_mailing($option, $project) { $who = array(); // verificar que hay mensaje if (empty($_POST['message'])) { Message::Error(Text::get('dashboard-investors-mail-text-required')); return false; } else { $msg_content = nl2br(\strip_tags($_POST['message'])); } // si a todos los participantes if ($option == 'messegers' && !empty($_POST['msg_all'])) { // a todos los participantes foreach (Model\Message::getMessegers($project->id) as $messeger => $msgData) { if ($messeger == $project->owner) { continue; } $who[$messeger] = $messeger; // unset($msgData); // los datos del mensaje del participante no se usan } } elseif ($option == 'rewards' && !empty($_POST['msg_all'])) { // a todos los cofinanciadores foreach (Model\Invest::investors($project->id, false, true) as $user => $investor) { // no duplicar $who[$investor->user] = $investor->user; } } elseif (!empty($_POST['msg_user'])) { // a usuario individual $who[$_POST['msg_user']] = $_POST['msg_user']; } elseif ($option == 'rewards') { $msg_rewards = array(); // estos son msg_reward-[rewardId], a un grupo de recompensa foreach ($_POST as $key => $value) { $parts = explode('-', $key); if ($parts[0] == 'msg_reward' && $value == 1) { $msg_rewards[] = $parts[1]; } } // para cada recompensa foreach ($msg_rewards as $reward) { foreach (Model\Invest::choosed($reward) as $user) { $who[$user] = $user; } } } // no hay destinatarios if (count($who) == 0) { Message::Error(Text::get('dashboard-investors-mail-nowho')); return false; } // obtener contenido // segun destinatarios $allsome = explode('/', Text::get('regular-allsome')); $enviandoa = !empty($_POST['msg_all']) ? $allsome[0] : $allsome[1]; if ($option == 'messegers') { Message::Info(Text::get('dashboard-messegers-mail-sendto', $enviandoa)); } else { Message::Info(Text::get('dashboard-investors-mail-sendto', $enviandoa)); } // Obtenemos la plantilla para asunto y contenido $template = Template::get(2); // Sustituimos los datos if (!empty($_POST['subject'])) { $subject = $_POST['subject']; } else { $subject = str_replace('%PROJECTNAME%', $project->name, $template->title); } $remite = $project->name . ' ' . Text::get('regular-from') . ' '; $remite .= NODE_ID != GOTEO_NODE ? NODE_NAME : GOTEO_MAIL_NAME; $search = array('%MESSAGE%', '%PROJECTNAME%', '%PROJECTURL%', '%OWNERURL%', '%OWNERNAME%'); $replace = array($msg_content, $project->name, SITE_URL . "/project/" . $project->id, SITE_URL . "/user/profile/" . $project->owner, $project->user->name); $content = \str_replace($search, $replace, $template->text); // para usar el proceso Sender: // - $who debe ser compatible con el formato $receivers // (falta nombre e email), sacarlo con getMini $receivers = array(); foreach ($who as $userId) { $user = Model\User::getMini($userId); $user->user = $user->id; $receivers[] = $user; } // - en la plantilla hay que cambiar %NAME% por %USERNAME% para que sender reemplace // - // - se crea un registro de tabla mail $sql = "INSERT INTO mail (id, email, html, template, node) VALUES ('', :email, :html, :template, :node)"; $values = array(':email' => 'any', ':html' => $content, ':template' => $template->id, ':node' => \GOTEO_NODE); $query = \Goteo\Core\Model::query($sql, $values); $mailId = \Goteo\Core\Model::insertId(); // - se usa el metodo initializeSending para grabar el envío (parametro para autoactivar) // , también metemos el reply y repplyName (remitente) en la instancia de envío if (\Goteo\Library\Sender::initiateSending($mailId, $subject, $receivers, 1, $project->user->email, $remite)) { Message::Info(Text::get('dashboard-investors-mail-sended', 'la cola de envíos')); // cambiar este texto } else { Message::Error(Text::get('dashboard-investors-mail-fail', 'la cola de envíos')); // cambiar este texto } return true; }
public static function process($action = 'list', $id = null, $filters = array()) { // año fiscal $year = Model\User\Donor::$currYear; $year0 = $year; $year1 = $year - 1; $errors = array(); $node = isset($_SESSION['admin_node']) ? $_SESSION['admin_node'] : \GOTEO_NODE; // Valores de filtro $interests = Model\User\Interest::getAll(); $status = Model\Project::status(); $methods = Model\Invest::methods(); $types = array('investor' => 'Cofinanciadores', 'owner' => 'Autores', 'user' => 'Usuarios'); $roles = array('admin' => 'Administrador', 'checker' => 'Revisor', 'translator' => 'Traductor'); // una variable de sesion para mantener los datos de todo esto if (!isset($_SESSION['mailing'])) { $_SESSION['mailing'] = array(); } switch ($action) { case 'edit': $_SESSION['mailing']['receivers'] = array(); $values = array(); $sqlFields = ''; $sqlInner = ''; $sqlFilter = ''; // cargamos los destiantarios //---------------------------- // por tipo de usuario switch ($filters['type']) { case 'investor': $sqlInner .= "INNER JOIN invest\n ON invest.user = user.id\n AND (invest.status = 0 OR invest.status = 1 OR invest.status = 3 OR invest.status = 4)\n INNER JOIN project\n ON project.id = invest.project\n "; $sqlFields .= ", project.name as project"; $sqlFields .= ", project.id as projectId"; break; case 'owner': $sqlInner .= "INNER JOIN project\n ON project.owner = user.id\n "; $sqlFields .= ", project.name as project"; $sqlFields .= ", project.id as projectId"; break; default: break; } $_SESSION['mailing']['filters_txt'] = 'los <strong>' . $types[$filters['type']] . '</strong> '; if (!empty($filters['project']) && !empty($sqlInner)) { $sqlFilter .= " AND project.name LIKE (:project) "; $values[':project'] = '%' . $filters['project'] . '%'; $_SESSION['mailing']['filters_txt'] .= 'de proyectos que su nombre contenga <strong>\'' . $filters['project'] . '\'</strong> '; } elseif (empty($filters['project']) && !empty($sqlInner)) { $_SESSION['mailing']['filters_txt'] .= 'de cualquier proyecto '; } if (isset($filters['status']) && $filters['status'] > -1 && !empty($sqlInner)) { $sqlFilter .= "AND project.status = :status "; $values[':status'] = $filters['status']; $_SESSION['mailing']['filters_txt'] .= 'en estado <strong>' . $status[$filters['status']] . '</strong> '; } elseif ($filters['status'] < 0 && !empty($sqlInner)) { $_SESSION['mailing']['filters_txt'] .= 'en cualquier estado '; } if ($filters['type'] == 'investor') { if (!empty($filters['method']) && !empty($sqlInner)) { $sqlFilter .= "AND invest.method = :method "; $values[':method'] = $filters['method']; $_SESSION['mailing']['filters_txt'] .= 'mediante <strong>' . $methods[$filters['method']] . '</strong> '; } elseif (empty($filters['method']) && !empty($sqlInner)) { $_SESSION['mailing']['filters_txt'] .= 'mediante cualquier metodo '; } } if (!empty($filters['interest'])) { $sqlInner .= "INNER JOIN user_interest\n ON user_interest.user = user.id\n AND user_interest.interest = :interest\n "; $values[':interest'] = $filters['interest']; if ($filters['interest'] == 15) { $_SESSION['mailing']['filters_txt'] .= 'del grupo de testeo '; } else { $_SESSION['mailing']['filters_txt'] .= 'interesados en fin <strong>' . $interests[$filters['interest']] . '</strong> '; } } if (!empty($filters['role'])) { $sqlInner .= "INNER JOIN user_role\n ON user_role.user_id = user.id\n AND user_role.role_id = :role\n "; $values[':role'] = $filters['role']; $_SESSION['mailing']['filters_txt'] .= 'que sean <strong>' . $roles[$filters['role']] . '</strong> '; } if (!empty($filters['name'])) { $sqlFilter .= " AND ( user.name LIKE (:name) OR user.email LIKE (:name) ) "; $values[':name'] = '%' . $filters['name'] . '%'; $_SESSION['mailing']['filters_txt'] .= 'que su nombre o email contenga <strong>\'' . $filters['name'] . '\'</strong> '; } if (!empty($filters['donant'])) { if ($filters['type'] == 'investor') { $sqlFilter .= " AND invest.resign = 1\n AND invest.status IN (1, 3)\n AND invest.charged >= '{$year0}-01-01'\n AND invest.charged < '{$year1}-01-01'\n AND (project.passed IS NOT NULL AND project.passed != '0000-00-00')\n "; $_SESSION['mailing']['filters_txt'] .= 'que haya hecho algun donativo '; } else { Message::Error('Solo se filtran donantes si se envia "A los: Cofinanciadores"'); } } if ($node != \GOTEO_NODE) { $sqlFilter .= " AND user.node = :node"; $values[':node'] = $node; if (!empty($sqlInner)) { $sqlFilter .= " AND project.node = :node"; } } $sql = "SELECT\n user.id as id,\n user.id as user,\n user.name as name,\n user.email as email\n {$sqlFields}\n FROM user\n {$sqlInner}\n WHERE user.active = 1\n {$sqlFilter}\n GROUP BY user.id\n ORDER BY user.name ASC\n "; // die('<pre>'.$sql . '<br />'.print_r($values, 1).'</pre>'); if ($query = Model\User::query($sql, $values)) { foreach ($query->fetchAll(\PDO::FETCH_OBJ) as $receiver) { $_SESSION['mailing']['receivers'][$receiver->id] = $receiver; } } else { Message::Error('Fallo el SQL!!!!! <br />' . $sql . '<pre>' . print_r($values, 1) . '</pre>'); } // si no hay destinatarios, salta a la lista con mensaje de error if (empty($_SESSION['mailing']['receivers'])) { Message::Error('No se han encontrado destinatarios para ' . $_SESSION['mailing']['filters_txt']); throw new Redirection('/admin/mailing/list'); } // si hay, mostramos el formulario de envio return new View('view/admin/index.html.php', array('folder' => 'mailing', 'file' => 'edit', 'filters' => $filters, 'interests' => $interests, 'status' => $status, 'types' => $types, 'roles' => $roles)); break; case 'send': // die(\trace($_POST)); $URL = NODE_ID != GOTEO_NODE ? NODE_URL : SITE_URL; // Enviando contenido recibido a destinatarios recibidos $receivers = array(); $subject = $_POST['subject']; $templateId = !empty($_POST['template']) ? $_POST['template'] : 11; $content = \str_replace('%SITEURL%', $URL, $_POST['content']); // quito usuarios desmarcados foreach ($_SESSION['mailing']['receivers'] as $usr => $userData) { $errors = array(); $campo = 'receiver_' . $usr; if (!isset($_POST[$campo])) { $_SESSION['mailing']['receivers'][$usr]->ok = null; } else { $receivers[] = $userData; } } // montamos el mailing // - se crea un registro de tabla mail $sql = "INSERT INTO mail (id, email, html, template, node) VALUES ('', :email, :html, :template, :node)"; $values = array(':email' => 'any', ':html' => $content, ':template' => $templateId, ':node' => $node); $query = \Goteo\Core\Model::query($sql, $values); $mailId = \Goteo\Core\Model::insertId(); // - se usa el metodo initializeSending para grabar el envío (parametro para autoactivar) // - initiateSending ($mailId, $subject, $receivers, $autoactive = 0) if (\Goteo\Library\Sender::initiateSending($mailId, $subject, $receivers, 1)) { $ok = true; // Evento Feed $log = new Feed(); $log->populate('comunicación masiva a usuarios (admin)', '/admin/mailing', \vsprintf("El admin %s ha iniciado una %s a %s", array(Feed::item('user', $_SESSION['user']->name, $_SESSION['user']->id), Feed::item('relevant', 'Comunicacion masiva'), $_SESSION['mailing']['filters_txt']))); $log->doAdmin('admin'); unset($log); } else { $ok = false; // Evento Feed $log = new Feed(); $log->populate('comunicación masiva a usuarios (admin)', '/admin/mailing', \vsprintf("El admin %s le ha %s una %s a %s", array(Feed::item('user', $_SESSION['user']->name, $_SESSION['user']->id), Feed::item('relevant', 'fallado'), Feed::item('relevant', 'Comunicacion masiva'), $_SESSION['mailing']['filters_txt']))); $log->doAdmin('admin'); unset($log); } return new View('view/admin/index.html.php', array('folder' => 'mailing', 'file' => 'send', 'subject' => $subject, 'interests' => $interests, 'status' => $status, 'methods' => $methods, 'types' => $types, 'roles' => $roles, 'users' => $receivers, 'ok' => $ok)); break; } return new View('view/admin/index.html.php', array('folder' => 'mailing', 'file' => 'list', 'interests' => $interests, 'status' => $status, 'methods' => $methods, 'types' => $types, 'roles' => $roles, 'filters' => $filters)); }