PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Taxonomy/Model/Taxonomy.php

https://github.com/kareypowell/croogo
PHP | 173 lines | 112 code | 14 blank | 47 comment | 12 complexity | 7c7f7383c137b1e7817a882d551ae1c5 MD5 | raw file
  1. <?php
  2. App::uses('TaxonomyAppModel', 'Taxonomy.Model');
  3. /**
  4. * Taxonomy
  5. *
  6. * @category Taxonomy.Model
  7. * @package Croogo.Taxonomy.Model
  8. * @since 1.3.1
  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 Taxonomy extends TaxonomyAppModel {
  14. /**
  15. * Model name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Taxonomy';
  21. /**
  22. * Behaviors used by the Model
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $actsAs = array(
  28. 'Tree',
  29. 'Croogo.Cached' => array(
  30. 'groups' => array(
  31. 'nodes',
  32. 'taxonomy',
  33. ),
  34. ),
  35. );
  36. /**
  37. * Model associations: belongsTo
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. public $belongsTo = array(
  43. 'Term' => array(
  44. 'className' => 'Taxonomy.Term',
  45. 'foreignKey' => 'term_id',
  46. 'conditions' => '',
  47. 'fields' => '',
  48. 'order' => '',
  49. ),
  50. 'Vocabulary' => array(
  51. 'className' => 'Taxonomy.Vocabulary',
  52. 'foreignKey' => 'vocabulary_id',
  53. 'conditions' => '',
  54. 'fields' => '',
  55. 'order' => '',
  56. ),
  57. );
  58. /**
  59. * Generates a tree of terms for a vocabulary
  60. *
  61. * @param string $alias Vocabulary alias (e.g., categories)
  62. * @param array $options
  63. * @return array
  64. */
  65. public function getTree($alias, $options = array()) {
  66. $_options = array(
  67. 'key' => 'slug', // Term.slug
  68. 'value' => 'title', // Term.title
  69. 'taxonomyId' => false,
  70. 'cache' => false,
  71. );
  72. $options = array_merge($_options, $options);
  73. // Check if cached
  74. if ($this->useCache && isset($options['cache']['config'])) {
  75. if (isset($options['cache']['prefix'])) {
  76. $cacheName = $options['cache']['prefix'] . '_' . md5($alias . serialize($options));
  77. } elseif (isset($options['cache']['name'])) {
  78. $cacheName = $options['cache']['name'];
  79. }
  80. if (isset($cacheName)) {
  81. $cacheName .= '_' . Configure::read('Config.language');
  82. $cachedResult = Cache::read($cacheName, $options['cache']['config']);
  83. if ($cachedResult) {
  84. return $cachedResult;
  85. }
  86. }
  87. }
  88. $vocabulary = $this->Vocabulary->findByAlias($alias);
  89. if (!isset($vocabulary['Vocabulary']['id'])) {
  90. return false;
  91. }
  92. $this->Behaviors->attach('Tree', array(
  93. 'scope' => array(
  94. $this->alias . '.vocabulary_id' => $vocabulary['Vocabulary']['id'],
  95. ),
  96. ));
  97. $treeConditions = array(
  98. $this->alias . '.vocabulary_id' => $vocabulary['Vocabulary']['id'],
  99. );
  100. $tree = $this->generateTreeList($treeConditions, '{n}.' . $this->alias . '.term_id', '{n}.' . $this->alias . '.id');
  101. $termsIds = array_keys($tree);
  102. $terms = $this->Term->find('list', array(
  103. 'conditions' => array(
  104. 'Term.id' => $termsIds,
  105. ),
  106. 'fields' => array(
  107. $options['key'],
  108. $options['value'],
  109. 'id',
  110. ),
  111. ));
  112. $termsTree = array();
  113. foreach ($tree as $termId => $tvId) {
  114. if (isset($terms[$termId])) {
  115. $term = $terms[$termId];
  116. $key = array_keys($term);
  117. $key = $key['0'];
  118. $value = $term[$key];
  119. if (strstr($tvId, '_')) {
  120. $tvIdN = str_replace('_', '', $tvId);
  121. $tvIdE = explode($tvIdN, $tvId);
  122. $value = $tvIdE['0'] . $value;
  123. }
  124. if (!$options['taxonomyId']) {
  125. $termsTree[$key] = $value;
  126. } else {
  127. $termsTree[str_replace('_', '', $tvId)] = $value;
  128. }
  129. }
  130. }
  131. // Write cache
  132. if (isset($cacheName)) {
  133. Cache::write($cacheName, $termsTree, $options['cache']['config']);
  134. }
  135. return $termsTree;
  136. }
  137. /**
  138. * Check if Term HABTM Vocabulary.
  139. *
  140. * If yes, return Taxonomy ID
  141. * otherwise, return false
  142. *
  143. * @param integer $termId
  144. * @param integer $vocabularyId
  145. * @return boolean
  146. */
  147. public function termInVocabulary($termId, $vocabularyId) {
  148. $taxonomy = $this->find('first', array(
  149. 'conditions' => array(
  150. $this->alias . '.term_id' => $termId,
  151. $this->alias . '.vocabulary_id' => $vocabularyId,
  152. ),
  153. ));
  154. if (isset($taxonomy[$this->alias]['id'])) {
  155. return $taxonomy[$this->alias]['id'];
  156. }
  157. return false;
  158. }
  159. }