getID(); $pageVars = []; $product = new \Product($ID); if (findModule(\Modules::PRODUCTS_RELATED)) { $qb = sqlQueryBuilder() ->select('p.title, p.code, p.in_store, ph.id as id_photo') ->fromProducts() ->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') ->addSelect(RelatedProducts::relatedProductsSpec([$ID])) ->groupBy('p.id'); if (findModule(\Modules::PRODUCTS_RELATED, \Modules::SUB_TYPES)) { $qb->addSelect('GROUP_CONCAT(pr.type) as types'); } $pageVars['products_related'] = $pageVars['products_related_dynamic'] = []; foreach ($qb->execute() as $key => $row) { $pageVars['products_related'][$key] = $row; $pageVars['products_related'][$key]['image'] = getImage($row['id_photo'], null, null, 4); if (findModule(\Modules::PRODUCTS_RELATED, \Modules::SUB_TYPES)) { $pageVars['products_related'][$key]['types'] = array_map('intval', explode(',', $row['types'])); } } if (findModule(\Modules::DYNAMIC_RELATED_PRODUCTS)) { $objectForHydrate = sqlQueryBuilder() ->select('prt.id', 'prt.name') ->from('products_related_types', 'prt') ->execute() ->fetchAll(); $getAllTypes = []; foreach ($objectForHydrate as $v) { $getAllTypes[$v['id']] = $v; } foreach ($pageVars['products_related'] as $key => $row) { if ($row['id_products_related_dynamic'] !== null) { foreach ($row['types'] as &$v) { $v = $getAllTypes[(int) $v]; } $pageVars['products_related_dynamic'][] = $row; unset($pageVars['products_related'][$key]); } } $pageVars['products_related'] = array_values($pageVars['products_related']); $pageVars['products_related_dynamic'] = array_values($pageVars['products_related_dynamic']); } if (findModule(\Modules::PRODUCTS_RELATED, \Modules::SUB_TYPES)) { $pageVars['products_related_types'] = sqlQueryBuilder()->select('*')->from('products_related_types')->execute()->fetchAllAssociative(); } } if (findModule(\Modules::PRODUCTS_COLLECTIONS)) { $pageVars['collections'] = $product->fetchCollections(false); if (!empty($pageVars['collections']['own'])) { foreach ($pageVars['collections']['own'] as &$collection) { $collection->fetchImages('admin'); } } } if (findModule(\Modules::ARTICLES)) { $pageVars['articles'] = sqlQueryBuilder() ->select('pia.id_article as value, a.title as label, a.title as text') ->from('products_in_articles', 'pia') ->leftJoin('pia', 'articles', 'a', 'a.id = pia.id_article') ->andWhere(Operator::equals(['pia.id_product' => $this->getID()])) ->execute()->fetchAllAssociative(); } $vars['body'] = array_merge($vars['body'], $pageVars); return $vars; } protected function handleSubmit(): void { $ID = $this->getID(); $data = getVal('data', null, []); // zapsani produktovych sekci if (findModule(\Modules::PRODUCTS_SECTIONS)) { $sec = getVal('sec', null, []); $no = count($sec); sqlQueryBuilder() ->delete('products_in_sections') ->where(Operator::equals(['id_product' => $ID, 'generated' => 0])) ->execute(); if ($no > 0) { foreach ($sec as $sectionId => $value) { if (!empty($sectionId)) { sqlQuery("INSERT IGNORE INTO products_in_sections SET id_product='".$ID."', id_section='".$sectionId."', figure='Y' "); } } } } // zapsani souvisejiciho zbozi if (findModule(\Modules::PRODUCTS_RELATED)) { $relProd = getVal('relPro', $data, []); // filter duplicates in relProd array $relProd = array_unique($relProd, SORT_REGULAR); foreach ($relProd as $id => $rel) { $rel['id_rel_product'] = intval($rel['id_product']); $rel['id_top_product'] = $ID; unset($rel['id_product']); // ignore item if generated automatically with 'id_products_related_dynamic' $isAutomatic = isset($rel['id_products_related_dynamic']); if ($isAutomatic) { $rel['id_products_related_dynamic'] = null; } if (findModule(\Modules::PRODUCTS_RELATED, \Modules::SUB_TYPES)) { $types = $rel['type'] ?? []; if (empty($types)) { $types[] = sqlQueryBuilder() ->select('id') ->from('products_related_types') ->orderBy('id', 'ASC') ->execute()->fetchOne(); } sqlQueryBuilder() ->delete('products_related') ->where(Operator::not(Operator::inStringArray($types, 'type'))) ->andWhere(Operator::equals($this->filterFields($rel, ['id_top_product', 'id_rel_product']))) ->execute(); foreach ($types as $type) { $this->saveRelatedProduct($id, $rel, $type); } } else { $this->saveRelatedProduct($id, $rel); } } } // zapsani kolekci if (findModule(\Modules::PRODUCTS_COLLECTIONS)) { $collections = getVal('collections', $data, []); foreach ($collections as $id => $collection) { $collection['id_product_related'] = intval($collection['id_product_related']); $collection['id_product'] = $ID; if (!empty($collection['delete']) || !$id || $collection['id_product_related'] <= 0) { if ($id > 0) { $this->deleteSQL('products_collections', $this->filterFields($collection, ['id_product', 'id_product_related'])); } continue; } if ($id < 0) { sqlQuery('INSERT IGNORE INTO products_collections (id_product, id_product_related) VALUES (:id_product, :id_product_related)', $collection); } else { $this->updateSQL('products_collections', $collection, $this->filterFields($collection, ['id_product', 'id_product_related'])); } } } // zapsani produktu k clankum if (findModule(\Modules::ARTICLES)) { $articles = getVal('articles', $data, []); foreach ($articles as $key => $article) { $article['id_article'] = intval($article['id_article']); $article['id_product'] = $ID; if (!empty($article['delete']) || !$key) { if ($key > 0) { $this->deleteSQL('products_in_articles', [ 'id_article' => $article['id_article'], 'id_product' => $article['id_product'], ]); } continue; } if ($key < 0) { if (!$this->selectSQL('products_in_articles', $article)->fetchOne()) { $this->insertSQL('products_in_articles', $article); } } } } } private function saveRelatedProduct($id, $rel, $type = null) { if ($this->isDuplicate() && $id > 0) { $id = -99; } if ($type) { $rel['type'] = $type; } if (!empty($rel['delete']) || !$id || $rel['id_rel_product'] <= 0) { if ($id > 0) { $this->deleteSQL('products-related', $this->filterFields($rel, ['id_top_product', 'id_rel_product', 'type'])); } return; } $where = $this->filterFields($rel, ['id_top_product', 'id_rel_product', 'type']); if (!$this->selectSQL('products-related', $where)->fetch()) { $this->insertSQL('products-related', $rel); } else { if (findModule(\Modules::DYNAMIC_RELATED_PRODUCTS)) { $rel['id_products_related_dynamic'] = null; } $this->updateSQL('products-related', $rel, $where); } } public function handleCopyRelatedProducts() { try { $copyId = getVal('copy_id'); $type = ' id_rel_product'; if (findModule(\Modules::PRODUCTS_RELATED, \Modules::SUB_TYPES)) { $type = $type.', type'; } sqlQuery( "INSERT IGNORE INTO products_related (id_top_product, position, {$type}) SELECT :id_paste as id_top_product, position, {$type} FROM products_related WHERE id_top_product = :id_copy", ['id_copy' => $copyId, 'id_paste' => $this->getID()] ); } catch (\Exception $e) { } $this->returnOK(); } } return ProductsSections::class;