fgcf.odt.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. $count = 0;
  43. $count_cat = array();
  44. // Identify replacement segment in the template.
  45. $thematique_1_boucle = $odf->setSegment('thematique_1_boucle');
  46. // Iterate over top-level terms in 'Thematique' vocab.
  47. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  48. $tree = taxonomy_get_tree($vid);
  49. foreach ($tree as $term) {
  50. if ($term->depth == 0) {
  51. // Insert replacements for the top-level term.
  52. fgcf_odt_replace_term_1st_level($thematique_1_boucle, $term);
  53. // Iterate over secondary terms.
  54. $sub_tree = taxonomy_get_tree($vid, $term->tid);
  55. foreach ($sub_tree as $term_2) {
  56. $count_cat[$term_2->tid] = 0;
  57. // Select nodes for that term.
  58. $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';
  59. $result = db_query(db_rewrite_sql($sql), $term_2->tid);
  60. while ($row = db_fetch_array($result)) {
  61. // Insert replacements for the node in the nested 'formations_boucle'
  62. // loop.
  63. $formation_boucle = $thematique_1_boucle->formations_boucle;
  64. $node = node_load($row['nid']);
  65. fgcf_odt_replace_node_formation($formation_boucle, $node);
  66. $formation_boucle->merge();
  67. $count++;
  68. $count_cat[$term_2->tid]++;
  69. }
  70. }
  71. $thematique_1_boucle->merge();
  72. }
  73. }
  74. // Merge into final document.
  75. $odf->mergeSegment($thematique_1_boucle);
  76. return $odf;
  77. }
  78. function fgcf_odt_replace_term_1st_level($segment, $term) {
  79. $replace = array();
  80. $replace['thematique_1_nom'] = strip_tags(filter_xss_admin($term->description));
  81. $replace['thematique_1_code'] = check_plain($term->name);
  82. _fgcf_odt_replace_segment($segment, $replace);
  83. // Additionally, insert image.
  84. $term_nodes = nat_get_nids(array($term->tid), TRUE);
  85. if ($term_node = current($term_nodes)) {
  86. $file = $term_node->field_fgcf_thematique_logo[0]['filepath'];
  87. $segment->setImage('thematique_1_image', $file);
  88. }
  89. }
  90. function fgcf_odt_replace_node_formation($segment, $node) {
  91. $keys = array(
  92. 'formation_drupal_id',
  93. 'formation_titre',
  94. 'formation_reference',
  95. 'formation_objectif',
  96. 'formation_contenu',
  97. 'formation_organisme',
  98. 'formation_modalites',
  99. 'formation_dif',
  100. 'formation_heures',
  101. 'formation_longueur',
  102. 'formation_format',
  103. 'formation_support',
  104. 'formation_sanction',
  105. 'formation_public',
  106. 'formation_thematique_1',
  107. 'formation_thematique_2',
  108. );
  109. // Initialise replacements with 'unspecified' text.
  110. $replace = array_fill_keys($keys, '-');
  111. // Note : check_plain() is needed to escape characters like &...
  112. // formation_drupal_id
  113. $replace['formation_drupal_id'] = $node->nid;
  114. // formation_titre
  115. $replace['formation_titre'] = check_plain($node->title);
  116. // formation_reference
  117. if (!empty($node->field_fgcf_fiche_reference[0]['value'])) {
  118. $replace['formation_reference'] = check_plain($node->field_fgcf_fiche_reference[0]['value']);
  119. }
  120. // formation_objectif
  121. if (!empty($node->field_fgcf_fiche_objectif[0]['value'])) {
  122. $replace['formation_objectif'] = _fgcf_odt_handle_markdown($node->field_fgcf_fiche_objectif[0]['value']);
  123. }
  124. // formation_contenu
  125. if (!empty($node->field_fgcf_fiche_contenu[0]['value'])) {
  126. $replace['formation_contenu'] = _fgcf_odt_handle_markdown($node->field_fgcf_fiche_contenu[0]['value']);
  127. }
  128. // formation_organisme
  129. $values = array();
  130. foreach ((array) $node->field_fgcf_fiche_organisme as $item) {
  131. if ($node_organisme = node_load($item['nid'])) {
  132. $values[] = check_plain($node_organisme->title);
  133. }
  134. }
  135. if ($values) {
  136. $replace['formation_organisme'] = implode(', ', $values);
  137. }
  138. // formation_modalites
  139. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  140. $map = content_allowed_values($field);
  141. $values = array();
  142. foreach ((array) $node->field_fgcf_fiche_modalite as $item) {
  143. if (isset($map[$item['value']])) {
  144. $values[] = check_plain($map[$item['value']]);
  145. }
  146. }
  147. if ($values) {
  148. $replace['formation_modalites'] = implode(', ', $values);
  149. }
  150. // formation_dif
  151. if (!empty($node->field_fgcf_fiche_dif[0]['value'])) {
  152. $item = $node->field_fgcf_fiche_dif['0']['value'];
  153. $field = content_fields('field_fgcf_fiche_dif', 'fgcf_fiche');
  154. $map = content_allowed_values($field);
  155. if (isset($map[$item])) {
  156. $replace['formation_dif'] = check_plain($map[$item]);
  157. }
  158. }
  159. // formation_heures
  160. if (isset($node->field_fgcf_fiche_heures[0]['value'])) {
  161. $value = $node->field_fgcf_fiche_heures[0]['value'];
  162. $replace['formation_heures'] = format_plural($value, '@count heure', '@count heures');
  163. }
  164. // formation_longeur
  165. if (isset($node->field_fgcf_fiche_jours[0]['value'])) {
  166. $value = $node->field_fgcf_fiche_jours[0]['value'];
  167. $replace['formation_longueur'] = format_plural($value, '@count jour', '@count jours');
  168. }
  169. // formation_format
  170. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_format);
  171. if ($values) {
  172. $replace['formation_format'] = implode(', ', $values);
  173. }
  174. // formation_support
  175. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_supports);
  176. if ($values) {
  177. $replace['formation_support'] = implode(', ', $values);
  178. }
  179. // formation_sanction
  180. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_sanction);
  181. if ($values) {
  182. $replace['formation_sanction'] = implode(', ', $values);
  183. }
  184. // formation_public
  185. $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_public);
  186. if ($values) {
  187. $replace['formation_public'] = implode(', ', $values);
  188. }
  189. // formation_thematique
  190. $values = array();
  191. foreach ((array) $node->taxonomy as $term) {
  192. if ($term->vid == _fgcf_get_vocabulary_by_name('FGCF Thématique')) {
  193. $parents = taxonomy_get_parents($term->tid);
  194. if ($parents) {
  195. $parent = current($parents);
  196. $replace['formation_thematique_1'] = strip_tags(filter_xss_admin($parent->description));
  197. }
  198. $replace['formation_thematique_2'] = strip_tags(filter_xss_admin($term->description));
  199. break;
  200. }
  201. }
  202. _fgcf_odt_replace_segment($segment, $replace);
  203. }
  204. /**
  205. * Helper function : perform pattern replacements in the template.
  206. */
  207. function _fgcf_odt_replace_segment($segment, $replace) {
  208. foreach ($replace as $key => $value) {
  209. try {
  210. $segment->setVars($key, $value, FALSE, 'UTF-8');
  211. }
  212. catch (SegmentException $e) {
  213. // Pattern was not found in the template.
  214. }
  215. }
  216. }
  217. /**
  218. * Helper function : handle formatted text with markdown syntax.
  219. *
  220. * Do not translate markdown syntax - only replace bullet lists.
  221. */
  222. function _fgcf_odt_handle_markdown($value) {
  223. // Remove trailing linefeeds.
  224. $value = trim($value);
  225. $value = check_plain($value);
  226. // Simple whitespace chars are munged together in the final doc, so we insert an explicit spacing tag.
  227. $value = preg_replace('|^(\s*)\*|em', '"<text:s text:c=\"" . ( 2 * strlen("$1") ) . "\"/>" . "-"', $value);
  228. return $value;
  229. }
  230. /**
  231. * Helper function : handle values in taxonomy fields.
  232. */
  233. function _fgcf_odt_handle_taxonomy_field($items) {
  234. $values = array();
  235. foreach ($items as $item) {
  236. $tid = $item['value'];
  237. if ($term = taxonomy_get_term($tid)) {
  238. $values[] = check_plain($term->name);
  239. }
  240. }
  241. return $values;
  242. }