Exemple #1
0
 public static function request($fields, $prefix = '', $operation = 'create', $source = null)
 {
     // Статика
     static $freq = null;
     static $fhid = null;
     if (is_null($freq)) {
         $freq = xbDataFields::flagName('required');
     }
     if (is_null($fhid)) {
         $fhid = xbDataFields::flagName('hidden');
     }
     // Инициализация
     $ret = array('values' => array(), 'notset' => array(), 'incorrect' => array(), 'ignored' => array());
     $op = xbData::operation($operation);
     if (!in_array($op, array('create', 'read', 'update'))) {
         return false;
     }
     // Поля
     foreach ($fields as $alias => $field) {
         $got = null;
         if (!is_array($source)) {
             $pa = $prefix . $alias;
             if (array_key_exists($pa, $_POST)) {
                 $got = $_POST[$pa];
             } elseif (array_key_exists($pa, $_GET)) {
                 $got = urldecode($_GET[$pa]);
             }
         } else {
             if (!array_key_exists($alias, $source)) {
                 continue;
             }
             $got = $source[$alias];
         }
         if (!$field['access'][$op] || ($field['flags'] & $fhid) != 0) {
             continue;
         }
         // Проверка обязательности
         $req = ($field['flags'] & $freq) != 0;
         if ($req) {
             if (is_null($got) || empty($got)) {
                 $ret['notset'][] = $alias;
                 continue;
             }
         }
         // Регулярки
         $cor = true;
         if (!is_null($field['regexp'])) {
             if (preg_match($field['regexp'], $got)) {
                 if (!is_null($field['replace'])) {
                     if (!empty($field['replace'])) {
                         $got = preg_replace($field['regexp'], $field['replace'], $got);
                     }
                 }
             } else {
                 $cor = false;
             }
         }
         // Элементы
         if ($cor && is_array($field['elements'])) {
             if (!is_null($got)) {
                 if (!array_key_exists($got, $field['elements'])) {
                     $cor = false;
                 }
             } else {
                 $cor = false;
             }
         }
         // Итог проверок на корректность
         if (!$cor) {
             if ($req) {
                 $ret['incorrect'][] = $alias;
             } else {
                 $ret['ignored'][] = $alias;
             }
             continue;
         }
         // Обработка
         if (!is_null($field['strip'])) {
             $got = preg_replace($field['strip'], '', $got);
         }
         $got = xbData::pack($field['type'], $got);
         $ret['values'][$alias] = $got;
     }
     return $ret;
 }
Exemple #2
0
 public function get()
 {
     if (!$this->_ready) {
         return false;
     }
     if (in_array($this->_type, array('replace', 'insert', 'update'))) {
         if (!is_array($this->_data)) {
             return false;
         }
         if (empty($this->_data)) {
             return false;
         }
     }
     $ret = array();
     $at = $this->_jmay ? '`[+prefix+]' . $this->owner->add['table'] . '`' : '';
     $mt = $this->table;
     switch ($this->_type) {
         case 'replace':
         case 'insert':
             foreach ($this->_data as $row) {
                 if (!isset($row['main']['create'])) {
                     continue;
                 }
                 if (!is_array($row['main']['create'])) {
                     continue;
                 }
                 $q = array();
                 foreach ($row['main']['create'] as $alias => $value) {
                     $q[] = $this->value($alias, $value);
                 }
                 $ret[] = $this->_query . " (" . implode(',', $q) . ")";
                 if (!empty($row['replace']['create']) && $this->_jmay) {
                     $v = array();
                     foreach ($row['replace']['create'] as $fn => $fv) {
                         $v[] = "([+last_insert_id+]," . $this->_adds[$fn] . "," . $this->value($fn, $fv) . ")";
                     }
                     if (!empty($v)) {
                         $ret[] = "insert into {$at} values " . implode(',', $v);
                     }
                 }
             }
             break;
         case 'select':
             if ($_ = $this->selectFields) {
                 $_ = str_replace('[+selectfields+]', $_, $this->_query);
                 return array($_ . $this->join_get() . $this->cond_get());
             } else {
                 return false;
             }
         case 'update':
             $p = $this->owner->primary;
             $join = $this->join_get();
             $limit = empty($this->_limit) ? '' : " limit " . $this->_limit;
             foreach ($this->_data as $row) {
                 $q = array();
                 foreach ($row['main']['update'] as $fn => $fv) {
                     $q[] = "tmain.`{$fn}` = " . $this->value($fn, $fv);
                 }
                 if (empty($q)) {
                     continue;
                 }
                 $_ = " set " . implode(',', $q);
                 $w = array();
                 if (!empty($this->_where)) {
                     $w[] = "(" . $this->_where . ")";
                 }
                 if (!empty($row['primary'])) {
                     $w[] = "(tmain.`{$p}` = " . $row['primary'] . ")";
                 }
                 $w = empty($w) ? '' : " where " . implode(' and ', $w);
                 $_ .= $w;
                 $ret[] = $this->_query . "{$join} {$_}" . $limit;
                 $cfs = "select tmain.`{$p}` from {$mt} as tmain" . $join . $w . $limit;
                 if (!empty($row['replace']['update']) && $this->_jmay) {
                     $ret[] = $cfs;
                     $v = array();
                     foreach ($row['replace']['update'] as $fn => $fv) {
                         $v[] = "([+id+]," . $this->_adds[$fn] . "," . $this->value($fn, $fv) . ")";
                     }
                     if (!empty($v)) {
                         $ret[] = "replace into {$at} values " . implode(',', $v);
                     }
                 }
                 if (!empty($row['delete']['update'])) {
                     $ret[] = "delete from {$at}" . " where (`field` in (" . implode(',', $row['delete']['update']) . "))" . " and (`" . $this->owner->add['field'] . "` in ([+ids+]))";
                 }
             }
             break;
         case 'delete':
             return array($this->_query . $this->join_get() . $this->cond_get());
         case 'clear':
             return array($this->_query);
         case 'table':
             $f = array();
             $k = array();
             $p = '';
             foreach ($this->_fields as $alias => $v) {
                 $f[] = "`{$alias}` " . xbDataFields::SQLType($this->owner->fields[$alias]);
                 if ($this->owner->fields[$alias]['primary']) {
                     $p = $alias;
                 }
                 if (!is_null($this->owner->fields[$alias]['key'])) {
                     $k[$alias] = $this->owner->fields[$alias]['key'];
                 }
             }
             $_ = $this->_query . " (" . implode(',', $f);
             if (is_array($this->owner->indexes)) {
                 foreach ($this->owner->indexes as $alias) {
                     $_ .= ",index (`{$alias}`)`";
                 }
             }
             if (is_array($this->owner->unique)) {
                 foreach ($this->owner->unique as $alias => $key) {
                     $_ .= ",unique key `{$alias}`(`" . implode('`,`', $key) . "`)";
                 }
             }
             if (!empty($p)) {
                 $_ .= ",primary key (`{$p}`)";
             }
             if (!empty($k)) {
                 foreach ($k as $alias => $key) {
                     $_ .= ",foreign key (`{$alias}`) references `[+prefix+]" . (is_null($key['table']) ? $this->owner->table : $key['table']) . "`(`" . $key['field'] . "`)" . " on update " . $key['update'] . " on delete " . $key['delete'];
                 }
             }
             $_ .= ") engine=InnoDB insert_method=first";
             // TODO
             return array($_);
         default:
             return false;
     }
     return $ret;
 }