Beispiel #1
0
 /**
  * Универсальный запрос и возврат
  * @param array $rec содержит массив REQUEST с параметрами
  * должны быть $id, $table, $format, соответственно идентификатор в таблице, таблица и формат возврата
  * Так как используется для запросов из скриптов (САМ, cmd, bash) форматы 
  * line - посторочно без ключа
  * keyline - посторочно ключ|значение (default)
  * array - в массиве PHP на всякий случай
  * json - ну понятно
  * @return  variable в зависимости
  * rem tear "http://baza4/?level=getdata&getdata[act]=uniget&table=customers&id=5" > res
  * rem tear "http://baza4/?level=getdata&getdata[act]=uniget&object=orders_customers_model&id=5" > res
  * rem tear "http://baza4/?level=getdata&getdata[act]=uniget&object=orders_customers_model&id=5&format=json" > res
  * rem tear "http://baza4/?level=getdata&getdata[act]=uniget&table=customers&id=%%D0%%90%%D0%%B7%%D0%%B8%%D0%%BC%%D1%%83%%D1%%82&field=customer" >res
  * tear "http://baza4/?level=getdata&getdata[act]=uniget&table=customers&id=Аврора&field=customer" >res
  */
 public function uniget($rec)
 {
     if (!multibyte::is_utf($rec)) {
         $rec = multibyte::cp1251_to_utf8($rec);
     }
     extract($rec);
     if (!isset($format)) {
         $format = "keyline";
     }
     if (isset($id)) {
         if (isset($table)) {
             if (1 * $id != 0) {
                 $sql = "SELECT * FROM `{$table}` WHERE id='{$id}'";
             } else {
                 if (isset($field)) {
                     $sql = "SELECT * FROM `{$table}` WHERE `{$field}`='{$id}'";
                 } else {
                     // не определить
                     return;
                 }
             }
             $res = sql::fetchOne($sql);
         } elseif (isset($object)) {
             if (class_exists($object)) {
                 $object = new $object();
                 if (method_exists($object, "getRecord")) {
                     $res = $object->getRecord($id);
                 }
             }
         }
         // вывод
         if ($format == "json") {
             $res = json_encode($res, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
             echo $res;
             return $res;
         } elseif ($format == "array") {
             return $res;
         } else {
             foreach ($res as $key => $value) {
                 if ($format == "line") {
                     echo "{$value}\n";
                 } else {
                     echo "{$key}|{$value}\n";
                 }
             }
             return;
         }
     }
 }