nyroFwk  0.2
dataTable.class.php
Go to the documentation of this file.
1 <?php
10 class helper_dataTable extends object {
11 
17  protected $table;
18 
24  protected $session;
25 
31  protected $count;
32 
38  protected $sortBy;
39 
45  protected $data;
46 
47  protected function afterInit() {
48  $this->table = $this->cfg->table;
49 
50  if (!is_array($this->cfg->query))
51  $this->cfg->query = array();
52 
53  if (empty($this->cfg->module))
54  $this->cfg->module = utils::getModuleName(debug::callFrom(6, null));
55 
56  if ($this->cfg->useSession)
57  $this->session = session::getInstance(array(
58  'nameSpace'=>'dataTable_'.($this->cfg->sessionName ? $this->cfg->sessionName : $this->cfg->name)
59  ));
60 
61  $paramFromRequest = array('page', 'sortBy', 'sortDir');
62  $paramA = request::get('paramA');
63  foreach($paramFromRequest as $pfr) {
64  if ($this->cfg->useSession && $this->session->check($pfr))
65  $this->cfg->set($pfr, $this->session->get($pfr));
66 
67  if (array_key_exists($pfr.$this->cfg->nameParam, $paramA)) {
68  $val = $paramA[$pfr.$this->cfg->nameParam];
69  if ($this->cfg->useSession)
70  $this->session->set(array(
71  'name'=>$pfr,
72  'val'=>$val
73  ));
74  $this->cfg->set($pfr, $val);
75  }
76  }
77 
78  if ($this->cfg->check('sortBy')) {
79  if ($this->table->isRelated($this->cfg->sortBy)) {
80  $related = $this->table->getRelated($this->cfg->sortBy);
81  $tmp = array();
82 
83  $fields = array_filter(explode(',', $related['fk2']['link']['fields']));
84  $tableName = $related['fk2']['link']['table'];
85  foreach($fields as $f)
86  $tmp[] = $tableName.'.'.$f;
87 
88  $fields = array_filter(explode(',', $related['fk2']['link']['i18nFields']));
89  $tableName.= db::getCfg('i18n');
90  foreach($fields as $f)
91  $tmp[] = $tableName.'.'.$f;
92  $this->sortBy = implode(', ', $tmp);
93  if (!$this->table->getCfg()->autoJoin) {
94  $f = $related['tableObj']->getRawName();
95  $query = $this->cfg->query;
96  if (!isset($query['join']))
97  $query['join'] = array();
98  $query['join'][] = array(
99  'table'=>$f,
100  'dir'=>'left outer',
101  'on'=>$this->table->getRawName().'.'.$related['fk1']['link']['ident'].'='.$f.'.'.$related['fk1']['name']
102  );
103  $query['join'][] = array(
104  'table'=>$related['table'],
105  'dir'=>'left outer',
106  'on'=>$f.'.'.$related['fk2']['name'].'='.$related['table'].'.'.$related['fk2']['link']['ident']
107  );
108 
109  if ($related['fk2']['link']['i18nFields']) {
110  $i18nTableName = $related['table'].db::getCfg('i18n');
111  $i18nTable = db::get('table', $i18nTableName, array('db'=>$this->table->getDb()));
112  $primary = $i18nTable->getPrimary();
113  $query['join'][] = array(
114  'table'=>$i18nTableName,
115  'dir'=>'left outer',
116  'on'=>$f.'.'.$related['fk2']['name'].'='.$i18nTableName.'.'.$primary[0].
117  ' AND '.$i18nTableName.'.'.$primary[1].'="'.request::get('lang').'"'
118  );
119  }
120 
121  $query['group'] = $this->table->getRawName().'.'.$this->table->getIdent();
122  $this->cfg->query = $query;
123  }
124  } else if ($this->table->isLinked($this->cfg->sortBy)) {
125  $linked = $this->table->getLinked($this->cfg->sortBy);
126  $tmpSort = array();
127  foreach(explode(',', trim($linked['fields'].','.$linked['i18nFields'], ',')) as $tmp)
128  $tmpSort[] = $linked['field'].'.'.$tmp;
129  $this->sortBy = implode(', ', $tmpSort);
130  if (!$this->table->getCfg()->autoJoin) {
131  $query = $this->cfg->query;
132  if (!isset($query['join']))
133  $query['join'] = array();
134  $alias = $linked['field'];
135  if ($linked['i18nFields']) {
136  $alias1 = $alias.'1';
137  $query['join'][] = array(
138  'table'=>$linked['table'],
139  'alias'=>$alias1,
140  'dir'=>'left outer',
141  'on'=>$this->table->getRawName().'.'.$linked['field'].'='.$alias1.'.'.$linked['ident']
142  );
143  $i18nTableName = $linked['table'].db::getCfg('i18n');
144  $i18nTable = db::get('table', $i18nTableName, array('db'=>$this->table->getDb()));
145  $primary = $i18nTable->getPrimary();
146  $query['join'][] = array(
147  'table'=>$i18nTableName,
148  'alias'=>$alias,
149  'dir'=>'left outer',
150  'on'=>$alias1.'.'.$linked['ident'].'='.$alias.'.'.$primary[0].
151  ' AND '.$alias.'.'.$primary[1].'="'.request::get('lang').'"'
152  );
153  } else {
154  $query['join'][] = array(
155  'table'=>$linked['table'],
156  'alias'=>$alias,
157  'dir'=>'left outer',
158  'on'=>$this->table->getRawName().'.'.$linked['field'].'='.$alias.'.'.$linked['ident']
159  );
160  }
161  $this->cfg->query = $query;
162  }
163  } else if ($this->cfg->sortBy) {
164  if (strpos($this->cfg->sortBy, $this->table->getName()) !== false || strpos($this->cfg->sortBy, ".") !== false)
165  $this->sortBy = $this->cfg->sortBy;
166  else
167  $this->sortBy = $this->table->getName().'.'.$this->cfg->sortBy;
168  }
169  }
170  }
171 
177  public function getData() {
178  if (is_null($this->data))
179  $this->data = $this->table->select(array_merge(
180  $this->getQuery(),
181  array(
182  'start'=>($this->cfg->page-1)*$this->cfg->nbPerPage,
183  'nb'=>$this->cfg->nbPerPage,
184  'order'=>$this->sortBy ? $this->sortBy.' '.$this->cfg->sortDir : ''
185  )));
186  return $this->data;
187  }
188 
194  public function getCount() {
195  if (is_null($this->count))
196  $this->count = count($this->table->select($this->getQuery()));
197  return $this->count;
198  }
199 
205  public function getNbPage() {
206  $nbPage = ceil($this->getCount() / $this->cfg->nbPerPage);
207  if ($this->cfg->nbPageMax && $nbPage > $this->cfg->nbPageMax)
208  $nbPage = $this->cfg->nbPageMax;
209  return $nbPage;
210  }
211 
217  public function getQuery() {
218  return $this->cfg->query;
219  }
220 
227  public function to($type) {
228  $tpl = factory::get('tpl', array(
229  'module'=>$this->cfg->module,
230  'action'=>$this->cfg->name,
231  'default'=>'dataTable',
232  'cache'=>$this->cfg->cache,
233  'layout'=>false,
234  ));
235 
236  $data = $this->getData();
237 
238  if (count($data)) {
239  if (empty($this->cfg->fields)) {
240  $headersT = $data->getFields('flatReal');
241  if ($keyRelated = array_search('related', $headersT))
242  unset($headersT[$keyRelated]);
243  foreach($this->table->getI18nFields() as $f)
244  $headersT[] = db::getCfg('i18n').$f['name'];
245  } else {
246  $headersT = $this->cfg->fields;
247  if ($this->cfg->addIdentField && !in_array($this->table->getIdent(), $headersT))
248  array_unshift($headersT, $this->table->getIdent());
249  }
250 
251  $headers = array();
252  $prmReplaceSortBy = '[sortBy]';
253  $prmReplaceSortDir = '[sortDir]';
254  $paramUrlA = request::get('paramA');
255  unset($paramUrlA['page'.$this->cfg->nameParam]);
256  $paramUrlA['sortBy'.$this->cfg->nameParam] = $prmReplaceSortBy;
257  $paramUrlA['sortDir'.$this->cfg->nameParam] = $prmReplaceSortDir;
258  $paramUrlA['page'.$this->cfg->nameParam] = 1;
259  $tmpSortLink = request::uriDef(array('paramA'=>$paramUrlA));
260 
261  foreach ($headersT as $k=>$h) {
262  $typeField = $this->table->getField($h, 'type');
263  if ($typeField == 'file' && is_array($tmp = $this->table->getField($h, 'comment')) && array_key_exists(0, $tmp))
264  $typeField = $tmp[0];
265  $headers[$k] = array(
266  'label' => $this->table->getLabel($h),
267  'name' => $h,
268  'url'=>str_replace(
269  array($prmReplaceSortBy, $prmReplaceSortDir),
270  array(
271  db::isI18nName($h)? $this->table->getI18nTable()->getName().'_'.db::unI18nName($h): $h,
272  $this->cfg->sortBy == $h && $this->cfg->sortDir == 'asc'? 'desc' : 'asc'
273  ),
274  $tmpSortLink),
275  'type'=>$typeField
276  );
277  }
278 
279  $actions = null;
280  $actionsAlt = null;
281  $actionsImg = null;
282  if (is_array($this->cfg->actions) && !empty($this->cfg->actions)) {
283  $actions = array();
284  if (!$this->cfg->addIdentField)
285  array_unshift($headersT, $this->table->getIdent());
286  array_walk($headersT, create_function('&$h', '$h = "[".$h."]";'));
287  $dataK = null;
288  $i = 0;
289  foreach($data as $d) {
290  $tmp = $this->getActions($d);
291  $tmpVals = $d->getValues('flatNoRelated');
292  $vals = array();
293  foreach($headersT as $k=>$v) {
294  $v = substr($v, 1, -1);
295  $vals[$k] = array_key_exists($v, $tmpVals) ? $tmpVals[$v] : null;
296  }
297 
298  $curData = $d->getValues('data');
299  unset($curData['related']);
300  unset($curData['linked']);
301  if (is_null($dataK)) {
302  $dataK = array_keys($curData);
303  array_walk($dataK, create_function('&$h', '$h = "[".$h."]";'));
304  }
305  foreach($tmp as &$t)
306  $t = str_replace($dataK, $curData, $t);
307 
308  $actions[$i] = $tmp;
309  $i++;
310  }
311  if (!empty($actions) && $this->cfg->actionsConfirmDelete)
312  response::getInstance()->addJs('actionsConfirmDelete');
313 
314  $actionsKey = array_keys($this->cfg->actions);
315  $actionsAlt = $this->cfg->actionsAlt;
316  if (!is_array($actionsAlt) || count($actionsAlt) < count($actionsKey)) {
317  foreach($actionsKey as $v)
318  if (!array_key_exists($v, $actionsAlt))
319  $actionsAlt[$v] = ucfirst($v);
320  }
321 
322  $actionsImg = $this->cfg->actionsImg;
323  foreach($actionsKey as $v) {
324  if (!array_key_exists($v, $actionsImg))
325  $actionsImg[$v] = utils::getIcon(array(
326  'name'=>$v,
327  'attr'=>array('title'=>$actionsAlt[$v]),
328  'alt'=>$actionsAlt[$v],
329  'type'=>$this->cfg->iconType,
330  ));
331  }
332  }
333 
334  if ($this->cfg->sortBy) {
335  $paramUrlA['sortBy'.$this->cfg->nameParam] = $this->cfg->sortBy;
336  $paramUrlA['sortDir'.$this->cfg->nameParam] = $this->cfg->sortDir;
337  } else {
338  unset($paramUrlA['sortBy'.$this->cfg->nameParam]);
339  unset($paramUrlA['sortDir'.$this->cfg->nameParam]);
340  }
341 
342  $nbPage = $this->getNbPage();
343 
344  $pageLinks = array();
345  $prmReplace = $this->cfg->pageLinkReplace;
346  if (!$this->cfg->pageLinkTpl) {
347  $paramUrlA['page'.$this->cfg->nameParam] = $prmReplace;
348  $tmpPageLink = request::uriDef(array('paramA'=>$paramUrlA));
349  } else
350  $tmpPageLink = $this->cfg->pageLinkTpl;
351  for($i = 1; $i<=$nbPage; $i++)
352  $pageLinks[$i] = str_replace($prmReplace, $i, $tmpPageLink);
353  if ($this->cfg->pageLinkTpl1)
354  $pageLinks[1] = $this->cfg->pageLinkTpl1;
355 
356  $hasMultiple = count($this->cfg->multiple) > 0;
357  if ($hasMultiple && $this->cfg->addCheckAllJs)
358  response::getInstance()->addJs('checkAll');
359 
360  $tpl->setA(array_merge(array(
361  'headers'=>$headers,
362  'list'=>$data,
363  'nbPage'=>$nbPage,
364  'currentPage'=>$this->cfg->page,
365  'pageLinks'=>$pageLinks,
366  'actions'=>$actions,
367  'actionsImg'=>$actionsImg,
368  'actionsAlt'=>$actionsAlt,
369  'iconType'=>$this->cfg->iconType,
370  'tblName'=>$this->table->getName(),
371  'sortBy'=>$this->cfg->sortBy,
372  'sortByReal'=>$this->sortBy,
373  'sortDir'=>$this->cfg->sortDir,
374  'hasMultiple'=>$hasMultiple,
375  'multipleLabel'=>$this->cfg->multipleLabel,
376  'multipleSubmit'=>$this->cfg->multipleSubmit,
377  'multipleAction'=>$this->cfg->multipleAction,
378  'multipleIdent'=>$this->table->getIdent(),
379  'multiple'=>$this->cfg->multiple
380  ), $this->cfg->tplVars));
381  } else {
382  // No data
383  if ($this->cfg->page > 1) {
384  $newPage = $this->cfg->page-1;
385  $uri = null;
386  if ($newPage == 1 && $this->cfg->pageLinkTpl1)
387  $uri = $this->cfg->pageLinkTpl1;
388  if (!$uri) {
389  $prmReplace = $this->cfg->pageLinkReplace;
390  if (!$this->cfg->pageLinkTpl) {
391  $paramUrlA = request::get('paramA');
392  unset($paramUrlA['page'.$this->cfg->nameParam]);
393  $prmReplaceSortBy = '[sortBy]';
394  $prmReplaceSortDir = '[sortDir]';
395  $paramUrlA['sortBy'.$this->cfg->nameParam] = $prmReplaceSortBy;
396  $paramUrlA['sortDir'.$this->cfg->nameParam] = $prmReplaceSortDir;
397  $paramUrlA['page'.$this->cfg->nameParam] = 1;
398 
399  if ($this->cfg->sortBy) {
400  $paramUrlA['sortBy'.$this->cfg->nameParam] = $this->cfg->sortBy;
401  $paramUrlA['sortDir'.$this->cfg->nameParam] = $this->cfg->sortDir;
402  } else {
403  unset($paramUrlA['sortBy'.$this->cfg->nameParam]);
404  unset($paramUrlA['sortDir'.$this->cfg->nameParam]);
405  }
406  $paramUrlA['page'.$this->cfg->nameParam] = $prmReplace;
407  $tmpPageLink = request::uriDef(array('paramA'=>$paramUrlA));
408  } else
409  $tmpPageLink = $this->cfg->pageLinkTpl;
410  $uri = str_replace($prmReplace, $newPage, $tmpPageLink);
411  }
412  response::getInstance()->redirect($uri);
413  }
414  $tpl->set('noData', utils::htmlOut($this->cfg->noData));
415  $tpl->set('list', null);
416  $tpl->setA($this->cfg->tplVars);
417  }
418  return $tpl->fetch(array('tplExt'=>$type));
419  }
420 
427  protected function getActions($row) {
428  $tmp = $this->cfg->actions;
429  $val = null;
430  if (is_callable($this->cfg->actionsAllowed))
431  $val = call_user_func($this->cfg->actionsAllowed, $row);
432  if (!$val)
433  $val = $this->cfg->actionsAllowedDefault;
434  if (is_array($val)) {
435  $tmp2 = array();
436  foreach($val as $v) {
437  if (isset($tmp[$v]))
438  $tmp2[$v] = $tmp[$v];
439  }
440  $tmp = $tmp2;
441  }
442  return $tmp;
443  }
444 
450  public function toHtml() {
451  return $this->to('html');
452  }
453 
459  public function __toString() {
460  try {
461  return $this->to(request::get('out'));
462  } catch (Exception $e) {
463  if (DEV) {
464  print_r($e); exit;
465  debug::trace($e, 2);
466  } else
467  throw $e;
468  }
469  }
470 
471 }
static callFrom($nb=1, $sep='-')
Definition: debug.class.php:38
static get($get=null)
static htmlOut($val, $key=false)
static getIcon(array $prm)
static getInstance(array $cfg=array())
static getCfg($key)
Definition: db.class.php:125
static getInstance()
static unI18nName($field)
Definition: db.class.php:145
static getModuleName($className)
static isI18nName($field)
Definition: db.class.php:135
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'))
$f
Definition: list.php:6
static get($className, array $cfg=array())
static trace($obj, $printExit=false)
Definition: debug.class.php:54
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13