00001 <?php
00017 include_once (OPENBIZ_HOME."/messages/excelService.msg");
00018
00028 class excelService
00029 {
00030
00037 function __construct(&$xmlArr)
00038 {
00039 }
00040
00047 function renderCSV ($objName)
00048 {
00049 $this->render($objName, ",", "csv");
00050
00051
00052 BizSystem::log(LOG_INFO, "ExcelService", "Export CSV file.");
00053 }
00054
00061 function renderTSV ($objName)
00062 {
00063 $this->render($objName, "\t", "tsv");
00064
00065 BizSystem::log(LOG_INFO, "ExcelService", "Export TSV file.");
00066 }
00067
00076 public function importCSV ($objName)
00077 {
00078
00079 foreach ($_FILES as $file)
00080 {
00081 $error = $file['error'];
00082 if ($error != 0)
00083 {
00084 $this->reportError($error);
00085 return;
00086 }
00087
00088 $tmpFileName = $file['tmp_name'];
00089 break;
00090 }
00091
00092 $filename = $file['name'];
00093 if (strpos($filename,".csv")===false)
00094 {
00095 $errorMsg = BizSystem::getMessage("EXCELSVC_INVALID_FILE",array($filename));
00096 BizSystem::log(LOG_ERR, "EXCEL SERVICE", "Import error = ".$errorMsg);
00097 BizSystem::clientProxy()->showClientAlert($errorMsg);
00098 return;
00099 }
00100
00101 $formObj = BizSystem::objectFactory()->getObject($objName);
00102 $parentFormObj = BizSystem::objectFactory()->getObject($formObj->m_ParentFormName);
00103 $dataObj = $parentFormObj->getDataObj();
00104
00105 $handle = fopen($tmpFileName, "r");
00106 $fields = fgetcsv($handle, 2000, ",");
00107 if (!$fields || count($fields)<2)
00108 {
00109 $errorMsg = BizSystem::getMessage("EXCELSVC_INVALID_FILE",array($filename));
00110 BizSystem::log(LOG_ERR, "EXCEL SERVICE", "Import error = ".$errorMsg);
00111 BizSystem::clientProxy()->showClientAlert($errorMsg);
00112 return;
00113 }
00114
00115
00116 foreach ($parentFormObj->m_DataPanel as $element)
00117 {
00118 $elem_fields[$element->m_Label] = $element->m_FieldName;
00119 }
00120
00121
00122 for ($i=0; $i<count($fields); $i++)
00123 {
00124 $fields[$i] = $elem_fields[$fields[$i]];
00125 $field = $fields[$i];
00126 if (!$dataObj->getField($field))
00127 {
00128 $errorMsg = BizSystem::getMessage("EXCELSVC_INVALID_COLUMN",array($field, $dataObj->m_Name));
00129 BizSystem::log(LOG_ERR, "EXCEL SERVICE", "Import error = ".$errorMsg);
00130 BizSystem::clientProxy()->showClientAlert($errorMsg);
00131 return;
00132 }
00133 }
00134
00135 while (($arr = fgetcsv($handle, 2000, ",")) !== FALSE)
00136 {
00137 if (count($arr) != count($fields))
00138 continue;
00139 unset($recArr);
00140 $i = 0;
00141 for ($i=0; $i<count($arr); $i++)
00142 {
00143 $recArr[$fields[$i]] = $arr[$i];
00144 }
00145
00146
00147 $dataRec = new DataRecord(null, $dataObj);
00148 foreach ($recArr as $k => $v)
00149 $dataRec[$k] = $v;
00150
00151 $ok = $dataRec->save();
00152 if (! $ok)
00153 {
00154
00155 return $formObj->processDataObjError($ok);
00156 }
00157
00158 }
00159 fclose($handle);
00160
00161
00162 if ($formObj->m_ParentFormName)
00163 {
00164 $formObj->close();
00165
00166 $formObj->renderParent();
00167 }
00168 }
00169
00178 protected function render($objName, $separator=",", $ext="csv")
00179 {
00180 ob_end_clean();
00181 header("Pragma: public");
00182 header("Expires: 0");
00183 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
00184 header("Cache-Control: public");
00185 header("Content-Description: File Transfer");
00186 header("Content-Type: application/vnd.ms-excel");
00187 header("Content-Disposition: filename=" . $objName."_".date('Y-m-d') . "." . $ext);
00188 header("Content-Transfer-Encoding: binary");
00189
00190 $dataTable = $this->getDataTable($objName);
00191 foreach ($dataTable as $row)
00192 {
00193 $line = "";
00194 foreach ($row as $cell) {
00195 $txt = $this->strip_cell($cell);
00196
00197
00198
00199 if (!is_null($txt))
00200 $line .= "\"" . $txt . "\"$separator";
00201 }
00202 $line = rtrim($line, $separator);
00203 $line = iconv("UTF-8","GB2312//IGNORE",$line);
00204 echo rtrim($line) . "\n";
00205 }
00206 }
00207
00208 protected function strip_cell($str){
00209 $str = strip_tags($str);
00210 $str = str_replace("\n",'',$str);
00211 $str = str_replace("\r",'',$str);
00212 return $str;
00213 }
00214
00215
00222 protected function getDataTable($objName)
00223 {
00224
00225 $formObj = BizSystem::objectFactory()->getObject($objName);
00226
00227
00228 if ($formObj instanceof BizForm)
00229 {
00230 $dataTable = $formObj->renderTable();
00231 }
00232
00233 if ($formObj instanceof EasyForm)
00234 {
00235 $recordSet = $formObj->fetchDataSet();
00236 $dataSet = $formObj->m_DataPanel->renderTable($recordSet);
00237 foreach ($dataSet['elems'] as $elem)
00238 {
00239 $labelRow[] = $elem['label'];
00240 }
00241 $dataTable = array_merge(array($labelRow), $dataSet['data']);
00242 }
00243
00244 return $dataTable;
00245
00246 }
00247
00253 protected function reportError($error)
00254 {
00255 if ($error==1)
00256 $errorStr = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
00257 else if ($error==2)
00258 $errorStr = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
00259 else if ($error==3)
00260 $errorStr = "The uploaded file was only partially uploaded";
00261 else if ($error==4)
00262 $errorStr = "No file was uploaded";
00263 else if ($error==6)
00264 $errorStr = "Missing a temporary folder";
00265 else if ($error==7)
00266 $errorStr = "Failed to write file to disk";
00267 else
00268 $errorStr = "Error in file upload";
00269
00270 BizSystem::clientProxy()->showErrorMessage($errorStr);
00271 }
00272 }
00273 ?>