db = DataBase::getDB(); $this->config = new Config(); $this->check = new Check(); $this->url = new URL(); $this->table_name = $this->config->db_prefix.$table_name; $this->format = new Format(); } public function add($data){ if(!$this->checkAddData($data)) return false; $query = "INSERT INTO `".$this->table_name."` ("; foreach($data as $key => $value) $query.=$key.","; $query = substr($query, 0, -1); $query.= ") VALUES ("; foreach($data as $key => $value) $query.=$this->config->sym_query.","; $query = substr($query, 0, -1); $query.= ")"; return $this->db->query($query, array_values($data)); } private function checkAddData($data){ $result = $this->checkMethod($data); if($result === true) return true; else { $sm = new SystemMessage(); return $sm->message($result); } } protected function checkMethod($data){ return false; } public function getAll($order = false, $up = true, $count = false, $offset = false){ $ol = $this->getOL($order, $up, $count, $offset); $query = "SELECT * FROM `".$this->table_name."` $ol"; return $this->db->select($query); } protected function getAllOnField($field, $value, $order = false, $up = true, $count = false, $offset = false){ $ol = $this->getOL($order, $up, $count, $offset); $query = "SELECT * FROM `".$this->table_name."` WHERE `$field` = ".$this->config->sym_query." $ol"; return $this->db->select($query, array($value)); } protected function getOnField($field, $value, $order = false, $up = false){ $ol = $this->getOL($order, $up, $count, $offset); $query = "SELECT * FROM `".$this->table_name."` WHERE `$field` =".$this->config->sym_query." $ol"; return $this->db->selectRow($query, array($value)); } protected function getOL($order, $up, $count, $offset){ if($order){ $order = "ORDER BY $order"; if(!$up) $order.= " DESC"; } $limit = $this->getL($count, $offset); return "$order $limit"; } public function getL($count, $offset){ if((!$count) && (!$this->check->count($count))) return false; if($offset){ if(!$this->check->offset) return false; $limit = "LIMIT $count $offset"; } else $limit = "LIMIT $count"; return $limit; } protected function transform($element, $recomend = false){ if(!$element) return false; if(isset($element[0])){ for($i = 0; $i < count($element); $i++) $element[$i] = $this->transformElement($element[$i]); return $element; } return $this->transformElement($element); } public function get($id){ return $this->getOnField("id", $id); } public function getTableName(){ return $this->table_name; } public function isExistID($id){ if(!$this->check->id($id)) return false; return $this->getOnField("id", $id); } public function selectFieldFromCell($field_in, $value_in, $field_out){ $query = "SELECT `$field_out` FROM `".$this->table_name."` WHERE `$field_in` = ".$this->config->sym_query.""; return $this->db->selectCell($query, array($value_in)); } public function selectOnIDs($ids, $select = "*"){ for($i = 0; $i < count($ids); $i++){ $query_id_list .= $this->config->sym_query.","; } $query_id_list = substr($query_id_list, 0, -1); $query = "SELECT $select FROM ".$this->table_name." WHERE `id` IN ($query_id_list)"; return $this->db->select($query, $ids); } public function search($q, $fields){ if($q == "") return false; if(!$fields) return false; $q = trim($q); $q = preg_replace("/\s\s+/", " ", $q); $q = mb_strtolower($q); $array_keys = explode(" ", $q); $logic = " AND "; $where = ""; $params = array(); for($i = 0; $i < count($array_keys); $i++) { if(isset($array_keys[$i - 1])) $where .= $logic; for($j = 0; $j < count($fields); $j++){ $where .= "`$fields[$j]` LIKE ".$this->config->sym_query; $params[] = "%$array_keys[$i]%"; if(isset($fields[$j + 1])) $where .= " OR "; } } $query = "SELECT * FROM `".$this->table_name."` WHERE $where"; return $this->db->select($query, $params); } } ?>