Finished IvtProductMatch
This commit is contained in:
@@ -24,11 +24,15 @@
|
||||
|
||||
<div class="row">
|
||||
<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-summary.php"); ?>
|
||||
|
||||
<table class="table table-sm table-striped table-hover">
|
||||
<tr>
|
||||
<th>IVT ID</th>
|
||||
<th class="text-right">IVT Produkt</th>
|
||||
<th>Preis</th>
|
||||
<th>Interval</th>
|
||||
@@ -40,6 +44,7 @@
|
||||
<?php foreach($ivtproducts as $ip): ?>
|
||||
<?php $match = false; ?>
|
||||
<tr>
|
||||
<td><?=$ip->id?></td>
|
||||
<td class="text-right"><?=$ip->name?></td>
|
||||
<td><?=$ip->price?></td>
|
||||
<td><?=$ip->interval?></td>
|
||||
@@ -49,8 +54,12 @@
|
||||
<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>
|
||||
@@ -59,10 +68,15 @@
|
||||
<?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.php"); ?>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
@@ -191,6 +191,8 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
|
||||
<div class="form-group">
|
||||
<input type="submit" name="submit" value="Speichern" class="btn btn-primary" />
|
||||
</div>
|
||||
|
||||
@@ -62,7 +62,7 @@ class AdminController extends mfBaseController {
|
||||
// pagination defaults
|
||||
$pagination = [];
|
||||
$pagination['start'] = 0;
|
||||
$pagination['count'] = 20;
|
||||
$pagination['count'] = 80;
|
||||
$pagination['maxItems'] = 0;
|
||||
|
||||
if(is_numeric($this->request->s)) {
|
||||
@@ -72,11 +72,54 @@ class AdminController extends mfBaseController {
|
||||
$pagination['maxItems'] = IvtProductModel::count($filter);
|
||||
$ivtproducts = IvtProductModel::search($filter, $pagination);
|
||||
|
||||
|
||||
$this->layout()->set("pagination", $pagination);
|
||||
$this->layout()->set("ivtproducts", $ivtproducts);
|
||||
$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() {
|
||||
$ivtproducts = IvtProductModel::getAll();
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
class IvtProduct extends mfBaseModel {
|
||||
protected $forcestr = [];
|
||||
private $match;
|
||||
|
||||
/**
|
||||
* Takes ID or DB row as arguments
|
||||
@@ -9,8 +10,10 @@ class IvtProduct extends mfBaseModel {
|
||||
*/
|
||||
public function __construct($_=NULL) {
|
||||
$this->log = mfLoghandler::singleton();
|
||||
$this->data = new stdClass();
|
||||
$this->table = "products";
|
||||
$this->data = new stdClass();
|
||||
$this->_old_data = new stdClass();
|
||||
|
||||
|
||||
$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() {
|
||||
$vars = get_object_vars($this);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class IvtProduct extends mfBaseModel {
|
||||
class IvtProductMatch extends mfBaseModel {
|
||||
private $product;
|
||||
private $ivtproduct;
|
||||
|
||||
@@ -8,7 +8,7 @@ class IvtProduct extends mfBaseModel {
|
||||
if($this->$name == null) {
|
||||
|
||||
if($name == "ivtproduct") {
|
||||
$ivtproduct = new IvtProduct($this->ivt_id);
|
||||
$ivtproduct = new IvtProduct($this->ivt_product_id);
|
||||
if($ivtproduct->id) {
|
||||
$this->ivtproduct = $ivtproduct;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
<?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) {
|
||||
$model = new Contract();
|
||||
$model = new IvtProductMatch();
|
||||
|
||||
foreach($data as $field => $value) {
|
||||
if(property_exists(get_called_class(), $field)) {
|
||||
@@ -27,25 +35,25 @@ class IvtProuctMatchModel {
|
||||
public static function getAll() {
|
||||
$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)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new IvtProuctMatch($data);
|
||||
$items[] = new IvtProductMatch($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function getFirst() {
|
||||
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
||||
public static function getFirst($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$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)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new IvtProuctMatch($data);
|
||||
$item = new IvtProductMatch($data);
|
||||
if($item->id) {
|
||||
return $item;
|
||||
} else {
|
||||
@@ -57,10 +65,10 @@ class IvtProuctMatchModel {
|
||||
|
||||
public static function search($filter, $limit = false) {
|
||||
$items = [];
|
||||
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$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);
|
||||
if(is_array($limit) && count($limit)) {
|
||||
@@ -74,7 +82,7 @@ class IvtProuctMatchModel {
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new IvtProuctMatch($data);
|
||||
$items[] = new IvtProductMatch($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
@@ -83,24 +91,17 @@ class IvtProuctMatchModel {
|
||||
private static function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
if(array_key_exists("ivt_id", $filter)) {
|
||||
$ivt_id = $filter['ivt_id'];
|
||||
if(is_numeric($ivt_id)) {
|
||||
$where .= " AND IvtProuctMatch.ivt_id=$ivt_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("ivt_product_id", $filter)) {
|
||||
$ivt_product_id = $filter['ivt_product_id'];
|
||||
if(is_numeric($ivt_product_id)) {
|
||||
$where .= " AND IvtProductMatch.ivt_product_id=$ivt_product_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("product_id", $filter)) {
|
||||
$product_id = $filter['product_id'];
|
||||
if(is_numeric($product_id)) {
|
||||
$where .= " AND IvtProuctMatch.product_id=$product_id";
|
||||
$where .= " AND IvtProductMatch.product_id=$product_id";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
39
db/migrations/20231003174028_add_table_ivt_product_match.php
Normal file
39
db/migrations/20231003174028_add_table_ivt_product_match.php
Normal 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") {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,7 @@ class FronkDB {
|
||||
//echo "$sql\n";
|
||||
}
|
||||
|
||||
try {
|
||||
if($this->result=mysqli_query($this->link,$sql)) {
|
||||
return $this->result;
|
||||
} else {
|
||||
@@ -88,6 +89,10 @@ class FronkDB {
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user