materio_taxonomy.module 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Implements hook_permission().
  4. */
  5. function materio_taxonomy_permission() {
  6. $perms = array();
  7. // foreach (taxonomy_get_vocabularies() as $voc) {
  8. // dsm($voc, '$voc');
  9. // // $perms['access taxonomy term page'] => array(
  10. // // 'title' => t(''),
  11. // // 'description' => t(''),
  12. // // );
  13. // }
  14. $perms['access taxonomy terms page'] = array(
  15. 'title' => t('access taxonomy terms page'),
  16. 'description' => t(''),
  17. );
  18. $perms['access fr2en materio'] = array(
  19. 'title' => t('access fr2en materio'),
  20. 'description' => t(''),
  21. );
  22. return $perms;
  23. }
  24. function materio_taxonomy_menu_alter(&$items) {
  25. $items['taxonomy/term/%taxonomy_term']['access arguments'] = array('access taxonomy terms page');
  26. $items['taxonomy/term/%taxonomy_term/view']['access arguments'] = array('access taxonomy terms page');
  27. $items['taxonomy/term/%taxonomy_term/feed']['access arguments'] = array('access taxonomy terms page');
  28. }
  29. /**
  30. * Implementation of hook_query_term_access_alter().
  31. *
  32. * debug de i18n_select_query_term_access_alter()
  33. * qui filtre les terms par language mm pour les vocabulaires en mode localized.
  34. * utile pour la recherche avancée de materio_search_api materio_search_api_advanced_search_form()
  35. *
  36. */
  37. function materio_taxonomy_query_term_access_alter(QueryAlterableInterface $query) {
  38. // dsm($query, 'materio taxo query');
  39. $conditions =& $query->conditions();
  40. # roll over condition first time to determine i18n_voc_mode
  41. $i18n_voc_mode = 0;
  42. foreach ($conditions as $condition) {
  43. if (is_array($condition)) {
  44. // dsm($condition, 'conditon '.$condition['field']);
  45. if($condition['field'] == 't.vid'){
  46. $vid = $condition['value'];
  47. $i18n_voc_mode = i18n_taxonomy_vocabulary_mode($vid);
  48. // dsm($i18n_voc_mode, 'i18n_voc_mode');
  49. }
  50. }
  51. }
  52. # roll over conditions a second time to re-alter/fixe language selection
  53. # i18n vocabulary mode is 1 for localized terms
  54. if($i18n_voc_mode == 1){
  55. foreach ($conditions as $index => $condition) {
  56. if (is_array($condition)) {
  57. // dsm($condition, 'conditon '.$condition['field']);
  58. if($condition['field'] == 't.language' && $i18n_voc_mode == 1){
  59. $l = array('und');
  60. $ll = i18n_language_list();
  61. // dsm($ll, 'list language');
  62. foreach ($ll as $langcode => $langname) {
  63. $l[] = $langcode;
  64. }
  65. $conditions[$index]['value'] = $l;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Implements hook_menu().
  73. */
  74. function materio_taxonomy_menu() {
  75. $items['admin/structure/taxonomy/fr2en'] = array(
  76. 'title' => 'fr2en taglibres',
  77. 'page callback' => 'drupal_get_form',
  78. 'page arguments' => array('materio_taxonomy_fr2en_form'),
  79. 'access arguments' => array('access fr2en materio'),
  80. 'type' => MENU_NORMAL_ITEM,
  81. 'weight' => 999,
  82. );
  83. return $items;
  84. }
  85. function materio_taxonomy_fr2en_form(){
  86. $form['description'] = array(
  87. '#type' => 'markup',
  88. '#markup' => t('convert tag libres terms name frome french to english from csv file'),
  89. );
  90. $form['batch'] = array(
  91. '#type' => 'select',
  92. '#title' => 'Choose batch',
  93. '#options' => array(
  94. 'fr2en' => t('fr2en'),
  95. 'en2fr' => t('en2fr'),
  96. ),
  97. );
  98. $form['submit'] = array(
  99. '#type' => 'submit',
  100. '#value' => 'Go',
  101. );
  102. return $form;
  103. }
  104. function materio_taxonomy_fr2en_form_submit($form, &$form_state) {
  105. $function = 'materio_taxonomy_fr2en';
  106. // Reset counter for debug information.
  107. $_SESSION['http_request_count'] = 0;
  108. // Execute the function named batch_example_1 or batch_example_2.
  109. $batch = $function($form_state['values']['batch']);
  110. batch_set($batch);
  111. }
  112. function materio_taxonomy_fr2en($op){
  113. $file = drupal_get_path('module', 'materio_taxonomy')."/assets/taglibres_".$op.".csv";
  114. $data = array();
  115. if (($handle = fopen($file, "r")) !== FALSE) {
  116. while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) {
  117. $data[$line[0]] = $line[1];
  118. }
  119. fclose($handle);
  120. }else{
  121. drupal_set_message("fopen csv file failed");
  122. return;
  123. }
  124. // get term list from vocabulary
  125. $tags = taxonomy_get_tree(4);
  126. $operations = array();
  127. foreach ($tags as $tid => $t) {
  128. if($t->name){
  129. if(isset($data[$t->name])){
  130. $operations[] = array(
  131. 'materio_taxonomy_fr2en_batch',
  132. array(
  133. $data[$t->name],
  134. $t
  135. )
  136. );
  137. }
  138. }
  139. }
  140. $batch = array(
  141. "operations" => $operations,
  142. "finished" => "materio_taxonomy_fr2en_batch_finished"
  143. );
  144. return $batch;
  145. }
  146. function materio_taxonomy_fr2en_batch($name, $t, &$context){
  147. $oldname = $t->name;
  148. $t->name = $name;
  149. taxonomy_term_save($t);
  150. $cont= array('term',$t->tid,'name');
  151. i18n_string_textgroup('taxonomy')->update_translation($cont, 'fr', $oldname);
  152. $context['message'][] = $oldname .' -> '. $name;
  153. $_SESSION['http_request_count']++;
  154. }
  155. function materio_taxonomy_fr2en_batch_finished($success, $results, $operations){
  156. if($success){
  157. drupal_set_message('All terms converted');
  158. }
  159. }