/**
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function addBranchSitesAction(Request $request)
 {
     $em = $this->getDoctrine()->getEntityManager();
     $site = $this->get('sudoux.cms.site')->getSite();
     $branches = $em->getRepository('SudouxMortgageBundle:Branch')->findBy(array('site' => $site, 'active' => true, 'deleted' => false));
     $siteCount = 0;
     foreach ($branches as $branch) {
         if (!$branch->hasSite()) {
             $newSite = new Site();
             $newSite->setParentSite($site);
             $newSite->setActive(true);
             $settings = new Settings();
             $newSite->setSettings($settings);
             $newSite->setName($branch->getName());
             $settings->setBranch($branch);
             $branch->setBranchSite($newSite);
             $em->persist($branch);
             $siteEmail = $branch->getEmail();
             if (isset($siteEmail)) {
                 $settings->setWebsiteEmail($siteEmail);
             }
             $siteType = $em->getRepository('SudouxCmsSiteBundle:SiteType')->findOneBy(array('key_name' => 'branch'));
             $newSite->setSiteType($siteType);
             $parentDomain = $site->getPrimaryDomain();
             $subdomain = str_replace(' ', '', strtolower($branch->getName()));
             $subdomain = preg_replace("/[^a-z ]/", '', $subdomain);
             $subdomain .= '.' . $parentDomain->getDomain();
             $em->persist($settings);
             $em->persist($newSite);
             $subdomain = $this->getAvailableDomain($subdomain);
             $domain = new Domain();
             $domain->setDomain($subdomain);
             $domain->setDescription(sprintf('Primary domain for %s', $newSite->getName()));
             $domain->setSite($newSite);
             $newSite->setPrimaryDomain($domain);
             $em->persist($domain);
             $siteCount++;
         }
     }
     $em->flush();
     $session = $request->getSession();
     $session->getFlashBag()->add('success', $siteCount . ' branch sites have been created.');
     return $this->redirect($this->generateUrl('sudoux_cms_admin_internal_tools'));
 }
예제 #2
0
 /**
  * @param LifecycleEventArgs $args
  */
 public function postPersist(LifecycleEventArgs $args)
 {
     $this->em = $args->getEntityManager();
     $entity = $args->getEntity();
     if ($entity instanceof LoanOfficer || $entity instanceof Branch) {
         $site = $entity->getSite();
         $autoCreateSites = $site->getSettings()->getInheritedAutoCreateSites();
         if ($autoCreateSites) {
             if ($entity instanceof LoanOfficer) {
                 if (!$entity->hasSite()) {
                     $newSite = new Site();
                     $newSite->setParentSite($site);
                     $newSite->setActive(true);
                     $settings = new Settings();
                     $newSite->setSettings($settings);
                     $newSite->setName($entity->getFullName());
                     $settings->setLoanOfficer($entity);
                     $entity->setOfficerSite($newSite);
                     $this->em->persist($entity);
                     $siteEmail = $entity->getEmail();
                     if (isset($siteEmail)) {
                         $settings->setWebsiteEmail($siteEmail);
                     }
                     $siteType = $this->em->getRepository('SudouxCmsSiteBundle:SiteType')->findOneBy(array('key_name' => 'loan_officer'));
                     $newSite->setSiteType($siteType);
                     $parentDomain = $site->getPrimaryDomain();
                     $subdomain = str_replace(' ', '', strtolower($entity->getFullName()));
                     $subdomain = preg_replace("/[^a-z ]/", '', $subdomain);
                     $subdomain .= '.' . $parentDomain->getDomain();
                     $this->em->persist($settings);
                     $this->em->persist($newSite);
                     $subdomain = $this->getAvailableDomain($subdomain);
                     $domain = new Domain();
                     $domain->setDomain($subdomain);
                     $domain->setDescription(sprintf('Primary domain for %s', $newSite->getName()));
                     $domain->setSite($newSite);
                     $this->em->persist($domain);
                     $newSite->setPrimaryDomain($domain);
                     $this->em->flush();
                 }
             }
             if ($entity instanceof Branch) {
                 if (!$entity->hasSite()) {
                     $newSite = new Site();
                     $newSite->setParentSite($site);
                     $newSite->setActive(true);
                     $settings = new Settings();
                     $newSite->setSettings($settings);
                     $newSite->setName($entity->getName());
                     $settings->setBranch($entity);
                     $entity->setBranchSite($newSite);
                     $this->em->persist($entity);
                     $siteEmail = $entity->getEmail();
                     if (isset($siteEmail)) {
                         $settings->setWebsiteEmail($siteEmail);
                     }
                     $siteType = $this->em->getRepository('SudouxCmsSiteBundle:SiteType')->findOneBy(array('key_name' => 'branch'));
                     $newSite->setSiteType($siteType);
                     $parentDomain = $site->getPrimaryDomain();
                     $subdomain = str_replace(' ', '', strtolower($entity->getName()));
                     $subdomain = preg_replace("/[^a-z ]/", '', $subdomain);
                     $subdomain .= '.' . $parentDomain->getDomain();
                     $this->em->persist($settings);
                     $this->em->persist($newSite);
                     $subdomain = $this->getAvailableDomain($subdomain);
                     $domain = new Domain();
                     $domain->setDomain($subdomain);
                     $domain->setDescription(sprintf('Primary domain for %s', $newSite->getName()));
                     $domain->setSite($newSite);
                     $this->em->persist($domain);
                     $newSite->setPrimaryDomain($domain);
                     $this->em->flush();
                 }
             }
         }
     }
 }
 /**
  * @param Request $request
  * @param $type
  * @param $id
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  * @throws \Symfony\Component\Form\Exception\AlreadyBoundException
  */
 public function addAction(Request $request, $type, $id)
 {
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     // check the site limit
     $siteLimitWarning = $this->container->getParameter('site_limit_warning');
     $customer = $site->getInheritedCustomer();
     if (isset($customer)) {
         if ($customer->getRemainingSiteCount() == 0) {
             $request->getSession()->getFlashBag()->add('error', $this->container->getParameter('site_limit_exceeded_message'));
             return $this->redirect($this->generateUrl('sudoux_cms_admin_site'));
         } else {
             if ($customer->getRemainingSiteCount() <= $siteLimitWarning) {
                 $request->getSession()->getFlashBag()->add('warning', sprintf('You have %s sites remaining.', $customer->getRemainingSiteCount()));
             }
         }
     }
     $newSite = new Site();
     $newSite->setParentSite($site);
     $newSite->setActive(true);
     $settings = new Settings();
     $newSite->setSettings($settings);
     if ($type == 'branch') {
         $branch = $em->getRepository('SudouxMortgageBundle:Branch')->findOneBy(array('id' => $id, 'site' => $site));
         if (isset($branch)) {
             $newSite->setName($branch->getName());
             $settings->setBranch($branch);
             $branch->setBranchSite($newSite);
             $em->persist($branch);
             //$subdomain = preg_replace("/[^a-z ]/", '', strtolower($branch->getName()));
             //$domain->setDomain($subdomain . '.' . $site->getPrimaryDomain()->getDomain());
         } else {
             throw $this->createNotFoundException('Branch not found');
         }
     } elseif ($type == 'loan_officer') {
         $loanOfficer = $em->getRepository('SudouxMortgageBundle:LoanOfficer')->findOneBy(array('id' => $id, 'site' => $site));
         if (isset($loanOfficer)) {
             $newSite->setName($loanOfficer->getFullName());
             $settings->setLoanOfficer($loanOfficer);
             $loanOfficer->setOfficerSite($newSite);
             $em->persist($loanOfficer);
         } else {
             throw $this->createNotFoundException('Loan Officer not found');
         }
     } else {
         throw new AccessDeniedException();
     }
     $siteType = $em->getRepository('SudouxCmsSiteBundle:SiteType')->findOneBy(array('key_name' => $type));
     $newSite->setSiteType($siteType);
     $form = $this->createForm(new SiteType($site), $newSite);
     if ($request->getMethod() == 'POST') {
         $form->bind($request);
         $domain = $form['primary_domain']->getData();
         if (isset($domain)) {
             $violations = $this->container->get('validator')->validate($domain);
             foreach ($violations as $violation) {
                 $form['primary_domain']->get('domain')->addError(new FormError($violation->getMessage()));
             }
         }
         if ($form->isValid()) {
             $em->persist($settings);
             $domain = $newSite->getPrimaryDomain();
             $domain->setDescription(sprintf('Primary domain for %s', $newSite->getName()));
             $domain->setSite($newSite);
             $em->persist($domain);
             //$em->persist($settings);
             $em->persist($newSite);
             $em->flush();
             $configure = $form['configure']->getData();
             if ($configure) {
                 return $this->redirect(sprintf('http://%s/admin/site/settings/edit/general', $newSite->getPrimaryDomain()->getDomain()));
             } else {
                 return $this->redirect($this->generateUrl('sudoux_cms_admin_site'));
             }
         }
     }
     return $this->render('SudouxMortgageBundle:SiteAdmin:add.html.twig', array('form' => $form->createView(), 'type' => $type, 'id' => $id));
 }