Example #1
0
 /**
  * Process ini file
  *
  * @param string $ini_file
  * @param string $environment
  */
 public static function ini($ini_file, $environment = null)
 {
     $result = [];
     $data = parse_ini_file($ini_file, true);
     // processing environment
     if (!empty($data['environment'])) {
         foreach ($data['environment'] as $k => $v) {
             array_key_set($result, explode('.', $k), $v);
         }
     }
     unset($data['environment']);
     // small chicken and egg problem for environment variable
     if ($environment == null && !empty($result['environment'])) {
         $environment = $result['environment'];
     }
     // processing dependencies first
     if (!empty($data['dependencies'])) {
         foreach ($data['dependencies'] as $k => $v) {
             array_key_set($result, $k, $v);
         }
     }
     unset($data['dependencies']);
     // proccesing environment specific sectings
     foreach ($data as $section => $values) {
         $sections = explode(',', $section);
         if (empty($values) || !in_array($environment, $sections) && !in_array('*', $sections)) {
             continue;
         }
         foreach ($values as $k => $v) {
             array_key_set($result, $k, $v);
         }
     }
     return $result;
 }
Example #2
0
 /**
  * Multi level options
  *
  * @param array $data
  * @param array $optmultis_map
  * @param array $options
  * @return array
  */
 public static function optmultis($data, $optmultis_map, $options = [])
 {
     $keys = array_keys($optmultis_map);
     $max_level = count($keys) - 1;
     $result = [];
     // process models
     $models = [];
     foreach ($optmultis_map as $k => $v) {
         if (!empty($v['model'])) {
             $model = $v['model'];
             $object = new $model();
             $models[$k] = $object->options();
         }
     }
     // generating all items in one run
     foreach ($data as $k => $v) {
         $temp_result = $k2_hash2 = $k2_hash = [];
         $level = -1;
         // a must
         foreach ($keys as $k2 => $v2) {
             $k2_alias = $optmultis_map[$v2]['alias'] ?? $v2;
             $k2_hash[$k2_alias] = $v[$v2];
             if (!empty($v[$v2])) {
                 $level++;
                 if ($k2 != 0) {
                     $k2_hash2[] = 'options';
                 }
                 $k2_hash2[] = $v[$v2];
             }
             if ($k2 < $max_level) {
                 if (!array_key_get($result, $k2_hash2)) {
                     $k2_temp = [];
                     $k2_temp['level'] = $level;
                     $k2_temp['name'] = $models[$v2][$v[$v2]]['name'] ?? $v[$v2];
                     if (!empty($options['i18n'])) {
                         $k2_temp['name'] = i18n(null, $k2_temp['name']);
                     }
                     $k2_temp['json_key'] = json_encode($k2_hash);
                     $k2_temp['disabled'] = $optmultis_map[$v2]['disabled'] ?? false;
                     array_key_set($result, $k2_hash2, $k2_temp);
                 }
             }
             // last key - we have items
             if ($k2 == $max_level) {
                 $temp_result['level'] = $level;
                 $name = '';
                 if (isset($optmultis_map[$v2]['column'])) {
                     $name = $v[$optmultis_map[$v2]['column']];
                 } else {
                     $name = $v[$k2_alias];
                 }
                 if (empty($options['i18n'])) {
                     $temp_result['name'] = $name;
                 } else {
                     $temp_result['name'] = i18n(null, $name);
                 }
                 // icon
                 $temp_result['icon_class'] = null;
                 if (isset($optmultis_map[$v2]['icon_column']) && !empty($v[$optmultis_map[$v2]['icon_column']])) {
                     $temp_result['icon_class'] = html::icon(['type' => $v[$optmultis_map[$v2]['icon_column']], 'class_only' => true]);
                 }
                 // only this value flag
                 if (!empty($optmultis_map[$v2]['only_this_value'])) {
                     $temp_result['json_key'] = $v[$v2];
                 } else {
                     $temp_result['json_key'] = json_encode($k2_hash);
                 }
                 array_key_set($result, $k2_hash2, $temp_result);
             }
         }
     }
     // sorting & generating final array
     array_key_sort($result, ['name' => SORT_ASC]);
     $result2 = [];
     foreach ($result as $v) {
         // level 0
         $result2[$v['json_key']] = ['name' => $v['name'], 'level' => $v['level'], 'icon_class' => $v['icon_class'] ?? null, 'disabled' => $v['disabled'] ?? false];
         // level 1
         if (!empty($v['options'])) {
             array_key_sort($v['options'], ['name' => SORT_ASC]);
             foreach ($v['options'] as $v2) {
                 $result2[$v2['json_key']] = ['name' => $v2['name'], 'level' => $v2['level'], 'icon_class' => $v2['icon_class'] ?? null, 'disabled' => $v2['disabled'] ?? false];
                 // level 2
                 if (!empty($v2['options'])) {
                     array_key_sort($v2['options'], ['name' => SORT_ASC]);
                     foreach ($v2['options'] as $v3) {
                         $result2[$v3['json_key']] = ['name' => $v3['name'], 'level' => $v3['level'], 'icon_class' => $v3['icon_class'] ?? null, 'disabled' => $v3['disabled'] ?? false];
                         // level 3
                         if (!empty($v3['options'])) {
                             array_key_sort($v3['options'], ['name' => SORT_ASC]);
                             foreach ($v3['options'] as $v4) {
                                 $result2[$v4['json_key']] = ['name' => $v4['name'], 'level' => $v4['level'], 'icon_class' => $v4['icon_class'] ?? null, 'disabled' => $v4['disabled'] ?? false];
                             }
                         }
                     }
                 }
             }
         }
     }
     return $result2;
 }
 /**
  * Process details
  *
  * @param array $details
  * @param array $parent_rows
  * @param array $options
  * @param array $parent_keys
  * @param array $parent_types
  * @param array $parent_settings
  */
 private function process_details(&$details, &$parent_rows, $options, $parent_keys = [], $parent_types = [], $parent_maps = [], $parent_settings = [])
 {
     foreach ($details as $k => $v) {
         $details[$k]['model_object'] = $model = factory::model($k, true);
         $pk = $v['pk'] ?? $model->pk;
         // generate keys from parent array
         $keys = [];
         $key_level = count($v['map']);
         if ($key_level == 1) {
             $k1 = key($v['map']);
             $v1 = $v['map'][$k1];
             $column = $v1;
         } else {
             $column = "concat_ws('::'[comma] " . implode('[comma] ', $v['map']) . ")";
         }
         // special array for keys
         $parent_keys2 = $parent_keys;
         $parent_keys2[] = $k;
         $parent_types2 = $parent_types;
         $parent_types2[] = $v['type'];
         $parent_maps2 = $parent_maps;
         $parent_maps2[] = $v['map'];
         // create empty arrays
         $result_keys = [];
         $this->get_all_child_keys($parent_rows, $parent_maps2, $parent_keys2, $parent_types2, $result_keys, $keys);
         foreach ($result_keys as $k0 => $v0) {
             array_key_set($parent_rows, $v0, []);
         }
         // if we have relation
         $sql_relation_join = '';
         $sql_relation_columns = '';
         if (!empty($v['__relation_pk'])) {
             $temp3 = [];
             foreach ($v['map'] as $k3 => $v3) {
                 $temp3[] = "b2.{$k3} = b.{$v3}";
             }
             $sql_relation_join = ' INNER JOIN ' . $this->primary_model->name . ' b2 ON ' . implode(' AND ', $temp3);
             $sql_relation_columns = ', ' . implode(',', $v['__relation_pk']);
         }
         // sql extensions
         $v['sql']['where'] = $v['sql']['where'] ?? null;
         // building SQL
         $sql = ' AND ' . $this->primary_model->db_object->prepare_condition([$column => $keys]);
         $sql_full = 'SELECT b.*' . $sql_relation_columns . ' FROM ' . $model->name . ' b ' . $sql_relation_join . ' WHERE 1=1' . $sql . ($v['sql']['where'] ? ' AND ' . $v['sql']['where'] : '');
         // order by
         $orderby = $options['orderby'] ?? (!empty($model->orderby) ? $model->orderby : null);
         if (!empty($orderby)) {
             $sql_full .= ' ORDER BY ' . array_key_sort_prepare_keys($orderby, true);
         }
         // if we need to lock rows
         if (!empty($options['for_update'])) {
             $sql_full .= ' FOR UPDATE';
         }
         // quering
         $result = $this->primary_model->db_object->query($sql_full, null);
         // important not to set pk
         if (!$result['success']) {
             throw new Exception(implode(", ", $result['error']));
         }
         // if we got rows
         if (!empty($result['rows'])) {
             $reverse_map = array_reverse($parent_maps2, true);
             foreach ($result['rows'] as $k2 => $v2) {
                 $master_key = [];
                 // entry itself
                 if ($v['type'] == '1M') {
                     $temp = [];
                     foreach ($pk as $v0) {
                         $temp[] = $v2[$v0];
                     }
                     $master_key[] = implode('::', $temp);
                 }
                 $previous = $v2;
                 foreach ($reverse_map as $k3 => $v3) {
                     $temp = [];
                     if (empty($v['__relation_pk'])) {
                         foreach ($v3 as $k4 => $v4) {
                             $previous[$k4] = $previous[$v4];
                             $temp[] = $previous[$v4];
                         }
                     } else {
                         foreach ($v['__relation_pk'] as $k4 => $v4) {
                             $temp[] = $previous[$v4];
                         }
                     }
                     array_unshift($master_key, $parent_keys2[$k3]);
                     if (($parent_types2[$k3 - 1] ?? '') != '11') {
                         array_unshift($master_key, implode('::', $temp));
                     }
                 }
                 array_key_set($parent_rows, $master_key, $v2);
             }
             // if we have more details
             if (!empty($v['details'])) {
                 $this->process_details($v['details'], $parent_rows, $options, $parent_keys2, $parent_types2, $parent_maps2, $v);
             }
         }
     }
 }
 /**
  * Set value in settings
  *
  * @param mixed $key
  * @param mixed $value
  * @param array $options
  *		boolean append - whether to append value to array
  */
 public static function set($key, $value, $options = [])
 {
     array_key_set(self::$settings, $key, $value, $options);
 }
