Пример #1
0
 public static function last_day_of_month($date, $format = false)
 {
     if ($date) {
         $m = false;
         if (\bbn\str\text::is_number($date)) {
             if ($date <= 12) {
                 $m = $date;
                 $y = date('Y');
             } else {
                 $m = (int) date('m', $date);
                 $y = date('Y', $date);
             }
         } else {
             if ($d = strtotime($date)) {
                 $m = (int) date('m', $d);
                 $y = date('Y', $d);
             }
         }
         if ($m) {
             $r = mktime(0, 0, -1, $m + 1, 1, $y);
             return $format ? date($format, $r) : $r;
         }
     }
 }
Пример #2
0
 public function load_config($id, $class = 'grid', $params = [])
 {
     if ($this->db) {
         if (\bbn\str\text::is_number($id) && ($obj = $this->db->rselect($this->admin_db . '.' . $this->prefix . 'objects', [], ["id" => $id]))) {
             $cfg = json_decode($obj['configuration'], 1);
             $cfg['class'] = $obj['class'];
             if (empty($cfg['url'])) {
                 $cfg['url'] = $id;
             }
             /*
                     if ( empty($cfg['select']) && \bbn\str\text::is_number($cfg['url']) ){
              $cfg['select'] = 'json/select/'.$id."/".implode("/", $params);
                     }
                     if ( empty($cfg['insert'])  && \bbn\str\text::is_number($cfg['url']) ){
              $cfg['insert'] = 'json/insert/'.$id."/".implode("/", $params);
                     }
                     if ( empty($cfg['update']) && \bbn\str\text::is_number($cfg['url']) ){
              $cfg['update'] = 'json/update/'.$id."/".implode("/", $params);
                     }
                     if ( empty($cfg['delete']) && \bbn\str\text::is_number($cfg['url']) ){
              $cfg['delete'] = 'json/delete/'.$id."/".implode("/", $params);
                     }
             * 
             */
             if (count($params) > 0) {
                 if (isset($cfg['select'])) {
                     $cfg['select'] .= "/" . implode("/", $params);
                 }
                 if (isset($cfg['insert'])) {
                     $cfg['insert'] .= "/" . implode("/", $params);
                 }
                 if (isset($cfg['update'])) {
                     $cfg['update'] .= "/" . implode("/", $params);
                 }
                 if (isset($cfg['delete'])) {
                     $cfg['delete'] .= "/" . implode("/", $params);
                 }
             }
             if (!empty($obj['description'])) {
                 $cfg['description'] = $obj['description'];
             }
             if (!is_null($obj['table'])) {
                 $cfg['table'] = $obj['table'];
             }
             $fields = $this->db->rselect_all($this->admin_db . '.' . $this->prefix . 'fields', [], ["id_obj" => $id], ["position" => "asc"]);
             foreach ($fields as $k => $f) {
                 $fields[$k] = json_decode($f['configuration'], 1);
                 if (isset($fields[$k]['sql'])) {
                     if (count($params) % 2 === 0 && isset($chplouif)) {
                         $fields[$k]['data'] = $this->db->get_rows($fields[$k]['sql'], $params[2]);
                     } else {
                         $fields[$k]['data'] = $this->db->get_rows($fields[$k]['sql']);
                     }
                 }
                 if (!is_null($f['column'])) {
                     $fields[$k]['column'] = $f['column'];
                 }
             }
             $cfg['elements'] = $fields;
         } else {
             if (is_array($id)) {
                 $cfg = $id;
             } else {
                 if ($class === 'form') {
                     if (is_object($params)) {
                         $cfg = $this->get_default_form_config($id, $params);
                     } else {
                         $cfg = $this->get_default_form_config($id);
                     }
                 } else {
                     $cfg = $this->get_default_grid_config($id, $params);
                 }
             }
         }
         return $cfg;
     }
 }
Пример #3
0
 /**
  * @return string
  */
 public function get_limit($limit, $start = 0)
 {
     if (is_array($limit)) {
         $args = $limit;
     } else {
         $args = func_get_args();
         if (is_array($args[0])) {
             $args = $args[0];
         }
     }
     if (count($args) === 2 && \bbn\str\text::is_number($args[0], $args[1])) {
         return " LIMIT {$args['1']}, {$args['0']}";
     }
     if (\bbn\str\text::is_number($args[0])) {
         return " LIMIT {$args['0']}";
     }
     return '';
 }
