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

/Blocks/Model/Region.php

https://github.com/kareypowell/croogo
PHP | 137 lines | 74 code | 12 blank | 51 comment | 3 complexity | 9c96905ffbeb3675afca06c1a3023eee MD5 | raw file
  1. <?php
  2. App::uses('BlocksAppModel', 'Blocks.Model');
  3. /**
  4. * Region
  5. *
  6. * @category Blocks.Model
  7. * @package Croogo.Blocks.Model
  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 Region extends BlocksAppModel {
  14. /**
  15. * Model name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Region';
  21. /**
  22. * Behaviors used by the Model
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $actsAs = array(
  28. 'Search.Searchable',
  29. 'Croogo.Cached' => array(
  30. 'groups' => array(
  31. 'blocks',
  32. ),
  33. ),
  34. 'Croogo.Trackable',
  35. );
  36. /**
  37. * Validation
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. public $validate = array(
  43. 'title' => array(
  44. 'rule' => array('minLength', 1),
  45. 'message' => 'Title cannot be empty.',
  46. ),
  47. 'alias' => array(
  48. 'isUnique' => array(
  49. 'rule' => 'isUnique',
  50. 'message' => 'This alias has already been taken.',
  51. ),
  52. 'minLength' => array(
  53. 'rule' => array('minLength', 1),
  54. 'message' => 'Alias cannot be empty.',
  55. ),
  56. ),
  57. );
  58. /**
  59. * Filter search fields
  60. *
  61. * @var array
  62. * @access public
  63. */
  64. public $filterArgs = array(
  65. 'title' => array('type' => 'like', 'field' => array('Region.title'))
  66. );
  67. /**
  68. * Model associations: hasMany
  69. *
  70. * @var array
  71. * @access public
  72. */
  73. public $hasMany = array(
  74. 'Block' => array(
  75. 'className' => 'Blocks.Block',
  76. 'foreignKey' => 'region_id',
  77. 'dependent' => false,
  78. 'limit' => 3,
  79. ),
  80. );
  81. /**
  82. * Display fields for this model
  83. *
  84. * @var array
  85. */
  86. protected $_displayFields = array(
  87. 'id',
  88. 'title',
  89. 'alias',
  90. );
  91. /**
  92. * Find methods
  93. */
  94. public $findMethods = array(
  95. 'active' => true,
  96. );
  97. /**
  98. * Find Regions currently in use
  99. */
  100. protected function _findActive($state, $query, $results = array()) {
  101. if ($state == 'after') {
  102. if ($results) {
  103. $keyPath = '{n}.'. $this->alias . '.id';
  104. $valuePath = '{n}.'. $this->alias . '.alias';
  105. $results = Hash::combine($results, $keyPath, $valuePath);
  106. }
  107. return $results;
  108. }
  109. $query = Hash::merge($query, array(
  110. 'recursive' => -1,
  111. 'conditions' => array(
  112. $this->escapeField('block_count') . ' >' => '0',
  113. ),
  114. 'fields' => array(
  115. $this->escapeField(),
  116. $this->escapeField('alias'),
  117. ),
  118. 'cache' => array(
  119. 'name' => 'regions',
  120. 'config' => 'croogo_blocks',
  121. ),
  122. ));
  123. return $query;
  124. }
  125. }