Example #1
0
 public function order($fieldOrder = 'date_create', $orderDirection = 'ASC', $results = array())
 {
     $res = count($results) ? $results : $this->results;
     $fields = $this->fields;
     $fields['id'] = array();
     $fields['date_create'] = array();
     if (!Arrays::is($fieldOrder)) {
         if (null !== $fieldOrder && !Arrays::exists($fieldOrder, $fields)) {
             $fields[$fieldOrder] = array();
         }
     } else {
         foreach ($fields as $tmpField => $info) {
             if (null !== $tmpField && !Arrays::exists($tmpField, $fields)) {
                 $fields[$tmpField] = array();
             }
         }
     }
     $sort = array();
     foreach ($res as $id) {
         $objectCreated = $this->row($id);
         foreach ($fields as $key => $infos) {
             $type = Arrays::exists('type', $fields[$key]) ? $fields[$key]['type'] : null;
             if ('data' == $type) {
                 list($dummy, $foreignTable, $foreignFieldKey) = $fields[$key]['contentList'];
                 $foreignFields = Arrays::exists($foreignTable, Data::$_fields) ? Data::$_fields[$foreignTable] : Data::noConfigFields($foreignTable);
                 $foreignStorage = new self($foreignTable);
                 $foreignRow = $foreignStorage->find($objectCreated->{$key});
                 $foreignFieldKeys = explode(',', $foreignFieldKey);
                 $value = '';
                 for ($i = 0; $i < count($foreignFieldKey); $i++) {
                     $tmpKey = $foreignFieldKey[$i];
                     $value .= $foreignRow->{$tmpKey} . ' ';
                 }
                 $value = substr($value, 0, -1);
             } else {
                 $value = isset($objectCreated->{$key}) ? $objectCreated->{$key} : null;
             }
             $sort[$key][] = $value;
         }
     }
     $asort = array();
     foreach ($sort as $key => $rows) {
         for ($i = 0; $i < count($rows); $i++) {
             if (empty(${$key}) || is_string(${$key})) {
                 ${$key} = array();
             }
             $asort[$i][$key] = $rows[$i];
             array_push(${$key}, $rows[$i]);
         }
     }
     if (Arrays::is($fieldOrder) && !Arrays::is($orderDirection)) {
         $t = array();
         foreach ($fieldOrder as $tmpField) {
             array_push($t, $orderDirection);
         }
         $orderDirection = $t;
     }
     if (Arrays::is($fieldOrder) && Arrays::is($orderDirection)) {
         if (count($orderDirection) < count($fieldOrder)) {
             throw new Exception('You must provide the same arguments number of fields sorting and directions sorting.');
         }
         if (count($fieldOrder) == 1) {
             $fieldOrder = Arrays::first($fieldOrder);
             if ('ASC' == Inflector::upper(Arrays::first($orderDirection))) {
                 array_multisort(${$fieldOrder}, SORT_ASC, $asort);
             } else {
                 array_multisort(${$fieldOrder}, SORT_DESC, $asort);
             }
         } elseif (count($fieldOrder) > 1) {
             $params = array();
             foreach ($fieldOrder as $k => $tmpField) {
                 $tmpSort = isset($orderDirection[$k]) ? $orderDirection[$k] : 'ASC';
                 $params[] = ${$tmpField};
                 $params[] = 'ASC' == $tmpSort ? SORT_ASC : SORT_DESC;
             }
             $params[] = $asort;
             call_user_func_array('array_multisort', $params);
         }
     } else {
         if ('ASC' == Inflector::upper($orderDirection)) {
             array_multisort(${$fieldOrder}, SORT_ASC, $asort);
         } else {
             array_multisort(${$fieldOrder}, SORT_DESC, $asort);
         }
     }
     $collection = array();
     foreach ($asort as $key => $row) {
         $tmpId = $row['id'];
         array_push($collection, $tmpId);
     }
     $this->results = $collection;
     return $this;
 }
Example #2
0
 function dataDecode($file)
 {
     if (File::exists($file)) {
         if (is_readable($file)) {
             return Data::unserialize(Data::load($file));
         }
     }
     return new Obj();
 }
