diff --git a/module/file/model.php b/module/file/model.php index b8dd7575b9..511c27311d 100644 --- a/module/file/model.php +++ b/module/file/model.php @@ -272,4 +272,112 @@ class fileModel extends model return $data; } + + /** + * Parse CSV. + * + * @param string $fileName + * @access public + * @return array + */ + public function parseCSV($fileName) + { + $handle = fopen($this->session->importFile, 'r'); + $col = -1; + $row = 0; + $data = array(); + while(($line = fgets($handle)) !== false) + { + $line = trim($line); + $line = preg_replace_callback('/(\"{2,})(\,+)/', array($this, 'removeInterference'), $line); + $line = str_replace('""', '"', $line); + + /* if only one column then line is the data. */ + if(strpos($line, ',') === false and $col == -1) + { + $data[$row][0] = trim($line, '"'); + } + else + { + /* if col is not -1, then the data of column is not end. */ + if($col != -1) + { + $pos = strpos($line, '",'); + if($pos === false) + { + $data[$row][$col] .= "\n" . $line; + $data[$row][$col] = str_replace(',', ',', trim($data[$row][$col], '"')); + continue; + } + else + { + $data[$row][$col] .= "\n" . substr($line, 0, $pos + 1); + $data[$row][$col] = str_replace(',', ',', trim($data[$row][$col], '"')); + $line = substr($line, $pos + 2); + $col++; + } + } + + if($col == -1) $col = 0; + /* explode cols with delimiter. */ + while($line) + { + /* the cell has '"', the delimiter is '",'. */ + if($line{0} == '"') + { + $pos = strpos($line, '",'); + if($pos === false) + { + $data[$row][$col] = $line; + /* if end of cell is not '"', then the data of cell is not end. */ + if($line{strlen($line) - 1} != '"') continue 2; + $line = ''; + } + else + { + $data[$row][$col] = substr($line, 0, $pos + 1); + $line = substr($line, $pos + 2); + } + $data[$row][$col] = str_replace(',', ',', trim($data[$row][$col], '"')); + } + else + { + /* the delimiter default is ','. */ + $pos = strpos($line, ','); + /* if line is not delimiter, then line is the data of cell. */ + if($pos === false) + { + $data[$row][$col] = $line; + $line = ''; + } + else + { + $data[$row][$col] = substr($line, 0, $pos); + $line = substr($line, $pos + 1); + } + } + + $data[$row][$col] = str_replace(',', ',', trim($data[$row][$col], '"')); + $col++; + } + } + $row ++; + $col = -1; + } + fclose ($handle); + + return $data; + } + + /** + * Remove interference for parse csv. + * + * @param array $matchs + * @access private + * @return string + */ + private function removeInterference($matchs) + { + return $matchs[1] . str_replace(',', ',', $matchs[2]); + } } diff --git a/module/testcase/control.php b/module/testcase/control.php index 52dbbe1a30..225925bb27 100644 --- a/module/testcase/control.php +++ b/module/testcase/control.php @@ -791,6 +791,7 @@ class testcase extends control $this->testcase->createFromImport($productID); die(js::locate(inlink('browse', "productID=$productID"), 'parent')); } + $this->testcase->setMenu($this->products, $productID); $file = $this->session->importFile; @@ -806,13 +807,13 @@ class testcase extends control unset($fields[$key]); } - $csv = file_get_contents($file); - $header = substr($csv, 0, strpos($csv, "\n")); - $csv = substr($csv, strpos($csv, "\n") + 1); + $rows = $this->loadModel('file')->parseCSV($file); + $header = $rows[0]; + unset($rows[0]); - foreach(explode(',', $header) as $title) + foreach($header as $title) { - $field = array_search(trim(trim($title, '"')), $fields); + $field = array_search($title, $fields); if(!$field) continue; $columnKey[] = $field; } @@ -822,45 +823,32 @@ class testcase extends control die(js::locate(inlink('browse', "productID=$productID"))); } - $row = 1; $endField = $field; $caseData = array(); $stepData = array(); - while($csv) + foreach($rows as $row => $data) { $case = new stdclass(); - foreach($columnKey as $field) + foreach($columnKey as $key => $field) { - $delimiter = $field == $endField ? "\n" : ','; - $delimiter = $csv[0] == '"' ? '"' . $delimiter : $delimiter; - $pos = strpos($csv, $delimiter); - $cellValue = substr($csv, 0, $pos); - - if($cellValue and $cellValue[0] == '"')$cellValue = substr($cellValue, 1); - + $cellValue = $data[$key]; if($field == 'story') { + $case->$field = 0; if(strrpos($cellValue, '(#') !== false) { $id = trim(substr($cellValue, strrpos($cellValue,'(#') + 2), ')'); $case->$field = $id; } - else - { - $case->$field = 0; - } } elseif($field == 'module') { + $case->$field = 0; if(strrpos($cellValue, '(#') !== false) { $id = trim(substr($cellValue, strrpos($cellValue,'(#') + 2), ')'); $case->$field = $id; } - else - { - $case->$field = 0; - } } elseif(in_array($field, $caseConfig->export->listFields)) { @@ -884,6 +872,7 @@ class testcase extends control $steps = explode("\n", $cellValue); $stepKey = str_replace('step', '', strtolower($field)); $caseStep = array(); + foreach($steps as $step) { $step = trim($step); @@ -896,21 +885,33 @@ class testcase extends control $step = trim(substr($step, strpos($step, $sign) + $signbit)); if(!empty($step)) $caseStep[$num] = $step; } + elseif(isset($num)) + { + $caseStep[$num] .= "\n" . $step; + } else { - if(isset($num)) $caseStep[$num] .= "\n" . $step; + if($field == 'stepDesc') + { + $num = 1; + $caseStep[$num] = $step; + } + if($field == 'stepExpect' and isset($stepData[$row]['desc'])) + { + end($stepData[$row]['desc']); + $num = key($stepData[$row]['desc']); + $caseStep[$num] = $step; + } } } unset($num); unset($sign); $stepData[$row][$stepKey] = $caseStep; } - $csv = substr($csv, $pos + strlen($delimiter)); } $caseData[$row] = $case; unset($case); - $row++; } if(empty($caseData)) diff --git a/module/testcase/lang/en.php b/module/testcase/lang/en.php index 10cc2be30b..c7a7b78b88 100644 --- a/module/testcase/lang/en.php +++ b/module/testcase/lang/en.php @@ -150,3 +150,4 @@ $lang->testcase->buttonToList = 'Back'; $lang->testcase->errorEncode = 'No data, please select right encode and upload again!'; $lang->testcase->noFunction = 'Iconv and mb_convert_encoding does not exist, you can not turn the data into the desired coding!'; +$lang->testcase->noRequire = "In the row of %s, the %s is a required field"; diff --git a/module/testcase/lang/zh-cn.php b/module/testcase/lang/zh-cn.php index 0a07bcecec..fff0eda36d 100644 --- a/module/testcase/lang/zh-cn.php +++ b/module/testcase/lang/zh-cn.php @@ -150,3 +150,4 @@ $lang->testcase->buttonToList = '返回'; $lang->testcase->errorEncode = '无数据,请选择正确的编码重新上传!'; $lang->testcase->noFunction = '不存在iconv和mb_convert_encoding转码方法,不能将数据转成想要的编码!'; +$lang->testcase->noRequire = "%s行的“%s”是必填字段,不能为空"; diff --git a/module/testcase/model.php b/module/testcase/model.php index b2ed4d0202..1ce9d1bea8 100644 --- a/module/testcase/model.php +++ b/module/testcase/model.php @@ -419,9 +419,9 @@ class testcaseModel extends model $oldCases = $this->dao->select('*')->from(TABLE_CASE)->where('id')->in($_POST['id'])->fetchAll('id'); } + $cases = array(); foreach($this->post->product as $key => $product) { - dao::getError(); $caseData = new stdclass(); $caseData->product = $product; @@ -439,15 +439,18 @@ class testcaseModel extends model if(isset($this->config->testcase->create->requiredFields)) { $requiredFields = explode(',', $this->config->testcase->create->requiredFields); - $invalid = false; foreach($requiredFields as $requiredField) { $requiredField = trim($requiredField); - if(empty($caseData->$requiredField)) $invalid = true; + if(empty($caseData->$requiredField)) die(js::alert(sprintf($this->lang->testcase->noRequire, $key, $this->lang->testcase->$requiredField))); } - if($invalid) continue; } + $cases[$key] =$caseData; + } + + foreach($cases as $key => $caseData) + { if(!empty($_POST['id'][$key])) { $caseID = $this->post->id[$key]; diff --git a/module/testcase/view/exporttemplet.html.php b/module/testcase/view/exporttemplet.html.php index c6d652732f..2b298ea02a 100644 --- a/module/testcase/view/exporttemplet.html.php +++ b/module/testcase/view/exporttemplet.html.php @@ -37,7 +37,7 @@ function closeWindow() ?> - + charsets[$this->cookie->lang], 'utf-8', "class='form-control'");?> diff --git a/module/testcase/view/showimport.html.php b/module/testcase/view/showimport.html.php index c5f9df73f5..b547809c88 100644 --- a/module/testcase/view/showimport.html.php +++ b/module/testcase/view/showimport.html.php @@ -76,7 +76,7 @@