croogo /Menus/Model/Link.php

Language PHP Lines 128
MD5 Hash 3ca60237888254007957c60c1e98b6b1
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

App::uses('MenusAppModel', 'Menus.Model');

/**
 * Link
 *
 * @category Model
 * @package  Croogo.Menus.Model
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class Link extends MenusAppModel {

/**
 * Model name
 *
 * @var string
 * @access public
 */
	public $name = 'Link';

/**
 * Behaviors used by the Model
 *
 * @var array
 * @access public
 */
	public $actsAs = array(
		'Croogo.Encoder',
		'Tree',
		'Croogo.Cached' => array(
			'groups' => array(
				'menus',
			),
		),
		'Croogo.Params',
		'Croogo.Publishable',
		'Croogo.Trackable',
	);

/**
 * Validation
 *
 * @var array
 * @access public
 */
	public $validate = array(
		'title' => array(
			'rule' => array('minLength', 1),
			'message' => 'Title cannot be empty.',
		),
		'link' => array(
			'rule' => array('minLength', 1),
			'message' => 'Link cannot be empty.',
		),
	);

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
	public $belongsTo = array(
		'Menu' => array(
			'className' => 'Menus.Menu',
			'counterCache' => true,
		)
	);

/**
 * Allow to change Tree scope to a specific menu
 *
 * @param int $menuId menu id
 * @return void
 */
	public function setTreeScope($menuId) {
		$settings = array(
			'scope' => array($this->alias . '.menu_id' => $menuId),
		);
		if ($this->Behaviors->loaded('Tree')) {
			$this->Behaviors->Tree->setup($this, $settings);
		} else {
			$this->Behaviors->load('Tree', $settings);
		}
	}

/**
 * If we are moving between Menus, save original id so that Link::afterSave()
 * recover() can recover the tree
 *
 */
	public function beforeSave($options = array()) {
		if (!isset($this->data['Link']['menu_id']) || !isset($this->data['Link']['id'])) {
			return true;
		}
		$previousMenuId = $this->field('menu_id', array(
			$this->escapeField('id') => $this->data['Link']['id']
		));
		$hasMenuChanged = ($previousMenuId != $this->data['Link']['menu_id']);
		if ($hasMenuChanged) {
			$this->_previousMenuId = $previousMenuId;
		}

		return true;
	}

/**
 * Calls TreeBehavior::recover when we are changing scope
 */
	public function afterSave($created, $options = array()) {
		if ($created) {
			return;
		}
		if (isset($this->_previousMenuId)) {
			$this->setTreeScope($this->data['Link']['menu_id']);
			$this->recover();
			$this->setTreeScope($this->_previousMenuId);
			$this->recover();
			unset($this->_previousMenuId);
		}
	}

}
Back to Top