File indexing completed on 2024-12-29 05:27:30

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Controller
0017  * @subpackage Zend_Controller_Action_Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Controller_Action_Helper_Abstract
0025  */
0026 // require_once 'Zend/Controller/Action/Helper/Abstract.php';
0027 
0028 /**
0029  * Create and send autocompletion lists
0030  *
0031  * @uses       Zend_Controller_Action_Helper_Abstract
0032  * @category   Zend
0033  * @package    Zend_Controller
0034  * @subpackage Zend_Controller_Action_Helper
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 abstract class Zend_Controller_Action_Helper_AutoComplete_Abstract extends Zend_Controller_Action_Helper_Abstract
0039 {
0040     /**
0041      * Suppress exit when sendJson() called
0042      *
0043      * @var boolean
0044      */
0045     public $suppressExit = false;
0046 
0047     /**
0048      * Validate autocompletion data
0049      *
0050      * @param  mixed $data
0051      * @return boolean
0052      */
0053     abstract public function validateData($data);
0054 
0055     /**
0056      * Prepare autocompletion data
0057      *
0058      * @param  mixed   $data
0059      * @param  boolean $keepLayouts
0060      * @return mixed
0061      */
0062     abstract public function prepareAutoCompletion($data, $keepLayouts = false);
0063 
0064     /**
0065      * Disable layouts and view renderer
0066      *
0067      * @return Zend_Controller_Action_Helper_AutoComplete_Abstract Provides a fluent interface
0068      */
0069     public function disableLayouts()
0070     {
0071         /**
0072          * @see Zend_Layout
0073          */
0074         // require_once 'Zend/Layout.php';
0075         if (null !== ($layout = Zend_Layout::getMvcInstance())) {
0076             $layout->disableLayout();
0077         }
0078 
0079         Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
0080 
0081         return $this;
0082     }
0083 
0084     /**
0085      * Encode data to JSON
0086      *
0087      * @param  mixed $data
0088      * @param  bool  $keepLayouts
0089      * @throws Zend_Controller_Action_Exception
0090      * @return string
0091      */
0092     public function encodeJson($data, $keepLayouts = false)
0093     {
0094         if ($this->validateData($data)) {
0095             return Zend_Controller_Action_HelperBroker::getStaticHelper('Json')->encodeJson($data, $keepLayouts);
0096         }
0097 
0098         /**
0099          * @see Zend_Controller_Action_Exception
0100          */
0101         // require_once 'Zend/Controller/Action/Exception.php';
0102         throw new Zend_Controller_Action_Exception('Invalid data passed for autocompletion');
0103     }
0104 
0105     /**
0106      * Send autocompletion data
0107      *
0108      * Calls prepareAutoCompletion, populates response body with this
0109      * information, and sends response.
0110      *
0111      * @param  mixed $data
0112      * @param  bool  $keepLayouts
0113      * @return string|void
0114      */
0115     public function sendAutoCompletion($data, $keepLayouts = false)
0116     {
0117         $data = $this->prepareAutoCompletion($data, $keepLayouts);
0118 
0119         $response = $this->getResponse();
0120         $response->setBody($data);
0121 
0122         if (!$this->suppressExit) {
0123             $response->sendResponse();
0124             exit;
0125         }
0126 
0127         return $data;
0128     }
0129 
0130     /**
0131      * Strategy pattern: allow calling helper as broker method
0132      *
0133      * Prepares autocompletion data and, if $sendNow is true, immediately sends
0134      * response.
0135      *
0136      * @param  mixed $data
0137      * @param  bool  $sendNow
0138      * @param  bool  $keepLayouts
0139      * @return string|void
0140      */
0141     public function direct($data, $sendNow = true, $keepLayouts = false)
0142     {
0143         if ($sendNow) {
0144             return $this->sendAutoCompletion($data, $keepLayouts);
0145         }
0146 
0147         return $this->prepareAutoCompletion($data, $keepLayouts);
0148     }
0149 }