/** * Process the form to add a new test session. * * @param sfWebRequest $request * @param ImportForm $form */ protected function processAdd(sfWebRequest $request, ImportForm $form) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { // Get sent values and uploaded files $values = $form->getValues(); $files = $request->getFiles(); // Retrieve values from form $projectGroupId = $values["project_group_id"]; $projectId = $values["project"]; $productId = $values["product"]; $date = $values["date"] . " " . date("H:i:s"); $buildId = $values["build_id"]; $testType = $values["testset"]; $title = $values["name"]; $environmentForm = $form->getValue("environmentForm"); $imageForm = $form->getValue("imageForm"); $userId = $this->getUser()->getGuardUser()->getId(); $buildSlug = MiscUtils::slugify($buildId); $testTypeSlug = MiscUtils::slugify($testType); // Customize database connection to begin a transactionnal query $conn = Doctrine_Manager::getInstance()->getConnection("qa_generic"); $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, FALSE); $conn->beginTransaction(); $project = Doctrine_Core::getTable("Project")->getBasicProjectById($projectId); $product = Doctrine_Core::getTable("ProductType")->getBasicProductById($productId); // Create a new relationship between project group, project and product if needed $projectToProductId = Doctrine_Core::getTable("ProjectToProduct")->getProjectToProductId($projectGroupId, $projectId, $productId); if ($projectToProductId == null) { $projectToProduct = new ProjectToProduct(); $projectToProduct->setProjectGroupId($projectGroupId); $projectToProduct->setProjectId($projectId); $projectToProduct->setProductId($productId); $projectToProduct->save($conn); $projectToProductId = $projectToProduct->getId(); } // Create a new environment if needed $environment = Doctrine_Core::getTable("TestEnvironment")->findByArray($environmentForm); if ($environment == null) { // Add new environment $environment = new TestEnvironment(); $environment->setName($environmentForm["name"]); $environment->setDescription($environmentForm["description"]); $environment->setCpu($environmentForm["cpu"]); $environment->setBoard($environmentForm["board"]); $environment->setGpu($environmentForm["gpu"]); $environment->setOtherHardware($environmentForm["other_hardware"]); // Check if its slug does not already exist and generate a new one if needed $slug = MiscUtils::slugify($environmentForm["name"]); $size = 1; while (Doctrine_Core::getTable("TestEnvironment")->checkSlug($slug)) { $slug = MiscUtils::slugify($environmentForm["name"]) . substr(microtime(), -$size); $size++; } $environment->setNameSlug($slug); $environment->save($conn); // Convert object into associative array $environment = $environment->toArray(); } // Create a new image if needed $image = Doctrine_Core::getTable("Image")->findByArray($imageForm); if ($image == null) { // Add new image $image = new Image(); $image->setName($imageForm["name"]); $image->setDescription($imageForm["description"]); $image->setOs($imageForm["os"]); $image->setDistribution($imageForm["distribution"]); $image->setVersion($imageForm["version"]); $image->setKernel($imageForm["kernel"]); $image->setArchitecture($imageForm["architecture"]); $image->setOtherFw($imageForm["other_fw"]); $image->setBinaryLink($imageForm["binary_link"]); $image->setSourceLink($imageForm["source_link"]); // Check if its slug does not already exist and generate a new one if needed $slug = MiscUtils::slugify($imageForm["name"]); $size = 1; while (Doctrine_Core::getTable("Image")->checkSlug($slug)) { $slug = MiscUtils::slugify($imageForm["name"]) . substr(microtime(), -$size); $size++; } $image->setNameSlug(MiscUtils::slugify($slug)); $image->save($conn); // Convert object into associative array $image = $image->toArray(); } // Create a new configuration relationship if needed $configurationId = Doctrine_Core::getTable("Configuration")->getConfigurationId($projectToProductId, $environment["id"], $image["id"]); if ($configurationId == null) { $configuration = new Configuration(); $configuration->setProjectToProductId($projectToProductId); $configuration->setTestEnvironmentId($environment["id"]); $configuration->setImageId($image["id"]); $configuration->save($conn); $configurationId = $configuration->getId(); } // Add the new session into DB $testSession = new TestSession(); if (empty($title)) { $title = $product["name"] . " Test Report: " . $environment["name"] . " " . $image["name"] . " " . substr($date, 0, -3); if (!empty($buildId)) { $title .= " Build ID: " . $buildId; } } $testSession->setBuildId($buildId); $testSession->setTestset($testType); $testSession->setName($title); $testSession->setUserId($userId); $testSession->setCreatedAt($date); $testSession->setUpdatedAt($date); $testSession->setStatus(2); $testSession->setPublished(0); $testSession->setConfigurationId($configurationId); $testSession->setBuildSlug($buildSlug); $testSession->setTestsetSlug($testTypeSlug); $testSession->save($conn); $testSessionId = $testSession->getId(); $tableName = Doctrine_Core::getTable("TableName")->findOneByName("test_session"); $tableNameId = $tableName->getId(); // Concatenate directory path $dir_path = sfConfig::get('sf_upload_dir') . "/testsession_" . $testSessionId; // Upload attachments and result files foreach ($files["upload"] as $key => $file) { $fileName = $file['name']; $fileSize = $file['size']; $fileType = $file['type']; $fileError = $file['error']; $fileChecksum = sha1_file($file["tmp_name"]); // Check file error and file size if (!$fileError and $fileSize <= sfConfig::get('app_max_file_size', '10000000')) { if (!is_dir($dir_path)) { mkdir($dir_path, 0777, true); } $dest_path = $dir_path . "/" . $fileName; // Move file to uploads directory move_uploaded_file($file['tmp_name'], $dest_path); // If it is an XML file, parse it and fill qa_generic database if (preg_match("#\\.xml *\$#i", $fileName) || preg_match("#\\.csv *\$#i", $fileName)) { // Fill qa_generic database if ($err_code = Import::file($dest_path, $testSessionId, $configurationId, $conn)) { $error_message = Import::getImportErrorMessage($err_code); MiscUtils::deleteDir($dir_path); $conn->rollback(); $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE); $this->getUser()->setFlash("error", "Invalid file content on " . $fileName . " : " . $error_message); $this->redirect("add_report", array()); } } else { MiscUtils::deleteDir($dir_path); $conn->rollback(); $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE); $this->getUser()->setFlash("error", "Invalid file format : only XML and CSV format are supported"); $this->redirect("add_report", array()); } $web_path = "/uploads" . "/testsession_" . $testSessionId . "/" . $fileName; $fileAttachment = new FileAttachment(); $fileAttachment->setName($fileName); $fileAttachment->setUserId($userId); $fileAttachment->setUploadedAt(date("Y-m-d H:i:s")); $fileAttachment->setFilename($fileName); $fileAttachment->setFileSize($fileSize); $fileAttachment->setFileMimeType($fileType); $fileAttachment->setLink($web_path); $fileAttachment->setChecksum($fileChecksum); $fileAttachment->setTableNameId($tableNameId); $fileAttachment->setTableEntryId($testSessionId); $fileAttachment->setCategory(1); $fileAttachment->save($conn); } else { MiscUtils::deleteDir($dir_path); $conn->rollback(); $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE); $this->getUser()->setFlash("error", "File size limit reached"); $this->redirect("add_report", array()); } } $conn->commit(); $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE); $this->redirect("finalize_report", array("project" => $project["name_slug"], "product" => $product["name_slug"], "environment" => $environment["name_slug"], "image" => $image["name_slug"], "id" => $testSessionId)); } }
public function executeImport(sfWebRequest $request) { $form = new ImportForm(); if ($request->isMethod(sfRequest::POST)) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $file = $form->getValue('file'); $fileText = file_get_contents($file->getTempName()); unlink($file->getTempName()); $fileStrings = explode("\r\n", $fileText); $OZIdata = explode(' ', $fileStrings[0]); $version = array_pop($OZIdata); $type = ucfirst(strtolower($OZIdata[1])); $OZItypes = array('Waypoint', 'Track'); if (in_array($type, $OZItypes)) { $method = 'parse' . $type; $this->locations = OZIConverter::$method($fileStrings, $version); $this->setTemplate('import' . $type); } } } $this->form = $form; }