/default_www/frontend/modules/tags/widgets/related.php

https://github.com/zakgrant/forkcms · PHP · 178 lines · 71 code · 33 blank · 74 comment · 9 complexity · 05dc48b1d939f37a616dc94d24254d39 MD5 · raw file

  1. <?php
  2. /**
  3. * This is a widget with the related items based on tags
  4. *
  5. * @package frontend
  6. * @subpackage tags
  7. *
  8. * @author Matthias Mullie <matthias@netlash.com>
  9. * @author Annelies Van Extergem <annelies@netlash.com>
  10. * @since 2.0
  11. */
  12. class FrontendTagsWidgetRelated extends FrontendBaseWidget
  13. {
  14. /**
  15. * Records to exclude
  16. *
  17. * @var array
  18. */
  19. private $exclude = array();
  20. /**
  21. * Tags on this page
  22. *
  23. * @var array
  24. */
  25. private $tags = array();
  26. /**
  27. * Related records
  28. *
  29. * @var array
  30. */
  31. private $related = array();
  32. /**
  33. * Execute the extra
  34. *
  35. * @return void
  36. */
  37. public function execute()
  38. {
  39. // call parent
  40. parent::execute();
  41. // get tags
  42. $this->getTags();
  43. // get related "things" based on tags
  44. $this->getRelated();
  45. // load template
  46. $this->loadTemplate();
  47. // parse
  48. $this->parse();
  49. }
  50. /**
  51. * Get related "things" based on tags
  52. *
  53. * @return void
  54. */
  55. private function getRelated()
  56. {
  57. // loop tags
  58. foreach($this->tags as $tag)
  59. {
  60. // fetch entries
  61. $items = FrontendModel::getDB()->getRecords('SELECT mt.module, mt.other_id
  62. FROM modules_tags AS mt
  63. INNER JOIN tags AS t ON t.id = mt.tag_id
  64. WHERE t.tag = ?', array($tag));
  65. // loop items
  66. foreach($items as $item)
  67. {
  68. // loop existing items
  69. foreach($this->related as $related)
  70. {
  71. // already exists
  72. if($item == $related) continue 2;
  73. }
  74. // add to list of related items
  75. $this->related[] = $item;
  76. }
  77. }
  78. // loop entries
  79. foreach($this->related as $id => $entry)
  80. {
  81. // loop excluded records
  82. foreach($this->exclude as $exclude)
  83. {
  84. // check if this entry should be excluded
  85. if($entry['module'] == $exclude['module'] && $entry['other_id'] == $exclude['other_id'])
  86. {
  87. unset($this->related[$id]);
  88. continue 2;
  89. }
  90. }
  91. // set module class
  92. $class = 'Frontend' . SpoonFilter::toCamelCase($entry['module']) . 'Model';
  93. // get module record
  94. $this->related[$id] = FrontendTagsModel::callFromInterface($entry['module'], $class, 'getForTags', (array) array($entry['other_id']));
  95. if($this->related[$id]) $this->related[$id] = array_pop($this->related[$id]);
  96. // remove empty items
  97. if(empty($this->related[$id])) unset($this->related[$id]);
  98. }
  99. // only show 3
  100. $this->related = array_splice($this->related, 0, 3);
  101. }
  102. /**
  103. * Get tags for current "page"
  104. *
  105. * @return void
  106. */
  107. private function getTags()
  108. {
  109. // get page id
  110. $pageId = FrontendPage::getCurrentPageId();
  111. // array of excluded records
  112. $this->exclude[] = array('module' => 'pages', 'other_id' => $pageId);
  113. // get tags for page
  114. $tags = (array) FrontendTagsModel::getForItem('pages', $pageId);
  115. foreach($tags as $tag) $this->tags = array_merge((array) $this->tags, (array) $tag['name']);
  116. // get page record
  117. $record = (array) FrontendNavigation::getPageInfo($pageId);
  118. // loop blocks
  119. foreach((array) $record['extra_blocks'] as $block)
  120. {
  121. // set module class
  122. $class = 'Frontend' . SpoonFilter::toCamelCase($block['module']) . 'Model';
  123. // get record for module
  124. $record = FrontendTagsModel::callFromInterface($block['module'], $class, 'getIdForTags', $this->URL);
  125. // check if record exists
  126. if(!$record) continue;
  127. // add to excluded records
  128. $this->exclude[] = array('module' => $block['module'], 'other_id' => $record['id']);
  129. // get record's tags
  130. $tags = (array) FrontendTagsModel::getForItem($block['module'], $record['id']);
  131. foreach($tags as $tag) $this->tags = array_merge((array) $this->tags, (array) $tag['name']);
  132. }
  133. }
  134. /**
  135. * Parse
  136. *
  137. * @return void
  138. */
  139. private function parse()
  140. {
  141. // assign
  142. $this->tpl->assign('widgetTagsRelated', $this->related);
  143. }
  144. }
  145. ?>