needlogin=true; $me = new User(); $me->loadMe(); $this->me = $me; $this->layout()->set("me",$me); if(!$me->is(["Admin","netowner","pipeplanner"])) { $this->redirect("Dashboard"); } } protected function indexAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } if($this->request->resetFilter) { unset($_SESSION[MFAPPNAME.'-Product-filter']); } $filter = []; if(is_array($this->request->filter)) { $filter = $this->request->filter; $_SESSION[MFAPPNAME.'-Product-filter'] = $filter; } else { if(array_key_exists(MFAPPNAME.'-Product-filter', $_SESSION) && count($_SESSION[MFAPPNAME.'-Product-filter'])) { $filter = $_SESSION[MFAPPNAME.'-Product-filter']; } } $this->layout->set("filter", $filter); $filter = $this->getPreparedFilter($filter); // pagination defaults $pagination = []; $pagination['start'] = 0; $pagination['count'] = 20; $pagination['maxItems'] = 0; if(is_numeric($this->request->s)) { $pagination['start'] = intval($this->request->s); } $pagination['maxItems'] = ProductModel::count($filter); $this->layout()->set("pagination", $pagination); $products = ProductModel::search($filter, $pagination); $this->layout()->set("products", $products); } private function getPreparedFilter($filter) { $new_filter = []; if(array_key_exists("name", $filter)) { $new_filter['name%'] = $filter['name']; unset($filter['name']); } if(array_key_exists("active", $filter)) { if($filter['active'] != "all") { $new_filter['active'] = $filter['active']; } unset($filter['active']); } else { $new_filter['active'] = 1; } foreach($filter as $name => $value) { $new_filter[$name] = $value; } return $new_filter; } protected function addAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $this->layout()->setTemplate("Product/Form"); $this->layout()->set("productgroups", ProductgroupModel::getAll()); $this->layout()->set("producttechs", ProducttechModel::getAll()); $this->layout()->set("slas", SlaModel::getAll()); $this->layout()->set("networks", NetworkModel::getAll()); } protected function editAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $product_id = $this->request->id; $product = new Product($product_id); if(!$product->id) { $this->layout()->setFlash("Produkt nicht gefunden.", "error"); $this->redirect("Product"); } //var_dump($product->attributes);exit; $this->layout()->set("product", $product); return $this->addAction(); } protected function copyAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $id = $this->request->id; if(!is_numeric($id) || $id < 1) { $this->layout()->setFlash("Produkt nicht gefunden", "error"); $this->redirect("Product"); } $product = new Product($id); if(!$product->id) { $this->layout()->setFlash("Produkt nicht gefunden", "error"); $this->redirect("Product"); } $copy = clone($product); if(!$copy->save()) { $this->layout()->setFlash("Kopie konnte nicht gespeichert werden", "error"); $this->redirect("Product"); } foreach($product->attributes as $attrib) { $new_attrib = clone($attrib); $new_attrib->product_id = $copy->id; $new_attrib->save(); } foreach(ProductNetworkModel::search(['product_id' => $product->id]) as $pn) { $new_pn = clone($pn); $new_pn->product_id = $copy->id; $new_pn->save(); } $this->layout()->setFlash("Kopie erfolgreich erstellt.", "success"); $this->redirect("Product", "edit", ["id" => $copy->id]); } protected function saveAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $r = $this->request; //var_dump($r);exit; $id = $r->id; if(is_numeric($id) && $id > 0) { $mode = "edit"; $product = new Product($id); if(!$product->id) { $this->layout()->setFlash("Produkt nicht gefunden", "error"); $this->redirect("Product"); } } else { $id = false; $mode = "add"; } $data = []; $data['name'] = $r->name; $data['description'] = $r->description; if($r->sla_id) { $data['sla_id'] = $r->sla_id; } $data['external'] = ($r->external == 1) ? 1 : 0; if($data['external'] == 1) { if(!is_numeric($r->external_id) || $r->external_id < 1) { $this->layout()->setFlash("Bitte Produktbesitzer auswählen", "warn"); $this->layout()->set("product", $product); return $this->add(); } $data['external_id'] = $r->external_id; } $data['active'] = 1; if(!$r->active) { $data['active'] = 0; } $data['price_nne'] = ($r->price_nne) ? Layout::commaToDot($r->price_nne) : 0; $data['price_nbe'] = ($r->price_nbe) ? Layout::commaToDot($r->price_nbe) : 0; $data['price'] = ($r->price) ? Layout::commaToDot($r->price) : 0; $data['price_setup'] = ($r->price_setup) ? Layout::commaToDot($r->price_setup) : 0; $data['vatgroup_id'] = $r->vatgroup_id; $data['billing_period'] = $r->billing_period; $data['billing_delay'] = ($r->billing_delay) ? $r->billing_delay : 0; $data['contract_term'] = $r->contract_term; $data['ivt_id'] = ($r->ivt_id) ? $r->ivt_id : null; $data['note'] = $r->note; if(is_numeric($r->producttech_id)) { $data['producttech_id'] = $r->producttech_id; } else { $data['producttech_id'] = null; } if(is_numeric($r->productgroup_id)) { $data['productgroup_id'] = $r->productgroup_id; } else { $data['productgroup_id'] = null; } $data['edit_by'] = $this->me->id; if($mode == "add") { $data['create_by'] = $this->me->id; $product = ProductModel::create($data); } else { $product->update($data); } //var_dump($address);exit; $new_id = $product->save(); if(!$new_id) { $this->layout()->setFlash("Fehler beim Speichern", "error"); $this->layout()->set("product", $product); return $this->add(); } // delete all networks to save networks $pnets = ProductNetworkModel::search(['product_id' => $new_id]); foreach($pnets as $pnet) { $pnet->delete(); } if(is_array($r->networks) && count($r->networks)) { foreach($r->networks as $network_id) { $network = new Network($network_id); if(!$network->id) { // ignore non-existing networks continue; } $pnet = ProductNetworkModel::create(['product_id' => $new_id, 'network_id' => $network_id]); $pnet->save(); } } // create new product group and tech if($r->productgroup_id == "new") { $ng = []; $ng['name'] = $r->productgroup_new_name; $ng['description'] = $r->productgroup_new_description; $ng['note'] = $r->productgroup_new_note; $group = ProductgroupModel::create($ng); $group_id = $group->save(); $product->productgroup_id = $group_id; $product->save(); } if($r->producttech_id == "new") { $nt = []; $nt['name'] = $r->producttech_new_name; //$nt['rtrcode'] = $r->producttech_new_rtrcode; $nt['customer_type'] = ($r->producttech_new_customer_type == "business") ? "business" : "residential"; $nt['description'] = $r->producttech_new_description; $nt['note'] = $r->producttech_new_note; $tech = ProducttechModel::create($nt); $tech_id = $tech->save(); $product->producttech_id = $tech_id; $product->save(); } if(is_array($r->attributes) && count($r->attributes)) { foreach($r->attributes as $pta_id => $attribute_value) { $attrib = ProductAttributeModel::getFirst(['product_id' => $new_id, 'producttechattribute_id' => $pta_id]); if(!$attrib) { $attrib = ProductAttributeModel::create([ 'product_id' => $new_id, 'producttechattribute_id' => $pta_id, ]); //var_dump($attrib);exit; } $attrib->value = $attribute_value; $attrib->edit_by = $this->me->id; $attrib->save(); } } $this->layout()->setFlash("Produkt erfolgreich gespeichert.", "success"); if($r->return == "index") { $this->redirect("Product"); } else { $this->redirect("Product", "Edit", ['id' => $new_id]); } } protected function deleteAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $id = $this->request->id; $product = new Product($id); if(!$product->id || $product->id != $id) { $this->layout()->setFlash("Produkt nicht gefunden.", "error"); $this->redirect("Product"); } // check if product is in use if(OrderProductModel::count(["product_id" => $product->id])) { $this->layout()->setFlash("Produkt kann nicht gelöscht werden, da es in Verwendung ist!", "error"); $this->redirect("Product"); } if(ContractModel::count(["product_id" => $product->id])) { $this->layout()->setFlash("Produkt kann nicht gelöscht werden, da es in Verwendung ist!", "error"); $this->redirect("Product"); } if(ContractqueueModel::count(["product_id" => $product->id])) { $this->layout()->setFlash("Produkt kann nicht gelöscht werden, da es in Verwendung ist!", "error"); $this->redirect("Product"); } if(PreorderModel::count(["product_id" => $product->id]) || PreorderModel::count(["setup_product_id" => $product->id])) { $this->layout()->setFlash("Produkt kann nicht gelöscht werden, da es in Verwendung ist!", "error"); $this->redirect("Product"); } // delete attributes foreach($product->attributes as $attrib) { $attrib->delete(); } // delete networks foreach(ProductNetworkModel::search(['product_id' => $product->id]) as $pn) { $pn->delete(); } // delete product $product->delete(); $this->layout()->setFlash("Produkt erfolgreich gelöscht", "success"); $this->redirect("Product"); } protected function apiAction() { if(!$this->me->is(["Admin","netowner","pipeplanner"])) { $this->redirect("Dashboard"); } $do = $this->request->do; $data = []; switch($do) { case "getProduct": $return = $this->getProductApi(); break; case "findProduct": $return = $this->findProductApi(); default: $return = false; } if(!is_array($return) || !count($return)) { $data = ["status" => "error"]; $this->returnJson($data); } $data['status'] = "OK"; $data['result'] = $return; $this->returnJson($data); } private function getProductApi() { $product_id = $this->request->product_id; if(!is_numeric($product_id) || $product_id < 1) { return false; } $form_id = false; if($this->request->form_id) { $form_id = $this->request->form_id; } $product = new Product($product_id); if(!$product->id) { return false; } if(is_array($product->attributes) && count($product->attributes)) { $attributes = $product->attributes; $product->data->attributes = []; foreach($attributes as $attrib) { $product->data->attributes[$attrib->name] = $attrib->value; } } return ["product" => $product->data, "form_id" => $form_id]; } private function findProductApi() { $search = trim($this->request->q); $products = []; if(is_numeric($search)) { $pnumbers = ProductModel::search(["active" => 1, "id" => $search]); if($pnumbers) { $products = array_merge($products, $pnumbers); } } $wild_search = implode("%", explode(" ", $search)); $products = array_merge($products, ProductModel::search(["active" => 1, "name%" => $wild_search])); if(!is_array($products) && !count($products)) { return false; } $results = []; if(!$this->me->isAdmin()) { $my_product_ids = []; $my_network_ids = []; foreach($this->me->my_networks as $network) { $my_network_ids[] = $network->id; } foreach(ProductNetworkModel::search(["network_id" => $my_network_ids]) as $pn) { if(!$pn->product->active) continue; if(!array_key_exists($pn->product_id, $my_product_ids)) { $my_product_ids[] = $pn->product_id; } } } // return bootstrap-autocomplete format foreach($products as $product) { if(!$this->me->isAdmin()) { if(!in_array($product->id, $my_product_ids)) continue; } $result = ['value' => $product->id, 'text' => str_replace("'", "\\'", str_replace(["\n", "\r"], " ",$product->name))]; $results[] = $result; if(count($results) > 15) { $results[] = ['value' => 0, 'text' => "  -->   Mehr Suchergebnisse vorhanden. Bitte Suchbegriff genauer definieren   <--"]; break; } } $this->returnJson($results); } }