Example #1
0
 public function __construct(Db $db, $name)
 {
     $this->db = $db;
     $this->wheres = $db->wheres;
     $this->orders = $db->orders;
     $this->selects = $db->selects;
     $this->offset = $db->offset;
     $this->limit = $db->limit;
     $this->store = $db->store;
     $this->joins = $db->joins;
     $this->age = $db->getAge();
 }
Example #2
0
 public function __construct(Database $model)
 {
     $this->model = $model->inCache(false);
     $fileConfig = APPLICATION_PATH . DS . 'models' . DS . 'CrudS3' . DS . ucfirst(Inflector::camelize($model->db)) . DS . ucfirst(Inflector::camelize($model->table)) . '.php';
     if (File::exists($fileConfig)) {
         $this->config = (include $fileConfig);
     } else {
         $this->config = [];
     }
     if (!empty($this->config)) {
         $this->prepareFields();
     }
     if (get_magic_quotes_gpc()) {
         $_REQUEST = $this->stripslashes($_REQUEST);
     }
 }
Example #3
0
 public function __construct()
 {
     $class = get_called_class();
     $newClass = strtolower(str_replace(['Thin\\', '_Db'], '', $class));
     list($db, $table) = explode('_', $newClass, 2);
     if (empty($table)) {
         $table = $db;
         $db = SITE_NAME;
     }
     $this->db = Db::instance($db, $table);
 }
Example #4
0
 public function from($what)
 {
     if (fnmatch("*.*", $what)) {
         list($this->db, $this->table) = explode('.', $what, 2);
     } else {
         $this->db = SITE_NAME;
         $this->table = $what;
     }
     $this->collection = $this->db . '.' . $this->table;
     $this->orm = Db::instance($this->db, $this->table);
     return $this;
 }
Example #5
0
 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 (empty($args)) {
         return Db::instance($database, $table);
     } elseif (count($args) == 1) {
         $id = current($args);
         if (is_numeric($id)) {
             return Db::instance($database, $table)->find((int) $id);
         }
     }
 }
Example #6
0
 public function __construct($ns)
 {
     $this->ns = $ns;
     $this->db = Db::instance('core', 'log');
 }
Example #7
0
 public function pivot($table, $id)
 {
     $cursor = new self(Db::instance($this->db->db, $table));
     $row = $cursor->getRow($id);
     if (!is_null($this->closure)) {
         if (is_callable($this->closure)) {
             $callable = $this->closure;
             $row = $callable($row);
         }
     }
     return $row;
 }
Example #8
0
 public static function hasTable($collection)
 {
     if (fnmatch('*.*', $collection)) {
         list($db, $table) = explode('.', $collection, 2);
     } else {
         $db = SITE_NAME;
         $table = $collection;
     }
     return !self::instance(Db::instance($db, $table))->isEmpty();
 }
Example #9
0
 public static function generate($database, $model, $fields = [], $overwrite = false)
 {
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudS3')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudS3');
     }
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudS3' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudS3' . DS . ucfirst(Inflector::camelize($database)))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudS3' . 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);
     }
 }
Example #10
0
 /**
  * [with description]
  *
  * @method with
  *
  * @param  [type]  $what   [description]
  * @param  boolean $object [description]
  *
  * @return [type]          [description]
  */
 public function with($what, $object = false)
 {
     $collection = $ids = $foreigns = $foreignsCo = [];
     if (is_string($what)) {
         if (fnmatch('*,*', $what)) {
             $what = explode(',', str_replace(' ', '', $what));
         }
         $res = $this->get(null, $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->get(null, $object);
     }
     if ($res->count() > 0) {
         foreach ($res as $r) {
             if (is_object($r)) {
                 $row = $r->toArray();
             } 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)])->get(null, $object);
                 if ($foreigns->count() > 0) {
                     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)])->get(null, $object);
                     if ($foreigns->count() > 0) {
                         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;
 }
Example #11
0
 public function pivot($table, $id)
 {
     $cursor = new self(Db::instance($this->db->db, $table));
     return $cursor->getRow($id);
 }
Example #12
0
 public function related()
 {
     $fields = func_get_args();
     foreach ($fields as $field) {
         if (fnmatch('*_*', $field)) {
             list($db, $table) = explode('_', $field, 2);
         } else {
             $table = $field;
             $db = SITE_NAME;
         }
         $fid = isAke($this->_data, $field . '_id', false);
         if ($fid) {
             $row = Db::instance($db, $table)->find((int) $fid, false);
             $this->_data[$key] = $row;
         }
     }
     return $this;
 }
Example #13
0
 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 .= '&nbsp;&nbsp;<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>&nbsp;&nbsp;';
         }
         $search .= '&nbsp;&nbsp;<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>&nbsp;&nbsp;';
     }
     $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 .= '&nbsp;&nbsp;<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="<">&lt;</option>
             <option value=">">&gt;</option>
             <option value="<=">&le;</option>
             <option value=">=">&ge;</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 .= '&nbsp;&nbsp;<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="<">&lt;</option>
                 <option value=">">&gt;</option>
                 <option value="<=">&le;</option>
                 <option value=">=">&ge;</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 .= '&nbsp;&nbsp;<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">';
 }