/plugins/fvNeighbors/includes/GridServerHandler.php

https://github.com/RadicalLinux/faceBot · PHP · 392 lines · 298 code · 72 blank · 22 comment · 24 complexity · 5814f709b12c6b9c8d24985c40429731 MD5 · raw file

  1. <?php
  2. require_once('JSON.php');
  3. define('CONTENTTYPE', "text/html; charset=UTF-8");
  4. define('GT_JSON_NAME', "_gt_json");
  5. define('DATA_ROOT', "data");
  6. function row2Json($row){
  7. global $JSONUtils ;
  8. if (!isset($row) || $row==null) return '';
  9. return $JSONUtils->encode($row);
  10. }
  11. function json2Row($jsonStr){
  12. global $JSONUtils ;
  13. return $JSONUtils->decode($jsonStr);
  14. }
  15. function getCommonDate( $times=null ){
  16. return date('Y-m-d H:i:s',!$times?time():$times);
  17. }
  18. function getParameter($key){
  19. // empty or isset ?
  20. $value=@$_POST[$key];
  21. $value=isset($value)?$value:(isset($_GET[$key])?$_GET[$key]:NULL);
  22. if (get_magic_quotes_gpc()){
  23. $value=stripslashes($value);
  24. }
  25. return $value;
  26. }
  27. //reserved
  28. function getRequestArray(){
  29. }
  30. function getDocumentRoot(){
  31. #get env variables under apache
  32. $document_root = isset($_SERVER["DOCUMENT_ROOT"]) ? $_SERVER["DOCUMENT_ROOT"] : "";
  33. #get env variables under IIS
  34. if( !$document_root ){
  35. $sf = str_replace("\\","/",$_SERVER["SCRIPT_FILENAME"]);
  36. $sn = $_SERVER["SCRIPT_NAME"];
  37. $document_root = str_replace( $sn, "", $sf );
  38. }
  39. return $document_root;
  40. }
  41. function debug($msg , $file="/_server_log.log"){
  42. $msg = print_r( $msg ,true );
  43. error_log($msg."\n", 3, getDocumentRoot()."/_server_log.log");
  44. }
  45. class GridServerHandler {
  46. var $jsonObject =null;
  47. var $action =null;
  48. var $recordType =null;
  49. var $data =null;
  50. var $exception = null;
  51. var $pageInfo = array();
  52. var $exportFileName = 'mydoc';
  53. var $orientation = 'P';
  54. var $pageFormats = 'A4';
  55. //var $columnInfo = array();
  56. function GridServerHandler($gtJson = false)
  57. {
  58. if ($gtJson==false || empty($gtJson)){
  59. $gtJson=getParameter(GT_JSON_NAME);
  60. }
  61. //echo $gtJson;
  62. $this->init( $gtJson );
  63. }
  64. function init($gtJson = false) {
  65. global $JSONUtils ;
  66. if (!empty($gtJson)){
  67. $this->jsonObject = $JSONUtils->decode($gtJson);
  68. $this->action= $this->jsonObject["action"];
  69. $this->recordType= $this->jsonObject["recordType"].'';
  70. if ("load"==$this->action){
  71. //echo "come here";
  72. $this->initPageInfo();
  73. $this->initSortInfo();
  74. $this->initFilterInfo();
  75. }else if ("save"==$this->action){
  76. }else if ("export"==$this->action){
  77. }
  78. }
  79. }
  80. function getColumnInfo(){
  81. $columnInfo_JS = $this->jsonObject["columnInfo"];
  82. if ($columnInfo_JS!=null){
  83. }
  84. return $columnInfo_JS;
  85. }
  86. function getDisplayColumnInfo(){
  87. $columnInfo_JS = $this->getColumnInfo();
  88. $disColumnInfo = array();
  89. if ($columnInfo_JS!=null){
  90. foreach ($columnInfo_JS as $idx => $col) {
  91. if ( $col['hidden'].''!='1' && $col['hidden'].''!='true' && $col['hidden'].''!='TRUE'){
  92. array_push($disColumnInfo,$col);
  93. }
  94. }
  95. }
  96. return $disColumnInfo;
  97. }
  98. function initPageInfo(){
  99. $pageInfo_JS = $this->jsonObject["pageInfo"];
  100. if ($pageInfo_JS!=null){
  101. $keys=array(
  102. "pageSize" ,
  103. "pageNum" ,
  104. "totalRowNum" ,
  105. "totalPageNum" ,
  106. "startRowNum" ,
  107. "endRowNum"
  108. );
  109. foreach ($keys as $value) {
  110. $this->pageInfo[$value]=intval($pageInfo_JS[$value]);
  111. }
  112. }
  113. }
  114. function initSortInfo(){
  115. $JSON = $this->jsonObject["sortInfo"];
  116. //var_dump($this->jsonObject);
  117. if ($JSON!=null){
  118. $this->sortInfo = $JSON;
  119. //echo var_dump($this->sortInfo);
  120. }
  121. }
  122. function initFilterInfo(){
  123. $JSON = $this->jsonObject["filterInfo"];
  124. if ($JSON!=null){
  125. $this->filterInfo = $JSON;
  126. }
  127. }
  128. function setData($data) {
  129. $this->data = $data;
  130. }
  131. function getTotalRowNum() {
  132. return intval($this->pageInfo["totalRowNum"]);
  133. }
  134. function setTotalRowNum($totalRowNum) {
  135. $this->pageInfo["totalRowNum"]=intval($totalRowNum);
  136. }
  137. function getLoadResponseJSON(){
  138. $json=array();
  139. $json[DATA_ROOT]=$this->data;
  140. $json["pageInfo"]=$this->pageInfo;
  141. $json["exception"]=$this->exception;
  142. return $json;
  143. }
  144. function getLoadResponseText(){
  145. global $JSONUtils ;
  146. $json=$this->getLoadResponseJSON();
  147. $jstr=$json==null?"": $JSONUtils->encode($json);
  148. return $jstr;
  149. }
  150. function printLoadResponseText(){
  151. echo $this->getLoadResponseText();
  152. }
  153. function getExportFileName(){
  154. $fileName=getParameter('exportFileName');
  155. return !empty($fileName)?$fileName:$this->exportFileName;
  156. }
  157. function object2array($var){
  158. $type= gettype($var);
  159. if ($type=='object'){
  160. return get_object_vars($var);
  161. }
  162. return $var;
  163. }
  164. function exportGrid( $data, $baseImgPath='') {
  165. $type= getParameter('exportType');
  166. $this->exportFileName = $this->getExportFileName();
  167. if ($type=='pdf'){
  168. $this->exportPDF($baseImgPath);
  169. }else if ($type=='xml'){
  170. $this->exportXML($data);
  171. }
  172. }
  173. function exportXML( $data ) {
  174. $this->exportFileName = $this->getExportFileName();
  175. $cols=$this->getDisplayColumnInfo();
  176. $xml=array('<root>');
  177. foreach ( $data as $idx => $record ){
  178. $record = $this->object2array($record);
  179. $recordA= array();
  180. array_push($xml,"\t<row>" );
  181. foreach ( $cols as $idx => $col ){
  182. $fieldName=$col['fieldIndex'];
  183. $f= $record[$fieldName];
  184. $f= empty($f)?'':$f;
  185. array_push($xml,"\t\t<".$fieldName.'>'.htmlspecialchars($f).'</'.$fieldName.'>');
  186. }
  187. array_push($xml,"\t</row>" );
  188. }
  189. array_push($xml,'</root>' );
  190. $this->downloadTextFile($this->exportFileName.'.xml', join($xml,"\n") );
  191. }
  192. function exportXLS( $data) {
  193. require_once('ExcelExport.php');
  194. $this->exportFileName = $this->getExportFileName();
  195. $cols=$this->getDisplayColumnInfo();
  196. $xls = new ExcelExport();
  197. $colHeader= array();
  198. foreach ( $cols as $idx => $col ){
  199. array_push($colHeader,$col['header'] );
  200. }
  201. $xls->addRow( $colHeader );
  202. foreach ( $data as $idx => $record ){
  203. $record = $this->object2array($record);
  204. $recordA= array();
  205. foreach ( $cols as $idx => $col ){
  206. $f= @$record[$col['fieldIndex']];
  207. array_push($recordA,empty($f)?'':$f );
  208. }
  209. $xls->addRow( $recordA );
  210. }
  211. $xls->download($this->exportFileName.'.xls');
  212. }
  213. function parseCSVCell( $cell) {
  214. $val=$cell;
  215. if(!is_numeric($val) && is_string($val)) {
  216. $val= empty($val)?'':$val;
  217. $val= str_replace("\n","\r", str_replace("\r\n","\r",$val) );
  218. $val= str_replace('"','""',$val);
  219. $val= '"'.$val.'"';
  220. }
  221. return $cell;
  222. }
  223. function exportCSV( $data) {
  224. $this->exportFileName = $this->getExportFileName();
  225. $cols=$this->getDisplayColumnInfo();
  226. $csv=array();
  227. $colHeader= array();
  228. foreach ( $cols as $idx => $col ){
  229. array_push($colHeader,$this->parseCSVCell($col['header']) );
  230. }
  231. array_push($csv,join($colHeader,",") );
  232. foreach ( $data as $idx => $record ){
  233. $record = $this->object2array($record);
  234. $recordA= array();
  235. foreach ( $cols as $idx => $col ){
  236. $f= @$record[$col['fieldIndex']];
  237. array_push($recordA, $this->parseCSVCell($f) );
  238. }
  239. array_push($csv, join($recordA,",") );
  240. }
  241. $this->downloadTextFile($this->exportFileName.'.csv', join($csv,"\n") );
  242. }
  243. function downloadTextFile($fileName,$text){
  244. header('Content-Length: '.strlen($text));
  245. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  246. header("Content-Type: application/force-download");
  247. header("Content-Type: application/octet-stream");
  248. header("Content-Type: application/download");
  249. header('Content-Disposition: attachment; filename="'.$fileName.'"');
  250. header('Cache-Control: private, max-age=0, must-revalidate');
  251. header('Pragma: public');
  252. ini_set('zlib.output_compression','0');
  253. echo $text;
  254. }
  255. function exportPDF( $orientation="P",$pageFormats="A4",$baseImgPath='') {
  256. $this->exportFileName = $this->getExportFileName();
  257. $this->exportHTML2PDF($orientation,$pageFormats,$baseImgPath);
  258. }
  259. function exportHTML2PDF($orientation,$pageFormats, $baseImgPath) {
  260. require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
  261. ob_start();
  262. ?>
  263. <style type="text/css">
  264. .gt-table {
  265. border: solid 0px #000000;
  266. border-left:1px;
  267. border-top:1px;
  268. width: 100%;
  269. }
  270. .gt-table th {
  271. background-color: #eeeeee;
  272. border-right:1px; border-bottom:1px;
  273. }
  274. .gt-table td {
  275. border-right:1px; border-bottom:1px;
  276. }
  277. .gt-inner {
  278. width: 100%;
  279. }
  280. .gt-inner-right {
  281. text-align : right;
  282. }
  283. </style>
  284. <?php
  285. $template_style = ob_get_clean();
  286. $_pageD='10mm';
  287. $tableHTML= getParameter('__gt_html');
  288. //$headS = strpos($tableHTML, '<!-- gt : head start -->')+strlen('<!-- gt : head start -->');
  289. $headE = strpos($tableHTML, '<!-- gt : head end -->') +strlen('<!-- gt : head end -->');
  290. $tableStartHTML = substr($tableHTML , 0,$headE );
  291. $tableHTML= str_replace('.gt-grid ','',$tableHTML);
  292. $tableHTML=str_replace('<!-- gt : page separator -->','</tbody></table></page><page backtop="'.$_pageD.'" backbottom="'.$_pageD.'">'.$tableStartHTML.'<tbody>',$tableHTML);
  293. //debug ( $template_style );
  294. //debug ("----------------\n");
  295. //debug ( $tableHTML );
  296. ////////////////////////////////////////////////////////////////////////
  297. //begin !!the following lines exist for enhance exporting performance
  298. ////////////////////////////////////////////////////////////////////////
  299. preg_match_all('/\.([a-z0-9_\-]+)\s+\{(.*?)display:none;(.*?)\}/', $tableHTML, $result, PREG_SET_ORDER);
  300. $patternArray = array();
  301. $replaceArray = array();
  302. for ($matchi = 0; $matchi < count($result); $matchi++) {
  303. $patternArray[$matchi] ='/<td\s+class="([a-z0-9_\-]+\s+)*' . $result[$matchi][1] . '(\s+[a-z0-9_\-]+)*\s*"[^>]*>(.*?)<\/td>/';
  304. //debug($patternArray[$matchi] ."\n");
  305. $replaceArray[$matchi] = '';
  306. }
  307. $tableHTML = preg_replace($patternArray,$replaceArray,$tableHTML);
  308. ////////////////////////////////////////////////////////////////////////
  309. //end !!
  310. ////////////////////////////////////////////////////////////////////////
  311. //debug ("----------------\n");
  312. //debug ( $tableHTML );
  313. $page_content = '<page backtop="'.$_pageD.'" backbottom="'.$_pageD.'">' .
  314. $template_style.$tableHTML.'</page>';
  315. $pdf = new HTML2PDF($orientation,$pageFormats,'en',false,$baseImgPath);
  316. $pdf->WriteHTML($page_content, false);
  317. $pdf->pdf->Output($this->exportFileName.'.pdf', 'D');
  318. }
  319. }
  320. ?>