00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class AjaxHelper extends Helper {
00038
00039
00040
00041
00042
00043
00044 var $helpers = array('Html', 'Javascript');
00045
00046
00047
00048
00049
00050
00051 var $callbacks = array('uninitialized', 'loading', 'loaded', 'interactive', 'complete', 'success', 'failure');
00052
00053
00054
00055
00056
00057
00058 var $ajaxOptions = array('type', 'confirm', 'condition', 'before', 'after', 'fallback', 'update', 'loading', 'loaded', 'interactive', 'complete', 'with', 'url', 'method', 'position', 'form', 'parameters', 'evalScripts', 'asynchronous', 'onComplete', 'onUninitialized', 'onLoading', 'onLoaded', 'onInteractive', 'success', 'failure', 'onSuccess', 'onFailure', 'insertion', 'requestHeaders');
00059
00060
00061
00062
00063
00064
00065 var $dragOptions = array('handle', 'revert', 'constraint', 'change', 'ghosting');
00066
00067
00068
00069
00070
00071
00072 var $dropOptions = array('accept', 'containment', 'overlap', 'greedy', 'hoverclass', 'onHover', 'onDrop');
00073
00074
00075
00076
00077
00078
00079 var $sortOptions = array('tag', 'only', 'overlap', 'constraint', 'containment', 'handle', 'hoverclass', 'ghosting', 'dropOnEmpty', 'onUpdate', 'onChange');
00080
00081
00082
00083
00084
00085
00086 var $sliderOptions = array('axis', 'increment', 'maximum', 'minimum', 'alignX', 'alignY', 'sliderValue', 'disabled', 'handleImage', 'handleDisabled', 'values', 'onSlide', 'onChange');
00087
00088
00089
00090
00091
00092
00093 var $editorOptions = array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'rows', 'cols', 'size', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'callback', 'ajaxOptions', 'clickToEditText');
00094
00095
00096
00097
00098
00099
00100 var $autoCompleteOptions = array('paramName', 'tokens', 'frequency', 'minChars', 'indicator', 'updateElement', 'afterUpdateElement', 'onShow', 'onHide');
00101
00102
00103
00104
00105
00106
00107 var $__ajaxBuffer = array();
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 function link($title, $href = null, $options = array(), $confirm = null, $escapeTitle = true) {
00168 if (!isset($href)) {
00169 $href = $title;
00170 }
00171
00172 if (!isset($options['url'])) {
00173 $options['url'] = $href;
00174 }
00175
00176 if (isset($confirm)) {
00177 $options['confirm'] = $confirm;
00178 unset($confirm);
00179 }
00180 $htmlOptions = $this->__getHtmlOptions($options);
00181
00182 if (empty($options['fallback']) || !isset($options['fallback'])) {
00183 $options['fallback'] = $href;
00184 }
00185
00186 if (!isset($htmlOptions['id'])) {
00187 $htmlOptions['id'] = 'link' . intval(rand());
00188 }
00189
00190 if (!isset($htmlOptions['onclick'])) {
00191 $htmlOptions['onclick'] = '';
00192 }
00193 $htmlOptions['onclick'] .= ' event.returnValue = false; return false;';
00194 $return = $this->Html->link($title, $href, $htmlOptions, null, $escapeTitle);
00195 $script = $this->Javascript->event("'{$htmlOptions['id']}'", "click", $this->remoteFunction($options));
00196
00197 if (is_string($script)) {
00198 $return .= $script;
00199 }
00200 return $return;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 function remoteFunction($options = null) {
00215 if (isset($options['update'])) {
00216 if (!is_array($options['update'])) {
00217 $func = "new Ajax.Updater('{$options['update']}',";
00218 } else {
00219 $func = "new Ajax.Updater(document.createElement('div'),";
00220 }
00221 if (!isset($options['requestHeaders'])) {
00222 $options['requestHeaders'] = array();
00223 }
00224 if (is_array($options['update'])) {
00225 $options['update'] = join(' ', $options['update']);
00226 }
00227 $options['requestHeaders']['X-Update'] = $options['update'];
00228 } else {
00229 $func = "new Ajax.Request(";
00230 }
00231
00232 $func .= "'" . $this->Html->url(isset($options['url']) ? $options['url'] : "") . "'";
00233 $func .= ", " . $this->__optionsForAjax($options) . ")";
00234
00235 if (isset($options['before'])) {
00236 $func = "{$options['before']}; $func";
00237 }
00238
00239 if (isset($options['after'])) {
00240 $func = "$func; {$options['after']};";
00241 }
00242
00243 if (isset($options['condition'])) {
00244 $func = "if ({$options['condition']}) { $func; }";
00245 }
00246
00247 if (isset($options['confirm'])) {
00248 $func = "if (confirm('" . $this->Javascript->escapeString($options['confirm'])
00249 . "')) { $func; } else { event.returnValue = false; return false; }";
00250 }
00251 return $func;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 function remoteTimer($options = null) {
00266 $frequency=(isset($options['frequency'])) ? $options['frequency'] : 10;
00267 $code="new PeriodicalExecuter(function() {" . $this->remoteFunction($options) . "}, $frequency)";
00268 return $this->Javascript->codeBlock($code);
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 function form($params = null, $type = 'post', $options = array()) {
00285 if (is_array($params)) {
00286 extract($params, EXTR_OVERWRITE);
00287
00288 if (!isset($action)) {
00289 $action = null;
00290 }
00291
00292 if (!isset($type)) {
00293 $type = 'post';
00294 }
00295
00296 if (!isset($options)) {
00297 $options = array();
00298 }
00299 } else {
00300 $action = $params;
00301 }
00302 $htmlOptions = $this->__getHtmlOptions($options);
00303 $htmlOptions['action'] = $action;
00304
00305 if (!isset($htmlOptions['id'])) {
00306 $htmlOptions['id'] = 'form' . intval(rand());
00307 }
00308 $htmlOptions['onsubmit']="event.returnValue = false; return false;";
00309
00310 if (!isset($options['with'])) {
00311 $options['with'] = "Form.serialize('{$htmlOptions['id']}')";
00312 }
00313 $options['url']=$action;
00314
00315 return $this->Html->formTag($htmlOptions['action'], $type, $htmlOptions)
00316 . $this->Javascript->event("'" . $htmlOptions['id']. "'", "submit", $this->remoteFunction($options));
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 function submit($title = 'Submit', $options = array()) {
00330 $htmlOptions =$this->__getHtmlOptions($options);
00331 $htmlOptions['value']=$title;
00332
00333 if (!isset($options['with'])) {
00334 $options['with'] = 'Form.serialize(Event.element(event).form)';
00335 }
00336
00337 if (!isset($htmlOptions['id'])) {
00338 $htmlOptions['id'] = 'submit' . intval(rand());
00339 }
00340 $htmlOptions['onclick']="event.returnValue = false; return false;";
00341 return $this->Html->submit($title, $htmlOptions)
00342 . $this->Javascript->event('"' . $htmlOptions['id'] . '"', 'click', $this->remoteFunction($options));
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 function observeField($field_id, $options = array()) {
00372 if (!isset($options['with'])) {
00373 $options['with'] = "Form.Element.serialize('$field_id')";
00374 }
00375 return $this->Javascript->codeBlock($this->_buildObserver('Form.Element.Observer', $field_id, $options));
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 function observeForm($field_id, $options = array()) {
00391 if (!isset($options['with'])) {
00392 $options['with'] = 'Form.serialize("' . $field_id . '")';
00393 }
00394 return $this->Javascript->codeBlock($this->_buildObserver('Form.Observer', $field_id, $options));
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 function autoComplete($field, $url = "", $options = array()) {
00412 $var = '';
00413 if (isset($options['var'])) {
00414 $var = 'var ' . $options['var'] . ' = ';
00415 unset($options['var']);
00416 }
00417
00418 if (!isset($options['id'])) {
00419 $options['id'] = Inflector::camelize(r("/", "_", $field));
00420 }
00421 $divOptions = array('id' => $options['id'] . "_autoComplete", 'class' => isset($options['class']) ? $options['class'] : 'auto_complete');
00422
00423 if (isset($options['div_id'])) {
00424 $divOptions['id'] = $options['div_id'];
00425 unset($options['div_id']);
00426 }
00427 $htmlOptions = $this->__getHtmlOptions($options);
00428 $htmlOptions['autocomplete'] = "off";
00429
00430 foreach ($this->autoCompleteOptions as $opt) {
00431 unset($htmlOptions[$opt]);
00432 }
00433
00434 if (isset($options['tokens'])) {
00435 if (is_array($options['tokens'])) {
00436 $options['tokens'] = $this->Javascript->object($options['tokens']);
00437 } else {
00438 $options['tokens'] = '"' . $options['tokens'] . '"';
00439 }
00440 }
00441 $options = $this->_optionsToString($options, array('paramName', 'indicator'));
00442 $options = $this->_buildOptions($options, $this->autoCompleteOptions);
00443 return $this->Html->input($field, $htmlOptions) . "\n" .
00444 $this->Html->tag('div', $divOptions, true) . "</div>\n" .
00445 $this->Javascript->codeBlock("{$var}new Ajax.Autocompleter('" . $htmlOptions['id']
00446 . "', '" . $divOptions['id'] . "', '" . $this->Html->url($url) . "', " .
00447 $options . ");");
00448 }
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 function drag($id, $options = array()) {
00460 return $this->Javascript->codeBlock("new Draggable('$id', " . $this->_optionsForDraggable($options) . ");");
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470 function div($id, $options = array()) {
00471 if (env('HTTP_X_UPDATE') != null) {
00472 $divs = explode(' ', env('HTTP_X_UPDATE'));
00473 if (in_array($id, $divs)) {
00474 @ob_end_clean();
00475 ob_start();
00476 return '';
00477 }
00478 }
00479 $attr = $this->Html->_parseAttributes(am($options, array('id' => $id)));
00480 return $this->output(sprintf($this->tags['blockstart'], $attr));
00481 }
00482
00483
00484
00485
00486
00487
00488
00489 function divEnd($id) {
00490 if (env('HTTP_X_UPDATE') != null) {
00491 $divs = explode(' ', env('HTTP_X_UPDATE'));
00492 if (in_array($id, $divs)) {
00493 $this->__ajaxBuffer[$id] = ob_get_contents();
00494 ob_end_clean();
00495 return '';
00496 }
00497 }
00498 return $this->output($this->tags['blockend']);
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 function _optionsForDraggable($options) {
00508 $options = $this->_optionsToString($options, array('handle', 'constraint'));
00509 return $this->_buildOptions($options, $this->dragOptions);
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 function drop($id, $options = array()) {
00522 $options = $this->_optionsForDroppable($options);
00523 return $this->Javascript->codeBlock("Droppables.add('$id', $options);");
00524 }
00525
00526
00527
00528
00529
00530
00531
00532 function _optionsForDroppable($options) {
00533 $options = $this->_optionsToString($options, array('accept', 'overlap', 'hoverclass'));
00534 return $this->_buildOptions($options, $this->dropOptions);
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 function dropRemote($id, $options = array(), $ajaxOptions = array()) {
00548 $options['onDrop'] = "function(element) {" . $this->remoteFunction($ajaxOptions) . "}";
00549 $options = $this->_optionsForDroppable($options);
00550 return $this->Javascript->codeBlock("Droppables.add('$id', $options);");
00551 }
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 function slider($id, $track_id, $options = array()) {
00563 $options = $this->_optionsToString($options, array('axis', 'handleImage', 'handleDisabled'));
00564
00565 if (isset($options['change'])) {
00566 $options['onChange'] = $options['change'];
00567 unset($options['change']);
00568 }
00569
00570 if (isset($options['slide'])) {
00571 $options['onSlide'] = $options['slide'];
00572 unset($options['slide']);
00573 }
00574
00575 $options = $this->_buildOptions($options, $this->sliderOptions);
00576 return $this->Javascript->codeBlock("var $id = new Control.Slider('$id', '$track_id', $options);");
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 function editor($id, $url, $options = array()) {
00589 $url = $this->Html->url($url);
00590 $options['ajaxOptions'] = $this->__optionsForAjax($options);
00591
00592 foreach ($this->ajaxOptions as $opt) {
00593 if (isset($options[$opt])) {
00594 unset($options[$opt]);
00595 }
00596 }
00597
00598 if (isset($options['callback'])) {
00599 $options['callback'] = 'function(form, value) {' . $options['callback'] . '}';
00600 }
00601
00602 $options = $this->_optionsToString($options, array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'clickToEditText'));
00603 $options = $this->_buildOptions($options, $this->editorOptions);
00604 return $this->Javascript->codeBlock("new Ajax.InPlaceEditor('{$id}', '{$url}', {$options});");
00605 }
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615 function sortable($id, $options = array()) {
00616 if (!empty($options['url'])) {
00617 $options['with'] = "Sortable.serialize('$id')";
00618 $options['onUpdate'] = 'function(sortable) {' . $this->remoteFunction($options) . '}';
00619 }
00620
00621 $options = $this->__optionsForSortable($options);
00622 return $this->Javascript->codeBlock("Sortable.create('$id', $options);");
00623 }
00624
00625
00626
00627
00628
00629
00630
00631 function __optionsForSortable($options) {
00632 $options = $this->_optionsToString($options, array('handle', 'tag', 'constraint', 'ghosting', 'only', 'hoverclass'));
00633 return $this->_buildOptions($options, $this->sortOptions);
00634 }
00635
00636
00637
00638
00639
00640
00641
00642 function __optionsForAjax($options = array()) {
00643
00644 if (isset($options['indicator'])) {
00645 if (isset($options['loading'])) {
00646 $options['loading'] .= "Element.show('{$options['indicator']}');";
00647 } else {
00648 $options['loading'] = "Element.show('{$options['indicator']}');";
00649 }
00650 if (isset($options['complete'])) {
00651 $options['complete'] .= "Element.hide('{$options['indicator']}');";
00652 } else {
00653 $options['complete'] = "Element.hide('{$options['indicator']}');";
00654 }
00655 unset($options['indicator']);
00656 }
00657
00658 $jsOptions = am(
00659 array('asynchronous' => 'true', 'evalScripts' => 'true'),
00660 $this->_buildCallbacks($options)
00661 );
00662 $options = $this->_optionsToString($options, array('method'));
00663
00664 foreach ($options as $key => $value) {
00665 switch($key) {
00666 case 'type':
00667 $jsOptions['asynchronous'] = ife(($value == 'synchronous'), 'false', 'true');
00668 break;
00669 case 'evalScripts':
00670 $jsOptions['evalScripts'] = ife($value, 'true', 'false');
00671 break;
00672 case 'position':
00673 $jsOptions['insertion'] = "Insertion." . Inflector::camelize($options['position']);
00674 break;
00675 case 'with':
00676 $jsOptions['parameters'] = $options['with'];
00677 break;
00678 case 'form':
00679 $jsOptions['parameters'] = 'Form.serialize(this)';
00680 break;
00681 case 'requestHeaders':
00682 $keys = array();
00683 foreach ($value as $key => $val) {
00684 $keys[] = "'" . $key . "'";
00685 $keys[] = "'" . $val . "'";
00686 }
00687 $jsOptions['requestHeaders'] = '[' . join(', ', $keys) . ']';
00688 break;
00689 }
00690 }
00691 return $this->_buildOptions($jsOptions, $this->ajaxOptions);
00692 }
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702 function __getHtmlOptions($options, $extra = array()) {
00703 foreach ($this->ajaxOptions as $key) {
00704 if (isset($options[$key])) {
00705 unset($options[$key]);
00706 }
00707 }
00708
00709 foreach ($extra as $key) {
00710 if (isset($options[$key])) {
00711 unset($options[$key]);
00712 }
00713 }
00714 return $options;
00715 }
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725 function _buildOptions($options, $acceptable) {
00726 if (is_array($options)) {
00727 $out = array();
00728
00729 foreach ($options as $k => $v) {
00730 if (in_array($k, $acceptable)) {
00731 $out[] = "$k:$v";
00732 }
00733 }
00734
00735 $out = join(', ', $out);
00736 $out = '{' . $out . '}';
00737 return $out;
00738 } else {
00739 return false;
00740 }
00741 }
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751 function _buildObserver($klass, $name, $options = null) {
00752 if (!isset($options['with']) && isset($options['update'])) {
00753 $options['with'] = 'value';
00754 }
00755
00756 $callback = $this->remoteFunction($options);
00757 $javascript = "new $klass('$name', ";
00758 $javascript .= (isset($options['frequency']) ? $options['frequency'] : 2) . ", function(element, value) {";
00759 $javascript .= "$callback})";
00760 return $javascript;
00761 }
00762
00763
00764
00765
00766
00767
00768
00769 function _buildCallbacks($options) {
00770 $callbacks = array();
00771
00772 foreach ($this->callbacks as $callback) {
00773 if (isset($options[$callback])) {
00774 $name = 'on' . ucfirst($callback);
00775 $code = $options[$callback];
00776 $callbacks[$name] = "function(request) {" . $code . "}";
00777 }
00778 }
00779 return $callbacks;
00780 }
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 function _optionsToString($options, $stringOpts = array()) {
00792 foreach ($stringOpts as $option) {
00793 if (isset($options[$option]) && !$options[$option][0] != "'") {
00794 if ($options[$option] === true || $options[$option] === 'true') {
00795 $options[$option] = 'true';
00796 } elseif ($options[$option] === false || $options[$option] === 'false') {
00797 $options[$option] = 'false';
00798 } else {
00799 $options[$option] = "'{$options[$option]}'";
00800 }
00801 }
00802 }
00803 return $options;
00804 }
00805
00806
00807
00808
00809
00810
00811 function afterRender() {
00812 if (env('HTTP_X_UPDATE') != null && count($this->__ajaxBuffer) > 0) {
00813 $data = array();
00814 $divs = explode(' ', env('HTTP_X_UPDATE'));
00815
00816 foreach ($this->__ajaxBuffer as $key => $val) {
00817 if (in_array($key, $divs)) {
00818 $data[] = $key . ':"' . rawurlencode($val) . '"';
00819 }
00820 }
00821
00822 $out = 'var __ajaxUpdater__ = {' . join(', ', $data) . '};' . "\n";
00823 $out .= 'for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string" && $(n)) Element.update($(n), unescape(decodeURIComponent(__ajaxUpdater__[n]))); }';
00824
00825 @ob_end_clean();
00826 e($this->Javascript->codeBlock($out));
00827 exit();
00828 }
00829 }
00830
00831
00832
00833
00834
00835 function linkToRemote($title, $options = array(), $html_options = array()) {
00836 trigger_error('Deprecated function: use AjaxHelper::link', E_USER_WARNING);
00837 $href = '#';
00838
00839 if (!empty($options['fallback']) && isset($options['fallback'])) {
00840 $href = $options['fallback'];
00841 }
00842
00843 if (isset($html_options['id'])) {
00844 return $this->Html->link($title, $href, $html_options) .
00845 $this->Javascript->event("$('{$html_options['id']}')", "click", $this->remoteFunction($options));
00846 } else {
00847 $html_options['onclick'] = $this->remoteFunction($options) . "; event.returnValue = false; return false;";
00848 return $this->Html->link($title, $href, $html_options);
00849 }
00850 }
00851 }
00852
00853 ?>