AjaxComponent.php 3.4 KB

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