PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Model/CroogoAppModel.php

https://github.com/kareypowell/croogo
PHP | 267 lines | 139 code | 21 blank | 107 comment | 25 complexity | 06e2b89db47785c6be49089036f41739 MD5 | raw file
  1. <?php
  2. App::uses('Model', 'Model');
  3. /**
  4. * Croogo App Model
  5. *
  6. * @category Croogo.Model
  7. * @package Croogo.Croogo.Model
  8. * @version 1.5
  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 CroogoAppModel extends Model {
  14. /**
  15. * use Caching
  16. *
  17. * @var string
  18. */
  19. public $useCache = true;
  20. /**
  21. * Default behaviors
  22. */
  23. public $actsAs = array(
  24. 'Containable',
  25. );
  26. /**
  27. * Display fields for admin_index. Use displayFields()
  28. *
  29. * @var array
  30. * @access protected
  31. */
  32. protected $_displayFields = array();
  33. /**
  34. * Edit fields for admin_edit. Use editFields()
  35. *
  36. * @var array
  37. * @access protected
  38. */
  39. protected $_editFields = array();
  40. /**
  41. * Constructor
  42. *
  43. * @param mixed $id Set this ID for this model on startup, can also be an array of options, see above.
  44. * @param string $table Name of database table to use.
  45. * @param string $ds DataSource connection name.
  46. */
  47. public function __construct($id = false, $table = null, $ds = null) {
  48. Croogo::applyHookProperties('Hook.model_properties', $this);
  49. parent::__construct($id, $table, $ds);
  50. }
  51. /**
  52. * Override find function to use caching
  53. *
  54. * Caching can be done either by unique names,
  55. * or prefixes where a hashed value of $options array is appended to the name
  56. *
  57. * @param mixed $type Type of find operation (all / first / count / neighbors / list / threaded)
  58. * @param array $options Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks)
  59. * @return array Array of records, or Null of failure
  60. * @access public
  61. */
  62. public function find($type = 'first', $options = array()) {
  63. if ($this->useCache) {
  64. $cachedResults = $this->_findCached($type, $options);
  65. if ($cachedResults) {
  66. return $cachedResults;
  67. }
  68. }
  69. $args = func_get_args();
  70. $results = call_user_func_array(array('parent', 'find'), $args);
  71. if ($this->useCache) {
  72. if (isset($options['cache']['name']) && isset($options['cache']['config'])) {
  73. $cacheName = $options['cache']['name'];
  74. } elseif (isset($options['cache']['prefix']) && isset($options['cache']['config'])) {
  75. $cacheName = $options['cache']['prefix'] . md5(serialize($options));
  76. }
  77. if (isset($cacheName)) {
  78. $cacheName .= '_' . Configure::read('Config.language');
  79. Cache::write($cacheName, $results, $options['cache']['config']);
  80. $this->cacheConfig = $options['cache']['config'];
  81. }
  82. }
  83. return $results;
  84. }
  85. /**
  86. * Check if find() was already cached
  87. *
  88. * @param mixed $type
  89. * @param array $options
  90. * @return array Array of records, or False when no records found in cache
  91. * @access private
  92. */
  93. protected function _findCached($type, $options) {
  94. if (isset($options['cache']['name']) && isset($options['cache']['config'])) {
  95. $cacheName = $options['cache']['name'];
  96. } elseif (isset($options['cache']['prefix']) && isset($options['cache']['config'])) {
  97. $cacheName = $options['cache']['prefix'] . md5(serialize($options));
  98. } else {
  99. return false;
  100. }
  101. $cacheName .= '_' . Configure::read('Config.language');
  102. $results = Cache::read($cacheName, $options['cache']['config']);
  103. if ($results) {
  104. return $results;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Updates multiple model records based on a set of conditions.
  110. *
  111. * call afterSave() callback after successful update.
  112. *
  113. * @param array $fields Set of fields and values, indexed by fields.
  114. * Fields are treated as SQL snippets, to insert literal values manually escape your data.
  115. * @param mixed $conditions Conditions to match, true for all records
  116. * @return boolean True on success, false on failure
  117. * @access public
  118. */
  119. public function updateAll($fields, $conditions = true) {
  120. $args = func_get_args();
  121. $output = call_user_func_array(array('parent', 'updateAll'), $args);
  122. if ($output) {
  123. $created = false;
  124. $options = array();
  125. $field = sprintf('%s.%s', $this->alias, $this->primaryKey);
  126. if (!empty($args[1][$field])) {
  127. foreach ((array)$args[1][$field] as $id) {
  128. $this->id = $id;
  129. $event = new CakeEvent('Model.afterSave', $this, array(
  130. $created, $options
  131. ));
  132. $this->getEventManager()->dispatch($event);
  133. }
  134. }
  135. $this->_clearCache();
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Fix to the Model::invalidate() method to display localized validate messages
  142. *
  143. * @param string $field The name of the field to invalidate
  144. * @param mixed $value Name of validation rule that was not failed, or validation message to
  145. * be returned. If no validation key is provided, defaults to true.
  146. * @access public
  147. */
  148. public function invalidate($field, $value = true) {
  149. return parent::invalidate($field, __d('croogo', $value));
  150. }
  151. /**
  152. * Return formatted display fields
  153. *
  154. * @param array $displayFields
  155. * @return array
  156. */
  157. public function displayFields($displayFields = null) {
  158. if (isset($displayFields)) {
  159. $this->_displayFields = $displayFields;
  160. }
  161. $out = array();
  162. $defaults = array('sort' => true, 'type' => 'text', 'url' => array(), 'options' => array());
  163. foreach ($this->_displayFields as $field => $label) {
  164. if (is_int($field)) {
  165. $field = $label;
  166. list(, $label) = pluginSplit($label);
  167. $out[$field] = Hash::merge($defaults, array(
  168. 'label' => Inflector::humanize($label),
  169. ));
  170. } elseif (is_array($label)) {
  171. $out[$field] = Hash::merge($defaults, $label);
  172. if (!isset($out[$field]['label'])) {
  173. $out[$field]['label'] = Inflector::humanize($field);
  174. }
  175. } else {
  176. $out[$field] = Hash::merge($defaults, array(
  177. 'label' => $label,
  178. ));
  179. }
  180. }
  181. return $out;
  182. }
  183. /**
  184. * Return formatted edit fields
  185. *
  186. * @param array $editFields
  187. * @return array
  188. */
  189. public function editFields($editFields = null) {
  190. if (isset($editFields)) {
  191. $this->_editFields = $editFields;
  192. }
  193. if (empty($this->_editFields)) {
  194. $this->_editFields = array_keys($this->schema());
  195. $id = array_search('id', $this->_editFields);
  196. if ($id !== false) {
  197. unset($this->_editFields[$id]);
  198. }
  199. }
  200. $out = array();
  201. foreach ($this->_editFields as $field => $label) {
  202. if (is_int($field)) {
  203. $out[$label] = array();
  204. } elseif (is_array($label)) {
  205. $out[$field] = $label;
  206. } else {
  207. $out[$field] = array(
  208. 'label' => $label,
  209. );
  210. }
  211. }
  212. return $out;
  213. }
  214. /**
  215. * Validation method for alias field
  216. * @return bool true when validation successful
  217. * @deprecated Protected validation methods are no longer supported
  218. */
  219. protected function _validAlias($check) {
  220. return $this->validAlias($check);
  221. }
  222. /**
  223. * Validation method for name or title fields
  224. * @return bool true when validation successful
  225. * @deprecated Protected validation methods are no longer supported
  226. */
  227. protected function _validName($check) {
  228. return $this->validName($check);
  229. }
  230. /**
  231. * Validation method for alias field
  232. *
  233. * @return bool true when validation successful
  234. */
  235. public function validAlias($check) {
  236. return (preg_match('/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}-_]+$/mu', $check[key($check)]) == 1);
  237. }
  238. /**
  239. * Validation method for name or title fields
  240. *
  241. * @return bool true when validation successful
  242. */
  243. public function validName($check) {
  244. return (preg_match('/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}-_\[\]\(\) ]+$/mu', $check[key($check)]) == 1);
  245. }
  246. }