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); } elseif (count($args) == 1) { $id = Arrays::first($args); if (is_numeric($id)) { return Db::instance($database, $table)->find($id); } } }
public function __construct($ns) { $this->ns = $ns; $this->db = Db::instance('core', 'log'); }
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">'; }
public static function generate($database, $model, $fields = [], $overwrite = false) { if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw')) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw'); } $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php'; if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)))) { File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . 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); } }