/** * Adds given address to given mailing list. * @param string listname * @param string email * @return boolean status (true=success, false=failure) */ public static function subscribe($listname, $email) { $conn = sfDoctrine::Connection(); try { $conn->beginTransaction(); $sympa = new Sympa(); $sympa->list_subscriber = $listname; $sympa->user_subscriber = $email; $sympa->save(); $conn->commit(); return true; } catch (Exception $e) { // Subscription failed! For instance because email address was already registered. $conn->rollback(); c2cTools::log("Failed adding address {$email} to list {$listname}: " . $e->getMessage()); return false; } }
/** * Handle a page that enables users to registers to various mailing lists. */ public function executeMailinglists() { $user_id = $this->getUser()->getId(); $this->user_private_data = UserPrivateData::find($user_id); $this->email = $this->user_private_data->get('email'); $lists = sfConfig::get('mod_users_mailinglists_values'); if ($this->getRequest()->getMethod() == sfRequest::POST) { $listname = $this->getRequestParameter('listname'); if ($this->getRequestParameter('reason') == 'sub') { Sympa::subscribe($listname, $this->email); $this->statsdIncrement("{$listname}.subscribe"); } else { Sympa::unsubscribe($listname, $this->email); $this->statsdIncrement("{$listname}.unsubscribe"); } } $subscribedLists = Sympa::getSubscribedLists($this->email); $ml_list_subscribed = array(); $ml_list_available = array(); foreach ($lists as $list) { if (in_array($list, $subscribedLists)) { $ml_list_subscribed[] = $list; } else { $ml_list_available[] = $list; } } $this->available_lists = $ml_list_available; $this->subscribed_lists = $ml_list_subscribed; $this->setPageTitle($this->__('mailing lists')); }