Finished IvtProductMatch

This commit is contained in:
Frank Schubert
2023-10-03 20:02:13 +02:00
parent dd4bf56aa3
commit 7c81125afd
8 changed files with 208 additions and 71 deletions

View File

@@ -24,45 +24,59 @@
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<form method="post" action="<?=self::getUrl("Admin", "saveIvtImportMatchProducts")?>">
<input type="hidden" name="s" value="<?=$pagination['start']?>" />
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination.php"); ?> <?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination.php"); ?>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?> <?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?>
<table class="table table-sm table-striped table-hover">
<tr> <table class="table table-sm table-striped table-hover">
<th class="text-right">IVT Produkt</th>
<th>Preis</th>
<th>Interval</th>
<th>Typ</th>
<th></th>
<th style="border-left: 1px solid #000">thetool Produkt</th>
<th>ivt_id hinterlegt</th>
</tr>
<?php foreach($ivtproducts as $ip): ?>
<?php $match = false; ?>
<tr> <tr>
<td class="text-right"><?=$ip->name?></td> <th>IVT ID</th>
<td><?=$ip->price?></td> <th class="text-right">IVT Produkt</th>
<td><?=$ip->interval?></td> <th>Preis</th>
<td><?=$ip->typ?></td> <th>Interval</th>
<td><?=$ip->id?></td> <th>Typ</th>
<td style="border-left: 1px solid #000"> <th></th>
<select class="form-control" name="product[<?=$ip->id?>]" id="product_<?=$ip->id?>"> <th style="border-left: 1px solid #000">thetool Produkt</th>
<option></option> <th>ivt_id hinterlegt</th>
<?php foreach($products as $p): ?>
<?php if($p->ivt_id == $ip->id) $match = "prod"; ?>
<option value="<?=$p->id?>" <?=($p->ivt_id == $ip->id) ? "selected='selected'" : ""?>><?=$p->name?></option>
<?php endforeach; ?>
</select>
</td>
<td><?=($match == "prod") ? "<i class='fas fa-check text-success'></i>" : ""?></td>
</tr> </tr>
<?php endforeach; ?> <?php foreach($ivtproducts as $ip): ?>
</table> <?php $match = false; ?>
<tr>
<td><?=$ip->id?></td>
<td class="text-right"><?=$ip->name?></td>
<td><?=$ip->price?></td>
<td><?=$ip->interval?></td>
<td><?=$ip->typ?></td>
<td><?=$ip->id?></td>
<td style="border-left: 1px solid #000">
<select class="form-control" name="product[<?=$ip->id?>]" id="product_<?=$ip->id?>">
<option></option>
<?php foreach($products as $p): ?>
<?php if($ip->match): ?>
<option value="<?=$p->id?>" <?=($p->id == $ip->match->product_id) ? "selected='selected'" : ""?>><?=$p->name?></option>
<?php else: ?>
<?php if($p->ivt_id == $ip->id) $match = "prod"; ?>
<option value="<?=$p->id?>" <?=($p->ivt_id == $ip->id) ? "selected='selected'" : ""?>><?=$p->name?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</td>
<td><?=($match == "prod") ? "<i class='fas fa-check text-success'></i>" : ""?></td>
</tr>
<?php endforeach; ?>
</table>
<div class="text-right">
<button type="submit" class="btn btn-success">Speichern</button>
</div>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?> <?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination.php"); ?> <?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination.php"); ?>
</form>
</div> </div>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">

View File

@@ -191,6 +191,8 @@
</div> </div>
</div> </div>
<hr />
<div class="form-group"> <div class="form-group">
<input type="submit" name="submit" value="Speichern" class="btn btn-primary" /> <input type="submit" name="submit" value="Speichern" class="btn btn-primary" />
</div> </div>

View File

