taxonomy_csv.import.result.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * @file
  4. * Show export result messages.
  5. */
  6. /**
  7. * Invoke associated include file.
  8. */
  9. $module_dir = drupal_get_path('module', 'taxonomy_csv');
  10. require_once($module_dir . '/taxonomy_csv.result.inc');
  11. /**
  12. * Display result messages of import process.
  13. *
  14. * @param $options
  15. * Array. Same as taxonomy_csv_import.
  16. * @param $worst_level
  17. * Integer. Worst watchdog level of the process.
  18. * @param $summary
  19. * Translated string. Short summary of informations on process.
  20. * @param $results
  21. * Array. Batch process results.
  22. *
  23. * @return
  24. * Nothing.
  25. */
  26. function _taxonomy_csv_import_result($options, $worst_level, $summary, $results) {
  27. // Prepare info and warning messages.
  28. $messages = array();
  29. // Summary message.
  30. $messages[] = array($worst_level => $summary);
  31. // Prepare batch result message.
  32. if ($options['total_lines'] == 0) {
  33. $message = t('Notice: Input has no term to import.');
  34. $messages[] = array(WATCHDOG_NOTICE => $message);
  35. }
  36. // Only summary is displayed in case of error.
  37. elseif ($worst_level >= WATCHDOG_WARNING) {
  38. // General infos.
  39. $messages[] = array(WATCHDOG_INFO => _taxonomy_csv_info_chosen_options($options));
  40. // Check and info on used or created vocabularies.
  41. $messages[] = array(WATCHDOG_INFO => _taxonomy_csv_info_vocabulary($options));
  42. if (!$options['check_line']) {
  43. $messages[] = array(WATCHDOG_NOTICE => t('Line checks have been disabled. Some warnings and notices may have not been reported.'));
  44. }
  45. // Display stats and eventually lists about imported terms.
  46. if ($options['result_stats']
  47. || $options['result_terms']) {
  48. $messages[] = array(WATCHDOG_INFO => _taxonomy_csv_info_terms($options));
  49. }
  50. // Display messages about line process.
  51. if ($options['result_level'] == 'first') {
  52. $messages[] = array(WATCHDOG_NOTICE => t('No more information: choice is to report only first warning or notice if any.'));
  53. }
  54. else {
  55. // Display detailled result of import. First info was already displayed.
  56. $messages = array_merge($messages, _taxonomy_csv_info_lines($options, $results));
  57. }
  58. }
  59. _taxonomy_csv_message_result($messages);
  60. }
  61. /**
  62. * Return formatted list of chosen options.
  63. */
  64. function _taxonomy_csv_info_chosen_options($options) {
  65. if (!isset($options['source_choice'])) {
  66. $options['source_choice'] = 'file';
  67. }
  68. // Local file, distant file or text.
  69. switch ($options['source_choice']) {
  70. case 'file':
  71. $message = t('File "%file" uploaded.', array(
  72. '%file' => $options['file']->filename));
  73. break;
  74. case 'text':
  75. $message = t('Import terms from text.');
  76. break;
  77. case 'url':
  78. $message = t('File "<a href="!url">%file</a>" uploaded from an url.', array(
  79. '!url' => $options['url'],
  80. '%file' => basename($options['url'])));
  81. break;
  82. default:
  83. $message = t('Import from Api.');
  84. }
  85. // Content.
  86. $message .= '<br />' . t('Source content: "!import_format".', array(
  87. '!import_format' => $options['import_format'],
  88. )) . '<br />';
  89. // Existing items.
  90. $message .= t('Existing terms choice: "!import_items".', array(
  91. '!import_items' => $options['update_or_ignore'],
  92. )) . '<br />';
  93. // Internationalization.
  94. if ($options['import_format'] != TAXONOMY_CSV_FORMAT_TRANSLATE) {
  95. $message .= t('Language of terms: "!language".', array(
  96. '!language' => $options['language'],
  97. )) . '<br />';
  98. }
  99. // Custom locale.
  100. if ($options['locale_custom']) {
  101. $message .= t('Custom locale "!locale" has been used.', array(
  102. '!locale' => $options['locale_custom'],
  103. )) . '<br />';
  104. }
  105. return $message;
  106. }
  107. /**
  108. * Check created vocabularies and return formatted info on them.
  109. */
  110. function _taxonomy_csv_info_vocabulary($options) {
  111. $messages = array();
  112. // General info on used or created vocabularies.
  113. $vocabularies = array($options['vocabulary_id']);
  114. $messages[] = t('%count used or created.', array(
  115. '%count' => format_plural(count($vocabularies), 'A vocabulary has been', '@count vocabularies have been', array())
  116. ));
  117. foreach ($vocabularies as $vocabulary_id) {
  118. $message = '';
  119. // Check or update hierarchy of vocabularies.
  120. $vocabulary = taxonomy_vocabulary_load($vocabulary_id);
  121. if ($options['check_hierarchy']) {
  122. $result = taxonomy_check_vocabulary_hierarchy($vocabulary, array('tid' => 0));
  123. }
  124. // Set vocabulary hierarchy only if needed.
  125. else {
  126. if ($options['set_hierarchy'] != $vocabulary->hierarchy) {
  127. $vocabulary->hierarchy = $options['set_hierarchy'];
  128. $result = taxonomy_vocabulary_save($vocabulary);
  129. }
  130. $result = $options['set_hierarchy'];
  131. }
  132. $new_hierarchy[$vocabulary_id] = $result;
  133. // Display general info about vocabulary.
  134. $message .= _taxonomy_csv_info_vocabulary_destination($vocabulary, $options['vocabulary_target']);
  135. $message .= '<br />';
  136. if (isset($vocabulary->language) && module_exists('i18n_taxonomy')) {
  137. $message .= t('Language of vocabulary: "!language".', array(
  138. '!language' => $vocabulary->language,
  139. )) . '<br />';
  140. $language_mode = array(
  141. I18N_MODE_NONE => t('None'),
  142. I18N_MODE_LOCALIZE => t('Localize'),
  143. I18N_MODE_TRANSLATE => t('Translate'),
  144. I18N_MODE_MULTIPLE => t('Multiple'),
  145. I18N_MODE_LANGUAGE => t('Fixed language'),
  146. );
  147. $message .= t('Internationalization mode of vocabulary: "!language".', array(
  148. '!language' => $language_mode[$vocabulary->i18n_mode],
  149. )) . '<br />';
  150. if ($options['language'] != $vocabulary->language) {
  151. $message .= '<strong>' . t('Warning') . '</strong>: ' . t('Language of imported terms is not the language of the vocabulary. You may check multilingual options to see terms.') . '<br />';
  152. }
  153. }
  154. if ($options['delete_terms']) {
  155. $message .= t('Existing terms of the vocabulary have been deleted.') . '<br />';
  156. }
  157. if (!$options['check_hierarchy']) {
  158. $message .= t('Hierarchy level has been manually set.') . '<br />';
  159. }
  160. $message .= _taxonomy_csv_info_vocabulary_result($vocabulary, $options['vocabulary_target'], $new_hierarchy[$vocabulary_id]) . '<br />';
  161. $messages[] = $message;
  162. }
  163. return implode('<br />', $messages);
  164. }
  165. /**
  166. * Return informations about destination vocabulary.
  167. */
  168. function _taxonomy_csv_info_vocabulary_destination($vocabulary, $vocabulary_target) {
  169. // Destination vocabulary.
  170. $list_messages = array(
  171. 'autocreate' => t('New vocabulary "%vocabulary_name" has been created.', array(
  172. '%vocabulary_name' => $vocabulary->name,
  173. )),
  174. 'duplicate' => t('Duplicate vocabulary "%vocabulary_name" has been created.', array(
  175. '%vocabulary_name' => $vocabulary->name,
  176. )),
  177. 'existing' => t('Terms are imported into existing vocabulary "%vocabulary_name".', array(
  178. '%vocabulary_name' => $vocabulary->name,
  179. )),
  180. );
  181. return $list_messages[$vocabulary_target];
  182. }
  183. /**
  184. * Return informations about result vocabulary.
  185. */
  186. function _taxonomy_csv_info_vocabulary_result($vocabulary, $vocabulary_target, $new_hierarchy) {
  187. $message = '';
  188. // Level of vocabulary.
  189. $hierarchy_text = array(
  190. 0 => t('no parent (flat)'),
  191. 1 => t('single parent (tree)'),
  192. 2 => t('multiple parents (polyhierarchy)'),
  193. );
  194. if ($vocabulary_target != 'autocreate') {
  195. $message .= t('Old hierarchy level was !level (!level_text).', array(
  196. '!level' => $vocabulary->hierarchy,
  197. '!level_text' => $hierarchy_text[$vocabulary->hierarchy],
  198. )) . '<br />';
  199. }
  200. $message .= t('Hierarchy level is !level (!level_text).', array(
  201. '!level' => $new_hierarchy,
  202. '!level_text' => $hierarchy_text[$new_hierarchy],
  203. )) . '<br />';
  204. // Direct links to edit vocabulary and view terms.
  205. $message .= t('Properties can be edited at <a href="!vocabulary_edit_link">Administer > Structure > Taxonomy > edit vocabulary</a>.', array(
  206. '!vocabulary_edit_link' => url("admin/structure/taxonomy/$vocabulary->machine_name/edit"),
  207. )) . '<br />';
  208. $message .= t('You can view terms at <a href="!vocabulary_list">Administer > Structure > Taxonomy > list terms</a>.', array(
  209. '!vocabulary_list' => url("admin/structure/taxonomy/$vocabulary->machine_name"),
  210. ));
  211. return $message;
  212. }
  213. /**
  214. * Display stats and eventually lists of terms about imported terms.
  215. *
  216. * Currently, display only total and list of imported terms.
  217. */
  218. function _taxonomy_csv_info_terms($options) {
  219. if (isset($options['imported_terms'])) {
  220. $tids = array();
  221. foreach ($options['imported_terms'] as $imported_tids) {
  222. $tids = array_unique(array_merge($tids, $imported_tids));
  223. }
  224. $message = '';
  225. // Display basic stats.
  226. if ($options['result_stats']) {
  227. // Display total of imported terms.
  228. $message .= t('Total created or updated terms : !count.', array('!count' => count($tids))) . '<br />';
  229. }
  230. // Display list of terms.
  231. if ($options['result_terms']) {
  232. $terms = taxonomy_term_load_multiple($tids);
  233. $term_list = array();
  234. foreach ($terms as $term) {
  235. $term_list[] = $term->name;
  236. }
  237. $message .= '"' . implode('", "', $term_list) . '".';
  238. }
  239. }
  240. else {
  241. $message = t('No term was imported.');
  242. }
  243. return $message;
  244. }
  245. /**
  246. * Display detailled result of import.
  247. */
  248. function _taxonomy_csv_info_lines($options, $results) {
  249. $messages = array();
  250. if (count($results)) {
  251. $messages[] = array(WATCHDOG_INFO => t('Available informations about lines import.'));
  252. switch ($options['result_type']) {
  253. case 'by_line':
  254. $messages = array_merge($messages, _taxonomy_csv_message_result_by_line($results));
  255. break;
  256. case 'by_message':
  257. $messages = array_merge($messages, _taxonomy_csv_message_result_by_message($results));
  258. break;
  259. }
  260. }
  261. else {
  262. $messages[] = array(WATCHDOG_INFO => t('No more information reported about lines import.'));
  263. }
  264. return $messages;
  265. }