Assembla home | Assembla project page
 

root/trunk/app_model.php

Revision 2, 6.1 kB (checked in by digitalspaghetti, 2 years ago)

Initial import of 0.3.0 codebase

Line 
1 <?php
2 class AppModel extends Model{
3     
4     var $actsAs = array('Utf8' => array('save' => array('convertTo' => 'UTF-8') ) );
5     
6     function getUniqueUrl($string, $field)
7     {
8         // Build URL
9         
10         $currentUrl = $this->_getStringAsURL($string);
11         
12         // Look for same URL, if so try until we find a unique one
13         
14         $conditions = array($this->name . '.' . $field => 'LIKE ' . $currentUrl . '%');
15         
16         $result = $this->findAll($conditions, $this->name . '.*', null);
17         
18         if ($result !== false && count($result) > 0)
19         {
20             $sameUrls = array();
21             
22             foreach($result as $record)
23             {
24                 $sameUrls[] = $record[$this->name][$field];
25             }
26         }
27     
28         if (isset($sameUrls) && count($sameUrls) > 0)
29         {
30             $currentBegginingUrl = $currentUrl;
31     
32             $currentIndex = 1;
33     
34             while($currentIndex > 0)
35             {
36                 if (!in_array($currentBegginingUrl . '_' . $currentIndex, $sameUrls))
37                 {
38                     $currentUrl = $currentBegginingUrl . '_' . $currentIndex;
39     
40                     $currentIndex = -1;
41                 }
42     
43                 $currentIndex++;
44             }
45         }
46         
47         return $currentUrl;
48     }
49     
50     function _getStringAsURL($string)
51     {
52         // Define the maximum number of characters allowed as part of the URL
53         
54         $currentMaximumURLLength = 100;
55         
56         $string = strtolower($string);
57         
58         // Any non valid characters will be treated as _, also remove duplicate _
59         
60         $string = preg_replace('/[^a-z0-9_]/i', '_', $string);
61         $string = preg_replace('/_[_]*/i', '_', $string);
62         
63         // Cut at a specified length
64         
65         if (strlen($string) > $currentMaximumURLLength)
66         {
67             $string = substr($string, 0, $currentMaximumURLLength);
68         }
69         
70         // Remove beggining and ending signs
71         
72         $string = preg_replace('/_$/i', '', $string);
73         $string = preg_replace('/^_/i', '', $string);
74         
75         return $string;
76     }
77     
78     
79      function afterFind($results)
80     {
81         if (isset($this->__runResetExpects) && $this->__runResetExpects)
82         {
83             $this->__resetExpects();
84             unset($this->__runResetExpects);
85         }
86         
87         return parent::afterFind($results);
88     }
89     
90     
91     function unbindAll($params = array())
92     {
93         foreach($this->__associations as $ass)
94         {
95             if(!empty($this->{$ass}))
96             {
97                  $this->__backAssociation[$ass] = $this->{$ass};
98                 if(isset($params[$ass]))
99                 {
100                     foreach($this->{$ass} as $model => $detail)
101                     {
102                         if(!in_array($model,$params[$ass]))
103                         {
104                              $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
105                             unset($this->{$ass}[$model]);
106                         }
107                     }
108                 }else
109                 {
110                     $this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
111                     $this->{$ass} = array();
112                 }
113                 
114             }
115         }
116         return true;
117     }
118     
119     /**
120      * Unbinds all relations from a model except the specified ones. Calling this function without
121      * parameters unbinds all related models.
122      *
123      * @access public
124      * @since 1.0
125      */
126     function expects()
127     {
128         $models = array();
129         $arguments = func_get_args();
130         $innerCall = false;
131
132         if (!empty($arguments) && is_bool($arguments[0]))
133         {
134             $innerCall = $arguments[0];
135         }
136         
137         foreach($arguments as $index => $argument)
138         {
139             if (is_array($argument))
140             {
141                 if (count($argument) > 0)
142                 {
143                     $arguments = am($arguments, $argument);
144                 }
145
146                 unset($arguments[$index]);
147             }
148         }
149         
150         foreach($arguments as $index => $argument)
151         {
152             if (!is_string($argument))
153             {
154                 unset($arguments[$index]);
155             }
156         }
157
158         if (count($arguments) == 0)
159         {
160             $models[$this->name] = array();
161         }
162         else
163         {
164             foreach($arguments as $argument)
165             {
166                 if (strpos($argument, '.') !== false)
167                 {
168                     $model = substr($argument, 0, strpos($argument, '.'));
169                     $child = substr($argument, strpos($argument, '.') + 1);
170
171                     if ($child == $model)
172                     {
173                         $models[$model] = array();
174                     }
175                     else
176                     {
177                         $models[$model][] = $child;
178                     }
179                 }
180                 else
181                 {
182                     $models[$this->name][] = $argument;
183                 }
184             }
185         }
186         
187         $relationTypes = array ('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
188
189         foreach($models as $bindingName => $children)
190         {
191             $model = null;
192             
193             foreach($relationTypes as $relationType)
194             {
195                 $currentRelation = (isset($this->$relationType) ? $this->$relationType : null);
196                 
197                 if (isset($currentRelation) && isset($currentRelation[$bindingName]) && is_array($currentRelation[$bindingName]) && isset($currentRelation[$bindingName]['className']))
198                 {
199                     $model = $currentRelation[$bindingName]['className'];
200                     break;
201                 }
202             }
203             
204             if (!isset($model))
205             {
206                 $model = $bindingName;
207             }
208             
209             if (isset($model) && $model != $this->name && isset($this->$model))
210             {
211                 if (!isset($this->__backInnerAssociation))
212                 {
213                     $this->__backInnerAssociation = array();
214                 }
215                 
216                 $this->__backInnerAssociation[] = $model;
217                 
218                 $this->$model->expects(true, $children);
219             }
220         }
221         
222         if (isset($models[$this->name]))
223         {
224             foreach($models as $model => $children)
225             {
226                 if ($model != $this->name)
227                 {
228                     $models[$this->name][] = $model;
229                 }
230             }
231     
232             $models = array_unique($models[$this->name]);
233             $unbind = array();
234     
235             foreach($relationTypes as $relation)
236             {
237                 if (isset($this->$relation))
238                 {
239                     foreach($this->$relation as $bindingName => $bindingData)
240                     {
241                         if (!in_array($bindingName, $models))
242                         {
243                             $unbind[$relation][] = $bindingName;
244                         }
245                     }
246                 }
247             }
248     
249             if (count($unbind) > 0)
250             {
251                 $this->unbindModel($unbind);
252             }
253         }
254
255         if (!$innerCall)
256         {
257             $this->__runResetExpects = true;
258         }
259     }
260     
261     /**
262      * Resets all relations and inner model relations after calling expects() and find().
263      *
264      * @access private
265      * @since 1.1
266      */
267     function __resetExpects()
268     {
269         if (isset($this->__backAssociation))
270         {
271             $this->__resetAssociations();
272         }
273         
274         if (isset($this->__backInnerAssociation))
275         {
276             foreach($this->__backInnerAssociation as $model)
277             {
278                 $this->$model->__resetExpects();
279             }
280             
281             unset($this->__backInnerAssociation);
282         }
283     }
284 }
285 ?>
Note: See TracBrowser for help on using the browser.