Beispiel #1
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;
 }