Example #1
0
 /**
  * @param mixed $empty_item. Set to false for none
  * @param array $excludes
  * @param null $collection
  * @return array
  */
 public static function get_selection($empty_item = 'Please select', $excludes = [], $collection = null)
 {
     $model = new self();
     $primaryKey = $model->primaryKey;
     $label_field = $model->labelField ?: 'name';
     $selection = [];
     $records = $collection !== null ? $collection : $model->all();
     if ($empty_item) {
         if (is_array($empty_item)) {
             $selection[key($empty_item)] = current($empty_item);
         } else {
             $selection[0] = $empty_item;
         }
     }
     foreach ($records as $record) {
         if (in_array($record->{$primaryKey}, $excludes)) {
             continue;
         }
         $selection[$record->{$primaryKey}] = $record->{$label_field};
     }
     return $selection;
 }
Example #2
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;
 }