Example #5
0
 /**
  * Add class to factory
  *
  * @param mixed $key
  * @param object $class_object
  */
 public static function set($key, $class_object)
 {
     array_key_set(self::$class_objects, $key, $class_object);
 }
Example #6
0
 /**
  * Add element to the row
  *
  * @param string $container_link
  * @param string $row_link
  * @param string $element_link
  * @param array $options
  */
 public function element($container_link, $row_link, $element_link, $options = [])
 {
     // presetting options for buttons, making them last
     if (in_array($row_link, [$this::buttons, $this::batch_buttons])) {
         $options['row_type'] = 'grid';
         if (!isset($options['row_order'])) {
             $options['row_order'] = PHP_INT_MAX - 500;
         }
     }
     // processing row and container
     $this->container($container_link, array_key_extract_by_prefix($options, 'container_'));
     $this->row($container_link, $row_link, array_key_extract_by_prefix($options, 'row_'));
     // setting value
     if (!isset($this->data[$container_link]['rows'][$row_link]['elements'][$element_link])) {
         if (!empty($options['container'])) {
             $this->data[$options['container']]['flag_child'] = true;
             $type = 'tab';
             $container = $options['container'];
             // need to add a container to the tabs
             $this->misc_settings['tabs'][$container] = $this->data[$container_link]['rows'][$row_link]['options']['label_name'];
         } else {
             // name & id
             if ($this->data[$container_link]['type'] == 'details' || $this->data[$container_link]['type'] == 'subdetails') {
                 // details & subdetails
                 $options['values_key'] = $options['error_name'] = $options['name'] = null;
                 $options['id'] = null;
                 $options['details_key'] = $this->data[$container_link]['options']['details_key'];
                 $options['details_parent_key'] = $this->data[$container_link]['options']['details_parent_key'] ?? null;
                 $options['details_field_name'] = $element_link;
                 $options['details_collection_key'] = $this->data[$container_link]['options']['details_collection_key'];
             } else {
                 // regular fields
                 $options['error_name'] = $options['name'] = $element_link;
                 $options['values_key'] = [$element_link];
                 $options['id'] = "form_{$this->form_link}_element_{$element_link}";
                 $options['details_collection_key'] = null;
                 // we do not validate preset fields
                 if (!empty($options['preset'])) {
                     $options['options_manual_validation'] = true;
                     $options['tree'] = true;
                     $options['searchable'] = true;
                 }
             }
             // multiple column
             if (!empty($options['multiple_column'])) {
                 $options['details_collection_key'] = array_merge($options['details_collection_key'] ?? [], ['details', $element_link]);
             }
             // process domain & type
             $temp = object_data_common::process_domains(['options' => $options]);
             $options = $temp['options'];
             $options['row_link'] = $row_link;
             $options['container_link'] = $container_link;
             // fix boolean type
             if (($options['type'] ?? '') == 'boolean' && !isset($options['method'])) {
                 $options['method'] = 'select';
                 $options['no_choose'] = true;
                 $options['options_model'] = 'object_data_model_inactive';
                 $options['searchable'] = false;
             }
             // put data into fields array
             $field = ['id' => $options['id'], 'name' => $options['name'], 'options' => $options, 'order' => $options['order'] ?? 0, 'row_order' => $this->data[$container_link]['rows'][$row_link]['order']];
             // we need to put values into fields and details
             $persistent_key = [];
             if ($this->data[$container_link]['type'] == 'details') {
                 array_key_set($this->detail_fields, [$this->data[$container_link]['options']['details_key'], 'elements', $element_link], $field);
                 array_key_set($this->detail_fields, [$this->data[$container_link]['options']['details_key'], 'options'], $this->data[$container_link]['options']);
                 // details_unique_select
                 if (!empty($field['options']['details_unique_select'])) {
                     $this->misc_settings['details_unique_select'][$this->data[$container_link]['options']['details_key']][$element_link] = [];
                 }
                 // persistent
                 $persistent_key[] = 'details';
                 $persistent_key[] = $this->data[$container_link]['options']['details_key'];
                 $persistent_key[] = $element_link;
             } else {
                 if ($this->data[$container_link]['type'] == 'subdetails') {
                     $this->data[$container_link]['options']['container_link'] = $container_link;
                     array_key_set($this->detail_fields, [$this->data[$container_link]['options']['details_parent_key'], 'subdetails', $this->data[$container_link]['options']['details_key'], 'elements', $element_link], $field);
                     array_key_set($this->detail_fields, [$this->data[$container_link]['options']['details_parent_key'], 'subdetails', $this->data[$container_link]['options']['details_key'], 'options'], $this->data[$container_link]['options']);
                     // details_unique_select
                     if (!empty($field['options']['details_unique_select'])) {
                         $this->misc_settings['details_unique_select'][$this->data[$container_link]['options']['details_parent_key'] . '::' . $this->data[$container_link]['options']['details_key']][$element_link] = [];
                     }
                     // persistent
                     $persistent_key[] = 'subdetails';
                     $persistent_key[] = $this->data[$container_link]['options']['details_parent_key'];
                     $persistent_key[] = $this->data[$container_link]['options']['details_key'];
                     $persistent_key[] = $element_link;
                 } else {
                     // persistent
                     array_key_set($this->fields, $element_link, $field);
                     $persistent_key[] = 'fields';
                     $persistent_key[] = $element_link;
                 }
             }
             // persistent
             if (!empty($field['options']['persistent']) && !empty($persistent_key)) {
                 array_key_set($this->misc_settings['persistent'], $persistent_key, $field['options']['persistent']);
             }
             // type is field by default
             $type = 'field';
             $container = null;
             // process submit elements
             if (!empty($options['process_submit'])) {
                 $this->process_submit_all[$element_link] = false;
             }
         }
         // setting data
         $this->data[$container_link]['rows'][$row_link]['elements'][$element_link] = ['type' => $type, 'container' => $container, 'options' => $options, 'order' => $options['order'] ?? 0];
         // we need to set few misc options
         if (!empty($options['options_model'])) {
             $temp = explode('::', $options['options_model']);
             $name = [];
             if (isset($this->misc_settings['tabs'][$container_link])) {
                 $name[] = $this->misc_settings['tabs'][$container_link];
             }
             $name[] = $options['label_name'];
             $this->misc_settings['option_models'][$element_link] = ['model' => $temp[0], 'field_code' => $element_link, 'field_name' => implode(': ', $name)];
         }
     } else {
         $this->data[$container_link]['rows'][$row_link]['elements'][$element_link]['options'] = array_merge_hard($this->data[$container_link]['rows'][$row_link]['elements'][$element_link], $options);
     }
 }
