CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.10 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.10
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper
  • None

Classes

  • AclBehavior
  • ContainableBehavior
  • TranslateBehavior
  • TreeBehavior
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @package       Cake.Model.Behavior
 13:  * @since         CakePHP(tm) v 1.2.0.4525
 14:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 15:  */
 16: 
 17: App::uses('ModelBehavior', 'Model');
 18: App::uses('I18n', 'I18n');
 19: App::uses('I18nModel', 'Model');
 20: 
 21: /**
 22:  * Translate behavior
 23:  *
 24:  * @package       Cake.Model.Behavior
 25:  * @link https://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
 26:  */
 27: class TranslateBehavior extends ModelBehavior {
 28: 
 29: /**
 30:  * Used for runtime configuration of model
 31:  *
 32:  * @var array
 33:  */
 34:     public $runtime = array();
 35: 
 36: /**
 37:  * Stores the joinTable object for generating joins.
 38:  *
 39:  * @var object
 40:  */
 41:     protected $_joinTable;
 42: 
 43: /**
 44:  * Stores the runtime model for generating joins.
 45:  *
 46:  * @var Model
 47:  */
 48:     protected $_runtimeModel;
 49: 
 50: /**
 51:  * Callback
 52:  *
 53:  * $config for TranslateBehavior should be
 54:  * array('fields' => array('field_one',
 55:  * 'field_two' => 'FieldAssoc', 'field_three'))
 56:  *
 57:  * With above example only one permanent hasMany will be joined (for field_two
 58:  * as FieldAssoc)
 59:  *
 60:  * $config could be empty - and translations configured dynamically by
 61:  * bindTranslation() method
 62:  *
 63:  * By default INNER joins are used to fetch translations. In order to use
 64:  * other join types $config should contain 'joinType' key:
 65:  * ```
 66:  * array(
 67:  *     'fields' => array('field_one', 'field_two' => 'FieldAssoc', 'field_three'),
 68:  *     'joinType' => 'LEFT',
 69:  * )
 70:  * ```
 71:  * In a model it may be configured this way:
 72:  * ```
 73:  * public $actsAs = array(
 74:  *     'Translate' => array(
 75:  *         'content',
 76:  *         'title',
 77:  *         'joinType' => 'LEFT',
 78:  *     ),
 79:  * );
 80:  * ```
 81:  *
 82:  * @param Model $Model Model the behavior is being attached to.
 83:  * @param array $config Array of configuration information.
 84:  * @return mixed
 85:  */
 86:     public function setup(Model $Model, $config = array()) {
 87:         $db = ConnectionManager::getDataSource($Model->useDbConfig);
 88:         if (!$db->connected) {
 89:             trigger_error(
 90:                 __d('cake_dev', 'Datasource %s for TranslateBehavior of model %s is not connected', $Model->useDbConfig, $Model->alias),
 91:                 E_USER_ERROR
 92:             );
 93:             return false;
 94:         }
 95: 
 96:         $this->settings[$Model->alias] = array();
 97:         $this->runtime[$Model->alias] = array(
 98:             'fields' => array(),
 99:             'joinType' => 'INNER',
100:         );
101:         if (isset($config['joinType'])) {
102:             $this->runtime[$Model->alias]['joinType'] = $config['joinType'];
103:             unset($config['joinType']);
104:         }
105:         $this->translateModel($Model);
106:         return $this->bindTranslation($Model, $config, false);
107:     }
108: 
109: /**
110:  * Cleanup Callback unbinds bound translations and deletes setting information.
111:  *
112:  * @param Model $Model Model being detached.
113:  * @return void
114:  */
115:     public function cleanup(Model $Model) {
116:         $this->unbindTranslation($Model);
117:         unset($this->settings[$Model->alias]);
118:         unset($this->runtime[$Model->alias]);
119:     }
120: 
121: /**
122:  * beforeFind Callback
123:  *
124:  * @param Model $Model Model find is being run on.
125:  * @param array $query Array of Query parameters.
126:  * @return array Modified query
127:  */
128:     public function beforeFind(Model $Model, $query) {
129:         $this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
130:         $locale = $this->_getLocale($Model);
131:         if (empty($locale)) {
132:             return $query;
133:         }
134:         $db = $Model->getDataSource();
135:         $RuntimeModel = $this->translateModel($Model);
136: 
137:         if (!empty($RuntimeModel->tablePrefix)) {
138:             $tablePrefix = $RuntimeModel->tablePrefix;
139:         } else {
140:             $tablePrefix = $db->config['prefix'];
141:         }
142:         $joinTable = new StdClass();
143:         $joinTable->tablePrefix = $tablePrefix;
144:         $joinTable->table = $RuntimeModel->table;
145:         $joinTable->schemaName = $RuntimeModel->getDataSource()->getSchemaName();
146: 
147:         $this->_joinTable = $joinTable;
148:         $this->_runtimeModel = $RuntimeModel;
149: 
150:         if (is_string($query['fields'])) {
151:             if ($query['fields'] === "COUNT(*) AS {$db->name('count')}") {
152:                 $query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
153:                 $query['joins'][] = array(
154:                     'type' => $this->runtime[$Model->alias]['joinType'],
155:                     'alias' => $RuntimeModel->alias,
156:                     'table' => $joinTable,
157:                     'conditions' => array(
158:                         $Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
159:                         $RuntimeModel->escapeField('model') => $Model->name,
160:                         $RuntimeModel->escapeField('locale') => $locale
161:                     )
162:                 );
163:                 $conditionFields = $this->_checkConditions($Model, $query);
164:                 foreach ($conditionFields as $field) {
165:                     $query = $this->_addJoin($Model, $query, $field, $field, $locale);
166:                 }
167:                 unset($this->_joinTable, $this->_runtimeModel);
168:                 return $query;
169:             } else {
170:                 $query['fields'] = CakeText::tokenize($query['fields']);
171:             }
172:         }
173:         $addFields = $this->_getFields($Model, $query);
174:         $this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
175:         $query = $this->_addAllJoins($Model, $query, $addFields);
176:         $this->runtime[$Model->alias]['beforeFind'] = $addFields;
177:         unset($this->_joinTable, $this->_runtimeModel);
178:         return $query;
179:     }
180: 
181: /**
182:  * Gets fields to be retrieved.
183:  *
184:  * @param Model $Model The model being worked on.
185:  * @param array $query The query array to take fields from.
186:  * @return array The fields.
187:  */
188:     protected function _getFields(Model $Model, $query) {
189:         $fields = array_merge(
190:             $this->settings[$Model->alias],
191:             $this->runtime[$Model->alias]['fields']
192:         );
193:         $addFields = array();
194:         if (empty($query['fields'])) {
195:             $addFields = $fields;
196:         } elseif (is_array($query['fields'])) {
197:             $isAllFields = (
198:                 in_array($Model->alias . '.' . '*', $query['fields']) ||
199:                 in_array($Model->escapeField('*'), $query['fields'])
200:             );
201:             foreach ($fields as $key => $value) {
202:                 $field = (is_numeric($key)) ? $value : $key;
203:                 if ($isAllFields ||
204:                     in_array($Model->alias . '.' . $field, $query['fields']) ||
205:                     in_array($field, $query['fields'])
206:                 ) {
207:                     $addFields[] = $field;
208:                 }
209:             }
210:         }
211:         return $addFields;
212:     }
213: 
214: /**
215:  * Appends all necessary joins for translated fields.
216:  *
217:  * @param Model $Model The model being worked on.
218:  * @param array $query The query array to append joins to.
219:  * @param array $addFields The fields being joined.
220:  * @return array The modified query
221:  */
222:     protected function _addAllJoins(Model $Model, $query, $addFields) {
223:         $locale = $this->_getLocale($Model);
224:         if ($addFields) {
225:             foreach ($addFields as $_f => $field) {
226:                 $aliasField = is_numeric($_f) ? $field : $_f;
227:                 foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
228:                     $key = array_search($_field, (array)$query['fields']);
229:                     if ($key !== false) {
230:                         unset($query['fields'][$key]);
231:                     }
232:                 }
233:                 $query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
234:             }
235:         }
236:         return $query;
237:     }
238: 
239: /**
240:  * Check a query's conditions for translated fields.
241:  * Return an array of translated fields found in the conditions.
242:  *
243:  * @param Model $Model The model being read.
244:  * @param array $query The query array.
245:  * @return array The list of translated fields that are in the conditions.
246:  */
247:     protected function _checkConditions(Model $Model, $query) {
248:         if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) {
249:             return array();
250:         }
251:         return $this->_getConditionFields($Model, $query['conditions']);
252:     }
253: 
254: /**
255:  * Extracts condition field names recursively.
256:  *
257:  * @param Model $Model The model being read.
258:  * @param array $conditions The conditions array.
259:  * @return array The list of condition fields.
260:  */
261:     protected function _getConditionFields(Model $Model, $conditions) {
262:         $conditionFields = array();
263:         foreach ($conditions as $col => $val) {
264:             if (is_array($val)) {
265:                 $subConditionFields = $this->_getConditionFields($Model, $val);
266:                 $conditionFields = array_merge($conditionFields, $subConditionFields);
267:             }
268:             foreach ($this->settings[$Model->alias] as $field => $assoc) {
269:                 if (is_numeric($field)) {
270:                     $field = $assoc;
271:                 }
272:                 if (strpos($col, $field) !== false) {
273:                     $conditionFields[] = $field;
274:                 }
275:             }
276:         }
277:         return $conditionFields;
278:     }
279: 
280: /**
281:  * Appends a join for translated fields.
282:  *
283:  * @param Model $Model The model being worked on.
284:  * @param array $query The query array to append a join to.
285:  * @param string $field The field name being joined.
286:  * @param string $aliasField The aliased field name being joined.
287:  * @param string|array $locale The locale(s) having joins added.
288:  * @return array The modified query
289:  */
290:     protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
291:         $db = ConnectionManager::getDataSource($Model->useDbConfig);
292:         $RuntimeModel = $this->_runtimeModel;
293:         $joinTable = $this->_joinTable;
294:         $aliasVirtual = "i18n_{$field}";
295:         $alias = "I18n__{$field}";
296:         if (is_array($locale)) {
297:             foreach ($locale as $_locale) {
298:                 $aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
299:                 $aliasLocale = "{$alias}__{$_locale}";
300:                 $Model->virtualFields[$aliasVirtualLocale] = "{$aliasLocale}.content";
301:                 if (!empty($query['fields']) && is_array($query['fields'])) {
302:                     $query['fields'][] = $aliasVirtualLocale;
303:                 }
304:                 $query['joins'][] = array(
305:                     'type' => 'LEFT',
306:                     'alias' => $aliasLocale,
307:                     'table' => $joinTable,
308:                     'conditions' => array(
309:                         $Model->escapeField() => $db->identifier("{$aliasLocale}.foreign_key"),
310:                         "{$aliasLocale}.model" => $Model->name,
311:                         "{$aliasLocale}.{$RuntimeModel->displayField}" => $aliasField,
312:                         "{$aliasLocale}.locale" => $_locale
313:                     )
314:                 );
315:             }
316:         } else {
317:             $Model->virtualFields[$aliasVirtual] = "{$alias}.content";
318:             if (!empty($query['fields']) && is_array($query['fields'])) {
319:                 $query['fields'][] = $aliasVirtual;
320:             }
321:             $query['joins'][] = array(
322:                 'type' => $this->runtime[$Model->alias]['joinType'],
323:                 'alias' => $alias,
324:                 'table' => $joinTable,
325:                 'conditions' => array(
326:                     "{$Model->alias}.{$Model->primaryKey}" => $db->identifier("{$alias}.foreign_key"),
327:                     "{$alias}.model" => $Model->name,
328:                     "{$alias}.{$RuntimeModel->displayField}" => $aliasField,
329:                     "{$alias}.locale" => $locale
330:                 )
331:             );
332:         }
333:         return $query;
334:     }
335: 
336: /**
337:  * afterFind Callback
338:  *
339:  * @param Model $Model Model find was run on
340:  * @param array $results Array of model results.
341:  * @param bool $primary Did the find originate on $model.
342:  * @return array Modified results
343:  */
344:     public function afterFind(Model $Model, $results, $primary = false) {
345:         $Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
346: 
347:         $this->runtime[$Model->alias]['virtualFields'] = array();
348:         if (!empty($this->runtime[$Model->alias]['restoreFields'])) {
349:             $this->runtime[$Model->alias]['fields'] = $this->runtime[$Model->alias]['restoreFields'];
350:             unset($this->runtime[$Model->alias]['restoreFields']);
351:         }
352: 
353:         $locale = $this->_getLocale($Model);
354: 
355:         if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
356:             return $results;
357:         }
358:         $beforeFind = $this->runtime[$Model->alias]['beforeFind'];
359: 
360:         foreach ($results as $key => &$row) {
361:             $results[$key][$Model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
362:             foreach ($beforeFind as $_f => $field) {
363:                 $aliasField = is_numeric($_f) ? $field : $_f;
364:                 $aliasVirtual = "i18n_{$field}";
365:                 if (is_array($locale)) {
366:                     foreach ($locale as $_locale) {
367:                         $aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
368:                         if (!isset($row[$Model->alias][$aliasField]) && !empty($row[$Model->alias][$aliasVirtualLocale])) {
369:                             $row[$Model->alias][$aliasField] = $row[$Model->alias][$aliasVirtualLocale];
370:                             $row[$Model->alias]['locale'] = $_locale;
371:                         }
372:                         unset($row[$Model->alias][$aliasVirtualLocale]);
373:                     }
374: 
375:                     if (!isset($row[$Model->alias][$aliasField])) {
376:                         $row[$Model->alias][$aliasField] = '';
377:                     }
378:                 } else {
379:                     $value = '';
380:                     if (isset($row[$Model->alias][$aliasVirtual])) {
381:                         $value = $row[$Model->alias][$aliasVirtual];
382:                     }
383:                     $row[$Model->alias][$aliasField] = $value;
384:                     unset($row[$Model->alias][$aliasVirtual]);
385:                 }
386:             }
387:         }
388:         return $results;
389:     }
390: 
391: /**
392:  * beforeValidate Callback
393:  *
394:  * @param Model $Model Model invalidFields was called on.
395:  * @param array $options Options passed from Model::save().
396:  * @return bool
397:  * @see Model::save()
398:  */
399:     public function beforeValidate(Model $Model, $options = array()) {
400:         unset($this->runtime[$Model->alias]['beforeSave']);
401:         $this->_setRuntimeData($Model);
402:         return true;
403:     }
404: 
405: /**
406:  * beforeSave callback.
407:  *
408:  * Copies data into the runtime property when `$options['validate']` is
409:  * disabled. Or the runtime data hasn't been set yet.
410:  *
411:  * @param Model $Model Model save was called on.
412:  * @param array $options Options passed from Model::save().
413:  * @return bool true.
414:  * @see Model::save()
415:  */
416:     public function beforeSave(Model $Model, $options = array()) {
417:         if (isset($options['validate']) && !$options['validate']) {
418:             unset($this->runtime[$Model->alias]['beforeSave']);
419:         }
420:         if (isset($this->runtime[$Model->alias]['beforeSave'])) {
421:             return true;
422:         }
423:         $this->_setRuntimeData($Model);
424:         return true;
425:     }
426: 
427: /**
428:  * Sets the runtime data.
429:  *
430:  * Used from beforeValidate() and beforeSave() for compatibility issues,
431:  * and to allow translations to be persisted even when validation
432:  * is disabled.
433:  *
434:  * @param Model $Model Model using this behavior.
435:  * @return bool true.
436:  */
437:     protected function _setRuntimeData(Model $Model) {
438:         $locale = $this->_getLocale($Model);
439:         if (empty($locale)) {
440:             return true;
441:         }
442:         $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
443:         $tempData = array();
444: 
445:         foreach ($fields as $key => $value) {
446:             $field = (is_numeric($key)) ? $value : $key;
447: 
448:             if (isset($Model->data[$Model->alias][$field])) {
449:                 $tempData[$field] = $Model->data[$Model->alias][$field];
450:                 if (is_array($Model->data[$Model->alias][$field])) {
451:                     if (is_string($locale) && !empty($Model->data[$Model->alias][$field][$locale])) {
452:                         $Model->data[$Model->alias][$field] = $Model->data[$Model->alias][$field][$locale];
453:                     } else {
454:                         $values = array_values($Model->data[$Model->alias][$field]);
455:                         $Model->data[$Model->alias][$field] = $values[0];
456:                     }
457:                 }
458:             }
459:         }
460:         $this->runtime[$Model->alias]['beforeSave'] = $tempData;
461:     }
462: 
463: /**
464:  * Restores model data to the original data.
465:  * This solves issues with saveAssociated and validate = first.
466:  *
467:  * @param Model $Model Model using this behavior.
468:  * @return bool true.
469:  */
470:     public function afterValidate(Model $Model) {
471:         $Model->data[$Model->alias] = array_merge(
472:             $Model->data[$Model->alias],
473:             $this->runtime[$Model->alias]['beforeSave']
474:         );
475:         return true;
476:     }
477: 
478: /**
479:  * afterSave Callback
480:  *
481:  * @param Model $Model Model the callback is called on
482:  * @param bool $created Whether or not the save created a record.
483:  * @param array $options Options passed from Model::save().
484:  * @return bool true.
485:  */
486:     public function afterSave(Model $Model, $created, $options = array()) {
487:         if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
488:             return true;
489:         }
490:         if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
491:             $tempData = $this->runtime[$Model->alias]['beforeValidate'];
492:         } else {
493:             $tempData = $this->runtime[$Model->alias]['beforeSave'];
494:         }
495: 
496:         unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
497:         $conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
498:         $RuntimeModel = $this->translateModel($Model);
499: 
500:         if ($created) {
501:             $tempData = $this->_prepareTranslations($Model, $tempData);
502:         }
503:         $locale = $this->_getLocale($Model);
504:         $atomic = array();
505:         if (isset($options['atomic'])) {
506:             $atomic = array('atomic' => $options['atomic']);
507:         }
508: 
509:         foreach ($tempData as $field => $value) {
510:             unset($conditions['content']);
511:             $conditions['field'] = $field;
512:             if (is_array($value)) {
513:                 $conditions['locale'] = array_keys($value);
514:             } else {
515:                 $conditions['locale'] = $locale;
516:                 if (is_array($locale)) {
517:                     $value = array($locale[0] => $value);
518:                 } else {
519:                     $value = array($locale => $value);
520:                 }
521:             }
522:             $translations = $RuntimeModel->find('list', array(
523:                 'conditions' => $conditions,
524:                 'fields' => array(
525:                     $RuntimeModel->alias . '.locale',
526:                     $RuntimeModel->alias . '.id'
527:                 )
528:             ));
529:             foreach ($value as $_locale => $_value) {
530:                 $RuntimeModel->create();
531:                 $conditions['locale'] = $_locale;
532:                 $conditions['content'] = $_value;
533:                 if (array_key_exists($_locale, $translations)) {
534:                     $RuntimeModel->save(array(
535:                         $RuntimeModel->alias => array_merge(
536:                             $conditions, array('id' => $translations[$_locale])
537:                         ),
538:                         $atomic
539:                     ));
540:                 } else {
541:                     $RuntimeModel->save(array($RuntimeModel->alias => $conditions), $atomic);
542:                 }
543:             }
544:         }
545:     }
546: 
547: /**
548:  * Prepares the data to be saved for translated records.
549:  * Add blank fields, and populates data for multi-locale saves.
550:  *
551:  * @param Model $Model Model using this behavior
552:  * @param array $data The sparse data that was provided.
553:  * @return array The fully populated data to save.
554:  */
555:     protected function _prepareTranslations(Model $Model, $data) {
556:         $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
557:         $locales = array();
558:         foreach ($data as $key => $value) {
559:             if (is_array($value)) {
560:                 $locales = array_merge($locales, array_keys($value));
561:             }
562:         }
563:         $locales = array_unique($locales);
564:         $hasLocales = count($locales) > 0;
565: 
566:         foreach ($fields as $key => $field) {
567:             if (!is_numeric($key)) {
568:                 $field = $key;
569:             }
570:             if ($hasLocales && !isset($data[$field])) {
571:                 $data[$field] = array_fill_keys($locales, '');
572:             } elseif (!isset($data[$field])) {
573:                 $data[$field] = '';
574:             }
575:         }
576:         return $data;
577:     }
578: 
579: /**
580:  * afterDelete Callback
581:  *
582:  * @param Model $Model Model the callback was run on.
583:  * @return void
584:  */
585:     public function afterDelete(Model $Model) {
586:         $RuntimeModel = $this->translateModel($Model);
587:         $conditions = array('model' => $Model->name, 'foreign_key' => $Model->id);
588:         $RuntimeModel->deleteAll($conditions);
589:     }
590: 
591: /**
592:  * Get selected locale for model
593:  *
594:  * @param Model $Model Model the locale needs to be set/get on.
595:  * @return mixed string or false
596:  */
597:     protected function _getLocale(Model $Model) {
598:         if (!isset($Model->locale) || $Model->locale === null) {
599:             $I18n = I18n::getInstance();
600:             $I18n->l10n->get(Configure::read('Config.language'));
601:             $Model->locale = $I18n->l10n->locale;
602:         }
603: 
604:         return $Model->locale;
605:     }
606: 
607: /**
608:  * Get instance of model for translations.
609:  *
610:  * If the model has a translateModel property set, this will be used as the class
611:  * name to find/use. If no translateModel property is found 'I18nModel' will be used.
612:  *
613:  * @param Model $Model Model to get a translatemodel for.
614:  * @return Model
615:  */
616:     public function translateModel(Model $Model) {
617:         if (!isset($this->runtime[$Model->alias]['model'])) {
618:             if (!isset($Model->translateModel) || empty($Model->translateModel)) {
619:                 $className = 'I18nModel';
620:             } else {
621:                 $className = $Model->translateModel;
622:             }
623: 
624:             $this->runtime[$Model->alias]['model'] = ClassRegistry::init($className);
625:         }
626:         if (!empty($Model->translateTable) && $Model->translateTable !== $this->runtime[$Model->alias]['model']->useTable) {
627:             $this->runtime[$Model->alias]['model']->setSource($Model->translateTable);
628:         } elseif (empty($Model->translateTable) && empty($Model->translateModel)) {
629:             $this->runtime[$Model->alias]['model']->setSource('i18n');
630:         }
631:         return $this->runtime[$Model->alias]['model'];
632:     }
633: 
634: /**
635:  * Bind translation for fields, optionally with hasMany association for
636:  * fake field.
637:  *
638:  * *Note* You should avoid binding translations that overlap existing model properties.
639:  * This can cause un-expected and un-desirable behavior.
640:  *
641:  * @param Model $Model using this behavior of model
642:  * @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
643:  * @param bool $reset Leave true to have the fields only modified for the next operation.
644:  *   if false the field will be added for all future queries.
645:  * @return bool
646:  * @throws CakeException when attempting to bind a translating called name. This is not allowed
647:  *   as it shadows Model::$name.
648:  */
649:     public function bindTranslation(Model $Model, $fields, $reset = true) {
650:         if (is_string($fields)) {
651:             $fields = array($fields);
652:         }
653:         $associations = array();
654:         $RuntimeModel = $this->translateModel($Model);
655:         $default = array(
656:             'className' => $RuntimeModel->alias,
657:             'foreignKey' => 'foreign_key',
658:             'order' => 'id'
659:         );
660: 
661:         foreach ($fields as $key => $value) {
662:             if (is_numeric($key)) {
663:                 $field = $value;
664:                 $association = null;
665:             } else {
666:                 $field = $key;
667:                 $association = $value;
668:             }
669:             if ($association === 'name') {
670:                 throw new CakeException(
671:                     __d('cake_dev', 'You cannot bind a translation named "name".')
672:                 );
673:             }
674:             $this->_removeField($Model, $field);
675: 
676:             if ($association === null) {
677:                 if ($reset) {
678:                     $this->runtime[$Model->alias]['fields'][] = $field;
679:                 } else {
680:                     $this->settings[$Model->alias][] = $field;
681:                 }
682:             } else {
683:                 if ($reset) {
684:                     $this->runtime[$Model->alias]['fields'][$field] = $association;
685:                     $this->runtime[$Model->alias]['restoreFields'][] = $field;
686:                 } else {
687:                     $this->settings[$Model->alias][$field] = $association;
688:                 }
689: 
690:                 foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
691:                     if (isset($Model->{$type}[$association]) || isset($Model->__backAssociation[$type][$association])) {
692:                         trigger_error(
693:                             __d('cake_dev', 'Association %s is already bound to model %s', $association, $Model->alias),
694:                             E_USER_ERROR
695:                         );
696:                         return false;
697:                     }
698:                 }
699:                 $associations[$association] = array_merge($default, array('conditions' => array(
700:                     'model' => $Model->name,
701:                     $RuntimeModel->displayField => $field
702:                 )));
703:             }
704:         }
705: 
706:         if (!empty($associations)) {
707:             $Model->bindModel(array('hasMany' => $associations), $reset);
708:         }
709:         return true;
710:     }
711: 
712: /**
713:  * Update runtime setting for a given field.
714:  *
715:  * @param Model $Model Model using this behavior
716:  * @param string $field The field to update.
717:  * @return void
718:  */
719:     protected function _removeField(Model $Model, $field) {
720:         if (array_key_exists($field, $this->settings[$Model->alias])) {
721:             unset($this->settings[$Model->alias][$field]);
722:         } elseif (in_array($field, $this->settings[$Model->alias])) {
723:             $this->settings[$Model->alias] = array_merge(array_diff($this->settings[$Model->alias], array($field)));
724:         }
725: 
726:         if (array_key_exists($field, $this->runtime[$Model->alias]['fields'])) {
727:             unset($this->runtime[$Model->alias]['fields'][$field]);
728:         } elseif (in_array($field, $this->runtime[$Model->alias]['fields'])) {
729:             $this->runtime[$Model->alias]['fields'] = array_merge(array_diff($this->runtime[$Model->alias]['fields'], array($field)));
730:         }
731:     }
732: 
733: /**
734:  * Unbind translation for fields, optionally unbinds hasMany association for
735:  * fake field
736:  *
737:  * @param Model $Model using this behavior of model
738:  * @param string|array $fields string with field, or array(field1, field2=>AssocName, field3), or null for
739:  *    unbind all original translations
740:  * @return bool
741:  */
742:     public function unbindTranslation(Model $Model, $fields = null) {
743:         if (empty($fields) && empty($this->settings[$Model->alias])) {
744:             return false;
745:         }
746:         if (empty($fields)) {
747:             return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
748:         }
749: 
750:         if (is_string($fields)) {
751:             $fields = array($fields);
752:         }
753:         $associations = array();
754: 
755:         foreach ($fields as $key => $value) {
756:             if (is_numeric($key)) {
757:                 $field = $value;
758:                 $association = null;
759:             } else {
760:                 $field = $key;
761:                 $association = $value;
762:             }
763: 
764:             $this->_removeField($Model, $field);
765: 
766:             if ($association !== null && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
767:                 $associations[] = $association;
768:             }
769:         }
770: 
771:         if (!empty($associations)) {
772:             $Model->unbindModel(array('hasMany' => $associations), false);
773:         }
774:         return true;
775:     }
776: 
777: }
778: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs