fgcf.odt.inc 11 KB

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