/**
 * Set array values based on keys in the array
 *
 * @param array $arr
 * @param mixed $keys
 * @param mixed $value
 * @param array $options 
 */
function array_key_set_by_key_name(&$arr, $keys = null, $value, $options = array())
{
    // transform keys
    if (!is_array($keys)) {
        $keys = explode(',', $keys . '');
    }
    $temp = [];
    foreach ($keys as $k) {
        $temp[] = $value[$k];
    }
    // unsetting keys
    if (!empty($options['unset_keys'])) {
        foreach ($temp as $k2) {
            unset($value[$k2]);
        }
    }
    array_key_set($arr, $temp, $value, $options);
}
Example #8
0
 /**
  * Process import object
  *
  * @return array
  */
 public function process()
 {
     $result = ['success' => false, 'error' => [], 'hint' => []];
     if (empty($this->import_data)) {
         throw new Exception('You must pecify import_data parameter.');
     }
     // if we have fixes to the data
     if (method_exists($this, 'overrides')) {
         $this->overrides();
     }
     // initialize alias & crypt objects
     $alias_object = new object_data_aliases();
     $alias_data = $alias_object->get();
     $crypt = new crypt();
     gc_enable();
     // processing one by one
     foreach (array_keys($this->import_data) as $k) {
         // we continue if we have no rows
         if (count($this->import_data[$k]['data']) == 0) {
             continue;
         }
         // class and object
         $class = $this->import_data[$k]['options']['model'];
         $object = new $class();
         // a short cut to skip updating large datasets
         if (!empty($this->import_data[$k]['options']['quick_pk_comparison'])) {
             // data from an array
             $groupped = [];
             foreach ($this->import_data[$k]['data'] as $k12 => $v12) {
                 $keys = [];
                 foreach ($this->import_data[$k]['options']['quick_pk_comparison'] as $v13) {
                     $keys[] = $v12[$v13];
                 }
                 $keys = implode('::', $keys);
                 $temp = array_key_get($groupped, $keys);
                 if (empty($temp)) {
                     $temp = 0;
                 }
                 $temp++;
                 array_key_set($groupped, $keys, $temp);
             }
             // get data from database
             $sql = "SELECT concat_ws('::', " . implode(', ', $this->import_data[$k]['options']['quick_pk_comparison']) . ") groupped, count(*) count FROM {$object->name} GROUP BY groupped";
             $db = $object->db_object();
             $result_compare = $db->query($sql);
             $groupped2 = [];
             foreach ($result_compare['rows'] as $v12) {
                 $groupped2[$v12['groupped']] = $v12['count'];
             }
             // compare
             $discrepancies = false;
             foreach ($groupped as $k12 => $v12) {
                 if (!isset($groupped2[$k12]) || $groupped2[$k12] != $v12) {
                     $discrepancies = true;
                 }
             }
             // we continue loop if there's no discrepancies
             if (!$discrepancies) {
                 $result['hint'][] = ' * Skipping ' . $object->name . ', db link: ' . $object->db_link;
                 continue;
             }
         }
         // if we need mass import
         $total_rows = count($this->import_data[$k]['data']);
         if ($total_rows >= self::mass_import_rows) {
             $db = $object->db_object();
             $db->create_temp_table('temp_' . $object->name, $object->columns, $this->import_data[$k]['options']['pk'], ['skip_serials' => true]);
         }
         $counter = 0;
         $buffer = [];
         do {
             // grab first element from the array
             $v2 = array_shift($this->import_data[$k]['data']);
             // we need to process overrides
             foreach ($v2 as $k3 => $v3) {
                 if (!is_string($v3)) {
                     continue;
                 }
                 // if we need id
                 if (strpos($v3, '~id~') === 0) {
                     $value = substr($v3, 4);
                     $alias = null;
                     foreach ($alias_data as $k4 => $v4) {
                         // todo: maybe need column prefix with alias
                         if (strpos($k3, $k4) !== false) {
                             $alias = $k4;
                         }
                     }
                     $v2[$k3] = $alias_object->get_id_by_code($alias, substr($v3, 4));
                     continue;
                 }
                 // password
                 if (strpos($v3, '~password~') === 0) {
                     $v2[$k3] = $crypt->password_hash(substr($v3, 10));
                 }
             }
             // if we have multiple
             if (!empty($this->import_data[$k]['options']['multiple'])) {
                 foreach ($v2[$this->import_data[$k]['options']['multiple'][0]] as $mv0) {
                     $temp = $v2;
                     $temp[$this->import_data[$k]['options']['multiple'][0]] = $mv0;
                     if (!empty($this->import_data[$k]['options']['multiple'][1])) {
                         foreach ($v2[$this->import_data[$k]['options']['multiple'][1]] as $mv1) {
                             $temp[$this->import_data[$k]['options']['multiple'][1]] = $mv1;
                             // todo: add third level
                         }
                     }
                     $buffer[] = $temp;
                 }
             } else {
                 $buffer[] = $v2;
             }
             // if buffer has 100 rows or we have no data
             if (count($buffer) > 249 || count($buffer) > 0 && count($this->import_data[$k]['data']) == 0) {
                 if ($total_rows >= self::mass_import_rows) {
                     // insert all rows
                     $result_insert = $db->insert('temp_' . $object->name, $buffer);
                     if (!$result_insert['success']) {
                         $result['error'] = $result_insert['error'];
                         return $result;
                     }
                     $counter += count($buffer);
                     // doing this might take some time
                     echo ".";
                 } else {
                     // less than 1000 records
                     foreach ($buffer as $v10) {
                         switch ($this->import_data[$k]['options']['method'] ?? 'save') {
                             case 'save_insert_new':
                                 $result_insert = $object->save($v10, ['pk' => $this->import_data[$k]['options']['pk'], 'flag_insert_only' => true, 'ignore_not_set_fields' => true]);
                                 break;
                             case 'save':
                             default:
                                 $result_insert = $object->save($v10, ['pk' => $this->import_data[$k]['options']['pk'], 'ignore_not_set_fields' => true]);
                         }
                         if (!$result_insert['success']) {
                             $result['error'] = $result_insert['error'];
                             return $result;
                         }
                         $counter++;
                     }
                 }
                 $buffer = [];
                 // free up memory
                 gc_collect_cycles();
             }
         } while (count($this->import_data[$k]['data']) > 0);
         // we need to run few queries for mass import
         if ($total_rows >= self::mass_import_rows) {
             $type = $this->import_data[$k]['options']['method'] ?? 'save';
             $columns = [];
             $columns_mysql = [];
             $where = [];
             $where_mysql = [];
             $where_delete = [];
             $serials = false;
             foreach ($object->columns as $k12 => $v12) {
                 if (in_array($k12, $this->import_data[$k]['options']['pk'])) {
                     $where[] = "a.{$k12} = temp_{$object->name}.{$k12}";
                     $where_mysql[] = "{$object->name}.{$k12} = temp_{$object->name}.{$k12}";
                     $where_delete[] = "a.{$k12} = b.{$k12}";
                 } else {
                     if (strpos($v12['type'], 'serial') !== false) {
                         $serials = true;
                         continue;
                     }
                     $columns[] = "{$k12} = temp_{$object->name}.{$k12}";
                     $columns_mysql[] = "{$object->name}.{$k12} = temp_{$object->name}.{$k12}";
                 }
             }
             if ($type == 'save') {
                 // update existing rows
                 if ($db->backend == 'pgsql') {
                     $sql = "UPDATE {$object->name} AS a SET " . implode(', ', $columns) . " FROM temp_{$object->name} AS temp_{$object->name} WHERE " . implode(' AND ', $where);
                 } else {
                     $sql = "UPDATE {$object->name}, temp_{$object->name} SET " . implode(', ', $columns_mysql) . " WHERE " . implode(' AND ', $where_mysql);
                 }
                 $result_insert = $db->query($sql);
                 if (!$result_insert['success']) {
                     $result['error'] = $result_insert['error'];
                     return $result;
                 }
             }
             // delete existing rows
             if ($db->backend == 'pgsql') {
                 $sql = "DELETE FROM temp_{$object->name} a WHERE EXISTS (SELECT 1 FROM {$object->name} b WHERE " . implode(' AND ', $where_delete) . ")";
             } else {
                 $sql = "DELETE FROM a USING temp_{$object->name} AS a INNER JOIN {$object->name} AS b WHERE " . implode(' AND ', $where_delete);
             }
             $result_insert = $db->query($sql);
             if (!$result_insert['success']) {
                 $result['error'] = $result_insert['error'];
                 return $result;
             }
             // we need to update serial columns
             if (!empty($serials)) {
                 $columns = [];
                 foreach ($object->columns as $k12 => $v12) {
                     if (strpos($v12['type'], 'serial') !== false) {
                         $columns[] = "{$k12} = nextval('{$object->name}_{$k12}_seq')";
                     }
                 }
                 $sql = "UPDATE temp_{$object->name} SET " . implode(', ', $columns);
                 $result_insert = $db->query($sql);
                 if (!$result_insert['success']) {
                     $result['error'] = $result_insert['error'];
                     return $result;
                 }
             }
             // insert the rest of the rows
             $sql = "INSERT INTO {$object->name} SELECT * FROM temp_{$object->name}";
             $result_insert = $db->query($sql);
             if (!$result_insert['success']) {
                 $result['error'] = $result_insert['error'];
                 return $result;
             }
             // drop temp table
             $sql = "DROP TABLE temp_{$object->name}";
             $result_insert = $db->query($sql);
             if (!$result_insert['success']) {
                 $result['error'] = $result_insert['error'];
                 return $result;
             }
         }
         $result['hint'][] = ' * Imported ' . $counter . ' rows into ' . $object->name . ', db link: ' . $object->db_link;
     }
     $result['success'] = true;
     return $result;
 }
