/** * Returns the topics table as a presentation model (array of arrays containing * information about each topic, suitable for use by a view) by mirroring * the domain model into a presentation model. The presentation model can be modified * to support the needs of a view, without mangling the raw, real underlying table data. * STAGE 4: Apply business logic to create a presentation model for the view. * @return array of ArrayObjects (containing topic info) indexed by topic id */ public static function getPresentationModel() { if (self::$_presentationModel === null) { foreach (self::getDomainModel() as $row) { $row = new ArrayObject($row->toArray(), ArrayObject::ARRAY_AS_PROPS); $row->user = ZFDemoModel_Users::getById($row->user_id); self::$_presentationModel[$row->topic_id] = $row; ///////////////////////////// // ==> SECTION: l10n <== // create a Locale object for the owner of this post (not the user of this request) $postLocale = new Zend_Locale($row->user->locale); $row->country = ZFModule_Forum::getCountry($postLocale->getRegion()); $userLocale = ZFModule_Forum::getUserLocale(); // locale of the user of this request $userLocale = Zend_Registry::get('userLocale'); $offset = ZFModule_Forum::getTimeOffset(); if ($row->modification_time != $row->creation_time) { $row->modification_time = new Zend_Date($row->modification_time, $userLocale); $row->modification_time->addTimestamp($offset); // express date/time in user's local timezone } else { $row->modification_time = ''; } $row->creation_time = new Zend_Date($row->creation_time, $userLocale); $row->creation_time->addTimestamp($offset); // express date/time in user's local timezone } } return self::$_presentationModel; }
/** * Return the singleton instance of the "users" table model used by this application. */ public static function getInstance() { if (self::$_modelTable === null) { self::$_modelTable = new self(); } return self::$_modelTable; }
/** * Create a cached set of sets of posts, grouped by topic. * Only one set of posts are created for one topic ($topicId) per invocation. * Cached information lasts only for the duration of this request. */ public static function getPostsByTopicId($topicId) { static $stmt = null; if (!isset(self::$_posts[$topicId])) { $db = Zend_Registry::get('db'); if ($stmt === null) { $q = 'SELECT * FROM posts WHERE topic_id = ? ORDER BY creation_time ASC'; $stmt = $db->prepare($q); if ($stmt === false) { throw new ZFDemo_Exception("Preparing query '{$q}' failed.", 500); } } $stmt->execute(array(intval($topicId))); $posts = array(); foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS); if ($row->modification_time != $row->creation_time) { $row->modification_time = new Zend_Date($row->modification_time, Zend_Date::ISO_8601); } else { $row->modification_time = ''; } $row->country = ''; $row->creation_time = new Zend_Date($row->creation_time, Zend_Date::ISO_8601); $row->user = ZFDemoModel_Users::getById($row->user_id); $posts[] = $row; } $stmt->closeCursor(); self::$_posts[$topicId] = $posts; } return self::$_posts[$topicId]; }
/** * This method is responsible for mapping the ZFDemo's authentication ids to forum module authorization ids. * Each module can maintain its own set of ids used for authorization, * thus allowing integration of diverse modules. */ public static function authenticationId2authorizationId($authenticationId) { self::loadModels(); // getByUsername returns null if lookup fails if (self::$_user = ZFDemoModel_Users::getByUsername($authenticationId['username'])) { self::$_username = self::$_user->username; self::$_authorizationId = self::$_user->user_id; } }
/** * Returns the topics table as a presentation model (array of arrays containing * information about each topic, suitable for use by a view) by mirroring * the domain model into a presentation model. The presentation model can be modified * to support the needs of a view, without mangling the raw, real underlying table data. * STAGE 4: Apply business logic to create a presentation model for the view. * @return array of ArrayObjects (containing topic info) indexed by topic id */ public static function getPresentationModel() { if (self::$_presentationModel === null) { foreach (self::getDomainModel() as $row) { $row = new ArrayObject($row->toArray(), ArrayObject::ARRAY_AS_PROPS); $row->user = ZFDemoModel_Users::getById($row->user_id); self::$_presentationModel[$row->topic_id] = $row; } } return self::$_presentationModel; }
/** * Create a presentation model of the forum's topics list, appropriate for use by a view. */ public static function getPresentationModel() { $db = Zend_Registry::get('db'); $q = 'SELECT * FROM topics ORDER BY creation_time ASC'; $posts = array(); foreach ($db->query($q) as $row) { $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS); if ($row->modification_time != $row->creation_time) { $row->modification_time = new Zend_Date($row->modification_time, Zend_Date::ISO_8601); } else { $row->modification_time = ''; } $row->country = ''; $row->creation_time = new Zend_Date($row->creation_time, Zend_Date::ISO_8601); $row->user = ZFDemoModel_Users::getById($row->user_id); $topics[] = $row; } return new ArrayObject($topics, ArrayObject::ARRAY_AS_PROPS); }
/** * Submit a new post to a topic. */ public static function submit($userId, $topicId, $subject, $body) { $table = self::getInstance(); $db = $table->getAdapter(); $data = array('creation_time' => new Zend_Db_Expr('NOW()'), 'user_id' => $userId, 'topic_id' => $topicId, 'subject' => $subject, 'content' => $body); $result = 0; // status flag indicating if successfully incremented user's post count $postId = 0; // id of the post inserted into the 'posts' table $registry = Zend_Registry::getInstance(); $abort = ignore_user_abort(true); // don't stop saving the post, if the user presses "STOP" in their browser if (!$registry['config']->db->transactions) { $rowsInserted = $table->insert($data); if ($rowsInserted === 1) { $postId = $db->lastInsertId(); $result = ZFDemoModel_Users::incrementPostCount($userId); } } else { // table type supports transactions $db->beginTransaction(); try { $rowsInserted = $table->insert($data); if ($rowsInserted === 1) { $postId = $db->lastInsertId(); $result = ZFDemoModel_Users::incrementPostCount($userId); } if ($result == 1) { $db->commit(); } } catch (Exception $e) { $db->rollBack(); // re-throw the exception so that it can be processed normally // by the preDispatch() of the plugin in bootstrap.php throw $e; } } if ($result != 1) { throw new ZFDemo_Exception(_('CRITICAL User id "%1$s" post count could not be incremented for post id "%2$s".', $userId, $postId), 500); } ignore_user_abort($abort); // restore the previous setting }