PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/bccie/parser.php

https://github.com/tbuljevic/bccie
PHP | 422 lines | 285 code | 40 blank | 97 comment | 64 complexity | b45c8ce6c7965b8ac15997b02b1213b6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * File containing the parser class.
  4. *
  5. * @copyright Copyright (C) 1999 - 2012 Brookins Consulting. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 (or any later version)
  7. * @version //autogentag//
  8. * @package bccie
  9. */
  10. include_once('lib/ezutils/classes/ezini.php');
  11. include_once('extension/bccie/modules/bccie/basehandler.php');
  12. include_once('kernel/classes/ezcontentobjecttreenode.php');
  13. class Parser {
  14. var $handlerMap=array();
  15. var $exportableDatatypes;
  16. function Parser() {
  17. $ini = eZINI::instance( "export.ini" );
  18. $this->exportableDatatypes=$ini->variable( "General", "ExportableDatatypes" );
  19. foreach ($this->exportableDatatypes as $typename) {
  20. include_once("extension/bccie/modules/bccie/".$ini->variable($typename, 'HandlerFile'));
  21. $classname = $ini->variable($typename, 'HandlerClass');
  22. $handler = new $classname;
  23. $this->handlerMap[$typename]=array("handler" => $handler, "exportable" => true);
  24. }
  25. }
  26. function getExportableDatatypes() {
  27. return $this->exportableDatatypes;
  28. }
  29. function exportAttributeHeader(&$attribute, $seperationChar) {
  30. $contentClassAttribute = $attribute->contentClassAttribute();
  31. return $contentClassAttribute->Identifier;
  32. }
  33. function exportAttribute(&$attribute, $seperationChar)
  34. {
  35. $handler=$this->handlerMap[eZContentClassAttribute::dataTypeByID( $attribute->ContentClassAttributeID )]['handler'];
  36. /*
  37. BC: Error Debug Comment Test Case Output
  38. echo ( '<hr />' );
  39. print_r( $objectAttribute->DataTypeString );
  40. echo ( '<hr />' );
  41. print_r( $objectAttribute );
  42. echo ( '<hr />' );
  43. print_r( $this->handlerMap );
  44. echo ( '<hr />' );
  45. */
  46. if( $attribute && $seperationChar && is_object( $handler ))
  47. {
  48. $ret = $handler->exportAttribute($attribute, $seperationChar);
  49. }
  50. if ( is_null( $ret ) )
  51. return false;
  52. else
  53. return $ret;
  54. }
  55. function exportCollectionObjectHeaderNew(&$collection, &$attributes_to_export, $seperationChar) {
  56. $resultstring = array();
  57. array_push( $resultstring, "ID" );
  58. $attributes2=$collection->informationCollectionAttributes();
  59. foreach ($attributes2 as $currentattribute2) {
  60. array_push($resultstring,$this->exportAttributeHeader($currentattribute2, $seperationChar));
  61. }
  62. return $resultstring;
  63. }
  64. function exportCollectionObject(&$collection, &$attributes_to_export, $seperationChar) {
  65. $resultstring = array();
  66. foreach ($attributes_to_export as $attributeid) {
  67. if ($attributeid == "contentobjectid") {
  68. array_push($resultstring,$collection->ID);
  69. } else if ($attributeid == -1) {
  70. array_push($resultstring,"");
  71. } else if ($attributeid != -2) {
  72. $attributes=$collection->informationCollectionAttributes();
  73. foreach ($attributes as $currentattribute)
  74. {
  75. if ( ( (int) $attributeid ) == ( (int) $currentattribute->ContentClassAttributeID ) )
  76. {
  77. array_push($resultstring,$this->exportAttribute($currentattribute, $seperationChar));
  78. }
  79. }
  80. }
  81. }
  82. return $resultstring;
  83. }
  84. function exportCollectionObjectHeader( &$attributes_to_export ) {
  85. $resultstring = array();
  86. foreach( $attributes_to_export as $attributeid )
  87. {
  88. if ( $attributeid == "contentobjectid" ) {
  89. array_push( $resultstring, "ID" );
  90. } else if ( $attributeid == -1 ) {
  91. array_push( $resultstring, "" );
  92. } else if ( $attributeid != -2 ) {
  93. $attribute = & eZContentClassAttribute::fetch( $attributeid );
  94. $attribute_name = $attribute->name();
  95. $attribute_name_escaped = preg_replace( "(\r\n|\n|\r)", " ", $attribute_name );
  96. $attribute_name_escaped = utf8_decode( $attribute_name_escaped );
  97. array_push( $resultstring, $attribute_name_escaped );
  98. // works for 3.8 only
  99. // array_push($resultstring,$attribute->Name);
  100. }
  101. }
  102. return $resultstring;
  103. }
  104. function exportInformationCollection( $collections, $attributes_to_export, $seperationChar, $export_type='csv', $days ) {
  105. // eZDebug::writeDebug($attributes_to_export);
  106. switch($export_type){
  107. case "csv" :
  108. $returnstring = array();
  109. // TODO: Refactor foreach into method
  110. array_push( $returnstring, $this->exportCollectionObjectHeaderNew( $collections[0], $attributes_to_export, $seperationChar ) );
  111. foreach ($collections as $collection) {
  112. if( $days != false )
  113. {
  114. $current_datestamp = strtotime("now");
  115. $ci_created = $collection->Created;
  116. $range = mktime(0, 0, 0, date("m") , date("d")-$days, date("Y"));
  117. /*
  118. print_r( $collection );
  119. print_r( "\n##################################" );
  120. print_r( "\nDate: $current_datestamp". strtotime("now") );
  121. print_r( "\nCreated: $ci_created" );
  122. print_r( "\nDays: $days | $range" );
  123. print_r( "\n##################################" );
  124. // die();
  125. */
  126. if( $ci_created < $current_datestamp && $ci_created >= $range ){
  127. // print_r( "\nCI Date is lt current date and CI Date is gt eq range \n" );
  128. array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  129. }
  130. }
  131. else
  132. {
  133. array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  134. }
  135. // array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  136. }
  137. return $this->csv($returnstring,$seperationChar);
  138. break;
  139. case "sylk":
  140. $returnstring = array();
  141. // array_push($returnstring, $this->exportCollectionObjectHeader($attributes_to_export));
  142. // TODO: Refactor foreach into method
  143. foreach ($collections as $collection) {
  144. if( $days != false )
  145. {
  146. $current_datestamp = strtotime("now");
  147. $ci_created = $collection->Created;
  148. $range = mktime(0, 0, 0, date("m") , date("d")-$days, date("Y"));
  149. /*
  150. print_r( $collection );
  151. print_r( "\n##################################" );
  152. print_r( "\nDate: $current_datestamp". strtotime("now") );
  153. print_r( "\nCreated: $ci_created" );
  154. print_r( "\nDays: $days | $range" );
  155. print_r( "\n##################################" );
  156. // die();
  157. */
  158. if( $ci_created < $current_datestamp && $ci_created >= $range ){
  159. // print_r( "\nCI Date is lt current date and CI Date is gt eq range \n" );
  160. array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  161. }
  162. }
  163. else
  164. {
  165. array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  166. }
  167. }
  168. return $this->sylk($returnstring);
  169. break;
  170. default:
  171. $export_type='csv';
  172. $returnstring = array();
  173. // TODO: Refactor foreach into method
  174. foreach ($collections as $collection) {
  175. if( $days != false )
  176. {
  177. $current_datestamp = strtotime("now");
  178. $ci_created = $collection->Created;
  179. $range = mktime(0, 0, 0, date("m") , date("d")-$days, date("Y"));
  180. /*
  181. print_r( $collection );
  182. print_r( "\n##################################" );
  183. print_r( "\nDate: $current_datestamp". strtotime("now") );
  184. print_r( "\nCreated: $ci_created" );
  185. print_r( "\nDays: $days | $range" );
  186. print_r( "\n##################################" );
  187. // die();
  188. */
  189. if( $ci_created < $current_datestamp && $ci_created >= $range ){
  190. // print_r( "\nCI Date is lt current date and CI Date is gt eq range \n" );
  191. array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  192. }
  193. }
  194. else
  195. {
  196. array_push($returnstring, $this->exportCollectionObject($collection, $attributes_to_export, $seperationChar));
  197. }
  198. }
  199. return $this->csv($returnstring,$seperationChar);
  200. break;
  201. }
  202. }
  203. /* -------------- SYLK EXPORT ------------ */
  204. function sylk( $tableau )
  205. {
  206. if( !defined( 'FORMAT_REEL' ) )
  207. define("FORMAT_REEL", 1); // #,##0.00
  208. if( !defined( 'FORMAT_ENTIER' ) )
  209. define("FORMAT_ENTIER", 2); // #,##0
  210. if( !defined( 'FORMAT_TEXTE' ) )
  211. define("FORMAT_TEXTE", 3); // @
  212. $cfg_formats[FORMAT_ENTIER] = "FF0";
  213. $cfg_formats[FORMAT_REEL] = "FF2";
  214. $cfg_formats[FORMAT_TEXTE] = "FG0";
  215. if ($tableau)
  216. {
  217. // en-t? du fichier SYLK
  218. // $sylkcontent = "ID;Atchoum Production\n"; // ID;Pappli
  219. $sylkcontent = "ID;Pcie\n"; // ID;Pappli
  220. $sylkcontent = $sylkcontent."\n";
  221. // formats
  222. $sylkcontent = $sylkcontent."P;PGeneral\n";
  223. $sylkcontent = $sylkcontent."P;P#,##0.00\n"; // P;Pformat_1 (reels)
  224. $sylkcontent = $sylkcontent."P;P#,##0\n"; // P;Pformat_2 (entiers)
  225. $sylkcontent = $sylkcontent."P;P@\n"; // P;Pformat_3 (textes)
  226. $sylkcontent = $sylkcontent."\n";
  227. // polices
  228. $sylkcontent = $sylkcontent."P;EArial;M200\n";
  229. $sylkcontent = $sylkcontent."P;EArial;M200\n";
  230. $sylkcontent = $sylkcontent."P;EArial;M200\n";
  231. $sylkcontent = $sylkcontent."P;FArial;M200;SB\n";
  232. $sylkcontent = $sylkcontent."\n";
  233. // nb lignes * nb colonnes : B;Yligmax;Xcolmax
  234. $sylkcontent = $sylkcontent."B;Y".(count($tableau));
  235. // detection du nb de colonnes
  236. for($i=0;$i < count($tableau) ;$i++)
  237. $tmp[$i]=count($tableau[$i]);
  238. $nbcol=max($tmp);
  239. $sylkcontent = $sylkcontent.";X".$nbcol."\n";
  240. $sylkcontent = $sylkcontent."\n";
  241. // r?p?tion des infos de formatage des colonnes
  242. for($cpt=0; $cpt< $nbcol;$cpt++)
  243. {
  244. if( isset( $tableau[1][$cpt] ) )
  245. {
  246. switch(gettype($tableau[1][$cpt]))
  247. {
  248. case "integer":
  249. $num_format[$cpt]=FORMAT_ENTIER;
  250. $format[$cpt]= $cfg_formats[$num_format[$cpt]]."R";
  251. break;
  252. case "double":
  253. $num_format[$cpt]=FORMAT_REEL;
  254. $format[$cpt]= $cfg_formats[$num_format[$cpt]]."R";
  255. break;
  256. default:
  257. $num_format[$cpt]=FORMAT_TEXTE;
  258. $format[$cpt]= $cfg_formats[$num_format[$cpt]]."L";
  259. break;
  260. }
  261. }
  262. }
  263. // largeurs des colonnes
  264. for ($cpt = 1; $cpt <= $nbcol; $cpt++)
  265. {
  266. for($t=0;$t < count($tableau);$t++)
  267. {
  268. // $tmpo[$t]= strlen($tableau[$t][$cpt-1]);
  269. if( isset( $tableau[$t] ) )
  270. {
  271. if( isset( $tableau[$t][$cpt-1] ) )
  272. {
  273. $tmpo[$t] = strlen($tableau[$t][$cpt-1]);
  274. }
  275. }
  276. }
  277. /*
  278. if( !isset( $tableau[$t] ) )
  279. {
  280. $xyz = $cpt-1;
  281. print_r( "TTD: $t | " . $xyz ."\n" );
  282. // print_r( $tableau[$t] );
  283. }
  284. */
  285. // print_r( $tableau[$t][$cpt-1] );
  286. /*
  287. print_r( $tmpo );
  288. print_r( max($tmpo) );
  289. die( $tmpo );
  290. */
  291. $taille=max($tmpo);
  292. if ($taille==0)
  293. $taille=1;
  294. // F;Wcoldeb colfin largeur
  295. if (strlen($tableau[0][$cpt-1]) > $taille)
  296. $taille=strlen($tableau[0][$cpt-1]);
  297. if ($taille>50)
  298. $taille=50;
  299. $sylkcontent = $sylkcontent."F;W".$cpt." ".$cpt." ".$taille."\n";
  300. }
  301. $sylkcontent = $sylkcontent."F;W".$cpt." 256 8\n"; // F;Wcoldeb colfin largeur
  302. $sylkcontent = $sylkcontent."\n";
  303. // en-t? des colonnes (en gras --> SDM4)
  304. for ($cpt = 1; $cpt <= $nbcol; $cpt++)
  305. {
  306. $sylkcontent = $sylkcontent."F;SDM4;FG0C;".($cpt == 1 ? "Y1;" : "")."X".$cpt."\n";
  307. $sylkcontent = $sylkcontent."C;N;K\"".$tableau[0][$cpt-1]."\"\n";
  308. }
  309. $sylkcontent = $sylkcontent."\n";
  310. // donn? utiles
  311. $ligne = 2;
  312. for($i=1;$i< count($tableau);$i++)
  313. {
  314. // parcours des champs
  315. for ($cpt = 0; $cpt < $nbcol; $cpt++)
  316. {
  317. // print_r( $num_format[$cpt] );
  318. // print_r( $format[$cpt] );
  319. if( isset( $format[$cpt] ) && isset( $format[$cpt] ) )
  320. {
  321. // format
  322. $sylkcontent = $sylkcontent."F;P".$num_format[$cpt].";".$format[$cpt];
  323. $sylkcontent = $sylkcontent.($cpt == 0 ? ";Y".$ligne : "").";X".($cpt+1)."\n";
  324. // valeur
  325. if ($num_format[$cpt] == FORMAT_TEXTE)
  326. {
  327. // $sylkcontent = $sylkcontent."C;N;K\"".str_replace(';', ';;', $tableau[$i][$cpt])."\"\n";
  328. // if( isset( $tableau[$t] ) )
  329. if( isset( $tableau[$i] ) )
  330. {
  331. if( isset( $tableau[$i][$cpt] ) )
  332. {
  333. $sylkcontent = $sylkcontent."C;N;K\"".str_replace(';', ';;', $tableau[$i][$cpt])."\"\n";
  334. }
  335. }
  336. // $sylkcontent = $sylkcontent."C;N;K\"".str_replace(';', ';;', $tableau[$i][$cpt])."\"\n";
  337. }
  338. else
  339. {
  340. $sylkcontent = $sylkcontent."C;N;K".$tableau[$i][$cpt]."\n";
  341. }
  342. }
  343. }
  344. $sylkcontent = $sylkcontent."\n";
  345. $ligne++;
  346. }
  347. // fin du fichier
  348. $sylkcontent = $sylkcontent."E\n";
  349. return $sylkcontent;
  350. }else
  351. return false;
  352. }
  353. /*
  354. CSV EXPORT
  355. */
  356. function csv( $tableau, $seperator )
  357. {
  358. if ( $tableau )
  359. {
  360. $line="truc";
  361. for($i=0;$i < count($tableau) ;$i++)
  362. $tmp[$i]=count($tableau[$i]);
  363. $nbcol=max($tmp);
  364. $line = "";
  365. for($i=0;$i< count($tableau);$i++)
  366. {
  367. // parcours des champs
  368. for ($cpt = 0; $cpt < $nbcol; $cpt++)
  369. {
  370. if( isset( $tableau[$i][$cpt] ) )
  371. {
  372. $line .= '"'.trim($tableau[$i][$cpt]) .'"'. $seperator;
  373. }
  374. }
  375. $line .= "\n";
  376. }
  377. return $line;
  378. }
  379. }
  380. }
  381. ?>