fgcf.odt.inc 8.7 KB

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