Пример #1
0
 public function addAction()
 {
     return false;
     $logger = $this->getServiceLocator()->get('Logger');
     $this->initialize();
     $form = $this->getServiceLocator()->get('PreReg\\Form\\ProductView');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($form->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $request->getPost();
             $logger->info(var_export($data, true));
             /*
              * get needed variables
              */
             $participant_id = 0;
             if (isset($data['participant_id'])) {
                 $participant_id = $data['participant_id'];
                 unset($data['participant_id']);
             }
             $agegroup_id = 0;
             if (isset($data['agegroup_id'])) {
                 $agegroup_id = $data['agegroup_id'];
                 unset($data['agegroup_id']);
             }
             /*
              *  check if participant already has a personalized ticket
              */
             $cartContainer = new Container('cart');
             $package = $cartContainer->order->getPackageByParticipantSessionId($participant_id);
             if ($package != null && $package->hasPersonalizedItem()) {
                 $logger->warn('Package for participant ' . $participant_id . ' already has a personalized item. What should I do?');
             }
             /*
              * get according product entity from database
              */
             $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
             $product = $em->getRepository("ErsBase\\Entity\\Product")->findOneBy(array('id' => $data['Product_id']));
             /*
              *  prepare product data to populate item
              */
             $product_data = $product->getArrayCopy();
             $product_data['Product_id'] = $product_data['id'];
             unset($product_data['id']);
             /*
              * search the according agegroup
              */
             if ($participant_id != 0) {
                 $participant = $cartContainer->order->getParticipantBySessionId($participant_id);
                 $agegroupService = $this->getServiceLocator()->get('ErsBase\\Service\\AgegroupService');
                 $agegroup = $agegroupService->getAgegroupByUser($participant);
             } elseif ($agegroup_id != 0) {
                 $agegroup = $em->getRepository("ErsBase\\Entity\\Agegroup")->findOneBy(array('id' => $agegroup_id));
             } else {
                 $logger->emerg('Unable to add/edit product!');
             }
             /*
              * get deadline
              */
             $deadlineService = $this->getServiceLocator()->get('ErsBase\\Service\\DeadlineService:price');
             /*$deadlineService = new Service\DeadlineService();
               $deadlines = $em->getRepository("ErsBase\Entity\Deadline")
                       ->findBy(array('price_change' => '1'));
               $deadlineService->setDeadlines($deadlines);*/
             $deadline = $deadlineService->getDeadline();
             /*
              * build up item entity
              */
             $item = new Entity\Item();
             #$item->populate($product_data);
             $item->setPrice($product->getProductPrice($agegroup, $deadline)->getCharge());
             $item->setAmount(1);
             $item->populate((array) $product_data);
             /*
              * add variant data to item entity
              */
             $variant_data = $data['pv'];
             foreach ($product->getProductVariants() as $variant) {
                 $value = $em->getRepository("ErsBase\\Entity\\ProductVariantValue")->findOneBy(array('id' => $variant_data[$variant->getId()]));
                 if ($value) {
                     $itemVariant = new Entity\ItemVariant();
                     $itemVariant->populateFromEntity($variant, $value);
                     $item->addItemVariant($itemVariant);
                     #$logger->info('VARIANT '.$variant->getName().': '.$value->getValue());
                 } else {
                     $logger->warn('Unable to find value for variant: ' . $variant->getName() . ' (id: ' . $variant->getId() . ')');
                 }
             }
             /*
              * check product packages and add data to item entity
              */
             $productPackages = $em->getRepository("ErsBase\\Entity\\ProductPackage")->findBy(array('Product_id' => $product->getId()));
             foreach ($productPackages as $package) {
                 $subProduct = $package->getSubProduct();
                 $subItem = new Entity\Item();
                 $subItem->setPrice(0);
                 $subItem->setAmount($package->getAmount());
                 $product_data = $subProduct->getArrayCopy();
                 $product_data['Product_id'] = $product_data['id'];
                 unset($product_data['id']);
                 $subItem->populate($product_data);
                 foreach ($subProduct->getProductVariants() as $variant) {
                     $value = $em->getRepository("ErsBase\\Entity\\ProductVariantValue")->findOneBy(array('id' => $variant_data[$variant->getId()]));
                     if ($value) {
                         $itemVariant = new Entity\ItemVariant();
                         $itemVariant->populateFromEntity($variant, $value);
                         $subItem->addItemVariant($itemVariant);
                         #$logger->info('VARIANT '.$variant->getName().': '.$value->getValue());
                     } else {
                         $logger->warn('Unable to find value for variant of subItem: ' . $variant->getName() . ' (id: ' . $variant->getId() . ')');
                     }
                 }
                 $itemPackage = new Entity\ItemPackage();
                 $itemPackage->setItem($item);
                 $itemPackage->setSubItem($subItem);
                 $item->addChildItem($itemPackage);
             }
             /*
              * delete the item we have edited when we're in edit mode
              */
             if (isset($cartContainer->editItem) && $cartContainer->editItem instanceof Entity\Item) {
                 $cartContainer->order->removeItem($cartContainer->editItem->getSessionId());
                 unset($cartContainer->editItem);
             }
             /*
              * add the newly created item
              */
             $cartContainer->order->addItem($item, $participant_id);
             /*
              * the chooser for product, shopping cart or stay on product 
              * page will be visible for two pageloads. This prevents the 
              * chooser overlay from getting displayd in strange situations
              */
             $cartContainer->chooser = true;
             $cartContainer->chooserCount = 2;
             /*
              * go the route of the breadcrumbs and find the way back. :)
              */
             $forrest = new Service\BreadcrumbService();
             if (!$forrest->exists('cart')) {
                 $forrest->set('cart', 'product');
             }
             $breadcrumb = $forrest->get('cart');
             $logger->info(var_export($breadcrumb, true));
             return $this->redirect()->toRoute($breadcrumb->route, $breadcrumb->params, $breadcrumb->options);
         } else {
             $logger = $this->getServiceLocator()->get('Logger');
             $logger->warn($form->getMessages());
         }
     }
 }
