Пример #1
0
 /**
  * @Route("/lots/{id}/edit", name="lots_edit", requirements={"id": "\d+"})
  * @ParamConverter("lot", class="BankrotSiteBundle:Lot")
  * @Template()
  */
 public function editAction(Request $request, Lot $lot)
 {
     $form = $this->createForm('lot', $lot);
     if ($lot->getBeginDate() && $lot->getEndDate()) {
         $form->get('livePeriod')->setData(sprintf('%s - %s', $lot->getBeginDate()->format('d.m.Y'), $lot->getEndDate()->format('d.m.Y')));
     }
     if ($request->isMethod('POST')) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $livePeriod = $request->request->get('lot')['livePeriod'];
             $isValid = true;
             if (preg_match('/^[\\d\\.]+ - [\\d\\.]+$/', $livePeriod, $m)) {
                 $m[0] = explode('-', $m[0]);
                 try {
                     $lot->setBeginDate(new \DateTime($m[0][0]));
                     $lot->setEndDate(new \DateTime($m[0][1]));
                 } catch (\Exception $e) {
                     $form->get('livePeriod')->addError(new FormError('Неверный формат записи'));
                     $isValid = false;
                 }
             } else {
                 $lot->setBeginDate(null);
                 $lot->setEndDate(null);
             }
             $newDropRulePeriod = $request->request->get('lot')['newDropRulePeriod'];
             $newDropRulePeriodWork = $request->request->get('lot')['newDropRulePeriodWork'];
             $newDropRuleOrder = $request->request->get('lot')['newDropRuleOrder'];
             $newDropRuleOrderCurrent = $request->request->get('lot')['newDropRuleOrderCurrent'];
             $newDropRuleLivePeriod = $request->request->get('lot')['newDropRuleLivePeriod'];
             if ($newDropRulePeriod || $newDropRulePeriodWork) {
                 $newDropRule = new DropRule();
                 if ($newDropRulePeriod) {
                     $newDropRule->setPeriod($newDropRulePeriod);
                 }
                 if ($newDropRulePeriodWork) {
                     $newDropRule->setPeriodWork($newDropRulePeriodWork);
                 }
                 if ($newDropRuleOrder || $newDropRuleOrderCurrent) {
                     if ($newDropRuleOrder) {
                         $newDropRule->setOrder($newDropRuleOrder);
                     }
                     if ($newDropRuleOrderCurrent) {
                         $newDropRule->setOrderCurrent($newDropRuleOrderCurrent);
                     }
                     if (preg_match('/^[\\d\\.]+ - [\\d\\.]+$/', $newDropRuleLivePeriod, $m)) {
                         $m[0] = explode('-', $m[0]);
                         try {
                             $newDropRule->setBeginDate(new \DateTime($m[0][0]));
                             $newDropRule->setEndDate(new \DateTime($m[0][1]));
                         } catch (\Exception $e) {
                             $form->get('newDropRuleLivePeriod')->addError(new FormError('Неверный формат записи'));
                             $isValid = false;
                         }
                     }
                     $lot->addDropRule($newDropRule);
                 }
             }
             if ($isValid) {
                 $em->persist($lot);
                 $em->flush();
                 return $this->redirectToRoute('lots_edit', ['id' => $lot->getId()]);
             }
         }
     }
     return ['lot' => $lot, 'form' => $form->createView()];
 }
Пример #2
0
 private function loadSingle(ObjectManager $manager, Generator $lorem, array $categories, array $lotStatuses)
 {
     $lot = new Lot();
     $lot->setName($this->getRandomName($lorem));
     $lot->setCategory($categories[array_rand($categories)]);
     $lot->setLotStatus($lotStatuses[array_rand($lotStatuses)]);
     if (0 !== rand(0, 10)) {
         $lot->setOwner($this->getReference('user_user'));
         $lot->setUrl($this->getRandomUrl($lorem));
         $lot->setPhone($this->getRandomPhone());
         $lot->setPrice(rand(10000, 999999));
         $lot->setAddress($this->getRandomAddress($lorem));
         if (0 !== rand(0, 3)) {
             $lot->setDescription($this->getRandomDescription($lorem));
         }
         if (0 !== rand(0, 3)) {
             $lot->setInitialPrice($lot->getPrice() / 100 * rand(80, 90));
             switch (rand(0, 1)) {
                 case 0:
                     $lot->setCutOffPrice($lot->getInitialPrice() / 100 * rand(5, 10));
                     break;
                 case 1:
                     $lot->setCutOffPricePercent(rand(5, 10));
                     break;
             }
             switch (rand(0, 2)) {
                 case 0:
                     $lot->setDepositPrice($lot->getInitialPrice() / 100 * rand(10, 20));
                     break;
                 case 1:
                     $lot->setDepositPricePercent(rand(10, 20));
                     break;
                 case 2:
                     $lot->setDepositPricePercentCurrent(rand(10, 20));
                     break;
             }
             if (0 === rand(0, 3)) {
                 $lot->setBeginDate(new \DateTime('@' . rand(1420059600, 1441054800)));
                 $lot->setEndDate(new \DateTime('@' . ($lot->getBeginDate()->getTimestamp() + 86400 * rand(90, 240))));
             } else {
                 for ($i = 0, $l = rand(0, 2); $i < $l; $i++) {
                     $dr = new DropRule();
                     if (0 === rand(0, 1)) {
                         $dr->setPeriod(rand(3, 10));
                     } else {
                         $dr->setPeriodWork(rand(3, 10));
                     }
                     if (0 === rand(0, 1)) {
                         $dr->setOrder(rand(10, 5000) / 100);
                     } else {
                         $dr->setOrderCurrent(rand(10, 5000) / 100);
                     }
                     $dr->setBeginDate(new \DateTime('@' . rand(1420059600, 1441054800)));
                     $dr->setEndDate(new \DateTime('@' . ($dr->getBeginDate()->getTimestamp() + 86400 * rand(7, 30))));
                     $lot->addDropRule($dr);
                 }
             }
         }
     }
     $manager->persist($lot);
 }