/** * Init function */ function __construct($json) { parent::__construct($json); // Delimiter and quote characters $del = ","; $enc = '"'; $groups = (array) $this->jsonList[0]; $fileList = array(); // Create directory @mkdir($this->tempFilePath, 0700); foreach ($groups as $grp) { $outputFile = $this->tempFilePath . "\\" . $grp->name . '.csv'; $fileList[] = $outputFile; $fp = fopen($outputFile, 'w'); // Header $headerList = $grp->header; $csv_header = array(); foreach ($headerList as $h) { if ($h == '@') { //$col--; } else { $csv_header[] = $h; } } $this->fwritecsv($fp, $csv_header, $del, $enc); // Values $values = $grp->values; $csv_val = ''; foreach ($values as $vList) { $csv_row = array(); foreach ($vList as $v) { // Links if (is_object($v)) { if (isset($v->shplink)) { } if (isset($v->hyperlink)) { $csv_row[] = $v->hyperlink[2]; //str_replace($enc, "@@@@", $hyperlink[3]); // } } else { $csv_row[] = $v; //str_replace($enc, "@@@@", $v); } } $this->fwritecsv($fp, $csv_row, $del, $enc); } fclose($fp); unset($fp); } // Write all csv files to zip $this->tempFileLocation .= '.zip'; $zipFilePath = "{$this->tempFilePath}.zip"; PMCommon::packFilesZip($zipFilePath, $fileList, true, true); // remove directory rmdir($this->tempFilePath); }
function __construct($driverName, $exportExt, $json, $map, $bExportAtt = false, $optDataSourceCreation = NULL, $bSearchSpatialRef = false) { $bOk = false; $msVersion = null; // MapServer version $groups = null; // Layers list that will be exported parent::__construct($json); OGRRegisterAll(); // looking for destination driver for ($iDriver = 0; $iDriver < OGRGetDriverCount() && !$bOk; $iDriver++) { if (!strcasecmp(OGR_DR_GetName(OGRGetDriver($iDriver)), $driverName)) { $this->driverOGRDest = OGRGetDriver($iDriver); $bOk = true; break; } } if ($bOk) { // is it possible to create new data source with current driver if (!OGR_Dr_TestCapability($this->driverOGRDest, ODrCCreateDataSource)) { $this->driverOGRDest = null; $bOk = false; return; } } if ($bOk) { $msVersion = $_SESSION['MS_VERSION']; $groups = (array) $this->jsonList[0]; // Create the destination directory @mkdir($this->tempFilePath, 0700); // for each layers that will be exported foreach ($groups as $grp) { $baseDest = null; $layerList = array(); $attNameList = $grp->header; $ObjList = $grp->values; if (!$ObjList[0][0]->shplink) { continue; } // if we need to export attributs if ($bExportAtt) { include_once '../common/pluginsMapUtils.inc.php'; for ($iAtt = 0; $iAtt < count($attNameList); $iAtt++) { if ($attNameList[$iAtt] == '@') { array_splice($attNameList, $iAtt, 1); $iAtt--; continue; } // remove accents and spaces: $newAttName = PluginsMapUtils::decodeMapFileVal($map, $attNameList[$iAtt], 'UTF-8'); $newAttName = str_replace($this->bad, $this->good, $attNameList[$iAtt]); if (is_numeric($newAttName[0])) { $newAttName = 'z' . $newAttName; } $attNameList[$iAtt] = $newAttName; } } // for each objects that we want to export foreach ($ObjList as $Obj) { $layerSrc = null; // Source layer $layerDest = null; // Destination layer $baseDest = null; // Destination DataBase $geoType = 0; $shpLink = $Obj[0]->shplink; $layerName = $shpLink[0]; $IdObj = $shpLink[1]; // if it's the first time we found current layer if (!isset($layerList[$layerName])) { // getting mapserver layer $layerSrc = $map->getLayerByName($layerName); // use layers with complex queries that are too long to select results $newdata = $layerSrc->getMetaData('PM_RESULT_DATASUBSTITION'); if ($newdata != '') { $layerSrc->set('data', $newdata); } // create destination data base $output = $this->tempFilePath . "\\{$layerName}.{$exportExt}"; $output = str_replace('/', '\\', $output); $baseDest = OGR_Dr_CreateDataSource($this->driverOGRDest, $output, $optDataSourceCreation); if (!$baseDest) { continue; } // is it possible to create new layers in current data source ? if (!OGR_DS_TestCapability($baseDest, ODsCCreateLayer)) { if ($baseDest) { OGR_DS_Destroy($baseDest); } continue; } // create new layer in the destination data base $geoType = $this->typeList[$layerSrc->type]; $layerDest = $this->createNewLayer($map, $baseDest, $layerName, $geoType, $bSearchSpatialRef); // add new attribut in destination layer if ($bExportAtt) { $idAtt = 0; foreach ($attNameList as $attName) { $att = OGR_Fld_Create($attName, OFTString); OGR_L_CreateField($layerDest, $att, $idAtt); $idAtt++; } } // saving all in layerList $layerList[$layerName] = array(); $layerList[$layerName][] = $layerSrc; $layerList[$layerName][] = $baseDest; $layerList[$layerName][] = $layerDest; $layerList[$layerName][] = $geoType; // add file to files list will be exorted $this->addFileToFileList($output); } else { $layerSrc = $layerList[$layerName][0]; $baseDest = $layerList[$layerName][1]; $layerDest = $layerList[$layerName][2]; $geoType = $layerList[$layerName][3]; } // gettint shape object $srcShp = PMCommon::resultGetShape($msVersion, $layerSrc, null, $IdObj, -1); // export geometry of the object in WKT $geoWKT = $srcShp->toWkt(); // create new geometry OGR object from WKT geometry $geoOGR = $this->createNewOGRGeometry($geoWKT); // create new data line on destination layer $layerDestDefn = OGR_L_GetLayerDefn($layerDest); $Data = OGR_F_Create($layerDestDefn); // add geometry in data line OGR_F_SetGeometry($Data, $geoOGR); // if we need to export attributs if ($bExportAtt) { // add attributs values in data line for ($iAtt = 1; $iAtt < count($Obj); $iAtt++) { $newAttVal = PluginsMapUtils::decodeLayerVal($map, $layerName, $Obj[$iAtt], 'UTF-8'); OGR_F_SetFieldString($Data, $iAtt - 1 + $this->nbFieldBase, $newAttVal); } } $this->insertSpecialFields($geoType, $layerDestDefn, $Data); // add data line in destination layer OGR_L_CreateFeature($layerDest, $Data); OGR_F_Destroy($Data); OGR_G_DestroyGeometry($geoOGR); } foreach ($layerList as $l) { if (isset($l[1]) && $l[1]) { OGR_DS_Destroy($l[1]); } } } // files compression $this->zipFiles(); // remove directory rmdir($this->tempFilePath); } }
/** * Init function */ function __construct($json, $map) { parent::__construct($json); $msVersion = $_SESSION['MS_VERSION']; $grouplist = $_SESSION['grouplist']; //pm_logDebug(3, $grouplist, 'grouplist in exportSHP'); $groups = (array) $this->jsonList[0]; $fileList = array(); $valueList = array(); $dbfFieldList = array(); $cpyPrjFile = ''; @mkdir($this->tempFilePath, 0700); foreach ($groups as $grp) { $layerNameList = array(); $layerList = array(); $values = $grp->values; if (!$values[0][0]->shplink) { continue; } // headers with invalid characters include_once '../common/pluginsMapUtils.inc.php'; $headerListTmp = $grp->header; $headerList = array(); foreach ($headerListTmp as $n => $h) { if ($h != '@') { $newHeader = PluginsMapUtils::decodeMapFileVal($map, $h, 'ISO-8859-1'); // remove accents and spaces: // adpated from http://www.lecoindunet.com/zone_php/scripts_utiles/remplacer-les-caracteres-accentues-dune-chaine-en-php-72 $bad = array(' ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'Ð', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', '?', '?', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', '?', '?', 'L', 'l', 'N', 'n', 'N', 'n', 'N', 'n', '?', 'O', 'o', 'O', 'o', 'O', 'o', 'Œ', 'œ', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'Š', 'š', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Ÿ', 'Z', 'z', 'Z', 'z', 'Ž', 'ž', '?', 'ƒ', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', '?', '?', '?', '?', '?', '?'); $good = array('_', 'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o'); $newHeader = str_replace($bad, $good, $newHeader); // upper case $newHeader = strtoupper($newHeader); // if header already exists :'FIELD_' + index if (in_array($newHeader, $headerList)) { $newHeader = "FIELD_{$n}"; } $headerList[] = $newHeader; // if '@' not at the beginning } else { if ($n != 0) { $headerList = array(); break; } } } foreach ($values as $vList) { $dbfRow = array(); foreach ($vList as $n => $v) { if ($shplink = $v->shplink) { //pm_logDebug(3, $shplink, "shplink"); $layerName = $shplink[0]; $shpIdxTmp = $shplink[1]; $shpIdx = preg_replace('/^.*@/', '', $shpIdxTmp); $tileShpIdx = -1; if (!in_array($layerName, $layerNameList)) { $typeList = array(MS_SHP_POINT, MS_SHP_ARC, MS_SHP_POLYGON); $srcLayer = $map->getLayerByName($layerName); // use layers with complex queries that are too long to select results $newdata = $srcLayer->getMetaData('PM_RESULT_DATASUBSTITION'); if ($newdata != '') { $srcLayer->set('data', $newdata); } $srcLayerType = $srcLayer->type; //error_log("type: $srcLayerType"); $layerNameList[] = $layerName; $layerList[] = $srcLayer; $valueList[$layerName] = array(); $srcLayer->open(); //pm_logDebug(3, $layerItems); $outShpFname = "{$this->tempFilePath}/{$layerName}"; $shpFile = ms_newShapeFileObj($outShpFname, $typeList[$srcLayerType]); $dbfFileName = "{$outShpFname}.dbf"; $cpyPrjFile = ''; unset($proj); $proj = $srcLayer->getProjection(); if (empty($proj)) { $proj = $map->getProjection(); } if (!empty($proj)) { $prjFile = dirname(__FILE__) . "\\shp\\"; $pos = strpos($proj, 'epsg:'); if ($pos !== false) { $proj = substr($proj, $pos + 5); $prjFile .= "{$proj}.prj"; } if (file_exists($prjFile) && !is_dir($prjFile)) { $cpyPrjFile = "{$outShpFname}.prj"; copy($prjFile, $cpyPrjFile); } } $fileList[$layerName] = array(); $fileList[$layerName][] = "{$outShpFname}.shp"; $fileList[$layerName][] = "{$outShpFname}.shx"; $fileList[$layerName][] = $dbfFileName; if (file_exists($cpyPrjFile)) { $fileList[$layerName][] = $cpyPrjFile; } } $srcShp = PMCommon::resultGetShape($msVersion, $srcLayer, null, $shpIdx, -1); // changed for compatibility with PG layers and MS >= 5.6 $shpFile->addShape($srcShp); } else { if ($headerList) { $hyperlink = $v->hyperlink; if ($hyperlink) { $val = $hyperlink[2]; } else { $val = $v; } $fldName = $headerList[$n - 1]; $dbfRow[] = utf8_decode($val); if (!isset($dbfFieldList[$layerName])) { $dbfFieldList[$layerName] = array(); } if (!isset($dbfFieldList[$layerName][$fldName])) { $dbfFieldList[$layerName][$fldName] = array(); } $dbfFieldList[$layerName][$fldName] = $this->getFieldType(trim($val), $dbfFieldList[$layerName][$fldName]); } } } $valueList[$layerName][] = $dbfRow; } // some clean up // PMCommon::freeMsObj($shpFile); // shapefileObj: changes are committed only when the object is destroyed $shpFile->free(); unset($shpFile); foreach ($layerList as $l) { $l->close(); } } // write dBase files foreach ($fileList as $layerName => $files) { $dbfFileName = $fileList[$layerName][2]; // Modified by Thomas RAFFIN (SIRAP) // bug export for groups (with many layers) $dbfFieldListTmp = $dbfFieldList[$layerName]; $valueListTmp = $valueList[$layerName]; $this->writeDbf($dbfFileName, $dbfFieldListTmp, $valueListTmp); } // Write all files to zip and remove tmp dir $this->tempFileLocation .= '.zip'; $zipFilePath = "{$this->tempFilePath}.zip"; $filesToZip = array(); foreach ($fileList as $files) { foreach ($files as $file) { $filesToZip[] = $file; } } PMCommon::packFilesZip($zipFilePath, $filesToZip, true, true); rmdir($this->tempFilePath); }
/** * Init function */ function __construct($json) { parent::__construct($json); if (!(require_once 'Spreadsheet/Excel/Writer.php')) { pm_logDebug(0, "Missing PEAR packages Spreadsheet_Excel_Writer (and maybe OLE). See plugin Readme.txt for details."); } $this->tempFilePath .= '.xls'; $this->tempFileLocation .= '.xls'; $this->workbook = new Spreadsheet_Excel_Writer($this->tempFilePath); // For UTF encoding $this->workbook->setVersion(8); $format_bold = $this->workbook->addFormat(); $format_bold->setBold(); $format_title = $this->workbook->addFormat(); $format_title->setBold(); $format_title->setColor('blue'); $worksheet = $this->workbook->addWorksheet(); $worksheet->setInputEncoding('UTF-8'); $groups = (array) $this->jsonList[0]; $r = 0; foreach ($groups as $grp) { $worksheet->write($r, 0, $grp->description, $format_title); $r++; $headerList = $grp->header; $hL = count($headerList); $col = 0; for ($hi = 0; $hi < $hL; $hi++) { $headline = $headerList[$hi]; if ($headline == '@') { //$col--; } else { $worksheet->write($r, $col, $headline, $format_bold); $col++; } } $r++; $values = $grp->values; foreach ($values as $vList) { $vcol = 0; foreach ($vList as $v) { // Links if (is_object($v)) { if (isset($v->shplink)) { } if (isset($v->hyperlink)) { $worksheet->write($r, $vcol, utf8_decode($v->hyperlink[2])); $vcol++; } } else { if (is_string($v)) { $worksheet->writeString($r, $vcol, $v); } else { $worksheet->write($r, $vcol, $v); } $vcol++; } } $r++; } $r++; } $this->workbook->close(); }
/** * Init function */ function __construct($json) { require 'mc_table.php'; parent::__construct($json); // Get Config $conf = isset($_SESSION['pluginsConfig']) && isset($_SESSION['pluginsConfig']['export']) && isset($_SESSION['pluginsConfig']['export']['PDF']) ? $_SESSION['pluginsConfig']['export'] : array('PDF' => array()); $defaultFont = isset($conf['PDF']['defaultFont']) ? $conf['PDF']['defaultFont'] : 'FreeSans'; $defaultFontSize = isset($conf['PDF']['defaultFontSize']) ? $conf['PDF']['defaultFontSize'] : 9; $headerFont = isset($conf['PDF']['headerFont']) ? $conf['PDF']['headerFont'] : $defaultFont; $headerFontSize = isset($conf['PDF']['headerFontSize']) ? $conf['PDF']['headerFontSize'] : $defaultFontSize; $headerFontStyle = isset($conf['PDF']['headerFontStyle']) ? $conf['PDF']['headerFontStyle'] : ''; $layerFont = isset($conf['PDF']['layerFont']) ? $conf['PDF']['layerFont'] : $defaultFont; $layerFontSize = isset($conf['PDF']['layerFontSize']) ? $conf['PDF']['layerFontSize'] : $defaultFontSize; $layerFontStyle = isset($conf['PDF']['layerFontStyle']) ? $conf['PDF']['layerFontStyle'] : ''; // Write to table $pdf = new PDF_MC_Table(); // only for tcpdf version >= 3 // - font not well defined --> errors // - header and footer printed = lines on top and bottom $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->Open(); $groups = (array) $this->jsonList[0]; foreach ($groups as $grp) { $ret = $this->prepareData4PDF($grp); $colsPerc = $ret[0]; $data = $ret[1]; $header = $data['header']; $records = $data['records']; $pdfW = 180; // orientation detection $orientation = 'P'; $colsum = $ret[2]; $maxSize = max($headerFontSize, $defaultFontSize); $widthTmp = $colsum * $maxSize / 4; if ($widthTmp > 1.5 * $pdfW) { $pdfW = 270; $orientation = 'L'; } $pdf->AddPage($orientation); $cols = array(); foreach ($colsPerc as $cp) { $cols[] = $cp * $pdfW; } // add group name: $pdf->SetFont($layerFont, $layerFontStyle, $layerFontSize); $x = $pdf->GetX(); $y = $pdf->GetY(); $pdf->Cell(0, 0, $grp->description); $pdf->SetXY($x, $y + 9); // Calculate column widths $pdf->SetWidths($cols); // Add header $pdf->SetFont($headerFont, $headerFontStyle, $headerFontSize); $pdf->Row($header); // Add records $pdf->SetFont($defaultFont, '', $defaultFontSize); foreach ($records as $row) { $pdf->Row($row); } } $pdfFilePath = $this->tempFilePath . '.pdf'; $this->tempFileLocation .= '.pdf'; $pdf->Output($pdfFilePath, 'F'); }