Пример #1
0
 /**
  * @Route("/client/{idClient}/job/{idJob}", requirements={"idClient" = "\d+", "idJob" = "\d+"}, defaults={"idJob" = "-1"}, name="saveJob")
  * @Method("POST")
  * @Template()
  */
 public function saveJobAction(Request $request, $idClient, $idJob)
 {
     $t = $this->get('translator');
     if ("-1" === $idJob) {
         $job = new Job();
         $client = $this->getDoctrine()->getRepository('BinovoElkarBackupBundle:Client')->find($idClient);
         if (null == $client) {
             throw $this->createNotFoundException($t->trans('Unable to find Client entity:', array(), 'BinovoElkarBackup') . $idClient);
         }
         $job->setClient($client);
     } else {
         $repository = $this->getDoctrine()->getRepository('BinovoElkarBackupBundle:Job');
         $job = $repository->find($idJob);
     }
     $storedOwner = $job->getOwner();
     $form = $this->createForm(new JobType(), $job, array('translator' => $t));
     $form->bind($request);
     if ($form->isValid()) {
         $job = $form->getData();
         if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
             // only allow chown to admin
             $job->setOwner($storedOwner);
         }
         if ($job->getOwner() == null) {
             $job->setOwner($this->get('security.context')->getToken()->getUser());
         }
         try {
             $em = $this->getDoctrine()->getManager();
             $em->persist($job);
             $this->info('Save client %clientid%, job %jobid%', array('%clientid%' => $job->getClient()->getId(), '%jobid%' => $job->getId()), array('link' => $this->generateJobRoute($job->getId(), $job->getClient()->getId())));
             $em->flush();
         } catch (Exception $e) {
             $this->get('session')->getFlashBag()->add('job', $t->trans('Unable to save your changes: %extrainfo%', array('%extrainfo%' => $e->getMessage()), 'BinovoElkarBackup'));
         }
         return $this->redirect($this->generateUrl('showClients'));
     } else {
         return $this->render('BinovoElkarBackupBundle:Default:job.html.twig', array('form' => $form->createView()));
     }
 }