<?php
namespace App\Repository;
use App\Entity\Cart;
use App\Entity\Prod;
use App\Model\Prod as ModelProd;
use App\RepositoryInterface\ProdRepositoryInterface;
use App\Service\Auth\Auth;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
/**
* @extends ServiceEntityRepository<Prod>
*
* @method Prod|null find($id, $lockMode = null, $lockVersion = null)
* @method Prod|null findOneBy(array $criteria, array $orderBy = null)
* @method Prod[] findAll()
* @method Prod[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProdRepository extends ServiceEntityRepository implements ProdRepositoryInterface
{
private ModelProd $ModelProd;
public function __construct(ManagerRegistry $registry, ModelProd $ModelProd, protected CharRepository $Chars, protected CharvalRepository $Charvals)
{
$this->ModelProd = $ModelProd;
parent::__construct($registry, Prod::class);
}
public function save(): void
{
$this->getEntityManager()->flush();
}
public function add(Prod $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Prod $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function get(int $id): ?Prod {
return $this->find($id);
}
public function getNewProds(int $results): array
{
return $this->getEntityManager()->createQuery('SELECT p FROM App\Entity\Prod p WHERE p.visible = 1 and p.skidka > 0 and (p.num != 0 or p.num2 != 0 or p.num3 != 0) ORDER BY p.changed desc')
->setMaxResults(12)
->getResult();
}
public function getallforexport(array $cats_for_export, $opt = 0): array
{
$cats = [];
foreach ($cats_for_export as $cat) {
$cats[$cat->getId()] = $cat->getId();
}
$prods = array();
$prods = $this->getEntityManager()->createQuery("SELECT p FROM \App\Entity\Prod p WHERE p.visible = 1 AND p.price > 0 AND p.num > 0 AND p.name != '' and p.cat in (".implode(",", $cats).") ORDER By p.id")->getResult();
return $prods;
}
public function getprodchars(int $prod_id): array
{
$prod_chars = array();
$q = "select c.id as cid, c.name as cname, c.izm, cv.id as cvid, cv.value as value, pc.charval as val, pc.value as text
from `prodchar` as pc
left join `char` as c on c.id = pc.`char`
left join `charval` as cv on cv.id = pc.charval
where c.inprod = 1 and pc.prod = '".$prod_id."' and cv.value != ''";
$prod_chars = $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
foreach ($prod_chars as $k => $v) {
$char = $this->Chars->find($v['cid']);
$charval = $this->Charvals->find($v['cvid']);
$prod_chars[$k]['cname'] = $char->getName();
$prod_chars[$k]['value'] = $charval->getValue();
}
return $prod_chars;
}
public function getprodchars_all(): array
{
$prod_chars = array();
$q = "select pc.prod as pid, c.id as cid, c.name as cname, c.izm, cv.value as value, pc.charval as val, pc.value as text
from `prodchar` as pc
left join `char` as c on c.id = pc.`char`
left join `charval` as cv on cv.id = pc.charval
where cv.value != ''";
$pc = $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
foreach ($pc as $r) {
$char = $this->Chars->find($r['cid']);
$charval = $this->Charvals->find($r['cvid']);
$r['cname'] = $char->getName();
$r['value'] = $charval->getValue();
$prod_chars[$r['pid']][$r['cid']] = $r;
}
return $prod_chars;
}
public function getprodchilds(int $id, bool $opt = false): array
{
$opt = $opt ? 'opt' : '';
$prods = [];
$prod_ids = [];
/** @var \App\Repository\CartRepository $Cart */
$Cart = $this->getEntityManager()->getRepository(Cart::class);
$cart = $Cart->findOneBy(['prod' => $id]);
//find findgetone(array("where" => "prod = ".$id));
if ($cart && $cart->getOrderId()) {
$q = "select p.id, p.art, p.name, p.num, p.num2, p.num3, p.price".$opt." as price, p.price2".$opt." as price2, p.price3".$opt." as price3, p.colors, p.skidka".$opt." as skidka, p.skidka".$opt."2 as skidka2, p.skidka".$opt."3 as skidka3, p.inpack
from cart as c
left join prod as p on p.id = c.prod_id
where c.order_id = ".$cart->getOrderId()." and p.visible = 1 and (p.num > 0 or p.num2 > 0 or p.num3 > 0) and p.id != ".$id." limit 6";
$pc = $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
foreach ($pc as $r) {
$r['colors'] = json_decode($r['colors']);
$prods[$r['id']] = $r;
//$prods[$r['id']]->prices = $this->ModelProd->getPrices($r);
}
}
foreach ($prods as $k => $prod) {
$prod_ids[] = $prod['id'];
}
if (count($prod_ids) == 0) {
return [];
}
$prods = $this->findBy(['id' => $prod_ids]);
foreach ($prods as $k => $prod) {
$prods[$k]->prices = $this->ModelProd->getPrices($prod);
}
return $prods;
}
function filter_selected ($char, $val)
{
$filter = $this->getchars();
if ($filter[$char] && ((is_array($filter[$char]) && in_array($val, $filter[$char])) || $filter[$char] == $val)) {
return true;
} else {
return false;
}
}
function filter ($prods, $chars = array(), bool $is_opt = false) {
$qwe = '';
if (empty($chars)) {
return $prods;
}
$filter = $chars;
foreach($filter as $k => $char) {
if($char == 'action' && $is_opt) {
$filter[$k] = 'actionopt';
}
}
if (isset($filter['sale']) && $filter['sale']) {
$filter[$filter['sale']] = "1";
unset($filter['sale']);
} else {
unset($filter['sale']);
}
$ids = array();
if (empty($filter)) {
return $prods;
}
$fc = count($filter);
foreach ($prods as $k => $prod) {
if (empty($prod['chars'])) {
continue;
}
$filter_vals_founded = 0;
foreach ($filter as $c => $cv) {
if (is_array($cv) && count($cv) > 1) { // Выбрано нескольно значений одной характеристики
foreach ($cv as $ccv) {
if (strstr($prod['chars'], $c.":".$ccv.";")) { // Если хотя бы одно значение найдено, то значение считается найдено
$qwe .= $c.":".$ccv.";";
$filter_vals_founded++;
break;
}
}
} else { // Выбрано ОДНО значение одной характеристики
if (is_array($cv)) {
$cv = $cv[0];
}
if (strstr($prod['chars'], $c.":".$cv.";")) {
$filter_vals_founded++;
}
}
}
if ($filter_vals_founded >= $fc) { // Если найдены все значения характеристик, помещаем товар в массив
$ids[] = $prod;
}
}
return $ids;
}
/**
*
* @param array $ids
* @return Prod[]
*/
public function prodArrayToEntity(array $prods): array
{
$ids = [];
foreach ($prods as $k => $v) {
$ids[] = $v['id'];
}
$prods = $this->findBy(['id' => $ids]);
foreach ($prods as $k => $prod) {
$prods[$k]->prices = $this->ModelProd->getPrices($prod);
}
return $prods;
}
public static function sortByChangedAsc($a, $b)
{
if ($a->getChanged() == $b->getChanged()) {
return 0;
}
return ($a->getChanged() < $b->getChanged()) ? 1 : -1;
}
public static function sortByChangedDesc($a, $b)
{
if ($a->getChanged() == $b->getChanged()) {
return 0;
}
return ($a->getChanged() > $b->getChanged()) ? 1 : -1;
}
public static function sortByPriorDesc($a, $b)
{
if ($a->getPrior() == $b->getPrior()) {
return 0;
}
return ($a->getPrior() > $b->getPrior()) ? -1 : 1;
}
public static function sortByPriceDesc($a, $b)
{
if ($a->getPrice() == $b->getPrice()) {
return 0;
}
return ($a->getPrice() > $b->getPrice()) ? -1 : 1;
}
public static function sortByPriceAsc($a, $b)
{
if ($a->getPrice() == $b->getPrice()) {
return 0;
}
return ($a->getPrice() < $b->getPrice()) ? -1 : 1;
}
public static function sortBySkidkaDesc($a, $b)
{
if ($a->getSkidka() == $b->getSkidka()) {
return 0;
}
return ($a->getSkidka() > $b->getSkidka()) ? -1 : 1;
}
public static function sortArrayByChangedAsc($a, $b)
{
if ($a['changed'] == $b['changed']) {
return 0;
}
return ($a['changed'] < $b['changed']) ? 1 : -1;
}
public static function sortArrayByChangedDesc($a, $b)
{
if ($a['changed'] == $b['changed']) {
return 0;
}
return ($a['changed'] > $b['changed']) ? 1 : -1;
}
public static function sortArrayByPriorDesc($a, $b)
{
if ($a['prior'] == $b['prior']) {
return 0;
}
return ($a['prior'] > $b['prior']) ? -1 : 1;
}
public static function sortArrayByPriceDesc($a, $b)
{
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] > $b['price']) ? -1 : 1;
}
public static function sortArrayByPriceAsc($a, $b)
{
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
public static function sortArrayBySkidkaDesc($a, $b)
{
if ($a['skidka'] == $b['skidka']) {
return 0;
}
return ($a['skidka'] > $b['skidka']) ? -1 : 1;
}
public function getDoubles(): array
{
$prods = array();
$q = "select id, name, art, count(art) as count_art
from `prod`
group by art
having count(art) > 1";
$prods = $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
return $prods;
}
public function getNotInCart(): array
{
$prods = array();
$q = "select p.id, p.external_id, p.name, p.art, c.id as cid
from `prod` as p
left join `cart` as c on c.prod_id = p.id
where c.id is NULL";
$prods = $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
return $prods;
}
public function deleteWithDependencies(int $id)
{
$q = "delete from prod where id = $id";
$this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
$q = "delete from prodchar where prod = $id";
$this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
$q = "delete from prod_translation where object_id = $id";
$this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
}
public function getProdPics(): array
{
$prods = array();
$q = "select id, pic from `prod`";
$pc = $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
foreach ($pc as $r) {
$prods[] = $r;
}
return $prods;
}
public function findOneProductsInLocale($ids, $locale)
{
$qb = $this->createQueryBuilder('p')
->select('p')
->where('p.id in (:id)')
->setParameter('id', $ids);
;
$query = $qb->getQuery()->useQueryCache(false);
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
// force Gedmo Translatable to not use current locale
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
$locale
);
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_FALLBACK,
1
);
return $query->getResult();
}
}