materio_taxonomy.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Inutile si i18n_taxonomy module is desactivate
  38. */
  39. function materio_taxonomy_query_term_access_alter(QueryAlterableInterface $query) {
  40. // dsm($query, 'materio taxo query');
  41. $conditions =& $query->conditions();
  42. # roll over condition first time to determine i18n_voc_mode
  43. $i18n_voc_mode = 0;
  44. foreach ($conditions as $condition) {
  45. if (is_array($condition)) {
  46. // dsm($condition, 'conditon '.$condition['field']);
  47. if($condition['field'] == 't.vid'){
  48. $vid = $condition['value'];
  49. $i18n_voc_mode = module_exists('i18n_taxonomy') ? i18n_taxonomy_vocabulary_mode($vid) : 0;
  50. // dsm($i18n_voc_mode, 'i18n_voc_mode');
  51. }
  52. }
  53. }
  54. # roll over conditions a second time to re-alter/fixe language selection
  55. # i18n vocabulary mode is 1 for localized terms
  56. if($i18n_voc_mode == 1){
  57. foreach ($conditions as $index => $condition) {
  58. if (is_array($condition)) {
  59. // dsm($condition, 'conditon '.$condition['field']);
  60. if($condition['field'] == 't.language' && $i18n_voc_mode == 1){
  61. $l = array('und');
  62. $ll = i18n_language_list();
  63. // dsm($ll, 'list language');
  64. foreach ($ll as $langcode => $langname) {
  65. $l[] = $langcode;
  66. }
  67. $conditions[$index]['value'] = $l;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Implements hook_menu().
  75. */
  76. function materio_taxonomy_menu() {
  77. $items['admin/structure/taxonomy/fr2en'] = array(
  78. 'title' => 'fr2en taglibres',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('materio_taxonomy_fr2en_form'),
  81. 'access arguments' => array('access fr2en materio'),
  82. 'type' => MENU_NORMAL_ITEM,
  83. 'weight' => 999,
  84. );
  85. return $items;
  86. }
  87. function materio_taxonomy_fr2en_form(){
  88. $form['description'] = array(
  89. '#type' => 'markup',
  90. '#markup' => t('convert tag libres terms name frome french to english from csv file'),
  91. );
  92. $form['batch'] = array(
  93. '#type' => 'select',
  94. '#title' => 'Choose batch',
  95. '#options' => array(
  96. 'fr2en' => t('fr2en'),
  97. 'en2fr' => t('en2fr'),
  98. ),
  99. );
  100. $form['submit'] = array(
  101. '#type' => 'submit',
  102. '#value' => 'Go',
  103. );
  104. return $form;
  105. }
  106. function materio_taxonomy_fr2en_form_submit($form, &$form_state) {
  107. $function = 'materio_taxonomy_fr2en';
  108. // Reset counter for debug information.
  109. $_SESSION['http_request_count'] = 0;
  110. // Execute the function named batch_example_1 or batch_example_2.
  111. $batch = $function($form_state['values']['batch']);
  112. batch_set($batch);
  113. }
  114. function materio_taxonomy_fr2en($op){
  115. $file = drupal_get_path('module', 'materio_taxonomy')."/assets/taglibres_".$op.".csv";
  116. $data = array();
  117. if (($handle = fopen($file, "r")) !== FALSE) {
  118. while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) {
  119. $data[$line[0]] = $line[1];
  120. }
  121. fclose($handle);
  122. }else{
  123. drupal_set_message("fopen csv file failed");
  124. return;
  125. }
  126. // get term list from vocabulary
  127. $tags = taxonomy_get_tree(4);
  128. $operations = array();
  129. foreach ($tags as $tid => $t) {
  130. if($t->name){
  131. if(isset($data[$t->name])){
  132. $operations[] = array(
  133. 'materio_taxonomy_fr2en_batch',
  134. array(
  135. $data[$t->name],
  136. $t
  137. )
  138. );
  139. }
  140. }
  141. }
  142. $batch = array(
  143. "operations" => $operations,
  144. "finished" => "materio_taxonomy_fr2en_batch_finished"
  145. );
  146. return $batch;
  147. }
  148. function materio_taxonomy_fr2en_batch($name, $t, &$context){
  149. $oldname = $t->name;
  150. $t->name = $name;
  151. taxonomy_term_save($t);
  152. // $cont= array('term',$t->tid,'name');
  153. // i18n_string_textgroup('taxonomy')->update_translation($cont, 'fr', $oldname);
  154. // https://www.drupal.org/project/entity_translation_export_import
  155. $context['message'][] = $oldname .' -> '. $name;
  156. $_SESSION['http_request_count']++;
  157. }
  158. function materio_taxonomy_fr2en_batch_finished($success, $results, $operations){
  159. if($success){
  160. drupal_set_message('All terms converted');
  161. }
  162. }