nyroFwk  0.2
nyro/module/scaffold/controller.class.php
Go to the documentation of this file.
1 <?php
11 
17  protected $table = null;
18 
24  protected $row = null;
25 
31  protected $filterTable;
32 
38  protected $dataTable;
39 
45  protected $form = null;
46 
52  protected $cols;
53 
59  protected $related;
60 
67  protected $fields;
68 
74  protected $indexPage;
75 
81  protected $ids;
82 
83  protected function afterInit() {
84  parent::afterInit();
85 
86  if ($this->getName() != 'scaffold')
87  $this->cfg->name = $this->getName();
88 
89  if (!empty($this->cfg->name)) {
90  $this->cfg->overload('module_scaffold_'.$this->cfg->name);
91 
92  $this->table = db::get('table', $this->cfg->name, array(
93  'name'=>$this->cfg->name
94  ));
95  $this->cols = $this->table->getCols();
96  $this->related = array_keys($this->table->getRelated());
97  $this->fields = $this->table->getField();
98  $this->indexPage = request::uriDef(array('action'=>'', 'param'=>''));
99  }
100 
101  $this->cfg->tplPrm = array(
102  'layout'=>$this->cfg->layout,
103  'module'=>'scaffold',
104  'action'=>$this->cfg->name.ucfirst($this->cfg->viewAction),
105  'defaultModule'=>'scaffold',
106  'default'=>$this->cfg->viewAction,
107  'cache'=>$this->cfg->cache
108  );
109  }
110 
111  protected function execIndex($prm=null) {
112  $this->setViewAction('list');
113  return $this->execScaffoldList($prm);
114  }
115 
116  protected function execShow($prm = null) {
117  return $this->execScaffoldShow($prm);
118  }
119 
120  protected function execScaffoldIndex($prm = null) {
121  if ($this->isScaffolded()) {
122  if (empty($this->cfg->name)) {
123  $db = db::getInstance();
124  $tables = $db->getTables();
125  $links = array();
126  foreach($tables as $t) {
127  if (!strpos($t, '_') && !strpos($t, db::getCfg('i18n')))
128  $links[$t] = request::uriDef(array('module'=>$t, 'action'=>'', 'param'=>''));
129  }
130  $this->setViewVar('links', $links);
131  } else {
132  $this->setViewAction('list');
133  return $this->execScaffoldList($prm);
134  }
135  }
136  }
137 
138  protected function isScaffolded() {
139  return strtolower($this->prmExec['prefix']) == 'scaffold';
140  }
141 
142  protected function execScaffoldList($prm = null) {
143  $iconType = $this->cfg->iconType ? $this->cfg->iconType : $this->cfg->name;
144 
145  $this->filterTable = null;
146  $query = null;
147  if (!empty($this->cfg->filter)) {
148  $this->filterTable = factory::getHelper('filterTable', array_merge($this->cfg->filterOpts, array(
149  'table'=>$this->table,
150  'fields'=>is_array($this->cfg->filter)? $this->cfg->filter : null,
151  )));
152  if ($this->cfg->addFilterTableJs)
153  response::getInstance()->addJs('filterTable');
154  if ($this->filterTable->hasValues())
155  $this->filterTable->getForm()->getCfg()->formPlus = str_replace('class="', 'class="filterTableActive ', $this->filterTable->getForm()->getCfg()->formPlus);
156  $this->hook('listFilter');
157  $query = array('where'=>$this->updateFilterWhere($this->filterTable->getWhere()));
158  }
159 
160  $conf = array(
161  'table'=>$this->table,
162  'query'=>$query,
163  'name'=>$this->cfg->name.'DataTable',
164  'iconType'=>$iconType,
165  'cache'=>$this->cfg->cache,
166  'fields'=>$this->cfg->list,
167  'actions'=>array(
168  'show'=>request::uriDef(array('action'=>'show', 'param'=>'[id]')),
169  'edit'=>request::uriDef(array('action'=>'edit', 'param'=>'[id]')),
170  'delete'=>request::uriDef(array('action'=>'delete', 'param'=>'[id]')),
171  ),
172  'actionsAlt'=>array(
173  'show'=>tr::__('scaffold_show'),
174  'edit'=>tr::__('scaffold_goEdit'),
175  'delete'=>tr::__('scaffold_delete'),
176  ),
177  'multiple'=>$this->cfg->multiple,
178  'multipleAction'=>$this->cfg->multipleAction,
179  );
180 
181  if ($this->cfg->multipleDelete) {
182  $conf['multiple'] = array_merge($conf['multiple'], array(
183  'delete'=>array(
184  'label'=>tr::__('scaffold_delete'),
185  )
186  ));
187  }
188 
189  factory::mergeCfg($conf, $this->cfg->listPrm);
190  $this->dataTable = factory::getHelper('dataTable', $conf);
191 
192  $this->hook('list');
193 
194  $this->setViewVars(array(
195  'filterTable'=>$this->filterTable,
196  'dataTable'=>$this->dataTable,
197  'iconType'=>$iconType,
198  'allowAdd'=>$this->cfg->allowAdd,
199  'addPage'=>request::uriDef(array('action'=>'add', 'param'=>''))
200  ));
201  }
202 
203  protected function updateFilterWhere(db_where $where = null) {
204  return $where;
205  }
206 
207  protected function execScaffoldMultiple() {
208  $action = http_vars::getInstance()->post('action');
209  if ($action) {
210  $fctName = 'multiple'.ucfirst($action);
211  $uAction = 'Multiple'.ucfirst($action);
212  $this->ids = http_vars::getInstance()->post($this->table->getIdent());
213  $this->hook('before'.$uAction);
214  $actionConf = $this->cfg->getInArray('multiple', $action);
215  $call = null;
216  if (is_array($actionConf) && isset($actionConf['callback']) && is_callable($actionConf['callback'])) {
217  $call = $actionConf['callback'];
218  } else if (is_callable(array($this, $fctName)))
219  $call = array($this, $fctName);
220  if (!is_null($call))
221  call_user_func($call, $this->ids);
222  $this->hook('after'.$uAction);
223  }
224  response::getInstance()->redirect($this->indexPage);
225  }
226 
232  protected function multipleDelete(array $ids) {
233  $this->table->delete($this->table->getWhere(array(
234  'clauses'=>factory::get('db_whereClause', array(
235  'name'=>$this->table->getRawName().'.'.$this->table->getIdent(),
236  'in'=>$ids
237  ))
238  )));
239  }
240 
254  protected function hook($action) {}
255 
256  protected function execScaffoldShow($prm = null) {
257  $id = $prm[0];
258 
259  $this->row = $this->table->find($id);
260  $this->hook('show');
261 
262  $this->form = $this->row->getForm($this->getFields('show'), array('mode'=>'view', 'sectionName'=>tr::__('scaffold_show')), false);
263  $this->form->action = request::uriDef(array('module'=>$this->table->getName(),'action'=>'edit', 'param'=>$id));
264  $this->form->method = 'get';
265  $this->form->setSubmitText(tr::__('scaffold_goEdit'));
266  $this->form->setSubmitplus('<a href="'.$this->indexPage.'">'.tr::__('scaffold_back').'</a>');
267 
268  $this->hook('formShow');
269 
270  $this->setViewVars(array(
271  'row'=>$this->row,
272  'form'=>$this->form
273  ));
274  }
275 
276  protected function execScaffoldAdd($prm = null) {
277  if (!$this->cfg->allowAdd) {
278  response::getInstance()->redirect($this->indexPage);
279  }
280  return $this->addEditForm('add');
281  }
282 
283  protected function execScaffoldDuplic($prm = null) {
284  $this->setViewAction('add');
285  return $this->addEditForm('duplic', $prm[0]);
286  }
287 
288  protected function execScaffoldEdit($prm = null) {
289  return $this->addEditForm('edit', $prm[0]);
290  }
291 
292  protected function addEditForm($action, $id = null) {
293  $uAction = ucfirst($action);
294  $this->row = $id ? $this->table->find($id) : $this->table->getRow();
295  if (!$this->row)
296  response::getInstance()->redirect($this->indexPage);
297 
298  if ($action == 'duplic') {
299  $tmp = $this->row;
300  $this->row = $this->table->getRow();
301  $this->row->setValues($tmp->getValues());
302  $this->row->setValues($tmp->getValues('flat'));
303  $action = 'add';
304  }
305 
306  $this->hook($action);
307 
308  $this->form = $this->row->getForm($this->getFields($action), array_merge(array('sectionName'=>tr::__('scaffold_'.$action)), $this->cfg->formOpts));
309  $this->hook('formInit');
310  $this->hook('formInit'.$uAction);
311 
312  if (request::isPost()) {
313  $this->form->refill();
314  $this->hook('formPost'.$uAction);
315  if ($this->form->isValid()) {
316  $this->row->setValues($this->form->getValues());
317  $this->hook('before'.$uAction);
318  if ($this->row->save()) {
319  $this->hook('after'.$uAction);
320  response::getInstance()->redirect($this->indexPage);
321  }
322  } else
323  $this->setViewVar('errors', $this->form->getErrors());
324  }
325 
326  $this->form->setSubmitText(tr::__('scaffold_'.$action));
327  $this->form->setSubmitplus('<a href="'.$this->indexPage.'">'.tr::__('scaffold_back').'</a>');
328 
329  $this->hook('form'.$uAction);
330 
331  $this->setViewVars(array(
332  'row'=>$this->row,
333  'form'=>$this->form
334  ));
335  }
336 
337  protected function execScaffoldDelete($prm = null) {
338  $id = $prm[0];
339  $this->row = $this->table->find($id);
340  $this->hook('delete');
341  $this->hook('beforeDelete');
342  if ($this->row)
343  $this->row->delete();
344  $this->hook('afterDelete');
345  response::getInstance()->redirect($this->indexPage);
346  }
347 
354  protected function getFields($action) {
355  $tmp = $this->cfg->get($action);
356  if (is_array($tmp))
357  return $tmp;
358  $ret = $this->cols;
359  if ($this->cfg->autoRelated)
360  $ret = array_merge($ret, $this->related);
361  if (count($this->table->getI18nFields())) {
362  foreach($this->table->getI18nFields() as $v)
363  $ret[] = db::getCfg('i18n').$v['name'];
364  }
365  return $ret;
366  }
367 
368 }
static __($key, $show=false, $out=true)
Definition: tr.class.php:40
static getHelper($className, array $cfg=array())
getErrors()
Definition: form.class.php:306
isValid()
Definition: form.class.php:278
setViewVar($name, $value)
static getCfg($key)
Definition: db.class.php:125
getValues($onlyFilled=false, $ignoreWhitePassword=true)
Definition: form.class.php:483
refill()
Definition: form.class.php:592
static getInstance()
setSubmitText($text)
Definition: form.class.php:94
static mergeCfg(array &$prm, array $cfg)
static get($type, $table, array $prm=array())
Definition: db.class.php:87
static uriDef(array $prm=array(), array $use=array('lang', 'module', 'action', 'param', 'out'))
static getInstance($cfg=null)
Definition: db.class.php:58
static isPost()
setViewVars(array $values)
static getInstance()
Definition: vars.class.php:29
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13