fgcf.odt.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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'] = $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. // formation_drupal_id
  106. $replace['formation_drupal_id'] = $node->nid;
  107. // formation_titre
  108. $replace['formation_titre'] = $node->title;
  109. // formation_reference
  110. if (!empty($node->field_fgcf_fiche_reference[0]['value'])) {
  111. $replace['formation_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  112. }
  113. // formation_objectif
  114. if (!empty($node->field_fgcf_fiche_objectif[0]['value'])) {
  115. $replace['formation_objectif'] = check_markup($node->field_fgcf_fiche_objectif[0]['value'], $node->field_fgcf_fiche_objectif[0]['format']);
  116. }
  117. // formation_contenu
  118. // @todo bug sur certains nodes 220
  119. if (!empty($node->field_fgcf_fiche_contenu[0]['value'])) {
  120. // Do not translate markdown syntax - only replace bullet points
  121. $replace['formation_contenu'] = $node->field_fgcf_fiche_contenu[0]['value'];
  122. //$replace['formation_contenu'] = check_markup($node->field_fgcf_fiche_contenu[0]['value'], $node->field_fgcf_fiche_contenu[0]['format']);
  123. }
  124. // formation_organisme
  125. // @todo : organisme peut être multiple
  126. // @todo bug sur nodes 220 221 : Staff & Line Management
  127. $node_organisme = node_load($node->field_fgcf_fiche_organisme[0]['nid']);
  128. $replace['formation_organisme'] = $node_organisme->title;
  129. // formation_modalites
  130. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  131. $map = content_allowed_values($field);
  132. $values = array();
  133. foreach ((array) $node->field_fgcf_fiche_modalite as $item) {
  134. if (isset($map[$item['value']])) {
  135. $values[] = $map[$item['value']];
  136. }
  137. }
  138. if ($values) {
  139. $replace['formation_modalites'] = implode(', ', $values);
  140. }
  141. // formation_dif
  142. if (!empty($node->field_fgcf_fiche_dif[0]['value'])) {
  143. $item = $node->field_fgcf_fiche_dif['0']['value'];
  144. $field = content_fields('field_fgcf_fiche_dif', 'fgcf_fiche');
  145. $map = content_allowed_values($field);
  146. if (isset($map[$item])) {
  147. $replace['formation_dif'] = $map[$item];
  148. }
  149. }
  150. // formation_heures
  151. if (isset($node->field_fgcf_fiche_heures[0]['value'])) {
  152. $value = $node->field_fgcf_fiche_heures[0]['value'];
  153. $replace['formation_heures'] = format_plural($value, '@count heure', '@count heures');
  154. }
  155. // formation_longeur
  156. if (isset($node->field_fgcf_fiche_jours[0]['value'])) {
  157. $value = $node->field_fgcf_fiche_jours[0]['value'];
  158. $replace['formation_longueur'] = format_plural($value, '@count jour', '@count jours');
  159. }
  160. // formation_format
  161. $values = array();
  162. foreach ((array) $node->field_fgcf_fiche_format as $item) {
  163. $tid = $item['value'];
  164. if ($term = taxonomy_get_term($tid)) {
  165. $values[] = $term->name;
  166. }
  167. }
  168. if ($values) {
  169. // @todo : liste en items ?
  170. $replace['formation_format'] = implode(', ', $values);
  171. }
  172. // formation_support
  173. $values = array();
  174. foreach ((array) $node->field_fgcf_fiche_supports as $item) {
  175. $tid = $item['value'];
  176. if ($term = taxonomy_get_term($tid)) {
  177. $values[] = $term->name;
  178. }
  179. }
  180. if ($values) {
  181. // @todo : liste en items ?
  182. $replace['formation_support'] = implode(', ', $values);
  183. }
  184. // formation_sanction
  185. $values = array();
  186. foreach ((array) $node->field_fgcf_fiche_sanction as $item) {
  187. $tid = $item['value'];
  188. if ($term = taxonomy_get_term($tid)) {
  189. $values[] = $term->name;
  190. }
  191. }
  192. if ($values) {
  193. // @todo : liste en items ?
  194. $replace['formation_sanction'] = implode(', ', $values);
  195. }
  196. // formation_public
  197. $values = array();
  198. foreach ((array) $node->field_fgcf_fiche_public as $item) {
  199. $tid = $item['value'];
  200. if ($term = taxonomy_get_term($tid)) {
  201. $values[] = $term->name;
  202. }
  203. }
  204. if ($values) {
  205. // @todo : liste en items ?
  206. $replace['formation_public'] = implode(', ', $values);
  207. }
  208. // formation_thematique
  209. $values = array();
  210. foreach ((array) $node->taxonomy as $term) {
  211. if ($term->vid == _fgcf_get_vocabulary_by_name('FGCF Thématique')) {
  212. $parents = taxonomy_get_parents($term->tid);
  213. if ($parents) {
  214. $parent = current($parents);
  215. $replace['formation_thematique_1'] = strip_tags(filter_xss_admin($parent->description));
  216. }
  217. $replace['formation_thematique_2'] = strip_tags(filter_xss_admin($term->description));
  218. break;
  219. }
  220. }
  221. _fgcf_odt_replace_segment($segment, $replace);
  222. }
  223. function _fgcf_odt_replace_segment($segment, $replace) {
  224. foreach ($replace as $key => $value) {
  225. try {
  226. $segment->setVars($key, $value, FALSE, 'UTF-8');
  227. }
  228. catch (SegmentException $e) {
  229. // Pattern was not found in the template.
  230. }
  231. }
  232. }
  233. /**
  234. * Stolen and adapted from markdown.php :
  235. * Form HTML ordered (numbered) and unordered (bulleted) lists.
  236. */
  237. function doLists($text) {
  238. $less_than_tab = $this->tab_width - 1;
  239. # Re-usable patterns to match list item bullets and number markers:
  240. $marker_ul_re = '[*+-]';
  241. $marker_ol_re = '\d+[.]';
  242. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  243. $markers_relist = array(
  244. $marker_ul_re => $marker_ol_re,
  245. $marker_ol_re => $marker_ul_re,
  246. );
  247. foreach ($markers_relist as $marker_re => $other_marker_re) {
  248. # Re-usable pattern to match any entirel ul or ol list:
  249. $whole_list_re = '
  250. ( # $1 = whole list
  251. ( # $2
  252. ([ ]{0,'.$less_than_tab.'}) # $3 = number of spaces
  253. ('.$marker_re.') # $4 = first list item marker
  254. [ ]+
  255. )
  256. (?s:.+?)
  257. ( # $5
  258. \z
  259. |
  260. \n{2,}
  261. (?=\S)
  262. (?! # Negative lookahead for another list item marker
  263. [ ]*
  264. '.$marker_re.'[ ]+
  265. )
  266. |
  267. (?= # Lookahead for another kind of list
  268. \n
  269. \3 # Must have the same indentation
  270. '.$other_marker_re.'[ ]+
  271. )
  272. )
  273. )
  274. '; // mx
  275. # We use a different prefix before nested lists than top-level lists.
  276. # See extended comment in _ProcessListItems().
  277. if ($this->list_level) {
  278. $text = preg_replace_callback('{
  279. ^
  280. '.$whole_list_re.'
  281. }mx',
  282. array(&$this, '_doLists_callback'), $text);
  283. }
  284. else {
  285. $text = preg_replace_callback('{
  286. (?:(?<=\n)\n|\A\n?) # Must eat the newline
  287. '.$whole_list_re.'
  288. }mx',
  289. array(&$this, '_doLists_callback'), $text);
  290. }
  291. }
  292. return $text;
  293. }
  294. function _doLists_callback($matches) {
  295. # Re-usable patterns to match list item bullets and number markers:
  296. $marker_ul_re = '[*+-]';
  297. $marker_ol_re = '\d+[.]';
  298. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  299. $list = $matches[1];
  300. $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol";
  301. $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re );
  302. $list .= "\n";
  303. $result = $this->processListItems($list, $marker_any_re);
  304. $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>");
  305. return "\n". $result ."\n\n";
  306. }