Files
thetool/application/ContractconfigValue/ContractconfigValue.php
2025-01-24 15:04:24 +01:00

132 lines
3.3 KiB
PHP

<?php
class ContractconfigValue extends mfBaseModel {
private $item;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
protected function afterSave() {
$this->runAfterSaveHooks();
}
public function set($new_value) {
// explicitly load item, because we might not have an id yet
$item = new ContractconfigItem($this->item_id);
$me = new User();
$me->loadMe();
$this->edit_by = $me->id;
if(!is_array($new_value) && ($new_value === null || strlen($new_value) == 0)) {
$this->int = null;
$this->number = null;
$this->string = null;
return true;
}
if($item->multiple) {
if(!is_array($new_value)) {
$new_value = [$new_value];
}
$json_array = [];
foreach($new_value as $v) {
if($v) {
$json_array[] = $v;
}
}
$this->json = json_encode($json_array);
} elseif($item->type == "int") {
if(!is_numeric($new_value)) return false;
$this->int = $new_value;
} elseif($item->type == "decimal") {
if(!is_float($new_value) && !is_numeric($new_value)) return false;
$this->number = $new_value;
} elseif(is_scalar($new_value)) {
$this->string = $new_value;
} else {
return false;
}
return true;
}
public function runAfterSaveHooks() {
$contract = $this->getProperty("item")->contract;
if(!$contract || !$contract->id) {
return false;
}
$folderpath = APPDIR."/Contractconfig/hooks/";
$dir = opendir($folderpath);
while($filename = readdir($dir)) {
if(substr($filename, 0, 1) == ".") {
continue;
}
if($filename == "Contractconfig_Hook.php") continue;
if(substr($filename, -4) != ".php") continue;
$hook_type = basename($filename, ".php");
$classname = "Contractconfig_Hook_$hook_type";
$hook_class_filename = $folderpath.$filename;
require_once $hook_class_filename;
if(!class_exists($classname)) {
continue;
}
$hook = new $classname($contract);
if($hook->isResponsible()) {
$this->log->debug("Running {$classname}->afterSave() for Contract id ".$contract->id);
$hook->afterSave();
if($hook->errors) {
$this->hook_errors = $hook->errors;
return false;
}
}
}
return true;
}
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "item") {
$this->item = new ContractconfigItem($this->item_id);
if(!$this->item->id) {
return null;
}
return $this->item;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}