AjaxComponent.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * Ajax Component to respond to AJAX requests.
  5. *
  6. * Works together with the AjaxView to easily switch
  7. * output type from HTML to JSON format.
  8. *
  9. * It will also avoid redirects and pass those down as content
  10. * of the JSON response object.
  11. *
  12. * Don't forget Configure::write('Ajax.flashKey', 'messages');
  13. * if you want to use it with Tools.Flash component.
  14. *
  15. * @author Mark Scherer
  16. * @license http://opensource.org/licenses/mit-license.php MIT
  17. * @deprecated Use https://github.com/dereuromark/cakephp-ajax/tree/2.x
  18. */
  19. class AjaxComponent extends Component {
  20. public $Controller;
  21. public $components = ['Session'];
  22. public $respondAsAjax = false;
  23. protected $_defaultConfig = [
  24. 'autoDetect' => true,
  25. 'resolveRedirect' => true,
  26. 'flashKey' => 'Message.flash' // Use "messages" for Tools plugin Flash component, set to false to disable
  27. ];
  28. /**
  29. * Constructor.
  30. *
  31. * @param ComponentCollection $collection
  32. * @param array $config
  33. */
  34. public function __construct(ComponentCollection $collection, $config = []) {
  35. $defaults = (array)Configure::read('Ajax') + $this->_defaultConfig;
  36. $config += $defaults;
  37. parent::__construct($collection, $config);
  38. }
  39. public function initialize(Controller $Controller) {
  40. $this->Controller = $Controller;
  41. if (!$this->settings['autoDetect']) {
  42. return;
  43. }
  44. $this->respondAsAjax = $this->Controller->request->is('ajax');
  45. }
  46. /**
  47. * Called before the Controller::beforeRender(), and before
  48. * the view class is loaded, and before Controller::render()
  49. *
  50. * @param Controller $controller Controller with components to beforeRender
  51. * @return void
  52. */
  53. public function beforeRender(Controller $controller) {
  54. if (!$this->respondAsAjax) {
  55. return;
  56. }
  57. $this->_respondAsAjax();
  58. }
  59. /**
  60. * AjaxComponent::respondAsAjax()
  61. *
  62. * @return void
  63. */
  64. protected function _respondAsAjax() {
  65. $this->Controller->viewClass = 'Tools.Ajax';
  66. // Set flash messages to the view
  67. if ($this->settings['flashKey']) {
  68. $_message = $this->Session->read($this->settings['flashKey']);
  69. $this->Session->delete($this->settings['flashKey']);
  70. $this->Controller->set(compact('_message'));
  71. }
  72. }
  73. /**
  74. * Called before Controller::redirect(). Allows you to replace the URL that will
  75. * be redirected to with a new URL. The return of this method can either be an array or a string.
  76. *
  77. * If the return is an array and contains a 'url' key. You may also supply the following:
  78. *
  79. * - `status` The status code for the redirect
  80. * - `exit` Whether or not the redirect should exit.
  81. *
  82. * If your response is a string or an array that does not contain a 'url' key it will
  83. * be used as the new URL to redirect to.
  84. *
  85. * @param Controller $controller Controller with components to beforeRedirect
  86. * @param string|array $url Either the string or URL array that is being redirected to.
  87. * @param int $status The status code of the redirect
  88. * @param bool $exit Will the script exit.
  89. * @return array|void Either an array or null.
  90. */
  91. public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
  92. if (!$this->respondAsAjax || !$this->settings['resolveRedirect']) {
  93. return parent::beforeRedirect($controller, $url, $status, $exit);
  94. }
  95. $url = Router::url($url, true);
  96. if (is_string($status)) {
  97. $codes = array_flip($this->response->httpCodes());
  98. if (isset($codes[$status])) {
  99. $status = $codes[$status];
  100. }
  101. }
  102. $this->Controller->autoRender = true;
  103. $this->Controller->set('_redirect', compact('url', 'status', 'exit'));
  104. $serializeKeys = ['_redirect', '_message'];
  105. if (!empty($this->Controller->viewVars['_serialize'])) {
  106. $serializeKeys = array_merge($serializeKeys, $this->Controller->viewVars['_serialize']);
  107. }
  108. $this->Controller->set('_serialize', $serializeKeys);
  109. return false;
  110. }
  111. }