PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Blocks/Model/Block.php

https://github.com/kareypowell/croogo
PHP | 153 lines | 87 code | 14 blank | 52 comment | 1 complexity | 201c7bb04983ecf0d73b6c8904859c51 MD5 | raw file
  1. <?php
  2. App::uses('BlocksAppModel', 'Blocks.Model');
  3. /**
  4. * Block
  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 Block extends BlocksAppModel {
  14. /**
  15. * Model name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Block';
  21. /**
  22. * Behaviors used by the Model
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $actsAs = array(
  28. 'Croogo.Encoder',
  29. 'Croogo.Ordered' => array(
  30. 'field' => 'weight',
  31. 'foreign_key' => false,
  32. ),
  33. 'Croogo.Publishable',
  34. 'Croogo.Cached' => array(
  35. 'groups' => array(
  36. 'blocks',
  37. ),
  38. ),
  39. 'Croogo.Params',
  40. 'Croogo.Trackable',
  41. 'Search.Searchable',
  42. );
  43. /**
  44. * Validation
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. public $validate = array(
  50. 'title' => array(
  51. 'rule' => array('minLength', 1),
  52. 'message' => 'Title cannot be empty.',
  53. ),
  54. 'alias' => array(
  55. 'isUnique' => array(
  56. 'rule' => 'isUnique',
  57. 'message' => 'This alias has already been taken.',
  58. ),
  59. 'minLength' => array(
  60. 'rule' => array('minLength', 1),
  61. 'message' => 'Alias cannot be empty.',
  62. ),
  63. ),
  64. );
  65. /**
  66. * Filter search fields
  67. *
  68. * @var array
  69. * @access public
  70. */
  71. public $filterArgs = array(
  72. 'title' => array('type' => 'like', 'field' => array('Block.title', 'Block.alias')),
  73. 'region_id' => array('type' => 'value'),
  74. );
  75. /**
  76. * Find methods
  77. */
  78. public $findMethods = array(
  79. 'published' => true,
  80. );
  81. /**
  82. * Model associations: belongsTo
  83. *
  84. * @var array
  85. * @access public
  86. */
  87. public $belongsTo = array(
  88. 'Region' => array(
  89. 'className' => 'Blocks.Region',
  90. 'foreignKey' => 'region_id',
  91. 'counterCache' => true,
  92. 'counterScope' => array('Block.status >=' => CroogoStatus::PUBLISHED),
  93. ),
  94. );
  95. /**
  96. * Find Published blocks
  97. *
  98. * Query options:
  99. * - status Status
  100. * - regionId Region Id
  101. * - roleId Role Id
  102. * - cacheKey Cache key (optional)
  103. */
  104. protected function _findPublished($state, $query, $results = array()) {
  105. if ($state === 'after') {
  106. return $results;
  107. }
  108. $status = isset($query['status']) ? $query['status'] : $this->status();
  109. $regionId = isset($query['regionId']) ? $query['regionId'] : null;
  110. $roleId = isset($query['roleId']) ? $query['roleId'] : 3;
  111. $cacheKey = isset($query['cacheKey']) ? $query['cacheKey'] : $regionId . '_' . $roleId;
  112. unset($query['status'], $query['regionId'], $query['roleId'], $query['cacheKey']);
  113. $visibilityRolesField = $this->escapeField('visibility_roles');
  114. $default = array(
  115. 'conditions' => array(
  116. $this->escapeField('status') => $status,
  117. $this->escapeField('region_id') => $regionId,
  118. 'AND' => array(
  119. array(
  120. 'OR' => array(
  121. $visibilityRolesField => '',
  122. $visibilityRolesField . ' LIKE' => '%"' . $roleId . '"%',
  123. ),
  124. ),
  125. ),
  126. ),
  127. 'order' => array(
  128. $this->escapeField('weight') => 'ASC'
  129. ),
  130. 'cache' => array(
  131. 'prefix' => 'blocks_' . $cacheKey,
  132. 'config' => 'croogo_blocks',
  133. ),
  134. 'recursive' => '-1',
  135. );
  136. $query = Hash::merge($query, $default);
  137. return $query;
  138. }
  139. }