/**
  * Display a form to edit a test session.
  *
  * @param sfWebRequest $request
  */
 public function executeEdit(sfWebRequest $request)
 {
     // Get project group id from project group name defined in app.yml
     $this->projectGroupId = Doctrine_Core::getTable("sfGuardGroup")->getProjectGroupId(sfConfig::get('app_project_group'));
     if ($this->projectGroupId == null) {
         throw new ErrorException("Unable to retrieve project group configuration! Application might need additional configuration or data!", 500);
     }
     // Get security level of current user
     $userSecurityLevel = $this->getUser()->isAuthenticated() ? $userSecurityLevel = $this->getUser()->getGuardUser()->getProfile()->getSecurityLevel() : 0;
     // Get list of all projects (Android, Tizen, Yocto, Linux, ...)
     $this->projects = Doctrine_Core::getTable("Project")->getBasicProjects($this->projectGroupId, $userSecurityLevel);
     // Set current project from URL parameters
     if ($request->getParameter("project")) {
         $this->currentProject = Doctrine_Core::getTable("Project")->getBasicProjectBySlug($request->getParameter("project"));
         $this->forward404Unless($this->currentProject != null, "This project does not exist anymore or you lack sufficient privileges!");
         $this->forward404Unless(in_array($this->currentProject, $this->projects), "This project is not accessible anymore or you lack sufficient privileges!");
     }
     // Get list of all products (Smartphone, Tablet, Netbook, Laptop, ...)
     $this->products = Doctrine_Core::getTable("Project")->getBasicProducts($this->projectGroupId, $this->currentProject["id"]);
     // Set current product
     if ($request->getParameter("product")) {
         $this->currentProduct = Doctrine_Core::getTable("ProductType")->getBasicProductBySlug($request->getParameter("product"));
         $this->forward404Unless($this->currentProduct != null, "This product type does not exist anymore or you lack sufficient privileges!");
         $this->forward404Unless(in_array($this->currentProduct, $this->products), "This product type is not accessible anymore or you lack sufficient privileges!");
     }
     // Set current environment
     if ($request->getParameter("environment")) {
         $this->currentEnvironment = Doctrine_Core::getTable("TestEnvironment")->getEnvironmentBySlug($request->getParameter("environment"));
         $this->forward404Unless($this->currentEnvironment != null, "This test environment type does not exist anymore or you lack sufficient privileges!");
     }
     // Set current image
     if ($request->getParameter("image")) {
         $this->currentImage = Doctrine_Core::getTable("Image")->getImageBySlug($request->getParameter("image"));
         $this->forward404Unless($this->currentImage != null, "This image does not exist anymore or you lack sufficient privileges!");
     }
     // Set submit button's label and cancel's route
     if (strpos(sfContext::getInstance()->getRouting()->getCurrentRouteName(), "finalize") === false) {
         $this->submitButton = "Save";
         $this->cancelRoute = "test_session";
     } else {
         $this->submitButton = "Finalize";
         $this->cancelRoute = "cancel_report";
         // Set prefill templates
         $prefill_templates = sfConfig::get('app_prefill_templates');
         if (!is_null($prefill_templates)) {
             foreach ($prefill_templates as $key => $template) {
                 // Remove "template" from "template_my_app_var"
                 $key = substr($key, 8);
                 // Camelize "_my_app_var" to "myAppVar"
                 $key = MiscUtils::camelize($key);
                 if (is_file(sfConfig::get('sf_upload_dir') . "/../templates/" . $template)) {
                     $content = file_get_contents(sfConfig::get('sf_upload_dir') . "/../templates/" . $template);
                     ${$key} = $content;
                 }
             }
         }
     }
     $this->currentRoute = sfContext::getInstance()->getRouting()->getCurrentRouteName();
     // Set current session
     if ($request->getParameter("id")) {
         $query = "SELECT * FROM " . sfConfig::get("app_table_qa_generic") . ".test_session ts WHERE ts.id = " . $request->getParameter("id") . "";
         /*
          * WORKAROUND To detect "finalize" in route's name because empty URL parameters are not
          * detected by hasParameter() method.
          */
         if (strpos(sfContext::getInstance()->getRouting()->getCurrentRouteName(), "finalize") === false) {
             $query .= " AND ts.published = 1";
         } else {
             $query .= " AND ts.published = 0";
         }
         $this->currentSession = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
         $this->forward404Unless(!empty($this->currentSession), "This test session is not accessible anymore or you lack sufficient privileges!");
     }
     // Get list of features with their numbers for current report
     $features = Doctrine_Core::getTable("TestSession")->getFeatures($this->currentSession['id']);
     // Add associative key to each feature
     $array = array();
     foreach ($features as $key => $value) {
         $array[MiscUtils::slugify($value["label"])] = $value;
     }
     $this->currentSession["features"] = $array;
     // Get all results (and add associative key for each result)
     $array = array();
     foreach (Doctrine_Core::getTable("TestSession")->getSessionResults($this->currentSession["id"]) as $key => $value) {
         $array[MiscUtils::slugify($value["name"] . $value["label"])] = $value;
     }
     $this->currentSession["results"] = $array;
     // Get all measures (and add associative key for each measure)
     $array = array();
     foreach (Doctrine_Core::getTable("TestSession")->getSessionMeasures($this->currentSession["id"]) as $key => $value) {
         $array[MiscUtils::slugify($value["name"] . $value["label"])] = $value;
     }
     $this->currentSession["measures"] = $array;
     // Get author and editor of the test session
     $query = "SELECT * FROM sf_guard_user u WHERE u.id = " . $this->currentSession["user_id"] . "";
     $this->author = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
     if ($this->currentSession["editor_id"]) {
         $query = "SELECT * FROM sf_guard_user u WHERE u.id = " . $this->currentSession["editor_id"] . "";
         $this->editor = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
     } else {
         $this->editor = $this->author;
     }
     // Get file attachments
     $this->attachments = Doctrine_Core::getTable("TestSession")->getFileAttachments($this->currentSession["id"], 2);
     $this->resultFiles = Doctrine_Core::getTable("TestSession")->getFileAttachments($this->currentSession["id"], 1);
     // Get numbers of result summary
     $this->currentSummaryNumbers = Doctrine_Core::getTable("TestSession")->getSessionNumbers($this->currentSession["id"]);
     $this->previousSummaryNumbers = Doctrine_Core::getTable("TestSession")->getSummaryNumbers($this->projectGroupId, $this->currentProject['id'], $this->currentProduct['id'], $this->currentEnvironment['id'], $this->currentImage['id'], $this->currentSession['created_at'], sfConfig::get("app_views_number_of_histograms_in_session", 3));
     $this->reversedPreviousSummaryNumbers = array_reverse($this->previousSummaryNumbers);
     $this->mandatoryBuildId = sfConfig::get('app_mandatory_build_id', false);
     $this->mandatoryTestset = sfConfig::get('app_mandatory_testset', false);
     // Initialize form
     $this->form = new SessionForm(array(), array("projectGroupId" => $this->projectGroupId, "securityLevel" => $userSecurityLevel, "projectId" => $this->currentProject["id"], "productId" => $this->currentProduct["id"], "session" => $this->currentSession, "environment" => $this->currentEnvironment, "image" => $this->currentImage, "mandatoryBuildId" => $this->mandatoryBuildId, "mandatoryTestset" => $this->mandatoryTestset));
     // Set prefilled templates
     if ($this->submitButton == "Finalize") {
         $this->form->setDefault("qa_summary", isset($qaSummary) ? $qaSummary : "");
         $this->form->setDefault("test_objective", isset($testObjective) ? $testObjective : "");
         $this->form->setDefault("environment_summary", isset($environmentSummary) ? $environmentSummary : "");
         $this->form->setDefault("issue_summary", isset($issueSummary) ? $issueSummary : "");
     }
     // Update list of products when submitting form (to match ajax selection)
     if (isset($_POST["test_session"])) {
         $project = Doctrine_Core::getTable("Project")->getBasicProjectById($_POST["test_session"]["project"]);
         $this->form = new SessionForm(array(), array("projectGroupId" => $this->projectGroupId, "securityLevel" => $userSecurityLevel, "projectId" => $project["id"], "productId" => $this->currentProduct["id"], "session" => $this->currentSession, "environment" => $this->currentEnvironment, "image" => $this->currentImage, "mandatoryBuildId" => $this->mandatoryBuildId, "mandatoryTestset" => $this->mandatoryTestset));
     }
     // Process form
     if ($request->isMethod("post")) {
         $this->processEdit($request, $this->form);
     }
 }