/controllers/component/taxonomy.php

https://github.com/laposa/onxshop · PHP · 339 lines · 146 code · 134 blank · 59 comment · 26 complexity · d10b3eefa271a7f16ef7603787f59a46 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2006-2011 Laposa Limited (https://laposa.ie)
  4. * Licensed under the New BSD License. See the file LICENSE.txt for details.
  5. *
  6. */
  7. class Onyx_Controller_Component_Taxonomy extends Onyx_Controller {
  8. /**
  9. * main action
  10. */
  11. public function mainAction() {
  12. /**
  13. * input variables
  14. */
  15. if (is_numeric($this->GET['id'])) $node_id = $this->GET['id'];
  16. else return false;
  17. $relation = $this->GET['relation'];
  18. /**
  19. * initialize related object
  20. */
  21. $this->Taxonomy = $this->initializeRelatedObject($relation);
  22. /**
  23. * process listing
  24. */
  25. if (is_numeric($node_id) && is_object($this->Taxonomy)) {
  26. $taxonomy_list = $this->getTaxonomyListForNodeId($node_id);
  27. $taxonomy_label_list = $this->mapTaxonomyLabels($taxonomy_list);
  28. $taxonomy_label_level_list = $this->joinSameLevelTitles($taxonomy_label_list);
  29. $taxonomy_label_route_list = $this->joinRouteTitles($taxonomy_label_level_list);
  30. $taxonomy_label_route_list = $this->addParentData($taxonomy_label_route_list);
  31. $taxonomy_label_route_list = $this->sortByPriority($taxonomy_label_route_list);
  32. $this->parseListToTemplate($taxonomy_label_route_list);
  33. }
  34. return true;
  35. }
  36. /**
  37. * initialize related object
  38. */
  39. public function initializeRelatedObject($relation = 'node') {
  40. require_once('models/common/common_node.php');
  41. $Node = new common_node();
  42. switch ($relation) {
  43. case 'product':
  44. require_once('models/ecommerce/ecommerce_product_taxonomy.php');
  45. $Taxonomy = new ecommerce_product_taxonomy();
  46. break;
  47. case 'variety':
  48. require_once('models/ecommerce/ecommerce_product_variety_taxonomy.php');
  49. $Taxonomy = new ecommerce_product_variety_taxonomy();
  50. break;
  51. case 'node':
  52. default:
  53. require_once('models/common/common_node_taxonomy.php');
  54. $Taxonomy = new common_node_taxonomy();
  55. break;
  56. }
  57. return $Taxonomy;
  58. }
  59. /**
  60. * map taxonomy labels
  61. */
  62. public function mapTaxonomyLabels($list) {
  63. if (!is_array($list)) return false;
  64. $label_list = array();
  65. foreach ($list as $item) {
  66. $label = $this->Taxonomy->getLabel($item['taxonomy_tree_id']);
  67. $label_list[$label['id']] = $label;
  68. }
  69. return $label_list;
  70. }
  71. /**
  72. * get root
  73. */
  74. function getParentData($item) {
  75. if (is_numeric($item['parent'])) {
  76. $parent_data = $this->Taxonomy->getLabel($item['parent']);
  77. } else {
  78. //root category (NULL)
  79. $parent_id = 0;
  80. $detail['taxonomy_data']['parent'] = 0;
  81. $parent_data['id'] = 0;
  82. $parent_data['title'] = 'Category';
  83. $parent_data['description'] = '';
  84. $parent_data['priority'] = 0;
  85. $parent_data['priority'] = 1;
  86. $parent_data['label'] = $detail['parent_data'];
  87. }
  88. return $parent_data;
  89. }
  90. /**
  91. * parse to template
  92. */
  93. function parseListToTemplate($list) {
  94. if (!is_array($list)) return false;
  95. $parsed_count = 0;
  96. //print_r($taxonomy_detail);
  97. foreach ($list as $item) {
  98. $parsed_item = $this->parseItem($item);
  99. $parsed_count = $parsed_count + $parsed_item;
  100. }
  101. if ($parsed_count > 0) $this->tpl->parse('content.taxonomy');
  102. }
  103. /**
  104. * parse item
  105. */
  106. public function parseItem($item) {
  107. //don't show root items in same cases
  108. if ($this->GET['hide_root'] == 0 || $item['parent'] > 0) {
  109. //display only published folders and items
  110. if ($item['parent_data']['label']['publish'] == 1 && $item['label']['publish'] == 1) {
  111. $this->tpl->assign("ITEM", $item);
  112. $this->tpl->parse("content.taxonomy.item");
  113. return 1;
  114. } else {
  115. return 0;
  116. }
  117. return 1;
  118. } else {
  119. return 0;
  120. }
  121. }
  122. /**
  123. * get taxonomy list for node id
  124. */
  125. public function getTaxonomyListForNodeId($node_id) {
  126. if (!is_numeric($node_id)) return false;
  127. $list = $this->Taxonomy->listing("node_id = " . $node_id);
  128. return $list;
  129. }
  130. /**
  131. * join together labels from the same level
  132. */
  133. function joinSameLevelTitles($list) {
  134. if (!is_array($list)) return false;
  135. $list_joined = array();
  136. $list_joined_final = array();
  137. $proceed_parent_id = array();
  138. foreach($list as $item) {
  139. if (in_array($item["parent"], $proceed_parent_id)) {
  140. $list_joined[$item["parent"]]['label']['title'] = $list_joined[$item["parent"]]['label']['title'] . ', ' . $item['label']['title'];
  141. } else {
  142. $list_joined[$item["parent"]] = $item;
  143. $proceed_parent_id[] = $item["parent"];
  144. }
  145. }
  146. //reformat
  147. foreach ($list_joined as $item) {
  148. $list_joined_final[$item['id']] = $item;
  149. }
  150. return $list_joined_final;
  151. }
  152. /**
  153. * join together labels from the same route
  154. */
  155. function joinRouteTitles($list) {
  156. if (!is_array($list)) return false;
  157. $list_joined = array();
  158. $list_joined_final = array();
  159. $proceed_parent_id = array();
  160. foreach($list as $item) {
  161. if (is_array($list[$item["parent"]])) {
  162. $proceed_parent_id[] = $item['parent']; //prepare to be removed
  163. $item['label']['title'] = $list[$item['parent']]['label']['title'] . ', ' . $item['label']['title'];
  164. $item['parent'] = $list[$item["parent"]]['parent'];
  165. $item['publish'] = $list[$item["parent"]]['publish'];
  166. $item['priority'] = $list[$item["parent"]]['priority'];
  167. }
  168. $list_joined[] = $item;
  169. }
  170. //remove proceed parent items
  171. foreach ($list_joined as $item) {
  172. if (!in_array($item['id'], $proceed_parent_id)) $list_joined_final[] = $item;
  173. }
  174. return $list_joined_final;
  175. }
  176. /**
  177. * add parent data
  178. */
  179. public function addParentData($list) {
  180. foreach ($list as $k=>$item) {
  181. $list[$k]['parent_data'] = $this->getParentData($item);
  182. }
  183. return $list;
  184. }
  185. /**
  186. * sort by priority
  187. */
  188. public function sortByPriority($list) {
  189. /**
  190. * first pass parent_priority to the same level in multiarray (preparation for multisort function)
  191. */
  192. foreach ($list as $k=>$item) {
  193. $list[$k]['parent_priority'] = $item['parent_data']['priority'];
  194. }
  195. /**
  196. * use multisort function
  197. */
  198. $list = php_multisort($list, array(array('key'=>'parent_priority', 'sort'=>'DESC'), array('key'=>'parent', 'type'=>'numeric')));
  199. foreach ($list as $item) {
  200. $l[] = $item;
  201. }
  202. $list = $l;
  203. return $list;
  204. }
  205. }