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', '"" . "-"', $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; }