Beispiel #1
0
	protected function execScaffoldShow($prm = null) {
		$id = $prm[0];

		$this->row = $this->table->find($id);
		$this->hook('show');

		$this->form = $this->row->getForm($this->getFields('show'), array('mode'=>'view', 'sectionName'=>tr::__('scaffold_show')), false);
		$this->form->action = request::uriDef(array('module'=>$this->table->getName(),'action'=>'edit', 'param'=>$id));
		$this->form->method = 'get';
		$this->form->setSubmitText(tr::__('scaffold_goEdit'));
		$this->form->setSubmitplus('<a href="'.$this->indexPage.'">'.tr::__('scaffold_back').'</a>');

		$this->hook('formShow');

		$this->setViewVars(array(
			'row'=>$this->row,
			'form'=>$this->form
		));
	}
Beispiel #2
0
 /**
  * Get the where clause
  *
  * @return db_where The where clause
  */
 public function getWhere()
 {
     $where = $this->table->getWhere();
     foreach ($this->form->getValues() as $name => $val) {
         $field = strpos($name, '.') === false ? $this->table->getName() . '.' . $name : $name;
         if (!is_null($val) && ($val || $val === '0')) {
             if (is_array($val)) {
                 if (array_key_exists('min', $val) || array_key_exists('max', $val)) {
                     $min = array_key_exists('min', $val) && !empty($val['min']) ? $val['min'] : null;
                     $max = array_key_exists('max', $val) && !empty($val['max']) ? $val['max'] : null;
                     if ($min) {
                         $where->add(array('field' => $field, 'val' => $min, 'op' => '>='));
                     }
                     if ($max) {
                         $where->add(array('field' => $field, 'val' => $max, 'op' => '<='));
                     }
                 } else {
                     $fieldRel = $this->table->getRelated($name);
                     $where->add(array('field' => $fieldRel ? $fieldRel['tableLink'] . '.' . $fieldRel['fk2']['name'] : $field, 'val' => array_map(array($this->table->getDb(), 'quoteValue'), $val), 'op' => 'IN'));
                 }
             } else {
                 if (strpos($name, '_file')) {
                     $where->add(array('field' => $field, 'val' => '', 'op' => '<>'));
                 } else {
                     $f = $this->table->getField($name);
                     if (is_array($f) && (!array_key_exists('text', $f) || $f['text'])) {
                         $tmp = explode(' ', $val);
                         array_walk($tmp, create_function('&$v', '$v = trim($v);'));
                         $tmp = array_filter($tmp);
                         foreach ($tmp as $t) {
                             $where->add(array('field' => $field, 'val' => '%' . $t . '%', 'op' => 'LIKE'));
                         }
                     } else {
                         if ($this->table->hasI18n() && db::isI18nName($name) && ($f = $this->table->getI18nTable()->getField(db::unI18nName($name)))) {
                             $tblName = $this->table->getI18nTable()->getName();
                             $prim = $this->table->getI18nTable()->getPrimary();
                             $field = $tblName . '.' . db::unI18nName($name);
                             $clause = '(' . $this->table->getName() . '.' . $this->table->getIdent() . ' IN (SELECT ' . $tblName . '.' . $prim[0] . ' FROM ' . $tblName . ' WHERE ';
                             $tmpWhere = $this->table->getI18nTable()->getWhere(array('op' => 'OR'));
                             if (!array_key_exists('text', $f) || $f['text']) {
                                 $tmp = explode(' ', $val);
                                 array_walk($tmp, create_function('&$v', '$v = trim($v);'));
                                 $tmp = array_filter($tmp);
                                 foreach ($tmp as $t) {
                                     $tmpWhere->add(array('field' => $field, 'val' => '%' . $t . '%', 'op' => 'LIKE'));
                                 }
                             } else {
                                 $tmpWhere->add(array('field' => $field, 'val' => $val));
                             }
                             $clause .= $tmpWhere . '))';
                             $where->add($clause);
                         } else {
                             $where->add(array('field' => $field, 'val' => $val));
                         }
                     }
                 }
             }
             $this->session->set(array('name' => $name, 'val' => $val));
         }
     }
     if (count($where)) {
         return $where;
     }
     return null;
 }
