Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 214 lines (196 sloc) 6.893 kB
b672560 @josegonzalez initial commit
authored
1 <?php
2491d9f @josegonzalez Docblocks
authored
2 /**
3 * Application Model class
4 *
5 * Add your application-wide methods to the class, your models will inherit them.
6 *
7 * @package app
8 */
d260815 @josegonzalez merging in base_app
authored
9 App::import('Lib', 'LazyModel.LazyModel');
10 class AppModel extends LazyModel {
4208aad @josegonzalez Docblocking
authored
11 /**
12 * List of behaviors to load when the model object is initialized. Settings can be
13 * passed to behaviors by using the behavior name as index. Eg:
14 *
15 * var $actsAs = array('Translate', 'MyBehavior' => array('setting1' => 'value1'))
16 *
17 * @var array
18 * @access public
19 * @link http://book.cakephp.org/view/1072/Using-Behaviors
20 */
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
21 var $actsAs = array(
22 'CakeDjjob.CakeDjjob',
23 'Containable',
de8b144 @josegonzalez fixed Syntax error in $actsAs array of AppModel
authored
24 'Linkable.Linkable',
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
25 'Log.Logable' => array('change' => 'full')
26 );
4208aad @josegonzalez Docblocking
authored
27
28 /**
29 * Number of associations to recurse through during find calls. Fetches only
30 * the first level by default.
31 *
32 * @var integer
33 * @access public
34 * @link http://book.cakephp.org/view/1057/Model-Attributes#recursive-1063
35 */
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
36 var $recursive = -1;
4208aad @josegonzalez Docblocking
authored
37
38 /**
39 * Query currently executing.
40 *
41 * @var array
42 * @access public
43 */
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
44 var $query = null;
b672560 @josegonzalez initial commit
authored
45
d260815 @josegonzalez merging in base_app
authored
46 public function __construct($id = false, $table = null, $ds = null) {
47 parent::__construct($id, $table, $ds);
48 $this->_findMethods['paginatecount'] = true;
49 }
b672560 @josegonzalez initial commit
authored
50
51 /**
d260815 @josegonzalez merging in base_app
authored
52 * Automatically set contain to false if not otherwise specified
b672560 @josegonzalez initial commit
authored
53 *
4208aad @josegonzalez Docblocking
authored
54 * @param array $queryData Data used to execute this query, i.e. conditions, order, etc.
55 * @return mixed true if the operation should continue, false if it should abort; or, modified
56 * $queryData to continue with new $queryData
57 * @access public
58 * @link http://book.cakephp.org/view/1048/Callback-Methods#beforeFind-1049
b672560 @josegonzalez initial commit
authored
59 */
d260815 @josegonzalez merging in base_app
authored
60 public function beforeFind($queryData = array()) {
61 if (!isset($queryData['contain'])) {
62 $queryData['contain'] = false;
63 }
64 return $queryData;
65 }
b672560 @josegonzalez initial commit
authored
66
67 /**
d260815 @josegonzalez merging in base_app
authored
68 * Removes 'fields' key from count query on custom finds when it is an array,
69 * as it will completely break the Model::_findCount() call
b672560 @josegonzalez initial commit
authored
70 *
d260815 @josegonzalez merging in base_app
authored
71 * @param string $state Either "before" or "after"
72 * @param array $query
73 * @param array $data
74 * @return int The number of records found, or false
75 * @access protected
76 * @see Model::find()
77 */
78 public function _findPaginatecount($state, $query, $results = array()) {
79 if ($state == 'before' && isset($query['operation'])) {
80 if (!empty($query['fields']) && is_array($query['fields'])) {
81 if (!preg_match('/^count/i', $query['fields'][0])) {
82 unset($query['fields']);
83 }
84 }
85 }
86 return parent::_findCount($state, $query, $results);
87 }
b672560 @josegonzalez initial commit
authored
88
d260815 @josegonzalez merging in base_app
authored
89 /**
90 * Custom Model::paginateCount() method to support custom model find pagination
91 *
92 * @param array $conditions
93 * @param int $recursive
94 * @param array $extra
95 * @return array
96 */
97 public function paginateCount($conditions = array(), $recursive = 0, $extra = array()) {
98 $parameters = compact('conditions');
b672560 @josegonzalez initial commit
authored
99
d260815 @josegonzalez merging in base_app
authored
100 if ($recursive != $this->recursive) {
101 $parameters['recursive'] = $recursive;
102 }
b672560 @josegonzalez initial commit
authored
103
d260815 @josegonzalez merging in base_app
authored
104 if (isset($extra['type']) && isset($this->_findMethods[$extra['type']])) {
105 $extra['operation'] = 'count';
106 return $this->find($extra['type'], array_merge($parameters, $extra));
107 } else {
108 return $this->find('count', array_merge($parameters, $extra));
109 }
110 }
b672560 @josegonzalez initial commit
authored
111
112 /**
4208aad @josegonzalez Docblocking
authored
113 * Convenience method to update one record without invoking any callbacks
b672560 @josegonzalez initial commit
authored
114 *
4208aad @josegonzalez Docblocking
authored
115 * @param array $fields Set of fields and values, indexed by fields.
116 * Fields are treated as SQL snippets, to insert literal values manually escape your data.
117 * @param mixed $conditions Conditions to match, true for all records
118 * @return boolean True on success, false on Model::id is not set or failure
119 * @access public
120 * @author Jose Diaz-Gonzalez
121 * @link http://book.cakephp.org/view/1031/Saving-Your-Data
b672560 @josegonzalez initial commit
authored
122 **/
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
123 function update($fields, $conditions = array()) {
124 $conditions = (array) $conditions;
125 if (!$this->id) return false;
b672560 @josegonzalez initial commit
authored
126
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
127 $conditions = array_merge(array("{$this->alias}.$this->primaryKey" => $this->id), $conditions);
b672560 @josegonzalez initial commit
authored
128
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
129 return $this->updateAll($fields, $conditions);
130 }
b672560 @josegonzalez initial commit
authored
131
132 /**
133 * Disables/detaches all behaviors from model
134 *
4208aad @josegonzalez Docblocking
authored
135 * @param mixed $except string or array of behaviors to exclude from detachment
136 * @param boolean $detach If true, detaches the behavior instead of disabling it
137 * @return void
138 * @access public
139 * @author Jose Diaz-Gonzalez
b672560 @josegonzalez initial commit
authored
140 */
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
141 function disableAllBehaviors($except = array(), $detach = false) {
142 $behaviors = array_diff($this->Behaviors->attached(), (array) $except);
143 foreach ($behaviors as &$behavior) {
144 if ($detach) {
145 $this->Behaviors->detach($behavior);
146 } else {
147 $this->Behaviors->disable($behavior);
148 }
149 }
150 }
b672560 @josegonzalez initial commit
authored
151
152 /**
153 * Enables all previously disabled attachments
154 *
4208aad @josegonzalez Docblocking
authored
155 * @return void
156 * @access public
157 * @author Jose Diaz-Gonzalez
b672560 @josegonzalez initial commit
authored
158 */
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
159 function enableAllBehaviors() {
160 $behaviors = $this->Behaviors->attached();
161 foreach($behaviors as &$behavior) {
162 if (!$this->Behaviors->enabled($behavior)) {
163 $this->Behaviors->enable($behavior);
164 }
165 }
166 }
fa33beb @josegonzalez removed LookupableBehavior in favor of AppModel::__findLookup()
authored
167
a4666a0 @josegonzalez Model::postal_multiple() validation rule
authored
168 /**
169 * Checks that a given value is a valid postal code.
170 *
171 * Modified version of Validation::postal - allows for multiple
172 * countries to be specified as an array.
9ad344e @josegonzalez Whitespace save
authored
173 *
4208aad @josegonzalez Docblocking
authored
174 * @param mixed $check Value to check
175 * @param string $regex Regular expression to use
176 * @param mixed $country Countries to use for formatting
177 * @return boolean Success
178 * @access public
179 * @author Jamie Nay
180 * @link http://github.com/jamienay/postal_validation
a4666a0 @josegonzalez Model::postal_multiple() validation rule
authored
181 */
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
182 function postal_multiple($check, $regex = null, $country = null) {
183 // List of regular expressions to use, if a custom one isn't specified.
184 $countryRegs = array(
185 'uk' => '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i',
186 'ca' => '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z][ ]?[0-9][A-Z][0-9]\\b\\z/i',
187 'it' => '/^[0-9]{5}$/i',
188 'de' => '/^[0-9]{5}$/i',
189 'be' => '/^[1-9]{1}[0-9]{3}$/i',
190 'us' => '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i',
191 'default' => '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i' // Same as US.
192 );
193
194 $value = array_values($check);
195 $value = $value[0];
196 if ($regex) {
197 return preg_match($regex, $value);
198 } else if (!is_array($country)) {
199 return preg_match($countryRegs[$country], $value);
200 }
a4666a0 @josegonzalez Model::postal_multiple() validation rule
authored
201
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
202 foreach ($country as $check) {
203 if (!isset($countryRegs[$check]) && preg_match($countryRegs['default'], $value)) {
204 return true;
205 } else if (preg_match($countryRegs[$check], $value)) {
206 return true;
207 }
208 }
a4666a0 @josegonzalez Model::postal_multiple() validation rule
authored
209
c742c81 @josegonzalez formatting and removal of unnecessary AppModel methods
authored
210 return false;
211 }
6d55c05 @josegonzalez simplified AppModel::__findRandom()
authored
212
b672560 @josegonzalez initial commit
authored
213 }
214 ?>
Something went wrong with that request. Please try again.