Example #9
0
 public function process($data, $options = [])
 {
     $temp = [];
     // we need to precess items that are controller and suboptions at the same time
     $subgroups = [];
     foreach ($data as $k => $v) {
         // determine acl
         if (!empty($v['sm_menuitm_acl_controller_id']) && !helper_acl::can_see_this_controller($v['sm_menuitm_acl_controller_id'], $v['sm_menuitm_acl_action_id'])) {
             unset($data[$k]);
             continue;
         }
         // go though each group
         for ($i = 1; $i <= 4; $i++) {
             if (!empty($v["g{$i}_code"])) {
                 $subgroups[$v["g{$i}_code"]] = true;
             }
         }
     }
     $subgroup_items = [];
     foreach ($data as $k => $v) {
         if (isset($subgroups[$v['sm_menuitm_code']])) {
             $subgroup_items[$v['sm_menuitm_code']] = $v;
             unset($data[$k]);
         }
     }
     // loop though data
     foreach ($data as $k => $v) {
         // loop though groups and add them to menu
         $key = [];
         for ($i = 1; $i <= 4; $i++) {
             if (!empty($v['g' . $i . '_code'])) {
                 $key[] = $v['g' . $i . '_code'];
                 // we need to set all groups
                 $temp2 = array_key_get($temp, $key);
                 if (is_null($temp2)) {
                     // if we have a controller that acts as submenu
                     if (!empty($subgroup_items[$v['g' . $i . '_code']])) {
                         $v9 = $subgroup_items[$v['g' . $i . '_code']];
                         array_key_set($temp, $key, ['code' => $v9['sm_menuitm_code'], 'name' => $v9['sm_menuitm_name'], 'name_extension' => null, 'icon' => $v9['sm_menuitm_icon'], 'url' => $v9['sm_menuitm_url'], 'order' => $v9['sm_menuitm_order'], 'options' => []]);
                     } else {
                         // if we do not have url we assume visitor wants to see extended menu
                         if (empty($v['g' . $i . '_url'])) {
                             $params = [];
                             for ($j = 1; $j <= $i; $j++) {
                                 $params['group' . $j . '_code'] = $v['g' . $j . '_code'];
                             }
                             $v['g' . $i . '_url'] = '/numbers/backend/system/menu/controller/menu?' . http_build_query2($params);
                         }
                         array_key_set($temp, $key, ['code' => $v['g' . $i . '_code'], 'name' => $v['g' . $i . '_name'], 'icon' => $v['g' . $i . '_icon'], 'order' => $v['g' . $i . '_order'], 'url' => $v['g' . $i . '_url'], 'options' => []]);
                     }
                 }
                 $key[] = 'options';
             }
         }
         // some replaces
         $name_extension = null;
         if ($v['sm_menuitm_code'] == 'entites.authorization.__entity_name') {
             $name_extension = '<b>' . session::get(['numbers', 'entity', 'em_entity_name']) . '</b>';
         }
         // finally we need to add menu item to the array
         $key[] = $v['sm_menuitm_code'];
         array_key_set($temp, $key, ['code' => $v['sm_menuitm_code'], 'name' => $v['sm_menuitm_name'], 'name_extension' => $name_extension, 'icon' => $v['sm_menuitm_icon'], 'url' => $v['sm_menuitm_url'], 'order' => $v['sm_menuitm_order'], 'options' => []]);
         // options generator
         if (!empty($v['sm_menuitm_options_generator'])) {
             $temp3 = explode('::', $v['sm_menuitm_options_generator']);
             $temp_data = factory::model($temp3[0])->{$temp3[1]}();
             $temp_key = $key;
             $temp_key[] = 'options';
             foreach ($temp_data as $k2 => $v2) {
                 $temp_key2 = $temp_key;
                 $temp_key2[] = $k2;
                 array_key_set($temp, $temp_key2, $v2);
             }
         }
     }
     // sorting
     foreach ($temp as $k => $v) {
         if (!empty($v['options'])) {
             foreach ($v['options'] as $k2 => $v2) {
                 if (!empty($v2['options'])) {
                     foreach ($v2['options'] as $k3 => $v3) {
                         if (!empty($v3['options'])) {
                             foreach ($v3['options'] as $k4 => $v4) {
                                 if (!empty($v4['options'])) {
                                     array_key_sort($temp[$k]['options'][$k2]['options'][$k3]['options'][$k4]['options'], ['order' => SORT_ASC], ['order' => SORT_NUMERIC]);
                                 }
                             }
                             array_key_sort($temp[$k]['options'][$k2]['options'][$k3]['options'], ['order' => SORT_ASC], ['order' => SORT_NUMERIC]);
                         }
                     }
                     array_key_sort($temp[$k]['options'][$k2]['options'], ['order' => SORT_ASC], ['order' => SORT_NUMERIC]);
                 }
             }
             array_key_sort($temp[$k]['options'], ['order' => SORT_ASC], ['order' => SORT_NUMERIC]);
         }
     }
     // sort root
     array_key_sort($temp, ['order' => SORT_ASC], ['order' => SORT_NUMERIC]);
     return $temp;
 }
Example #10
0
 /**
  * Set session variable
  * 
  * @param string $key
  * @param mixed $value
  */
 public function __set($key, $value)
 {
     array_key_set($_SESSION, $key, $value);
 }