/** * Create a database handle * Factory method * @param array params * engine - pdo * type - mysql * * @return \Doctrine\DBAL\Connection */ public static function get_doctrine($id = self::DEFAULT_CONNECTION, array $config = array()) { if (empty($config) && !isset(self::$dbs[$id])) { throw new dbal_exception('Try to get unloaded db connection : ' . $id); } $engine = @$config['engine'] ?: 'pdo_mysql'; if (isset(self::$dbs[$id])) { return self::$dbs[$id]; } core::dprint('[dbloader|doctrine::get] ' . $id . '(' . $engine . ')'); if ($engine == 'null') { if (!class_exists('null_db', 0)) { require "modules/core/dbal/null.php"; } $conn = new null_db(); self::$dbs[$id] = $conn; } else { $d_config = new \Doctrine\DBAL\Configuration(); $d_config->setSQLLogger(new \SatCMS\Modules\Core\Dbal\Doctrine\Logger()); /* * 'dbname' => @$config['database'] , 'user' => @$config['login'] ?: 'root' , 'password' => @$config['password'] , 'host' => @$config['server'] ?: 'localhost' , 'driver' => $engine , 'path' => (isset($config['path']) ? loader::get_root($config['path']) : null) */ $connection_params = array('driver' => $engine, 'prefix' => @$config['prefix'], 'charset' => 'UTF8'); unset($config['engine']); // fix path if (isset($config['path'])) { $config['path'] = loader::get_root($config['path']); } // merge params $connection_params = array_merge($connection_params, $config); core::dprint_r($connection_params); try { $conn = \Doctrine\DBAL\DriverManager::getConnection($connection_params, $d_config); self::$dbs[$id] = $conn; } catch (Exception $e) { core::dprint($e->getMessage()); return false; } } return self::$dbs[$id]; }
/** * Change item * * @param array data (mostly post) * @param mixed ID if update, false if new * @return integer new item id */ function modify($data, $id = false) { $this->filter_input_data($data); $this->_last_id = false; if (false === $this->modify_before($data)) { return false; } if (!$id && false === $this->create_before($data)) { return false; } // // Insert // if (empty($id)) { if ($this->config->get('debug')) { core::dprint_r(array('NEW-ITEM:', $data)); } // create new item $elm = $this->_alloc($data); // dyn append to list $this->append($elm, $elm->id); // dyn append to list $this->_last_id = $this->is_key_autoincrement() ? $elm->get_id() : count($this->items); } else { $elm = $this->get_item_by_id($id); if ($elm === false) { throw new collection_exception("Modify unloaded/removed item #{$id}"); } $elm->modify($data); $this->_last_id = $elm->id; } if (!$id) { $this->create_after($data, $this->_last_id); } $this->modify_after($data, $this->_last_id); return $this->_last_id; }
/** * update fields for parent item */ function update_field_values($data) { core::dprint('efs_collection::update_field_values', core::E_DEBUG4); $fvs = $this->get_field_values(); foreach ($this as $v) { $fv = $v->get_field_value(); $value = $v->efs_format_modify(@$data[$v->name]); $value = $v->efs_format_serialize($value); if (!$fv) { // create one $fv = $this->_extra_fs_values_collection->alloc(); $fv->ctype_id = $this->get_parent()->get_ctype_id(); $fv->pid = $this->get_parent()->get_id(); $fv->fid = $v->get_id(); } else { // update } $fv->value = $value; $fv->save(); ///debug core::dprint(array('ex_ [%d%s] %s %s ', $fv->id, $fv->is_new() ? '+' : '-', $v->name, $fv->value)); $v->get_field_value(true); } core::dprint_r($this->_extra_fs_values_collection->as_array()); ///debug core::dprint(array('ex_ count %d ', $this->_extra_fs_values_collection->count())); }
/** * Format all fields * Calls to container method * @param array data * @param string type {@see format_field} * @throws tf_exception */ public function format_fields(&$data, $type = false) { if (!$type) { throw new collection_exception('Undefined format type'); } if (!is_array($data)) { core::dprint_r($data); throw new collection_exception('Data wrong type: ' . gettype($data)); } $container = $this->get_container(); $vfs_keys = $this->get_fields_keys(); foreach ($vfs_keys as $k) { if (!isset($data[$k])) { $data[$k] = ''; } $v =& $data[$k]; $current = $this->get_data($k); /* Fields modificators: + autosave + default + make_seo */ $skip = false; if ('modify' == $type) { if (!$this->in_working_set($k)) { $skip = true; } else { if ($vf = $this->field($k)) { // make seo if (isset($vf['make_seo']) && !empty($vf['make_seo'])) { $with_translit = false; $with_strict = false; if (is_array($vf['make_seo'])) { if (isset($vf['make_seo']['key'])) { // new syntax: key, translit, strict (without spaces) $with_strict = @$vf['make_seo']['strict']; $with_translit = @$vf['make_seo']['translit']; $vf['make_seo'] = $vf['make_seo']['key']; } else { $with_translit = $vf['make_seo'][1]; $vf['make_seo'] = $vf['make_seo'][0]; } // fill url, if empty if (empty($v) && $vf['make_seo'] && isset($data[$vf['make_seo']])) { $v = $data[$vf['make_seo']]; } } else { // oldstyle: make_seo = 1|true switch $with_translit = true; } if ($with_translit && !empty($v)) { $v = functions::translit($v); } // something bad trigger this code if (empty($v)) { $v = uniqid(); } else { $v = preg_replace('/[^а-яёa-z\\-\\_0-9[:space:]]/ui', '', $v); if ($with_strict) { $v = preg_replace('/\\s{1,}/u', '-', $v); } $v = strings::strtolower(trim($v)); // @todo fix name spaces if (($this->is_new() || empty($v)) && !empty($data[$vf['make_seo']])) { $v = $this->_unique_alias($k, @$vf['space'], $data); } } } // autosave if (isset($vf['autosave']) && $vf['autosave'] && !$this->is_new()) { $data[$k] = $this->get_data($k); } // default if (isset($vf['default']) && $this->is_new() && empty($data[$k])) { $data[$k] = $vf['default']; // for unixtime 'now' set, parsed in collection::on_modify to time() } } } } if (!$skip) { $v = $container->format_field($k, $v, $type, $current, $this); } } }
/** * Route request * * Warn! no exceptions * * @return bool false if no routes found * @throws controller_exception, router_exception */ function route($parts) { $this->_uri = implode('/', $parts); if (is_callable(array($this, 'route_before'))) { $this->route_before($parts); } core::dprint(array('[route] %s using defaut router, mod: %s', $this->_uri, $this->context->get_name())); // give up loading routes if set in routers class if (empty($this->_routes)) { $this->_routes = $this->load_routes(); } if (empty($this->_routes)) { core::dprint('Empty routes in ' . get_class($this), core::E_ERROR); return false; } foreach ($this->_routes as $id => $route) { // normalize if (!isset($route['match']) && !isset($route['regex'])) { $route['match'] = $id; } if (!isset($route['action'])) { $route['action'] = $id; } if (!isset($route['type'])) { $route['type'] = 'method'; } // class if (!isset($route['template'])) { $route['template'] = $id; } if ($route['action'] instanceof Closure) { $route['type'] = 'inline'; } if ($route['type'] == 'method') { $route['action'] = str_replace('/', '_', $route['action']); } // append section to match if any // if (isset($route['section']) && !empty($route['match'])) $route['match'] = $route['section'] . '/' . $route['match']; $this->_filters = array(); $back_uri = $this->_uri; // match filters // all filters created before dispatch! $this->match_filters($route); // route $params = null; if ($this->_debug) { core::dprint('.. route ' . $id); core::dprint_r($route); } if ($this->_is_route_matched($route, $params)) { // pure ajax routes if (isset($route['ajax']) && true !== loader::in_ajax()) { throw new router_exception('Invalid query. Code.A6299'); } if (isset($route['auth']['level'])) { if ($route['auth']['level'] > core::lib('auth')->get_user()->level) { throw new router_exception('Access denied. Code.A6298'); } } core::dprint(array('Route matched "%s"', $id)); $this->_route = $route; $this->context->get_controller()->run($route, $params); $this->run_filters(); return true; } // restore uri, loop again? $this->_uri = $back_uri; } return false; }
/** * Dump iatems (debug) */ function dump() { core::dprint($this->id . ' : '); core::dprint_r($this->data); }
/** * Register module * @throws modules_exception * @return core_module */ public function register($module, $params = null) { $module_class = isset($params['class']) ? $params['class'] : $module; $module_class = (isset($params['prefix']) ? $params['prefix'] : loader::CLASS_PREFIX) . $module_class; $module_path_orig = loader::DIR_MODULES . $module . '/'; $module_path = loader::get_public(); $module_path .= isset($params['path']) ? $params['path'] : loader::DIR_MODULES . $module; $module_path .= '/'; $module_file = $module_path; $module_file .= (isset($params['file']) ? $params['file'] : 'module') . loader::DOT_PHP; core::dprint(array('module::register %s, %s', $module, $module_class), core::E_DEBUG0); if (!fs::file_exists($module_file)) { core::dprint_r(array($module_class, $module_file)); throw new module_exception('Failed to register module ' . $module . '. File does not exists'); } require_once $module_file; if (!class_exists($module_class, 0)) { throw new module_exception('Cant load module ' . $module . ', wrong config?'); } // autotag module, if alternative class used if (!isset($params['tag']) && !empty($params['class'])) { $params['tag'] = $module; } $module_path = loader::fix_path(loader::get_public() . (!empty($params['chroot']) ? $module_path : $module_path_orig)); $this->set($module, new $module_class($module_path, $params)); $newb = $this->get($module); $newb->init_config($this->_get_module_config($module), abs_config::INIT_APPEND); return $newb; }
/** * Item data submit * * @param array data (mostly post) * @param mixed ID if update, false if new * @return integer new item id */ function modify($data, $id = false) { // do something new if (empty($id)) { if ($this->get_cfg_var('debug')) { core::dprint_r(array('NEW', $data)); } // create new item $newb = new $this->item_class($this, $this->config, $data); // dyn append to list $this->items[$newb->id] = $newb; return $newb->id; } else { // update $elm = $this->get_item_by_id($id); if ($elm === false) { throw new collection_exception("Modify unloaded/removed item #{$id}"); } $elm->modify($data); return $elm->id; } return false; }