Пример #2
0
 public function addAction()
 {
     /*
      * Get and check parameters
      */
     $product_id = (int) $this->params()->fromRoute('product_id', 0);
     $item_id = (int) $this->params()->fromRoute('item_id', 0);
     if (!$product_id) {
         return $this->redirect()->toRoute('product', array('action' => 'index'));
     }
     $participant_id = $this->params()->fromQuery('participant_id');
     $agegroup_id = $this->params()->fromQuery('agegroup_id');
     if (!is_numeric($participant_id)) {
         $participant_id = null;
     }
     if (!is_numeric($agegroup_id)) {
         $agegroup_id = null;
     }
     /*
      * Build and set breadcrumbs
      */
     $forrest = new Service\BreadcrumbService();
     if (!$forrest->exists('product')) {
         $forrest->set('product', 'product');
     }
     $params = array();
     #$params2 = array();
     $options = $this->params()->fromQuery();
     $params['action'] = 'add';
     $params['product_id'] = $product_id;
     if ($participant_id != 0) {
         $options['query'] = array('participant_id' => $participant_id);
     } elseif ($agegroup_id != 0) {
         $options['query'] = array('agegroup_id' => $agegroup_id);
     }
     $params2 = $params;
     if ($item_id) {
         $params['action'] = 'edit';
         # When we're in edit mode there may not be a item_id from
         # returning back from participant (#114)
         $params2 = $params;
         $params2['item_id'] = $item_id;
     }
     $forrest->set('participant', 'product', $params2, $options);
     $forrest->set('cart', 'product', $params2, $options);
     $forrest->set('product', 'product', $params2, $options);
     $forrest->set('bc_stay', 'product', $params2, $options);
     /*
      * Get data for this product
      */
     $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $product = $em->getRepository("ErsBase\\Entity\\Product")->findOneBy(array('id' => $product_id));
     $status = $em->getRepository('ErsBase\\Entity\\Status')->findOneBy(array('value' => 'order pending'));
     $form = $this->getServiceLocator()->get('PreReg\\Form\\ProductView');
     #$url = $this->url()->fromRoute('cart', array('action' => 'add'));
     $url = $this->url()->fromRoute('product', $params2, $options);
     $form->setAttribute('action', $url);
     /*
      * Get variants for this product and subproducts
      */
     $variants = $em->getRepository("ErsBase\\Entity\\ProductVariant")->findBy(array('Product_id' => $product_id));
     $defaults = $this->params()->fromQuery();
     $package_info = array();
     foreach ($variants as $variant) {
         $package_info[$variant->getId()] = false;
     }
     $productPackages = $em->getRepository("ErsBase\\Entity\\ProductPackage")->findBy(array('Product_id' => $product_id));
     foreach ($productPackages as $package) {
         $subProduct = $package->getSubProduct();
         $subVariants = $em->getRepository("ErsBase\\Entity\\ProductVariant")->findBy(array('Product_id' => $subProduct->getId()));
         foreach ($subVariants as $variant) {
             $package_info[$variant->getId()] = true;
         }
         $variants = array_merge($variants, $subVariants);
     }
     $form->setVariants($variants, $defaults, $package_info);
     /*
      * get the according deadline
      */
     $deadlineService = $this->getServiceLocator()->get('ErsBase\\Service\\DeadlineService:price');
     $deadline = $deadlineService->getDeadline();
     /*
      * Here starts the cart add Action
      */
     $logger = $this->getServiceLocator()->get('Logger');
     #$this->initializeCart();
     $cartContainer = new Container('cart');
     $formfail = false;
     $request = $this->getRequest();
     if ($request->isPost()) {
         $inputFilter = $form->getInputFilter();
         $form->setInputFilter($inputFilter);
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $request->getPost();
             $logger->info(var_export($data, true));
             /*
              * get needed variables
              */
             $participant_id = 0;
             if (isset($data['participant_id'])) {
                 $participant_id = $data['participant_id'];
                 unset($data['participant_id']);
             }
             $agegroup_id = 0;
             if (isset($data['agegroup_id'])) {
                 $agegroup_id = $data['agegroup_id'];
                 unset($data['agegroup_id']);
             }
             /*
              *  check if participant already has a personalized ticket
              */
             $orderService = $this->getServiceLocator()->get('ErsBase\\Service\\OrderService');
             $order = $orderService->getOrder();
             $package = $order->getPackageByParticipantId($participant_id);
             if ($package != null && $package->hasPersonalizedItem()) {
                 $logger->warn('Package for participant ' . $participant_id . ' already has a personalized item. What should I do?');
             }
             /*
              * get according product entity from database
              */
             $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
             $product = $em->getRepository("ErsBase\\Entity\\Product")->findOneBy(array('id' => $data['Product_id']));
             /*
              * search the according agegroup
              */
             $agegroup = null;
             if ($participant_id != 0) {
                 $participant = $order->getParticipantById($participant_id);
                 if ($participant instanceof Entity\User) {
                     $agegroupService = $this->getServiceLocator()->get('ErsBase\\Service\\AgegroupService');
                     $agegroup = $agegroupService->getAgegroupByUser($participant);
                 }
             } elseif ($agegroup_id != 0) {
                 $agegroup = $em->getRepository("ErsBase\\Entity\\Agegroup")->findOneBy(array('id' => $agegroup_id));
             } else {
                 $logger->emerg('Unable to add/edit product!');
             }
             /*
              *  prepare product data to populate item
              */
             $product_data = $product->getArrayCopy();
             $product_data['Product_id'] = $product_data['id'];
             unset($product_data['id']);
             /*
              * build up item entity
              */
             $item = new Entity\Item();
             $item->setPrice($product->getProductPrice($agegroup, $deadline)->getCharge());
             $item->setStatus($status);
             $item->setProduct($product);
             $item->setAmount(1);
             if ($agegroup != null) {
                 $item->setAgegroup($agegroup->getAgegroup());
             }
             $item->populate((array) $product_data);
             /*
              * add variant data to item entity
              */
             $variant_data = $data['pv'];
             foreach ($product->getProductVariants() as $variant) {
                 $value = $em->getRepository("ErsBase\\Entity\\ProductVariantValue")->findOneBy(array('id' => $variant_data[$variant->getId()]));
                 if ($value && !$value->getDisabled()) {
                     $itemVariant = new Entity\ItemVariant();
                     $itemVariant->populateFromEntity($variant, $value);
                     $itemVariant->setProductVariant($variant);
                     $itemVariant->setProductVariantValue($value);
                     $item->addItemVariant($itemVariant);
                     $itemVariant->setItem($item);
                     #$logger->info('VARIANT '.$variant->getName().': '.$value->getValue());
                 } else {
                     $logger->warn('Unable to find value for variant: ' . $variant->getName() . ' (id: ' . $variant->getId() . ')');
                 }
             }
             /*
              * delete the item we edit when we're in edit mode
              */
             if (is_numeric($item_id) && $item_id != 0) {
                 #$order->removeItem($item_id);
                 $orderService->removeItemById($item_id);
                 #$cartContainer->order->removeItem($item_id);
             }
             /*
              * check product packages and add data to item entity
              */
             $productPackages = $em->getRepository("ErsBase\\Entity\\ProductPackage")->findBy(array('Product_id' => $product->getId()));
             foreach ($productPackages as $package) {
                 $subProduct = $package->getSubProduct();
                 $subItem = new Entity\Item();
                 $subItem->setStatus($status);
                 $subItem->setProduct($subProduct);
                 $subItem->setAmount($package->getAmount());
                 if ($agegroup) {
                     $subItem->setAgegroup($agegroup->getAgegroup());
                 }
                 $product_data = $subProduct->getArrayCopy();
                 $product_data['Product_id'] = $product_data['id'];
                 unset($product_data['id']);
                 $subItem->populate($product_data);
                 $subItem->setPrice(0);
                 $add = false;
                 foreach ($subProduct->getProductVariants() as $variant) {
                     $value = $em->getRepository("ErsBase\\Entity\\ProductVariantValue")->findOneBy(array('id' => $variant_data[$variant->getId()]));
                     if ($value && !$value->getDisabled()) {
                         $add = true;
                         $itemVariant = new Entity\ItemVariant();
                         $itemVariant->populateFromEntity($variant, $value);
                         $itemVariant->setProductVariant($variant);
                         $itemVariant->setProductVariantValue($value);
                         $subItem->addItemVariant($itemVariant);
                         $itemVariant->setItem($subItem);
                     } else {
                         $logger->warn('Unable to find value for variant of subItem: ' . $variant->getName() . ' (id: ' . $variant->getId() . ')');
                     }
                 }
                 if ($add) {
                     $itemPackage = new Entity\ItemPackage();
                     $itemPackage->setSurItem($item);
                     $itemPackage->setSubItem($subItem);
                     $item->addItemPackageRelatedBySurItemId($itemPackage);
                 }
             }
             /*
              * add the newly created item
              */
             $order->addItem($item, $participant_id);
             $em->persist($order);
             $em->flush();
             /*
              * the chooser for product, shopping cart or stay on product 
              * page will be visible for two pageloads. This prevents the 
              * chooser overlay from getting displayd in strange situations
              */
             $cartContainer->chooser = true;
             $cartContainer->chooserCount = 2;
             /*
              * go the route of the breadcrumbs and find the way back. :)
              */
             $forrest = new Service\BreadcrumbService();
             if (!$forrest->exists('product')) {
                 $forrest->set('product', 'product');
             }
             $breadcrumb = $forrest->get('product');
             return $this->redirect()->toRoute($breadcrumb->route, $breadcrumb->params, $breadcrumb->options);
         } else {
             $logger = $this->getServiceLocator()->get('Logger');
             $logger->warn($form->getMessages());
             $formfail = true;
         }
     }
     /*
      * Set form values
      */
     $form->get('submit')->setAttribute('value', 'Add to Cart');
     /*
      * build participant select options
      */
     $optionService = $this->getServiceLocator()->get('ErsBase\\Service\\OptionService');
     $person_options = $optionService->getPersonOptions($product, $participant_id);
     $form->get('participant_id')->setAttribute('options', $person_options);
     /*
      * Disable submit button when there is no person but the ticket is personalized
      */
     if (count($person_options) <= 0 && $product->getPersonalized()) {
         $form->get('submit')->setAttribute('disabled', 'disabled');
     }
     /*
      * Get and build agegroup select options
      */
     $form->get('agegroup_id')->setAttribute('options', $optionService->getAgegroupOptions($agegroup_id));
     /*
      * get all variables for ViewModel
      */
     $breadcrumb = $forrest->get('product');
     $chooser = $cartContainer->chooser;
     $cartContainer->chooser = false;
     $agegroups = $em->getRepository("ErsBase\\Entity\\Agegroup")->findBy(array('price_change' => '1'), array('agegroup' => 'DESC'));
     $participantForm = new Form\Participant();
     $participantForm->setServiceLocator($this->getServiceLocator());
     $participantForm->get('Country_id')->setValueOptions($optionService->getCountryOptions());
     return new ViewModel(array('product' => $product, 'formfail' => $formfail, 'form' => $form, 'participantForm' => $participantForm, 'breadcrumb' => $breadcrumb, 'bc_stay' => $forrest->get('bc_stay'), 'chooser' => $chooser, 'agegroups' => $agegroups, 'deadline' => $deadline));
 }