public function createAction() { if (!$this->_helper->requireUser()->isValid()) { return; } if (!$this->_helper->requireAuth()->setAuthParams('poll', null, 'create')->isValid()) { return; } // Get navigation $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('poll_main'); $this->view->options = array(); $this->view->maxOptions = $max_options = Engine_Api::_()->getApi('settings', 'core')->getSetting('poll.maxoptions', 15); $this->view->form = $form = new Poll_Form_Create(); $viewer = Engine_Api::_()->user()->getViewer(); if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($this->getRequest()->getPost())) { return; } // Check options $options = (array) $this->_getParam('optionsArray'); $options = array_filter(array_map('trim', $options)); $options = array_slice($options, 0, $max_options); $this->view->options = $options; if (empty($options) || !is_array($options) || count($options) < 2) { return $form->addError('You must provide at least two possible answers.'); } foreach ($options as $index => $option) { if (strlen($option) > 80) { $options[$index] = Engine_String::substr($option, 0, 80); } } // Process $pollTable = Engine_Api::_()->getItemTable('poll'); $pollOptionsTable = Engine_Api::_()->poll()->api()->getDbtable('options', 'poll'); $db = $pollTable->getAdapter(); $db->beginTransaction(); try { $values = $form->getValues(); $values['user_id'] = $viewer->getIdentity(); // Create poll $poll = $pollTable->createRow(); $poll->setFromArray($values); $poll->save(); // Create options $censor = new Engine_Filter_Censor(); foreach ($options as $option) { $pollOptionsTable->insert(array('poll_id' => $poll->getIdentity(), 'poll_option' => $censor->filter($option))); } // Privacy $auth = Engine_Api::_()->authorization()->context; $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone'); if (empty($values['auth_view'])) { $values['auth_view'] = array('everyone'); } if (empty($values['auth_comment'])) { $values['auth_comment'] = array('everyone'); } $viewMax = array_search($values['auth_view'], $roles); $commentMax = array_search($values['auth_comment'], $roles); foreach ($roles as $i => $role) { $auth->setAllowed($poll, $role, 'view', $i <= $viewMax); $auth->setAllowed($poll, $role, 'comment', $i <= $commentMax); } $auth->setAllowed($poll, 'registered', 'vote', true); $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } // Process activity $db = Engine_Api::_()->getDbTable('polls', 'poll')->getAdapter(); $db->beginTransaction(); try { $action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity(Engine_Api::_()->user()->getViewer(), $poll, 'poll_new'); if ($action) { Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $poll); } $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } // Redirect return $this->_helper->redirector->gotoUrl($poll->getHref(), array('prependBase' => false)); }
public function init() { parent::init(); $this->setTitle('Edit Poll Privacy')->setDescription('Edit your poll privacy below, then click "Save Privacy" to apply the new privacy settings for the poll.'); $this->submit->setLabel('Save Privacy'); }