Example #1
0
 /**
  * Shows the import page for this table.
  */
 public function actionImport()
 {
     $importPage = new ImportPage();
     $importPage->db = $this->db;
     $importPage->table = $this->table;
     $importPage->schema = $this->schema;
     $importPage->formTarget = Yii::app()->urlManager->baseUrl . '/schema/' . $this->schema . '/tables/' . $this->table . '/import';
     $res = $importPage->run();
     if ($res instanceof AjaxResponse) {
         $this->sendJSON($res);
     } else {
         $this->render('../global/import', array('model' => $importPage));
     }
 }
Example #2
0
 /**
  * Get preview data for an importing CSV file
  * @param String filePath
  * @return Array
  */
 protected function getPreviewDataFromCsv($filePath)
 {
     $fileHandle = OpenCSVFile($filePath, "");
     if ($fileHandle === FALSE) {
         return array();
     }
     $previewData = array();
     $previewData["CSVPreview"] = true;
     $firstTwoLinesData = $this->getNLinesFromFile($fileHandle, 2);
     $delimiter = $this->getCSVDelimiter($firstTwoLinesData);
     $previewData["delimiter"] = $delimiter;
     // reset the CSV file pointer
     $fileHandle = rewindFilePointerPosition($fileHandle, $filePath);
     $linesData = array();
     $fistNotEmptyLineData = $this->getNLinesFromFile($fileHandle, 1);
     $fieldsNamesLine = $fistNotEmptyLineData[0];
     $headerFieldsFromCSV = parceCSVLine($fieldsNamesLine, $delimiter, true);
     // add the first line with columns' names to the preview
     $linesData[] = $fieldsNamesLine;
     $fieldsData = $this->getCorrespondingImportFieldsData($headerFieldsFromCSV);
     $previewData["fieldsData"] = $fieldsData;
     $hasDTFields = ImportPage::hasDateTimeFields($fieldsData);
     // traversing CSV file lines starting from the second one
     while (($line = getLineFromFile($fileHandle, 1000000)) !== FALSE && count($linesData) < 100) {
         if (!strlen(trim($line))) {
             break;
         }
         $linesData[] = $line;
         if (!$hasDTFields) {
             continue;
         }
         $elems = parceCSVLine($line, $delimiter, true);
         foreach ($elems as $idx => $elem) {
             if (isset($fieldsData[$idx]) && $fieldsData[$idx]["dateTimeType"] && !strlen($dateFormat)) {
                 $dateFormat = ImportPage::extractDateFormat($elem);
             }
         }
     }
     if (ImportPage::hasDateTimeFields($fieldsData)) {
         $previewData["dateFormat"] = $this->getImportDateFormat($dateFormat);
     }
     $previewData["CSVlinesData"] = $linesData;
     CloseCSVFile($fileHandle);
     return $previewData;
 }
Example #3
0
$layout->skinsparams["2"] = array("button" => "button1");
$layout->skinsparams["3"] = array("button" => "button1");
require_once 'include/xtempl.php';
$xt = new Xtempl();
$id = postvalue("id");
$id = $id != "" ? $id : 1;
$xt->assign("id", $id);
//an array of params for ImportPage constructor
$params = array();
$params["id"] = $id;
$params["xt"] =& $xt;
$params["tName"] = $strTableName;
$params["pageType"] = PAGE_IMPORT;
$params["needSearchClauseObj"] = false;
$params["strOriginalTableName"] = $strOriginalTableName;
$pageObject = new ImportPage($params);
$pageObject->init();
$action = postvalue("a");
if (!strlen($action)) {
    $pageObject->removeOldTemporaryFiles();
}
if ($action == "importPreview") {
    $returnJSON = array();
    // prepare the temp import file name
    $rnrTempFileName = $pageObject->getImportTempFileName();
    if (postvalue("importType") == "text") {
        $importText = postvalue("importText");
        $returnJSON["previewData"] = $pageObject->getPreviewDataFromText($importText);
        // prepare the temp file path
        $rnrTempImportFilePath = getabspath("templates_c/" . $rnrTempFileName . ".csv");
        // save file in a temporary directory
/**
 * Get no more the 100 rows of the file's data to display the file's preview.
 * It could augment the fieldsData array 
 * It requires the PHPExcel plugin
 * @param PHPExcel fileHandle
 * @param &Array fieldsData		The corresponding import fields data
 * @return Array
 */
function getPreviewDataFromExcel($fileHandle, &$fieldsData)
{
    global $locale_info;
    $previewData = array();
    $remainNumOfPreviewRows = 100;
    foreach ($fileHandle->getWorksheetIterator() as $worksheet) {
        if ($remainNumOfPreviewRows <= 0) {
            break;
        }
        // get the number of rows for the current worksheet
        $highestRow = $worksheet->getHighestRow();
        if ($highestRow > $remainNumOfPreviewRows) {
            $highestRow = $remainNumOfPreviewRows;
        }
        $remainNumOfPreviewRows -= $highestRow;
        // get a litteral index of the 'highest' column (e.g. 'K')
        $highestColumn = $worksheet->getHighestColumn();
        // get an index number of the 'highest' column (e.g. 11)
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
        // start traversing rows from the first one that contains columns' names
        for ($row = 1; $row <= $highestRow; $row++) {
            $rowData = array();
            for ($col = 0; $col < $highestColumnIndex; $col++) {
                $cell = $worksheet->getCellByColumnAndRow($col, $row);
                $cellValue = $cell->getValue();
                if ($row > 1) {
                    $columnMatched = isset($fieldsData[$col]);
                    if (PHPExcel_Shared_Date::isDateTime($cell)) {
                        $cellDateFormat = $fileHandle->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode();
                        $cellTextValue = PHPExcel_Style_NumberFormat::ToFormattedString($cellValue, $cellDateFormat);
                        $cellValue = getTimeStamp($cellTextValue, $cellDateFormat);
                        if (!$columnMatched) {
                            $fieldsData[$col] = array();
                        }
                        $fieldsData[$col]["dateTimeType"] = true;
                        $fieldsData[$col]["requireFormatting"] = true;
                    } else {
                        if ($columnMatched && $fieldsData[$col]["dateTimeType"] && !strlen($dateFormat)) {
                            $dateFormat = ImportPage::extractDateFormat($cellValue);
                        }
                    }
                }
                $rowData[] = $cellValue;
            }
            $tableData[] = $rowData;
        }
    }
    $previewData["tableData"] = $tableData;
    if (ImportPage::hasDateTimeFields($fieldsData)) {
        $previewData["dateFormat"] = !strlen($dateFormat) ? $locale_info["LOCALE_SSHORTDATE"] : $dateFormat;
    }
    return $previewData;
}