AjaxContext.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Controller_Action_Helper_ContextSwitch
  24. */
  25. require_once 'Zend/Controller/Action/Helper/ContextSwitch.php';
  26. /**
  27. * Simplify AJAX context switching based on requested format
  28. *
  29. * @uses Zend_Controller_Action_Helper_Abstract
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage Zend_Controller_Action_Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Controller_Action_Helper_AjaxContext extends Zend_Controller_Action_Helper_ContextSwitch
  37. {
  38. /**
  39. * Controller property to utilize for context switching
  40. * @var string
  41. */
  42. protected $_contextKey = 'ajaxable';
  43. /**
  44. * Constructor
  45. *
  46. * Add HTML context
  47. *
  48. * @return void
  49. */
  50. public function __construct()
  51. {
  52. parent::__construct();
  53. $this->addContext('html', array('suffix' => 'ajax'));
  54. }
  55. /**
  56. * Initialize AJAX context switching
  57. *
  58. * Checks for XHR requests; if detected, attempts to perform context switch.
  59. *
  60. * @param string $format
  61. * @return void
  62. */
  63. public function initContext($format = null)
  64. {
  65. $this->_currentContext = null;
  66. $request = $this->getRequest();
  67. if (!method_exists($request, 'isXmlHttpRequest') ||
  68. !$this->getRequest()->isXmlHttpRequest())
  69. {
  70. return;
  71. }
  72. return parent::initContext($format);
  73. }
  74. }