user_import.install 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @file
  4. * Import and update users from a comma separated file (csv).
  5. */
  6. /**
  7. * Implementation of hook_schema().
  8. */
  9. function user_import_schema() {
  10. $schema['user_import'] = array(
  11. 'description' => t("Settings for each import, and import setting templates."),
  12. 'fields' => array(
  13. 'import_id' => array(
  14. 'description' => t("ID key of import or template."),
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. 'disp-width' => '10'
  19. ),
  20. 'name' => array(
  21. 'description' => t("Label of import template, only used if this is an import template."),
  22. 'type' => 'varchar',
  23. 'length' => '25',
  24. 'not null' => TRUE,
  25. 'default' => ''
  26. ),
  27. 'auto_import_directory' => array(
  28. 'description' => t("Name of directory associated with an import template."),
  29. 'type' => 'varchar',
  30. 'length' => '255',
  31. 'not null' => TRUE,
  32. 'default' => ''
  33. ),
  34. 'filename' => array(
  35. 'description' => t("Name of file being used as source of data for import."),
  36. 'type' => 'varchar',
  37. 'length' => '50',
  38. 'not null' => TRUE,
  39. 'default' => ''
  40. ),
  41. 'oldfilename' => array(
  42. 'description' => t("Original name of file being used as source of data for import."),
  43. 'type' => 'varchar',
  44. 'length' => '50',
  45. 'not null' => TRUE,
  46. 'default' => ''
  47. ),
  48. 'filepath' => array(
  49. 'description' => t("Path to file being used as source of data for import."),
  50. 'type' => 'text',
  51. 'size' => 'small',
  52. 'not null' => TRUE
  53. ),
  54. 'started' => array(
  55. 'description' => t("Datestamp of when import was started."),
  56. 'type' => 'int',
  57. 'not null' => TRUE,
  58. 'default' => 0,
  59. 'disp-width' => '11'
  60. ),
  61. 'pointer' => array(
  62. 'description' => t("Pointer to where test/import last finished."),
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'default' => 0,
  66. 'disp-width' => '10'
  67. ),
  68. 'processed' => array(
  69. 'description' => t("Number of users processed by import."),
  70. 'type' => 'int',
  71. 'not null' => TRUE,
  72. 'default' => 0,
  73. 'disp-width' => '10'
  74. ),
  75. 'valid' => array(
  76. 'description' => t("Number of users processed without errors."),
  77. 'type' => 'int',
  78. 'not null' => TRUE,
  79. 'default' => 0,
  80. 'disp-width' => '10'
  81. ),
  82. 'field_match' => array(
  83. 'description' => t("Settings for how data matches to Drupal fields."),
  84. 'type' => 'text',
  85. 'size' => 'big',
  86. 'not null' => TRUE,
  87. 'serialize' => TRUE
  88. ),
  89. 'roles' => array(
  90. 'description' => t("Roles to give imported users."),
  91. 'type' => 'text',
  92. 'size' => 'big',
  93. 'not null' => TRUE,
  94. 'serialize' => TRUE
  95. ),
  96. 'options' => array(
  97. 'description' => t("Store of all other options for import. Most of the other settings in this table will be moved into here in future."),
  98. 'type' => 'text',
  99. 'size' => 'big',
  100. 'not null' => TRUE,
  101. 'serialize' => TRUE
  102. ),
  103. 'setting' => array(
  104. 'description' => t("Status of import, or whether it is an import template."),
  105. 'type' => 'varchar',
  106. 'length' => '10',
  107. 'not null' => TRUE,
  108. 'default' => ''
  109. )
  110. ),
  111. 'primary key' => array('import_id'),
  112. );
  113. $schema['user_import_errors'] = array(
  114. 'description' => t("Record of errors encountered during an import."),
  115. 'fields' => array(
  116. 'import_id' => array(
  117. 'description' => t("ID key of import or template."),
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. 'disp-width' => '10'
  122. ),
  123. 'data' => array(
  124. 'description' => t("Data (matched to fields) for user that failed to import due to error."),
  125. 'type' => 'text',
  126. 'size' => 'big',
  127. 'not null' => TRUE,
  128. 'serialize' => TRUE
  129. ),
  130. 'errors' => array(
  131. 'description' => t("Error(s) encountered for user that failed to import."),
  132. 'type' => 'text',
  133. 'size' => 'big',
  134. 'not null' => TRUE,
  135. 'serialize' => TRUE
  136. )
  137. ),
  138. 'indexes' => array(
  139. 'import_id' => array('import_id')
  140. ),
  141. );
  142. return $schema;
  143. }
  144. /**
  145. * Implementation of hook_install().
  146. */
  147. function user_import_install() {
  148. // Add a new mail system for HTML emails.
  149. $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  150. $mail_system['user_import'] = 'UserImportMailSystem';
  151. variable_set('mail_system', $mail_system);
  152. }
  153. /**
  154. * Implementation of hook_uninstall().
  155. */
  156. function user_import_uninstall() {
  157. variable_del('user_import_settings');
  158. variable_del('user_import_max');
  159. variable_del('user_import_line_max');
  160. variable_del('user_export_checked_usernames');
  161. variable_del('user_import_profile_date_format');
  162. //
  163. $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  164. unset($mail_system['user_import']);
  165. variable_set('mail_system', $mail_system);
  166. }
  167. function user_import_update_1() {
  168. $ret = array();
  169. _system_update_utf8(array('user_import', 'user_import_errors'));
  170. return $ret;
  171. }
  172. function user_import_update_2() {
  173. $ret = array();
  174. db_add_column($ret, 'user_import', 'options', 'longtext');
  175. return $ret;
  176. }
  177. function user_import_update_3() {
  178. $ret = array();
  179. db_drop_primary_key($ret, 'user_import');
  180. db_change_field($ret, 'user_import', 'iid', 'import_id', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => '10'), array('primary key' => array('import_id')));
  181. db_change_field($ret, 'user_import', 'first_line', 'first_line_skip', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'), array('primary key' => array('import_id')));
  182. db_drop_index($ret, 'user_import_errors', 'import_id');
  183. db_change_field($ret, 'user_import_errors', 'iid', 'import_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'));
  184. db_add_index($ret, 'user_import_errors', 'import_id', array('import_id'));
  185. return $ret;
  186. }
  187. function user_import_update_4() {
  188. $ret = array();
  189. db_drop_index($ret, 'user_import_errors', 'import_id');
  190. db_change_field($ret, 'user_import_errors', 'error', 'errors', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'serialize' => TRUE));
  191. db_add_index($ret, 'user_import_errors', 'import_id', array('import_id'));
  192. return $ret;
  193. }
  194. function user_import_update_5() {
  195. $ret = array();
  196. db_drop_index($ret, 'user_import_errors', 'import_id');
  197. db_change_field($ret, 'user_import_errors', 'errors', 'errors', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'serialize' => TRUE));
  198. db_add_index($ret, 'user_import_errors', 'import_id', array('import_id'));
  199. return $ret;
  200. }
  201. function user_import_update_6001() {
  202. // Rebuild schema cache
  203. drupal_get_schema('user_import', TRUE);
  204. return array();
  205. }
  206. /**
  207. * Move settings into the 'options' column.
  208. */
  209. function user_import_update_6002() {
  210. $ret = array();
  211. $result = db_query("SELECT * FROM {user_import}");
  212. // Update each import.
  213. while ($import = db_fetch_array($result)) {
  214. $options = unserialize($import['options']);
  215. $options['first_line_skip'] = $import['first_line_skip'];
  216. $options['contact'] = $import['contact'];
  217. $options['username_space'] = $import['username_space'];
  218. $options['send_email'] = $import['send_email'];
  219. //Avoid using update_sql() as it has issues with serialized data.
  220. db_query("UPDATE {user_import} SET options = '%s' WHERE import_id = %d", serialize($options), $import['import_id']);
  221. }
  222. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN first_line_skip');
  223. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN contact');
  224. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN username_space');
  225. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN send_email');
  226. return $ret;
  227. }
  228. /**
  229. * Change the Roles column to LOMGTEXT.
  230. */
  231. function user_import_update_6003() {
  232. $ret = array();
  233. db_change_field($ret, 'user_import', 'roles', 'roles', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'serialize' => TRUE));
  234. return $ret;
  235. }
  236. /**
  237. * Add database field to store the name of a directory associated with an import template.
  238. *
  239. **/
  240. function user_import_update_7200(&$sandbox) {
  241. $field = array(
  242. 'description' => t("Name of directory associated with an import template."),
  243. 'type' => 'varchar',
  244. 'length' => '255',
  245. 'not null' => TRUE,
  246. 'default' => '',
  247. );
  248. db_add_field('user_import', 'auto_import_directory', $field);
  249. }
  250. /**
  251. * Add a new mail system for HTML emails.
  252. */
  253. function user_import_update_7201() {
  254. $ret = array();
  255. $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  256. $mail_system['user_import'] = 'UserImportMailSystem';
  257. variable_set('mail_system', $mail_system);
  258. return $ret;
  259. }