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 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 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

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * Short description for file.
  5:  *
  6:  * Long description for file
  7:  *
  8:  * PHP versions 4 and 5
  9:  *
 10:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 12:  *
 13:  * Licensed under The MIT License
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP(tm) Project
 18:  * @package       cake
 19:  * @subpackage    cake.cake.libs.model.behaviors
 20:  * @since         CakePHP(tm) v 1.2.0.4525
 21:  * @version       $Revision$
 22:  * @modifiedby    $LastChangedBy$
 23:  * @lastmodified  $Date$
 24:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 25:  */
 26: /**
 27:  * Short description for file.
 28:  *
 29:  * Long description for file
 30:  *
 31:  * @package       cake
 32:  * @subpackage    cake.cake.libs.model.behaviors
 33:  */
 34: class TranslateBehavior extends ModelBehavior {
 35: /**
 36:  * Used for runtime configuration of model
 37:  */
 38:     var $runtime = array();
 39: /**
 40:  * Callback
 41:  *
 42:  * $config for TranslateBehavior should be
 43:  * array( 'fields' => array('field_one',
 44:  * 'field_two' => 'FieldAssoc', 'field_three'))
 45:  *
 46:  * With above example only one permanent hasMany will be joined (for field_two
 47:  * as FieldAssoc)
 48:  *
 49:  * $config could be empty - and translations configured dynamically by
 50:  * bindTranslation() method
 51:  *
 52:  * @param array $config
 53:  * @return mixed
 54:  * @access public
 55:  */
 56:     function setup(&$model, $config = array()) {
 57:         $db =& ConnectionManager::getDataSource($model->useDbConfig);
 58:         if (!$db->connected) {
 59:             trigger_error(
 60:                 sprintf(__('Datasource %s for TranslateBehavior of model %s is not connected', true), $model->useDbConfig, $model->alias),
 61:                 E_USER_ERROR
 62:             );
 63:             return false;
 64:         }
 65: 
 66:         $this->settings[$model->alias] = array();
 67:         $this->runtime[$model->alias] = array('fields' => array());
 68:         $this->translateModel($model);
 69:         return $this->bindTranslation($model, $config, false);
 70:     }
 71: /**
 72:  * Callback
 73:  *
 74:  * @return void
 75:  * @access public
 76:  */
 77:     function cleanup(&$model) {
 78:         $this->unbindTranslation($model);
 79:         unset($this->settings[$model->alias]);
 80:         unset($this->runtime[$model->alias]);
 81:     }
 82: /**
 83:  * beforeFind Callback
 84:  *
 85:  * @param array $query
 86:  * @return array Modified query
 87:  * @access public
 88:  */
 89:     function beforeFind(&$model, $query) {
 90:         $locale = $this->_getLocale($model);
 91:         if (empty($locale)) {
 92:             return $query;
 93:         }
 94:         $db =& ConnectionManager::getDataSource($model->useDbConfig);
 95:         $RuntimeModel =& $this->translateModel($model);
 96:         if (!empty($RuntimeModel->tablePrefix)) {
 97:             $tablePrefix = $RuntimeModel->tablePrefix;
 98:         } else {
 99:             $tablePrefix = $db->config['prefix'];
100:         }
101: 
102:         if (is_string($query['fields']) && 'COUNT(*) AS '.$db->name('count') == $query['fields']) {
103:             $query['fields'] = 'COUNT(DISTINCT('.$db->name($model->alias . '.' . $model->primaryKey) . ')) ' . $db->alias . 'count';
104:             $query['joins'][] = array(
105:                 'type' => 'INNER',
106:                 'alias' => $RuntimeModel->alias,
107:                 'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
108:                 'conditions' => array(
109:                     $model->alias . '.' . $model->primaryKey => $db->identifier($RuntimeModel->alias.'.foreign_key'),
110:                     $RuntimeModel->alias.'.model' => $model->name,
111:                     $RuntimeModel->alias.'.locale' => $locale
112:                 )
113:             );
114:             return $query;
115:         }
116:         $autoFields = false;
117: 
118:         if (empty($query['fields'])) {
119:             $query['fields'] = array($model->alias.'.*');
120: 
121:             $recursive = $model->recursive;
122:             if (isset($query['recursive'])) {
123:                 $recursive = $query['recursive'];
124:             }
125: 
126:             if ($recursive >= 0) {
127:                 foreach (array('hasOne', 'belongsTo') as $type) {
128:                     foreach ($model->{$type} as $key => $value) {
129: 
130:                         if (empty($value['fields'])) {
131:                             $query['fields'][] = $key.'.*';
132:                         } else {
133:                             foreach ($value['fields'] as $field) {
134:                                 $query['fields'][] = $key.'.'.$field;
135:                             }
136:                         }
137:                     }
138:                 }
139:             }
140:             $autoFields = true;
141:         }
142:         $fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
143:         $addFields = array();
144:         if (is_array($query['fields'])) {
145:             foreach ($fields as $key => $value) {
146:                 $field = (is_numeric($key)) ? $value : $key;
147: 
148:                 if (in_array($model->alias.'.*', $query['fields']) || $autoFields || in_array($model->alias.'.'.$field, $query['fields']) || in_array($field, $query['fields'])) {
149:                     $addFields[] = $field;
150:                 }
151:             }
152:         }
153: 
154:         if ($addFields) {
155:             foreach ($addFields as $field) {
156:                 foreach (array($field, $model->alias.'.'.$field) as $_field) {
157:                     $key = array_search($_field, $query['fields']);
158: 
159:                     if ($key !== false) {
160:                         unset($query['fields'][$key]);
161:                     }
162:                 }
163: 
164:                 if (is_array($locale)) {
165:                     foreach ($locale as $_locale) {
166:                         $query['fields'][] = 'I18n__'.$field.'__'.$_locale.'.content';
167:                         $query['joins'][] = array(
168:                             'type' => 'LEFT',
169:                             'alias' => 'I18n__'.$field.'__'.$_locale,
170:                             'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
171:                             'conditions' => array(
172:                                 $model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}__{$_locale}.foreign_key"),
173:                                 'I18n__'.$field.'__'.$_locale.'.model' => $model->name,
174:                                 'I18n__'.$field.'__'.$_locale.'.'.$RuntimeModel->displayField => $field,
175:                                 'I18n__'.$field.'__'.$_locale.'.locale' => $_locale
176:                             )
177:                         );
178:                     }
179:                 } else {
180:                     $query['fields'][] = 'I18n__'.$field.'.content';
181:                     $query['joins'][] = array(
182:                         'type' => 'LEFT',
183:                         'alias' => 'I18n__'.$field,
184:                         'table' => $db->name($tablePrefix . $RuntimeModel->useTable),
185:                         'conditions' => array(
186:                             $model->alias . '.' . $model->primaryKey => $db->identifier("I18n__{$field}.foreign_key"),
187:                             'I18n__'.$field.'.model' => $model->name,
188:                             'I18n__'.$field.'.'.$RuntimeModel->displayField => $field
189:                         )
190:                     );
191: 
192:                     if (is_string($query['conditions'])) {
193:                         $query['conditions'] = $db->conditions($query['conditions'], true, false, $model) . ' AND '.$db->name('I18n__'.$field.'.locale').' = \''.$locale.'\'';
194:                     } else {
195:                         $query['conditions'][$db->name("I18n__{$field}.locale")] = $locale;
196:                     }
197:                 }
198:             }
199:         }
200:         if (is_array($query['fields'])) {
201:             $query['fields'] = array_merge($query['fields']);
202:         }
203:         $this->runtime[$model->alias]['beforeFind'] = $addFields;
204:         return $query;
205:     }
206: /**
207:  * afterFind Callback
208:  *
209:  * @param array $results
210:  * @param boolean $primary
211:  * @return array Modified results
212:  * @access public
213:  */
214:     function afterFind(&$model, $results, $primary) {
215:         $this->runtime[$model->alias]['fields'] = array();
216:         $locale = $this->_getLocale($model);
217: 
218:         if (empty($locale) || empty($results) || empty($this->runtime[$model->alias]['beforeFind'])) {
219:             return $results;
220:         }
221:         $beforeFind = $this->runtime[$model->alias]['beforeFind'];
222: 
223:         foreach ($results as $key => $row) {
224:             $results[$key][$model->alias]['locale'] = (is_array($locale)) ? @$locale[0] : $locale;
225: 
226:             foreach ($beforeFind as $field) {
227:                 if (is_array($locale)) {
228:                     foreach ($locale as $_locale) {
229:                         if (!isset($results[$key][$model->alias][$field]) && !empty($results[$key]['I18n__'.$field.'__'.$_locale]['content'])) {
230:                             $results[$key][$model->alias][$field] = $results[$key]['I18n__'.$field.'__'.$_locale]['content'];
231:                         }
232:                         unset($results[$key]['I18n__'.$field.'__'.$_locale]);
233:                     }
234: 
235:                     if (!isset($results[$key][$model->alias][$field])) {
236:                         $results[$key][$model->alias][$field] = '';
237:                     }
238:                 } else {
239:                     $value = '';
240:                     if (!empty($results[$key]['I18n__'.$field]['content'])) {
241:                         $value = $results[$key]['I18n__'.$field]['content'];
242:                     }
243:                     $results[$key][$model->alias][$field] = $value;
244:                     unset($results[$key]['I18n__'.$field]);
245:                 }
246:             }
247:         }
248:         return $results;
249:     }
250: /**
251:  * beforeValidate Callback
252:  *
253:  * @return boolean
254:  * @access public
255:  */
256:     function beforeValidate(&$model) {
257:         $locale = $this->_getLocale($model);
258:         if (empty($locale)) {
259:             return true;
260:         }
261:         $fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
262:         $tempData = array();
263: 
264:         foreach ($fields as $key => $value) {
265:             $field = (is_numeric($key)) ? $value : $key;
266: 
267:             if (isset($model->data[$model->alias][$field])) {
268:                 $tempData[$field] = $model->data[$model->alias][$field];
269:                 if (is_array($model->data[$model->alias][$field])) {
270:                     if (is_string($locale) && !empty($model->data[$model->alias][$field][$locale])) {
271:                         $model->data[$model->alias][$field] = $model->data[$model->alias][$field][$locale];
272:                     } else {
273:                         $values = array_values($model->data[$model->alias][$field]);
274:                         $model->data[$model->alias][$field] = $values[0];
275:                     }
276:                 }
277:             }
278:         }
279:         $this->runtime[$model->alias]['beforeSave'] = $tempData;
280:         return true;
281:     }
282: /**
283:  * afterSave Callback
284:  *
285:  * @param boolean $created
286:  * @return void
287:  * @access public
288:  */
289:     function afterSave(&$model, $created) {
290:         if (!isset($this->runtime[$model->alias]['beforeSave'])) {
291:             return true;
292:         }
293:         $locale = $this->_getLocale($model);
294:         $tempData = $this->runtime[$model->alias]['beforeSave'];
295:         unset($this->runtime[$model->alias]['beforeSave']);
296:         $conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
297:         $RuntimeModel =& $this->translateModel($model);
298: 
299:         foreach ($tempData as $field => $value) {
300:             unset($conditions['content']);
301:             $conditions['field'] = $field;
302:             if (is_array($value)) {
303:                 $conditions['locale'] = array_keys($value);
304:             } else {
305:                 $conditions['locale'] = $locale;
306:                 if (is_array($locale)) {
307:                     $value = array($locale[0] => $value);
308:                 } else {
309:                     $value = array($locale => $value);
310:                 }
311:             }
312:             $translations = $RuntimeModel->find('list', array('conditions' => $conditions, 'fields' => array($RuntimeModel->alias . '.locale', $RuntimeModel->alias . '.id')));
313:             foreach ($value as $_locale => $_value) {
314:                 $RuntimeModel->create();
315:                 $conditions['locale'] = $_locale;
316:                 $conditions['content'] = $_value;
317:                 if (array_key_exists($_locale, $translations)) {
318:                     $RuntimeModel->save(array($RuntimeModel->alias => array_merge($conditions, array('id' => $translations[$_locale]))));
319:                 } else {
320:                     $RuntimeModel->save(array($RuntimeModel->alias => $conditions));
321:                 }
322:             }
323:         }
324:     }
325: /**
326:  * afterDelete Callback
327:  *
328:  * @return void
329:  * @access public
330:  */
331:     function afterDelete(&$model) {
332:         $RuntimeModel =& $this->translateModel($model);
333:         $conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
334:         $RuntimeModel->deleteAll($conditions);
335:     }
336: /**
337:  * Get selected locale for model
338:  *
339:  * @return mixed string or false
340:  * @access protected
341:  */
342:     function _getLocale(&$model) {
343:         if (!isset($model->locale) || is_null($model->locale)) {
344:             if (!class_exists('I18n')) {
345:                 App::import('Core', 'i18n');
346:             }
347:             $I18n =& I18n::getInstance();
348:             $I18n->l10n->get(Configure::read('Config.language'));
349:             $model->locale = $I18n->l10n->locale;
350:         }
351: 
352:         return $model->locale;
353:     }
354: /**
355:  * Get instance of model for translations
356:  *
357:  * @return object
358:  * @access public
359:  */
360:     function &translateModel(&$model) {
361:         if (!isset($this->runtime[$model->alias]['model'])) {
362:             if (!isset($model->translateModel) || empty($model->translateModel)) {
363:                 $className = 'I18nModel';
364:             } else {
365:                 $className = $model->translateModel;
366:             }
367: 
368:             if (PHP5) {
369:                 $this->runtime[$model->alias]['model'] = ClassRegistry::init($className, 'Model');
370:             } else {
371:                 $this->runtime[$model->alias]['model'] =& ClassRegistry::init($className, 'Model');
372:             }
373:         }
374:         if (!empty($model->translateTable) && $model->translateTable !== $this->runtime[$model->alias]['model']->useTable) {
375:             $this->runtime[$model->alias]['model']->setSource($model->translateTable);
376:         } elseif (empty($model->translateTable) && empty($model->translateModel)) {
377:             $this->runtime[$model->alias]['model']->setSource('i18n');
378:         }
379:         $model =& $this->runtime[$model->alias]['model'];
380:         return $model;
381:     }
382: /**
383:  * Bind translation for fields, optionally with hasMany association for
384:  * fake field
385:  *
386:  * @param object instance of model
387:  * @param mixed string with field or array(field1, field2=>AssocName, field3)
388:  * @param boolean $reset
389:  * @return bool
390:  */
391:     function bindTranslation(&$model, $fields, $reset = true) {
392:         if (is_string($fields)) {
393:             $fields = array($fields);
394:         }
395:         $associations = array();
396:         $RuntimeModel =& $this->translateModel($model);
397:         $default = array('className' => $RuntimeModel->alias, 'foreignKey' => 'foreign_key');
398: 
399:         foreach ($fields as $key => $value) {
400:             if (is_numeric($key)) {
401:                 $field = $value;
402:                 $association = null;
403:             } else {
404:                 $field = $key;
405:                 $association = $value;
406:             }
407: 
408:             if (array_key_exists($field, $this->settings[$model->alias])) {
409:                 unset($this->settings[$model->alias][$field]);
410:             } elseif (in_array($field, $this->settings[$model->alias])) {
411:                 $this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
412:             }
413: 
414:             if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
415:                 unset($this->runtime[$model->alias]['fields'][$field]);
416:             } elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
417:                 $this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
418:             }
419: 
420:             if (is_null($association)) {
421:                 if ($reset) {
422:                     $this->runtime[$model->alias]['fields'][] = $field;
423:                 } else {
424:                     $this->settings[$model->alias][] = $field;
425:                 }
426:             } else {
427:                 if ($reset) {
428:                     $this->runtime[$model->alias]['fields'][$field] = $association;
429:                 } else {
430:                     $this->settings[$model->alias][$field] = $association;
431:                 }
432: 
433:                 foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
434:                     if (isset($model->{$type}[$association]) || isset($model->__backAssociation[$type][$association])) {
435:                         trigger_error(
436:                             sprintf(__('Association %s is already binded to model %s', true), $association, $model->alias),
437:                             E_USER_ERROR
438:                         );
439:                         return false;
440:                     }
441:                 }
442:                 $associations[$association] = array_merge($default, array('conditions' => array(
443:                     'model' => $model->alias,
444:                     $RuntimeModel->displayField => $field
445:                 )));
446:             }
447:         }
448: 
449:         if (!empty($associations)) {
450:             $model->bindModel(array('hasMany' => $associations), $reset);
451:         }
452:         return true;
453:     }
454: /**
455:  * Unbind translation for fields, optionally unbinds hasMany association for
456:  * fake field
457:  *
458:  * @param object instance of model
459:  * @param mixed string with field, or array(field1, field2=>AssocName, field3), or null for unbind all original translations
460:  * @return bool
461:  */
462:     function unbindTranslation(&$model, $fields = null) {
463:         if (empty($fields)) {
464:             return $this->unbindTranslation($model, $this->settings[$model->alias]);
465:         }
466: 
467:         if (is_string($fields)) {
468:             $fields = array($fields);
469:         }
470:         $RuntimeModel =& $this->translateModel($model);
471:         $associations = array();
472: 
473:         foreach ($fields as $key => $value) {
474:             if (is_numeric($key)) {
475:                 $field = $value;
476:                 $association = null;
477:             } else {
478:                 $field = $key;
479:                 $association = $value;
480:             }
481: 
482:             if (array_key_exists($field, $this->settings[$model->alias])) {
483:                 unset($this->settings[$model->alias][$field]);
484:             } elseif (in_array($field, $this->settings[$model->alias])) {
485:                 $this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
486:             }
487: 
488:             if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
489:                 unset($this->runtime[$model->alias]['fields'][$field]);
490:             } elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
491:                 $this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
492:             }
493: 
494:             if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
495:                 $associations[] = $association;
496:             }
497:         }
498: 
499:         if (!empty($associations)) {
500:             $model->unbindModel(array('hasMany' => $associations), false);
501:         }
502:         return true;
503:     }
504: }
505: if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
506: /**
507:  * @package       cake
508:  * @subpackage    cake.cake.libs.model.behaviors
509:  */
510:     class I18nModel extends AppModel {
511:         var $name = 'I18nModel';
512:         var $useTable = 'i18n';
513:         var $displayField = 'field';
514:     }
515: }
516: ?>
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