fgcf.odt.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Load the OdtPHP library and initialize some variables.
  4. */
  5. function fgcf_load_odt() {
  6. // Use correct temp folders - see http://www.odtphp.com/forum/viewtopic.php?f=5&t=20#p126
  7. // Temp folder used by PclZipProxy - see the beginning of PclZipProxy.php.
  8. // This folder is deleted and recreated each time, so we use a subfolder of
  9. // the tmp dir.
  10. define('PCL_ZIP_TMP', file_directory_temp() .'/phpodt/');
  11. // Temp folder used by the pclzip library - see the beginning of
  12. // pclzip.lib.php.
  13. define('PCLZIP_TEMPORARY_DIR', file_directory_temp() .'/');
  14. // Load the OdtPHP library.
  15. require_once(dirname(__FILE__) .'/odtphp/library/odf.php');
  16. }
  17. /**
  18. * Create a new odf object.
  19. *
  20. * @param $filename
  21. * The initial odt template (path relative to drupal root).
  22. *
  23. * @return odf
  24. * An odf object.
  25. */
  26. function fgcf_new_odf($filename) {
  27. fgcf_load_odt();
  28. $options = array(
  29. 'PATH_TO_TMP' => file_directory_temp() .'/',
  30. // The PHP Zip extension in recent PHP versions produces corrupt ODT
  31. // archives. See http://www.odtphp.com/forum/viewtopic.php?f=4&t=43#p154.
  32. 'ZIP_PROXY' => 'PclZipProxy',
  33. );
  34. return new odf($filename, $options);
  35. }
  36. /**
  37. * Generate the complete catalogue of 'formation' nodes.
  38. */
  39. function fgcf_odt_export_formations($filename) {
  40. // Create new odf object from the template.
  41. $odf = fgcf_new_odf($filename);
  42. // Identify replacement segment in the template.
  43. $thematique_1_boucle = $odf->setSegment('thematique_1_boucle');
  44. // Iterate over top-level terms in 'Thematique' vocab.
  45. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  46. $tree = taxonomy_get_tree($vid);
  47. foreach ($tree as $term) {
  48. if ($term->depth == 0) {
  49. // Insert replacements for the top-level term.
  50. fgcf_odt_replace_term_1st_level($thematique_1_boucle, $term);
  51. // Iterate over secondary terms.
  52. $sub_tree = taxonomy_get_tree($vid, $term->tid);
  53. foreach ($sub_tree as $term_2) {
  54. // Select nodes for that term.
  55. $result = taxonomy_select_nodes(array($term_2->tid), 'or', 0, FALSE, 'n.nid ASC');
  56. while ($row = db_fetch_array($result)) {
  57. // Insert replacements for the node in the nested 'formations_boucle'
  58. // loop.
  59. $formation_boucle = $thematique_1_boucle->formations_boucle;
  60. $node = node_load($row['nid']);
  61. fgcf_odt_replace_node_formation($formation_boucle, $node);
  62. $formation_boucle->merge();
  63. }
  64. }
  65. $thematique_1_boucle->merge();
  66. }
  67. }
  68. // Merge into final document.
  69. $odf->mergeSegment($thematique_1_boucle);
  70. return $odf;
  71. }
  72. function fgcf_odt_replace_term_1st_level($segment, $term) {
  73. $replace = array();
  74. $replace['thematique_1_nom'] = strip_tags(filter_xss_admin($term->description));
  75. $replace['thematique_1_code'] = check_plain($term->name);
  76. _fgcf_odt_replace_segment($segment, $replace);
  77. // Additionally, insert image.
  78. $term_nodes = nat_get_nids(array($term->tid), TRUE);
  79. if ($term_node = current($term_nodes)) {
  80. $file = $term_node->field_fgcf_thematique_logo[0]['filepath'];
  81. $segment->setImage('thematique_1_image', $file);
  82. }
  83. }
  84. function fgcf_odt_replace_node_formation($segment, $node) {
  85. $keys = array(
  86. 'formation_drupal_id',
  87. 'formation_titre',
  88. 'formation_reference',
  89. 'formation_objectif',
  90. 'formation_contenu',
  91. 'formation_organisme',
  92. 'formation_modalites',
  93. 'formation_dif',
  94. 'formation_heures',
  95. 'formation_longueur',
  96. 'formation_format',
  97. 'formation_support',
  98. 'formation_sanction',
  99. 'formation_public',
  100. 'formation_thematique_1',
  101. 'formation_thematique_2',
  102. );
  103. // Initialise replacements with 'unspecified' text.
  104. $replace = array_fill_keys($keys, '-');
  105. // Note : check_plain() is needed to escape characters like &...
  106. // formation_drupal_id
  107. $replace['formation_drupal_id'] = $node->nid;
  108. // formation_titre
  109. $replace['formation_titre'] = check_plain($node->title);
  110. // formation_reference
  111. if (!empty($node->field_fgcf_fiche_reference[0]['value'])) {
  112. $replace['formation_reference'] = check_plain($node->field_fgcf_fiche_reference[0]['value']);
  113. }
  114. // formation_objectif
  115. if (!empty($node->field_fgcf_fiche_objectif[0]['value'])) {
  116. $replace['formation_objectif'] = _fgcf_odt_handle_markdown($node->field_fgcf_fiche_objectif[0]['value']);
  117. }
  118. // formation_contenu
  119. if (!empty($node->field_fgcf_fiche_contenu[0]['value'])) {
  120. $replace['formation_contenu'] = _fgcf_odt_handle_markdown($node->field_fgcf_fiche_contenu[0]['value']);
  121. }
  122. // formation_organisme
  123. $values = array();
  124. foreach ((array) $node->field_fgcf_fiche_organisme as $item) {
  125. if ($node_organisme = node_load($item['nid'])) {
  126. $values[] = check_plain($node_organisme->title);
  127. }
  128. }
  129. if ($values) {
  130. $replace['formation_organisme'] = implode(', ', $values);
  131. }
  132. // formation_modalites
  133. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  134. $map = content_allowed_values($field);
  135. $values = array();
  136. foreach ((array) $node->field_fgcf_fiche_modalite as $item) {
  137. if (isset($map[$item['value']])) {
  138. $values[] = check_plain($map[$item['value']]);
  139. }
  140. }
  141. if ($values) {
  142. $replace['formation_modalites'] = implode(', ', $values);
  143. }
  144. // formation_dif
  145. if (!empty($node->field_fgcf_fiche_dif[0]['value'])) {
  146. $item = $node->field_fgcf_fiche_dif['0']['value'];
  147. $field = content_fields('field_fgcf_fiche_dif', 'fgcf_fiche');
  148. $map = content_allowed_values($field);
  149. if (isset($map[$item])) {
  150. $replace['formation_dif'] = check_plain($map[$item]);
  151. }
  152. }
  153. // formation_heures
  154. if (isset($node->field_fgcf_fiche_heures[0]['value'])) {
  155. $value = $node->field_fgcf_fiche_heures[0]['value'];
  156. $replace['formation_heures'] = format_plural($value, '@count heure', '@count heures');
  157. }
  158. // formation_longeur
  159. if (isset($node->field_fgcf_fiche_jours[0]['value'])) {
  160. $value = $node->field_fgcf_fiche_jours[0]['value'];
  161. $replace['formation_longueur'] = format_plural($value, '@count jour', '@count jours');
  162. }
  163. // formation_format
  164. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_format);
  165. if ($values) {
  166. $replace['formation_format'] = implode(', ', $values);
  167. }
  168. // formation_support
  169. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_supports);
  170. if ($values) {
  171. $replace['formation_support'] = implode(', ', $values);
  172. }
  173. // formation_sanction
  174. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_sanction);
  175. if ($values) {
  176. $replace['formation_sanction'] = implode(', ', $values);
  177. }
  178. // formation_public
  179. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_public);
  180. if ($values) {
  181. $replace['formation_public'] = implode(', ', $values);
  182. }
  183. // formation_thematique
  184. $values = array();
  185. foreach ((array) $node->taxonomy as $term) {
  186. if ($term->vid == _fgcf_get_vocabulary_by_name('FGCF Thématique')) {
  187. $parents = taxonomy_get_parents($term->tid);
  188. if ($parents) {
  189. $parent = current($parents);
  190. $replace['formation_thematique_1'] = strip_tags(filter_xss_admin($parent->description));
  191. }
  192. $replace['formation_thematique_2'] = strip_tags(filter_xss_admin($term->description));
  193. break;
  194. }
  195. }
  196. _fgcf_odt_replace_segment($segment, $replace);
  197. }
  198. /**
  199. * Helper function : perform pattern replacements in the template.
  200. */
  201. function _fgcf_odt_replace_segment($segment, $replace) {
  202. foreach ($replace as $key => $value) {
  203. try {
  204. $segment->setVars($key, $value, FALSE, 'UTF-8');
  205. }
  206. catch (SegmentException $e) {
  207. // Pattern was not found in the template.
  208. }
  209. }
  210. }
  211. /**
  212. * Helper function : handle formatted text with markdown syntax.
  213. *
  214. * Do not translate markdown syntax - only replace bullet lists.
  215. */
  216. function _fgcf_odt_handle_markdown($value) {
  217. // Remove trailing linefeeds.
  218. $value = trim($value);
  219. $value = check_plain($value);
  220. // Simple whitespace chars are munged together in the final doc, so we insert an explicit spacing tag.
  221. $value = preg_replace('|^(\s*)\*|em', '"<text:s text:c=\"" . ( 2 * strlen("$1") ) . "\"/>" . "-"', $value);
  222. return $value;
  223. }
  224. /**
  225. * Helper function : handle values in taxonomy fields.
  226. */
  227. function _fgcf_odt_handle_taxonomy_field($items) {
  228. $values = array();
  229. foreach ($items as $item) {
  230. $tid = $item['value'];
  231. if ($term = taxonomy_get_term($tid)) {
  232. $values[] = check_plain($term->name);
  233. }
  234. }
  235. return $values;
  236. }