PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Taxonomy/Controller/Component/TaxonomiesComponent.php

https://github.com/kareypowell/croogo
PHP | 202 lines | 124 code | 15 blank | 63 comment | 10 complexity | 0292c9d51e1d55b7990f8cc34ce9f1a6 MD5 | raw file
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * Taxonomies Component
  5. *
  6. * @category Component
  7. * @package Croogo.Taxonomy.Controller.Component
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class TaxonomiesComponent extends Component {
  14. /**
  15. * Other components used by this component
  16. *
  17. * @var array
  18. * @access public
  19. */
  20. public $components = array(
  21. 'Croogo.Croogo',
  22. );
  23. /**
  24. * Types for layout
  25. *
  26. * @var string
  27. * @access public
  28. */
  29. public $typesForLayout = array();
  30. /**
  31. * Vocabularies for layout
  32. *
  33. * @var string
  34. * @access public
  35. */
  36. public $vocabulariesForLayout = array();
  37. /**
  38. * Startup
  39. *
  40. * @param object $controller instance of controller
  41. * @return void
  42. */
  43. public function startup(Controller $controller) {
  44. $this->controller = $controller;
  45. if (isset($controller->Taxonomy)) {
  46. $this->Taxonomy = $controller->Taxonomy;
  47. } else {
  48. $this->Taxonomy = ClassRegistry::init('Taxonomy.Taxonomy');
  49. }
  50. if (!isset($this->controller->request->params['admin']) && !isset($this->controller->request->params['requested'])) {
  51. $this->types();
  52. $this->vocabularies();
  53. } else {
  54. $this->_adminData();
  55. }
  56. }
  57. public function beforeRender(Controller $controller) {
  58. $this->controller = $controller;
  59. $this->controller->set('types_for_layout', $this->typesForLayout);
  60. $this->controller->set('vocabularies_for_layout', $this->vocabulariesForLayout);
  61. }
  62. /**
  63. * Set variables for admin layout
  64. *
  65. * @return void
  66. */
  67. protected function _adminData() {
  68. // types
  69. $types = $this->Taxonomy->Vocabulary->Type->find('all', array(
  70. 'conditions' => array(
  71. 'Type.plugin' => null,
  72. ),
  73. 'order' => 'Type.alias ASC',
  74. ));
  75. $this->controller->set('types_for_admin_layout', $types);
  76. // vocabularies
  77. $vocabularies = $this->Taxonomy->Vocabulary->find('all', array(
  78. 'recursive' => '-1',
  79. 'conditions' => array(
  80. 'Vocabulary.plugin' => null,
  81. ),
  82. 'order' => 'Vocabulary.alias ASC',
  83. ));
  84. $this->controller->set('vocabularies_for_admin_layout', $vocabularies);
  85. }
  86. /**
  87. * Types
  88. *
  89. * Types will be available in this variable in views: $types_for_layout
  90. *
  91. * @return void
  92. */
  93. public function types() {
  94. $types = $this->Taxonomy->Vocabulary->Type->find('all', array(
  95. 'cache' => array(
  96. 'name' => 'types',
  97. 'config' => 'croogo_types',
  98. ),
  99. ));
  100. foreach ($types as $type) {
  101. $alias = $type['Type']['alias'];
  102. $this->typesForLayout[$alias] = $type;
  103. }
  104. }
  105. /**
  106. * Vocabularies
  107. *
  108. * Vocabularies will be available in this variable in views: $vocabularies_for_layout
  109. *
  110. * @return void
  111. */
  112. public function vocabularies() {
  113. $vocabularies = array();
  114. $themeData = $this->Croogo->getThemeData(Configure::read('Site.theme'));
  115. if (isset($themeData['vocabularies']) && is_array($themeData['vocabularies'])) {
  116. $vocabularies = Hash::merge($vocabularies, $themeData['vocabularies']);
  117. }
  118. $vocabularies = Hash::merge($vocabularies, array_keys($this->controller->Blocks->blocksData['vocabularies']));
  119. $vocabularies = array_unique($vocabularies);
  120. foreach ($vocabularies as $vocabularyAlias) {
  121. $vocabulary = $this->Taxonomy->Vocabulary->find('first', array(
  122. 'conditions' => array(
  123. 'Vocabulary.alias' => $vocabularyAlias,
  124. ),
  125. 'cache' => array(
  126. 'name' => 'vocabulary_' . $vocabularyAlias,
  127. 'config' => 'croogo_vocabularies',
  128. ),
  129. 'recursive' => '-1',
  130. ));
  131. if (isset($vocabulary['Vocabulary']['id'])) {
  132. $threaded = $this->Taxonomy->find('threaded', array(
  133. 'conditions' => array(
  134. 'Taxonomy.vocabulary_id' => $vocabulary['Vocabulary']['id'],
  135. ),
  136. 'contain' => array(
  137. 'Term',
  138. ),
  139. 'cache' => array(
  140. 'name' => 'vocabulary_threaded_' . $vocabularyAlias,
  141. 'config' => 'croogo_vocabularies',
  142. ),
  143. 'order' => 'Taxonomy.lft ASC',
  144. ));
  145. $this->vocabulariesForLayout[$vocabularyAlias] = array();
  146. $this->vocabulariesForLayout[$vocabularyAlias]['Vocabulary'] = $vocabulary['Vocabulary'];
  147. $this->vocabulariesForLayout[$vocabularyAlias]['threaded'] = $threaded;
  148. }
  149. }
  150. }
  151. /**
  152. * Prepare required taxonomy baseline data for use in views
  153. *
  154. * @param array $type Type data
  155. * @param array $options Options
  156. * @return void
  157. * @throws UnexpectedException
  158. */
  159. public function prepareCommonData($type, $options = array()) {
  160. $options = Hash::merge(array(
  161. 'modelClass' => $this->controller->modelClass,
  162. ), $options);
  163. $typeAlias = $type['Type']['alias'];
  164. $modelClass = $options['modelClass'];
  165. if (isset($this->controller->{$modelClass})) {
  166. $Model = $this->controller->{$modelClass};
  167. } else {
  168. throw new UnexpectedException(sprintf(
  169. 'Model %s not found in controller %s',
  170. $model, $this->controller->name
  171. ));
  172. }
  173. $Model->type = $typeAlias;
  174. $vocabularies = Hash::combine($type['Vocabulary'], '{n}.id', '{n}');
  175. $taxonomy = array();
  176. foreach ($type['Vocabulary'] as $vocabulary) {
  177. $vocabularyId = $vocabulary['id'];
  178. $taxonomy[$vocabularyId] = $Model->Taxonomy->getTree(
  179. $vocabulary['alias'],
  180. array('taxonomyId' => true)
  181. );
  182. }
  183. $this->controller->set(compact(
  184. 'type', 'typeAlias', 'taxonomy', 'vocabularies'
  185. ));
  186. }
  187. }