Example #3
0
 public function saveLite($type)
 {
     $table = $type . 's';
     $fields = Arrays::exists($type, Data::$_fields) ? Data::$_fields[$type] : [];
     $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
     if (count($fields) && count($settings)) {
         $dbType = Arrays::exists('db', $settings) ? $settings['db'] : null;
         $checkId = Arrays::exists('checkId', $settings) ? $settings['checkId'] : 'id';
         if (!empty($dbType)) {
             $data = $this->toArray();
             $fields['id'] = [];
             $fields['date_create'] = [];
             $id = Arrays::exists('id', $data) ? $data['id'] : Data::getKeyLite($type);
             $date_create = Arrays::exists('date_create', $data) ? $data['date_create'] : time();
             $this->id = $data['id'] = $id;
             $this->date_create = $data['date_create'] = $date_create;
             $fields = array_keys($fields);
             $db = Data::$dbType($type);
             $q = "SELECT {$checkId} FROM {$table} WHERE {$checkId} = '" . $data[$checkId] . "'";
             $res = $db->query($q);
             if (false === $res->fetchArray()) {
                 $values = [];
                 foreach ($fields as $field) {
                     $values[] = "'" . SQLite3::escapeString($data[$field]) . "'";
                 }
                 $values = implode(', ', $values);
                 $insert = "INSERT INTO {$table}\n                        (" . implode(', ', $fields) . ")\n                        VALUES ({$values})";
                 $db->exec($insert);
             } else {
                 $update = "UPDATE {$table} SET ";
                 foreach ($fields as $field) {
                     if ($field != $checkId) {
                         $update .= "{$field} = '" . SQLite3::escapeString($data[$field]) . "', ";
                     }
                 }
                 $update = substr($update, 0, -2);
                 $update .= " WHERE {$checkId} = '" . $data[$checkId] . "'";
                 $db->exec($update);
             }
         }
     }
     return $this;
 }
Example #4
0
 private function sort($orderField, $orderDirection, $multiSort = false)
 {
     $fieldsEntity = $this->settings['modelFields'];
     $entity = $this->settings['entity'];
     $db = $this->prepare();
     $this->ids();
     $fieldsEntity['date_create'] = array();
     $q = "SELECT id FROM {$entity} WHERE id IN ('" . implode("', '", $this->ids) . "') ORDER BY ";
     if (false === $multiSort) {
         $type = Arrays::exists('type', $fieldsEntity[$orderField]) ? $fieldsEntity[$orderField]['type'] : null;
         if ('data' == $type) {
             $q = repl('SELECT id', 'SELECT ' . $entity . '.id', $q);
             list($dummy, $foreignTable, $foreignField) = $fieldsEntity[$orderField]['contentList'];
             $fields = Arrays::exists($foreignTable, Data::$_fields) ? Data::$_fields[$foreignTable] : Data::noConfigFields($foreignTable);
             $query = "DROP TABLE IF EXISTS {$foreignTable}; CREATE TABLE {$foreignTable} (id INTEGER PRIMARY KEY, date_create";
             if (count($fields)) {
                 foreach ($fields as $field => $infos) {
                     $query .= ", {$field}";
                 }
             }
             $query .= ");";
             $db->exec($query);
             $lite = new self($foreignTable);
             $datas = $lite->all();
             foreach ($datas as $object) {
                 $query = "INSERT INTO {$foreignTable} (id, date_create) VALUES ('" . SQLite3::escapeString($object->id) . "', '" . SQLite3::escapeString($object->date_create) . "')";
                 $db->exec($query);
                 foreach ($fields as $field => $info) {
                     $value = is_object($object->{$field}) ? 'object' : $object->{$field};
                     $query = "UPDATE {$foreignTable} SET {$field} = '" . SQLite3::escapeString($value) . "' WHERE id = '" . SQLite3::escapeString($object->id) . "'";
                     $db->exec($query);
                 }
             }
             $replace = " LEFT JOIN {$foreignTable} ON {$entity}.{$orderField} = {$foreignTable}.id  WHERE {$entity}.";
             $q = repl(" WHERE ", $replace, $q);
             $foreignFields = explode(',', $foreignField);
             for ($i = 0; $i < count($foreignFields); $i++) {
                 $order = $foreignFields[$i];
                 $q .= "{$foreignTable}.{$order} {$orderDirection}, ";
             }
             $q = substr($q, 0, -2);
         } else {
             $q .= "{$orderField} {$orderDirection}";
         }
     } else {
         for ($i = 0; $i < count($orderField); $i++) {
             $order = $orderField[$i];
             if (Arrays::is($orderDirection)) {
                 $direction = isset($orderDirection[$i]) ? $orderDirection[$i] : 'ASC';
             } else {
                 $direction = $orderDirection;
             }
             $q .= "{$order} {$direction}, ";
         }
         $q = substr($q, 0, -2);
     }
     $res = $db->query($q);
     $collection = array();
     while ($row = $res->fetchArray()) {
         array_push($collection, $this->find($row['id']));
     }
     $this->results = $collection;
 }