Beispiel #3
0
 /**
  * Create the data Table out
  *
  * @param string $type Out type
  * @return string
  */
 public function to($type)
 {
     $tpl = factory::get('tpl', array('module' => $this->cfg->module, 'action' => $this->cfg->name, 'default' => 'dataTable', 'cache' => $this->cfg->cache, 'layout' => false));
     $data = $this->getData();
     if (count($data)) {
         if (empty($this->cfg->fields)) {
             $headersT = $data->getFields('flatReal');
             if ($keyRelated = array_search('related', $headersT)) {
                 unset($headersT[$keyRelated]);
             }
             foreach ($this->table->getI18nFields() as $f) {
                 $headersT[] = db::getCfg('i18n') . $f['name'];
             }
         } else {
             $headersT = $this->cfg->fields;
             if ($this->cfg->addIdentField && !in_array($this->table->getIdent(), $headersT)) {
                 array_unshift($headersT, $this->table->getIdent());
             }
         }
         $headers = array();
         $prmReplaceSortBy = '[sortBy]';
         $prmReplaceSortDir = '[sortDir]';
         $paramUrlA = request::get('paramA');
         unset($paramUrlA['page' . $this->cfg->nameParam]);
         $paramUrlA['sortBy' . $this->cfg->nameParam] = $prmReplaceSortBy;
         $paramUrlA['sortDir' . $this->cfg->nameParam] = $prmReplaceSortDir;
         $paramUrlA['page' . $this->cfg->nameParam] = 1;
         $tmpSortLink = request::uriDef(array('paramA' => $paramUrlA));
         foreach ($headersT as $k => $h) {
             $typeField = $this->table->getField($h, 'type');
             if ($typeField == 'file' && is_array($tmp = $this->table->getField($h, 'comment')) && array_key_exists(0, $tmp)) {
                 $typeField = $tmp[0];
             }
             $headers[$k] = array('label' => $this->table->getLabel($h), 'name' => $h, 'url' => str_replace(array($prmReplaceSortBy, $prmReplaceSortDir), array(db::isI18nName($h) ? $this->table->getI18nTable()->getName() . '_' . db::unI18nName($h) : $h, $this->cfg->sortBy == $h && $this->cfg->sortDir == 'asc' ? 'desc' : 'asc'), $tmpSortLink), 'type' => $typeField);
         }
         $actions = null;
         $actionsAlt = null;
         $actionsImg = null;
         if (is_array($this->cfg->actions) && !empty($this->cfg->actions)) {
             $actions = array();
             if (!$this->cfg->addIdentField) {
                 array_unshift($headersT, $this->table->getIdent());
             }
             array_walk($headersT, create_function('&$h', '$h = "[".$h."]";'));
             $dataK = null;
             $i = 0;
             foreach ($data as $d) {
                 $tmp = $this->getActions($d);
                 $tmpVals = $d->getValues('flatNoRelated');
                 $vals = array();
                 foreach ($headersT as $k => $v) {
                     $v = substr($v, 1, -1);
                     $vals[$k] = array_key_exists($v, $tmpVals) ? $tmpVals[$v] : null;
                 }
                 $curData = $d->getValues('data');
                 unset($curData['related']);
                 unset($curData['linked']);
                 if (is_null($dataK)) {
                     $dataK = array_keys($curData);
                     array_walk($dataK, create_function('&$h', '$h = "[".$h."]";'));
                 }
                 foreach ($tmp as &$t) {
                     $t = str_replace($dataK, $curData, $t);
                 }
                 $actions[$i] = $tmp;
                 $i++;
             }
             if (!empty($actions) && $this->cfg->actionsConfirmDelete) {
                 response::getInstance()->addJs('actionsConfirmDelete');
             }
             $actionsKey = array_keys($this->cfg->actions);
             $actionsAlt = $this->cfg->actionsAlt;
             if (!is_array($actionsAlt) || count($actionsAlt) < count($actionsKey)) {
                 foreach ($actionsKey as $v) {
                     if (!array_key_exists($v, $actionsAlt)) {
                         $actionsAlt[$v] = ucfirst($v);
                     }
                 }
             }
             $actionsImg = $this->cfg->actionsImg;
             foreach ($actionsKey as $v) {
                 if (!array_key_exists($v, $actionsImg)) {
                     $actionsImg[$v] = utils::getIcon(array('name' => $v, 'attr' => array('title' => $actionsAlt[$v]), 'alt' => $actionsAlt[$v], 'type' => $this->cfg->iconType));
                 }
             }
         }
         if ($this->cfg->sortBy) {
             $paramUrlA['sortBy' . $this->cfg->nameParam] = $this->cfg->sortBy;
             $paramUrlA['sortDir' . $this->cfg->nameParam] = $this->cfg->sortDir;
         } else {
             unset($paramUrlA['sortBy' . $this->cfg->nameParam]);
             unset($paramUrlA['sortDir' . $this->cfg->nameParam]);
         }
         $nbPage = $this->getNbPage();
         $pageLinks = array();
         $prmReplace = $this->cfg->pageLinkReplace;
         if (!$this->cfg->pageLinkTpl) {
             $paramUrlA['page' . $this->cfg->nameParam] = $prmReplace;
             $tmpPageLink = request::uriDef(array('paramA' => $paramUrlA));
         } else {
             $tmpPageLink = $this->cfg->pageLinkTpl;
         }
         for ($i = 1; $i <= $nbPage; $i++) {
             $pageLinks[$i] = str_replace($prmReplace, $i, $tmpPageLink);
         }
         if ($this->cfg->pageLinkTpl1) {
             $pageLinks[1] = $this->cfg->pageLinkTpl1;
         }
         $hasMultiple = count($this->cfg->multiple) > 0;
         if ($hasMultiple && $this->cfg->addCheckAllJs) {
             response::getInstance()->addJs('checkAll');
         }
         $tpl->setA(array_merge(array('headers' => $headers, 'list' => $data, 'nbPage' => $nbPage, 'currentPage' => $this->cfg->page, 'pageLinks' => $pageLinks, 'actions' => $actions, 'actionsImg' => $actionsImg, 'actionsAlt' => $actionsAlt, 'iconType' => $this->cfg->iconType, 'tblName' => $this->table->getName(), 'sortBy' => $this->cfg->sortBy, 'sortByReal' => $this->sortBy, 'sortDir' => $this->cfg->sortDir, 'hasMultiple' => $hasMultiple, 'multipleLabel' => $this->cfg->multipleLabel, 'multipleSubmit' => $this->cfg->multipleSubmit, 'multipleAction' => $this->cfg->multipleAction, 'multipleIdent' => $this->table->getIdent(), 'multiple' => $this->cfg->multiple), $this->cfg->tplVars));
     } else {
         // No data
         if ($this->cfg->page > 1) {
             $newPage = $this->cfg->page - 1;
             $uri = null;
             if ($newPage == 1 && $this->cfg->pageLinkTpl1) {
                 $uri = $this->cfg->pageLinkTpl1;
             }
             if (!$uri) {
                 $prmReplace = $this->cfg->pageLinkReplace;
                 if (!$this->cfg->pageLinkTpl) {
                     $paramUrlA = request::get('paramA');
                     unset($paramUrlA['page' . $this->cfg->nameParam]);
                     $prmReplaceSortBy = '[sortBy]';
                     $prmReplaceSortDir = '[sortDir]';
                     $paramUrlA['sortBy' . $this->cfg->nameParam] = $prmReplaceSortBy;
                     $paramUrlA['sortDir' . $this->cfg->nameParam] = $prmReplaceSortDir;
                     $paramUrlA['page' . $this->cfg->nameParam] = 1;
                     if ($this->cfg->sortBy) {
                         $paramUrlA['sortBy' . $this->cfg->nameParam] = $this->cfg->sortBy;
                         $paramUrlA['sortDir' . $this->cfg->nameParam] = $this->cfg->sortDir;
                     } else {
                         unset($paramUrlA['sortBy' . $this->cfg->nameParam]);
                         unset($paramUrlA['sortDir' . $this->cfg->nameParam]);
                     }
                     $paramUrlA['page' . $this->cfg->nameParam] = $prmReplace;
                     $tmpPageLink = request::uriDef(array('paramA' => $paramUrlA));
                 } else {
                     $tmpPageLink = $this->cfg->pageLinkTpl;
                 }
                 $uri = str_replace($prmReplace, $newPage, $tmpPageLink);
             }
             response::getInstance()->redirect($uri);
         }
         $tpl->set('noData', utils::htmlOut($this->cfg->noData));
         $tpl->set('list', null);
         $tpl->setA($this->cfg->tplVars);
     }
     return $tpl->fetch(array('tplExt' => $type));
 }
