PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Menus/Model/Link.php

https://github.com/kareypowell/croogo
PHP | 127 lines | 68 code | 11 blank | 48 comment | 8 complexity | 3ca60237888254007957c60c1e98b6b1 MD5 | raw file
  1. <?php
  2. App::uses('MenusAppModel', 'Menus.Model');
  3. /**
  4. * Link
  5. *
  6. * @category Model
  7. * @package Croogo.Menus.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 Link extends MenusAppModel {
  14. /**
  15. * Model name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Link';
  21. /**
  22. * Behaviors used by the Model
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $actsAs = array(
  28. 'Croogo.Encoder',
  29. 'Tree',
  30. 'Croogo.Cached' => array(
  31. 'groups' => array(
  32. 'menus',
  33. ),
  34. ),
  35. 'Croogo.Params',
  36. 'Croogo.Publishable',
  37. 'Croogo.Trackable',
  38. );
  39. /**
  40. * Validation
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. public $validate = array(
  46. 'title' => array(
  47. 'rule' => array('minLength', 1),
  48. 'message' => 'Title cannot be empty.',
  49. ),
  50. 'link' => array(
  51. 'rule' => array('minLength', 1),
  52. 'message' => 'Link cannot be empty.',
  53. ),
  54. );
  55. /**
  56. * Model associations: belongsTo
  57. *
  58. * @var array
  59. * @access public
  60. */
  61. public $belongsTo = array(
  62. 'Menu' => array(
  63. 'className' => 'Menus.Menu',
  64. 'counterCache' => true,
  65. )
  66. );
  67. /**
  68. * Allow to change Tree scope to a specific menu
  69. *
  70. * @param int $menuId menu id
  71. * @return void
  72. */
  73. public function setTreeScope($menuId) {
  74. $settings = array(
  75. 'scope' => array($this->alias . '.menu_id' => $menuId),
  76. );
  77. if ($this->Behaviors->loaded('Tree')) {
  78. $this->Behaviors->Tree->setup($this, $settings);
  79. } else {
  80. $this->Behaviors->load('Tree', $settings);
  81. }
  82. }
  83. /**
  84. * If we are moving between Menus, save original id so that Link::afterSave()
  85. * recover() can recover the tree
  86. *
  87. */
  88. public function beforeSave($options = array()) {
  89. if (!isset($this->data['Link']['menu_id']) || !isset($this->data['Link']['id'])) {
  90. return true;
  91. }
  92. $previousMenuId = $this->field('menu_id', array(
  93. $this->escapeField('id') => $this->data['Link']['id']
  94. ));
  95. $hasMenuChanged = ($previousMenuId != $this->data['Link']['menu_id']);
  96. if ($hasMenuChanged) {
  97. $this->_previousMenuId = $previousMenuId;
  98. }
  99. return true;
  100. }
  101. /**
  102. * Calls TreeBehavior::recover when we are changing scope
  103. */
  104. public function afterSave($created, $options = array()) {
  105. if ($created) {
  106. return;
  107. }
  108. if (isset($this->_previousMenuId)) {
  109. $this->setTreeScope($this->data['Link']['menu_id']);
  110. $this->recover();
  111. $this->setTreeScope($this->_previousMenuId);
  112. $this->recover();
  113. unset($this->_previousMenuId);
  114. }
  115. }
  116. }