fgcf.odt.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. function fgcf_odt_export_formations($filename) {
  37. // Create new odf object from the template.
  38. $odf = fgcf_new_odf($filename);
  39. $thematique_1_boucle = $odf->setSegment('thematique_1_boucle');
  40. // Iterate over top-level terms in 'Thematique' vocab.
  41. $vid = _fgcf_get_vocabulary_by_name('FGCF Thématique');
  42. $tree = taxonomy_get_tree($vid);
  43. foreach ($tree as $term) {
  44. if ($term->depth == 0) {
  45. // Insert replacements for the top-level term.
  46. fgcf_odt_replace_term_1st_level($thematique_1_boucle, $term);
  47. // Iterate over secondary terms.
  48. $sub_tree = taxonomy_get_tree($vid, $term->tid);
  49. foreach ($sub_tree as $term_2) {
  50. // Select nodes for that term.
  51. $result = taxonomy_select_nodes(array($term_2->tid), 'or', 0, FALSE, 'n.nid ASC');
  52. while ($row = db_fetch_array($result)) {
  53. // Insert replacements for the node in the nested 'formations_boucle'
  54. // loop.
  55. $formation_boucle = $thematique_1_boucle->formations_boucle;
  56. $node = node_load($row['nid']);
  57. fgcf_odt_replace_node_formation($formation_boucle, $node);
  58. $formation_boucle->merge();
  59. }
  60. }
  61. $thematique_1_boucle->merge();
  62. }
  63. }
  64. $odf->mergeSegment($thematique_1_boucle);
  65. return $odf;
  66. }
  67. function fgcf_odt_replace_term_1st_level($segment, $term) {
  68. $replace = array();
  69. $replace['thematique_1_nom'] = strip_tags(filter_xss_admin($term->description));
  70. $replace['thematique_1_code'] = $term->name;
  71. _fgcf_odt_replace_segment($segment, $replace);
  72. // Additionally, insert image.
  73. $term_nodes = nat_get_nids(array($term->tid), TRUE);
  74. if ($term_node = current($term_nodes)) {
  75. $file = $term_node->field_fgcf_thematique_logo[0]['filepath'];
  76. $segment->setImage('thematique_1_image', $file);
  77. }
  78. }
  79. function fgcf_odt_replace_node_formation($segment, $node) {
  80. $keys = array(
  81. 'formation_drupal_id',
  82. 'formation_titre',
  83. 'formation_reference',
  84. 'formation_objectif',
  85. 'formation_contenu',
  86. 'formation_organisme',
  87. 'formation_modalites',
  88. 'formation_dif',
  89. 'formation_heures',
  90. 'formation_longueur',
  91. 'formation_format',
  92. 'formation_support',
  93. 'formation_sanction',
  94. 'formation_public',
  95. 'formation_thematique_1',
  96. 'formation_thematique_2',
  97. );
  98. // Initialise replacements with 'unspecified' text.
  99. $replace = array_fill_keys($keys, '-');
  100. // formation_drupal_id
  101. $replace['formation_drupal_id'] = $node->nid;
  102. // formation_titre
  103. $replace['formation_titre'] = $node->title;
  104. // formation_reference
  105. if (!empty($node->field_fgcf_fiche_reference[0]['value'])) {
  106. $replace['formation_reference'] = $node->field_fgcf_fiche_reference[0]['value'];
  107. }
  108. // formation_objectif
  109. if (!empty($node->field_fgcf_fiche_objectif[0]['value'])) {
  110. $replace['formation_objectif'] = check_markup($node->field_fgcf_fiche_objectif[0]['value'], $node->field_fgcf_fiche_objectif[0]['format']);
  111. }
  112. // formation_contenu
  113. // @todo bug sur certains nodes 220
  114. if (!empty($node->field_fgcf_fiche_contenu[0]['value'])) {
  115. // Do not translate markdown syntax - only replace bullet points
  116. $replace['formation_contenu'] = $node->field_fgcf_fiche_contenu[0]['value'];
  117. //$replace['formation_contenu'] = check_markup($node->field_fgcf_fiche_contenu[0]['value'], $node->field_fgcf_fiche_contenu[0]['format']);
  118. }
  119. // formation_organisme
  120. // @todo : organisme peut être multiple
  121. // @todo bug sur nodes 220 221 : Staff & Line Management
  122. $node_organisme = node_load($node->field_fgcf_fiche_organisme[0]['nid']);
  123. $replace['formation_organisme'] = $node_organisme->title;
  124. // formation_modalites
  125. $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
  126. $map = content_allowed_values($field);
  127. $values = array();
  128. foreach ((array) $node->field_fgcf_fiche_modalite as $item) {
  129. if (isset($map[$item['value']])) {
  130. $values[] = $map[$item['value']];
  131. }
  132. }
  133. if ($values) {
  134. $replace['formation_modalites'] = implode(', ', $values);
  135. }
  136. // formation_dif
  137. if (!empty($node->field_fgcf_fiche_dif[0]['value'])) {
  138. $item = $node->field_fgcf_fiche_dif['0']['value'];
  139. $field = content_fields('field_fgcf_fiche_dif', 'fgcf_fiche');
  140. $map = content_allowed_values($field);
  141. if (isset($map[$item])) {
  142. $replace['formation_dif'] = $map[$item];
  143. }
  144. }
  145. // formation_heures
  146. if (isset($node->field_fgcf_fiche_heures[0]['value'])) {
  147. $value = $node->field_fgcf_fiche_heures[0]['value'];
  148. $replace['formation_heures'] = format_plural($value, '@count heure', '@count heures');
  149. }
  150. // formation_longeur
  151. if (isset($node->field_fgcf_fiche_jours[0]['value'])) {
  152. $value = $node->field_fgcf_fiche_jours[0]['value'];
  153. $replace['formation_longueur'] = format_plural($value, '@count jour', '@count jours');
  154. }
  155. // formation_format
  156. $values = array();
  157. foreach ((array) $node->field_fgcf_fiche_format as $item) {
  158. $tid = $item['value'];
  159. if ($term = taxonomy_get_term($tid)) {
  160. $values[] = $term->name;
  161. }
  162. }
  163. if ($values) {
  164. // @todo : liste en items ?
  165. $replace['formation_format'] = implode(', ', $values);
  166. }
  167. // formation_support
  168. $values = array();
  169. foreach ((array) $node->field_fgcf_fiche_supports as $item) {
  170. $tid = $item['value'];
  171. if ($term = taxonomy_get_term($tid)) {
  172. $values[] = $term->name;
  173. }
  174. }
  175. if ($values) {
  176. // @todo : liste en items ?
  177. $replace['formation_support'] = implode(', ', $values);
  178. }
  179. // formation_sanction
  180. $values = array();
  181. foreach ((array) $node->field_fgcf_fiche_sanction as $item) {
  182. $tid = $item['value'];
  183. if ($term = taxonomy_get_term($tid)) {
  184. $values[] = $term->name;
  185. }
  186. }
  187. if ($values) {
  188. // @todo : liste en items ?
  189. $replace['formation_sanction'] = implode(', ', $values);
  190. }
  191. // formation_public
  192. $values = array();
  193. foreach ((array) $node->field_fgcf_fiche_public as $item) {
  194. $tid = $item['value'];
  195. if ($term = taxonomy_get_term($tid)) {
  196. $values[] = $term->name;
  197. }
  198. }
  199. if ($values) {
  200. // @todo : liste en items ?
  201. $replace['formation_public'] = implode(', ', $values);
  202. }
  203. // formation_thematique
  204. $values = array();
  205. foreach ((array) $node->taxonomy as $term) {
  206. if ($term->vid == _fgcf_get_vocabulary_by_name('FGCF Thématique')) {
  207. $parents = taxonomy_get_parents($term->tid);
  208. if ($parents) {
  209. $parent = current($parents);
  210. $replace['formation_thematique_1'] = strip_tags(filter_xss_admin($parent->description));
  211. }
  212. $replace['formation_thematique_2'] = strip_tags(filter_xss_admin($term->description));
  213. break;
  214. }
  215. }
  216. _fgcf_odt_replace_segment($segment, $replace);
  217. }
  218. function _fgcf_odt_replace_segment($segment, $replace) {
  219. foreach ($replace as $key => $value) {
  220. try {
  221. $segment->setVars($key, $value, FALSE, 'UTF-8');
  222. }
  223. catch (SegmentException $e) {
  224. // Pattern was not found in the template.
  225. }
  226. }
  227. }
  228. /**
  229. * Stolen and adapted from markdown.php :
  230. * Form HTML ordered (numbered) and unordered (bulleted) lists.
  231. */
  232. function doLists($text) {
  233. $less_than_tab = $this->tab_width - 1;
  234. # Re-usable patterns to match list item bullets and number markers:
  235. $marker_ul_re = '[*+-]';
  236. $marker_ol_re = '\d+[.]';
  237. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  238. $markers_relist = array(
  239. $marker_ul_re => $marker_ol_re,
  240. $marker_ol_re => $marker_ul_re,
  241. );
  242. foreach ($markers_relist as $marker_re => $other_marker_re) {
  243. # Re-usable pattern to match any entirel ul or ol list:
  244. $whole_list_re = '
  245. ( # $1 = whole list
  246. ( # $2
  247. ([ ]{0,'.$less_than_tab.'}) # $3 = number of spaces
  248. ('.$marker_re.') # $4 = first list item marker
  249. [ ]+
  250. )
  251. (?s:.+?)
  252. ( # $5
  253. \z
  254. |
  255. \n{2,}
  256. (?=\S)
  257. (?! # Negative lookahead for another list item marker
  258. [ ]*
  259. '.$marker_re.'[ ]+
  260. )
  261. |
  262. (?= # Lookahead for another kind of list
  263. \n
  264. \3 # Must have the same indentation
  265. '.$other_marker_re.'[ ]+
  266. )
  267. )
  268. )
  269. '; // mx
  270. # We use a different prefix before nested lists than top-level lists.
  271. # See extended comment in _ProcessListItems().
  272. if ($this->list_level) {
  273. $text = preg_replace_callback('{
  274. ^
  275. '.$whole_list_re.'
  276. }mx',
  277. array(&$this, '_doLists_callback'), $text);
  278. }
  279. else {
  280. $text = preg_replace_callback('{
  281. (?:(?<=\n)\n|\A\n?) # Must eat the newline
  282. '.$whole_list_re.'
  283. }mx',
  284. array(&$this, '_doLists_callback'), $text);
  285. }
  286. }
  287. return $text;
  288. }
  289. function _doLists_callback($matches) {
  290. # Re-usable patterns to match list item bullets and number markers:
  291. $marker_ul_re = '[*+-]';
  292. $marker_ol_re = '\d+[.]';
  293. $marker_any_re = "(?:$marker_ul_re|$marker_ol_re)";
  294. $list = $matches[1];
  295. $list_type = preg_match("/$marker_ul_re/", $matches[4]) ? "ul" : "ol";
  296. $marker_any_re = ( $list_type == "ul" ? $marker_ul_re : $marker_ol_re );
  297. $list .= "\n";
  298. $result = $this->processListItems($list, $marker_any_re);
  299. $result = $this->hashBlock("<$list_type>\n" . $result . "</$list_type>");
  300. return "\n". $result ."\n\n";
  301. }