AjaxView.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. App::uses('View', 'View');
  3. /**
  4. * A view to handle AJAX requests.
  5. *
  6. * Expects all incoming requests to be of extension "json" and that the expected result
  7. * will also be in JSON format.
  8. * A response to an invalid request may be just HTTP status "code" and error "message"
  9. * (e.g, on 4xx or 5xx).
  10. * A response to a valid request will always contain at least "content" and "error" keys.
  11. * You can add more data using _serialize.
  12. *
  13. * @author Mark Scherer
  14. * @license http://opensource.org/licenses/mit-license.php MIT
  15. * @deprecated Use https://github.com/dereuromark/cakephp-ajax/tree/2.x
  16. */
  17. class AjaxView extends View {
  18. /**
  19. * List of variables to collect from the associated controller.
  20. *
  21. * @var array
  22. */
  23. protected $_passedVars = [
  24. 'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme',
  25. 'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction', 'subDir'
  26. ];
  27. /**
  28. * The subdirectory. AJAX views are always in ajax.
  29. *
  30. * @var string
  31. */
  32. public $subDir = 'ajax';
  33. /**
  34. * Name of layout to use with this View.
  35. *
  36. * @var string
  37. */
  38. public $layout = false;
  39. /**
  40. * Constructor
  41. *
  42. * @param Controller $controller
  43. */
  44. public function __construct(Controller $controller = null) {
  45. parent::__construct($controller);
  46. // Unfortunately, layout gets overwritten via passed Controller attribute
  47. if ($this->layout === 'default' || $this->layout === 'ajax') {
  48. $this->layout = false;
  49. }
  50. if ($this->subDir === null) {
  51. $this->subDir = 'ajax';
  52. }
  53. if (isset($controller->response) && $controller->response instanceof CakeResponse) {
  54. $controller->response->type('json');
  55. }
  56. }
  57. /**
  58. * Renders an AJAX view.
  59. * The rendered content will be part of the JSON response object and
  60. * can be accessed via response.content in JavaScript.
  61. *
  62. * If an error has been set, the rendering will be skipped.
  63. *
  64. * @param string $view The view being rendered.
  65. * @param string $layout The layout being rendered.
  66. * @return string The rendered view.
  67. */
  68. public function render($view = null, $layout = null) {
  69. $response = [
  70. 'error' => null,
  71. 'content' => null,
  72. ];
  73. if (!empty($this->viewVars['error'])) {
  74. $view = false;
  75. }
  76. if ($view !== false && !isset($this->viewVars['_redirect']) && $this->_getViewFileName($view)) {
  77. $response['content'] = parent::render($view, $layout);
  78. }
  79. if (isset($this->viewVars['_serialize'])) {
  80. $response = $this->_serialize($response, $this->viewVars['_serialize']);
  81. }
  82. return json_encode($response);
  83. }
  84. /**
  85. * Serializes view vars.
  86. *
  87. * @param array $response Response data array.
  88. * @param array $serialize The viewVars that need to be serialized.
  89. * @return array The serialized data.
  90. */
  91. protected function _serialize($response, $serialize) {
  92. if (is_array($serialize)) {
  93. foreach ($serialize as $alias => $key) {
  94. if (is_numeric($alias)) {
  95. $alias = $key;
  96. }
  97. if (array_key_exists($key, $this->viewVars)) {
  98. $response[$alias] = $this->viewVars[$key];
  99. }
  100. }
  101. } else {
  102. $response[$serialize] = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  103. }
  104. return $response;
  105. }
  106. }