getIDs(); $products = []; // $SQL = $this->selectSQL('products_in_sections', ['id_section' => $ID], ['id_product', 'figure']); $qb = sqlQueryBuilder() ->select('SQL_CALC_FOUND_ROWS COALESCE(ps.id_product, p.id) as id_product, COALESCE(ps.id_product, p.id) as id, p.title, p.code, p.in_store, ph.id as id_photo') ->from('products', 'p') ->leftJoin('p', 'products_in_sections', 'ps', 'p.id = ps.id_product') ->leftJoin('p', 'photos_products_relation', 'ppr', 'ppr.id_product=p.id AND ppr.show_in_lead="Y"') ->leftJoin('ppr', 'photos', 'ph', 'ph.id=ppr.id_photo') ->groupBy('p.id') ->setMaxResults(100); if ($this->section) { $qb->where(\Query\Operator::equals(['ps.id_section' => $this->section])); } if ($this->producer) { $qb->andWhere(\Query\Operator::equals(['p.producer' => $this->producer])); } if (findModule(Modules::PRODUCTS_SECTIONS, 'virtual_to_db')) { $qb->andWhere(\Query\Operator::equals(['ps.generated' => 0])); } $qb = $qb->execute(); $totalCount = returnSQLResult('SELECT FOUND_ROWS()'); foreach ($qb as $product) { $product['image'] = getImage($product['id_photo'], null, null, 4); $products[] = $product; } $pageVars['ID'] = $this->getID(); $pageVars['products'] = $products; $pageVars['productsTotalCount'] = $totalCount; $qb = sqlQueryBuilder() ->select('psp.id as id_position, psp.id_product as id, psp.position, p.title, p.code, p.in_store, ph.id as id_photo') ->from('products_in_sections_positions', 'psp') ->leftJoin('psp', 'products', 'p', 'p.id = psp.id_product') ->leftJoin('p', 'photos_products_relation', 'ppr', 'ppr.id_product=p.id AND ppr.show_in_lead="Y"') ->leftJoin('ppr', 'photos', 'ph', 'ph.id=ppr.id_photo') ->where(\Query\Operator::equalsNullable(['psp.id_section' => $this->section, 'psp.id_producer' => $this->producer])) ->orderBy('psp.position', 'ASC') ->groupBy('p.id') ->execute(); $cfg = Config::get(); $topProducts = []; foreach ($qb as $topProduct) { $topProduct['image'] = getImage($topProduct['id_photo'], null, null, 4); $topProducts[] = $topProduct; } $pageVars['top_products'] = $topProducts; $vars['body'] = $pageVars; return $vars; } public function handle() { $this->getIDs(); $data = getVal('data'); $topProductIds = []; if (!empty($data['topPro'])) { foreach ($data['topPro'] as $id => $item) { if ($productId = $item['id_top_product'] ?? null) { $topProductIds[] = (int) $productId; } // delete item if (!empty($item['delete'])) { if (!empty($item['id'])) { sqlQueryBuilder()->delete('products_in_sections_positions') ->where(\Query\Operator::equals(['id' => $item['id']])) ->execute(); } } else { try { if ( (!$this->section || ($this->section && $this->checkProductCategory($item['id_top_product']))) && (!$this->producer || ($this->producer && $this->checkProductProducer($item['id_top_product'], $this->producer))) ) { $cond = ['id_section' => $this->section ?: null, 'id_producer' => $this->producer ?: null, 'id_product' => $item['id_top_product'], ]; if ($id < 0) { $cond['position'] = $item['position']; $this->insertSQL('products_in_sections_positions', $cond); } else { sqlQueryBuilder()->update('products_in_sections_positions') ->set('position', $item['position']) ->where(\Query\Operator::equalsNullable($cond)) ->execute(); } } else { $this->returnError('Chyba: Produkt ID '.$item['id_top_product'].' nemohl být přidán do top produktů, protože nepatří do této nebo podřazené sekce! '); } } catch (Exception $e) { $this->returnError('Chyba při ukládání: '.$e->getMessage()); } } } } $this->removeDuplicateProductsInSectionsPositions(); $productIds = []; if (!empty($data['products'])) { if (!empty($this->section)) { foreach ($data['products'] as $id => $item) { if ($productId = $item['id_product'] ?? null) { $productIds[] = (int) $productId; } $item['id'] = intval($id); if (!empty($item['delete']) || !$id) { if ($id > 0) { $this->deleteSQL('products_in_sections', ['id_product' => $id, 'id_section' => $this->section]); } continue; } try { if ($id < 0) { $this->insertSQL('products_in_sections', ['id_product' => $item['id_product'], 'id_section' => $this->section]); } } catch (Doctrine\DBAL\DBALException $e) { } } } } if (!empty($data['products']) || !empty($data['topPro'])) { if (findModule(\Modules::PRODUCTS_SECTIONS, \Modules::SUB_ELASTICSEARCH) && ($allProductIds = array_unique([...$topProductIds, ...$productIds])) && !findModule(Modules::KAFKA) ) { $this->updateProductsFulltext($allProductIds); } $this->returnOK(); } } public function checkProductCategory($productId) { $virtual = $this->selectSQL('sections', ['id' => $this->section], ['virtual'])->fetchColumn(); if ($virtual == 'Y') { return true; } $categories = getDescendantCategories($this->section); $count = sqlQueryBuilder() ->select('*') ->from('products_in_sections') ->where(\Query\Operator::equals(['id_product' => $productId])) ->andWhere(\Query\Operator::inIntArray($categories, 'id_section')) ->execute()->rowCount(); if ($count > 0) { return true; } return false; } public function checkProductProducer($productId) { return $this->selectSQL('products', ['id' => $productId, 'producer' => $this->producer])->fetchColumn(); } public function getIDs() { if (!$this->producer && !$this->section) { $this->producer = getVal('producer') ?: null; $this->section = $this->getID() ?: null; $this->setID($this->section.'&producer='.$this->producer); } } /** Remove duplicate products_in_sections_positions */ public function removeDuplicateProductsInSectionsPositions() { $this->getIDs(); $cond = [ 'id_section' => $this->section ?: null, 'id_producer' => $this->producer ?: null, ]; $duplicates = sqlQueryBuilder() ->select('psp.id, psp.id_product, psp.id_section, psp.id_producer, COUNT(psp.position) AS duplicate') ->from('products_in_sections_positions', 'psp') ->where(\Query\Operator::equalsNullable($cond)) ->groupBy('psp.id_product, psp.id_section, psp.id_producer') ->having('duplicate > 1') ->orderBy('psp.position', 'ASC') ->execute()->fetchAll(); $deleted = 0; foreach ($duplicates as $row) { $cond['id_product'] = $row['id_product']; $count = sqlQueryBuilder()->delete('products_in_sections_positions') ->where(\Query\Operator::equalsNullable($cond)) ->andWhere('id <> '.$row['id'])->execute(); $deleted += $count; } return $deleted; } }