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.2 API

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

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

Functions

  • __
  • __c
  • __d
  • __dc
  • __dcn
  • __dn
  • __n
  • am
  • cache
  • clearCache
  • config
  • convertSlash
  • debug
  • env
  • fileExistsInPath
  • h
  • LogError
  • pluginSplit
  • pr
  • sortByKey
  • stripslashes_deep
  1: <?php
  2: /**
  3:  * Basic Cake functionality.
  4:  *
  5:  * Core functions for including other source files, loading models and so forth.
  6:  *
  7:  * PHP 5
  8:  *
  9:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 10:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * Redistributions of files must retain the above copyright notice.
 14:  *
 15:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://cakephp.org CakePHP(tm) Project
 17:  * @package       Cake
 18:  * @since         CakePHP(tm) v 0.2.9
 19:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 20:  */
 21: 
 22: /**
 23:  * Basic defines for timing functions.
 24:  */
 25:     define('SECOND', 1);
 26:     define('MINUTE', 60);
 27:     define('HOUR', 3600);
 28:     define('DAY', 86400);
 29:     define('WEEK', 604800);
 30:     define('MONTH', 2592000);
 31:     define('YEAR', 31536000);
 32: 
 33: /**
 34:  * Loads configuration files. Receives a set of configuration files
 35:  * to load.
 36:  * Example:
 37:  *
 38:  * `config('config1', 'config2');`
 39:  *
 40:  * @return boolean Success
 41:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#config
 42:  */
 43: function config() {
 44:     $args = func_get_args();
 45:     foreach ($args as $arg) {
 46:         if (file_exists(APP . 'Config' . DS . $arg . '.php')) {
 47:             include_once APP . 'Config' . DS . $arg . '.php';
 48: 
 49:             if (count($args) == 1) {
 50:                 return true;
 51:             }
 52:         } else {
 53:             if (count($args) == 1) {
 54:                 return false;
 55:             }
 56:         }
 57:     }
 58:     return true;
 59: }
 60: 
 61: /**
 62:  * Prints out debug information about given variable.
 63:  *
 64:  * Only runs if debug level is greater than zero.
 65:  *
 66:  * @param boolean $var Variable to show debug information for.
 67:  * @param boolean $showHtml If set to true, the method prints the debug data in a browser-friendly way.
 68:  * @param boolean $showFrom If set to true, the method prints from where the function was called.
 69:  * @link http://book.cakephp.org/2.0/en/development/debugging.html#basic-debugging
 70:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug
 71:  */
 72: function debug($var = false, $showHtml = null, $showFrom = true) {
 73:     if (Configure::read('debug') > 0) {
 74:         App::uses('Debugger', 'Utility');
 75:         $file = '';
 76:         $line = '';
 77:         $lineInfo = '';
 78:         if ($showFrom) {
 79:             $trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
 80:             $file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
 81:             $line = $trace[0]['line'];
 82:         }
 83:         $html = <<<HTML
 84: <div class="cake-debug-output">
 85: %s
 86: <pre class="cake-debug">
 87: %s
 88: </pre>
 89: </div>
 90: HTML;
 91:         $text = <<<TEXT
 92: %s
 93: ########## DEBUG ##########
 94: %s
 95: ###########################
 96: TEXT;
 97:         $template = $html;
 98:         if (php_sapi_name() == 'cli' || $showHtml === false) {
 99:             $template = $text;
100:             if ($showFrom) {
101:                 $lineInfo = sprintf('%s (line %s)', $file, $line);
102:             }
103:         }
104:         if ($showHtml === null && $template !== $text) {
105:             $showHtml = true;
106:         }
107:         $var = Debugger::exportVar($var, 25);
108:         if ($showHtml) {
109:             $template = $html;
110:             $var = h($var);
111:             if ($showFrom) {
112:                 $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
113:             }
114:         }
115:         printf($template, $lineInfo, $var);
116:     }
117: }
118: 
119: if (!function_exists('sortByKey')) {
120: 
121: /**
122:  * Sorts given $array by key $sortby.
123:  *
124:  * @param array $array Array to sort
125:  * @param string $sortby Sort by this key
126:  * @param string $order  Sort order asc/desc (ascending or descending).
127:  * @param integer $type Type of sorting to perform
128:  * @return mixed Sorted array
129:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#sortByKey
130:  */
131:     function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
132:         if (!is_array($array)) {
133:             return null;
134:         }
135: 
136:         foreach ($array as $key => $val) {
137:             $sa[$key] = $val[$sortby];
138:         }
139: 
140:         if ($order == 'asc') {
141:             asort($sa, $type);
142:         } else {
143:             arsort($sa, $type);
144:         }
145: 
146:         foreach ($sa as $key => $val) {
147:             $out[] = $array[$key];
148:         }
149:         return $out;
150:     }
151: 
152: }
153: 
154: /**
155:  * Convenience method for htmlspecialchars.
156:  *
157:  * @param string|array|object $text Text to wrap through htmlspecialchars.  Also works with arrays, and objects.
158:  *    Arrays will be mapped and have all their elements escaped.  Objects will be string cast if they
159:  *    implement a `__toString` method.  Otherwise the class name will be used.
160:  * @param boolean $double Encode existing html entities
161:  * @param string $charset Character set to use when escaping.  Defaults to config value in 'App.encoding' or 'UTF-8'
162:  * @return string Wrapped text
163:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#h
164:  */
165: function h($text, $double = true, $charset = null) {
166:     if (is_array($text)) {
167:         $texts = array();
168:         foreach ($text as $k => $t) {
169:             $texts[$k] = h($t, $double, $charset);
170:         }
171:         return $texts;
172:     } elseif (is_object($text)) {
173:         if (method_exists($text, '__toString')) {
174:             $text = (string)$text;
175:         } else {
176:             $text = '(object)' . get_class($text);
177:         }
178:     } elseif (is_bool($text)) {
179:         return $text;
180:     }
181: 
182:     static $defaultCharset = false;
183:     if ($defaultCharset === false) {
184:         $defaultCharset = Configure::read('App.encoding');
185:         if ($defaultCharset === null) {
186:             $defaultCharset = 'UTF-8';
187:         }
188:     }
189:     if (is_string($double)) {
190:         $charset = $double;
191:     }
192:     return htmlspecialchars($text, ENT_QUOTES, ($charset) ? $charset : $defaultCharset, $double);
193: }
194: 
195: /**
196:  * Splits a dot syntax plugin name into its plugin and classname.
197:  * If $name does not have a dot, then index 0 will be null.
198:  *
199:  * Commonly used like `list($plugin, $name) = pluginSplit($name);`
200:  *
201:  * @param string $name The name you want to plugin split.
202:  * @param boolean $dotAppend Set to true if you want the plugin to have a '.' appended to it.
203:  * @param string $plugin Optional default plugin to use if no plugin is found. Defaults to null.
204:  * @return array Array with 2 indexes.  0 => plugin name, 1 => classname
205:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pluginSplit
206:  */
207: function pluginSplit($name, $dotAppend = false, $plugin = null) {
208:     if (strpos($name, '.') !== false) {
209:         $parts = explode('.', $name, 2);
210:         if ($dotAppend) {
211:             $parts[0] .= '.';
212:         }
213:         return $parts;
214:     }
215:     return array($plugin, $name);
216: }
217: 
218: /**
219:  * Print_r convenience function, which prints out <PRE> tags around
220:  * the output of given array. Similar to debug().
221:  *
222:  * @see debug()
223:  * @param array $var Variable to print out
224:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#pr
225:  */
226: function pr($var) {
227:     if (Configure::read('debug') > 0) {
228:         echo '<pre>';
229:         print_r($var);
230:         echo '</pre>';
231:     }
232: }
233: 
234: /**
235:  * Merge a group of arrays
236:  *
237:  * @param array First array
238:  * @param array Second array
239:  * @param array Third array
240:  * @param array Etc...
241:  * @return array All array parameters merged into one
242:  * @link http://book.cakephp.org/2.0/en/development/debugging.html#am
243:  */
244: function am() {
245:     $r = array();
246:     $args = func_get_args();
247:     foreach ($args as $a) {
248:         if (!is_array($a)) {
249:             $a = array($a);
250:         }
251:         $r = array_merge($r, $a);
252:     }
253:     return $r;
254: }
255: 
256: /**
257:  * Gets an environment variable from available sources, and provides emulation
258:  * for unsupported or inconsistent environment variables (i.e. DOCUMENT_ROOT on
259:  * IIS, or SCRIPT_NAME in CGI mode).  Also exposes some additional custom
260:  * environment information.
261:  *
262:  * @param  string $key Environment variable name.
263:  * @return string Environment variable setting.
264:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#env
265:  */
266: function env($key) {
267:     if ($key === 'HTTPS') {
268:         if (isset($_SERVER['HTTPS'])) {
269:             return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
270:         }
271:         return (strpos(env('SCRIPT_URI'), 'https://') === 0);
272:     }
273: 
274:     if ($key === 'SCRIPT_NAME') {
275:         if (env('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
276:             $key = 'SCRIPT_URL';
277:         }
278:     }
279: 
280:     $val = null;
281:     if (isset($_SERVER[$key])) {
282:         $val = $_SERVER[$key];
283:     } elseif (isset($_ENV[$key])) {
284:         $val = $_ENV[$key];
285:     } elseif (getenv($key) !== false) {
286:         $val = getenv($key);
287:     }
288: 
289:     if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
290:         $addr = env('HTTP_PC_REMOTE_ADDR');
291:         if ($addr !== null) {
292:             $val = $addr;
293:         }
294:     }
295: 
296:     if ($val !== null) {
297:         return $val;
298:     }
299: 
300:     switch ($key) {
301:         case 'SCRIPT_FILENAME':
302:             if (defined('SERVER_IIS') && SERVER_IIS === true) {
303:                 return str_replace('\\\\', '\\', env('PATH_TRANSLATED'));
304:             }
305:             break;
306:         case 'DOCUMENT_ROOT':
307:             $name = env('SCRIPT_NAME');
308:             $filename = env('SCRIPT_FILENAME');
309:             $offset = 0;
310:             if (!strpos($name, '.php')) {
311:                 $offset = 4;
312:             }
313:             return substr($filename, 0, -(strlen($name) + $offset));
314:         case 'PHP_SELF':
315:             return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
316:         case 'CGI_MODE':
317:             return (PHP_SAPI === 'cgi');
318:         case 'HTTP_BASE':
319:             $host = env('HTTP_HOST');
320:             $parts = explode('.', $host);
321:             $count = count($parts);
322: 
323:             if ($count === 1) {
324:                 return '.' . $host;
325:             } elseif ($count === 2) {
326:                 return '.' . $host;
327:             } elseif ($count === 3) {
328:                 $gTLD = array(
329:                     'aero',
330:                     'asia',
331:                     'biz',
332:                     'cat',
333:                     'com',
334:                     'coop',
335:                     'edu',
336:                     'gov',
337:                     'info',
338:                     'int',
339:                     'jobs',
340:                     'mil',
341:                     'mobi',
342:                     'museum',
343:                     'name',
344:                     'net',
345:                     'org',
346:                     'pro',
347:                     'tel',
348:                     'travel',
349:                     'xxx'
350:                 );
351:                 if (in_array($parts[1], $gTLD)) {
352:                     return '.' . $host;
353:                 }
354:             }
355:             array_shift($parts);
356:             return '.' . implode('.', $parts);
357:     }
358:     return null;
359: }
360: 
361: /**
362:  * Reads/writes temporary data to cache files or session.
363:  *
364:  * @param  string $path File path within /tmp to save the file.
365:  * @param  mixed  $data The data to save to the temporary file.
366:  * @param  mixed  $expires A valid strtotime string when the data expires.
367:  * @param  string $target  The target of the cached data; either 'cache' or 'public'.
368:  * @return mixed  The contents of the temporary file.
369:  * @deprecated Please use Cache::write() instead
370:  */
371: function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
372:     if (Configure::read('Cache.disable')) {
373:         return null;
374:     }
375:     $now = time();
376: 
377:     if (!is_numeric($expires)) {
378:         $expires = strtotime($expires, $now);
379:     }
380: 
381:     switch (strtolower($target)) {
382:         case 'cache':
383:             $filename = CACHE . $path;
384:         break;
385:         case 'public':
386:             $filename = WWW_ROOT . $path;
387:         break;
388:         case 'tmp':
389:             $filename = TMP . $path;
390:         break;
391:     }
392:     $timediff = $expires - $now;
393:     $filetime = false;
394: 
395:     if (file_exists($filename)) {
396:         //@codingStandardsIgnoreStart
397:         $filetime = @filemtime($filename);
398:         //@codingStandardsIgnoreEnd
399:     }
400: 
401:     if ($data === null) {
402:         if (file_exists($filename) && $filetime !== false) {
403:             if ($filetime + $timediff < $now) {
404:                 //@codingStandardsIgnoreStart
405:                 @unlink($filename);
406:                 //@codingStandardsIgnoreEnd
407:             } else {
408:                 //@codingStandardsIgnoreStart
409:                 $data = @file_get_contents($filename);
410:                 //@codingStandardsIgnoreEnd
411:             }
412:         }
413:     } elseif (is_writable(dirname($filename))) {
414:         //@codingStandardsIgnoreStart
415:         @file_put_contents($filename, $data, LOCK_EX);
416:         //@codingStandardsIgnoreEnd
417:     }
418:     return $data;
419: }
420: 
421: /**
422:  * Used to delete files in the cache directories, or clear contents of cache directories
423:  *
424:  * @param string|array $params As String name to be searched for deletion, if name is a directory all files in
425:  *   directory will be deleted. If array, names to be searched for deletion. If clearCache() without params,
426:  *   all files in app/tmp/cache/views will be deleted
427:  * @param string $type Directory in tmp/cache defaults to view directory
428:  * @param string $ext The file extension you are deleting
429:  * @return true if files found and deleted false otherwise
430:  */
431: function clearCache($params = null, $type = 'views', $ext = '.php') {
432:     if (is_string($params) || $params === null) {
433:         $params = preg_replace('/\/\//', '/', $params);
434:         $cache = CACHE . $type . DS . $params;
435: 
436:         if (is_file($cache . $ext)) {
437:             //@codingStandardsIgnoreStart
438:             @unlink($cache . $ext);
439:             //@codingStandardsIgnoreEnd
440:             return true;
441:         } elseif (is_dir($cache)) {
442:             $files = glob($cache . '*');
443: 
444:             if ($files === false) {
445:                 return false;
446:             }
447: 
448:             foreach ($files as $file) {
449:                 if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
450:                     //@codingStandardsIgnoreStart
451:                     @unlink($file);
452:                     //@codingStandardsIgnoreEnd
453:                 }
454:             }
455:             return true;
456:         } else {
457:             $cache = array(
458:                 CACHE . $type . DS . '*' . $params . $ext,
459:                 CACHE . $type . DS . '*' . $params . '_*' . $ext
460:             );
461:             $files = array();
462:             while ($search = array_shift($cache)) {
463:                 $results = glob($search);
464:                 if ($results !== false) {
465:                     $files = array_merge($files, $results);
466:                 }
467:             }
468:             if (empty($files)) {
469:                 return false;
470:             }
471:             foreach ($files as $file) {
472:                 if (is_file($file) && strrpos($file, DS . 'empty') !== strlen($file) - 6) {
473:                     //@codingStandardsIgnoreStart
474:                     @unlink($file);
475:                     //@codingStandardsIgnoreEnd
476:                 }
477:             }
478:             return true;
479:         }
480:     } elseif (is_array($params)) {
481:         foreach ($params as $file) {
482:             clearCache($file, $type, $ext);
483:         }
484:         return true;
485:     }
486:     return false;
487: }
488: 
489: /**
490:  * Recursively strips slashes from all values in an array
491:  *
492:  * @param array $values Array of values to strip slashes
493:  * @return mixed What is returned from calling stripslashes
494:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#stripslashes_deep
495:  */
496: function stripslashes_deep($values) {
497:     if (is_array($values)) {
498:         foreach ($values as $key => $value) {
499:             $values[$key] = stripslashes_deep($value);
500:         }
501:     } else {
502:         $values = stripslashes($values);
503:     }
504:     return $values;
505: }
506: 
507: /**
508:  * Returns a translated string if one is found; Otherwise, the submitted message.
509:  *
510:  * @param string $singular Text to translate
511:  * @param mixed $args Array with arguments or multiple arguments in function
512:  * @return mixed translated string
513:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__
514:  */
515: function __($singular, $args = null) {
516:     if (!$singular) {
517:         return;
518:     }
519: 
520:     App::uses('I18n', 'I18n');
521:     $translated = I18n::translate($singular);
522:     if ($args === null) {
523:         return $translated;
524:     } elseif (!is_array($args)) {
525:         $args = array_slice(func_get_args(), 1);
526:     }
527:     return vsprintf($translated, $args);
528: }
529: 
530: /**
531:  * Returns correct plural form of message identified by $singular and $plural for count $count.
532:  * Some languages have more than one form for plural messages dependent on the count.
533:  *
534:  * @param string $singular Singular text to translate
535:  * @param string $plural Plural text
536:  * @param integer $count Count
537:  * @param mixed $args Array with arguments or multiple arguments in function
538:  * @return mixed plural form of translated string
539:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__n
540:  */
541: function __n($singular, $plural, $count, $args = null) {
542:     if (!$singular) {
543:         return;
544:     }
545: 
546:     App::uses('I18n', 'I18n');
547:     $translated = I18n::translate($singular, $plural, null, 6, $count);
548:     if ($args === null) {
549:         return $translated;
550:     } elseif (!is_array($args)) {
551:         $args = array_slice(func_get_args(), 3);
552:     }
553:     return vsprintf($translated, $args);
554: }
555: 
556: /**
557:  * Allows you to override the current domain for a single message lookup.
558:  *
559:  * @param string $domain Domain
560:  * @param string $msg String to translate
561:  * @param mixed $args Array with arguments or multiple arguments in function
562:  * @return translated string
563:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__d
564:  */
565: function __d($domain, $msg, $args = null) {
566:     if (!$msg) {
567:         return;
568:     }
569:     App::uses('I18n', 'I18n');
570:     $translated = I18n::translate($msg, null, $domain);
571:     if ($args === null) {
572:         return $translated;
573:     } elseif (!is_array($args)) {
574:         $args = array_slice(func_get_args(), 2);
575:     }
576:     return vsprintf($translated, $args);
577: }
578: 
579: /**
580:  * Allows you to override the current domain for a single plural message lookup.
581:  * Returns correct plural form of message identified by $singular and $plural for count $count
582:  * from domain $domain.
583:  *
584:  * @param string $domain Domain
585:  * @param string $singular Singular string to translate
586:  * @param string $plural Plural
587:  * @param integer $count Count
588:  * @param mixed $args Array with arguments or multiple arguments in function
589:  * @return plural form of translated string
590:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dn
591:  */
592: function __dn($domain, $singular, $plural, $count, $args = null) {
593:     if (!$singular) {
594:         return;
595:     }
596:     App::uses('I18n', 'I18n');
597:     $translated = I18n::translate($singular, $plural, $domain, 6, $count);
598:     if ($args === null) {
599:         return $translated;
600:     } elseif (!is_array($args)) {
601:         $args = array_slice(func_get_args(), 4);
602:     }
603:     return vsprintf($translated, $args);
604: }
605: 
606: /**
607:  * Allows you to override the current domain for a single message lookup.
608:  * It also allows you to specify a category.
609:  *
610:  * The category argument allows a specific category of the locale settings to be used for fetching a message.
611:  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
612:  *
613:  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
614:  *
615:  * - LC_ALL       0
616:  * - LC_COLLATE   1
617:  * - LC_CTYPE     2
618:  * - LC_MONETARY  3
619:  * - LC_NUMERIC   4
620:  * - LC_TIME      5
621:  * - LC_MESSAGES  6
622:  *
623:  * @param string $domain Domain
624:  * @param string $msg Message to translate
625:  * @param integer $category Category
626:  * @param mixed $args Array with arguments or multiple arguments in function
627:  * @return translated string
628:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dc
629:  */
630: function __dc($domain, $msg, $category, $args = null) {
631:     if (!$msg) {
632:         return;
633:     }
634:     App::uses('I18n', 'I18n');
635:     $translated = I18n::translate($msg, null, $domain, $category);
636:     if ($args === null) {
637:         return $translated;
638:     } elseif (!is_array($args)) {
639:         $args = array_slice(func_get_args(), 3);
640:     }
641:     return vsprintf($translated, $args);
642: }
643: 
644: /**
645:  * Allows you to override the current domain for a single plural message lookup.
646:  * It also allows you to specify a category.
647:  * Returns correct plural form of message identified by $singular and $plural for count $count
648:  * from domain $domain.
649:  *
650:  * The category argument allows a specific category of the locale settings to be used for fetching a message.
651:  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
652:  *
653:  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
654:  *
655:  * - LC_ALL       0
656:  * - LC_COLLATE   1
657:  * - LC_CTYPE     2
658:  * - LC_MONETARY  3
659:  * - LC_NUMERIC   4
660:  * - LC_TIME      5
661:  * - LC_MESSAGES  6
662:  *
663:  * @param string $domain Domain
664:  * @param string $singular Singular string to translate
665:  * @param string $plural Plural
666:  * @param integer $count Count
667:  * @param integer $category Category
668:  * @param mixed $args Array with arguments or multiple arguments in function
669:  * @return plural form of translated string
670:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__dcn
671:  */
672: function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
673:     if (!$singular) {
674:         return;
675:     }
676:     App::uses('I18n', 'I18n');
677:     $translated = I18n::translate($singular, $plural, $domain, $category, $count);
678:     if ($args === null) {
679:         return $translated;
680:     } elseif (!is_array($args)) {
681:         $args = array_slice(func_get_args(), 5);
682:     }
683:     return vsprintf($translated, $args);
684: }
685: 
686: /**
687:  * The category argument allows a specific category of the locale settings to be used for fetching a message.
688:  * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
689:  *
690:  * Note that the category must be specified with a numeric value, instead of the constant name.  The values are:
691:  *
692:  * - LC_ALL       0
693:  * - LC_COLLATE   1
694:  * - LC_CTYPE     2
695:  * - LC_MONETARY  3
696:  * - LC_NUMERIC   4
697:  * - LC_TIME      5
698:  * - LC_MESSAGES  6
699:  *
700:  * @param string $msg String to translate
701:  * @param integer $category Category
702:  * @param mixed $args Array with arguments or multiple arguments in function
703:  * @return translated string
704:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#__c
705:  */
706: function __c($msg, $category, $args = null) {
707:     if (!$msg) {
708:         return;
709:     }
710:     App::uses('I18n', 'I18n');
711:     $translated = I18n::translate($msg, null, null, $category);
712:     if ($args === null) {
713:         return $translated;
714:     } elseif (!is_array($args)) {
715:         $args = array_slice(func_get_args(), 2);
716:     }
717:     return vsprintf($translated, $args);
718: }
719: 
720: /**
721:  * Shortcut to Log::write.
722:  *
723:  * @param string $message Message to write to log
724:  * @return void
725:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#LogError
726:  */
727: function LogError($message) {
728:     App::uses('CakeLog', 'Log');
729:     $bad = array("\n", "\r", "\t");
730:     $good = ' ';
731:     CakeLog::write('error', str_replace($bad, $good, $message));
732: }
733: 
734: /**
735:  * Searches include path for files.
736:  *
737:  * @param string $file File to look for
738:  * @return Full path to file if exists, otherwise false
739:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#fileExistsInPath
740:  */
741: function fileExistsInPath($file) {
742:     $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
743:     foreach ($paths as $path) {
744:         $fullPath = $path . DS . $file;
745: 
746:         if (file_exists($fullPath)) {
747:             return $fullPath;
748:         } elseif (file_exists($file)) {
749:             return $file;
750:         }
751:     }
752:     return false;
753: }
754: 
755: /**
756:  * Convert forward slashes to underscores and removes first and last underscores in a string
757:  *
758:  * @param string String to convert
759:  * @return string with underscore remove from start and end of string
760:  * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#convertSlash
761:  */
762: function convertSlash($string) {
763:     $string = trim($string, '/');
764:     $string = preg_replace('/\/\//', '/', $string);
765:     $string = str_replace('/', '_', $string);
766:     return $string;
767: }
768: 
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