AjaxComponent.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. */
  18. class AjaxComponent extends Component {
  19. public $Controller;
  20. public $components = array('Session');
  21. public $respondAsAjax = false;
  22. protected $_defaultConfig = array(
  23. 'autoDetect' => true,
  24. 'resolveRedirect' => true,
  25. 'flashKey' => 'Message.flash' // Use "messages" for Tools plugin Flash component, set to false to disable
  26. );
  27. /**
  28. * Constructor.
  29. *
  30. * @param ComponentCollection $collection
  31. * @param array $config
  32. */
  33. public function __construct(ComponentCollection $collection, $config = array()) {
  34. $defaults = (array)Configure::read('Ajax') + $this->_defaultConfig;
  35. $config += $defaults;
  36. parent::__construct($collection, $config);
  37. }
  38. public function initialize(Controller $Controller) {
  39. $this->Controller = $Controller;
  40. if (!$this->settings['autoDetect']) {
  41. return;
  42. }
  43. $this->respondAsAjax = $this->Controller->request->is('ajax');
  44. }
  45. /**
  46. * Called before the Controller::beforeRender(), and before
  47. * the view class is loaded, and before Controller::render()
  48. *
  49. * @param Controller $controller Controller with components to beforeRender
  50. * @return void
  51. */
  52. public function beforeRender(Controller $controller) {
  53. if (!$this->respondAsAjax) {
  54. return;
  55. }
  56. $this->_respondAsAjax();
  57. }
  58. /**
  59. * AjaxComponent::respondAsAjax()
  60. *
  61. * @return void
  62. */
  63. protected function _respondAsAjax() {
  64. $this->Controller->viewClass = 'Tools.Ajax';
  65. // Set flash messages to the view
  66. if ($this->settings['flashKey']) {
  67. $_message = $this->Session->read($this->settings['flashKey']);
  68. $this->Session->delete($this->settings['flashKey']);
  69. $this->Controller->set(compact('_message'));
  70. }
  71. }
  72. /**
  73. * Called before Controller::redirect(). Allows you to replace the URL that will
  74. * be redirected to with a new URL. The return of this method can either be an array or a string.
  75. *
  76. * If the return is an array and contains a 'url' key. You may also supply the following:
  77. *
  78. * - `status` The status code for the redirect
  79. * - `exit` Whether or not the redirect should exit.
  80. *
  81. * If your response is a string or an array that does not contain a 'url' key it will
  82. * be used as the new URL to redirect to.
  83. *
  84. * @param Controller $controller Controller with components to beforeRedirect
  85. * @param string|array $url Either the string or URL array that is being redirected to.
  86. * @param int $status The status code of the redirect
  87. * @param bool $exit Will the script exit.
  88. * @return array|void Either an array or null.
  89. */
  90. public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
  91. if (!$this->respondAsAjax || !$this->settings['resolveRedirect']) {
  92. return parent::beforeRedirect($controller, $url, $status, $exit);
  93. }
  94. $url = Router::url($url, true);
  95. if (is_string($status)) {
  96. $codes = array_flip($this->response->httpCodes());
  97. if (isset($codes[$status])) {
  98. $status = $codes[$status];
  99. }
  100. }
  101. $this->Controller->autoRender = true;
  102. $this->Controller->set('_redirect', compact('url', 'status', 'exit'));
  103. $serializeKeys = array('_redirect');
  104. if (!empty($this->Controller->viewVars['_serialize'])) {
  105. $serializeKeys = array_merge($serializeKeys, $this->Controller->viewVars['_serialize']);
  106. }
  107. $this->Controller->set('_serialize', $serializeKeys);
  108. return false;
  109. }
  110. }