* @package Newsletter */ /** * Load base class */ WBClass::load('WBContent' , 'WBContent_Newsletter'); /** * Content component: Newsletter Subscriber * * @version 0.2.5 * @package Newsletter */ class WBContent_Newsletter_Subscriber extends WBContent_Newsletter { /** * @var WBDatasource_Newsletter_Subscriber */ private $sub; /** * default config of this component * @var array */ protected $config = array( 'action' => 'profile', 'captcha' => 'auto', 'obscureid' => '' ); /** * Run component * * @return array config */ public function run() { $this->sub = WBClass::create('WBDatasource_Newsletter_Subscriber'); if ($this->user->isAuthenticated()) { $this->sub->setUser($this->user); $this->sub->loadByUser($this->user->getId()); } $this->addConfigAsGlobalVars(); // obscure id has priority $oid = $this->config['obscureid']; if (!empty($oid)) { $this->sub->loadByObscureId($oid); } // add obscureid if ($this->sub->isOK()) { $this->tmpl->addGlobalVar('obscureid', $this->sub->getObscureId()); } // if there is mandatorid, make it available $this->tmpl->addGlobalVar('mandatorid', $this->req->get('mandatorid', 0)); $rm = false; switch ($this->config['action']) { case 'register': // don't register if already registered if ($this->sub->isOk()) { $this->showProfile(); break; } $this->register(); break; case 'approve': if (!$this->sub->isOk()) { $this->register(); break; } if ($this->sub->approve()) { $this->addAlert(WBContent::ALERT_TYPE_SUCCESS, patI18n::dgettext('wombat', 'Your subscription has been approved!')); } $this->showProfile(); break; case 'subscribe': if (!$this->sub->isOK()) { $this->register(); break; } $this->sub->subscribe(); $this->showProfile(); break; case 'unsubscribe': if (!$this->sub->isOK()) { $this->register(); break; } $this->sub->unsubscribe(); $this->showProfile(); break; case 'unsubscribemandator': $rm = true; case 'subscribemandator': if (!$this->sub->isOK()) { $this->register(); break; } $mId = $this->req->get('mandatorid', '__invalid'); if ('__invalid' != $mId) { $this->sub->add4Mandator($mId, $rm); if (!$rm) { $this->sub->setAllTopic($mId); } } $this->showProfile(); break; case 'edit': if (!$this->sub->isOk()) { $this->register(); break; } $this->processForm('edit', $this->sub->get()); break; default: case 'profile': $this->showProfile(); break; } } /** * Update Subscriber Data * * Save values to subscriber's profile * * @param patForms * @param array * @return bool always true */ protected function onEditValid($form, $values) { $this->sub->update($values); $this->showProfile(); $this->addAlert(WBContent::ALERT_TYPE_SUCCESS, patI18n::dgettext('wombat', 'Newsletter settings saved.')); return false; } /** * Show Newsletter Profile * */ private function showProfile() { $this->loadTemplates('profile'); $this->tmpl->addGlobalVars($this->sub->get()); $list = $this->sub->getMandatorList(); $this->list2Template($list, 'list_mandator_active'); $list = $this->sub->getMandatorList(false); $this->list2Template($list, 'list_mandator_other'); } /** * Register Subscriber * * Prepare form processor. Merge values with usar data and current locale settings. */ private function register() { $values = $this->user->getData(); WBClass::load('patI18n'); $values['lang'] = patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT); $this->processForm('register', $values); } /** * Actually Register * * Use values and add subscriber * * @param patForms * @param array * @return bool always true */ protected function onRegisterValid($form, $values) { if (empty($values['lang'])) { $values['lang'] = patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT); } $this->sub->add($values); $mId = $this->req->get('mandatorid', 0); $this->sub->add4Mandator($mId); $this->sub->setAllTopic($mId); return true; } /** * Fetch list of form elements * * Use different file regardless of actual form name. * * @param string name of form * @return array $elements */ protected function getFormElementList($name) { $list = parent::getFormElementList($name); if ('register' == $name) { $this->injectElement4Captcha($list, $this->user->isAuthenticated(), $this->config['captcha']); return $list; } if ('edit' != $name) { return $list; } $available = $this->sub->getAvailableLanguages(); if (1 == count($available)) { unset($list['lang']); } return $list; } }