/** * Constructor * @param array configuration array */ function __construct($config) { $this->config = new collection_params($this->config ? $this->config : null); $this->formats = new collection_formats($this->formats ? $this->formats : null); // default doamin if (!$this->DOMAIN) { $this->DOMAIN = strtolower(substr(get_class($this), 0, -1 * strlen('_collection'))); } $class = get_class($this); if ($class == __CLASS__ && isset($config['class'])) { $class = $config['class']; } if ($class == __CLASS__) { throw new collection_exception('Aliases must set "class" param to collection |' . $class); } // default domain (like: users\users) if (!$this->_class) { $this->_class = strtolower(substr($class, 0, -1 * strlen('_collection'))); } // pull objects $this->core = core::get_instance(); $this->db = db_loader::get(@$config['connection']); $this->_root = $config['root']; // // external configuration (model.php) // if (!isset($this->fields)) { $model_file = $this->_root . self::VF_FILE; $deb_01 = $class; //isset(self::$_vf_file[$this->DOMAIN]); $ext_config = array(); if (!isset(self::$_vf_file[$class])) { if (file_exists($model_file)) { $ext_config = (require $model_file); self::$_vf_file[$class] = $ext_config; } } else { $ext_config = self::$_vf_file[$class]; } $this->fields = array(); // multiconfig model {fields, config} if (isset($ext_config['fields'])) { $this->fields = $ext_config['fields']; if (!empty($ext_config['config'])) { $config = array_merge($config, $ext_config['config']); } if (!empty($ext_config['formats'])) { $this->formats->from_array($ext_config['formats']); } } else { // just fields $this->fields = $ext_config; } // allow external data if (isset($ext_config['data']) && !isset($config['data'])) { $config['data'] = $ext_config['data']; } // behaviors if (isset($ext_config['behaviors']) && empty($this->behaviors)) { $this->behaviors = $ext_config['behaviors']; } } if (empty($this->fields)) { throw new collection_exception('Empty valid fields in ' . get_class($this)); } // attach behaviors $this->create_behaviors(); $this->construct_before($config); // update keys $this->prepare_fields(); // key override if (!empty($config['key'])) { $this->set_key($config['key']); } if (empty($this->_key)) { if (($idvf = $this->get_field('id')) !== false) { $this->set_key('id'); // legacy $this->is_key_autoincrement(true); } } if ($m_class = @$config['item_class']) { // model class hack (get_class($this) dont work when class_alias used) $this->item_class = $m_class; } if (empty($this->_name)) { if (isset($config['name'])) { $this->_name = $config['name']; } else { $this->_name = str_replace('\\', '_', $this->_class); } } // auto assign items class (like class_collection) if (empty($this->item_class)) { $sz_cl = $class; $sz_cl = str_replace('collection', 'item', $sz_cl); $this->item_class = $sz_cl; } // positioning if ($this->with_positions() && empty($config['order_sql'])) { $config['order_sql'] = 'position'; } if (!isset($config['prefix'])) { $config['prefix'] = $this->db->get_prefix(); } if (!empty($config['table'])) { $config['table'] = $config['prefix'] . $config['table']; } else { $this->readonly = true; } // use class name? $config['table'] = str_replace('%class%', $this->get_name(), $config['table']); /* по-умолчанию включаем сортировку по ID в обратном порядке (для лимита больше 1) */ if (!isset($config['order_sql']) && $this->get_key() && $this->is_key_autoincrement() && (empty($config['limit_sql']) || 2 < $config['limit_sql'][1])) { $config['order_sql'] = $this->get_key() . ' DESC'; } if (empty($this->item_class) || !class_exists($this->item_class, 0)) { throw new collection_exception('Cannot instanciate class ' . $this->item_class); } // WITHOUT EXTRA if (isset($config['no_extra'])) { $this->disable_extra_fields(); } $this->check_config($config); $this->config->from_array($config); // old compat if ($rbk = $this->config->get('render_by_key')) { $this->is_render_by_key($rbk); } // extra fields if ($this->with_extra_fields()) { $this->load_extra_fields(); } if (!empty($config['load'])) { $this->load(); } if (!empty($config['data'])) { $this->load_from_array($config['data']); } $this->_order_sql = $this->get_order(); // save original order if (empty($this->_order_sql)) { $this->_order_sql = $this->get_order(); } else { $this->set_order($this->_order_sql); } if (class_exists('core', 0)) { $this->_ctype_id = ($this->ctype = $this->core->get_ctype($this->_get_ctype(), 1)) ? $this->ctype->get_id() : false; } $this->construct_after(); }