public static function instance($from) { if (fnmatch('*::*', $from)) { list($database, $table) = explode('::', $from, 2); $instance = Db::instance($database, $table); } else { $instance = Db::instance(SITE_NAME, $from); } return $instance; }
public function __construct() { $class = get_called_class(); if (strstr($class, '\\')) { $class = Arrays::last(explode('\\', $class)); $class = str_replace('Model_', '', $class); } if (fnmatch('*_*', $class)) { list($database, $table) = explode('_', $class, 2); } else { $database = SITE_NAME; $table = $class; } self::$db = Db::instance($database, $table); $this->model = self::$db->model(func_get_args()); }
private function run($what, array $task) { $dbTask = Db::instance('queue', 'task'); $dbInstance = Db::instance('queue', 'instance'); $check = $dbInstance->where(['task_id', '=', $task['id']])->cursor()->count(); if ($check == 0) { $instance = $dbInstance->create(['task_id' => $task['id']])->save(); call_user_func_array($what, $task['args']); $dt = $dbTask->find($task['id']); if ($dt) { $dt->delete(); } $instance->delete(); Db::instance('queue', 'history')->create(['task' => $task, 'execution_time' => time()])->save(); } }
public function __construct($type, $model, $inCache = true) { if (!is_object($model)) { throw new Exception("The first argument is not a model."); } if ($model instanceof Container) { $motor = 'dbjson'; } else { $motor = 'dbredis'; } $this->db = Db::instance(SITE_NAME, $type)->inCache($inCache); $this->optDb = $model->db()->db; $this->optTable = $model->db()->table; $this->optMotor = $motor; $this->id = $model->id; }
public static function __callStatic($method, $args) { $db = Inflector::uncamelize($method); if (fnmatch('*_*', $db)) { list($database, $table) = explode('_', $db, 2); } else { $database = SITE_NAME; $table = $db; } if (!count($args)) { return Db::instance($database, $table)->reset(); } elseif (count($args) == 1) { $id = Arrays::first($args); if (is_numeric($id)) { return Db::instance($database, $table)->reset()->find($id); } } }
public function checkGlue($row) { if (!empty($this->glues)) { foreach ($this->glues as $field) { $one = isAke($row, $field . '_id', false); if ($one) { $data = Db::instance($this->db->db, $field)->find((int) $one, false); $row[$field] = $data; } else { if ($field[strlen($field) - 1] == 's' && isset($row['id']) && $field[0] != '_') { $db = Db::instance($this->db->db, substr($field, 0, -1)); $idField = $this->db->table . '_id'; $row[$field] = $db->where([$idField, '=', (int) $row['id']])->cursor()->toArray(); } } } } return $row; }
private function dispatch($uri) { if (fnmatch('authorize/*/*', $uri) && !fnmatch('authorize/*/*/*', $uri) && $this->isGet()) { list($publicKey, $privateKey) = explode('/', str_replace('authorize/', '', $uri), 2); return $this->authorize($publicKey, $privateKey); } else { if (fnmatch('*/*/*/*', $uri) && !fnmatch('*/*/*/*/*', $uri)) { list($token, $collection, $method, $args) = explode('/', $uri, 4); } if (fnmatch('*/*/*', $uri)) { list($token, $collection, $method) = explode('/', $uri, 34); } if (strlen($token) == 40) { $this->token = $token; $check = $this->checkToken(); if (fnmatch('*.*', $collection)) { list($db, $table) = explode('.', $collection, 2); $this->db = Db::instance($db, $table); } else { $this->db = Db::instance(SITE_NAME, $collection); } if (strstr($method, '?')) { list($method, $query) = explode('?', $method, 2); $query = urldecode($query); parse_str($query, $query); foreach ($query as $k => $v) { $_REQUEST[$k] = $v; } } $methods = get_class_methods($this); if (in_array($method, $methods)) { return isset($args) ? $this->{$method}([$args]) : $this->{$method}(); } } } Api::forbidden(); }
public static function make($db, $what) { if (is_string($what)) { $file = APPLICATION_PATH . DS . 'models' . DS . 'Bigdata' . DS . 'views' . DS . $db->db . DS . ucfirst(I::lower($db->table)) . DS . I::camelize($what) . '.php'; if (files()->exists($file)) { require_once $file; $class = '\\Thin\\' . ucfirst(I::lower($db->db)) . ucfirst(I::lower($db->table)) . 'View'; return $class::make($db); } else { return $db; } } elseif (A::is($what)) { $nameview = 'view_' . $db->db . '_' . $db->table . '_' . sha1(serialize($what)); $ageDb = $db->getage(); $viewDb = Db::instance($db->db, $nameview); $ageView = $db->getage(); $exists = strlen($db->cache()->get('dbRedis.views.' . $nameview)) ? true : false; if ($ageView < $ageDb || !$exists) { $viewDb->getCollection()->remove(); foreach ($what as $wh) { $op = 'AND'; if (count($wh) == 4) { $op = $wh[3]; unset($wh[3]); } $db = $db->where($wh); } $res = $db->exec(); foreach ($res as $row) { $viewDb->saveView($row); } $db->cache()->set('dbRedis.views.' . $nameview, 1); } return $viewDb; } }
public function __construct($ns) { $this->ns = $ns; $this->db = Db::instance('core', 'log'); }
public static function generate($database, $model, $fields = [], $overwrite = false) { if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRedis')) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRedis'); } $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudRedis' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php'; if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRedis' . DS . ucfirst(Inflector::camelize($database)))) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRedis' . DS . ucfirst(Inflector::camelize($database))); } if (!File::exists($file) || $overwrite) { $db = Db::instance($database, $model); $crud = Crud::instance($db); File::delete($file); $tplModel = File::read(__DIR__ . DS . 'Model.tpl'); $tplField = File::read(__DIR__ . DS . 'Field.tpl'); $fields = empty($fields) ? $crud->fields() : $fields; $singular = ucfirst($model); $plural = $singular . 's'; $default_order = $crud->pk(); $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##foreigns##', '##uniques##', '##soft_delete##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$singular, $plural, $default_order, '[]', '[]', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false'], $tplModel); $fieldsSection = ''; foreach ($fields as $field) { if ($field != $crud->pk()) { $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field)); $fieldsSection .= str_replace(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, 'text', 'false', 'true', 'false', 'false', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'false', 'false', 'false', 'false', $label], $tplField); } } $tplModel = str_replace('##fields##', $fieldsSection, $tplModel); File::put($file, $tplModel); } }
public function __construct(Db $from) { $this->from = $from; $this->to = Db::instance('transaction', $from->db . '_' . $from->table . '_' . time()); $this->populate(); }
public function with($what, $object = false) { $collection = $ids = $foreigns = $foreignsCo = []; if (is_string($what)) { if (fnmatch('*,*', $what)) { $what = explode(',', str_replace(' ', '', $what)); } $res = $this->exec($object); } elseif (is_array($what)) { foreach ($what as $key => $closure) { $what = $key; break; } if (fnmatch('*,*', $what)) { $what = str_replace(' ', '', $what); $what = explode(',', $what); } $db = $this; call_user_func_array($closure, [$db]); $res = $db->exec($object); } if (!empty($res)) { foreach ($res as $r) { if (is_object($r)) { $row = $r->assoc(); } else { $row = $r; } if (is_string($what)) { $value = isAke($row, $what . '_id', false); if (false !== $value) { if (!in_array($value, $ids)) { array_push($ids, $value); } } } elseif (is_array($what)) { foreach ($what as $fk) { if (!isset($ids[$fk])) { $ids[$fk] = []; } $value = isAke($row, $fk . '_id', false); if (false !== $value) { if (!in_array($value, $ids[$fk])) { array_push($ids[$fk], $value); } } } } } if (!empty($ids)) { if (is_string($what)) { $db = Db::instance($this->db, $what); $foreigns = $db->where(['id', 'IN', implode(',', $ids)])->exec($object); if (!empty($foreigns)) { foreach ($foreigns as $foreign) { $id = $object ? (int) $foreign->id : (int) $foreign['id']; $foreignsCo[$id] = $foreign; } } } elseif (is_array($what)) { foreach ($what as $fk) { $idsFk = $ids[$fk]; $db = Db::instance($this->db, $fk); $foreigns = $db->where(['id', 'IN', implode(',', $idsFk)])->exec($object); if (!empty($foreigns)) { foreach ($foreigns as $foreign) { $id = $object ? $foreign->id : $foreign['id']; $foreignsCo[$fk][$id] = $foreign; } } } } if (!empty($foreignsCo)) { if (is_string($what)) { $whatId = $what . '_id'; foreach ($res as $r) { if (is_object($r)) { if (isset($r->{$whatId})) { if (isset($foreignsCo[$r->{$whatId}])) { $r->{$what} = $foreignsCo[$r->{$whatId}]; } } } else { if (isset($r[$whatId])) { if (isset($foreignsCo[$r[$whatId]])) { $r[$what] = $foreignsCo[$r[$whatId]]; } } } array_push($collection, $r); } } elseif (is_array($what)) { foreach ($res as $r) { foreach ($what as $fk) { $fkId = $fk . '_id'; if (is_object($r)) { if (isset($r->{$fkId})) { if (isset($foreignsCo[$fk])) { if (isset($foreignsCo[$fk][$r->{$fkId}])) { $r->{$fk} = $foreignsCo[$fk][$r->{$fkId}]; } } } } else { if (isset($r[$fkId])) { if (isset($foreignsCo[$fk])) { if (isset($foreignsCo[$fk][$r[$fkId]])) { $r[$fk] = $foreignsCo[$fk][$r[$fkId]]; } } } } } array_push($collection, $r); } } } } } return $collection; }
public function countThrough($t1, $t2) { $database = $this->_db->db; $db1 = Db::instance($database, $t1); $fk = $this->_db->table . '_id'; $sub = $db1->where([$fk, '=', (int) $this->_data['id']])->cursor(); $ids = []; foreach ($sub as $row) { $ids[] = $row['id']; } $fk2 = $t1 . '_id'; return Db::instance($database, $t2)->where([$fk2, 'IN', implode(',', $ids)])->cursor()->count(); }
public function makeSearch() { $fieldsSettings = Db::instance('core', 'datafieldssettings')->where("table = " . $this->model->table)->where('database = ' . $this->model->db)->where('user_id = ' . auth()->user()->getId())->exec(); $userSettings = []; if (count($fieldsSettings)) { foreach ($fieldsSettings as $fieldSettings) { foreach ($fieldSettings as $k => $v) { if (strstr($k, 'is_')) { $userSettings[$fieldSettings['field']][$k] = 1 == $v ? true : false; } } } } $fields = $this->fields(); $fieldInfos = isAke($this->config, 'fields'); $where = isAke($_REQUEST, 'crud_where', session('dataTableCrudRedis::' . $this->model->table)->getWhere()); $search = '<div class="row"><div class="col-md-10">' . NL; $queriesRecorded = Db::instance('core', 'dataquery')->where('user_id = ' . auth()->user()->getId())->where('table = ' . $this->model->table)->where('database = ' . $this->model->db)->exec(); if (count($queriesRecorded)) { $search .= '<div><a rel="tooltip" href="' . urlAction('saveddataqueries') . '/table/' . $this->model->table . '/database/' . $this->model->db . '" title="Accéder aux recherches enregistrées" class="btn btn-primary"><i class="fa fa-database"></i> <i class="fa fa-search"></i></a></div><br />'; } if (!empty($where)) { $queryRecorded = Db::instance('core', 'dataquery')->where('user_id = ' . auth()->user()->getId())->where('table = ' . $this->model->table)->where('database = ' . $this->model->db)->where('clause = ' . base64_encode($where))->first(true); $whereReadable = $this->readableQuery($where); $search .= '<span class="badge badge-lg alert-success">Recherche en cours : ' . $whereReadable . '</span>'; $exists = !is_null($queryRecorded); if (!$exists) { $search .= ' <a rel="tooltip" title="Sauvegarder cette recherche" class="btn btn-primary" href="' . urlAction('savedataquery') . '/table/' . $this->model->table . '/database/' . $this->model->db . '/query/' . base64_encode($where) . '"><i class="fa fa-save"></i></a> '; } $search .= ' <a class="btn btn-warning" href="#" onclick="document.location.href = \'' . urlAction('list') . '/table/' . $this->model->table . '/database/' . $this->model->db . '/kill/1\'"><i class="fa fa-trash-o icon-white"></i> Supprimer cette recherche</a> '; } $search .= '<button id="newCrudSearch" type="button" class="btn btn-info" onclick="$(\'#crudSearchDiv\').slideDown();$(\'#newCrudSearch\').hide();$(\'#hideCrudSearch\').show();"><i class="fa fa-search fa-white"></i> Effectuer une nouvelle recherche</button>'; $search .= ' <button id="hideCrudSearch" type="button" style="display: none;" class="btn btn-danger" onclick="$(\'#crudSearchDiv\').slideUp();$(\'#newCrudSearch\').show();$(\'#hideCrudSearch\').hide();"><i class="fa fa-power-off fa-white"></i> Masquer la recherche</button>'; $search .= '<fieldset id="crudSearchDiv" style="display: none;">' . NL; $search .= '<hr />' . NL; $i = 0; $fieldsJs = []; $js = '<script type="text/javascript">' . NL; $searchFields = ['created_at' => 'Date de création', 'updated_at' => 'Date de mise à jour']; foreach ($searchFields as $field => $label) { $fieldsJs[] = "'{$field}'"; $search .= '<div class="control-group">' . NL; $search .= '<label class="control-label">' . \Thin\Html\Helper::display($label) . '</label>' . NL; $search .= '<div class="controls" id="crudControl_' . $i . '">' . NL; $search .= '<select id="crudSearchOperator_' . $i . '"> <option value="=">=</option> <option value="<"><</option> <option value=">">></option> <option value="<=">≤</option> <option value=">=">≥</option> </select>' . NL; if ($field == 'id') { $search .= '<input pattern="\\\\d*" style="width: 150px;" type="text" id="crudSearchValue_' . $i . '" value="" />'; } elseif ($field == 'created_at' || $field == 'updated_at') { $search .= '<input class="crudDate" data-date-clear-btn="true" data-date-format="dd/mm/yyyy" style="width: 150px;" type="text" id="crudSearchValue_' . $i . '" value="" />'; } $search .= ' <span class="btn btn-success" href="#" onclick="addRowSearch(\'' . $field . '\', ' . $i . '); return false;"><i class="fa fa-plus"></i></span>'; $search .= '</div>' . NL; $search .= '</div><hr>' . NL; $i++; } foreach ($fields as $field) { $userInfos = isAke($userSettings, $field, []); $fieldSettings = isAke($fieldInfos, $field); $searchable = isAke($userInfos, 'is_searchable', isAke($fieldSettings, 'is_listable', true)); $label = isAke($fieldSettings, 'label', ucfirst($field)); $type = isAke($fieldSettings, 'type', 'text'); $closure = isAke($fieldSettings, 'content_search', false); if (true === $searchable) { $fieldsJs[] = "'{$field}'"; $search .= '<div class="control-group">' . NL; $search .= '<label class="control-label">' . \Thin\Html\Helper::display($label) . '</label>' . NL; $search .= '<div class="controls" id="crudControl_' . $i . '">' . NL; $search .= '<select id="crudSearchOperator_' . $i . '"> <option value="=">=</option> <option value="LIKE">Contient</option> <option value="NOT LIKE">Ne contient pas</option> <option value="START">Commence par</option> <option value="END">Finit par</option> <option value="<"><</option> <option value=">">></option> <option value="<=">≤</option> <option value=">=">≥</option> </select>' . NL; if (!$closure) { $search .= '<input style="width: 150px;" type="text" id="crudSearchValue_' . $i . '" value="" />'; } else { if (is_callable($closure)) { $customSearch = call_user_func_array($closure, array('crudSearchValue_' . $i)); $search .= $customSearch; } else { $search .= '<input style="150px;" type="text" id="crudSearchValue_' . $i . '" value="" />'; } } $search .= ' <span class="btn btn-success" href="#" onclick="addRowSearch(\'' . $field . '\', ' . $i . '); return false;"><i class="fa fa-plus"></i></span>'; $search .= '</div>' . NL; $search .= '</div>' . NL; $i++; } } $js .= 'var searchFields = [' . implode(', ', $fieldsJs) . ']; var numFieldsSearch = ' . ($i - 1) . ';'; $js .= '</script>' . NL; $search .= '<div class="control-group"> <div class="controls"> <button type="submit" class="btn btn-primary" name="Rechercher" onclick="makeCrudSearch();">Rechercher</button> </div> </div>' . NL; $search .= '</fieldset>' . NL; $search .= '</div><div class="col-md-2 clear"></div>' . NL . $js . NL; return $search . '</div></div><div class="wrapper">'; }