/**
  * @param Request      $request
  * @param Congregation $congregation
  *
  * @return array
  *
  * @Template()
  */
 public function newAction(Request $request, Congregation $congregation)
 {
     $territory = new Territory();
     $territory->setCongregation($congregation);
     $form = $this->createForm('kingdomhall_form_territory', $territory, array('action' => $this->generateUrl('kingdom_hall_territories_new', array('congregationCode' => $congregation->getCode()))));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $manager = $this->getDoctrine()->getManager();
         $manager->persist($territory);
         $manager->flush();
     }
     return array('form' => $form->createView());
 }
 /**
  * @Delete("/api/congregations/{congregation}/territories/{territory}/histories")
  *
  * @param Congregation $congregation
  * @ParamConverter(name="congregation", class="KingdomHallDataBundle:Congregation")
  *
  * @param Territory $territory
  * @ParamConverter(name="territory", class="KingdomHallDataBundle:Territory")
  *
  * @param ParamFetcher $fetcher
  * @QueryParam("ids")
  *
  * @return string
  */
 public function deleteCongregationTerritoryHistoriesAction(Congregation $congregation, Territory $territory, ParamFetcher $fetcher)
 {
     $manager = $this->getDoctrine()->getManager();
     $histories = $territory->getHistories();
     foreach (explode(',', $fetcher->get('ids')) as $id) {
         $history = $histories->get($id);
         if ($history) {
             $manager->remove($history);
         }
     }
     $manager->flush();
     return new Response('', 204);
 }