Beispiel #4
0
 /**
  * Create the right array paremeter for select query
  *
  * @param array $prm The initial parameter
  * @param array $tmpTables Array used with parseLinked
  */
 public function selectQuery(array $prm, &$tmpTables)
 {
     config::initTab($prm, array('where' => '', 'whereOp' => 'AND', 'order' => '', 'autoJoin' => $this->cfg->autoJoin));
     if (is_array($prm['where'])) {
         foreach ($prm['where'] as $k => $v) {
             if (!is_numeric($k) && strpos($k, '.') === false) {
                 $newK = $this->rawName . '.' . $k;
                 if (!array_key_exists($newK, $prm['where'])) {
                     $prm['where'][$newK] = $v;
                     unset($prm['where'][$k]);
                 }
             }
         }
     } else {
         if (!empty($prm['where']) && !is_array($prm['where']) && !is_object($prm['where']) && (strpos($prm['where'], '=') === false && strpos($prm['where'], '<') === false && strpos($prm['where'], '>') === false && stripos($prm['where'], 'LIKE') === false && stripos($prm['where'], 'IN') === false)) {
             $prm['where'] = $this->rawName . '.' . $this->cfg->ident . '=' . $prm['where'];
         }
     }
     $prm = array_merge(array('fields' => $this->getDb()->quoteIdentifier($this->rawName) . '.*', 'table' => $this->rawName), $prm);
     if (is_array($prm['fields'])) {
         array_walk($prm['fields'], create_function('&$v', '$v = strpos($v, ".") === false? "' . $this->rawName . '.".$v: $v;'));
         $prm['fields'] = implode(',', $prm['fields']);
     }
     $join = isset($prm['join']) ? $prm['join'] : array();
     $prm['join'] = array();
     $tmpTables = array();
     if (!empty($this->linkedTables) && $prm['autoJoin']) {
         foreach ($this->linkedTables as $f => $p) {
             $alias = $f;
             $prm['join'][] = array('table' => $p['table'], 'alias' => $alias, 'dir' => 'left outer', 'on' => $this->rawName . '.' . $f . '=' . $alias . '.' . $p['ident']);
             $fields = explode(',', $p['fields']);
             array_unshift($fields, $p['ident']);
             $fields = array_flip(array_flip(array_filter($fields)));
             $fieldsT = $fields;
             array_walk($fieldsT, create_function('&$v', '$v = "' . $alias . '_".$v;'));
             $tmpTables[$f] = $fieldsT;
             $tmpTables[$f]['sep'] = $p['sep'];
             $tmpTables[$f]['ident'] = $alias . '_' . $p['ident'];
             $tmp = array();
             $linkedTable = db::get('table', $p['table'], array('db' => $this->getDb()));
             foreach ($fields as $t) {
                 if ($linkedInfo = $linkedTable->getLinkedTableName($t)) {
                     $aliasT = $alias . '_' . $linkedInfo['table'];
                     $prm['join'][] = array('table' => $linkedInfo['table'], 'alias' => $aliasT, 'dir' => 'left outer', 'on' => $alias . '.' . $linkedInfo['field'] . '=' . $aliasT . '.' . $linkedInfo['ident']);
                     $ttmp = array();
                     foreach (explode(',', $linkedInfo['fields']) as $tt) {
                         $ttmp[] = $aliasT . '.' . $tt;
                         $ttmp[] = '"' . $linkedInfo['sep'] . '"';
                     }
                     array_pop($ttmp);
                     $tmp[] = 'CONCAT(' . implode(',', $ttmp) . ') AS ' . $alias . '_' . $t;
                 } else {
                     $tmp[] = $alias . '.' . $t . ' AS ' . $alias . '_' . $t;
                 }
             }
             $fields = $tmp;
             $prm['fields'] .= ',' . implode(',', $fields);
             if ($p['i18nFields']) {
                 $fieldsI18n = array();
                 $i18nTableName = $p['table'] . db::getCfg('i18n');
                 $i18nTable = db::get('table', $i18nTableName, array('db' => $this->getDb()));
                 $primary = $i18nTable->getPrimary();
                 $i18nAlias = $alias . db::getCfg('i18n');
                 $prm['join'][] = array('table' => $i18nTableName, 'alias' => $i18nAlias, 'dir' => 'left outer', 'on' => $alias . '.' . $p['ident'] . '=' . $i18nAlias . '.' . $primary[0] . ' AND ' . $i18nAlias . '.' . $primary[1] . '="' . request::get('lang') . '"');
                 $fields = explode(',', $p['i18nFields']);
                 $fields = array_flip(array_flip(array_filter($fields)));
                 $fieldsI18n = $fields;
                 array_walk($fieldsI18n, create_function('&$v', '$v = "' . $alias . '_' . db::getCfg('i18n') . '".$v;'));
                 array_walk($fields, create_function('&$v', '$v = "' . $i18nAlias . '.".$v." AS ' . $alias . '_' . db::getCfg('i18n') . '".$v;'));
                 $tmpTables[$f] = array_merge($tmpTables[$f], $fieldsI18n);
                 if (!empty($fields)) {
                     $prm['fields'] .= ',' . implode(',', $fields);
                 }
             }
         }
     }
     if (!empty($this->relatedTables) && $prm['autoJoin'] || $this->i18nTable) {
         foreach ($this->relatedTables as $f => $p) {
             $prm['join'][] = array('table' => $f, 'dir' => 'left outer', 'on' => $this->rawName . '.' . $p['fk1']['link']['ident'] . '=' . $f . '.' . $p['fk1']['name']);
             // related Table fields
             $fields = array_keys($p['fields']);
             $fieldsTableLink = $fields;
             array_walk($fieldsTableLink, create_function('&$v', '$v = "' . $f . '_".$v;'));
             array_walk($fields, create_function('&$v', '$v = "' . $f . '.".$v." AS ' . $f . '_".$v;'));
             if (!empty($fields)) {
                 $prm['fields'] .= ',' . implode(',', $fields);
             }
             $prm['join'][] = array('table' => $p['table'], 'dir' => 'left outer', 'on' => $f . '.' . $p['fk2']['name'] . '=' . $p['table'] . '.' . $p['fk2']['link']['ident']);
             // related Table fields
             $fields = explode(',', $p['fk2']['link']['fields']);
             array_unshift($fields, $p['fk2']['link']['ident']);
             $fields = array_flip(array_flip(array_filter($fields)));
             $fieldsT = $fields;
             array_walk($fieldsT, create_function('&$v', '$v = "' . $p['table'] . '_".$v;'));
             array_walk($fields, create_function('&$v', '$v = "' . $p['table'] . '.".$v." AS ' . $p['table'] . '_".$v;'));
             if (!empty($fields)) {
                 $prm['fields'] .= ',' . implode(',', $fields);
             }
             // i18n related Table fields
             if ($p['fk2']['link']['i18nFields']) {
                 $fieldsI18n = array();
                 $i18nTableName = $p['table'] . db::getCfg('i18n');
                 $i18nTable = db::get('table', $i18nTableName, array('db' => $this->getDb()));
                 $primary = $i18nTable->getPrimary();
                 $prm['join'][] = array('table' => $i18nTableName, 'dir' => 'left outer', 'on' => $f . '.' . $p['fk2']['name'] . '=' . $i18nTableName . '.' . $primary[0] . ' AND ' . $i18nTableName . '.' . $primary[1] . '="' . request::get('lang') . '"');
                 $fields = explode(',', $p['fk2']['link']['i18nFields']);
                 $fields = array_flip(array_flip(array_filter($fields)));
                 $fieldsI18n = $fields;
                 array_walk($fieldsI18n, create_function('&$v', '$v = "' . $i18nTableName . '_".$v;'));
                 array_walk($fields, create_function('&$v', '$v = "' . $i18nTableName . '.".$v." AS ' . $p['table'] . '_' . db::getCfg('i18n') . '".$v;'));
                 if (!empty($fields)) {
                     $prm['fields'] .= ',' . implode(',', $fields);
                 }
             }
             $tmpTables['relatedTable'][$f] = array('ident' => $this->getIdent(), 'tableName' => $p['table'], 'tableLink' => $fieldsTableLink, 'table' => array_merge($fieldsT, array('field' => $p['fk2']['name'], 'sep' => $p['fk2']['link']['sep'], 'ident' => $p['table'] . '_' . $p['fk2']['link']['ident'])));
         }
         if ($this->i18nTable) {
             $i18nName = $this->i18nTable->getName();
             $primary = $this->i18nTable->getPrimary();
             $prm['join'][] = array('table' => $i18nName, 'dir' => 'left outer', 'on' => $this->rawName . '.' . $this->getIdent() . '=' . $i18nName . '.' . $primary[0]);
             // related Table fields
             $fields = array($primary[1]);
             foreach ($this->getI18nFields() as $f) {
                 $fields[] = $f['name'];
             }
             $fieldsTableLink = $fields;
             array_walk($fieldsTableLink, create_function('&$v', '$v = "' . $i18nName . '_".$v;'));
             array_walk($fields, create_function('&$v', '$v = "' . $i18nName . '.".$v." AS ' . $i18nName . '_".$v;'));
             if (!empty($fields)) {
                 $prm['fields'] .= ',' . implode(',', $fields);
             }
             // related Table fields
             $tmpTables['relatedTable'][$i18nName] = array('ident' => $this->getIdent(), 'tableName' => $i18nName, 'tableLink' => $fieldsTableLink, 'table' => array('field' => $i18nName . '_' . $primary[0], 'sep' => null, 'ident' => $i18nName . '_' . $primary[1]));
         }
         if (array_key_exists('nb', $prm)) {
             $tmpTables['nb'] = $prm['nb'];
             $tmpTables['st'] = array_key_exists('start', $prm) ? $prm['start'] : 0;
             unset($prm['nb']);
         }
     }
     $prm['join'] = array_merge($prm['join'], $join);
     return $prm;
 }