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