materio_taxonomy.module 5.0 KB

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