PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/ContentHasContent.php

https://github.com/jiiarra/site
PHP | 246 lines | 151 code | 28 blank | 67 comment | 12 complexity | 2c0cb5c7be954ff26cadb2f871896f13 MD5 | raw file
  1. <?php
  2. /**
  3. * ContentHasContent -> ContentHasContent database model for content has content link table.
  4. *
  5. * Copyright (c) <2009>, Markus Riihel�
  6. * Copyright (c) <2009>, Mikko Sallinen
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * License text found in /license/
  19. */
  20. /**
  21. * ContentHasContent - class
  22. *
  23. * @package models
  24. * @author Markus Riihel� & Mikko Sallinen
  25. * @copyright 2009 Markus Riihel� & Mikko Sallinen
  26. * @license GPL v2
  27. * @version 1.0
  28. */
  29. class Default_Model_ContentHasContent extends Zend_Db_Table_Abstract
  30. {
  31. // Table name
  32. protected $_name = 'cnt_has_cnt';
  33. // Table reference map
  34. protected $_referenceMap = array(
  35. 'ParentContent' => array(
  36. 'columns' => array('id_parent_cnt'),
  37. 'refTableClass' => 'Default_Model_Content',
  38. 'refColumns' => array('id_cnt')
  39. ),
  40. 'ChildContent' => array(
  41. 'columns' => array('id_child_cnt'),
  42. 'refTableClass' => 'Default_Model_Content',
  43. 'refColumns' => array('id_cnt')
  44. ),
  45. );
  46. /**
  47. * addContentToContent
  48. *
  49. * Add content to be a child of another content
  50. *
  51. * @param integer $id_parent_cnt
  52. * @param integer $id_child_cnt
  53. */
  54. public function addContentToContent($id_parent_cnt = 0, $id_child_cnt = 0)
  55. {
  56. // If id values not 0
  57. if($id_parent_cnt != 0 && $id_child_cnt != 0)
  58. {
  59. $data = array(
  60. 'id_parent_cnt' => $id_parent_cnt,
  61. 'id_child_cnt' => $id_child_cnt,
  62. 'created_cnt' => new Zend_Db_Expr('NOW()')
  63. );
  64. $this->insert($data);
  65. } // end if
  66. } // end of addContentToContent
  67. /*
  68. * Get the parents and children of content
  69. *
  70. * @param id integer id of the content for which we want the tree
  71. * @return return array array of the family tree
  72. */
  73. public function getContentFamilyTree($id = -1)
  74. {
  75. $return = array();
  76. $selectParents = $this->_db->select()
  77. ->from(array('cnt_has_cnt' => 'cnt_has_cnt'),
  78. array('id_parent_cnt'))
  79. ->joinLeft(array('cnt' => 'contents_cnt'),
  80. 'cnt.id_cnt = cnt_has_cnt.id_parent_cnt',
  81. array())
  82. ->where('id_child_cnt = ?', $id)
  83. ->where('cnt.published_cnt = 1')
  84. ->order('cnt_has_cnt.created_cnt desc')
  85. ;
  86. $parents = $this->_db->fetchAll($selectParents);
  87. $i = 0;
  88. foreach ($parents as $parent) {
  89. $return['parents'][$i] = $parent['id_parent_cnt'];
  90. $i++;
  91. }
  92. $selectChildren = $this->_db->select()
  93. ->from(array('cnt_has_cnt' => 'cnt_has_cnt'),
  94. array('id_child_cnt'))
  95. ->joinLeft(array('cnt' => 'contents_cnt'),
  96. 'cnt.id_cnt = cnt_has_cnt.id_child_cnt',
  97. array())
  98. ->where('id_parent_cnt = ?', $id)
  99. ->where('cnt.published_cnt = 1')
  100. ->order('cnt_has_cnt.created_cnt desc')
  101. ;
  102. $children = $this->_db->fetchAll($selectChildren);
  103. $i = 0;
  104. foreach ($children as $child) {
  105. $return['children'][$i] = $child['id_child_cnt'];
  106. $i++;
  107. }
  108. return $return;
  109. }
  110. public function checkIfContentHasContent($id_parent_cnt = -1, $id_child_cnt = -1) {
  111. $return = false;
  112. if($id_parent_cnt != -1 && $id_child_cnt != -1)
  113. {
  114. $select = $this->select()
  115. ->from($this, array('*'))
  116. ->where('`id_parent_cnt` = ?', $id_parent_cnt)
  117. ->where('`id_child_cnt` = ?', $id_child_cnt);
  118. $result = $this->fetchAll($select)->toArray();
  119. if(count($result) != 0) {
  120. $return = true;
  121. }
  122. }
  123. return $return;
  124. }
  125. /**
  126. * getContentContents
  127. * Get all content contents
  128. *
  129. * @author Mikko Korpinen
  130. * @param int id_parent_cnt Id of content
  131. * @return array
  132. */
  133. public function getContentContents($id_cnt) {
  134. $result = array(); // container for final results array
  135. $contentSelectParents = $this->_db->select()
  136. ->from(array('chc' => 'cnt_has_cnt'),
  137. array('id_parent_cnt', 'id_child_cnt'))
  138. ->joinLeft(array('cnt' => 'contents_cnt'),
  139. 'cnt.id_cnt = chc.id_child_cnt',
  140. array('id_cnt', 'id_cty_cnt', 'title_cnt',
  141. 'lead_cnt', 'published_cnt', 'created_cnt'))
  142. ->joinLeft(array('cty' => 'content_types_cty'),
  143. 'cty.id_cty = cnt.id_cty_cnt',
  144. array('key_cty'))
  145. ->joinLeft(array('chu' => 'cnt_has_usr'),
  146. 'chu.id_cnt = cnt.id_cnt',
  147. array('id_usr'))
  148. ->joinLeft(array('usr' => 'users_usr'),
  149. 'usr.id_usr = chu.id_usr',
  150. array('login_name_usr'))
  151. ->where('chc.id_parent_cnt = ?', $id_cnt)
  152. ->group('cnt.id_cnt');
  153. $contentSelectChilds = $this->_db->select()
  154. ->from(array('chc' => 'cnt_has_cnt'),
  155. array('id_parent_cnt', 'id_child_cnt'))
  156. ->joinLeft(array('cnt' => 'contents_cnt'),
  157. 'cnt.id_cnt = chc.id_parent_cnt',
  158. array('id_cnt', 'id_cty_cnt', 'title_cnt',
  159. 'lead_cnt', 'published_cnt', 'created_cnt'))
  160. ->joinLeft(array('cty' => 'content_types_cty'),
  161. 'cty.id_cty = cnt.id_cty_cnt',
  162. array('key_cty'))
  163. ->joinLeft(array('chu' => 'cnt_has_usr'),
  164. 'chu.id_cnt = cnt.id_cnt',
  165. array('id_usr'))
  166. ->joinLeft(array('usr' => 'users_usr'),
  167. 'usr.id_usr = chu.id_usr',
  168. array('login_name_usr'))
  169. ->where('chc.id_child_cnt = ?', $id_cnt)
  170. ->group('cnt.id_cnt');
  171. $result['parents'] = $this->_db->fetchAll($contentSelectParents);
  172. $result['childs'] = $this->_db->fetchAll($contentSelectChilds);
  173. return $result;
  174. }
  175. /**
  176. * removeContentFromContents
  177. * Removes specified content from contents (child or parent)
  178. *
  179. * @param int id_cnt Id of content
  180. * @author Mikko Korpinen
  181. */
  182. public function removeContentFromContents($id_cnt = 0)
  183. {
  184. $parent = $this->getAdapter()->quoteInto('id_parent_cnt = ?', (int)$id_cnt);
  185. $child = $this->getAdapter()->quoteInto('id_child_cnt = ?', (int)$id_cnt);
  186. $where = "$parent OR $child";
  187. $this->delete($where);
  188. return !$this->fetchAll($where)->count();
  189. }
  190. /**
  191. * removeContentFromContent
  192. * Removes specified content from content
  193. *
  194. * @param int id_parent_cnt Id of content
  195. * @param int id_child_cnt Id of content
  196. * @author Mikko Korpinen
  197. */
  198. public function removeContentFromContent($id_parent_cnt = 0, $id_child_cnt = 0)
  199. {
  200. $parent = $this->getAdapter()->quoteInto('id_parent_cnt = ?', (int)$id_parent_cnt);
  201. $child = $this->getAdapter()->quoteInto('id_child_cnt = ?', (int)$id_child_cnt);
  202. $where = "$parent AND $child";
  203. if ($this->delete($where)) {
  204. return true;
  205. } else {
  206. return false;
  207. }
  208. }
  209. public function getContentLinkIds($id) {
  210. $selectParents = $this->select()->from($this, array('id_cnt' => 'id_parent_cnt'))
  211. ->where('id_child_cnt = ?', $id);
  212. $ids = $this->fetchAll($selectParents)->toArray();
  213. $selectChilds = $this->select() ->from($this, array('id_cnt' => 'id_child_cnt' ))
  214. ->where('id_parent_cnt = ?', $id);
  215. return array_merge($ids, $this->fetchAll($selectChilds)->toArray());
  216. }
  217. } // end of class
  218. ?>