/**
  * 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 actionIndex()
 {
     ini_set('memory_limit', '650M');
     set_time_limit(0);
     $tyresForm = new ImportForm();
     $result = array();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['ImportForm'])) {
         $tyresForm->attributes = $_POST['ImportForm'];
         if ($tyresForm->validate()) {
             # Вот тут начинается импорт
             /* вторая страница
              * 1. CAI
              * 2. Профиль(мм)
              * 3. Высота (%)
              * 4. Диаметр (,R)
              * 5. Индекс
              * 6. Модель
              * 7. Производитель
              * 8. Сезон
              * 9. Кол-во (ОПТ)
              * 10. Цена (ОПТ)
              * 11. Кол-во (ИМ)
              * 12. Цена (ИМ) 
             */
             TyreSizes::model()->updateAll(array('rest' => 0, 'price' => 0));
             $uploaded = Yii::app()->file->set('ImportForm[file]');
             Yii::app()->file->set(Yii::getPathOfAlias('webroot.files.' . $this->id . '.' . $this->action->id))->createDir();
             $newfile = $uploaded->copy(strtolower(Yii::getPathOfAlias('webroot.files.' . $this->id . '.' . $this->action->id)) . '/' . $uploaded->basename);
             $newfile->filename = $newfile->filename . '.' . date('YmdHis');
             Yii::import('ext.phpexcelreader.EPhpExcelReader');
             $data = new EPhpExcelReader($newfile->realpath, false);
             $sheet = 0;
             $rowcount = $data->rowcount($sheet);
             $result['rowcount'] = $rowcount;
             $result['count'] = 0;
             #$colcount = $data->colcount();
             #$rowcount = 5;
             $r = 2;
             while ($r <= $rowcount) {
                 $cai = $data->val($r, 1, $sheet);
                 if ($size = TyreSizes::model()->find('code=:code', array(':code' => $cai))) {
                     $size->price = $data->val($r, 12, $sheet);
                     if (!empty($tyresForm->margin)) {
                         $size->price += $size->price * ($tyresForm->margin / 100);
                     }
                     $rest11 = $data->val($r, 11, $sheet);
                     $rest9 = $data->val($r, 9, $sheet);
                     if (is_int($rest11) && is_int($rest9)) {
                         if ($rest11 > $rest9) {
                             $size->rest = $rest11;
                         } else {
                             $size->rest = $rest9;
                         }
                     } elseif (!is_int($rest11) && is_int($rest9)) {
                         $size->rest = 10;
                     } elseif (is_int($rest11) && !is_int($rest9)) {
                         $size->rest = 20;
                     } else {
                         $size->rest = 20;
                     }
                     if (!$size->save()) {
                         $result['errors']['size:' . $data->val($r, 1, $sheet)] = $size->errors;
                     }
                     $result['count']++;
                 } else {
                     echo '<p>' . $cai . ' ' . $data->val($r, 7, $sheet) . ' ' . $data->val($r, 6, $sheet) . ' not found</p>';
                 }
                 $r++;
             }
             #$this->redirect(array('view','id'=>$model->id));
         }
     }
     $this->render('index', array('model' => $tyresForm, 'results' => $result));
 }
 public function actionIndex()
 {
     ini_set('memory_limit', '650M');
     set_time_limit(0);
     $form = new ImportForm();
     $result = array();
     if (isset($_POST['ImportForm'])) {
         $form->attributes = $_POST['ImportForm'];
         if ($form->validate()) {
             # Вот тут начинается импорт
             /* Первая страница
              * 1. CAI
              * 2. J
              * 3. R
              * 4. /
              * 5. X
              * 6. ET
              * 7. Ø
              * 8. Модель
              * 9. Производитель
              * 10.Кол-во (ОПТ)
              * 11.Цена (ОПТ)
              * 12.Кол-во (ИМ)
              * 13.Цена (ИМ)
             */
             DiskSizes::model()->updateAll(array('rest' => 0, 'price' => 0));
             $sheet = 1;
             $uploaded = Yii::app()->file->set('ImportForm[file]');
             Yii::app()->file->set(Yii::getPathOfAlias('webroot.files.' . $this->id . '.' . $this->action->id))->createDir();
             $newfile = $uploaded->copy(strtolower(Yii::getPathOfAlias('webroot.files.' . $this->id . '.' . $this->action->id)) . '/' . $uploaded->basename);
             $newfile->filename = $newfile->filename . '.' . date('YmdHis');
             Yii::import('ext.phpexcelreader.EPhpExcelReader');
             $data = new EPhpExcelReader($newfile->realpath, false);
             $rowcount = $data->rowcount($sheet);
             $result['rowcount'] = $rowcount;
             $result['count'] = 0;
             #$colcount = $data->colcount();
             #$rowcount = 5;
             $r = 2;
             while ($r <= $rowcount) {
                 $cai = $data->val($r, 1, $sheet);
                 if ($size = DiskSizes::model()->find('code=:code', array(':code' => $cai))) {
                     $size->price = $data->val($r, 13, $sheet);
                     if (!empty($form->margin)) {
                         $size->price += $size->price * ($form->margin / 100);
                     }
                     $rest10 = $data->val($r, 10, $sheet);
                     $rest12 = $data->val($r, 12, $sheet);
                     if (is_int($rest12) && is_int($rest10)) {
                         if ($rest12 > $rest10) {
                             $size->rest = $rest12;
                         } else {
                             $size->rest = $rest10;
                         }
                     } elseif (!is_int($rest12) && is_int($rest10)) {
                         $size->rest = 10;
                     } elseif (is_int($rest12) && !is_int($rest10)) {
                         $size->rest = 20;
                     } else {
                         $size->rest = 20;
                     }
                     if (!$size->save()) {
                         $result['errors']['size:' . $data->val($r, 1, $sheet)] = $size->errors;
                     }
                     $result['count']++;
                 } else {
                     echo '<p>' . $cai . ' ' . $data->val($r, 9, $sheet) . ' ' . $data->val($r, 8, $sheet) . ' not found</p>';
                 }
                 $r++;
             }
             #$this->redirect(array('view','id'=>$model->id));
         }
     }
     $this->render('index', array('model' => $form, 'results' => $result));
 }
