Files
thetool/application/Contractconfig/hooks/Contractconfig_Hook.php
2023-03-22 18:52:05 +01:00

99 lines
2.4 KiB
PHP

<?php
abstract class Contractconfig_Hook {
protected $log;
protected $required_product_attributes = [];
protected $contract;
protected $items;
protected $product;
protected $product_attributes = [];
protected $config_prefix;
protected $configitems = [];
public $errors = [];
abstract public function beforeSave();
abstract public function afterSave();
abstract public function beforeCancel();
abstract public function afterCancel();
public function __construct(Contract $contract) {
$this->log = mfLoghandler::singleton();
$this->contract = $contract;
if($contract->product) {
$this->product = $contract->product;
if(is_array($this->product->attributes) && count($this->product->attributes)) {
$this->product_attributes = $this->product->attributes;
}
}
$this->loadConfigItems();
if(method_exists($this, "init")) {
$this->init();
}
}
private function loadConfigItems() {
$m = [];
if(preg_match('/^Contractconfig_Hook_(.+)$/', get_class($this), $m)) {
if($m[1]) {
$this->config_prefix = strtolower($m[1]);
}
} else {
return false;
}
foreach($this->contract->configgroups as $configgroup) {
foreach($configgroup->items as $item) {
if(strpos($item->name, $this->config_prefix) === 0) {
$short_name = substr($item->name, strlen($this->config_prefix)+1);
//var_dump($short_name);
$this->configitems[$short_name] = $item;
}
}
}
return true;
}
public function isResponsible() {
if(!$this->contract) return false;
if(!$this->product) return false;
if(!$this->product_attributes) return false;
// only work on contracts with our producttech attributes
if(!$this->testMyProductAttributes()) {
return false;
}
return true;
}
private function testMyProductAttributes() {
if(!$this->product_attributes) return false;
//var_dump($this->product_attributes);exit;
//var_dump($this->required_product_attributes);exit;
foreach($this->required_product_attributes as $needed_attribute) {
// every needed attribute must exist in product
if(!array_key_exists($needed_attribute, $this->product_attributes)) {
return false;
}
}
return true;
}
}