123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- /**
- * @file
- *
- * Generation of catalog of 'fiche formation' nodes in ODT format.
- */
- /**
- * Load the OdtPHP library and initialize some variables.
- */
- function fgcf_load_odt() {
- // Use correct temp folders - see http://www.odtphp.com/forum/viewtopic.php?f=5&t=20#p126
- // Temp folder used by PclZipProxy - see the beginning of PclZipProxy.php.
- // This folder is deleted and recreated each time, so we use a subfolder of
- // the tmp dir.
- define('PCL_ZIP_TMP', file_directory_temp() .'/phpodt/');
- // Temp folder used by the pclzip library - see the beginning of
- // pclzip.lib.php.
- define('PCLZIP_TEMPORARY_DIR', file_directory_temp() .'/');
- // Load the OdtPHP library.
- require_once(dirname(__FILE__) .'/odtphp/library/odf.php');
- }
- /**
- * Create a new odf object.
- *
- * @param $filename
- * The initial odt template (path relative to drupal root).
- *
- * @return odf
- * An odf object.
- */
- function fgcf_new_odf($filename) {
- fgcf_load_odt();
- $options = array(
- 'PATH_TO_TMP' => file_directory_temp() .'/',
- // The PHP Zip extension in recent PHP versions produces corrupt ODT
- // archives. See http://www.odtphp.com/forum/viewtopic.php?f=4&t=43#p154.
- 'ZIP_PROXY' => 'PclZipProxy',
- );
- return new odf($filename, $options);
- }
- /**
- * Generate the complete catalogue of 'formation' nodes.
- */
- function fgcf_odt_export_formations($filename) {
- // Create new odf object from the template.
- $odf = fgcf_new_odf($filename);
- $count = 0;
- $count_cat = array();
- // Identify replacement segment in the template.
- $thematique_1_boucle = $odf->setSegment('thematique_1_boucle');
- // Iterate over top-level terms in 'Thème' vocab.
- $vid = _fgcf_get_vocabulary_by_name('FGCF Thème');
- $tree = taxonomy_get_tree($vid);
- foreach ($tree as $term) {
- if ($term->depth == 0) {
- // Insert replacements for the top-level term.
- fgcf_odt_replace_term_1st_level($thematique_1_boucle, $term);
- // Iterate over secondary terms.
- $sub_tree = taxonomy_get_tree($vid, $term->tid);
- foreach ($sub_tree as $term_2) {
- $count_cat[$term_2->tid] = 0;
- // Select nodes for that term.
- $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';
- $result = db_query(db_rewrite_sql($sql), $term_2->tid);
- while ($row = db_fetch_array($result)) {
- // Insert replacements for the node in the nested 'formations_boucle'
- // loop.
- $formation_boucle = $thematique_1_boucle->formations_boucle;
- $node = node_load($row['nid']);
- fgcf_odt_replace_node_formation($formation_boucle, $node);
- $formation_boucle->merge();
- $count++;
- $count_cat[$term_2->tid]++;
- }
- }
- $thematique_1_boucle->merge();
- }
- }
- // Merge into final document.
- $odf->mergeSegment($thematique_1_boucle);
- return $odf;
- }
- function fgcf_odt_replace_term_1st_level($segment, $term) {
- $replace = array();
- $replace['thematique_1_nom'] = strip_tags(filter_xss_admin($term->description));
- $replace['thematique_1_code'] = check_plain($term->name);
- _fgcf_odt_replace_segment($segment, $replace);
- // Additionally, insert image.
- $term_nodes = nat_get_nids(array($term->tid), TRUE);
- if ($term_node = current($term_nodes)) {
- $file = $term_node->field_fgcf_thematique_logo[0]['filepath'];
- $segment->setImage('thematique_1_image', $file);
- }
- }
- function fgcf_odt_replace_node_formation($segment, $node) {
- $keys = array(
- 'formation_drupal_id',
- 'formation_titre',
- 'formation_reference',
- 'formation_objectif',
- 'formation_contenu',
- 'formation_organisme',
- 'formation_modalites',
- 'formation_dif',
- 'formation_heures',
- 'formation_longueur',
- 'formation_format',
- 'formation_support',
- 'formation_sanction',
- 'formation_public',
- 'formation_thematique_1',
- 'formation_thematique_2',
- );
- // Initialise replacements with 'unspecified' text.
- $replace = array_fill_keys($keys, '-');
- // Note : check_plain() is needed to escape characters like &...
- // formation_drupal_id
- $replace['formation_drupal_id'] = $node->nid;
- // formation_titre
- $replace['formation_titre'] = check_plain($node->title);
- // formation_reference
- if (!empty($node->field_fgcf_fiche_reference[0]['value'])) {
- $replace['formation_reference'] = check_plain($node->field_fgcf_fiche_reference[0]['value']);
- }
- // formation_objectif
- if (!empty($node->field_fgcf_fiche_objectif[0]['value'])) {
- $replace['formation_objectif'] = _fgcf_odt_handle_markdown($node->field_fgcf_fiche_objectif[0]['value']);
- }
- // formation_contenu
- if (!empty($node->field_fgcf_fiche_contenu[0]['value'])) {
- $replace['formation_contenu'] = _fgcf_odt_handle_markdown($node->field_fgcf_fiche_contenu[0]['value']);
- }
- // formation_organisme
- $values = array();
- foreach ((array) $node->field_fgcf_fiche_organisme as $item) {
- if ($node_organisme = node_load($item['nid'])) {
- $values[] = check_plain($node_organisme->title);
- }
- }
- if ($values) {
- $replace['formation_organisme'] = implode(', ', $values);
- }
- // formation_modalites
- $field = content_fields('field_fgcf_fiche_modalite', 'fgcf_fiche');
- $map = content_allowed_values($field);
- $values = array();
- foreach ((array) $node->field_fgcf_fiche_modalite as $item) {
- if (isset($map[$item['value']])) {
- $values[] = check_plain($map[$item['value']]);
- }
- }
- if ($values) {
- $replace['formation_modalites'] = implode(', ', $values);
- }
- // formation_dif
- if (!empty($node->field_fgcf_fiche_dif[0]['value'])) {
- $item = $node->field_fgcf_fiche_dif['0']['value'];
- $field = content_fields('field_fgcf_fiche_dif', 'fgcf_fiche');
- $map = content_allowed_values($field);
- if (isset($map[$item])) {
- $replace['formation_dif'] = check_plain($map[$item]);
- }
- }
- // formation_heures
- if (isset($node->field_fgcf_fiche_heures[0]['value'])) {
- $value = $node->field_fgcf_fiche_heures[0]['value'];
- $replace['formation_heures'] = format_plural($value, '@count heure', '@count heures');
- }
- // formation_longeur
- if (isset($node->field_fgcf_fiche_jours[0]['value'])) {
- $value = $node->field_fgcf_fiche_jours[0]['value'];
- $replace['formation_longueur'] = format_plural($value, '@count jour', '@count jours');
- }
- // formation_format
- $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_format);
- if ($values) {
- $replace['formation_format'] = implode(', ', $values);
- }
- // formation_support
- $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_supports);
- if ($values) {
- $replace['formation_support'] = implode(', ', $values);
- }
- // formation_sanction
- $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_sanction);
- if ($values) {
- $replace['formation_sanction'] = implode(', ', $values);
- }
- // formation_public
- $values = _fgcf_odt_handle_taxonomy_field((array) $node->field_fgcf_fiche_public);
- if ($values) {
- $replace['formation_public'] = implode(', ', $values);
- }
- // formation_thematique
- $values = array();
- foreach ((array) $node->taxonomy as $term) {
- if ($term->vid == _fgcf_get_vocabulary_by_name('FGCF Thème')) {
- $parents = taxonomy_get_parents($term->tid);
- if ($parents) {
- $parent = current($parents);
- $replace['formation_thematique_1'] = strip_tags(filter_xss_admin($parent->description));
- }
- $replace['formation_thematique_2'] = strip_tags(filter_xss_admin($term->description));
- break;
- }
- }
- _fgcf_odt_replace_segment($segment, $replace);
- }
- /**
- * Helper function : perform pattern replacements in the template.
- */
- function _fgcf_odt_replace_segment($segment, $replace) {
- foreach ($replace as $key => $value) {
- try {
- $segment->setVars($key, $value, FALSE, 'UTF-8');
- }
- catch (SegmentException $e) {
- // Pattern was not found in the template.
- }
- }
- }
- /**
- * Helper function : handle formatted text with markdown syntax.
- *
- * Do not translate markdown syntax - only replace bullet lists.
- */
- function _fgcf_odt_handle_markdown($value) {
- // Remove trailing linefeeds.
- $value = trim($value);
- $value = check_plain($value);
- // Simple whitespace chars are munged together in the final doc, so we insert an explicit spacing tag.
- $value = preg_replace('|^(\s*)\*|em', '"<text:s text:c=\"" . ( 2 * strlen("$1") ) . "\"/>" . "-"', $value);
- return $value;
- }
- /**
- * Helper function : handle values in taxonomy fields.
- */
- function _fgcf_odt_handle_taxonomy_field($items) {
- $values = array();
- foreach ($items as $item) {
- $tid = $item['value'];
- if ($term = taxonomy_get_term($tid)) {
- $values[] = check_plain($term->name);
- }
- }
- return $values;
- }
|