00001 <?php
00010 class module_scaffold_controller extends module_abstract {
00011
00017 protected $table = null;
00018
00024 protected $row = null;
00025
00031 protected $filterTable;
00032
00038 protected $dataTable;
00039
00045 protected $form = null;
00046
00052 protected $cols;
00053
00059 protected $related;
00060
00067 protected $fields;
00068
00074 protected $indexPage;
00075
00081 protected $ids;
00082
00083 protected function afterInit() {
00084 parent::afterInit();
00085
00086 if ($this->getName() != 'scaffold')
00087 $this->cfg->name = $this->getName();
00088
00089 if (!empty($this->cfg->name)) {
00090 $this->cfg->overload('module_scaffold_'.$this->cfg->name);
00091
00092 $this->table = db::get('table', $this->cfg->name, array(
00093 'name'=>$this->cfg->name
00094 ));
00095 $this->cols = $this->table->getCols();
00096 $this->related = array_keys($this->table->getRelated());
00097 $this->fields = $this->table->getField();
00098 $this->indexPage = request::uriDef(array('action'=>'', 'param'=>''));
00099 }
00100
00101 $this->cfg->tplPrm = array(
00102 'layout'=>$this->cfg->layout,
00103 'module'=>'scaffold',
00104 'action'=>$this->cfg->name.ucfirst($this->cfg->viewAction),
00105 'defaultModule'=>'scaffold',
00106 'default'=>$this->cfg->viewAction,
00107 'cache'=>$this->cfg->cache
00108 );
00109 }
00110
00111 protected function execIndex($prm=null) {
00112 $this->setViewAction('list');
00113 return $this->execScaffoldList($prm);
00114 }
00115
00116 protected function execShow($prm = null) {
00117 return $this->execScaffoldShow($prm);
00118 }
00119
00120 protected function execScaffoldIndex($prm = null) {
00121 if ($this->isScaffolded()) {
00122 if (empty($this->cfg->name)) {
00123 $db = db::getInstance();
00124 $tables = $db->getTables();
00125 $links = array();
00126 foreach($tables as $t) {
00127 if (!strpos($t, '_') && !strpos($t, db::getCfg('i18n')))
00128 $links[$t] = request::uriDef(array('module'=>$t, 'action'=>'', 'param'=>''));
00129 }
00130 $this->setViewVar('links', $links);
00131 } else {
00132 $this->setViewAction('list');
00133 return $this->execScaffoldList($prm);
00134 }
00135 }
00136 }
00137
00138 protected function isScaffolded() {
00139 return strtolower($this->prmExec['prefix']) == 'scaffold';
00140 }
00141
00142 protected function execScaffoldList($prm = null) {
00143 $iconType = $this->cfg->iconType ? $this->cfg->iconType : $this->cfg->name;
00144
00145 $this->filterTable = null;
00146 $query = null;
00147 if (!empty($this->cfg->filter)) {
00148 $this->filterTable = factory::getHelper('filterTable', array_merge($this->cfg->filterOpts, array(
00149 'table'=>$this->table,
00150 'fields'=>is_array($this->cfg->filter)? $this->cfg->filter : null,
00151 )));
00152 if ($this->cfg->addFilterTableJs)
00153 response::getInstance()->addJs('filterTable');
00154 if ($this->filterTable->hasValues())
00155 $this->filterTable->getForm()->getCfg()->formPlus = str_replace('class="', 'class="filterTableActive ', $this->filterTable->getForm()->getCfg()->formPlus);
00156 $this->hook('listFilter');
00157 $query = array('where'=>$this->filterTable->getWhere());
00158 }
00159
00160 $conf = array(
00161 'table'=>$this->table,
00162 'query'=>$query,
00163 'name'=>$this->cfg->name.'DataTable',
00164 'iconType'=>$iconType,
00165 'cache'=>$this->cfg->cache,
00166 'fields'=>$this->cfg->list,
00167 'actions'=>array(
00168 'show'=>request::uriDef(array('action'=>'show', 'param'=>'[id]')),
00169 'edit'=>request::uriDef(array('action'=>'edit', 'param'=>'[id]')),
00170 'delete'=>request::uriDef(array('action'=>'delete', 'param'=>'[id]')),
00171 ),
00172 'actionsAlt'=>array(
00173 'show'=>tr::__('scaffold_show'),
00174 'edit'=>tr::__('scaffold_goEdit'),
00175 'delete'=>tr::__('scaffold_delete'),
00176 ),
00177 'multiple'=>$this->cfg->multiple,
00178 'multipleAction'=>$this->cfg->multipleAction,
00179 );
00180
00181 if ($this->cfg->multipleDelete) {
00182 $conf['multiple'] = array_merge($conf['multiple'], array(
00183 'delete'=>array(
00184 'label'=>tr::__('scaffold_delete'),
00185 )
00186 ));
00187 }
00188
00189 factory::mergeCfg($conf, $this->cfg->listPrm);
00190 $this->dataTable = factory::getHelper('dataTable', $conf);
00191
00192 $this->hook('list');
00193
00194 $this->setViewVars(array(
00195 'filterTable'=>$this->filterTable,
00196 'dataTable'=>$this->dataTable,
00197 'iconType'=>$iconType,
00198 'allowAdd'=>$this->cfg->allowAdd,
00199 'addPage'=>request::uriDef(array('action'=>'add', 'param'=>''))
00200 ));
00201 }
00202
00203 protected function execScaffoldMultiple() {
00204 $action = http_vars::getInstance()->post('action');
00205 if ($action) {
00206 $fctName = 'multiple'.ucfirst($action);
00207 $uAction = 'Multiple'.ucfirst($action);
00208 $this->ids = http_vars::getInstance()->post($this->table->getIdent());
00209 $this->hook('before'.$uAction);
00210 $actionConf = $this->cfg->getInArray('multiple', $action);
00211 $call = null;
00212 if (is_array($actionConf) && isset($actionConf['callback']) && is_callable($actionConf['callback'])) {
00213 $call = $actionConf['callback'];
00214 } else if (is_callable(array($this, $fctName)))
00215 $call = array($this, $fctName);
00216 if (!is_null($call))
00217 call_user_func($call, $this->ids);
00218 $this->hook('after'.$uAction);
00219 }
00220 response::getInstance()->redirect($this->indexPage);
00221 }
00222
00228 protected function multipleDelete(array $ids) {
00229 $this->table->delete($this->table->getWhere(array(
00230 'clauses'=>factory::get('db_whereClause', array(
00231 'name'=>$this->table->getRawName().'.'.$this->table->getIdent(),
00232 'in'=>$ids
00233 ))
00234 )));
00235 }
00236
00250 protected function hook($action) {}
00251
00252 protected function execScaffoldShow($prm = null) {
00253 $id = $prm[0];
00254
00255 $this->row = $this->table->find($id);
00256 $this->hook('show');
00257
00258 $this->form = $this->row->getForm($this->getFields('show'), array('mode'=>'view', 'sectionName'=>tr::__('scaffold_show')), false);
00259 $this->form->action = request::uriDef(array('module'=>$this->table->getName(),'action'=>'edit', 'param'=>$id));
00260 $this->form->method = 'get';
00261 $this->form->setSubmitText(tr::__('scaffold_goEdit'));
00262 $this->form->setSubmitplus('<a href="'.$this->indexPage.'">'.tr::__('scaffold_back').'</a>');
00263
00264 $this->hook('formShow');
00265
00266 $this->setViewVars(array(
00267 'row'=>$this->row,
00268 'form'=>$this->form
00269 ));
00270 }
00271
00272 protected function execScaffoldAdd($prm = null) {
00273 if (!$this->cfg->allowAdd) {
00274 response::getInstance()->redirect($this->indexPage);
00275 }
00276 return $this->addEditForm('add');
00277 }
00278
00279 protected function execScaffoldEdit($prm = null) {
00280 return $this->addEditForm('edit', $prm[0]);
00281 }
00282
00283 protected function addEditForm($action, $id = null) {
00284 $uAction = ucfirst($action);
00285 $this->row = $id ? $this->table->find($id) : $this->table->getRow();
00286 if (!$this->row)
00287 response::getInstance()->redirect($this->indexPage);
00288 $this->hook($action);
00289
00290 $this->form = $this->row->getForm($this->getFields($action), array_merge(array('sectionName'=>tr::__('scaffold_'.$action)), $this->cfg->formOpts));
00291 $this->hook('formInit');
00292 $this->hook('formInit'.$uAction);
00293
00294 if (request::isPost()) {
00295 $this->form->refill();
00296 $this->hook('formPost'.$uAction);
00297 if ($this->form->isValid()) {
00298 $this->row->setValues($this->form->getValues());
00299 $this->hook('before'.$uAction);
00300 if ($this->row->save()) {
00301 $this->hook('after'.$uAction);
00302 response::getInstance()->redirect($this->indexPage);
00303 }
00304 } else
00305 $this->setViewVar('errors', $this->form->getErrors());
00306 }
00307
00308 $this->form->setSubmitText(tr::__('scaffold_'.$action));
00309 $this->form->setSubmitplus('<a href="'.$this->indexPage.'">'.tr::__('scaffold_back').'</a>');
00310
00311 $this->hook('form'.$uAction);
00312
00313 $this->setViewVars(array(
00314 'row'=>$this->row,
00315 'form'=>$this->form
00316 ));
00317 }
00318
00319 protected function execScaffoldDelete($prm = null) {
00320 $id = $prm[0];
00321 $this->row = $this->table->find($id);
00322 $this->hook('delete');
00323 $this->hook('beforeDelete');
00324 if ($this->row)
00325 $this->row->delete();
00326 $this->hook('afterDelete');
00327 response::getInstance()->redirect($this->indexPage);
00328 }
00329
00336 protected function getFields($action) {
00337 $tmp = $this->cfg->get($action);
00338 if (is_array($tmp))
00339 return $tmp;
00340 $ret = $this->cols;
00341 if ($this->cfg->autoRelated)
00342 $ret = array_merge($ret, $this->related);
00343 if (count($this->table->getI18nFields())) {
00344 foreach($this->table->getI18nFields() as $v)
00345 $ret[] = db::getCfg('i18n').$v['name'];
00346 }
00347 return $ret;
00348 }
00349
00350 }