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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 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
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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