Go to the documentation of this file.00001 <?php
00010 abstract class module_abstract extends object {
00011
00017 protected $prmExec;
00018
00024 protected $tpl;
00025
00026 protected function afterInit() {
00027 if ($this->cfg->forceSecure)
00028 request::forceSecure();
00029
00030 if (!$this->cfg->enabled)
00031 throw new module_exception('Module '.$this->getName().' disabled.');
00032 }
00033
00040 final public function exec(array $prm=array()) {
00041 $this->prmExec = array_merge(array(
00042 'module'=>$this->getName(),
00043 'action'=>'index',
00044 'param'=>'',
00045 'paramA'=>null,
00046 'prefix'=>null),
00047 $prm);
00048
00049 $this->prmExec['prefix'] = null;
00050
00051 if (array_key_exists(NYROENV, $this->cfg->basicPrefixExec) &&
00052 in_array($this->prmExec['action'], $this->cfg->getInArray('basicPrefixExec', NYROENV)))
00053 $this->prmExec['prefix'] = ucfirst(NYROENV);
00054 else if ($this->cfg->prefixExec && !in_array($this->prmExec['action'], $this->cfg->noPrefixExec))
00055 $this->prmExec['prefix'] = $this->cfg->prefixExec;
00056
00057 $this->beforeExec($prm);
00058
00059 if (!$this->cfg->render)
00060 security::getInstance()->check($this->prmExec);
00061
00062 $fctName = ($this->cfg->render? 'render' : 'exec').$this->prmExec['prefix'].ucfirst($this->prmExec['action']);
00063 if (!method_exists($this, $fctName))
00064 response::getInstance()->error();
00065
00066 $this->setViewAction($this->prmExec['action']);
00067
00068 $param = is_array($this->prmExec['paramA'])? $this->prmExec['paramA'] : request::parseParam($this->prmExec['param']);
00069
00070 $tags = $this->cfg->cacheTags;
00071 $search = array('/', '<', '>');
00072 $replace = array('', '', '');
00073 if (is_array($this->prmExec['paramA']))
00074 foreach($this->prmExec['paramA'] as $k=>$v) {
00075 if (!is_numeric($k))
00076 $tags[] = $k.'='.str_replace($search, $replace, $v);
00077 else
00078 $tags[] = str_replace($search, $replace, $v);
00079 }
00080
00081 $paramTpl = array();
00082 foreach($param as $k=>$v) {
00083 $paramTpl[] = $k.':'.$v;
00084 }
00085
00086 $conf = array(
00087 'layout'=>$this->cfg->layout,
00088 'module'=>$this->getName(),
00089 'action'=>$this->cfg->viewAction,
00090 'param'=>implode(',', $paramTpl),
00091 'cache'=>array_merge(array(
00092 'enabled'=>$this->isCacheEnabled(),
00093 'serialize'=>false,
00094 'tags'=>$tags,
00095 'request'=>array('uri'=>false, 'meth'=>array())
00096 ), $this->cfg->cache)
00097 );
00098 $this->tpl = factory::get('tpl', array_merge_recursive($conf, $this->cfg->tplPrm));
00099 $this->tpl->getCfg()->layout = $this->cfg->layout;
00100 $this->tpl->getCfg()->module = $this->getName();
00101 $this->tpl->getCfg()->action = $this->cfg->viewAction;
00102 $this->tpl->getCfg()->default = $this->cfg->viewAction;
00103
00104 $this->prmExec['callbackPrm'] = array(
00105 'fctName'=>$fctName,
00106 'fctNameParam'=>$param,
00107 'prm'=>$prm
00108 );
00109 $this->middleExec($prm);
00110 }
00111
00112 public function callbackTpl(array $prm) {
00113 $this->$prm['fctName']($prm['fctNameParam']);
00114 $this->afterExec($prm['prm']);
00115 return $this->getViewAction();
00116 }
00117
00118 public function getReponse() {
00119 return $this->tpl->getResponseProxy();
00120 }
00121
00125 protected function beforeExec($realExec) {}
00126
00130 protected function middleExec($realExec) {}
00131
00135 protected function afterExec($realExec) {}
00136
00142 protected function getViewAction() {
00143 return $this->cfg->viewAction;
00144 }
00145
00151 protected function setViewAction($action) {
00152 $this->cfg->viewAction = $action;
00153 if ($this->tpl) {
00154 $this->tpl->getCfg()->action = $this->cfg->viewAction;
00155 $this->tpl->getCfg()->default = $this->cfg->viewAction;
00156 }
00157 }
00158
00165 protected function getViewVar($name) {
00166 return $this->tpl->get($name);
00167 }
00168
00175 protected function setViewVar($name, $value) {
00176 $this->tpl->set($name, $value);
00177 }
00178
00184 protected function setViewVars(array $values) {
00185 $this->tpl->setA($values);
00186 }
00187
00193 public function publish(array $prm = array()) {
00194 if (!$this->cfg->viewAction)
00195 return null;
00196 $this->preFetch();
00197 return $this->tpl->fetch(array_merge(array(
00198 'callback'=>array($this, 'callbackTpl'),
00199 'callbackPrm'=>$this->prmExec['callbackPrm'],
00200 ), $prm));
00201 }
00202
00207 protected function preFetch() {}
00208
00214 protected function isCacheEnabled() {
00215 $url = $this->prmExec;
00216 if (utils::isContained($url, $this->cfg->noCache))
00217 return false;
00218 if (utils::isContained($url, $this->cfg->forceCache))
00219 return true;
00220 return $this->cfg->defaultCache;
00221 }
00222
00228 protected function addCacheTag($val) {
00229 $this->cfg->setInArrayA('cacheTags', array($val));
00230 if ($this->tpl) {
00231 $this->tpl->getCfg()->setInarray('cache', 'tags', $this->cfg->cacheTags);
00232 }
00233 }
00234
00240 public function getName() {
00241 return utils::getModuleName(get_class($this));
00242 }
00243
00244 }