/** * Create the singleton instance. */ public static function getInstance() { if (self::$_table === null) { self::$_table = new self(); } return self::$_table; }
/** * STAGE 3: Choose, create, and optionally update models using business logic. * STAGE 4. Apply business logic to create a presentation model for the view. */ protected function displayStage3_4() { // the posts and topics models auto-instantiate themselves, as needed (STAGE 3) $this->view->posts = ZFDemoModel_Posts::getPostsByTopicId($this->topicId); $this->view->topicId = $this->topicId; // the posts and topics models auto-instantiate themselves, as needed (STAGE 3) $topic = ZFDemoModel_Topics::getPresentationModelByTopicId($this->topicId); $this->view->topicName = $topic->topic_name; $this->view->hide = false; }
/** * STAGE 3: Choose, create, and optionally update models using business logic. * STAGE 4. Apply business logic to create a presentation model for the view. */ protected function displayStage3_4() { // the posts and topics models auto-instantiate themselves, as needed (STAGE 3) $this->view->posts = ZFDemoModel_Posts::getPostsByTopicId($this->topicId); $this->view->topicId = $this->topicId; // the posts and topics models auto-instantiate themselves, as needed (STAGE 3) $topic = ZFDemoModel_Topics::getPresentationModelByTopicId($this->topicId); $this->view->topicName = $topic->topic_name; $this->view->hide = false; ///////////////////////////// // ==> SECTION: acl <== $role = ZFModule_Forum::getRole(); // another form of access control (whether or not to show hidden posts) $this->view->hide = $role === 'moderator' || $role === 'admin' ? false : true; }
/** * Process a submitted post. */ public function submitFormAction() { // STAGE 3: Choose, create, and optionally update models using business logic. $topicId = $this->getTopicId(); require_once 'Zend/Filter/StripTags.php'; $filter = new Zend_Filter_StripTags(); // only permit digits using ctype_digit() $subject = $filter->filter($_POST['subject']); $body = $filter->filter($_POST['body']); ZFDemoModel_Posts::submit(ZFModule_Forum::getAuthorizationId(), $topicId, $subject, $body); // STAGE 4: Apply business logic to create a presentation model for the view. // STAGE 5: Choose view and submit presentation model to view. $this->setRedirectCode(303); // PRG pattern via http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html $this->_redirect("/forum/index/index/topic/{$topicId}"); }
/** * STAGE 3: Choose, create, and optionally update models using business logic. * STAGE 4: Apply business logic to create a presentation model for the view. */ public function editAction() { $config = Zend_Registry::get('config'); // application-wide configuration ini if ($config->db->modelSet === 'pdo') { $this->renderToSegment('body', 'addedInSectionDb'); // initiate STAGE 6 in the view template } ///////////////////////////// // ==> SECTION: db <== // ZFDemoModel_Topics provides static methods that auto-instantiate and manage the model // ZFDemoModel_Posts provides static methods that auto-instantiate and manage the model $topicId = intval($_POST['topicId']); if ($topicId < 1 || $topicId > 99999) { $this->_redirect('forum/admin_topics'); } $posts = ZFDemoModel_Posts::getDomainModel($topicId); $deletedRows = array(); // list of deleted posts indexed by post id $visible = array(); // visibility settings indexed by post id $changedRows = array(); // list of altered post rows ZFDemo_Log::log(_('INFO BEGIN editAction()')); try { // iterate over the form variable, queing actions for each change requested foreach ($_POST as $var => $val) { if (!strncmp($var, 'delete', 6)) { $id = intval(substr($var, 6)); if (isset($posts[$id])) { $deletedRows[$id] = $posts[$id]; ZFDemo_Log::log(_('INFO deleting post id #%1$d', $id)); } } elseif (!strncmp($var, 'visible', 7)) { $id = intval(substr($var, 7)); if (isset($posts[$id])) { $visible[$id] = 1; // ask Zend_Db_Table_Row to delete itself } } } // for every post, make sure visibility setting matches the selections submitted via the form foreach ($posts as $id => $row) { if (!isset($deletedRows[$id])) { // don't update a row that will be deleted if (!isset($visible[$id])) { // the checkbox for visibility of this post was not checked $visible[$id] = 0; // web interface defaults to checking visible posts, so unchecked means not visible } if ($row->is_visible != $visible[$id]) { ZFDemo_Log::log(_('INFO post id %1$d visibility set to %2$d', $id, $visible[$id])); $row->is_visible = intval($visible[$id]); $changedRows[$id] = $row; // add this row to the list of changed rows } } } // now commit changes to DB $registry = Zend_Registry::getInstance(); if (!$registry['config']->db->transactions) { // note: query aggregation would help performance (reader excercise) foreach ($deletedRows as $row) { $row->delete(); // now do all the deletes at once } foreach ($changedRows as $row) { $row->save(); // now synchronize all the modified rows with the table } } else { // table type supports transactions $db = $registry['db']; $db->beginTransaction(); try { // note: query aggregation would help performance (reader excercise) foreach ($deletedRows as $row) { $row->delete(); // now do all the deletes at once } foreach ($changedRows as $row) { $row->save(); // now synchronize all the modified rows with the table } $db->commit(); } catch (Exception $exception) { $db->rollBack(); // re-throw the exception throw $exception; // allow normal processing by the preDispatch() of the plugin in bootstrap.php } } } catch (Exception $exception) { ZFDemo_Log::log(_('ERROR END editAction() - failed')); throw $exception; // resume normal processing of the exception } ZFDemo_Log::log(_('NOTICE END editAction() - succeeded')); // Controller = 'index', Action = 'index' $this->setRedirectCode(303); // PRG pattern via http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html $this->_redirect('forum/admin_posts/index/topic/' . $topicId); }