taxonomy_csv.api.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @file
  4. * Manage variables and features of module.
  5. *
  6. * More infos (schemas of file, use as an api, etc.) in TECHINFO.txt.
  7. */
  8. /**
  9. * Available internal import/export formats.
  10. */
  11. define('TAXONOMY_CSV_FORMAT_STRUCTURE', 'structure');
  12. define('TAXONOMY_CSV_FORMAT_FLAT', 'flat');
  13. define('TAXONOMY_CSV_FORMAT_TREE', 'tree');
  14. define('TAXONOMY_CSV_FORMAT_POLYHIERARCHY', 'polyhierarchy');
  15. define('TAXONOMY_CSV_FORMAT_FIELDS', 'fields');
  16. define('TAXONOMY_CSV_FORMAT_TRANSLATE', 'translate');
  17. /**
  18. * Available import options.
  19. */
  20. define('TAXONOMY_CSV_EXISTING_UPDATE', 'update');
  21. define('TAXONOMY_CSV_EXISTING_IGNORE', 'ignore');
  22. // Internal use only.
  23. define('TAXONOMY_CSV_EXISTING_IGNORE_PREVIOUS', 'ignore_previous');
  24. /**
  25. * List of process levels matching watchdog levels.
  26. *
  27. * See _taxonomy_csv_message_watchdog_type and _taxonomy_csv_message_text.
  28. */
  29. define('TAXONOMY_CSV_PROCESS_ERROR', 300); // Stop import process.
  30. define('TAXONOMY_CSV_PROCESS_WARNING', 400); // Stop line process and go next.
  31. define('TAXONOMY_CSV_PROCESS_NOTICE', 500); // Continue current line process.
  32. define('TAXONOMY_CSV_PROCESS_INFO', 600); // Successfully processed.
  33. define('TAXONOMY_CSV_PROCESS_DEBUG', 700); // Internal use only.
  34. /**
  35. * Information about import process.
  36. *
  37. * Use too default Drupal constants:
  38. * - SAVED_NEW = 1
  39. * - SAVED_UPDATED = 2
  40. * Possibly use of:
  41. * - SAVED_DELETED = 3
  42. */
  43. define('TAXONOMY_CSV_ERROR', 0);
  44. define('TAXONOMY_CSV_NEW_UPDATED', 4);
  45. define('TAXONOMY_CSV_UNCHANGED', 5);
  46. /**
  47. * Helper to remember some items and to describe features.
  48. *
  49. * @param $list
  50. * A string matching list to be returned:
  51. * - 'import_format' : available formats for import.
  52. * - 'export_format' : available formats for export.
  53. * - 'import_default_ui' : default options to import by user interface.
  54. * - 'import_default_api': default options to import by api.
  55. * - 'export_default_ui' : default options to export by user interface.
  56. * - 'export_default_api': default options to export by api.
  57. *
  58. * @return
  59. * Array of wanted content.
  60. */
  61. function _taxonomy_csv_values($list) {
  62. switch ($list) {
  63. case 'import_format':
  64. return array(
  65. TAXONOMY_CSV_FORMAT_STRUCTURE => t('Structure'),
  66. TAXONOMY_CSV_FORMAT_FIELDS => t('Fields'),
  67. TAXONOMY_CSV_FORMAT_TRANSLATE => t('Translation'),
  68. );
  69. case 'export_format':
  70. return array(
  71. TAXONOMY_CSV_FORMAT_FLAT => t('Term names'),
  72. TAXONOMY_CSV_FORMAT_TREE => t('Hierarchical tree structure'),
  73. TAXONOMY_CSV_FORMAT_FIELDS => t('Fields'),
  74. TAXONOMY_CSV_FORMAT_TRANSLATE => t('Translation'),
  75. );
  76. case 'import_default_ui':
  77. return array(
  78. 'import_format' => TAXONOMY_CSV_FORMAT_STRUCTURE,
  79. 'structure_type' => TAXONOMY_CSV_FORMAT_FLAT,
  80. 'import_fields_format' => 'name',
  81. 'translate_by' => 'name',
  82. 'translate_languages' => '',
  83. 'keep_order' => FALSE,
  84. 'source_choice' => 'text',
  85. 'import_delimiter' => 'comma',
  86. 'import_delimiter_soft_tab_width' => '2',
  87. 'import_delimiter_custom' => '',
  88. 'import_enclosure' => 'none',
  89. 'import_enclosure_custom' => '',
  90. 'filter_format' => 'plain_text',
  91. 'filter_format_custom' => 'none',
  92. 'import_language' => 'und', // Undefined.
  93. 'check_line' => TRUE,
  94. 'check_utf8' => TRUE,
  95. 'locale_custom' => '',
  96. 'vocabulary_target' => 'autocreate',
  97. 'vocabulary_id' => 'choose_vocabulary',
  98. 'i18n_mode' => 0, // I18N_MODE_NONE.
  99. 'language' => 'und', // Undefined.
  100. 'import_fields_custom' => '',
  101. 'delete_terms' => FALSE,
  102. 'check_hierarchy' => TRUE,
  103. 'set_hierarchy' => 2, // Polyhierarchy.
  104. 'update_or_ignore' => TAXONOMY_CSV_EXISTING_UPDATE,
  105. // General options.
  106. 'result_stats' => 'result_stats',
  107. 'result_terms' => 'result_terms',
  108. 'result_level' => 'notices',
  109. 'result_type' => 'by_message',
  110. );
  111. case 'import_default_api':
  112. return array(
  113. 'import_format' => TAXONOMY_CSV_FORMAT_FLAT,
  114. 'fields_format' => array(0 => 'name'),
  115. 'translate_by' => 'name',
  116. 'translate_languages' => '',
  117. 'keep_order' => FALSE,
  118. 'delimiter' => ',',
  119. 'enclosure' => '',
  120. 'filter_format' => 'plain_text',
  121. 'filter_format_custom' => 'none',
  122. // Warning: in API, language is option for terms.
  123. 'language' => 'und', // Undefined.
  124. 'check_line' => FALSE,
  125. 'check_utf8' => FALSE,
  126. 'locale_custom' => '',
  127. 'vocabulary_target' => 'autocreate',
  128. 'vocabulary_id' => 'choose_vocabulary',
  129. 'i18n_mode' => 0, // I18N_MODE_NONE.
  130. 'vocabulary_language' => 'und', // Undefined.
  131. 'fields_custom' => array(),
  132. 'delete_terms' => FALSE,
  133. 'check_hierarchy' => TRUE,
  134. 'set_hierarchy' => 2, // Polyhierarchy.
  135. 'update_or_ignore' => TAXONOMY_CSV_EXISTING_UPDATE,
  136. // General options.
  137. 'check_options' => FALSE,
  138. 'result_display' => FALSE,
  139. 'result_stats' => FALSE,
  140. 'result_terms' => FALSE,
  141. 'result_level' => 'first',
  142. 'result_type' => 'by_message',
  143. );
  144. case 'export_default_ui':
  145. return array(
  146. 'export_format' => TAXONOMY_CSV_FORMAT_FLAT,
  147. 'export_fields_format' => 'name',
  148. 'export_vocabulary_id' => 0,
  149. 'export_delimiter' => 'comma',
  150. 'export_delimiter_custom' => '',
  151. 'export_enclosure' => 'quotation',
  152. 'export_enclosure_custom' => '',
  153. 'export_line_ending' => 'Unix',
  154. // Default options of specific imports.
  155. 'def_links_terms_ids' => 'name_if_needed',
  156. 'def_links_vocabularies_ids' => 'none',
  157. // General options.
  158. 'export_order' => 'name',
  159. 'result_duplicates' => TRUE,
  160. );
  161. case 'export_default_api':
  162. return array(
  163. 'export_format' => TAXONOMY_CSV_FORMAT_FLAT,
  164. 'fields_format' => array(0 => 'name'),
  165. 'vocabulary_id' => 0,
  166. 'delimiter' => ',',
  167. 'enclosure' => '"',
  168. 'line_ending' => 'Unix',
  169. 'order' => 'name',
  170. // Default options of specific imports.
  171. 'def_links_terms_ids' => 'name_if_needed',
  172. 'def_links_vocabularies_ids' => 'none',
  173. // General options.
  174. 'result_duplicates' => FALSE,
  175. 'check_options' => FALSE,
  176. 'result_display' => FALSE,
  177. );
  178. }
  179. }
  180. /**
  181. * Returns worst message of a set of message codes.
  182. *
  183. * @todo Move into another included file or remove it.
  184. *
  185. * @param $messages
  186. * Array of message code (000 to 799).
  187. *
  188. * @return
  189. * Worst message code.
  190. */
  191. function _taxonomy_csv_worst_message($messages) {
  192. return count($messages) ?
  193. min($messages) :
  194. 799;
  195. }
  196. /**
  197. * Escapes carriage return and linefeed.
  198. *
  199. * This function is used for description field of terms and allows to get only
  200. * one csv line for one term.
  201. *
  202. * @param $string
  203. * String to update.
  204. *
  205. * @return
  206. * Updated string.
  207. */
  208. function _taxonomy_csv_escape_line_break($string) {
  209. return str_replace(
  210. array("\r", "\n"),
  211. array('\r', '\n'),
  212. $string);
  213. }
  214. /**
  215. * Remove escapes carriage return and linefeed.
  216. *
  217. * This function is used for description field of terms and allows to import a
  218. * multiline text.
  219. *
  220. * @param $string
  221. * String to update.
  222. *
  223. * @return
  224. * Updated string.
  225. */
  226. function _taxonomy_csv_set_line_break($string) {
  227. return str_replace(
  228. array('\r', '\n'),
  229. array("\r", "\n"),
  230. $string);
  231. }