fgcf.odt.inc 10 KB

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