Files
thetool/application/ConstructionConsentOwner/ConstructionConsentOwner.php
2025-03-25 15:28:03 +00:00

238 lines
6.7 KiB
PHP

<?php
class ConstructionConsentOwner extends mfBaseModel {
protected $forcestr = ["zip", "phone", "phone2", "fax", "email"];
private $consent;
private $files;
private $result_file;
protected function afterSave() {
$this->createHistory();
}
protected function beforeDelete() {
if(!$this->id) return true;
foreach($this->getProperty("files") as $file) {
$file->delete();
}
return true;
}
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
private function createHistory() {
if(!$this->id) return true;
$changed = $this->getChangedFields();
try {
foreach($changed as $field) {
$this->log->debug(__METHOD__ . ": $field changed from '" . $this->_old_data->$field . "' to '" . $this->data->$field . "'");
$history = ConstructionConsentHistory::create([
"constructionconsent_id" => $this->id,
"key" => "owner-".$this->id."-$field",
"old_value" => $this->_old_data->$field,
"new_value" => $this->data->$field
]);
$history->save();
}
} catch(Exception $e) {
$this->log->debug($e->getTraceAsString());
}
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "files") {
if(!$this->id) return [];
$files = ConstructionConsentOwnerFile::search(["constructionconsentowner_id" => $this->id]);
if(!count($files)) {
return [];
}
foreach($files as $file) {
$this->files[$file->filename] = $file;
}
return $this->files;
}
if($name == "consent") {
$consent = new ConstructionConsent($this->constructionconsent_id);
if($consent->id) {
$this->consent = $consent;
}
return $consent;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new ConstructionConsentOwner();
$table_fields = [
"constructionconsent_id", "title", "firstname", "lastname", "street", "zip", "city", "country",
"phone", "phone2", "birthdate", "fax", "email", "status", "result", "create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ConstructionConsentOwner", "*", "1 = 1 ORDER BY lastname,firstname");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentOwner($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsentOwner
WHERE $where
ORDER BY lastname,firstname LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentOwner($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM ConstructionConsentOwner
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "lastname,firstname ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsentOwner
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ConstructionConsentOwner($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("constructionconsent_id", $filter)) {
$constructionconsent_id = $filter['constructionconsent_id'];
if(is_numeric($constructionconsent_id)) {
$where .= " AND constructionconsent_id=$constructionconsent_id";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}