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

Classes

  • CacheHelper
  • FormHelper
  • HtmlHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • MootoolsEngineHelper
  • NumberHelper
  • PaginatorHelper
  • PrototypeEngineHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://cakephp.org CakePHP(tm) Project
 11:  * @package       Cake.View.Helper
 12:  * @since         CakePHP(tm) v 1.3
 13:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 14:  */
 15: 
 16: App::uses('JsBaseEngineHelper', 'View/Helper');
 17: 
 18: /**
 19:  * MooTools Engine Helper for JsHelper
 20:  *
 21:  * Provides MooTools specific Javascript for JsHelper.
 22:  * Assumes that you have the following MooTools packages
 23:  *
 24:  * - Remote, Remote.HTML, Remote.JSON
 25:  * - Fx, Fx.Tween, Fx.Morph
 26:  * - Selectors, DomReady,
 27:  * - Drag, Drag.Move
 28:  *
 29:  * @package       Cake.View.Helper
 30:  */
 31: class MootoolsEngineHelper extends JsBaseEngineHelper {
 32: 
 33: /**
 34:  * Option mappings for MooTools
 35:  *
 36:  * @var array
 37:  */
 38:     protected $_optionMap = array(
 39:         'request' => array(
 40:             'complete' => 'onComplete',
 41:             'success' => 'onSuccess',
 42:             'before' => 'onRequest',
 43:             'error' => 'onFailure'
 44:         ),
 45:         'sortable' => array(
 46:             'distance' => 'snap',
 47:             'containment' => 'constrain',
 48:             'sort' => 'onSort',
 49:             'complete' => 'onComplete',
 50:             'start' => 'onStart',
 51:         ),
 52:         'drag' => array(
 53:             'snapGrid' => 'snap',
 54:             'start' => 'onStart',
 55:             'drag' => 'onDrag',
 56:             'stop' => 'onComplete',
 57:         ),
 58:         'drop' => array(
 59:             'drop' => 'onDrop',
 60:             'hover' => 'onEnter',
 61:             'leave' => 'onLeave',
 62:         ),
 63:         'slider' => array(
 64:             'complete' => 'onComplete',
 65:             'change' => 'onChange',
 66:             'direction' => 'mode',
 67:             'step' => 'steps'
 68:         )
 69:     );
 70: 
 71: /**
 72:  * Contains a list of callback names -> default arguments.
 73:  *
 74:  * @var array
 75:  */
 76:     protected $_callbackArguments = array(
 77:         'slider' => array(
 78:             'onTick' => 'position',
 79:             'onChange' => 'step',
 80:             'onComplete' => 'event'
 81:         ),
 82:         'request' => array(
 83:             'onRequest' => '',
 84:             'onComplete' => '',
 85:             'onCancel' => '',
 86:             'onSuccess' => 'responseText, responseXML',
 87:             'onFailure' => 'xhr',
 88:             'onException' => 'headerName, value',
 89:         ),
 90:         'drag' => array(
 91:             'onBeforeStart' => 'element',
 92:             'onStart' => 'element',
 93:             'onSnap' => 'element',
 94:             'onDrag' => 'element, event',
 95:             'onComplete' => 'element, event',
 96:             'onCancel' => 'element',
 97:         ),
 98:         'drop' => array(
 99:             'onBeforeStart' => 'element',
100:             'onStart' => 'element',
101:             'onSnap' => 'element',
102:             'onDrag' => 'element, event',
103:             'onComplete' => 'element, event',
104:             'onCancel' => 'element',
105:             'onDrop' => 'element, droppable, event',
106:             'onLeave' => 'element, droppable',
107:             'onEnter' => 'element, droppable',
108:         ),
109:         'sortable' => array(
110:             'onStart' => 'element, clone',
111:             'onSort' => 'element, clone',
112:             'onComplete' => 'element',
113:         )
114:     );
115: 
116: /**
117:  * Create javascript selector for a CSS rule
118:  *
119:  * @param string $selector The selector that is targeted
120:  * @return MootoolsEngineHelper instance of $this. Allows chained methods.
121:  */
122:     public function get($selector) {
123:         $this->_multipleSelection = false;
124:         if ($selector == 'window' || $selector == 'document') {
125:             $this->selection = "$(" . $selector . ")";
126:             return $this;
127:         }
128:         if (preg_match('/^#[^\s.]+$/', $selector)) {
129:             $this->selection = '$("' . substr($selector, 1) . '")';
130:             return $this;
131:         }
132:         $this->_multipleSelection = true;
133:         $this->selection = '$$("' . $selector . '")';
134:         return $this;
135:     }
136: 
137: /**
138:  * Add an event to the script cache. Operates on the currently selected elements.
139:  *
140:  * ### Options
141:  *
142:  * - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
143:  * - 'stop' - Whether you want the event to stopped. (defaults true)
144:  *
145:  * @param string $type Type of event to bind to the current dom id
146:  * @param string $callback The Javascript function you wish to trigger or the function literal
147:  * @param array $options Options for the event.
148:  * @return string completed event handler
149:  */
150:     public function event($type, $callback, $options = array()) {
151:         $defaults = array('wrap' => true, 'stop' => true);
152:         $options = array_merge($defaults, $options);
153: 
154:         $function = 'function (event) {%s}';
155:         if ($options['wrap'] && $options['stop']) {
156:             $callback = "event.stop();\n" . $callback;
157:         }
158:         if ($options['wrap']) {
159:             $callback = sprintf($function, $callback);
160:         }
161:         $out = $this->selection . ".addEvent(\"{$type}\", $callback);";
162:         return $out;
163:     }
164: 
165: /**
166:  * Create a domReady event. This is a special event in many libraries
167:  *
168:  * @param string $functionBody The code to run on domReady
169:  * @return string completed domReady method
170:  */
171:     public function domReady($functionBody) {
172:         $this->selection = 'window';
173:         return $this->event('domready', $functionBody, array('stop' => false));
174:     }
175: 
176: /**
177:  * Create an iteration over the current selection result.
178:  *
179:  * @param string $callback The function body you wish to apply during the iteration.
180:  * @return string completed iteration
181:  */
182:     public function each($callback) {
183:         return $this->selection . '.each(function (item, index) {' . $callback . '});';
184:     }
185: 
186: /**
187:  * Trigger an Effect.
188:  *
189:  * @param string $name The name of the effect to trigger.
190:  * @param array $options Array of options for the effect.
191:  * @return string completed string with effect.
192:  * @see JsBaseEngineHelper::effect()
193:  */
194:     public function effect($name, $options = array()) {
195:         $speed = null;
196:         if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
197:             if ($options['speed'] == 'fast') {
198:                 $speed = '"short"';
199:             } elseif ($options['speed'] == 'slow') {
200:                 $speed = '"long"';
201:             }
202:         }
203:         $effect = '';
204:         switch ($name) {
205:             case 'hide':
206:                 $effect = 'setStyle("display", "none")';
207:             break;
208:             case 'show':
209:                 $effect = 'setStyle("display", "")';
210:             break;
211:             case 'fadeIn':
212:             case 'fadeOut':
213:             case 'slideIn':
214:             case 'slideOut':
215:                 list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
216:                 $direction = strtolower($direction);
217:                 if ($speed) {
218:                     $effect .= "set(\"$effectName\", {duration:$speed}).";
219:                 }
220:                 $effect .= "$effectName(\"$direction\")";
221:             break;
222:         }
223:         return $this->selection . '.' . $effect . ';';
224:     }
225: 
226: /**
227:  * Create an new Request.
228:  *
229:  * Requires `Request`.  If you wish to use 'update' key you must have ```Request.HTML```
230:  * if you wish to do Json requests you will need ```JSON``` and ```Request.JSON```.
231:  *
232:  * @param string|array $url
233:  * @param array $options
234:  * @return string The completed ajax call.
235:  */
236:     public function request($url, $options = array()) {
237:         $url = html_entity_decode($this->url($url), ENT_COMPAT, Configure::read('App.encoding'));
238:         $options = $this->_mapOptions('request', $options);
239:         $type = $data = null;
240:         if (isset($options['type']) || isset($options['update'])) {
241:             if (isset($options['type']) && strtolower($options['type']) == 'json') {
242:                 $type = '.JSON';
243:             }
244:             if (isset($options['update'])) {
245:                 $options['update'] = str_replace('#', '', $options['update']);
246:                 $type = '.HTML';
247:             }
248:             unset($options['type']);
249:         }
250:         if (!empty($options['data'])) {
251:             $data = $options['data'];
252:             unset($options['data']);
253:         }
254:         $options['url'] = $url;
255:         $options = $this->_prepareCallbacks('request', $options);
256:         if (!empty($options['dataExpression'])) {
257:             unset($options['dataExpression']);
258:         } elseif (!empty($data)) {
259:             $data = $this->object($data);
260:         }
261:         $options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
262:         return "var jsRequest = new Request$type({{$options}}).send($data);";
263:     }
264: 
265: /**
266:  * Create a sortable element.
267:  *
268:  * Requires the `Sortables` plugin from MootoolsMore
269:  *
270:  * @param array $options Array of options for the sortable.
271:  * @return string Completed sortable script.
272:  * @see JsBaseEngineHelper::sortable() for options list.
273:  */
274:     public function sortable($options = array()) {
275:         $options = $this->_processOptions('sortable', $options);
276:         return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
277:     }
278: 
279: /**
280:  * Create a Draggable element.
281:  *
282:  * Requires the `Drag` plugin from MootoolsMore
283:  *
284:  * @param array $options Array of options for the draggable.
285:  * @return string Completed draggable script.
286:  * @see JsHelper::drag() for options list.
287:  */
288:     public function drag($options = array()) {
289:         $options = $this->_processOptions('drag', $options);
290:         return $this->selection . '.makeDraggable({' . $options . '});';
291:     }
292: 
293: /**
294:  * Create a Droppable element.
295:  *
296:  * Requires the `Drag` and `Drag.Move` plugins from MootoolsMore
297:  *
298:  * Droppables in Mootools function differently from other libraries.  Droppables
299:  * are implemented as an extension of Drag.  So in addition to making a get() selection for
300:  * the droppable element. You must also provide a selector rule to the draggable element. Furthermore,
301:  * Mootools droppables inherit all options from Drag.
302:  *
303:  * @param array $options Array of options for the droppable.
304:  * @return string Completed droppable script.
305:  * @see JsBaseEngineHelper::drop() for options list.
306:  */
307:     public function drop($options = array()) {
308:         if (empty($options['drag'])) {
309:             trigger_error(
310:                 __d('cake_dev', 'MootoolsEngine::drop() requires a "drag" option to properly function'), E_USER_WARNING
311:             );
312:             return false;
313:         }
314:         $options['droppables'] = $this->selection;
315: 
316:         $this->get($options['drag']);
317:         unset($options['drag']);
318: 
319:         $options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
320:         $options = $this->_prepareCallbacks('drop', $options);
321:         $safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
322:         $optionString = $this->_parseOptions($options, $safe);
323:         $out = $this->selection . '.makeDraggable({' . $optionString . '});';
324:         $this->selection = $options['droppables'];
325:         return $out;
326:     }
327: 
328: /**
329:  * Create a slider control
330:  *
331:  * Requires `Slider` from MootoolsMore
332:  *
333:  * @param array $options Array of options for the slider.
334:  * @return string Completed slider script.
335:  * @see JsBaseEngineHelper::slider() for options list.
336:  */
337:     public function slider($options = array()) {
338:         $slider = $this->selection;
339:         $this->get($options['handle']);
340:         unset($options['handle']);
341: 
342:         if (isset($options['min']) && isset($options['max'])) {
343:             $options['range'] = array($options['min'], $options['max']);
344:             unset($options['min'], $options['max']);
345:         }
346:         $optionString = $this->_processOptions('slider', $options);
347:         if (!empty($optionString)) {
348:             $optionString = ', {' . $optionString . '}';
349:         }
350:         $out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
351:         $this->selection = $slider;
352:         return $out;
353:     }
354: 
355: /**
356:  * Serialize the form attached to $selector.
357:  *
358:  * @param array $options Array of options.
359:  * @return string Completed serializeForm() snippet
360:  * @see JsBaseEngineHelper::serializeForm()
361:  */
362:     public function serializeForm($options = array()) {
363:         $options = array_merge(array('isForm' => false, 'inline' => false), $options);
364:         $selection = $this->selection;
365:         if (!$options['isForm']) {
366:             $selection = '$(' . $this->selection . '.form)';
367:         }
368:         $method = '.toQueryString()';
369:         if (!$options['inline']) {
370:             $method .= ';';
371:         }
372:         return $selection . $method;
373:     }
374: 
375: }
376: 
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