/library/Html5Wiki/Model/ArticleVersion/Table.php

https://github.com/HTML5Wiki/HTML5Wiki · PHP · 148 lines · 72 code · 29 blank · 47 comment · 3 complexity · f5f5b2d07b6187b9d56bba60679dc696 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the HTML5Wiki Project.
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.github.com/HTML5Wiki/HTML5Wiki/blob/master/LICENSE
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to mweibel@hsr.ch so we can send you a copy immediately.
  14. *
  15. * @author Nicolas Karrer <nkarrer@hsr.ch>
  16. * @copyright (c) HTML5Wiki Team 2011
  17. * @category Html5Wiki
  18. * @package Library
  19. * @subpackage Model
  20. */
  21. /**
  22. * Article version table model
  23. */
  24. class Html5Wiki_Model_ArticleVersion_Table extends Zend_Db_Table_Abstract {
  25. protected $_name = 'ArticleVersion';
  26. protected $_primary = array('mediaVersionId', 'mediaVersionTimestamp');
  27. protected $_sequence = true;
  28. protected $_rowClass = 'Html5Wiki_Model_ArticleVersion';
  29. protected $_dependentTables = array('Html5Wiki_Model_MediaVersion_Table');
  30. protected $_referenceMap = array(
  31. 'MediaVersion' => array(
  32. 'columns' => array('mediaVersionId','mediaVersionTimestamp')
  33. ,'refTableClass' => 'Html5Wiki_Model_MediaVersion_Table'
  34. ,'refColumns' => array('id','timestamp')
  35. )
  36. );
  37. /**
  38. *
  39. */
  40. public function getArticleData($idMediaVersion, $timestampMediaVersion) {
  41. $selectStatement = $this->select()->setIntegrityCheck(false);
  42. $selectStatement->where($this->_primary[1] . ' = ?', $idMediaVersion);
  43. if( $timestampMediaVersion > 0) {
  44. $selectStatement->where($this->_primary[2] . ' = ?', $timestampMediaVersion);
  45. } else {
  46. $selectStatement->limit(1);
  47. $selectStatement->order($this->_primary[2] . ' DESC');
  48. }
  49. return $this->fetchRow($selectStatement);
  50. }
  51. /**
  52. *
  53. * @param $data
  54. * @return unknown_type
  55. */
  56. public function saveArticle($saveData) {
  57. $localSaveData = array(
  58. 'mediaVersionId' => intval($saveData['mediaVersionId']),
  59. 'mediaVersionTimestamp' => intval($saveData['mediaVersionTimestamp']),
  60. 'title' => $saveData['title'],
  61. );
  62. if(isset($saveData['content'])) $localSaveData['content'] = $saveData['content'];
  63. return $this->insert($localSaveData);
  64. }
  65. /**
  66. * @param $idArticle
  67. * @return Zend_Db_Table_Rowset_Abstract
  68. */
  69. public function fetchArticlesById($idArticle) {
  70. $idArticle = intval($idArticle);
  71. $selectStatement = $this->initSelectStatement('PUBLISHED');
  72. $selectStatement->where('mediaVersionId = ?', $idArticle);
  73. $selectStatement = $this->addMediaVersionJoinStatement($selectStatement);
  74. $selectStatement->order('timestamp DESC');
  75. return $this->fetchAll($selectStatement);
  76. }
  77. /**
  78. * @return type
  79. */
  80. public function fetchLatestArticles() {
  81. $select = $this->select()->setIntegrityCheck(false);
  82. $subselect = $this->select()->setIntegrityCheck(false)
  83. ->from('MediaVersion')
  84. ->where('mediaVersionType = ?', 'ARTICLE')
  85. ->where('state = ?', 'PUBLISHED')
  86. ->order('timestamp DESC');
  87. $select->from($subselect);
  88. $idJoinCondition = 't.id = ' . $this->_name . '.' . $this->_primary[1];
  89. $timestampJoinCondition = 't.timestamp = ' . $this->_name . '.' . $this->_primary[2];
  90. $select->join('ArticleVersion', $idJoinCondition . ' AND ' . $timestampJoinCondition);
  91. $select->group('t.id');
  92. $select->order('t.timestamp DESC');
  93. return $this->fetchAll($select);
  94. }
  95. /**
  96. * @param $state
  97. * @return Zend_Db_Select
  98. */
  99. private function initSelectStatement($state) {
  100. $selectStatement = $this->select()->setIntegrityCheck(false);
  101. $selectStatement->from($this);
  102. $selectStatement->where('mediaVersionType = ?', 'ARTICLE');
  103. $selectStatement->where('state = ?', $state);
  104. return $selectStatement;
  105. }
  106. /**
  107. * Add Join to the select statement
  108. *
  109. * @param Zend_Db_Select $selectStatement
  110. * @return Zend_Db_Select
  111. */
  112. private function addMediaVersionJoinStatement($selectStatement) {
  113. $idJoinCondition = $this->_name . '.' . $this->_primary[1] . ' = MediaVersion.id';
  114. $timestampJoinCondition = $this->_name . '.' . $this->_primary[2] . ' = MediaVersion.timestamp';
  115. $selectStatement->join('MediaVersion', $idJoinCondition . ' AND ' . $timestampJoinCondition);
  116. return $selectStatement;
  117. }
  118. }
  119. ?>