public function createPopupAction() { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); // Check authorization to create feedback. if (!$this->_helper->requireAuth()->setAuthParams('ynfeedback_idea', null, 'create')->isValid()) { return; } $viewer = Engine_Api::_()->user()->getViewer(); //get values $values = $this->_getAllParams(); if (empty($values['title']) || $values['title'] == "") { return; } //user_id & status $values['user_id'] = $viewer->getIdentity(); $values['status_id'] = 1; $db = Engine_Db_Table::getDefaultAdapter(); $db->beginTransaction(); try { $class = new Engine_Filter_HtmlSpecialChars(); $values['description'] = $class->filter($values['description']); $class = new Engine_Filter_Censor(); $values['description'] = $class->filter($values['description']); $class = new Engine_Filter_EnableLinks(); $values['description'] = $class->filter($values['description']); //save feedback $ideaTable = Engine_Api::_()->getItemTable('ynfeedback_idea'); $idea = $ideaTable->createRow(); $idea->setFromArray($values); $idea->save(); // Set auth $auth = Engine_Api::_()->authorization()->context; $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'everyone'); if (empty($values['auth_view'])) { $values['auth_view'] = 'everyone'; } if (empty($values['auth_comment'])) { $values['auth_comment'] = 'everyone'; } $viewMax = array_search($values['auth_view'], $roles); $commentMax = array_search($values['auth_comment'], $roles); foreach ($roles as $i => $role) { $auth->setAllowed($idea, $role, 'view', $i <= $viewMax); $auth->setAllowed($idea, $role, 'comment', $i <= $commentMax); } if ($viewer->getIdentity()) { //add activity $activityApi = Engine_Api::_()->getDbtable('actions', 'activity'); $action = $activityApi->addActivity($idea->getOwner(), $idea, 'ynfeedback_feedback_create'); if ($action) { $activityApi->attachActivity($action, $idea); } } if (Engine_Api::_()->hasModuleBootstrap("yncredit")) { if ($viewer->getIdentity()) { $user = $idea->getOwner(); if ($user->getIdentity()) { Engine_Api::_()->yncredit()->hookCustomEarnCredits($user, $user->getTitle(), 'ynfeedback_new', $user); } } } $db->commit(); echo Zend_Json::encode(array('message' => 'Feedback successfully!')); } catch (Exception $e) { $db->rollBack(); echo Zend_Json::encode(array('message' => 'Feedback failure!')); } }
public function editAction() { // Return if guest try to access to create link. $this->_helper->content->setEnabled(); $viewer = Engine_Api::_()->user()->getViewer(); $view = Zend_Registry::get('Zend_View'); $idea = Engine_Api::_()->getItem('ynfeedback_idea', $this->_getParam('idea_id')); if (empty($idea)) { return $this->_helper->requireSubject()->forward(); } if (!$idea->isEditable()) { return $this->_helper->requireAuth()->forward(); } //get category $tableCategory = Engine_Api::_()->getItemTable('ynfeedback_category'); $categories = $tableCategory->getCategories(); $category_id = $this->_getParam('category_id', $idea->category_id); // Create Form //get current category $category = Engine_Api::_()->getItem('ynfeedback_category', $category_id); //get profile question $topStructure = Engine_Api::_()->fields()->getFieldStructureTop('ynfeedback_idea'); if (count($topStructure) == 1 && $topStructure[0]->getChild()->type == 'profile_type') { $profileTypeField = $topStructure[0]->getChild(); $formArgs = array('topLevelId' => $profileTypeField->field_id, 'topLevelValue' => $category->option_id); } $this->view->form = $form = new Ynfeedback_Form_Feedback_Edit(array('formArgs' => $formArgs, 'item' => $idea)); //populate all data $idea->description = htmlspecialchars_decode($idea->description); $idea->description = strip_tags($idea->description); $form->populate($idea->toArray()); // Populate auth $auth = Engine_Api::_()->authorization()->context; $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'everyone'); foreach ($roles as $role) { if (isset($form->auth_view->options[$role]) && $auth->isAllowed($idea, $role, 'view')) { $form->auth_view->setValue($role); } if (isset($form->auth_comment->options[$role]) && $auth->isAllowed($idea, $role, 'comment')) { $form->auth_comment->setValue($role); } } // Populate category list. $categories = $tableCategory->getCategories(); unset($categories[0]); foreach ($categories as $item) { $form->category_id->addMultiOption($item['category_id'], str_repeat("-- ", $item['level'] - 1) . $view->translate($item['title'])); } //repopulate category if ($category_id) { $form->category_id->setValue($category_id); } else { $form->addError('Create feedback require at least one category. Please contact admin for more details.'); } //populate data $posts = $this->getRequest()->getPost(); $form->populate($posts); //populate co-authors if (!$posts) { $authorTable = Engine_Api::_()->getDbTable('authors', 'ynfeedback'); $this->view->authors = $authors = $authorTable->getAuthorsByIdeaId($idea->getIdentity()); } if (!isset($posts['submit_button'])) { $this->view->posts = $posts; return; } // Check method and data validity. if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($posts)) { $this->view->posts = $posts; return; } //get values $params = $this->_getAllParams(); $values = $form->getValues(); //check email if (!empty($values['guest_email'])) { $regexp = "/^[A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}\$/"; if (!preg_match($regexp, $values['guest_email'])) { $form->addError('Please enter valid email!'); return; } } $db = Engine_Db_Table::getDefaultAdapter(); $db->beginTransaction(); try { $class = new Engine_Filter_HtmlSpecialChars(); $values['description'] = $class->filter($values['description']); $class = new Engine_Filter_Censor(); $values['description'] = $class->filter($values['description']); $class = new Engine_Filter_EnableLinks(); $values['description'] = $class->filter($values['description']); //save feedback $idea->setFromArray($values); $idea->save(); //Set Co-authors $tableAuthor = Engine_Api::_()->getDbTable('authors', 'ynfeedback'); $tableAuthor->deleteAllAuthorsByIdeaId($idea->getIdentity()); $toValues = $this->_getParam('toValues'); if (!empty($toValues)) { $authors = explode(",", $toValues); foreach ($authors as $authorID) { if (is_numeric($authorID)) { $user = Engine_Api::_()->getItem('user', $authorID); if ($user->getIdentity()) { $authorRow = $tableAuthor->createRow(); $authorRow->idea_id = $idea->getIdentity(); $authorRow->user_id = $authorID; $authorRow->save(); } else { $authorRow = $tableAuthor->createRow(); $authorRow->idea_id = $idea->getIdentity(); $authorRow->name = $authorID; $authorRow->save(); } } else { $authorRow = $tableAuthor->createRow(); $authorRow->idea_id = $idea->getIdentity(); $authorRow->name = $authorID; $authorRow->save(); } } } //save custom field $customfieldform = $form->getSubForm('fields'); $customfieldform->setItem($idea); $customfieldform->saveValues(); // Set auth $auth = Engine_Api::_()->authorization()->context; $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'everyone'); if (empty($values['auth_view'])) { $values['auth_view'] = 'everyone'; } if (empty($values['auth_comment'])) { $values['auth_comment'] = 'everyone'; } $viewMax = array_search($values['auth_view'], $roles); $commentMax = array_search($values['auth_comment'], $roles); foreach ($roles as $i => $role) { $auth->setAllowed($idea, $role, 'view', $i <= $viewMax); $auth->setAllowed($idea, $role, 'comment', $i <= $commentMax); } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } //send to follower Engine_Api::_()->ynfeedback()->sendNotificationToFollower($idea, 'ynfeedback_idea_edit', $idea, $idea); return $this->_forward('success', 'utility', 'core', array('parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'view', 'idea_id' => $idea->getIdentity()), 'ynfeedback_specific', true), 'messages' => array(Zend_Registry::get('Zend_Translate')->_('Please wait...')))); }