Base for a static organization website

ModelBehavior.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Model behaviors base class.
  4. *
  5. * Adds methods and automagic functionality to CakePHP Models.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Model
  17. * @since CakePHP(tm) v 1.2.0.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /**
  21. * Model behavior base class.
  22. *
  23. * Defines the Behavior interface, and contains common model interaction functionality. Behaviors
  24. * allow you to simulate mixins, and create reusable blocks of application logic, that can be reused across
  25. * several models. Behaviors also provide a way to hook into model callbacks and augment their behavior.
  26. *
  27. * ### Mixin methods
  28. *
  29. * Behaviors can provide mixin like features by declaring public methods. These methods should expect
  30. * the model instance to be shifted onto the parameter list.
  31. *
  32. * ```
  33. * function doSomething(Model $model, $arg1, $arg2) {
  34. * //do something
  35. * }
  36. * ```
  37. *
  38. * Would be called like `$this->Model->doSomething($arg1, $arg2);`.
  39. *
  40. * ### Mapped methods
  41. *
  42. * Behaviors can also define mapped methods. Mapped methods use pattern matching for method invocation. This
  43. * allows you to create methods similar to Model::findAllByXXX methods on your behaviors. Mapped methods need to
  44. * be declared in your behaviors `$mapMethods` array. The method signature for a mapped method is slightly different
  45. * than a normal behavior mixin method.
  46. *
  47. * ```
  48. * public $mapMethods = array('/do(\w+)/' => 'doSomething');
  49. *
  50. * function doSomething(Model $model, $method, $arg1, $arg2) {
  51. * //do something
  52. * }
  53. * ```
  54. *
  55. * The above will map every doXXX() method call to the behavior. As you can see, the model is
  56. * still the first parameter, but the called method name will be the 2nd parameter. This allows
  57. * you to munge the method name for additional information, much like Model::findAllByXX.
  58. *
  59. * @package Cake.Model
  60. * @see Model::$actsAs
  61. * @see BehaviorCollection::load()
  62. */
  63. class ModelBehavior extends Object {
  64. /**
  65. * Contains configuration settings for use with individual model objects. This
  66. * is used because if multiple models use this Behavior, each will use the same
  67. * object instance. Individual model settings should be stored as an
  68. * associative array, keyed off of the model name.
  69. *
  70. * @var array
  71. * @see Model::$alias
  72. */
  73. public $settings = array();
  74. /**
  75. * Allows the mapping of preg-compatible regular expressions to public or
  76. * private methods in this class, where the array key is a /-delimited regular
  77. * expression, and the value is a class method. Similar to the functionality of
  78. * the findBy* / findAllBy* magic methods.
  79. *
  80. * @var array
  81. */
  82. public $mapMethods = array();
  83. /**
  84. * Setup this behavior with the specified configuration settings.
  85. *
  86. * @param Model $model Model using this behavior
  87. * @param array $config Configuration settings for $model
  88. * @return void
  89. */
  90. public function setup(Model $model, $config = array()) {
  91. }
  92. /**
  93. * Clean up any initialization this behavior has done on a model. Called when a behavior is dynamically
  94. * detached from a model using Model::detach().
  95. *
  96. * @param Model $model Model using this behavior
  97. * @return void
  98. * @see BehaviorCollection::detach()
  99. */
  100. public function cleanup(Model $model) {
  101. if (isset($this->settings[$model->alias])) {
  102. unset($this->settings[$model->alias]);
  103. }
  104. }
  105. /**
  106. * beforeFind can be used to cancel find operations, or modify the query that will be executed.
  107. * By returning null/false you can abort a find. By returning an array you can modify/replace the query
  108. * that is going to be run.
  109. *
  110. * @param Model $model Model using this behavior
  111. * @param array $query Data used to execute this query, i.e. conditions, order, etc.
  112. * @return bool|array False or null will abort the operation. You can return an array to replace the
  113. * $query that will be eventually run.
  114. */
  115. public function beforeFind(Model $model, $query) {
  116. return true;
  117. }
  118. /**
  119. * After find callback. Can be used to modify any results returned by find.
  120. *
  121. * @param Model $model Model using this behavior
  122. * @param mixed $results The results of the find operation
  123. * @param bool $primary Whether this model is being queried directly (vs. being queried as an association)
  124. * @return mixed An array value will replace the value of $results - any other value will be ignored.
  125. */
  126. public function afterFind(Model $model, $results, $primary = false) {
  127. }
  128. /**
  129. * beforeValidate is called before a model is validated, you can use this callback to
  130. * add behavior validation rules into a models validate array. Returning false
  131. * will allow you to make the validation fail.
  132. *
  133. * @param Model $model Model using this behavior
  134. * @param array $options Options passed from Model::save().
  135. * @return mixed False or null will abort the operation. Any other result will continue.
  136. * @see Model::save()
  137. */
  138. public function beforeValidate(Model $model, $options = array()) {
  139. return true;
  140. }
  141. /**
  142. * afterValidate is called just after model data was validated, you can use this callback
  143. * to perform any data cleanup or preparation if needed
  144. *
  145. * @param Model $model Model using this behavior
  146. * @return mixed False will stop this event from being passed to other behaviors
  147. */
  148. public function afterValidate(Model $model) {
  149. return true;
  150. }
  151. /**
  152. * beforeSave is called before a model is saved. Returning false from a beforeSave callback
  153. * will abort the save operation.
  154. *
  155. * @param Model $model Model using this behavior
  156. * @param array $options Options passed from Model::save().
  157. * @return mixed False if the operation should abort. Any other result will continue.
  158. * @see Model::save()
  159. */
  160. public function beforeSave(Model $model, $options = array()) {
  161. return true;
  162. }
  163. /**
  164. * afterSave is called after a model is saved.
  165. *
  166. * @param Model $model Model using this behavior
  167. * @param bool $created True if this save created a new record
  168. * @param array $options Options passed from Model::save().
  169. * @return bool
  170. * @see Model::save()
  171. */
  172. public function afterSave(Model $model, $created, $options = array()) {
  173. return true;
  174. }
  175. /**
  176. * Before delete is called before any delete occurs on the attached model, but after the model's
  177. * beforeDelete is called. Returning false from a beforeDelete will abort the delete.
  178. *
  179. * @param Model $model Model using this behavior
  180. * @param bool $cascade If true records that depend on this record will also be deleted
  181. * @return mixed False if the operation should abort. Any other result will continue.
  182. */
  183. public function beforeDelete(Model $model, $cascade = true) {
  184. return true;
  185. }
  186. /**
  187. * After delete is called after any delete occurs on the attached model.
  188. *
  189. * @param Model $model Model using this behavior
  190. * @return void
  191. */
  192. public function afterDelete(Model $model) {
  193. }
  194. /**
  195. * DataSource error callback
  196. *
  197. * @param Model $model Model using this behavior
  198. * @param string $error Error generated in DataSource
  199. * @return void
  200. */
  201. public function onError(Model $model, $error) {
  202. }
  203. /**
  204. * If $model's whitelist property is non-empty, $field will be added to it.
  205. * Note: this method should *only* be used in beforeValidate or beforeSave to ensure
  206. * that it only modifies the whitelist for the current save operation. Also make sure
  207. * you explicitly set the value of the field which you are allowing.
  208. *
  209. * @param Model $model Model using this behavior
  210. * @param string $field Field to be added to $model's whitelist
  211. * @return void
  212. */
  213. protected function _addToWhitelist(Model $model, $field) {
  214. if (is_array($field)) {
  215. foreach ($field as $f) {
  216. $this->_addToWhitelist($model, $f);
  217. }
  218. return;
  219. }
  220. if (!empty($model->whitelist) && !in_array($field, $model->whitelist)) {
  221. $model->whitelist[] = $field;
  222. }
  223. }
  224. }