/application/models/ContentHasRelatedCompany.php

https://github.com/jiiarra/site · PHP · 190 lines · 87 code · 26 blank · 77 comment · 12 complexity · 009dc8aad5095a0403e0413c505c5a29 MD5 · raw file

  1. <?php
  2. /**
  3. * ContentHasRelatedCompany -> ContentHasRelatedCompany database model for table cnt_has_rec
  4. *
  5. * Copyright (c) <2009>, Pekka Piispanen
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  11. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  15. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * License text found in /license/
  18. */
  19. /**
  20. * ContentHasRelatedCompany - class
  21. *
  22. * @package models
  23. * @author Pekka Piispanen
  24. * @copyright 2009 Pekka Piispanen
  25. * @license GPL v2
  26. * @version 1.0
  27. */
  28. class Default_Model_ContentHasRelatedCompany extends Zend_Db_Table_Abstract
  29. {
  30. // Table name
  31. protected $_name = 'cnt_has_rec';
  32. // Table reference map
  33. protected $_referenceMap = array(
  34. 'RelCompContent' => array(
  35. 'columns' => array('id_cnt'),
  36. 'refTableClass' => 'Default_Model_Content',
  37. 'refColumns' => array('id_cnt')
  38. ),
  39. 'RelCompRelComp' => array(
  40. 'columns' => array('id_rec'),
  41. 'refTableClass' => 'Default_Model_RelatedCompanies',
  42. 'refColumns' => array('id_rec')
  43. )
  44. );
  45. /**
  46. * addRelCompToContent
  47. *
  48. * Add specified related company to specified content.
  49. *
  50. * @param integer $id_rec
  51. * @param integer $id_cnt
  52. */
  53. public function addRelCompToContent($id_rec = 0, $id_cnt = 0)
  54. {
  55. // If related company id and content id is not 0
  56. if($id_rec != 0 && $id_cnt != 0)
  57. {
  58. // Create a new row
  59. $row = $this->createRow();
  60. // Set id values
  61. $row->id_cnt = $id_cnt;
  62. $row->id_rec = $id_rec;
  63. // Add row to database
  64. $row->save();
  65. } // end if
  66. } // end of addRelCompToContent
  67. public function getContentRelComps($id_cnt = 0)
  68. {
  69. $select = $this->_db->select()
  70. ->from(array('cnt_has_rec' => 'cnt_has_rec'), array('id_rec'))
  71. ->where('id_cnt = ?', $id_cnt)
  72. ->join(array('related_companies_rec' => 'related_companies_rec'), 'related_companies_rec.id_rec = cnt_has_rec.id_rec', array('name_rec'))
  73. ;
  74. $result = $this->_db->fetchAll($select);
  75. return $result;
  76. } // end of getContentRelComps
  77. /**
  78. * checkIfOtherContentHasRelComp
  79. *
  80. * This function checks, if other content(s) have specified related company when
  81. * deleting content. If no other content uses the specified related company, the
  82. * entire related company can be deleted from the system
  83. *
  84. * @param int $id_rec The related company to check
  85. * @param int $id_cnt The content which is going to be deleted
  86. * @return bool
  87. * @author Pekka Piispanen
  88. */
  89. public function checkIfOtherContentHasRelComp($id_rec = 0, $id_cnt = 0)
  90. {
  91. $return = false;
  92. $select = $this->select()
  93. ->where('id_rec = ?', $id_rec)
  94. ->where('id_cnt != ?', $id_cnt);
  95. $result = $this->fetchAll($select)->toArray();
  96. if(count($result) != 0)
  97. {
  98. $return = true;
  99. }
  100. return $return;
  101. }
  102. /**
  103. * removeContentRelComps
  104. *
  105. * Remove related companies from specified content
  106. *
  107. * @param integer $id_cnt
  108. * @return bool $return
  109. * @author Pekka Piispanen
  110. */
  111. public function removeContentRelComps($id_cnt = 0)
  112. {
  113. $where = $this->getAdapter()->quoteInto('id_cnt = ?', $id_cnt);
  114. $this->delete($where);
  115. return $this->fetchAll($where)->count() ? false : true;
  116. } // end of removeContentRelComps
  117. /**
  118. *
  119. *
  120. */
  121. public function deleteRelCompFromContent($id_rec = 0, $id_cnt = 0)
  122. {
  123. $return = false;
  124. $where = array();
  125. $where[] = $this->getAdapter()->quoteInto('id_rec = ?', $id_rec);
  126. $where[] = $this->getAdapter()->quoteInto('id_cnt = ?', $id_cnt);
  127. if($this->delete($where))
  128. {
  129. $return = true;
  130. }
  131. return $return;
  132. }
  133. /**
  134. *
  135. *
  136. */
  137. public function checkExistingCompanies($contentId = -1, array $companies = array())
  138. {
  139. $result = null;
  140. if($contentId != -1 && !empty($companies)) {
  141. // Go through all existing related companies
  142. $existingRecs = $this->getContentRelComps($contentId);
  143. foreach($existingRecs as $id => $existingRec) {
  144. // If some of the existing related companies aren't found in sent
  145. // related companies, that related company is deleted from the
  146. // content and maybe even from thedatabase
  147. if(!in_array($existingRec['name_rec'], $companies)) {
  148. // Removing rec from content
  149. $this->deleteRelCompFromContent($existingRec['id_rec'], $contentId);
  150. // If other content(s) doesn't have this related company, the whole
  151. // related company is going to be removed from the database
  152. if(!$this->checkIfOtherContentHasRelComp($existingRec['id_rec'], $contentId)) {
  153. $modelRecs = new Default_Model_RelatedCompanies();
  154. $modelRecs->removeRelComp($existingRec['id_rec']);
  155. }
  156. // Remove related company from existingRecs array
  157. unset($existingRecs[$id]);
  158. }
  159. }
  160. $result = $existingRecs;
  161. }
  162. return $result;
  163. }
  164. } // end of class