Пример #4
0
 /**
  * This will fetch the route to the controller for a given path. Chainable
  *
  * @param string $path The request path <em>(e.g books/466565 or xml/books/48465)</em>
  * @return void 
  */
 private function route($path = '')
 {
     if (!$this->is_routed && self::check_path($path)) {
         $this->is_routed = 1;
         $this->path = $path;
         $fpath = $path;
         // We go through each path, starting by the longest until it's empty
         while (strlen($fpath) > 0) {
             if ($this->get_controller($fpath)) {
                 if (strlen($fpath) < strlen($this->path)) {
                     $this->arguments = [];
                     $args = explode('/', substr($this->path, strlen($fpath)));
                     foreach ($args as $a) {
                         if (\bbn\str\text::is_number($a)) {
                             $a = (int) $a;
                         }
                         array_push($this->arguments, $a);
                     }
                     // Trimming the array
                     while (empty($this->arguments[0])) {
                         array_shift($this->arguments);
                     }
                     $t = end($this->arguments);
                     while (empty($t)) {
                         array_pop($this->arguments);
                         $t = end($this->arguments);
                     }
                 }
                 break;
             } else {
                 $fpath = strpos($fpath, '/') === false ? '' : substr($this->path, 0, strrpos($fpath, '/'));
             }
         }
         if (!$this->controller) {
             $this->get_controller('default');
         }
     }
     return $this;
 }
Пример #5
0
 /**
  * Sets the user ID that will be used to fill the user_id field
  * @return void 
  */
 public static function set_huser($huser)
 {
     // Sets the history table name
     if (\bbn\str\text::is_number($huser)) {
         self::$huser = $huser;
     }
 }
Пример #6
0
 public function css(array $cfg)
 {
     foreach ($cfg as $i => $k) {
         if (!\bbn\str\text::is_number($i)) {
             $this->css[$i] = $k;
         }
     }
     $this->update();
     return $this;
 }
Пример #7
0
 public function filter($filters)
 {
     $res = '';
     if ($this->check() && isset($filters['filters'])) {
         if (isset($filters['filters']) && count($filters['filters']) > 0) {
             $logic = isset($filters['logic']) && $filters['logic'] === 'or' ? 'OR' : 'AND';
             foreach ($filters['filters'] as $f) {
                 $ok = false;
                 if (empty($res)) {
                     $pre = " ( ";
                 } else {
                     $pre = " {$logic} ";
                 }
                 if (isset($f['logic'])) {
                     $res .= $pre . $this->filter($f);
                 } else {
                     if ($field = $this->get_field($f)) {
                         if ($this->structure && isset($this->structure['fields'][$f['field']])) {
                             if ($this->structure['fields'][$f['field']]['type'] === 'int' && $this->structure['fields'][$f['field']]['maxlength'] == 1 && !\bbn\str\text::is_integer($f['value'])) {
                                 $f['value'] = $f['value'] === 'true' ? 1 : 0;
                             }
                         }
                         $res .= $pre . $field . " ";
                         switch ($f['operator']) {
                             case 'eq':
                                 $res .= \bbn\str\text::is_number($f['value']) ? "= " . $f['value'] : "LIKE '" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'neq':
                                 $res .= \bbn\str\text::is_number($f['value']) ? "!= " . $f['value'] : "NOT LIKE '" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'startswith':
                                 $res .= "LIKE '" . $this->db->escape_value($f['value']) . "%'";
                                 break;
                             case 'endswith':
                                 $res .= "LIKE '%" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'gte':
                                 $res .= ">= '" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'gt':
                                 $res .= "> '" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'lte':
                                 $res .= "<= '" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'lt':
                                 $res .= "< '" . $this->db->escape_value($f['value']) . "'";
                                 break;
                             case 'contains':
                             default:
                                 $res .= "LIKE '%" . $this->db->escape_value($f['value']) . "%'";
                                 break;
                             case 'doesnotcontain':
                                 $res .= "NOT LIKE ? ";
                                 $f['value'] = '%' . $f['value'] . '%';
                                 break;
                         }
                     }
                 }
             }
             if (!empty($res)) {
                 $res .= " ) ";
             }
         }
     }
     return $res;
 }