Beispiel #4
0
 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;
 }
    public function actionImport()
    {
        $model = new ImportForm();
        if (isset($_GET['examplefile'])) {
            $product = new Product();
            $importFields = $product->attributeLabels();
            $value = new ProductValue();
            foreach ($value->getFields() as $field) {
                $importFields[$field->varname] = $field->title;
            }
            if ($_GET['examplefile'] == 'xml') {
                //header('Content-Disposition: attachment; filename="examplefile.xml"');
            } else {
                header('Content-Disposition: attachment; filename="examplefile.csv"');
                $content = implode(';', array_values($importFields)) . "\n";
                $content .= implode(';', array_keys($importFields)) . "\n";
                if (isset(Yii::app()->getModule('cart')->importCSVCharset) && Yii::app()->getModule('cart')->importCSVCharset != 'UTF-8') {
                    $content = iconv("UTF-8", Yii::app()->getModule('cart')->importCSVCharset, $content);
                }
                echo $content;
            }
            Yii::app()->end();
        }
        if (isset($_POST['ajax'])) {
            $session = Yii::app()->session[self::IMPORT_SESSION_NAME];
            if ($_POST['line'] < $session['count']) {
                // Import product
                //echo '<pre>'; print_r(array($session,$_POST)); die();
                $content = explode("\n", file_get_contents($session['filename']));
                if (isset($content[$_POST['line']])) {
                    echo CJSON::encode($this->productImport(CJSON::decode($content[$_POST['line']])));
                } else {
                    echo CJSON::encode(array('message' => $session['filename'] . ' - Error data line ' . $_POST['line']));
                }
            } else {
                echo CJSON::encode(array('message' => 'Finished!'));
            }
            //sleep(1);
            Yii::app()->end();
            //*/
        }
        if (isset($_POST['ImportForm'])) {
            $model->attributes = $_POST['ImportForm'];
            if ($model->validate()) {
                $model->importFile = CUploadedFile::getInstance($model, 'importFile');
                $content = file_get_contents($model->importFile->tempName);
                if (isset(Yii::app()->getModule('cart')->importCSVCharset) && Yii::app()->getModule('cart')->importCSVCharset != 'UTF-8') {
                    $content = iconv(Yii::app()->getModule('cart')->importCSVCharset, "UTF-8", $content);
                }
                $fields = array();
                $array = array();
                $content = explode("\n", $content);
                $fields = explode(';', $content[1]);
                foreach ($content as $i => $line) {
                    $line = trim($line);
                    if ($line && $i > 1) {
                        $line = explode(';', $line);
                        $item = array();
                        foreach ($fields as $n => $field) {
                            $item[trim($field)] = trim($line[$n]);
                        }
                        if ($item) {
                            $item = CJSON::encode($item);
                            array_push($array, $item);
                        }
                    }
                }
                $session = array('line' => 0, 'count' => count($array), 'filename' => Yii::app()->getModule('cart')->importPathPhotos . '/yii-cart-import_' . date('Y-m-d_H-i-s') . '.import');
                if ($fh = fopen($session['filename'], 'w+')) {
                    fwrite($fh, implode("\n", $array));
                    fclose($fh);
                    Yii::app()->session[self::IMPORT_SESSION_NAME] = $session;
                    Yii::app()->user->setFlash('importMessage', CartModule::t('Import {count} products', 'import', array('{count}' => count($array))));
                    $cs = Yii::app()->getClientScript();
                    $cs->registerCoreScript('jquery');
                    $css = '
						/* progress bar */
						#progressbar {
							margin: 10px 0;
							padding:4px;
							border:1px solid #6FACCF;
							position:relative;
							-moz-border-radius: 5px;-webkit-border-radius:5px;
						}
						#progressbar #percent {
							position:absolute;
							left:0;
							right:0;
							text-align:center;
							color:#0066A4;
							font-weight:bold;
						}
						#progressbar #progress {
							background: #EFFDFF;
							border:1px solid #B7D6E7;
							margin:-1px;
							-moz-border-radius: 3px;-webkit-border-radius:3px;
						}
						#log {
							position:relative;
							overflow: auto;
							max-height: 300px;
						}
						#log .logline {
							position:absolute;
							padding: 5px;
							border: 1px solid #fff;
							-moz-border-radius: 5px;-webkit-border-radius:5px;
						}
						#log .logline:hover {
							border: 1px solid #B7D6E7
						}
						#log .tohide {
							color:#298DCD;/*#B7D6E7*/
						}
						#log .error {color:#f00;}
						#log .addnew {color:#298DCD;}
						#log span.warning {color:#f00;}
						#log span.note {color:#ff9900;}
					';
                    $js = '
						$(document).ready(function(){
							$("#showlog").click(function(){
								$("#log div.logline").attr("style","position:relative;display:none;").show(500);
								$(this).hide();
								$("#hidelog").show();
							});
							$("#hidelog").click(function(){
								$("#log div.logline").hide(500);
								$(this).hide();
								$("#showlog").show();
							});
							
			    			var i = 0;
			    			var size = ' . count($array) . ';
			    			
			    			getData();
			    			
			    			function getData() {
			    				var AjaxError = false;
			    				$.ajax({
			    					async: false,
			    					type: \'POST\',
			    					data: ({ajax : 1,line: i}),
			    					dataType:\'json\',
			    					url:\'' . $this->createUrl('product/import') . '\',
			    					success: function(data) {
			    						if (data!=null) {
		    								addLogLine(data.message,data.error,data.warning,data.note,data.new);
				    						i++;
				    						setProgress(i,size);
				    					} else {
			    							addLogLine("{url: ' . $this->createUrl('product/import') . ', data: {ajax : 1,line: "+i+"}, return: null}");
			    							addLogLine("' . CartModule::t('Import error!\\nPlease contact your administrator!', 'import') . '");
				    						AjaxError = true;
										}
									},
									error: function() {
		    							addLogLine("{url: ' . $this->createUrl('product/import') . ', data: {ajax : 1,line: "+i+"}}");
		    							addLogLine("' . CartModule::t('Import error!\\nPlease contact your administrator!', 'import') . '");
					    				AjaxError = true;
									},
								});
								if (AjaxError) {
									alert("' . CartModule::t('Import error!\\nPlease contact your administrator!', 'import') . '");
				    				$("#showlog").show();
								} else {
				    				if (i<size) {
				    					' . (Yii::app()->getModule('cart')->importAjaxSleep ? 'sleep(' . Yii::app()->getModule('cart')->importAjaxSleep . ');' : '') . '
				    					getData();
				    				} else {
				    					$("#showlog").show();
									}
								}
			    			}
			    			
			    			function setProgress(n,c) {
			    				var p = parseInt(100/c*n);
			    				$("#percent").text(p+" %");
			    				//$("#progress").animate({width: p+"%"});
			    				$("#progress").width(p+"%");
			    			}
			    			
			    			function addLogLine(line,er,wr,nt,nw) {
			    				$("#log div.last").removeClass("last").addClass("logline").hide();
			    				$("#log").append("<div class=\\"last "+((nw)?"addnew":"update")+((er)?" error":"")+"\\">"+((wr)?"<span class=\\"warning\\">"+wr+"</span><br/>":"")+((nt)?"<span class=\\"note\\">"+nt+"</span><br/>":"")+line+"</div>");
			    			}
			    			function sleep(milliseconds) {
							  var start = new Date().getTime();
							  for (var i = 0; i < 1e7; i++) {
							    if ((new Date().getTime() - start) > milliseconds){
							      break;
							    }
							  }
							}	    			
						});
					';
                    $cs->registerCss(__CLASS__ . '#form', $css);
                    $cs->registerScript(__CLASS__ . '#form', $js);
                } else {
                    Yii::app()->user->setFlash('importError', CartModule::t('Failed to create file: {filename}', 'import', array('{filename}' => $session['filename'])));
                }
            }
        }
        $this->render('importform', array('model' => $model));
    }