00001 <?php
00010 class helper_dataTable extends object {
00011
00017 protected $table;
00018
00024 protected $session;
00025
00031 protected $count;
00032
00038 protected $sortBy;
00039
00045 protected $data;
00046
00047 protected function afterInit() {
00048 $this->table = $this->cfg->table;
00049
00050 if (!is_array($this->cfg->query))
00051 $this->cfg->query = array();
00052
00053 if (empty($this->cfg->module))
00054 $this->cfg->module = utils::getModuleName(debug::callFrom(6, null));
00055
00056 if ($this->cfg->useSession)
00057 $this->session = session::getInstance(array(
00058 'nameSpace'=>'dataTable_'.($this->cfg->sessionName ? $this->cfg->sessionName : $this->cfg->name)
00059 ));
00060
00061 $paramFromRequest = array('page', 'sortBy', 'sortDir');
00062 $paramA = request::get('paramA');
00063 foreach($paramFromRequest as $pfr) {
00064 if ($this->cfg->useSession && $this->session->check($pfr))
00065 $this->cfg->set($pfr, $this->session->get($pfr));
00066
00067 if (array_key_exists($pfr.$this->cfg->nameParam, $paramA)) {
00068 $val = $paramA[$pfr.$this->cfg->nameParam];
00069 if ($this->cfg->useSession)
00070 $this->session->set(array(
00071 'name'=>$pfr,
00072 'val'=>$val
00073 ));
00074 $this->cfg->set($pfr, $val);
00075 }
00076 }
00077
00078 if ($this->cfg->check('sortBy')) {
00079 if ($this->table->isRelated($this->cfg->sortBy)) {
00080 $related = $this->table->getRelated($this->cfg->sortBy);
00081 $tmp = array();
00082
00083 $fields = array_filter(explode(',', $related['fk2']['link']['fields']));
00084 $tableName = $related['fk2']['link']['table'];
00085 foreach($fields as $f)
00086 $tmp[] = $tableName.'.'.$f;
00087
00088 $fields = array_filter(explode(',', $related['fk2']['link']['i18nFields']));
00089 $tableName.= db::getCfg('i18n');
00090 foreach($fields as $f)
00091 $tmp[] = $tableName.'.'.$f;
00092 $this->sortBy = implode(', ', $tmp);
00093 } else if ($this->cfg->sortBy) {
00094 if (strpos($this->cfg->sortBy, $this->table->getName()) !== false || strpos($this->cfg->sortBy, ".") !== false)
00095 $this->sortBy = $this->cfg->sortBy;
00096 else
00097 $this->sortBy = $this->table->getName().'.'.$this->cfg->sortBy;
00098 }
00099 }
00100 }
00101
00107 public function getData() {
00108 if (is_null($this->data))
00109 $this->data = $this->table->select(array_merge(
00110 $this->getQuery(),
00111 array(
00112 'start'=>($this->cfg->page-1)*$this->cfg->nbPerPage,
00113 'nb'=>$this->cfg->nbPerPage,
00114 'order'=>$this->sortBy ? $this->sortBy.' '.$this->cfg->sortDir : ''
00115 )));
00116 return $this->data;
00117 }
00118
00124 public function getCount() {
00125 if (is_null($this->count))
00126 $this->count = count($this->table->select($this->getQuery()));
00127 return $this->count;
00128 }
00129
00135 public function getNbPage() {
00136 $nbPage = ceil($this->getCount() / $this->cfg->nbPerPage);
00137 if ($this->cfg->nbPageMax && $nbPage > $this->cfg->nbPageMax)
00138 $nbPage = $this->cfg->nbPageMax;
00139 return $nbPage;
00140 }
00141
00147 public function getQuery() {
00148 return $this->cfg->query;
00149 }
00150
00157 public function to($type) {
00158 $tpl = factory::get('tpl', array(
00159 'module'=>$this->cfg->module,
00160 'action'=>$this->cfg->name,
00161 'default'=>'dataTable',
00162 'cache'=>$this->cfg->cache,
00163 'layout'=>false,
00164 ));
00165
00166 $data = $this->getData();
00167
00168 if (count($data)) {
00169 if (empty($this->cfg->fields)) {
00170 $headersT = $data->getFields('flatReal');
00171 if ($keyRelated = array_search('related', $headersT))
00172 unset($headersT[$keyRelated]);
00173 foreach($this->table->getI18nFields() as $f)
00174 $headersT[] = db::getCfg('i18n').$f['name'];
00175 } else {
00176 $headersT = $this->cfg->fields;
00177 if ($this->cfg->addIdentField && !in_array($this->table->getIdent(), $headersT))
00178 array_unshift($headersT, $this->table->getIdent());
00179 }
00180
00181 $headers = array();
00182 $prmReplaceSortBy = '[sortBy]';
00183 $prmReplaceSortDir = '[sortDir]';
00184 $paramUrlA = request::get('paramA');
00185 unset($paramUrlA['page'.$this->cfg->nameParam]);
00186 $paramUrlA['sortBy'.$this->cfg->nameParam] = $prmReplaceSortBy;
00187 $paramUrlA['sortDir'.$this->cfg->nameParam] = $prmReplaceSortDir;
00188 $paramUrlA['page'.$this->cfg->nameParam] = 1;
00189 $tmpSortLink = request::uriDef(array('paramA'=>$paramUrlA));
00190
00191 foreach ($headersT as $k=>$h) {
00192 $typeField = $this->table->getField($h, 'type');
00193 if ($typeField == 'file' && is_array($tmp = $this->table->getField($h, 'comment')) && array_key_exists(0, $tmp))
00194 $typeField = $tmp[0];
00195 $headers[$k] = array(
00196 'label' => $this->table->getLabel($h),
00197 'name' => $h,
00198 'url'=>str_replace(
00199 array($prmReplaceSortBy, $prmReplaceSortDir),
00200 array(
00201 db::isI18nName($h)? $this->table->getI18nTable()->getName().'_'.db::unI18nName($h): $h,
00202 $this->cfg->sortBy == $h && $this->cfg->sortDir == 'asc'? 'desc' : 'asc'
00203 ),
00204 $tmpSortLink),
00205 'type'=>$typeField
00206 );
00207 }
00208
00209 $actions = null;
00210 $actionsAlt = null;
00211 $actionsImg = null;
00212 if (is_array($this->cfg->actions) && !empty($this->cfg->actions)) {
00213 $actions = array();
00214 if (!$this->cfg->addIdentField)
00215 array_unshift($headersT, $this->table->getIdent());
00216 array_walk($headersT, create_function('&$h', '$h = "[".$h."]";'));
00217 $dataK = null;
00218 $i = 0;
00219 foreach($data as $d) {
00220 $tmp = $this->getActions($d);
00221 $tmpVals = $d->getValues('flatNoRelated');
00222 $vals = array();
00223 foreach($headersT as $k=>$v) {
00224 $v = substr($v, 1, -1);
00225 $vals[$k] = array_key_exists($v, $tmpVals) ? $tmpVals[$v] : null;
00226 }
00227
00228 $curData = $d->getValues('data');
00229 unset($curData['related']);
00230 unset($curData['linked']);
00231 if (is_null($dataK)) {
00232 $dataK = array_keys($curData);
00233 array_walk($dataK, create_function('&$h', '$h = "[".$h."]";'));
00234 }
00235 foreach($tmp as &$t)
00236 $t = str_replace($dataK, $curData, $t);
00237
00238 $actions[$i] = $tmp;
00239 $i++;
00240 }
00241 if (!empty($actions) && $this->cfg->actionsConfirmDelete)
00242 response::getInstance()->addJs('actionsConfirmDelete');
00243
00244 $actionsKey = array_keys($this->cfg->actions);
00245 $actionsAlt = $this->cfg->actionsAlt;
00246 if (!is_array($actionsAlt) || count($actionsAlt) < count($actionsKey)) {
00247 foreach($actionsKey as $v)
00248 if (!array_key_exists($v, $actionsAlt))
00249 $actionsAlt[$v] = ucfirst($v);
00250 }
00251
00252 $actionsImg = $this->cfg->actionsImg;
00253 foreach($actionsKey as $v) {
00254 if (!array_key_exists($v, $actionsImg))
00255 $actionsImg[$v] = utils::getIcon(array(
00256 'name'=>$v,
00257 'attr'=>array('title'=>$actionsAlt[$v]),
00258 'alt'=>$actionsAlt[$v],
00259 'type'=>$this->cfg->iconType,
00260 ));
00261 }
00262 }
00263
00264 if ($this->cfg->sortBy) {
00265 $paramUrlA['sortBy'.$this->cfg->nameParam] = $this->cfg->sortBy;
00266 $paramUrlA['sortDir'.$this->cfg->nameParam] = $this->cfg->sortDir;
00267 } else {
00268 unset($paramUrlA['sortBy'.$this->cfg->nameParam]);
00269 unset($paramUrlA['sortDir'.$this->cfg->nameParam]);
00270 }
00271
00272 $nbPage = $this->getNbPage();
00273
00274 $pageLinks = array();
00275 $prmReplace = $this->cfg->pageLinkReplace;
00276 if (!$this->cfg->pageLinkTpl) {
00277 $paramUrlA['page'.$this->cfg->nameParam] = $prmReplace;
00278 $tmpPageLink = request::uriDef(array('paramA'=>$paramUrlA));
00279 } else
00280 $tmpPageLink = $this->cfg->pageLinkTpl;
00281 for($i = 1; $i<=$nbPage; $i++)
00282 $pageLinks[$i] = str_replace($prmReplace, $i, $tmpPageLink);
00283 if ($this->cfg->pageLinkTpl1)
00284 $pageLinks[1] = $this->cfg->pageLinkTpl1;
00285
00286 $hasMultiple = count($this->cfg->multiple) > 0;
00287 if ($hasMultiple)
00288 response::getInstance()->addJs('checkAll');
00289
00290 $tpl->setA(array_merge(array(
00291 'headers'=>$headers,
00292 'list'=>$data,
00293 'nbPage'=>$nbPage,
00294 'currentPage'=>$this->cfg->page,
00295 'pageLinks'=>$pageLinks,
00296 'actions'=>$actions,
00297 'actionsImg'=>$actionsImg,
00298 'actionsAlt'=>$actionsAlt,
00299 'iconType'=>$this->cfg->iconType,
00300 'tblName'=>$this->table->getName(),
00301 'sortBy'=>$this->sortBy,
00302 'sortDir'=>$this->cfg->sortDir,
00303 'hasMultiple'=>$hasMultiple,
00304 'multipleLabel'=>$this->cfg->multipleLabel,
00305 'multipleSubmit'=>$this->cfg->multipleSubmit,
00306 'multipleAction'=>$this->cfg->multipleAction,
00307 'multipleIdent'=>$this->table->getIdent(),
00308 'multiple'=>$this->cfg->multiple
00309 ), $this->cfg->tplVars));
00310 } else {
00311
00312 if ($this->cfg->page > 1) {
00313 $newPage = $this->cfg->page-1;
00314 $uri = null;
00315 if ($newPage == 1 && $this->cfg->pageLinkTpl1)
00316 $uri = $this->cfg->pageLinkTpl1;
00317 if (!$uri) {
00318 $prmReplace = $this->cfg->pageLinkReplace;
00319 if (!$this->cfg->pageLinkTpl) {
00320 $paramUrlA = request::get('paramA');
00321 unset($paramUrlA['page'.$this->cfg->nameParam]);
00322 $prmReplaceSortBy = '[sortBy]';
00323 $prmReplaceSortDir = '[sortDir]';
00324 $paramUrlA['sortBy'.$this->cfg->nameParam] = $prmReplaceSortBy;
00325 $paramUrlA['sortDir'.$this->cfg->nameParam] = $prmReplaceSortDir;
00326 $paramUrlA['page'.$this->cfg->nameParam] = 1;
00327
00328 if ($this->cfg->sortBy) {
00329 $paramUrlA['sortBy'.$this->cfg->nameParam] = $this->cfg->sortBy;
00330 $paramUrlA['sortDir'.$this->cfg->nameParam] = $this->cfg->sortDir;
00331 } else {
00332 unset($paramUrlA['sortBy'.$this->cfg->nameParam]);
00333 unset($paramUrlA['sortDir'.$this->cfg->nameParam]);
00334 }
00335 $paramUrlA['page'.$this->cfg->nameParam] = $prmReplace;
00336 $tmpPageLink = request::uriDef(array('paramA'=>$paramUrlA));
00337 } else
00338 $tmpPageLink = $this->cfg->pageLinkTpl;
00339 $uri = str_replace($prmReplace, $newPage, $tmpPageLink);
00340 }
00341 response::getInstance()->redirect($uri);
00342 }
00343 $tpl->set('noData', utils::htmlOut($this->cfg->noData));
00344 $tpl->set('list', null);
00345 $tpl->setA($this->cfg->tplVars);
00346 }
00347 return $tpl->fetch(array('tplExt'=>$type));
00348 }
00349
00356 protected function getActions($row) {
00357 $tmp = $this->cfg->actions;
00358 $val = null;
00359 if (is_callable($this->cfg->actionsAllowed))
00360 $val = call_user_func($this->cfg->actionsAllowed, $row);
00361 if (!$val)
00362 $val = $this->cfg->actionsAllowedDefault;
00363 if (is_array($val)) {
00364 $tmp2 = array();
00365 foreach($val as $v) {
00366 if (isset($tmp[$v]))
00367 $tmp2[$v] = $tmp[$v];
00368 }
00369 $tmp = $tmp2;
00370 }
00371 return $tmp;
00372 }
00373
00379 public function toHtml() {
00380 return $this->to('html');
00381 }
00382
00388 public function __toString() {
00389 try {
00390 return $this->to(request::get('out'));
00391 } catch (Exception $e) {
00392 if (DEV) {
00393 print_r($e); exit;
00394 debug::trace($e, 2);
00395 } else
00396 throw $e;
00397 }
00398 }
00399
00400 }