Files
thetool/application/Workflowvalue/Workflowvalue.php
2022-02-15 22:26:10 +01:00

170 lines
3.8 KiB
PHP

<?php
class Workflowvalue extends mfBaseModel {
private $item;
private $changer;
private $me;
public $gps = [];
public $is_changed = false;
protected function init() {
$this->me = mfValuecache::get("me");
if(!$this->me) {
$this->me = new User();
$this->me->loadMe();
mfValuecache::set("me", $this->me);
}
}
protected function afterLoad() {
if(!$this->id) {
return true;
}
if($this->getProperty("item")->type == "gps" && $this->value_string) {
$gps = explode(";", $this->value_string);
$this->gps = $gps;
}
}
protected function beforeSave() {
if($this->new() == "new") {
$this->changed = date('U');
$this->changed_by = $this->me->id;
}
}
public function setValue($value) {
$this->item = $this->getProperty("item");
if($this->item->type == "delimiter" || $this->item->type == "empty") {
return true;
}
$value_type = "string";
switch($this->item->type) {
case "string":
$value_type = "string";
break;
case "enum":
$value_type = "string";
break;
case "gps":
$value_type = "string";
break;
case "color":
$value_type = "string";
break;
case "date":
$value_type = "string";
break;
case "int":
$value_type = "int";
break;
case "bool":
$value_type = "int";
break;
case "file":
$value_type = "int";
break;
case "text":
$value_type = "text";
break;
}
if($this->item->type == "gps") {
if(is_array($value) && count($value) == 2) {
$lat = str_replace(",", ".", $value['lat']);
$long = str_replace(",", ".", $value['long']);
$value = "$lat;$long";
if($lat == null && $long == null) {
$value = null;
}
} else {
$value = null;
}
}
/*if($this->item->name == "customer_passive_finished") {
var_dump($value, $this->{"value_".$value_type});exit;
}*/
if($value === null) {
return true;
}
if($value_type == "int" && $this->{"value_".$value_type} == null && !$value) {
return true;
}
if($this->item->type == "bool" && $this->{"value_".$value_type} == null && $value == 0) {
return true;
}
if($value == null && $this->{"value_".$value_type} == null) {
return true;
}
$this->is_changed = true;
/*$me = new User();
$me->loadMe();*/
if($this->{"value_".$value_type} != $value) {
$this->{"value_".$value_type} = $value;
$this->changed = date('U');
$this->changed_by = $this->me->id;
}
/*if($this->new() == "new") {
$this->changed = date('U');
$this->changed_by = $this->me->id;
}*/
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "item") {
$this->item = new Workflowitem($this->item_id);
return $this->item;
}
if($name == "changer") {
$user = mfValuecache::get("Worker-id-".$this->changed);
if($user) {
$this->changer = $user;
return $this->changer;
}
if($this->changed && $this->changed_by) {
$this->changer = new User($this->changed_by);
if($this->changer->id) {
mfValuecache::set("Worker-id-".$this->changed, $this->changer);
}
return $this->changer;
} else {
return new Address();
}
}
$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;
}
}