db = $db; } public function create($user, $notrigger = 0) { $this->ref = $this->getNextNumRef(); $this->date_creation = dol_now(); $this->fk_user = $user->id; $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element; $sql .= " (ref, label, description, fk_user, status, priority,"; $sql .= " date_start, date_end, date_creation, entity)"; $sql .= " VALUES ("; $sql .= "'" . $this->db->escape($this->ref) . "',"; $sql .= "'" . $this->db->escape($this->label) . "',"; $sql .= "'" . $this->db->escape($this->description) . "',"; $sql .= (int) $this->fk_user . ","; $sql .= (int) $this->status . ","; $sql .= (int) $this->priority . ","; $sql .= ($this->date_start ? "'" . $this->db->idate($this->date_start) . "'" : "NULL") . ","; $sql .= ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "NULL") . ","; $sql .= "'" . $this->db->idate($this->date_creation) . "',"; $sql .= $conf->entity; $sql .= ")"; $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); return $this->id; } $this->error = $this->db->lasterror(); return -1; } public function fetch($id, $ref = '') { $sql = "SELECT * FROM " . MAIN_DB_PREFIX . $this->table_element; $sql .= " WHERE entity IN (" . getEntity($this->element) . ")"; if ($id) { $sql .= " AND rowid = " . (int) $id; } elseif ($ref) { $sql .= " AND ref = '" . $this->db->escape($ref) . "'"; } $resql = $this->db->query($sql); if ($resql && $this->db->num_rows($resql)) { $obj = $this->db->fetch_object($resql); $this->id = $obj->rowid; $this->ref = $obj->ref; $this->label = $obj->label; $this->description = $obj->description; $this->fk_user = $obj->fk_user; $this->status = $obj->status; $this->priority = $obj->priority; $this->date_start = $this->db->jdate($obj->date_start); $this->date_end = $this->db->jdate($obj->date_end); $this->date_creation = $this->db->jdate($obj->date_creation); $this->tms = $this->db->jdate($obj->tms); return 1; } return 0; } public function update($user, $notrigger = 0) { $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET"; $sql .= " label = '" . $this->db->escape($this->label) . "',"; $sql .= " description = '" . $this->db->escape($this->description) . "',"; $sql .= " status = " . (int) $this->status . ","; $sql .= " priority = " . (int) $this->priority . ","; $sql .= " date_start = " . ($this->date_start ? "'" . $this->db->idate($this->date_start) . "'" : "NULL") . ","; $sql .= " date_end = " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "NULL"); $sql .= " WHERE rowid = " . (int) $this->id; $resql = $this->db->query($sql); if ($resql) { return 1; } $this->error = $this->db->lasterror(); return -1; } public function delete($user, $notrigger = 0) { $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element; $sql .= " WHERE rowid = " . (int) $this->id; $resql = $this->db->query($sql); if ($resql) { return 1; } $this->error = $this->db->lasterror(); return -1; } }