* @package Newsletter */ /** * Load base class */ WBClass::load('WBContent' , 'WBContent_Newsletter'); /** * Content component: Newsletter Subscriber * * @version 0.2.0 * @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()); } 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; } $this->sub->approve(); $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 '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(); return false; } /** * Show Newsletter Profile * */ private function showProfile() { $this->loadTemplates('profile'); $this->tmpl->addGlobalVars($this->sub->get()); } /** * Register Subscriber * * Prepare form processor. Merge values with usar data and current locale settings. */ private function register() { $values = $this->user->getData(); $values = array(); 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); 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; } }