__engineName = $className . 'Engine'; $engineClass = $engineName . 'Engine'; $this->helpers = array($engineClass); parent::__construct(); } /** * call__ * * @param string $method Method to be called * @param array $params Parameters for the method being called. * @access public * @return void */ function call__($method, $params) { if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) { return $this->{$this->__engineName}->dispatchMethod($method, $params); } if (method_exists($this, $method . '_')) { return $this->dispatchMethod($method . '_', $params); } trigger_error(sprintf(__('JsHelper:: Missing Method %s is undefined', true), $method), E_USER_WARNING); } /** * Create an alert message in Javascript * * @param string $message Message you want to alter. * @access public * @return void */ function alert_($message) { return 'alert("' . $this->escape($message) . '");'; } function if_($if, $then, $else = null, $elseIf = array()) { $len = strlen($if) - 1; if ($if{$len} == ';') { $if{$len} = null; } $out = 'if (' . $if . ') { ' . $then . ' }'; foreach ($elseIf as $cond => $exec) { //$out .= } if (!empty($else)) { $out .= ' else { ' . $else . ' }'; } return $out; } /** * Create a confirm() message * * @param string $message Message you want confirmed. * @access public * @return void */ function confirm_($message) { return 'confirm("' . $this->escape($message) . '");'; } /** * Create a prompt() Javascript function * * @param string $message Message you want to prompt. * @param string $default Default message * @access public * @return void */ function prompt_($message, $default = '') { return 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '");'; } /* * Tries a series of expressions, and executes after first successful completion. * (See Prototype's Try.these). * * @return string */ function tryThese_($expr1, $expr2, $expr3) { } /** * Loads a remote URL * * @param string $url * @param array $options * @return string */ function load_($url = null, $options = array()) { if (isset($options['update'])) { if (!is_array($options['update'])) { $func = "new Ajax.Updater('{$options['update']}',"; } else { $func = "new Ajax.Updater(document.createElement('div'),"; } if (!isset($options['requestHeaders'])) { $options['requestHeaders'] = array(); } if (is_array($options['update'])) { $options['update'] = join(' ', $options['update']); } $options['requestHeaders']['X-Update'] = $options['update']; } else { $func = "new Ajax.Request("; } $func .= "'" . Router::url($url) . "'"; $ajax =& new AjaxHelper(); $func .= ", " . $ajax->__optionsForAjax($options) . ")"; if (isset($options['before'])) { $func = "{$options['before']}; $func"; } if (isset($options['after'])) { $func = "$func; {$options['after']};"; } if (isset($options['condition'])) { $func = "if ({$options['condition']}) { $func; }"; } if (isset($options['confirm'])) { $func = "if (confirm('" . $this->Javascript->escapeString($options['confirm']) . "')) { $func; } else { return false; }"; } return $func; } /** * Redirects to a URL * * @param mixed $url * @param array $options * @return string */ function redirect_($url = null) { return 'window.location = "' . Router::url($url) . '";'; } /** * 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 escape($string) { $escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'"); return str_replace(array_keys($escape), array_values($escape), $string); } /* function get__($name) { return $this->__object($name, 'id'); } */ function select($pattern) { return $this->__object($pattern, 'pattern'); } function real($var) { return $this->__object($var, 'real'); } function __object($name, $var) { if (!isset($this->__objects[$name])) { $this->__objects[$name] = new JsHelperObject($this); $this->__objects[$name]->{$var} = $name; } return $this->__objects[$name]; } /** * 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