Assembla home | Assembla project page
 

root/branch/app_model.php

Revision 133, 9.9 kB (checked in by digitalspaghetti, 1 year ago)

Improved accessibility plugin, added themes

  • Property svn:executable set to *
Line 
1 <?php
2 /* SVN FILE: $Id: app_model.php 5118 2007-05-18 17:19:53Z phpnut $ */
3
4 /**
5  * Application model for Cake.
6  *
7  * This file is application-wide model file. You can put all
8  * application-wide model-related methods here.
9  *
10  * PHP versions 4 and 5
11  *
12  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
13  * Copyright 2005-2007, Cake Software Foundation, Inc.
14  *                                1785 E. Sahara Avenue, Suite 490-204
15  *                                Las Vegas, Nevada 89104
16  *
17  * Licensed under The MIT License
18  * Redistributions of files must retain the above copyright notice.
19  *
20  * @filesource
21  * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
22  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
23  * @package            cake
24  * @subpackage        cake.app
25  * @since            CakePHP(tm) v 0.2.9
26  * @version            $Revision: 5118 $
27  * @modifiedby        $LastChangedBy: phpnut $
28  * @lastmodified    $Date: 2007-05-18 18:19:53 +0100 (Fri, 18 May 2007) $
29  * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
30  */
31
32 /**
33  * Application model for Cake.
34  *
35  * Add your application-wide methods in the class below, your models
36  * will inherit them.
37  *
38  * @package        cake
39  * @subpackage    cake.app
40  */
41 class AppModel extends Model{
42     
43     /** Default behaviors used in Webrocket
44      *
45      *     Utf8 - Converts characters to UTF-8 on save
46      *     Containable - This allows associations to unbind associations from a model recursively on the fly.
47      */
48     var $actsAs = array(/*'Utf8' => array('save' => array('convertTo' => 'UTF-8'),'read'=>array('useMbstring'=>false)),*/
49                         'Containable'
50     );
51     
52     // This gets the URL stub of a item such as post or page
53     function getUniqueUrl($string, $field)
54     {
55         //Build URL
56         $currentUrl = $this->_getStringAsURL($string);
57         // Look for same URL, if so try until we find a unique one
58         $conditions = array($this->name . '.' . $field => 'LIKE ' . $currentUrl . '%');
59         $result = $this->findAll($conditions, $this->name . '.*', null);
60         if ($result !== false && count($result) > 0)
61         {
62             $sameUrls = array();
63             foreach ($result as $record)
64             {
65                 $sameUrls[] = $record[$this->name][$field];
66             }
67         }
68         if (isset($sameUrls) && count($sameUrls) > 0)
69         {
70             $currentBegginingUrl = $currentUrl;
71             $currentIndex = 1;
72             while ($currentIndex > 0)
73             {
74                 if (!in_array($currentBegginingUrl . '_' . $currentIndex, $sameUrls))
75                 {
76                     $currentUrl = $currentBegginingUrl . '_' . $currentIndex;
77                     $currentIndex = -1;
78                 }
79                 $currentIndex++;
80             }
81         }
82         return $currentUrl;
83     }
84
85     function _getStringAsURL($string)
86     {
87         // Define the maximum number of characters allowed as part of the URL
88         $currentMaximumURLLength = 100;
89         $string = strtolower($string);
90         // Any non valid characters will be treated as _, also remove duplicate _
91         $string = preg_replace('/[^a-z0-9_]/i', '_', $string);
92         $string = preg_replace('/_[_]*/i', '_', $string);
93         // Cut at a specified length
94         if (strlen($string) > $currentMaximumURLLength)
95         {
96             $string = substr($string, 0, $currentMaximumURLLength);
97         }
98         // Remove beggining and ending signs
99         $string = preg_replace('/_$/i', '', $string);
100         $string = preg_replace('/^_/i', '', $string);
101         return $string;
102     }
103     
104     /**
105      * The below code is only needed for othAuth.  May remove
106      */
107         function afterFind($results)
108         {
109           if (isset($this->__runResetExpects) && $this->__runResetExpects)
110             {
111               $this->__resetExpects();
112               unset($this->__runResetExpects);
113             }
114           return parent::afterFind($results);
115         }
116       
117       function unbindAll($params = array())
118         {
119           foreach ($this->__associations as $ass)
120             {
121               if (!empty($this->{$ass}))
122                 {
123                   $this->__backAssociation[$ass] = $this->{$ass};
124                   if (isset($params[$ass]))
125                     {
126                       foreach ($this->{$ass} as $model => $detail)
127                         {
128                           if (!in_array($model, $params[$ass]))
129                             {
130                               $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
131                               unset($this->{$ass}[$model]);
132                             }
133                         }
134                     }
135                   else
136                     {
137                       $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
138                       $this->{$ass} = array();
139                     }
140                 }
141             }
142           return true;
143         }
144       /**
145        * Unbinds all relations from a model except the specified ones. Calling this function without
146        * parameters unbinds all related models.
147        *
148        * @access public
149        * @since 1.0
150        */
151       function expects()
152         {
153           $models = array();
154           $arguments = func_get_args();
155           $innerCall = false;
156           if (!empty($arguments) && is_bool($arguments[0]))
157             {
158               $innerCall = $arguments[0];
159             }
160           foreach ($arguments as $index => $argument)
161             {
162               if (is_array($argument))
163                 {
164                   if (count($argument) > 0)
165                     {
166                       $arguments = am($arguments, $argument);
167                     }
168                   unset($arguments[$index]);
169                 }
170             }
171           foreach ($arguments as $index => $argument)
172             {
173               if (!is_string($argument))
174                 {
175                   unset($arguments[$index]);
176                 }
177             }
178           if (count($arguments) == 0)
179             {
180               $models[$this->name] = array();
181             }
182           else
183             {
184               foreach ($arguments as $argument)
185                 {
186                   if (strpos($argument, '.') !== false)
187                     {
188                       $model = substr($argument, 0, strpos($argument, '.'));
189                       $child = substr($argument, strpos($argument, '.') + 1);
190                       if ($child == $model)
191                         {
192                           $models[$model] = array();
193                         }
194                       else
195                         {
196                           $models[$model][] = $child;
197                         }
198                     }
199                   else
200                     {
201                       $models[$this->name][] = $argument;
202                     }
203                 }
204             }
205           $relationTypes = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
206           foreach ($models as $bindingName => $children)
207             {
208               $model = null;
209               foreach ($relationTypes as $relationType)
210                 {
211                   $currentRelation = (isset($this->$relationType) ? $this->$relationType : null);
212                   if (isset($currentRelation) && isset($currentRelation[$bindingName]) && is_array($currentRelation[$bindingName]) && isset($currentRelation[$bindingName]['className']))
213                     {
214                       $model = $currentRelation[$bindingName]['className'];
215                       break;
216                     }
217                 }
218               if (!isset($model))
219                 {
220                   $model = $bindingName;
221                 }
222               if (isset($model) && $model != $this->name && isset($this->$model))
223                 {
224                   if (!isset($this->__backInnerAssociation))
225                     {
226                       $this->__backInnerAssociation = array();
227                     }
228                   $this->__backInnerAssociation[] = $model;
229                   $this->$model->expects(true, $children);
230                 }
231             }
232           if (isset($models[$this->name]))
233             {
234               foreach ($models as $model => $children)
235                 {
236                   if ($model != $this->name)
237                     {
238                       $models[$this->name][] = $model;
239                     }
240                 }
241               $models = array_unique($models[$this->name]);
242               $unbind = array();
243               foreach ($relationTypes as $relation)
244                 {
245                   if (isset($this->$relation))
246                     {
247                       foreach ($this->$relation as $bindingName => $bindingData)
248                         {
249                           if (!in_array($bindingName, $models))
250                             {
251                               $unbind[$relation][] = $bindingName;
252                             }
253                         }
254                     }
255                 }
256               if (count($unbind) > 0)
257                 {
258                   $this->unbindModel($unbind);
259                 }
260             }
261           if (!$innerCall)
262             {
263               $this->__runResetExpects = true;
264             }
265         }
266       /**
267        * Resets all relations and inner model relations after calling expects() and find().
268        *
269        * @access private
270        * @since 1.1
271        */
272       function __resetExpects()
273         {
274           if (isset($this->__backAssociation))
275             {
276               $this->__resetAssociations();
277             }
278           if (isset($this->__backInnerAssociation))
279             {
280               foreach ($this->__backInnerAssociation as $model)
281                 {
282                   $this->$model->__resetExpects();
283                 }
284               unset($this->__backInnerAssociation);
285             }
286         }
287         
288     function publish($id)
289     {
290         $this->id = $id;
291         $this->$this->name->savefield('published', 1);
292     }
293     
294     function unpublish($id)
295     {
296         $this->id = $id;
297         $this->$this->name->savefield('published', 0);
298     }
299     
300     function usertheme()
301     {
302         
303     }
304 }
305 ?>
Note: See TracBrowser for help on using the browser.