Пример #1
0
 /**
  * Try to get an output cached. If not found, information will be stored
  * and used with the next call from end.
  * The cache id is made with 5 kinds :
  *  - the $id passed to the function
  *  - the get, post, session or cookie variable if set
  *  - the class and function name where the cache is call
  *  - the tags if set
  *  - 'cache' is added (to differenciate from variable caching)
  *
  * @param array $prm Parameter for the cached variable:
  *  - int ttl: Time to live, in minutes, 0 for eternal (default: 60)
  *  - string id: Cache id (required)
  *  - array tags: Optionnal tags for the id
  *  - array request: Array for build the request ID (@see cache::idRequest)
  * @return bool True if cache found and content printed
  * @see end
  */
 public function start(array $prm)
 {
     if ($this->isEnabled() && config::initTab($prm, array('ttl' => $this->cfg->ttl, 'id' => null, 'tags' => $this->cfg->tags, 'request' => $this->cfg->request))) {
         $prm['file'] = $this->file(array_merge($prm, array('callFrom' => debug::callFrom(2), 'type' => 'cache')));
         if (file::exists($prm['file']) && ($prm['ttl'] == 0 || file::date($prm['file']) + $prm['ttl'] * 60 > time())) {
             echo file::read($prm['file']);
             return true;
         } else {
             $this->obSave = '';
             /*
             				if (ob_get_length()) {
             					$this->obSave = ob_get_contents();
             					ob_end_clean();
             				}
             				// */
             ob_start();
             $this->prmOut = $prm;
         }
     }
     return false;
 }
Пример #2
0
 protected function afterInit()
 {
     $this->table = $this->cfg->table;
     if (!is_array($this->cfg->query)) {
         $this->cfg->query = array();
     }
     if (empty($this->cfg->module)) {
         $this->cfg->module = utils::getModuleName(debug::callFrom(6, null));
     }
     if ($this->cfg->useSession) {
         $this->session = session::getInstance(array('nameSpace' => 'dataTable_' . ($this->cfg->sessionName ? $this->cfg->sessionName : $this->cfg->name)));
     }
     $paramFromRequest = array('page', 'sortBy', 'sortDir');
     $paramA = request::get('paramA');
     foreach ($paramFromRequest as $pfr) {
         if ($this->cfg->useSession && $this->session->check($pfr)) {
             $this->cfg->set($pfr, $this->session->get($pfr));
         }
         if (array_key_exists($pfr . $this->cfg->nameParam, $paramA)) {
             $val = $paramA[$pfr . $this->cfg->nameParam];
             if ($this->cfg->useSession) {
                 $this->session->set(array('name' => $pfr, 'val' => $val));
             }
             $this->cfg->set($pfr, $val);
         }
     }
     if ($this->cfg->check('sortBy')) {
         if ($this->table->isRelated($this->cfg->sortBy)) {
             $related = $this->table->getRelated($this->cfg->sortBy);
             $tmp = array();
             $fields = array_filter(explode(',', $related['fk2']['link']['fields']));
             $tableName = $related['fk2']['link']['table'];
             foreach ($fields as $f) {
                 $tmp[] = $tableName . '.' . $f;
             }
             $fields = array_filter(explode(',', $related['fk2']['link']['i18nFields']));
             $tableName .= db::getCfg('i18n');
             foreach ($fields as $f) {
                 $tmp[] = $tableName . '.' . $f;
             }
             $this->sortBy = implode(', ', $tmp);
             if (!$this->table->getCfg()->autoJoin) {
                 $f = $related['tableObj']->getRawName();
                 $query = $this->cfg->query;
                 if (!isset($query['join'])) {
                     $query['join'] = array();
                 }
                 $query['join'][] = array('table' => $f, 'dir' => 'left outer', 'on' => $this->table->getRawName() . '.' . $related['fk1']['link']['ident'] . '=' . $f . '.' . $related['fk1']['name']);
                 $query['join'][] = array('table' => $related['table'], 'dir' => 'left outer', 'on' => $f . '.' . $related['fk2']['name'] . '=' . $related['table'] . '.' . $related['fk2']['link']['ident']);
                 if ($related['fk2']['link']['i18nFields']) {
                     $i18nTableName = $related['table'] . db::getCfg('i18n');
                     $i18nTable = db::get('table', $i18nTableName, array('db' => $this->table->getDb()));
                     $primary = $i18nTable->getPrimary();
                     $query['join'][] = array('table' => $i18nTableName, 'dir' => 'left outer', 'on' => $f . '.' . $related['fk2']['name'] . '=' . $i18nTableName . '.' . $primary[0] . ' AND ' . $i18nTableName . '.' . $primary[1] . '="' . request::get('lang') . '"');
                 }
                 $query['group'] = $this->table->getRawName() . '.' . $this->table->getIdent();
                 $this->cfg->query = $query;
             }
         } else {
             if ($this->table->isLinked($this->cfg->sortBy)) {
                 $linked = $this->table->getLinked($this->cfg->sortBy);
                 $tmpSort = array();
                 foreach (explode(',', trim($linked['fields'] . ',' . $linked['i18nFields'], ',')) as $tmp) {
                     $tmpSort[] = $linked['field'] . '.' . $tmp;
                 }
                 $this->sortBy = implode(', ', $tmpSort);
                 if (!$this->table->getCfg()->autoJoin) {
                     $query = $this->cfg->query;
                     if (!isset($query['join'])) {
                         $query['join'] = array();
                     }
                     $alias = $linked['field'];
                     if ($linked['i18nFields']) {
                         $alias1 = $alias . '1';
                         $query['join'][] = array('table' => $linked['table'], 'alias' => $alias1, 'dir' => 'left outer', 'on' => $this->table->getRawName() . '.' . $linked['field'] . '=' . $alias1 . '.' . $linked['ident']);
                         $i18nTableName = $linked['table'] . db::getCfg('i18n');
                         $i18nTable = db::get('table', $i18nTableName, array('db' => $this->table->getDb()));
                         $primary = $i18nTable->getPrimary();
                         $query['join'][] = array('table' => $i18nTableName, 'alias' => $alias, 'dir' => 'left outer', 'on' => $alias1 . '.' . $linked['ident'] . '=' . $alias . '.' . $primary[0] . ' AND ' . $alias . '.' . $primary[1] . '="' . request::get('lang') . '"');
                     } else {
                         $query['join'][] = array('table' => $linked['table'], 'alias' => $alias, 'dir' => 'left outer', 'on' => $this->table->getRawName() . '.' . $linked['field'] . '=' . $alias . '.' . $linked['ident']);
                     }
                     $this->cfg->query = $query;
                 }
             } else {
                 if ($this->cfg->sortBy) {
                     if (strpos($this->cfg->sortBy, $this->table->getName()) !== false || strpos($this->cfg->sortBy, ".") !== false) {
                         $this->sortBy = $this->cfg->sortBy;
                     } else {
                         $this->sortBy = $this->table->getName() . '.' . $this->cfg->sortBy;
                     }
                 }
             }
         }
     }
 }