private function setData(\Club\ShopBundle\Entity\Product $product, $data) { $em = $this->getDoctrine()->getManager(); foreach ($data as $attribute => $value) { if ($attribute == 'location') { $str = ''; foreach ($value as $l) { $str .= $l->getId() . ','; } $str = preg_replace("/,\$/", "", $str); $value = $str != '' ? $str : null; } if (preg_match("/^(start_date|expire_date)\$/", $attribute) && $value != '') { $value = $value->format('Y-m-d'); } if (preg_match("/^(start_time|stop_time)\$/", $attribute) && $value != '') { $value = $value->format('H:i:s'); } $prod_attr = $em->getRepository('ClubShopBundle:ProductAttribute')->findOneBy(array('product' => $product->getId(), 'attribute' => $attribute)); if (strlen($value)) { $prod_attr = !$prod_attr ? $this->buildProductAttribute($product, $attribute) : $prod_attr; $prod_attr->setValue($value); $em->persist($prod_attr); } elseif ($prod_attr && $value == '') { $em->remove($prod_attr); } } $em->flush(); }
public function getAccount(\Club\ShopBundle\Entity\Product $product, \Club\UserBundle\Entity\Location $location) { if ($product->getAccount()) { return $product->getAccount(); } $account = $this->_em->getRepository('ClubUserBundle:LocationConfig')->getObjectByKey('account_default_income', $location); return $account; }
public function getAttribute(\Club\ShopBundle\Entity\Product $product) { $attr = new \Club\ShopBundle\Model\Attribute(); foreach ($product->getProductAttributes() as $a) { $v = 'set' . preg_replace("/_/", "", $a->getAttribute()); if ($a->getAttribute() == 'location') { $res = new \Doctrine\Common\Collections\ArrayCollection(); $locations = $this->em->getRepository('ClubUserBundle:Location')->getByIds(explode(",", $a->getValue())); foreach ($locations as $location) { $res[] = $location; } $attr->{$v}($res); } else { $attr->{$v}($a->getValue()); } } return $attr; }
private function setData(\Club\ShopBundle\Entity\Product $product, $data) { $em = $this->getDoctrine()->getEntityManager(); foreach ($em->getRepository('ClubShopBundle:Attribute')->findAll() as $attr) { $prod_attr = $em->getRepository('ClubShopBundle:ProductAttribute')->findOneBy(array('product' => $product->getId(), 'attribute' => $attr->getId())); if (($attr->getAttributeName() == 'StartDate' || $attr->getAttributeName() == 'ExpireDate') && $data[$attr->getAttributeName()] != '') { $data[$attr->getAttributeName()] = $data[$attr->getAttributeName()]->format('Y-m-d'); } if ($attr->getAttributeName() == 'Location') { $str = ''; foreach ($data[$attr->getAttributeName()] as $l) { $str .= $l->getId() . ','; } $str = preg_replace("/,\$/", "", $str); $str = $str != '' ? $str : null; if ($str == '') { if ($prod_attr) { $em->remove($prod_attr); } } else { if (!$prod_attr) { $prod_attr = $this->buildProductAttribute($product, $attr); } $prod_attr->setValue($str); $em->persist($prod_attr); } } else { if ($prod_attr && $data[$attr->getAttributeName()] == '') { $em->remove($prod_attr); } elseif ($prod_attr && $data[$attr->getAttributeName()] != '') { $prod_attr->setValue($data[$attr->getAttributeName()]); $em->persist($prod_attr); } elseif (!$prod_attr && $data[$attr->getAttributeName()] != '') { $prod_attr = $this->buildProductAttribute($product, $attr); $prod_attr->setValue($data[$attr->getAttributeName()]); $em->persist($prod_attr); } } } $em->flush(); }
private function validateAttributes(\Club\ShopBundle\Entity\Product $product) { foreach ($product->getProductAttributes() as $attr) { switch ($attr->getAttribute()) { case 'only_member': if (!$this->security_context->isGranted('IS_AUTHENTICATED_FULLY')) { throw new \Exception('You have to be logged in to buy this product'); } elseif ($attr->getValue() && !$this->club_user->isMember($this->token->getUser())) { throw new \Exception('Cannot buy due to missing subscription'); } break; case 'amount_per_member': if ($attr->getValue() > 0) { $cartProduct = $this->em->getRepository('ClubShopBundle:CartProduct')->getProductInCart($product, $this->cart); if ($cartProduct && $cartProduct->getQuantity() >= $attr->getValue()) { throw new \Exception('Cannot buy more of this item'); } if ($this->security_context->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $bought = $this->em->getRepository('ClubShopBundle:OrderProduct')->getBoughtByUser($product, $this->cart->getUser()); if (count($bought) >= $attr->getValue()) { throw new \Exception('You cannot buy anymore of this product'); } } } break; } } }
public function getBoughtByUser(\Club\ShopBundle\Entity\Product $product, \Club\UserBundle\Entity\User $user) { return $this->createQueryBuilder('op')->join('op.order', 'o')->join('o.user', 'u')->join('op.product', 'p')->where('u.id = :user')->andWhere('p.id = :product')->andWhere('o.cancelled = false')->setParameter('user', $user->getId())->setParameter('product', $product->getId())->getQuery()->getResult(); }
public function getUsersByProduct(\Club\ShopBundle\Entity\Product $product) { return $this->_em->createQueryBuilder()->select('op, o, u')->from('ClubShopBundle:OrderProduct', 'op')->join('op.order', 'o')->join('o.user', 'u')->where('op.product = :product')->andWhere('o.paid = true')->orderBy('o.id', 'DESC')->setParameter('product', $product->getId())->getQuery()->getResult(); }
public function getProductInCart(\Club\ShopBundle\Entity\Product $product, \Club\ShopBundle\Entity\Cart $cart) { return $this->createQueryBuilder('cp')->join('cp.cart', 'c')->join('cp.product', 'p')->where('c.id = :cart')->andWhere('p.id = :product')->setParameter('cart', $cart->getId())->setParameter('product', $product->getId())->getQuery()->getOneOrNullResult(); }