AjaxComponent.php 3.5 KB

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