48 $this->table = $this->cfg->table;
50 if (!is_array($this->cfg->query))
51 $this->cfg->query = array();
53 if (empty($this->cfg->module))
56 if ($this->cfg->useSession)
58 'nameSpace'=>
'dataTable_'.($this->cfg->sessionName ? $this->cfg->sessionName : $this->cfg->name)
61 $paramFromRequest = array(
'page',
'sortBy',
'sortDir');
63 foreach($paramFromRequest as $pfr) {
64 if ($this->cfg->useSession && $this->session->check($pfr))
65 $this->cfg->set($pfr, $this->session->get($pfr));
67 if (array_key_exists($pfr.$this->cfg->nameParam, $paramA)) {
68 $val = $paramA[$pfr.$this->cfg->nameParam];
69 if ($this->cfg->useSession)
74 $this->cfg->set($pfr, $val);
78 if ($this->cfg->check(
'sortBy')) {
79 if ($this->table->isRelated($this->cfg->sortBy)) {
80 $related = $this->table->getRelated($this->cfg->sortBy);
83 $fields = array_filter(explode(
',', $related[
'fk2'][
'link'][
'fields']));
84 $tableName = $related[
'fk2'][
'link'][
'table'];
85 foreach($fields as
$f)
86 $tmp[] = $tableName.
'.'.
$f;
88 $fields = array_filter(explode(
',', $related[
'fk2'][
'link'][
'i18nFields']));
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(
101 'on'=>$this->table->getRawName().
'.'.$related[
'fk1'][
'link'][
'ident'].
'='.$f.
'.'.$related[
'fk1'][
'name']
103 $query[
'join'][] = array(
104 'table'=>$related[
'table'],
106 'on'=>$f.
'.'.$related[
'fk2'][
'name'].
'='.$related[
'table'].
'.'.$related[
'fk2'][
'link'][
'ident']
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,
116 'on'=>$f.
'.'.$related[
'fk2'][
'name'].
'='.$i18nTableName.
'.'.$primary[0].
117 ' AND '.$i18nTableName.
'.'.$primary[1].
'="'.
request::get(
'lang').
'"' 121 $query[
'group'] = $this->table->getRawName().
'.'.$this->table->getIdent();
122 $this->cfg->query = $query;
124 }
else if ($this->table->isLinked($this->cfg->sortBy)) {
125 $linked = $this->table->getLinked($this->cfg->sortBy);
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'],
141 'on'=>$this->table->getRawName().
'.'.$linked[
'field'].
'='.$alias1.
'.'.$linked[
'ident']
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,
150 'on'=>$alias1.
'.'.$linked[
'ident'].
'='.$alias.
'.'.$primary[0].
151 ' AND '.$alias.
'.'.$primary[1].
'="'.
request::get(
'lang').
'"' 154 $query[
'join'][] = array(
155 'table'=>$linked[
'table'],
158 'on'=>$this->table->getRawName().
'.'.$linked[
'field'].
'='.$alias.
'.'.$linked[
'ident']
161 $this->cfg->query = $query;
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;
167 $this->sortBy = $this->table->getName().
'.'.$this->cfg->sortBy;
178 if (is_null($this->data))
179 $this->data = $this->table->select(array_merge(
182 'start'=>($this->cfg->page-1)*$this->cfg->nbPerPage,
183 'nb'=>$this->cfg->nbPerPage,
184 'order'=>$this->sortBy ? $this->sortBy.
' '.$this->cfg->sortDir :
'' 195 if (is_null($this->count))
196 $this->count = count($this->table->select($this->getQuery()));
206 $nbPage = ceil($this->
getCount() / $this->cfg->nbPerPage);
207 if ($this->cfg->nbPageMax && $nbPage > $this->cfg->nbPageMax)
208 $nbPage = $this->cfg->nbPageMax;
218 return $this->cfg->query;
227 public function to($type) {
229 'module'=>$this->cfg->module,
230 'action'=>$this->cfg->name,
231 'default'=>
'dataTable',
232 'cache'=>$this->cfg->cache,
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)
246 $headersT = $this->cfg->fields;
247 if ($this->cfg->addIdentField && !in_array($this->table->getIdent(), $headersT))
248 array_unshift($headersT, $this->table->getIdent());
252 $prmReplaceSortBy =
'[sortBy]';
253 $prmReplaceSortDir =
'[sortDir]';
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;
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),
269 array($prmReplaceSortBy, $prmReplaceSortDir),
272 $this->cfg->sortBy == $h && $this->cfg->sortDir ==
'asc'?
'desc' :
'asc' 282 if (is_array($this->cfg->actions) && !empty($this->cfg->actions)) {
284 if (!$this->cfg->addIdentField)
285 array_unshift($headersT, $this->table->getIdent());
286 array_walk($headersT, create_function(
'&$h',
'$h = "[".$h."]";'));
289 foreach(
$data as $d) {
291 $tmpVals = $d->getValues(
'flatNoRelated');
293 foreach($headersT as $k=>$v) {
294 $v = substr($v, 1, -1);
295 $vals[$k] = array_key_exists($v, $tmpVals) ? $tmpVals[$v] : null;
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."]";'));
306 $t = str_replace($dataK, $curData, $t);
311 if (!empty($actions) && $this->cfg->actionsConfirmDelete)
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);
322 $actionsImg = $this->cfg->actionsImg;
323 foreach($actionsKey as $v) {
324 if (!array_key_exists($v, $actionsImg))
327 'attr'=>array(
'title'=>$actionsAlt[$v]),
328 'alt'=>$actionsAlt[$v],
329 'type'=>$this->cfg->iconType,
334 if ($this->cfg->sortBy) {
335 $paramUrlA[
'sortBy'.$this->cfg->nameParam] = $this->cfg->sortBy;
336 $paramUrlA[
'sortDir'.$this->cfg->nameParam] = $this->cfg->sortDir;
338 unset($paramUrlA[
'sortBy'.$this->cfg->nameParam]);
339 unset($paramUrlA[
'sortDir'.$this->cfg->nameParam]);
344 $pageLinks = array();
345 $prmReplace = $this->cfg->pageLinkReplace;
346 if (!$this->cfg->pageLinkTpl) {
347 $paramUrlA[
'page'.$this->cfg->nameParam] = $prmReplace;
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;
356 $hasMultiple = count($this->cfg->multiple) > 0;
357 if ($hasMultiple && $this->cfg->addCheckAllJs)
360 $tpl->setA(array_merge(array(
364 'currentPage'=>$this->cfg->page,
365 'pageLinks'=>$pageLinks,
367 'actionsImg'=>$actionsImg,
368 'actionsAlt'=>$actionsAlt,
369 'iconType'=>$this->cfg->iconType,
370 'tblName'=>$this->table->getName(),
371 'sortBy'=>$this->cfg->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));
383 if ($this->cfg->page > 1) {
384 $newPage = $this->cfg->page-1;
386 if ($newPage == 1 && $this->cfg->pageLinkTpl1)
387 $uri = $this->cfg->pageLinkTpl1;
389 $prmReplace = $this->cfg->pageLinkReplace;
390 if (!$this->cfg->pageLinkTpl) {
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;
399 if ($this->cfg->sortBy) {
400 $paramUrlA[
'sortBy'.$this->cfg->nameParam] = $this->cfg->sortBy;
401 $paramUrlA[
'sortDir'.$this->cfg->nameParam] = $this->cfg->sortDir;
403 unset($paramUrlA[
'sortBy'.$this->cfg->nameParam]);
404 unset($paramUrlA[
'sortDir'.$this->cfg->nameParam]);
406 $paramUrlA[
'page'.$this->cfg->nameParam] = $prmReplace;
409 $tmpPageLink = $this->cfg->pageLinkTpl;
410 $uri = str_replace($prmReplace, $newPage, $tmpPageLink);
415 $tpl->set(
'list', null);
416 $tpl->setA($this->cfg->tplVars);
418 return $tpl->fetch(array(
'tplExt'=>$type));
428 $tmp = $this->cfg->actions;
430 if (is_callable($this->cfg->actionsAllowed))
431 $val = call_user_func($this->cfg->actionsAllowed, $row);
433 $val = $this->cfg->actionsAllowedDefault;
434 if (is_array($val)) {
436 foreach($val as $v) {
438 $tmp2[$v] = $tmp[$v];
451 return $this->
to(
'html');
462 }
catch (Exception $e) {
static callFrom($nb=1, $sep='-')
static htmlOut($val, $key=false)
static getIcon(array $prm)
static getInstance(array $cfg=array())
static unI18nName($field)
static getModuleName($className)
static isI18nName($field)
static get($type, $table, array $prm=array())
static uriDef(array $prm=array(), array $use=array('lang', 'module', 'action', 'param', 'out'))
static get($className, array $cfg=array())
static trace($obj, $printExit=false)