/models/node.php

https://github.com/ShadowCross/croogo · PHP · 244 lines · 154 code · 4 blank · 86 comment · 17 complexity · fe5350385c662162d8cc0185b842219b MD5 · raw file

  1. <?php
  2. /**
  3. * Node
  4. *
  5. * PHP version 5
  6. *
  7. * @category Model
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class Node extends AppModel {
  15. /**
  16. * Model name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Node';
  22. /**
  23. * Behaviors used by the Model
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $actsAs = array(
  29. 'Containable',
  30. 'Tree',
  31. 'Encoder',
  32. 'Meta',
  33. 'Url',
  34. 'Cached' => array(
  35. 'prefix' => array(
  36. 'node_',
  37. 'nodes_',
  38. 'croogo_nodes_',
  39. ),
  40. ),
  41. );
  42. /**
  43. * Node type
  44. *
  45. * If the Model is associated to Node model, this variable holds the Node type value
  46. *
  47. * @var string
  48. * @access public
  49. */
  50. public $type = null;
  51. /**
  52. * Guid
  53. *
  54. * @var string
  55. * @access public
  56. */
  57. public $guid = null;
  58. /**
  59. * Validation
  60. *
  61. * @var array
  62. * @access public
  63. */
  64. public $validate = array(
  65. 'title' => array(
  66. 'rule' => 'notEmpty',
  67. 'message' => 'This field cannot be left blank.',
  68. ),
  69. 'slug' => array(
  70. 'isUniquePerType' => array(
  71. 'rule' => 'isUniquePerType',
  72. 'message' => 'This slug has already been taken.',
  73. ),
  74. 'minLength' => array(
  75. 'rule' => array('minLength', 1),
  76. 'message' => 'Slug cannot be empty.',
  77. ),
  78. ),
  79. );
  80. /**
  81. * Model associations: belongsTo
  82. *
  83. * @var array
  84. * @access public
  85. */
  86. public $belongsTo = array(
  87. 'User' => array(
  88. 'className' => 'User',
  89. 'foreignKey' => 'user_id',
  90. 'conditions' => '',
  91. 'fields' => '',
  92. 'order' => '',
  93. ),
  94. );
  95. /**
  96. * Model associations: hasMany
  97. *
  98. * @var array
  99. * @access public
  100. */
  101. public $hasMany = array(
  102. 'Comment' => array(
  103. 'className' => 'Comment',
  104. 'foreignKey' => 'node_id',
  105. 'dependent' => false,
  106. 'conditions' => array('Comment.status' => 1),
  107. 'fields' => '',
  108. 'order' => '',
  109. 'limit' => '5',
  110. 'offset' => '',
  111. 'exclusive' => '',
  112. 'finderQuery' => '',
  113. 'counterQuery' => '',
  114. ),
  115. 'Meta' => array(
  116. 'className' => 'Meta',
  117. 'foreignKey' => 'foreign_key',
  118. 'dependent' => false,
  119. 'conditions' => array('Meta.model' => 'Node'),
  120. 'fields' => '',
  121. 'order' => 'Meta.key ASC',
  122. 'limit' => '',
  123. 'offset' => '',
  124. 'exclusive' => '',
  125. 'finderQuery' => '',
  126. 'counterQuery' => '',
  127. ),
  128. );
  129. /**
  130. * Model associations: hasAndBelongsToMany
  131. *
  132. * @var array
  133. * @access public
  134. */
  135. public $hasAndBelongsToMany = array(
  136. 'Taxonomy' => array(
  137. 'className' => 'Taxonomy',
  138. 'with' => 'NodesTaxonomy',
  139. 'joinTable' => 'nodes_taxonomies',
  140. 'foreignKey' => 'node_id',
  141. 'associationForeignKey' => 'taxonomy_id',
  142. 'unique' => true,
  143. 'conditions' => '',
  144. 'fields' => '',
  145. 'order' => '',
  146. 'limit' => '',
  147. 'offset' => '',
  148. 'finderQuery' => '',
  149. 'deleteQuery' => '',
  150. 'insertQuery' => '',
  151. ),
  152. );
  153. /**
  154. * beforeFind callback
  155. *
  156. * @param array $q
  157. * @return array
  158. */
  159. public function beforeFind($q) {
  160. if ($this->type != null && !isset($q['conditions']['Node.type'])) {
  161. $q['conditions']['Node.type'] = $this->type;
  162. }
  163. return $q;
  164. }
  165. /**
  166. * beforeSave callback
  167. *
  168. * @return boolean
  169. */
  170. public function beforeSave() {
  171. if ($this->type != null) {
  172. $this->data['Node']['type'] = $this->type;
  173. }
  174. $this->__cacheTerms();
  175. return true;
  176. }
  177. /**
  178. * Caches Term in Node.terms field
  179. *
  180. * @return void
  181. */
  182. public function __cacheTerms() {
  183. if (isset($this->data['Taxonomy']['Taxonomy']) && count($this->data['Taxonomy']['Taxonomy']) > 0) {
  184. $taxonomyIds = $this->data['Taxonomy']['Taxonomy'];
  185. $taxonomies = $this->Taxonomy->find('all', array(
  186. 'conditions' => array(
  187. 'Taxonomy.id' => $taxonomyIds,
  188. ),
  189. ));
  190. $terms = Set::combine($taxonomies, '{n}.Term.id', '{n}.Term.slug');
  191. $this->data['Node']['terms'] = $this->encodeData($terms, array(
  192. 'trim' => false,
  193. 'json' => true,
  194. ));
  195. }
  196. }
  197. /**
  198. * Returns false if any fields passed match any (by default, all if $or = false) of their matching values.
  199. *
  200. * @param array $fields Field/value pairs to search (if no values specified, they are pulled from $this->data)
  201. * @param boolean $or If false, all fields specified must match in order for a false return value
  202. * @return boolean False if any records matching any fields are found
  203. * @access public
  204. */
  205. function isUniquePerType($fields, $or = true) {
  206. if (!is_array($fields)) {
  207. $fields = func_get_args();
  208. if (is_bool($fields[count($fields) - 1])) {
  209. $or = $fields[count($fields) - 1];
  210. unset($fields[count($fields) - 1]);
  211. }
  212. }
  213. foreach ($fields as $field => $value) {
  214. if (is_numeric($field)) {
  215. unset($fields[$field]);
  216. $field = $value;
  217. if (isset($this->data[$this->alias][$field])) {
  218. $value = $this->data[$this->alias][$field];
  219. } else {
  220. $value = null;
  221. }
  222. }
  223. if (strpos($field, '.') === false) {
  224. unset($fields[$field]);
  225. $fields[$this->alias . '.' . $field] = $value;
  226. }
  227. }
  228. if ($or) {
  229. $fields = array('or' => $fields);
  230. }
  231. if (!empty($this->id)) {
  232. $fields[$this->alias . '.' . $this->primaryKey . ' !='] = $this->id;
  233. }
  234. if (!empty($this->type)) {
  235. $fields[$this->alias . '.type'] = $this->type;
  236. }
  237. return ($this->find('count', array('conditions' => $fields, 'recursive' => -1)) == 0);
  238. }
  239. }
  240. ?>