newRecord = true; } public function isDir() { return empty($this->file); } public function save() { try { if ($this->newRecord) { if (!isset($this->id)) { $sql = "INSERT INTO documents (name, created_at, target_id, target_type, parent, file) VALUES ('" . $this->name . "', NOW(), '" . $this->target_id . "', '" . $this->target_type . "', '" . $this->parent . "', '" . $this->file . "')"; } else { $sql = "INSERT INTO documents (id, name, created_at, target_id, target_type, parent, file) VALUES ('" . $this->id . "', '" . $this->name . "', NOW(), '" . $this->target_id . "', '" . $this->target_type . "', '" . $this->parent . "', '" . $this->file . "')"; } mysql_query($sql); $err = mysql_errno(); if ($err > 0) { echo mysql_error(); return false; } if (!isset($this->id)) { $this->id = mysql_insert_id(); } } else { throw new Exception("no document update implemented"); } return true; } catch (Exception $e) { echo $e->getMessage(); return false; } } public function saveNewName() { try { $sql = "UPDATE documents SET name='" . $this->name . "' WHERE id='" . $this->id . "' and target_id = '" . $this->target_id . "' and target_type = '" . $this->target_type . "'"; mysql_query($sql); return true; } catch (Exception $e) { echo $e->getMessage(); return false; } } public function getIsNewRecord() { return $this->newRecord; } public static function deleteAll($condition = '') { throw new Exception("deleteAll not implemented"); } public static function className() { return get_called_class(); } public static function findOne($id) { throw new Exception("findOne only by id not implemented"); } public static function findById($targetId, $targetType, $id) { $sql = "SELECT * FROM documents WHERE id='$id' and target_id = '$targetId' and target_type = '$targetType'"; $rez = mysql_query($sql); if (mysql_num_rows($rez)) { $document = new Document(); $document->newRecord = false; $dbObject = mysql_fetch_array($rez); $document->id = $dbObject['id']; $document->name = $dbObject['name']; $document->target_id = $dbObject['target_id']; $document->target_type = $dbObject['target_type']; $document->parent = $dbObject['parent']; $document->created_at = $dbObject['created_at']; $document->file = $dbObject['file']; return $document; } return null; } public static function getChildren($targetId, $targetType, $parentId) { $sql = "SELECT * FROM documents WHERE parent='$parentId' and target_id = '$targetId' and target_type = '$targetType'"; $rez = mysql_query($sql); $documents = array(); if (mysql_num_rows($rez) > 0) { while ($dbObject = mysql_fetch_assoc($rez)) { $document = new Document(); $document->newRecord = false; $document->id = $dbObject['id']; $document->name = $dbObject['name']; $document->target_id = $dbObject['target_id']; $document->target_type = $dbObject['target_type']; $document->parent = $dbObject['parent']; $document->created_at = $dbObject['created_at']; $document->file = $dbObject['file']; $documents[] = $document; } } return $documents; } public function delete() { $sql = "DELETE FROM documents WHERE id='" . $this->id . "' and target_id = '" . $this->target_id . "' and target_type = '" . $this->target_type . "'"; mysql_query($sql); if (!$this->isDir() && stripos($this->file, "http://") === false) { unlink($_SERVER['DOCUMENT_ROOT'] . $this->file); } } }