1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
abstract class Action extends \Zend_Controller_Action
{
/**
* Helper to get better code completion
*
* @var \Zend_Controller_Action_Helper_ContextSwitch
*/
protected $contextSwitch;
public function init()
{
parent::init();
$this->contextSwitch = $this->_helper->contextSwitch()
->setContext('html', array(
'suffix' => ''
))
->setAutoDisableLayout(true)
->setAutoJsonSerialization(true)
->setDefaultContext('html');
$this->contextSwitch->initContext();
}
/**
* Returns flash messenger action helper
*
* @return \Zend_Controller_Action_Helper_FlashMessenger
*/
protected function getFlashMessenger()
{
return $this->_helper->getHelper('flashMessenger');
}
/**
* Returns redirector action helper
*
* @return \Zend_Controller_Action_Helper_Redirector
*/
protected function getRedirector()
{
return $this->_helper->getHelper('redirector');
}
/**
* Returns session namespace for the controller
*
* @return \Zend_Session_Namespace
*/
protected function getSessionNamespace()
{
return new \Zend_Session_Namespace(get_class($this));
}
/**
* Returns view object
*
* @return \Zend_View_Abstract
*/
protected function getView()
{
return $this->view;
}
/**
* @param $action
* @param null $controller
* @param null $module
* @param array $params
* @return mixed
*/
protected function redirectToReturnUrlOr($action, $controller = null, $module = null, $params = array())
{
$red = $this->getRedirector()->setCode(303);
if ($this->_getParam('returnurl', false)) {
$red->gotoUrl(RequestUtils::decodeUrlFromParameter($this->_getParam('returnurl')));
return;
}
$red->gotoSimple($action, $controller, $module, $params);
return;
}
/**
* Returns whether the value of the given parameter
* in the request of this controller is empty.
*
* @param string $name
* @return boolean
* @see \Macq\Controller\Request\Utils::paramNotEmpty( $name, $this->getRequest() )
*/
protected final function paramNotEmpty($name)
{
return RequestUtils::paramNotEmpty($name, $this->getRequest());
}
/**
* Returns whether the value of the given parameter
* in the request of this controller represents a real integer.
*
* @param string $name
* @return boolean
* @see \Macq\Controller\Request\Utils::paramRepresentsInt( $name, $this->getRequest() )
*/
protected final function paramRepresentsInt($name)
{
return RequestUtils::paramRepresentsInt($name, $this->getRequest());
}
/**
* Returns the value of the given parameter
* in the request of this controller parsed as integer.
*
* @param string $name
* @return int
* @throws \Zend_Controller_Request_Exception The value for the given parameter cannot be parsed to an integer
* @see \Macq\Controller\Request\Utils::getParamParsedToInt( $name, $this->getRequest() )
*/
protected final function getParamParsedToInt($name)
{
return RequestUtils::getParamParsedToInt($name, $this->getRequest());
}
/**
* Returns whether the value of the given parameter
* in the request of this controller represents a real boolean.
*
* @param string $name
* @return boolean
* @see \Macq\Controller\Request\Utils::paramRepresentsBool( $name, $this->getRequest() )
*/
protected final function paramRepresentsBool($name)
{
return RequestUtils::paramRepresentsBool($name, $this->getRequest());
}
/**
* Returns the value of the given parameter
* in the request of this controller parsed as boolean.
*
* @param string $name
* @return bool
* @throws \Zend_Controller_Request_Exception The value for the given parameter cannot be parsed to a boolean
* @see \Macq\Controller\Request\Utils::getParamParsedToBool( $name, $this->getRequest() )
*/
protected final function getParamParsedToBool($name)
{
return RequestUtils::getParamParsedToBool($name, $this->getRequest());
}
/**
* Returns whether the value of the given parameter
* in the request of this controller represents a real float.
*
* @param string $name
* @return boolean
* @see \Macq\Controller\Request\Utils::paramRepresentsFloat( $name, $this->getRequest() )
*/
protected final function paramRepresentsFloat($name)
{
return RequestUtils::paramRepresentsFloat($name, $this->getRequest());
}
/**
* Returns the value of the given parameter
* in the request of this controller parsed as float.
*
* @param string $name
* @return int
* @throws \Zend_Controller_Request_Exception The value for the given parameter cannot be parsed to a float
* @see \Macq\Controller\Request\Utils::getParamParsedToFloat( $name, $this->getRequest() )
*/
protected final function getParamParsedToFloat($name)
{
return RequestUtils::getParamParsedToFloat($name, $this->getRequest());
}
/**
* Returns the HTTP request of this controller.
*
* @return \Zend_Controller_Request_Http
* @throws \Zend_Controller_Request_Exception The request of this controller is not a HTTP request
*/
protected final function getHttpRequest()
{
if ($this->getRequest() instanceof \Zend_Controller_Request_Http) {
return $this->getRequest();
}
throw new \Zend_Controller_Request_Exception('The request is not a HTTP request');
}
/**
* Returns the given string translated by the translator registered in the Zend_Registry
*
* If there is no translator registered in the Zend_Registry, the given string is returned.
*
* @param string $tag
* @return string
*/
protected final function translate($tag)
{
if (\Zend_Registry::isRegistered(Translator::REGISTRY_KEY)) {
/** @var $translate \Zend_Translate_Adapter */
$translate = \Zend_Registry::get(Translator::REGISTRY_KEY);
return $translate->translate($tag);
}
return $tag;
}
/**
* Returns form from session (if exists), otherwise it returns a fresh instance
*
* @param $form
* @return \Zend_Form
*/
protected function getForm($form)
{
// Fetch form from session if possible
$sessionNamespace = $this->getSessionNamespace();
if (isset($sessionNamespace->form)) {
$form = $sessionNamespace->form;
unset($sessionNamespace->form);
}
// Otherwise, create one under default configuration
else {
$form = new $form();
}
return $form;
}
/**
* Stores form in session.
*
* @param \Zend_Form $form
*/
protected function storeForm(\Zend_Form $form)
{
$this->getSessionNamespace()->form = $form;
}
/**
* @return \Zend_Log
*/
public function getLogger()
{
return \Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('log');
}
} |
Partager