コード例 #1
0
 public function storeSystemAction(Request $request)
 {
     $this->assertUserRights(UserRole::ROLE_ADMIN);
     $system = $request->get('system');
     try {
         if (array_key_exists("id", $system) && $system['id'] != 'new') {
             $systemObject = $this->getDoctrine()->getRepository('KoalamonIncidentDashboardBundle:System')->find($system["id"]);
         } else {
             $systemObject = new System();
             $systemObject->setProject($this->getProject());
         }
         if (array_key_exists('identifier', $system)) {
             $existingSystem = $this->getDoctrine()->getRepository('KoalamonIncidentDashboardBundle:System')->findOneBy(['identifier' => $system["identifier"], 'project' => $this->getProject()]);
             if ($existingSystem) {
                 if (!array_key_exists('id', $system) || $existingSystem->getId() != $system['id']) {
                     throw new \RuntimeException('The system identifier already exists.');
                 }
             }
         }
         if (array_key_exists("parent", $system)) {
             $parent = $this->getDoctrine()->getRepository('KoalamonIncidentDashboardBundle:System')->find($system["parent"]);
             $systemObject->setParent($parent);
         }
         if (array_key_exists("identifier", $system) && $system["identifier"] != "") {
             $systemObject->setIdentifier($system["identifier"]);
         } else {
             if (!array_key_exists("parent", $system)) {
                 throw new \RuntimeException('The parameter "identifier" is required');
             }
         }
         if ($system["url"] != "") {
             if (!filter_var($system['url'], FILTER_VALIDATE_URL) === false) {
                 $systemObject->setUrl($system["url"]);
             } else {
                 throw new \RuntimeException('The parameter "URL" requires a valid URL');
             }
         }
         if ($system["size"] != "") {
             $systemObject->setSize($system['size']);
         }
         // system in leankoala ------------------------------
         if (array_key_exists('domain', $system) && $system['domain'] != "") {
             if (strpos($system['domain'], '/') !== false) {
                 throw new \RuntimeException('The given url must specify a domain not an path.');
             }
             $url = $system['schema'] . '://' . $system['domain'];
             if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
                 $subDomain = substr($system['domain'], 0, self::strrposCount($system['domain'], '.', 2));
                 if ($subDomain == "") {
                     $delta = 0;
                 } else {
                     $delta = 1;
                 }
                 $domain = substr($system['domain'], self::strrposCount($system['domain'], '.', 2) + $delta);
                 $systemObject->setDomain($domain);
                 $systemObject->setSubDomain($subDomain);
                 $systemObject->setSchema($system['schema']);
                 $systemObject->setUrl("");
             } else {
                 throw new \RuntimeException('The parameter "URL" requires a valid URL');
             }
         }
         // ---------------------------------------------------
         if (array_key_exists('alias', $system) && $system['alias'] != "") {
             $aliasSystem = $this->getDoctrine()->getRepository('KoalamonIncidentDashboardBundle:System')->find($system["alias"]);
             $systemObject->setAliasFor($aliasSystem);
         }
         if ($system["name"] != "") {
             $systemObject->setName($system["name"]);
         } else {
             $systemObject->setName($system['url']);
         }
         if ($system["basicAuthUser"] != "") {
             $systemObject->setBasicAuthUser($system["basicAuthUser"]);
             if ($system["basicAuthPassword"] != "") {
                 $systemObject->setBasicAuthPassword($system["basicAuthPassword"]);
             } else {
                 throw new \RuntimeException('If the basic authentication user name is set there must be a password');
             }
         } else {
             $systemObject->setBasicAuthPassword('');
             $systemObject->setBasicAuthUser('');
         }
         if ($system["description"] != "") {
             $systemObject->setDescription($system["description"]);
         } else {
             $systemObject->setDescription(null);
         }
         $em = $this->getDoctrine()->getManager();
         $em->persist($systemObject);
         $em->flush();
         // @todo event dispatcher
         if ($systemObject->getIdentifier() == 'new') {
             $systemObject->setIdentifier($systemObject->getId());
             $em->persist($systemObject);
             $em->flush();
         }
     } catch (\Exception $e) {
         if ($request->isXmlHttpRequest()) {
             return $this->getJsonResponse('failure', $e->getMessage(), (int) $system['elementId']);
         } else {
             $this->addFlash('notice', $e->getMessage());
             return $this->redirectToRoute('koalamon_default_system_edit', ['system' => $system['id']]);
         }
     }
     if ($request->isXmlHttpRequest()) {
         return $this->getJsonResponse('success', 'System "' . $systemObject->getName() . '" successfully saved.', (int) $system['elementId'], $systemObject->getId());
     } else {
         $this->addFlash('success', 'System successfully stored.');
         if (array_key_exists("id", $system) && $system['id'] == 'new') {
             return $this->redirectToRoute('koalamon_default_admin_system_subsystems', ['system' => $systemObject->getId()]);
         } else {
             return $this->redirectToRoute('koalamon_default_system_admin');
         }
     }
 }