Files
thetool/application/Preordercampaign/Preordercampaign.php
2022-09-12 13:51:01 +02:00

150 lines
4.4 KiB
PHP

<?php
class Preordercampaign extends mfBaseModel {
private $network;
private $preorders;
private $types;
private $setup_products;
private $gemeinden;
private $apiusers;
public function addTypes(Array $types) {
$allowd_types = ["interest","provision","order"];
$new_types = [];
foreach($types as $type) {
if(!in_array($type, $allowd_types)) {
$this->log->debug("$type not in allowed_types");
continue;
}
$new_types[] = $type;
}
//var_dump($new_types);
foreach($allowd_types as $atype) {
$existing = PreordercampaignTypeModel::getFirst(['preordercampaign_id' => $this->id, "type" => $atype]);
//var_dump($existing);exit;
if($existing) {
if(!in_array($atype,$new_types)) {
$existing->delete();
}
} else {
if(in_array($atype, $new_types)) {
$data = [];
$data['preordercampaign_id'] = $this->id;
$data['type'] = $atype;
$nt = PreordercampaignTypeModel::create($data);
$nt->save();
}
}
}
return true;
}
public function getSetupProducts() {
$provision_products = [];
$activation_products = [];
$provisions = ProductModel::search(["attributename" => "presales", "attributevalue" => "provision"]);
$activations = ProductModel::search(["attributename" => "presales", "attributevalue" => "activation"]);
foreach($provisions as $p) {
if(ProductNetworkModel::search(['product_id' => $p->id, "network_id" => $this->network_id])) {
$provision_products[] = $p;
}
}
foreach($activations as $p) {
if(ProductNetworkModel::search(['product_id' => $p->id, "network_id" => $this->network_id])) {
$activation_products[] = $p;
}
}
return ['activation' => $activation_products, 'provision' => $provision_products];
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "preorders") {
$this->preorders = PreorderModel::search(['preordercampaign_id' => $this->id]);
return $this->preorders;
}
if($name == "types") {
$types = PreordercampaignTypeModel::search(['preordercampaign_id' => $this->id]);
foreach($types as $type) {
$this->types[$type->type] = $type;
}
return $this->types;
}
if($name == "setup_products") {
$this->setup_products = $this->getSetupProducts();
//var_dump($this->setup_products);exit;
return $this->setup_products;
}
if($name == "gemeinden") {
$items = PreordercampaignGemeindeModel::search(["preordercampaign_id" => $this->id]);
foreach($items as $pog) {
$this->gemeinden[$pog->gemeinde_id] = $pog;
}
return $this->gemeinden;
}
if($name == "apiusers") {
$items = PreordercampaignApiuserModel::search(["preordercampaign_id" => $this->id]);
foreach($items as $poa) {
$this->apiusers[$poa->worker_id] = $poa;
}
return $this->apiusers;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
if($user) {
$this->editor = $user;
return $this->editor;
}
$this->editor = new User($this->edit_by);
if($this->editor->id) {
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
}
return $this->editor;
}
$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;
}
}