@@ -62,7 +62,7 @@ class AdminController extends mfBaseController {
// pagination defaults // pagination defaults
$pagination = []; $pagination = [];
$pagination['start'] = 0; $pagination['start'] = 0;
$pagination['count'] = 20; $pagination['count'] = 80;
$pagination['maxItems'] = 0; $pagination['maxItems'] = 0;
if(is_numeric($this->request->s)) { if(is_numeric($this->request->s)) {
@@ -72,11 +72,54 @@ class AdminController extends mfBaseController {
$pagination['maxItems'] = IvtProductModel::count($filter); $pagination['maxItems'] = IvtProductModel::count($filter);
$ivtproducts = IvtProductModel::search($filter, $pagination); $ivtproducts = IvtProductModel::search($filter, $pagination);
$this->layout()->set("pagination", $pagination); $this->layout()->set("pagination", $pagination);
$this->layout()->set("ivtproducts", $ivtproducts); $this->layout()->set("ivtproducts", $ivtproducts);
$this->layout()->set("products", ProductModel::getActive()); $this->layout()->set("products", ProductModel::getActive());
} }
protected function saveIvtImportMatchProducts() {
$r = $this->request;
//var_dump($r);exit;
if(!is_array($r->product) || !count($r->product)) {
$this->layout()->setFlash("Nichts zu Speichern.", "info");
$this->redirect("Admin", "ivtImportMatchProducts");
}
foreach($r->product as $ivt_product_id => $product_id) {
if(!$ivt_product_id || !$product_id) continue;
// find IvtProductMatch by ivt product id ...
$match = IvtProductMatchModel::getFirst(["ivt_product_id" => $ivt_product_id]);
if(!$match) {
// ... or create it
$ivt_product = new IvtProduct($ivt_product_id);
if(!$ivt_product->id) {
$this->layout()->setFlash("Ivt Product $ivt_product_id not found.", "error");
$this->redirect("Admin", "ivtImportMatchProducts");
}
$match = IvtProductMatchModel::create([
"ivt_product_id" => $ivt_product_id,
"ivt_product_name" => $ivt_product->name
]);
}
// save product_id to IvtProductMatch
$match->product_id = $product_id;
if(!$match->save()) {
$this->layout()->setFlash("Fehler beim Speichern.", "error");
}
}
$this->layout()->setFlash("Erfolgreich gespeichert.", "success");
if(is_numeric($r->s) && $r->s) {
$this->redirect("Admin", "ivtImportMatchProducts", ["s" => $r->s]);
}
$this->redirect("Admin", "ivtImportMatchProducts");
}
protected function ivtDownloadActiveProducts() { protected function ivtDownloadActiveProducts() {
$ivtproducts = IvtProductModel::getAll(); $ivtproducts = IvtProductModel::getAll();

View File

@@ -2,6 +2,7 @@
class IvtProduct extends mfBaseModel { class IvtProduct extends mfBaseModel {
protected $forcestr = []; protected $forcestr = [];
private $match;
/** /**
* Takes ID or DB row as arguments * Takes ID or DB row as arguments
@@ -9,8 +10,10 @@ class IvtProduct extends mfBaseModel {
*/ */
public function __construct($_=NULL) { public function __construct($_=NULL) {
$this->log = mfLoghandler::singleton(); $this->log = mfLoghandler::singleton();
$this->data = new stdClass();
$this->table = "products"; $this->table = "products";
$this->data = new stdClass();
$this->_old_data = new stdClass();
$this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); $this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
@@ -26,6 +29,36 @@ class IvtProduct extends mfBaseModel {
} }
public function getProperty($name) {
if($this->$name == null) {
if($name == "match") {
$match = IvtProductMatchModel::getFirst(["ivt_product_id" => $this->id]);
if($match) {
$this->match = $match;
}
return $this->match;
}
$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;
}
public function __debugInfo() { public function __debugInfo() {
$vars = get_object_vars($this); $vars = get_object_vars($this);

View File

@@ -1,6 +1,6 @@
<?php <?php
class IvtProduct extends mfBaseModel { class IvtProductMatch extends mfBaseModel {
private $product; private $product;
private $ivtproduct; private $ivtproduct;
@@ -8,7 +8,7 @@ class IvtProduct extends mfBaseModel {
if($this->$name == null) { if($this->$name == null) {
if($name == "ivtproduct") { if($name == "ivtproduct") {
$ivtproduct = new IvtProduct($this->ivt_id); $ivtproduct = new IvtProduct($this->ivt_product_id);
if($ivtproduct->id) { if($ivtproduct->id) {
$this->ivtproduct = $ivtproduct; $this->ivtproduct = $ivtproduct;
} }

View File

@@ -1,9 +1,17 @@
<?php <?php
class IvtProuctMatchModel { class IvtProductMatchModel {
public $ivt_product_id;
public $ivt_product_name;
public $product_id;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) { public static function create(Array $data) {
$model = new Contract(); $model = new IvtProductMatch();
foreach($data as $field => $value) { foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) { if(property_exists(get_called_class(), $field)) {
@@ -27,25 +35,25 @@ class IvtProuctMatchModel {
public static function getAll() { public static function getAll() {
$items = []; $items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); $db = FronkDB::singleton();
$res = $db->select("IvtProuctMatch", "*", "1=1 ORDER BY ivt_name,id"); $res = $db->select("IvtProductMatch", "*", "1=1 ORDER BY ivt_product_id,id");
if($db->num_rows($res)) { if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) { while($data = $db->fetch_object($res)) {
$items[] = new IvtProuctMatch($data); $items[] = new IvtProductMatch($data);
} }
} }
return $items; return $items;
} }
public static function getFirst() { public static function getFirst($filter) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); $db = FronkDB::singleton();
$where = self::getSqlFilter($filter); $where = self::getSqlFilter($filter);
$res = $db->select("IvtProuctMatch", "*", "$where ORDER BY ivt_name,id LIMIT 1"); $res = $db->select("IvtProductMatch", "*", "$where ORDER BY ivt_product_id,id LIMIT 1");
if($db->num_rows($res)) { if($db->num_rows($res)) {
$data = $db->fetch_object($res); $data = $db->fetch_object($res);
$item = new IvtProuctMatch($data); $item = new IvtProductMatch($data);
if($item->id) { if($item->id) {
return $item; return $item;
} else { } else {
@@ -57,10 +65,10 @@ class IvtProuctMatchModel {
public static function search($filter, $limit = false) { public static function search($filter, $limit = false) {
$items = []; $items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME); $db = FronkDB::singleton();
$where = self::getSqlFilter($filter); $where = self::getSqlFilter($filter);
$sql = "SELECT * FROM IvtProuctMatch $where ORDER by ivt_name,id"; $sql = "SELECT * FROM IvtProductMatch $where ORDER by ivt_product_id,id";
mfLoghandler::singleton()->debug($sql); mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) { if(is_array($limit) && count($limit)) {
@@ -74,7 +82,7 @@ class IvtProuctMatchModel {
$res = $db->query($sql); $res = $db->query($sql);
if($db->num_rows($res)) { if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) { while($data = $db->fetch_object($res)) {
$items[] = new IvtProuctMatch($data); $items[] = new IvtProductMatch($data);
} }
} }
return $items; return $items;
@@ -83,24 +91,17 @@ class IvtProuctMatchModel {
private static function getSqlFilter($filter) { private static function getSqlFilter($filter) {
$where = "1=1 "; $where = "1=1 ";
if(array_key_exists("ivt_id", $filter)) { if(array_key_exists("ivt_product_id", $filter)) {
$ivt_id = $filter['ivt_id']; $ivt_product_id = $filter['ivt_product_id'];
if(is_numeric($ivt_id)) { if(is_numeric($ivt_product_id)) {
$where .= " AND IvtProuctMatch.ivt_id=$ivt_id"; $where .= " AND IvtProductMatch.ivt_product_id=$ivt_product_id";
}
}
if(array_key_exists("ivt_name", $filter)) {
$ivt_name = FronkDB::singleton()->escape($filter["ivt_name"]);
if($ivt_name) {
$where .= " AND IvtProuctMatch.ivt_name like '%$ivt_name%'";
} }
} }
if(array_key_exists("product_id", $filter)) { if(array_key_exists("product_id", $filter)) {
$product_id = $filter['product_id']; $product_id = $filter['product_id'];
if(is_numeric($product_id)) { if(is_numeric($product_id)) {
$where .= " AND IvtProuctMatch.product_id=$product_id"; $where .= " AND IvtProductMatch.product_id=$product_id";
} }
} }

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddTableIvtProductMatch extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("IvtProductMatch");
$table->addColumn("ivt_product_id", "integer", ["null" => false]);
$table->addColumn("ivt_product_name", "string", ["null" => false, "length" => 255]);
$table->addColumn("product_id", "integer", ["null" => false]);
$table->addColumn("create_by", "integer", ["null" => false]);
$table->addColumn("edit_by", "integer", ["null" => false]);
$table->addColumn("create", "integer", ["null" => false]);
$table->addColumn("edit", "integer", ["null" => false]);
$table->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
}
if($this->getEnvironment() == "addressdb") {
}
}
}

View File

@@ -79,16 +79,21 @@ class FronkDB {
//echo "$sql\n"; //echo "$sql\n";
} }
if($this->result=mysqli_query($this->link,$sql)) { try {
return $this->result; if($this->result=mysqli_query($this->link,$sql)) {
} else { return $this->result;
$this->lastError=mysqli_error($this->link); } else {
if($print_error) { $this->lastError=mysqli_error($this->link);
echo "Error in SQL-query:<br />\n".$sql."<br />\n".$this->lastError."<br />\n"; if($print_error) {
} echo "Error in SQL-query:<br />\n".$sql."<br />\n".$this->lastError."<br />\n";
$this->log->warn("SQL Query failed: $sql"); }
return false; $this->log->warn("SQL Query failed: $sql");
} return false;
}
} catch (\Exception $e) {
$this->log->warn("SQL Query Exception ".$e->getCode().": ". $e->getMessage()."\nQuery was:\n$sql");
throw $e;
}
} }
public function getLastError() { public function getLastError() {