mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 19:38:26 +00:00
4a8c03ef1c
Revision: [2618] Adding fix for Ticket #609 Revision: [2617] Added fix for Ticket #684 Revision: [2616] Adding patch from Ticket #649 Revision: [2615] Adding fix for Ticket #608 Revision: [2614] Additional fix for Ticket #584 Revision: [2613] Adding fix for Ticket #584 Revision: [2612] Added fix for undefined index notices Revision: [2609] Adding fix for Ticket #658 Revision: [2608] Adding fix for Ticket #635, and code formatting fixes in FormHelper Revision: [2607] Adding fix for Ticket #636 Revision: [2606] Adding fix to allow associations to be defined through non-associative arrays Revision: [2605] Adding fix for Ticket #672 Revision: [2604] Adding fix for Ticket #708 Revision: [2603] Adding fix for Ticket #687 Revision: [2602] Refactoring database drivers, and adding fix for Ticket #398 Revision: [2601] Merging change from model_php5.php Revision: [2600] Adding ODBC driver Revision: [2599] Adding fix for Ticket #702 Revision: [2598] Adding fix for Ticket #699 Revision: [2597] Fixing an issue in Model::set(), and moving limit() to DboSource Revision: [2595] Fixing unit test download URL in Bake Revision: [2594] Adding fix for Ticket #698 Revision: [2593] Adding fox for Ticket #231 Revision: [2592] Adding fix for Ticket #630, and updating MS SQL driver docstring Revision: [2577] Adding $alias property to enable future Oracle support Revision: [2568] Merging changes to bake from old sandboxes git-svn-id: https://svn.cakephp.org/repo/trunk/cake/1.x.x.x@2620 3807eeeb-6ff5-0310-8944-8be069107fe0
288 lines
No EOL
7.4 KiB
PHP
288 lines
No EOL
7.4 KiB
PHP
<?php
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
* Javascript Helper class file.
|
|
*
|
|
* PHP versions 4 and 5
|
|
*
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
* Las Vegas, Nevada 89104
|
|
*
|
|
* Licensed under The MIT License
|
|
* Redistributions of files must retain the above copyright notice.
|
|
*
|
|
* @filesource
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
* @package cake
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
* @since CakePHP v 0.10.0.1076
|
|
* @version $Revision$
|
|
* @modifiedby $LastChangedBy$
|
|
* @lastmodified $Date$
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
*/
|
|
|
|
|
|
/**
|
|
* Javascript Helper class for easy use of JavaScript.
|
|
*
|
|
* JavascriptHelper encloses all methods needed while working with JavaScript.
|
|
*
|
|
* @package cake
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
* @since CakePHP v 0.10.0.1076
|
|
*/
|
|
|
|
class JavascriptHelper extends Helper
|
|
{
|
|
|
|
var $_cachedEvents = array();
|
|
var $_cacheEvents = false;
|
|
|
|
/**
|
|
* Returns a JavaScript script tag.
|
|
*
|
|
* @param string $script The JavaScript to be wrapped in SCRIPT tags.
|
|
* @return string The full SCRIPT element, with the JavaScript inside it.
|
|
*/
|
|
function codeBlock($script)
|
|
{
|
|
return sprintf($this->tags['javascriptblock'], $script);
|
|
}
|
|
|
|
/**
|
|
* Returns a JavaScript include tag (SCRIPT element)
|
|
*
|
|
* @param string $url URL to JavaScript file.
|
|
* @return string
|
|
*/
|
|
function link($url)
|
|
{
|
|
if(strpos($url, ".") === false) $url .= ".js";
|
|
return sprintf($this->tags['javascriptlink'], $this->webroot.JS_URL.$this->themeWeb.$url);
|
|
}
|
|
|
|
/**
|
|
* Returns a JavaScript include tag for an externally-hosted script
|
|
*
|
|
* @param string $url URL to JavaScript file.
|
|
* @return string
|
|
*/
|
|
function linkOut($url)
|
|
{
|
|
if(strpos($url, ".") === false) $url .= ".js";
|
|
return sprintf($this->tags['javascriptlink'], $url);
|
|
}
|
|
|
|
/**
|
|
* Escape carriage returns and single and double quotes for JavaScript segments.
|
|
*
|
|
* @param string $script string that might have javascript elements
|
|
* @return string escaped string
|
|
*/
|
|
function escapeScript ($script)
|
|
{
|
|
$script = str_replace(array("\r\n","\n","\r"),'\n', $script);
|
|
$script = str_replace(array('"', "'"), array('\"', "\\'"), $script);
|
|
return $script;
|
|
}
|
|
|
|
/**
|
|
* Escape a string to be JavaScript friendly.
|
|
*
|
|
* List of escaped ellements:
|
|
* + "\r\n" => '\n'
|
|
* + "\r" => '\n'
|
|
* + "\n" => '\n'
|
|
* + '"' => '\"'
|
|
* + "'" => "\\'"
|
|
*
|
|
* @param string $script String that needs to get escaped.
|
|
* @return string Escaped string.
|
|
*/
|
|
function escapeString($string)
|
|
{
|
|
$escape = array(
|
|
"\r\n" => '\n',
|
|
"\r" => '\n',
|
|
"\n" => '\n',
|
|
'"' => '\"',
|
|
"'" => "\\'"
|
|
);
|
|
|
|
return str_replace(array_keys($escape), array_values($escape), $string);
|
|
}
|
|
/**
|
|
* Attach an event to an element. Used with the Prototype library.
|
|
*
|
|
* @param string $object Object to be observed
|
|
* @param string $event event to observe
|
|
* @param string $observer function to call
|
|
* @param boolean $useCapture default true
|
|
* @return boolean true on success
|
|
*/
|
|
function event ($object, $event, $observer, $useCapture = false)
|
|
{
|
|
if($useCapture == true)
|
|
{
|
|
$useCapture = "true";
|
|
}
|
|
else
|
|
{
|
|
$useCapture = "false";
|
|
}
|
|
|
|
$b = "Event.observe($object, '$event', function(event){ $observer }, $useCapture);";
|
|
if($this->_cacheEvents === true)
|
|
{
|
|
$this->_cachedEvents[] = $b;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return $this->codeBlock($b);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Cache JavaScript events created with event()
|
|
*
|
|
* @return null
|
|
*/
|
|
function cacheEvents ()
|
|
{
|
|
$this->_cacheEvents = true;
|
|
}
|
|
|
|
|
|
/**
|
|
* Write cached JavaScript events
|
|
*
|
|
* @return string A single code block of all cached JavaScript events created with event()
|
|
*/
|
|
function writeEvents ()
|
|
{
|
|
$this->_cacheEvents = false;
|
|
return $this->codeBlock("\n" . implode("\n", $this->_cachedEvents) . "\n");
|
|
}
|
|
|
|
/**
|
|
* Includes the Prototype Javascript library (and anything else) inside a single script tag.
|
|
*
|
|
* Note: The recommended approach is to copy the contents of
|
|
* javascripts into your application's
|
|
* public/javascripts/ directory, and use @see javascriptIncludeTag() to
|
|
* create remote script links.
|
|
* @return string script with all javascript in/javascripts folder
|
|
*/
|
|
function includeScript ($script = "")
|
|
{
|
|
if($script == "")
|
|
{
|
|
$files = scandir(JS);
|
|
$javascript = '';
|
|
foreach($files as $file)
|
|
{
|
|
if (substr($file, -3) == '.js')
|
|
{
|
|
$javascript .= file_get_contents(JS."{$file}") . "\n\n";
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$javascript = file_get_contents(JS."$script.js") . "\n\n";
|
|
}
|
|
return $this->codeBlock("\n\n" . $javascript);
|
|
}
|
|
|
|
/**
|
|
* Generates a JavaScript object in JavaScript Object Notation (JSON)
|
|
* from an array
|
|
*
|
|
* @param array $data Data to be converted
|
|
* @param boolean $block Wraps return value in a <script/> block if true
|
|
* @param string $prefix Prepends the string to the returned data
|
|
* @param string $postfix Appends the string to the returned data
|
|
* @param array $stringKeys A list of array keys to be treated as a string
|
|
* @param boolean $quoteKeys If false, treats $stringKey as a list of keys *not* to be quoted
|
|
* @param string $q The type of quote to use
|
|
* @return string A JSON code block
|
|
*/
|
|
function object ($data = array(), $block = false, $prefix = '', $postfix = '', $stringKeys = array(), $quoteKeys = true, $q = "\"")
|
|
{
|
|
if (is_object($data))
|
|
{
|
|
$data = get_object_vars($data);
|
|
}
|
|
|
|
$out = array();
|
|
$key = array();
|
|
if (is_array($data))
|
|
{
|
|
$keys = array_keys($data);
|
|
}
|
|
$numeric = true;
|
|
|
|
if (!empty($keys))
|
|
{
|
|
foreach ($keys as $key)
|
|
{
|
|
if (!is_numeric($key))
|
|
{
|
|
$numeric = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach($data as $key => $val)
|
|
{
|
|
if (is_array($val) || is_object($val))
|
|
{
|
|
$val = $this->object($val, false, '', '', $stringKeys, $quoteKeys, $q);
|
|
$out[] = $val;
|
|
}
|
|
else
|
|
{
|
|
if ((!count($stringKeys) && !is_numeric($val) && !is_bool($val)) || ($quoteKeys && in_array($key, $stringKeys)) || (!$quoteKeys && !in_array($key, $stringKeys)))
|
|
{
|
|
$val = $q.$val.$q;
|
|
}
|
|
if (trim($val) == '')
|
|
{
|
|
$val = 'null';
|
|
}
|
|
}
|
|
|
|
if (!$numeric)
|
|
{
|
|
$val = $key.':'.$val;
|
|
}
|
|
$out[] = $val;
|
|
}
|
|
|
|
if (!$numeric)
|
|
{
|
|
$rt = '{'.join(', ', $out).'}';
|
|
}
|
|
else
|
|
{
|
|
$rt = 'new Array('.join(', ', $out).')';
|
|
}
|
|
$rt = $prefix.$rt.$postfix;
|
|
|
|
if ($block)
|
|
{
|
|
$rt = $this->codeBlock($rt);
|
|
}
|
|
return $rt;
|
|
}
|
|
}
|
|
|
|
?>
|