1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21:
22: 23: 24: 25: 26: 27: 28: 29:
30: class ContainableBehavior extends ModelBehavior {
31:
32: 33: 34: 35: 36:
37: public $types = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
38:
39: 40: 41: 42: 43:
44: public $runtime = array();
45:
46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
64: public function setup(Model $Model, $settings = array()) {
65: if (!isset($this->settings[$Model->alias])) {
66: $this->settings[$Model->alias] = array('recursive' => true, 'notices' => true, 'autoFields' => true);
67: }
68: $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
92: public function beforeFind(Model $Model, $query) {
93: $reset = (isset($query['reset']) ? $query['reset'] : true);
94: $noContain = false;
95: $contain = array();
96:
97: if (isset($this->runtime[$Model->alias]['contain'])) {
98: $noContain = empty($this->runtime[$Model->alias]['contain']);
99: $contain = $this->runtime[$Model->alias]['contain'];
100: unset($this->runtime[$Model->alias]['contain']);
101: }
102:
103: if (isset($query['contain'])) {
104: $noContain = $noContain || empty($query['contain']);
105: if ($query['contain'] !== false) {
106: $contain = array_merge($contain, (array)$query['contain']);
107: }
108: }
109: $noContain = $noContain && empty($contain);
110:
111: if (
112: $noContain || empty($contain) || (isset($contain[0]) && $contain[0] === null)
113: ) {
114: if ($noContain) {
115: $query['recursive'] = -1;
116: }
117: return $query;
118: }
119: if ((isset($contain[0]) && is_bool($contain[0])) || is_bool(end($contain))) {
120: $reset = is_bool(end($contain))
121: ? array_pop($contain)
122: : array_shift($contain);
123: }
124: $containments = $this->containments($Model, $contain);
125: $map = $this->containmentsMap($containments);
126:
127: $mandatory = array();
128: foreach ($containments['models'] as $name => $model) {
129: $instance = $model['instance'];
130: $needed = $this->fieldDependencies($instance, $map, false);
131: if (!empty($needed)) {
132: $mandatory = array_merge($mandatory, $needed);
133: }
134: if ($contain) {
135: $backupBindings = array();
136: foreach ($this->types as $relation) {
137: if (!empty($instance->__backAssociation[$relation])) {
138: $backupBindings[$relation] = $instance->__backAssociation[$relation];
139: } else {
140: $backupBindings[$relation] = $instance->{$relation};
141: }
142: }
143: foreach ($this->types as $type) {
144: $unbind = array();
145: foreach ($instance->{$type} as $assoc => $options) {
146: if (!isset($model['keep'][$assoc])) {
147: $unbind[] = $assoc;
148: }
149: }
150: if (!empty($unbind)) {
151: if (!$reset && empty($instance->__backOriginalAssociation)) {
152: $instance->__backOriginalAssociation = $backupBindings;
153: }
154: $instance->unbindModel(array($type => $unbind), $reset);
155: }
156: foreach ($instance->{$type} as $assoc => $options) {
157: if (isset($model['keep'][$assoc]) && !empty($model['keep'][$assoc])) {
158: if (isset($model['keep'][$assoc]['fields'])) {
159: $model['keep'][$assoc]['fields'] = $this->fieldDependencies($containments['models'][$assoc]['instance'], $map, $model['keep'][$assoc]['fields']);
160: }
161: if (!$reset && empty($instance->__backOriginalAssociation)) {
162: $instance->__backOriginalAssociation = $backupBindings;
163: } elseif ($reset) {
164: $instance->__backAssociation[$type] = $backupBindings[$type];
165: }
166: $instance->{$type}[$assoc] = array_merge($instance->{$type}[$assoc], $model['keep'][$assoc]);
167: }
168: if (!$reset) {
169: $instance->__backInnerAssociation[] = $assoc;
170: }
171: }
172: }
173: }
174: }
175:
176: if ($this->settings[$Model->alias]['recursive']) {
177: $query['recursive'] = (isset($query['recursive'])) ? $query['recursive'] : $containments['depth'];
178: }
179:
180: $autoFields = ($this->settings[$Model->alias]['autoFields']
181: && !in_array($Model->findQueryType, array('list', 'count'))
182: && !empty($query['fields']));
183:
184: if (!$autoFields) {
185: return $query;
186: }
187:
188: $query['fields'] = (array)$query['fields'];
189: foreach (array('hasOne', 'belongsTo') as $type) {
190: if (!empty($Model->{$type})) {
191: foreach ($Model->{$type} as $assoc => $data) {
192: if ($Model->useDbConfig == $Model->{$assoc}->useDbConfig && !empty($data['fields'])) {
193: foreach ((array)$data['fields'] as $field) {
194: $query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
195: }
196: }
197: }
198: }
199: }
200:
201: if (!empty($mandatory[$Model->alias])) {
202: foreach ($mandatory[$Model->alias] as $field) {
203: if ($field == '--primaryKey--') {
204: $field = $Model->primaryKey;
205: } elseif (preg_match('/^.+\.\-\-[^-]+\-\-$/', $field)) {
206: list($modelName, $field) = explode('.', $field);
207: if ($Model->useDbConfig == $Model->{$modelName}->useDbConfig) {
208: $field = $modelName . '.' . (
209: ($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field
210: );
211: } else {
212: $field = null;
213: }
214: }
215: if ($field !== null) {
216: $query['fields'][] = $field;
217: }
218: }
219: }
220: $query['fields'] = array_unique($query['fields']);
221: return $query;
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231:
232: public function contain(Model $Model) {
233: $args = func_get_args();
234: $contain = call_user_func_array('am', array_slice($args, 1));
235: $this->runtime[$Model->alias]['contain'] = $contain;
236: }
237:
238: 239: 240: 241: 242: 243: 244: 245:
246: public function resetBindings(Model $Model) {
247: if (!empty($Model->__backOriginalAssociation)) {
248: $Model->__backAssociation = $Model->__backOriginalAssociation;
249: unset($Model->__backOriginalAssociation);
250: }
251: $Model->resetAssociations();
252: if (!empty($Model->__backInnerAssociation)) {
253: $assocs = $Model->__backInnerAssociation;
254: $Model->__backInnerAssociation = array();
255: foreach ($assocs as $currentModel) {
256: $this->resetBindings($Model->$currentModel);
257: }
258: }
259: }
260:
261: 262: 263: 264: 265: 266: 267: 268: 269:
270: public function containments(Model $Model, $contain, $containments = array(), $throwErrors = null) {
271: $options = array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery');
272: $keep = array();
273: if ($throwErrors === null) {
274: $throwErrors = (empty($this->settings[$Model->alias]) ? true : $this->settings[$Model->alias]['notices']);
275: }
276: foreach ((array)$contain as $name => $children) {
277: if (is_numeric($name)) {
278: $name = $children;
279: $children = array();
280: }
281: if (preg_match('/(?<!\.)\(/', $name)) {
282: $name = str_replace('(', '.(', $name);
283: }
284: if (strpos($name, '.') !== false) {
285: $chain = explode('.', $name);
286: $name = array_shift($chain);
287: $children = array(implode('.', $chain) => $children);
288: }
289:
290: $children = (array)$children;
291: foreach ($children as $key => $val) {
292: if (is_string($key) && is_string($val) && !in_array($key, $options, true)) {
293: $children[$key] = (array)$val;
294: }
295: }
296:
297: $keys = array_keys($children);
298: if ($keys && isset($children[0])) {
299: $keys = array_merge(array_values($children), $keys);
300: }
301:
302: foreach ($keys as $i => $key) {
303: if (is_array($key)) {
304: continue;
305: }
306: $optionKey = in_array($key, $options, true);
307: if (!$optionKey && is_string($key) && preg_match('/^[a-z(]/', $key) && (!isset($Model->{$key}) || !is_object($Model->{$key}))) {
308: $option = 'fields';
309: $val = array($key);
310: if ($key{0} == '(') {
311: $val = preg_split('/\s*,\s*/', substr($key, 1, -1));
312: } elseif (preg_match('/ASC|DESC$/', $key)) {
313: $option = 'order';
314: $val = $Model->{$name}->alias . '.' . $key;
315: } elseif (preg_match('/[ =!]/', $key)) {
316: $option = 'conditions';
317: $val = $Model->{$name}->alias . '.' . $key;
318: }
319: $children[$option] = is_array($val) ? $val : array($val);
320: $newChildren = null;
321: if (!empty($name) && !empty($children[$key])) {
322: $newChildren = $children[$key];
323: }
324: unset($children[$key], $children[$i]);
325: $key = $option;
326: $optionKey = true;
327: if (!empty($newChildren)) {
328: $children = Hash::merge($children, $newChildren);
329: }
330: }
331: if ($optionKey && isset($children[$key])) {
332: if (!empty($keep[$name][$key]) && is_array($keep[$name][$key])) {
333: $keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array)$children[$key]);
334: } else {
335: $keep[$name][$key] = $children[$key];
336: }
337: unset($children[$key]);
338: }
339: }
340:
341: if (!isset($Model->{$name}) || !is_object($Model->{$name})) {
342: if ($throwErrors) {
343: trigger_error(__d('cake_dev', 'Model "%s" is not associated with model "%s"', $Model->alias, $name), E_USER_WARNING);
344: }
345: continue;
346: }
347:
348: $containments = $this->containments($Model->{$name}, $children, $containments);
349: $depths[] = $containments['depth'] + 1;
350: if (!isset($keep[$name])) {
351: $keep[$name] = array();
352: }
353: }
354:
355: if (!isset($containments['models'][$Model->alias])) {
356: $containments['models'][$Model->alias] = array('keep' => array(), 'instance' => &$Model);
357: }
358:
359: $containments['models'][$Model->alias]['keep'] = array_merge($containments['models'][$Model->alias]['keep'], $keep);
360: $containments['depth'] = empty($depths) ? 0 : max($depths);
361: return $containments;
362: }
363:
364: 365: 366: 367: 368: 369: 370: 371:
372: public function fieldDependencies(Model $Model, $map, $fields = array()) {
373: if ($fields === false) {
374: foreach ($map as $parent => $children) {
375: foreach ($children as $type => $bindings) {
376: foreach ($bindings as $dependency) {
377: if ($type == 'hasAndBelongsToMany') {
378: $fields[$parent][] = '--primaryKey--';
379: } elseif ($type == 'belongsTo') {
380: $fields[$parent][] = $dependency . '.--primaryKey--';
381: }
382: }
383: }
384: }
385: return $fields;
386: }
387: if (empty($map[$Model->alias])) {
388: return $fields;
389: }
390: foreach ($map[$Model->alias] as $type => $bindings) {
391: foreach ($bindings as $dependency) {
392: $innerFields = array();
393: switch ($type) {
394: case 'belongsTo':
395: $fields[] = $Model->{$type}[$dependency]['foreignKey'];
396: break;
397: case 'hasOne':
398: case 'hasMany':
399: $innerFields[] = $Model->$dependency->primaryKey;
400: $fields[] = $Model->primaryKey;
401: break;
402: }
403: if (!empty($innerFields) && !empty($Model->{$type}[$dependency]['fields'])) {
404: $Model->{$type}[$dependency]['fields'] = array_unique(array_merge($Model->{$type}[$dependency]['fields'], $innerFields));
405: }
406: }
407: }
408: return array_unique($fields);
409: }
410:
411: 412: 413: 414: 415: 416:
417: public function containmentsMap($containments) {
418: $map = array();
419: foreach ($containments['models'] as $name => $model) {
420: $instance = $model['instance'];
421: foreach ($this->types as $type) {
422: foreach ($instance->{$type} as $assoc => $options) {
423: if (isset($model['keep'][$assoc])) {
424: $map[$name][$type] = isset($map[$name][$type]) ? array_merge($map[$name][$type], (array)$assoc) : (array)$assoc;
425: }
426: }
427: }
428: }
429: return $map;
430: }
431:
432: }
433: