/classes/parser.php

https://bitbucket.org/netgen/bccie · PHP · 675 lines · 488 code · 64 blank · 123 comment · 81 complexity · 190f8d4e8b597f95db963f39642cea4a MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the parser class.
  4. *
  5. * @copyright Copyright (C) 1999 - 2017 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. class Parser
  11. {
  12. var $handlerMap = array();
  13. var $exportableDatatypes;
  14. var $contentClassCollectorAttributes;
  15. var $exportCreationDate = false;
  16. var $exportModificationDate = false;
  17. function Parser( $objectID = false )
  18. {
  19. $ini = eZINI::instance( "export.ini" );
  20. $this->exportableDatatypes = $ini->variable( "General", "ExportableDatatypes" );
  21. foreach ( $this->exportableDatatypes as $typename )
  22. {
  23. $classname = $ini->variable( $typename, 'HandlerClass' );
  24. $handler = new $classname;
  25. $this->handlerMap[$typename] = array( "handler" => $handler, "exportable" => true );
  26. }
  27. /*
  28. if ( $objectID )
  29. {
  30. $this->getContentClassCollectorAttributes( $objectID );
  31. }
  32. */
  33. }
  34. function getExportableDatatypes()
  35. {
  36. return $this->exportableDatatypes;
  37. }
  38. function exportAttributeHeader( &$attribute, $seperationChar )
  39. {
  40. /* Older alternate */
  41. /*
  42. $contentClassAttribute = $attribute->contentClassAttribute();
  43. return $contentClassAttribute->Identifier;
  44. */
  45. return $attribute->attribute( 'name' );
  46. }
  47. function exportAttribute( &$attribute, $seperationChar )
  48. {
  49. $ret = false;
  50. $datatypeName = eZContentClassAttribute::dataTypeByID( $attribute->ContentClassAttributeID );
  51. if ( array_key_exists( $datatypeName, $this->handlerMap ) )
  52. {
  53. $handler = $this->handlerMap[$datatypeName]['handler'];
  54. } else {
  55. $handler = new BaseHandler();
  56. }
  57. /*
  58. BC: Error Debug Comment Test Case Output
  59. echo ( '<hr />' );
  60. print_r( $objectAttribute->DataTypeString );
  61. echo ( '<hr />' );
  62. print_r( $objectAttribute );
  63. echo ( '<hr />' );
  64. print_r( $this->handlerMap );
  65. echo ( '<hr />' );
  66. */
  67. if ( $attribute && $seperationChar && is_object( $handler ) )
  68. {
  69. $ret = $handler->exportAttribute( $attribute, $seperationChar );
  70. }
  71. if ( is_null( $ret ) )
  72. {
  73. return false;
  74. }
  75. else
  76. {
  77. return $ret;
  78. }
  79. }
  80. /*
  81. * Returns all collection attributes from the ContentClass of contentObject $objectID
  82. * @param $objectID int
  83. * @return set $this->contentClassCollectorAttributes
  84. */
  85. function getContentClassCollectorAttributes( $objectID )
  86. {
  87. $formObject = eZContentObject::fetch( $objectID );
  88. $formObjectClass = $formObject->contentClass();
  89. $contentClassAttributes = $formObjectClass->fetchAttributes();
  90. $this->contentClassCollectorAttributes = array();
  91. foreach ( $contentClassAttributes as $contentClassAttribute )
  92. {
  93. if ( $contentClassAttribute->attribute( 'is_information_collector' ) )
  94. {
  95. array_push(
  96. $this->contentClassCollectorAttributes,
  97. $contentClassAttribute->attribute( 'id' )
  98. );
  99. }
  100. }
  101. }
  102. function exportCollectionObjectHeaderNew(
  103. &$collection,
  104. &$attributes_to_export,
  105. $seperationChar
  106. )
  107. {
  108. $resultstring = array();
  109. array_push( $resultstring, "ID" );
  110. /* Older alternate */
  111. /*
  112. $attributes2=$collection->informationCollectionAttributes();
  113. foreach ( $attributes2 as $currentattribute2 ) {
  114. array_push( $resultstring,$this->exportAttributeHeader( $currentattribute2, $seperationChar ) );
  115. }
  116. */
  117. foreach ( $attributes_to_export as $classAttributeID )
  118. {
  119. $contentClassAttribute = eZContentClassAttribute::fetch( $classAttributeID );
  120. if ( $contentClassAttribute instanceof eZContentClassAttribute )
  121. {
  122. array_push(
  123. $resultstring,
  124. $this->exportAttributeHeader( $contentClassAttribute, $seperationChar )
  125. );
  126. }
  127. }
  128. if ( $this->getCreationDate() === true )
  129. {
  130. array_push(
  131. $resultstring,
  132. ezpI18n::tr( "design/bccie/export", "Created" )
  133. );
  134. }
  135. if ( $this->getModificationDate() === true )
  136. {
  137. array_push(
  138. $resultstring,
  139. ezpI18n::tr( "design/bccie/export", "Modified" )
  140. );
  141. }
  142. return $resultstring;
  143. }
  144. function exportCollectionObject( &$collection, &$attributes_to_export, $seperationChar )
  145. {
  146. $resultstring = array();
  147. $emptyAttributeExport = eZINI::instance( "cie.ini" )->variable( "CieSettings", "ExportZeroToEmptyString" ) === 'enabled';
  148. foreach ( $attributes_to_export as $attributeid )
  149. {
  150. if ( $attributeid == "contentobjectid" )
  151. {
  152. array_push( $resultstring, $collection->ID );
  153. }
  154. else if ( $attributeid == -1 )
  155. {
  156. array_push( $resultstring, "" );
  157. }
  158. else if ( $attributeid != -2 )
  159. {
  160. $attributes = $collection->informationCollectionAttributes();
  161. $exportedAttribute = false;
  162. foreach ( $attributes as $currentattribute )
  163. {
  164. if ( ( (int)$attributeid ) == ( (int)$currentattribute->ContentClassAttributeID ) )
  165. {
  166. /* array_push( $resultstring, $this->exportAttribute( $currentattribute, $seperationChar ) ); */
  167. $exportedAttribute = $this->exportAttribute(
  168. $currentattribute,
  169. $seperationChar
  170. );
  171. }
  172. }
  173. if ( ( !$emptyAttributeExport && $exportedAttribute !== false ) || ( $emptyAttributeExport && $exportedAttribute ) )
  174. {
  175. array_push( $resultstring, utf8_encode( $exportedAttribute ) );
  176. }
  177. else
  178. {
  179. array_push( $resultstring, "" );
  180. }
  181. }
  182. }
  183. if ( $this->getCreationDate() === true )
  184. {
  185. array_push( $resultstring, date( 'c', $collection->attribute( 'created' ) ) );
  186. }
  187. if ( $this->getModificationDate() === true )
  188. {
  189. array_push( $resultstring, date( 'c', $collection->attribute( 'modified' ) ) );
  190. }
  191. return $resultstring;
  192. }
  193. function exportCollectionObjectHeader( &$attributes_to_export )
  194. {
  195. $resultstring = array();
  196. foreach ( $attributes_to_export as $attributeid )
  197. {
  198. if ( $attributeid == "contentobjectid" )
  199. {
  200. array_push( $resultstring, "ID" );
  201. }
  202. else if ( $attributeid == -1 )
  203. {
  204. array_push( $resultstring, "" );
  205. }
  206. else if ( $attributeid != -2 )
  207. {
  208. $attribute = & eZContentClassAttribute::fetch( $attributeid );
  209. $attribute_name = $attribute->name();
  210. $attribute_name_escaped = preg_replace( "(\r\n|\n|\r)", " ", $attribute_name );
  211. $attribute_name_escaped = utf8_decode( $attribute_name_escaped );
  212. array_push( $resultstring, $attribute_name_escaped );
  213. // works for 3.8 only
  214. // array_push( $resultstring, $attribute->Name );
  215. }
  216. }
  217. return $resultstring;
  218. }
  219. function exportInformationCollection(
  220. $collections,
  221. $attributes_to_export,
  222. $seperationChar,
  223. $export_type = 'csv',
  224. $days = false,
  225. $creation_date = false,
  226. $modification_date = false
  227. )
  228. {
  229. // eZDebug::writeDebug( $attributes_to_export );
  230. if ( $creation_date !== false )
  231. {
  232. $this->setCreationDate( true );
  233. }
  234. if ( $modification_date !== false )
  235. {
  236. $this->setModificationDate( true );
  237. }
  238. switch ( $export_type )
  239. {
  240. case "csv" :
  241. $returnstring = array();
  242. // TODO: Refactor foreach into method
  243. array_push(
  244. $returnstring,
  245. $this->exportCollectionObjectHeaderNew(
  246. $collections[0],
  247. $attributes_to_export,
  248. $seperationChar
  249. )
  250. );
  251. foreach ( $collections as $collection )
  252. {
  253. if ( $days != false )
  254. {
  255. $current_datestamp = strtotime( "now" );
  256. $ci_created = $collection->Created;
  257. $range = mktime( 0, 0, 0, date( "m" ), date( "d" ) - $days, date( "Y" ) );
  258. /*
  259. print_r( $collection );
  260. print_r( "\n##################################" );
  261. print_r( "\nDate: $current_datestamp". strtotime("now") );
  262. print_r( "\nCreated: $ci_created" );
  263. print_r( "\nDays: $days | $range" );
  264. print_r( "\n##################################" );
  265. // die();
  266. */
  267. if ( $ci_created < $current_datestamp && $ci_created >= $range )
  268. {
  269. // print_r( "\nCI Date is lt current date and CI Date is gt eq range \n" );
  270. array_push(
  271. $returnstring,
  272. $this->exportCollectionObject(
  273. $collection,
  274. $attributes_to_export,
  275. $seperationChar
  276. )
  277. );
  278. }
  279. }
  280. else
  281. {
  282. array_push(
  283. $returnstring,
  284. $this->exportCollectionObject(
  285. $collection,
  286. $attributes_to_export,
  287. $seperationChar
  288. )
  289. );
  290. }
  291. // array_push( $returnstring, $this->exportCollectionObject( $collection, $attributes_to_export, $seperationChar ) );
  292. }
  293. return $this->csv( $returnstring, $seperationChar );
  294. break;
  295. case "sylk":
  296. $returnstring = array();
  297. // array_push( $returnstring, $this->exportCollectionObjectHeader( $attributes_to_export ) );
  298. // TODO: Refactor foreach into method
  299. foreach ( $collections as $collection )
  300. {
  301. if ( $days != false )
  302. {
  303. $current_datestamp = strtotime( "now" );
  304. $ci_created = $collection->Created;
  305. $range = mktime( 0, 0, 0, date( "m" ), date( "d" ) - $days, date( "Y" ) );
  306. /*
  307. print_r( $collection );
  308. print_r( "\n##################################" );
  309. print_r( "\nDate: $current_datestamp". strtotime("now") );
  310. print_r( "\nCreated: $ci_created" );
  311. print_r( "\nDays: $days | $range" );
  312. print_r( "\n##################################" );
  313. // die();
  314. */
  315. if ( $ci_created < $current_datestamp && $ci_created >= $range )
  316. {
  317. // print_r( "\nCI Date is lt current date and CI Date is gt eq range \n" );
  318. array_push(
  319. $returnstring,
  320. $this->exportCollectionObject(
  321. $collection,
  322. $attributes_to_export,
  323. $seperationChar
  324. )
  325. );
  326. }
  327. }
  328. else
  329. {
  330. array_push(
  331. $returnstring,
  332. $this->exportCollectionObject(
  333. $collection,
  334. $attributes_to_export,
  335. $seperationChar
  336. )
  337. );
  338. }
  339. }
  340. return $this->sylk( $returnstring );
  341. break;
  342. default:
  343. $export_type = 'csv';
  344. $returnstring = array();
  345. // TODO: Refactor foreach into method
  346. foreach ( $collections as $collection )
  347. {
  348. if ( $days != false )
  349. {
  350. $current_datestamp = strtotime( "now" );
  351. $ci_created = $collection->Created;
  352. $range = mktime( 0, 0, 0, date( "m" ), date( "d" ) - $days, date( "Y" ) );
  353. /*
  354. print_r( $collection );
  355. print_r( "\n##################################" );
  356. print_r( "\nDate: $current_datestamp". strtotime("now") );
  357. print_r( "\nCreated: $ci_created" );
  358. print_r( "\nDays: $days | $range" );
  359. print_r( "\n##################################" );
  360. // die();
  361. */
  362. if ( $ci_created < $current_datestamp && $ci_created >= $range )
  363. {
  364. // print_r( "\nCI Date is lt current date and CI Date is gt eq range \n" );
  365. array_push(
  366. $returnstring,
  367. $this->exportCollectionObject(
  368. $collection,
  369. $attributes_to_export,
  370. $seperationChar
  371. )
  372. );
  373. }
  374. }
  375. else
  376. {
  377. array_push(
  378. $returnstring,
  379. $this->exportCollectionObject(
  380. $collection,
  381. $attributes_to_export,
  382. $seperationChar
  383. )
  384. );
  385. }
  386. }
  387. return $this->csv( $returnstring, $seperationChar );
  388. break;
  389. }
  390. }
  391. /*
  392. * SYLK EXPORT
  393. */
  394. function sylk( $tableau )
  395. {
  396. if ( !defined( 'FORMAT_REEL' ) )
  397. {
  398. define( "FORMAT_REEL", 1 );
  399. } // #,##0.00
  400. if ( !defined( 'FORMAT_ENTIER' ) )
  401. {
  402. define( "FORMAT_ENTIER", 2 );
  403. } // #,##0
  404. if ( !defined( 'FORMAT_TEXTE' ) )
  405. {
  406. define( "FORMAT_TEXTE", 3 );
  407. } // @
  408. $cfg_formats[FORMAT_ENTIER] = "FF0";
  409. $cfg_formats[FORMAT_REEL] = "FF2";
  410. $cfg_formats[FORMAT_TEXTE] = "FG0";
  411. if ( $tableau )
  412. {
  413. // en-t? du fichier SYLK
  414. // $sylkcontent = "ID;Atchoum Production\n"; // ID;Pappli
  415. $sylkcontent = "ID;Pcie\n"; // ID;Pappli
  416. $sylkcontent = $sylkcontent . "\n";
  417. // formats
  418. $sylkcontent = $sylkcontent . "P;PGeneral\n";
  419. $sylkcontent = $sylkcontent . "P;P#,##0.00\n"; // P;Pformat_1 (reels)
  420. $sylkcontent = $sylkcontent . "P;P#,##0\n"; // P;Pformat_2 (entiers)
  421. $sylkcontent = $sylkcontent . "P;P@\n"; // P;Pformat_3 (textes)
  422. $sylkcontent = $sylkcontent . "\n";
  423. // polices
  424. $sylkcontent = $sylkcontent . "P;EArial;M200\n";
  425. $sylkcontent = $sylkcontent . "P;EArial;M200\n";
  426. $sylkcontent = $sylkcontent . "P;EArial;M200\n";
  427. $sylkcontent = $sylkcontent . "P;FArial;M200;SB\n";
  428. $sylkcontent = $sylkcontent . "\n";
  429. // nb lignes * nb colonnes : B;Yligmax;Xcolmax
  430. $sylkcontent = $sylkcontent . "B;Y" . ( count( $tableau ) );
  431. // detection du nb de colonnes
  432. for ( $i = 0; $i < count( $tableau ); $i++ )
  433. {
  434. $tmp[$i] = count( $tableau[$i] );
  435. }
  436. $nbcol = max( $tmp );
  437. $sylkcontent = $sylkcontent . ";X" . $nbcol . "\n";
  438. $sylkcontent = $sylkcontent . "\n";
  439. // r?p?tion des infos de formatage des colonnes
  440. for ( $cpt = 0; $cpt < $nbcol; $cpt++ )
  441. {
  442. if ( isset( $tableau[1][$cpt] ) )
  443. {
  444. switch ( gettype( $tableau[1][$cpt] ) )
  445. {
  446. case "integer":
  447. $num_format[$cpt] = FORMAT_ENTIER;
  448. $format[$cpt] = $cfg_formats[$num_format[$cpt]] . "R";
  449. break;
  450. case "double":
  451. $num_format[$cpt] = FORMAT_REEL;
  452. $format[$cpt] = $cfg_formats[$num_format[$cpt]] . "R";
  453. break;
  454. default:
  455. $num_format[$cpt] = FORMAT_TEXTE;
  456. $format[$cpt] = $cfg_formats[$num_format[$cpt]] . "L";
  457. break;
  458. }
  459. }
  460. }
  461. // largeurs des colonnes
  462. for ( $cpt = 1; $cpt <= $nbcol; $cpt++ )
  463. {
  464. for ( $t = 0; $t < count( $tableau ); $t++ )
  465. {
  466. // $tmpo[$t]= strlen( $tableau[$t][$cpt-1] );
  467. if ( isset( $tableau[$t] ) )
  468. {
  469. if ( isset( $tableau[$t][$cpt - 1] ) )
  470. {
  471. $tmpo[$t] = strlen( $tableau[$t][$cpt - 1] );
  472. }
  473. }
  474. }
  475. /*
  476. if( !isset( $tableau[$t] ) )
  477. {
  478. $xyz = $cpt-1;
  479. print_r( "TTD: $t | " . $xyz ."\n" );
  480. // print_r( $tableau[$t] );
  481. }
  482. */
  483. // print_r( $tableau[$t][$cpt-1] );
  484. /*
  485. print_r( $tmpo );
  486. print_r( max($tmpo) );
  487. die( $tmpo );
  488. */
  489. $taille = max( $tmpo );
  490. if ( $taille == 0 )
  491. {
  492. $taille = 1;
  493. }
  494. // F;Wcoldeb colfin largeur
  495. if ( strlen( $tableau[0][$cpt - 1] ) > $taille )
  496. {
  497. $taille = strlen( $tableau[0][$cpt - 1] );
  498. }
  499. if ( $taille > 50 )
  500. {
  501. $taille = 50;
  502. }
  503. $sylkcontent = $sylkcontent . "F;W" . $cpt . " " . $cpt . " " . $taille . "\n";
  504. }
  505. $sylkcontent = $sylkcontent . "F;W" . $cpt . " 256 8\n"; // F;Wcoldeb colfin largeur
  506. $sylkcontent = $sylkcontent . "\n";
  507. // en-t? des colonnes (en gras --> SDM4)
  508. for ( $cpt = 1; $cpt <= $nbcol; $cpt++ )
  509. {
  510. $sylkcontent = $sylkcontent . "F;SDM4;FG0C;" . ( $cpt == 1 ? "Y1;" : "" ) . "X" . $cpt . "\n";
  511. $sylkcontent = $sylkcontent . "C;N;K\"" . $tableau[0][$cpt - 1] . "\"\n";
  512. }
  513. $sylkcontent = $sylkcontent . "\n";
  514. // donn? utiles
  515. $ligne = 2;
  516. for ( $i = 1; $i < count( $tableau ); $i++ )
  517. {
  518. // parcours des champs
  519. for ( $cpt = 0; $cpt < $nbcol; $cpt++ )
  520. {
  521. // print_r( $num_format[$cpt] );
  522. // print_r( $format[$cpt] );
  523. if ( isset( $format[$cpt] ) && isset( $format[$cpt] ) )
  524. {
  525. // format
  526. $sylkcontent = $sylkcontent . "F;P" . $num_format[$cpt] . ";" . $format[$cpt];
  527. $sylkcontent = $sylkcontent . ( $cpt == 0 ? ";Y" . $ligne : "" ) . ";X" . ( $cpt + 1 ) . "\n";
  528. // valeur
  529. if ( $num_format[$cpt] == FORMAT_TEXTE )
  530. {
  531. // $sylkcontent = $sylkcontent."C;N;K\"".str_replace(';', ';;', $tableau[$i][$cpt])."\"\n";
  532. // if( isset( $tableau[$t] ) )
  533. if ( isset( $tableau[$i] ) )
  534. {
  535. if ( isset( $tableau[$i][$cpt] ) )
  536. {
  537. $sylkcontent = $sylkcontent . "C;N;K\"" . str_replace(
  538. ';',
  539. ';;',
  540. $tableau[$i][$cpt]
  541. ) . "\"\n";
  542. }
  543. }
  544. // $sylkcontent = $sylkcontent."C;N;K\"".str_replace( ';', ';;', $tableau[$i][$cpt] )."\"\n";
  545. }
  546. else
  547. {
  548. $sylkcontent = $sylkcontent . "C;N;K" . $tableau[$i][$cpt] . "\n";
  549. }
  550. }
  551. }
  552. $sylkcontent = $sylkcontent . "\n";
  553. $ligne++;
  554. }
  555. // fin du fichier
  556. $sylkcontent = $sylkcontent . "E\n";
  557. return $sylkcontent;
  558. }
  559. else
  560. {
  561. return false;
  562. }
  563. }
  564. /*
  565. * CSV EXPORT
  566. */
  567. function csv( $tableau, $seperator )
  568. {
  569. if ( $tableau )
  570. {
  571. $line = "truc";
  572. for ( $i = 0; $i < count( $tableau ); $i++ )
  573. {
  574. $tmp[$i] = count( $tableau[$i] );
  575. }
  576. $nbcol = max( $tmp );
  577. $line = "";
  578. for ( $i = 0; $i < count( $tableau ); $i++ )
  579. {
  580. // parcours des champs
  581. for ( $cpt = 0; $cpt < $nbcol; $cpt++ )
  582. {
  583. if ( isset( $tableau[$i][$cpt] ) )
  584. {
  585. $line .= '"' . trim( $tableau[$i][$cpt] ) . '"' . $seperator;
  586. }
  587. }
  588. $line .= "\n";
  589. }
  590. return $line;
  591. }
  592. }
  593. public function setModificationDate( $date )
  594. {
  595. $this->exportModificationDate = $date;
  596. }
  597. public function setCreationDate( $date )
  598. {
  599. $this->exportCreationDate = $date;
  600. }
  601. public function getCreationDate()
  602. {
  603. return $this->exportCreationDate;
  604. }
  605. public function getModificationDate()
  606. {
  607. return $this->exportModificationDate;
  608. }
  609. }
  610. ?>