user_import.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. <?php
  2. /**
  3. * @file
  4. * Import or update users with data from a comma separated file (csv).
  5. */
  6. // Update options for existing users
  7. define ('UPDATE_NONE', 0);
  8. define ('UPDATE_REPLACE', 1);
  9. define ('UPDATE_ADD', 2);
  10. /**
  11. * - - - - - - - - HOOKS - - - - - - - -
  12. */
  13. /**
  14. * Implementation of hook_theme().
  15. */
  16. function user_import_theme() {
  17. return array(
  18. 'user_import_list' => array(
  19. 'variables' => array(),
  20. ),
  21. 'user_import_edit' => array(
  22. 'render element' => 'form',
  23. ),
  24. 'user_import_errors_display' => array(
  25. 'variables' => array('settings' => NULL),
  26. ),
  27. 'user_import_username_errors' => array(
  28. 'variables' => array('errors' => NULL),
  29. ),
  30. );
  31. }
  32. /**
  33. * Implementation of hook_help().
  34. */
  35. // function user_import_help($path, $arg) {
  36. // switch ($path) {
  37. // case 'admin/people/user_import':
  38. // return t("Import or update users from a comma separated file (csv). Click 'Import' to start a new import.");
  39. // }
  40. // }
  41. /**
  42. * Implementation of hook_perm().
  43. */
  44. function user_import_permission() {
  45. return array(
  46. 'import users' => array(
  47. 'title' => t('Import users'),
  48. 'description' => t('Import users.'),
  49. ),
  50. 'limited user import' => array(
  51. 'title' => t('Import users Reports'),
  52. 'description' => t('View import error reports.'),
  53. ),
  54. );
  55. }
  56. /**
  57. * Implementation of hook_menu().
  58. */
  59. function user_import_menu() {
  60. $items['admin/people/user_import'] = array(
  61. 'title' => 'Import',
  62. 'description' => 'Import or update users from a comma separated file (csv).',
  63. 'page callback' => 'user_import_list',
  64. 'access arguments' => array('import users'),
  65. 'type' => MENU_LOCAL_TASK,
  66. 'file' => 'user_import.admin.inc',
  67. );
  68. $items['admin/people/user_import/list'] = array(
  69. 'title' => 'List Imports',
  70. 'access arguments' => array('import users'),
  71. 'weight' => -10,
  72. 'type' => MENU_DEFAULT_LOCAL_TASK,
  73. 'file' => 'user_import.admin.inc',
  74. );
  75. $items['admin/people/user_import/add'] = array(
  76. 'title' => 'New Import',
  77. 'page callback' => 'user_import_preferences',
  78. 'access arguments' => array('import users'),
  79. 'weight' => -5,
  80. 'type' => MENU_LOCAL_TASK,
  81. 'file' => 'user_import.admin.inc',
  82. );
  83. $items['admin/people/user_import/continue/%'] = array(
  84. 'title' => 'Continue',
  85. 'page callback' => 'drupal_get_form',
  86. 'page arguments' => array('user_import_confirm_continue', 4),
  87. 'access arguments' => array('import users'),
  88. 'type' => MENU_CALLBACK,
  89. 'file' => 'user_import.admin.inc',
  90. );
  91. $items['admin/people/user_import/import/%'] = array(
  92. 'title' => 'Import',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('user_import_confirm_import', 4),
  95. 'access arguments' => array('import users'),
  96. 'type' => MENU_CALLBACK,
  97. 'file' => 'user_import.admin.inc',
  98. );
  99. $items['admin/people/user_import/delete/%'] = array(
  100. 'title' => 'Delete Import',
  101. 'page callback' => 'drupal_get_form',
  102. 'page arguments' => array('user_import_confirm_delete', 4),
  103. 'access arguments' => array('import users'),
  104. 'type' => MENU_CALLBACK,
  105. 'file' => 'user_import.admin.inc',
  106. );
  107. $items['admin/people/user_import/configure'] = array(
  108. 'title' => 'Configure',
  109. 'page callback' => 'drupal_get_form',
  110. 'page arguments' => array('user_import_configure_form'),
  111. 'access arguments' => array('import users'),
  112. 'type' => MENU_LOCAL_TASK,
  113. 'file' => 'user_import.admin.inc',
  114. );
  115. $items['admin/people/user_import/errors/%'] = array(
  116. 'title' => 'Import Errors',
  117. 'page callback' => 'user_import_limited_errors',
  118. 'page arguments' => array(4),
  119. 'type' => MENU_CALLBACK,
  120. 'access arguments' => array('limited user import'),
  121. );
  122. return $items;
  123. }
  124. /**
  125. * Implementation of hook_cron().
  126. */
  127. function user_import_cron() {
  128. module_load_include('inc', 'user_import', 'user_import.import');
  129. // Continue any on-going imports.
  130. user_import_continue_imports();
  131. // Check for new imports.
  132. user_import_trigger_imports();
  133. }
  134. /**
  135. * Continue any on-going imports, durring a cron run.
  136. *
  137. **/
  138. function user_import_continue_imports() {
  139. $imports = _user_import_settings_select();
  140. if ($imports) {
  141. foreach ($imports as $import) {
  142. if ($import['setting'] == 'test' || $import['setting'] == 'import') {
  143. _user_import_process($import);
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Trigger imports if new files are found durring a cron run.
  150. */
  151. function user_import_trigger_imports() {
  152. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  153. if (empty($auto_imports_enabled)) {
  154. return;
  155. }
  156. // Load import functions.
  157. module_load_include('inc', 'user_import', 'user_import.admin');
  158. // Get list of templates to check.
  159. $imports = db_query("SELECT * FROM {user_import} WHERE auto_import_directory != '' AND setting = 'template'");
  160. foreach ($imports as $import) {
  161. // Check for file in the uploads directory of this template.
  162. $directory = 'private://user_import/uploads/' . $import->auto_import_directory;
  163. $files = file_scan_directory($directory, '/.*$/');
  164. foreach ($files as $import_file) {
  165. // Move file to processing directory.
  166. $filename_new = $import_file->filename . '-' . rand(1000000, 2000000);
  167. $import_file_new = file_unmanaged_move($import_file->uri, 'private://user_import/processing/' . $filename_new);
  168. // Create import.
  169. // Get template.
  170. $settings = _user_import_settings_select($import->import_id);
  171. $import_id = '';
  172. $name = '';
  173. $pointer = 0;
  174. $processed = 0;
  175. $valid = 0;
  176. $field_match = isset($settings['field_match']) ? serialize($settings['field_match']) : '';
  177. $roles = isset($settings['roles']) ? serialize($settings['roles']) : '';
  178. $options = isset($settings['options']) ? serialize($settings['options']) : '';
  179. $setting = 'import';
  180. $file->filename = $filename_new;
  181. $file->oldfilename = $import_file->filename;
  182. $file->filepath = 'private://user_import/processing/' . $filename_new;
  183. $import_id = user_import_import_set($name, $file, $pointer, $processed, $valid, $field_match, $roles, $options, $setting, $import_id);
  184. $settings = _user_import_settings_select($import_id);
  185. _user_import_process($settings);
  186. }
  187. }
  188. return;
  189. // Get template.
  190. $settings = _user_import_settings_select(2);
  191. foreach ($files as $filename) {
  192. if ($filename == 'sample.txt') {
  193. // Check if it's used for an import already.
  194. $imported = db_query('SELECT import_id FROM {user_import} WHERE filename = :filename', array(':filename' => $filename))->fetchField();
  195. if (!$imported) {
  196. $import_id = '';
  197. $name = '';
  198. $pointer = 0;
  199. $processed = 0;
  200. $valid = 0;
  201. $field_match = isset($settings['field_match']) ? serialize($settings['field_match']) : '';
  202. $roles = isset($settings['roles']) ? serialize($settings['roles']) : '';
  203. $options = isset($settings['options']) ? serialize($settings['options']) : '';
  204. $setting = 'import';
  205. $file->filename = $filename;
  206. $file->oldfilename = $filename;
  207. $file->filepath = drupal_get_path('module', 'user_import') . '/' . $filename;
  208. $import_id = user_import_import_set($name, $file, $pointer, $processed, $valid, $field_match, $roles, $options, $setting, $import_id);
  209. $settings = _user_import_settings_select($import_id);
  210. _user_import_process($settings);
  211. }
  212. }
  213. }
  214. return;
  215. }
  216. // - - - - - - - - FORMS - - - - - - - -
  217. /**
  218. * Saves options on content type configuration form
  219. * @todo check if this is cruft
  220. * @todo check $form['type']
  221. */
  222. function user_import_content_type_submit($form, &$form_state) {
  223. // user import template for Organic Groups content type
  224. $templates = variable_get('user_import_og_template', array());
  225. $templates[$form['type']] = $form_state['values']['user_import_og'];
  226. variable_set('user_import_og_template', $templates);
  227. }
  228. // - - - - - - - - PAGES - - - - - - - -
  229. function user_import_limited_errors($import_id = NULL, $template_id = NULL) {
  230. if (empty($import_id) || !is_numeric($import_id)) {
  231. drupal_goto('admin/people/user_import/' . $template_id);
  232. }
  233. $pager_id = 1;
  234. $max = 25;
  235. $import = _user_import_settings_select($import_id);
  236. $output = '';
  237. $file_lines = array();
  238. $total = db_query('SELECT count(data) FROM {user_import_errors} WHERE import_id = :import_id', array(':import_id' => $import_id))->fetchField();
  239. if (empty($total)) {
  240. $output .= theme('There were no import errors');
  241. }
  242. else {
  243. $header = array(
  244. array('data' => t('ID'), 'field' => 'import_id', 'sort' => 'desc'),
  245. array('data' => t('Data'), 'field' => 'data'),
  246. array('data' => t('Errors'), 'field' => 'errors')
  247. );
  248. $query = db_select('user_import_errors', 'uie')
  249. ->fields('uie')
  250. ->extend('PagerDefault')
  251. ->extend('TableSort');
  252. $query
  253. ->condition('import_id', $import_id, '=')
  254. ->limit($max)
  255. ->orderByHeader($header);
  256. $result = $query->execute();
  257. foreach ($result as $line) {
  258. $file_lines[] = array('data' => unserialize($line->data), 'errors' => unserialize($line->errors));
  259. }
  260. $output .= theme('user_import_errors_display', array('import' => $import, 'file_lines' => $file_lines, 'total' => $total));
  261. }
  262. $output .= l(t('Return'), "admin/people/user_import");
  263. return $output;
  264. }
  265. /**
  266. * @param null $import_id
  267. * @param null $template_id
  268. */
  269. function user_import_limited_delete($import_id = NULL, $template_id = NULL) {
  270. module_load_include('inc', 'user_import', 'user_import.admin');
  271. user_import_delete($import_id, "admin/people/user_import");
  272. }
  273. // - - - - - - - - THEMES - - - - - - - -
  274. /**
  275. * @return string
  276. */
  277. function theme_user_import_list() {
  278. $output = '';
  279. $imports = _user_import_settings_select();
  280. if (!$imports) return ' ';
  281. foreach ($imports as $import) {
  282. // header labels
  283. $import_label = ($import['setting'] == 'tested' || $import['setting'] == 'test') ? t('importable') : t('imported');
  284. $header = array(t('file'), t('started'), t('processed'), $import_label, t('errors'), t('status'));
  285. // info row
  286. $errors = db_query('SELECT COUNT(import_id) FROM {user_import_errors} WHERE import_id = :import_id', array(':import_id' => $import['import_id']))->fetchField();
  287. $errors_link = ($errors == 0) ? '0' : l($errors, 'admin/people/user_import/errors/' . $import['import_id']);
  288. $rows[0] = array(
  289. check_plain($import['oldfilename']),
  290. format_date($import['started'], 'small'),
  291. array("data" => $import['processed'], "align" => 'center'),
  292. array("data" => $import['valid'], "align" => 'center'),
  293. array("data" => $errors_link, "align" => 'center'),
  294. $import['setting'],
  295. );
  296. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  297. // action buttons
  298. $delete_link = l(t('Delete'), 'admin/people/user_import/delete/' . $import['import_id']);
  299. $continue_link = l(t('Continue Processing'), 'admin/people/user_import/continue/' . $import['import_id']);
  300. $import_link = l(t('Import'), 'admin/people/user_import/import/' . $import['import_id']);
  301. $output .= $delete_link;
  302. if ($import['setting'] == 'tested' || $import['setting'] == 'test') $output .= ' | ' . $import_link;
  303. if ($import['setting'] == 'test' || $import['setting'] == 'import') $output .= ' | ' . $continue_link;
  304. }
  305. return $output;
  306. }
  307. function theme_user_import_edit($variables) {
  308. $output = '';
  309. $rows = array();
  310. $form = $variables['form'];
  311. $header = array(t('CSV column'), t('Drupal fields'), t('Username'), t('Abbreviate'));
  312. foreach (element_children($form['field_match']) as $key) {
  313. $rows[] = array(
  314. drupal_render($form['field_match'][$key]['csv']),
  315. drupal_render($form['field_match'][$key]['field_match']),
  316. drupal_render($form['field_match'][$key]['username']),
  317. drupal_render($form['field_match'][$key]['abbreviate']),
  318. );
  319. }
  320. $form['field_match']['#value'] = theme('table', array('header' => $header, 'rows' => $rows));
  321. if (isset($form['remove'])) {
  322. $output .= drupal_render($form['remove']);
  323. }
  324. if (isset($form['options'])) {
  325. $output .= drupal_render($form['options']);
  326. }
  327. if (isset($form['field_match'])) {
  328. $output .= drupal_render($form['field_match']);
  329. }
  330. $output .= drupal_render_children($form);
  331. return $output;
  332. }
  333. function theme_user_import_errors_display($settings) {
  334. $output = '';
  335. $header[0] = t('Email Address');
  336. $data = $settings['file_lines'];
  337. $total = $settings['total'];
  338. $oldfilename = $settings['import']['oldfilename'];
  339. $field_match = $settings['import']['field_match'];
  340. $error_count = 0;
  341. $field_match = _user_import_unconcatenate_field_match($field_match);
  342. foreach ($data as $data_row) {
  343. $row = array();
  344. foreach ($data_row['data'] as $type => $fields) {
  345. if (!empty($fields)) {
  346. foreach ($fields as $field_id => $field_data) {
  347. foreach ($field_match as $column_info) {
  348. if ($column_info['type'] == $type && $column_info['field_id'] == $field_id) {
  349. if (!empty($column_info['username'])) {
  350. $header[$column_info['username']] = t('Name %sort', array('%sort' => $column_info['username']));
  351. $row[$column_info['username']] = array("data" => $field_data[0], "align" => "left");
  352. }
  353. if ($column_info['field_id'] == 'email') {
  354. $row[0] = array("data" => $field_data[0], "align" => "left");
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. ksort($row);
  362. $row[] = implode('<br />', $data_row['errors']);
  363. $rows[] = $row;
  364. }
  365. $output .= '<p>' . t('<strong>CSV File:</strong> %file', array('%file' => $oldfilename)) . '<br />';
  366. $output .= t('<strong>Errors:</strong> !total', array('!total' => $total)) . '</p>';
  367. $header['errors'] = t('Errors');
  368. // Output of table with the paging
  369. $output .= theme('table',
  370. array(
  371. "header" => $header,
  372. "rows" => $rows,
  373. "attributes" => array(),
  374. "sticky" => FALSE, // Table header will be sticky
  375. "caption" => '',
  376. "colgroups" => array(),
  377. "empty" => t("There are no errors.") // The message to be displayed if table is empty
  378. )
  379. ) . theme("pager");
  380. //$output .= theme('table', array('header' => $header, 'rows' => $rows));
  381. return $output;
  382. }
  383. function theme_user_import_username_errors($errors) {
  384. if (empty($errors)) {
  385. $output = '<p><strong>' . t('All usernames are OK.') . '</strong></p>';
  386. }
  387. else {
  388. $header = array(t('User ID'), t('Email'), t('Username'), t('Error'));
  389. $output = theme('table', array('header' => $header, 'errors' => $errors));
  390. }
  391. return $output;
  392. }
  393. // - - - - - - - - MISC - - - - - - - -
  394. function _user_import_settings_save($settings) {
  395. // Database field defaults.
  396. $database_fields = array('import_id' => NULL,
  397. 'name' => '',
  398. 'auto_import_directory' => '',
  399. 'filename' => '',
  400. 'oldfilename' => '',
  401. 'filepath' => '',
  402. 'started' => 0,
  403. 'pointer' => 0,
  404. 'processed' => 0,
  405. 'valid' => 0,
  406. 'field_match' => array(),
  407. 'roles' => '',
  408. 'options' => array(),
  409. 'setting' => '',
  410. );
  411. // Form elements we never want to save in the options column.
  412. $form_variables = array('form_id', 'form_token', 'form_build_id', 'cancel', 'import', 'submit', 'op', 0, 'return_path');
  413. $form_variables = array_flip($form_variables);
  414. // Remove settings we don't need in the options column.
  415. $options = array_diff_key($settings, $database_fields);
  416. $options = array_diff_key($options, $form_variables);
  417. // Optimise Email Domain option if it's set.
  418. if (!empty($options['email_domain'])) {
  419. $options['email_domain'] = trim($options['email_domain']);
  420. if (substr($options['email_domain'], 0, 1) != '@') {
  421. $options['email_domain'] = '@'. $options['email_domain'];
  422. }
  423. }
  424. // Set defaults for options.
  425. foreach ($options as $key => $value) {
  426. $settings['options'][$key] = isset($settings[$key]) ? $settings[$key] : '';
  427. }
  428. // Optimise Email Domain option if it's set.
  429. if (!empty($settings['options']['email_domain'])) {
  430. $settings['options']['email_domain'] = trim($settings['options']['email_domain']);
  431. if (substr($settings['options']['email_domain'], 0, 1) != '@') {
  432. $settings['options']['email_domain'] = '@'. $settings['options']['email_domain'];
  433. }
  434. }
  435. // Set defaults for fields.
  436. foreach ($database_fields as $key => $value) {
  437. $settings[$key] = isset($settings[$key]) ? $settings[$key] : $value;
  438. }
  439. // Set default values.
  440. $import_id = isset($settings['import_id']) ? $settings['import_id'] : '';
  441. $name = isset($settings['name']) ? trim($settings['name']) : '';
  442. $pointer = isset($settings['pointer']) ? $settings['pointer'] : 0;
  443. $processed = isset($settings['processed']) ? $settings['processed'] : 0;
  444. $valid = isset($settings['valid']) ? $settings['valid'] : 0;
  445. $field_match = isset($settings['field_match']) ? serialize($settings['field_match']) : '';
  446. $roles = isset($settings['roles']) ? serialize($settings['roles']) : '';
  447. $options = isset($settings['options']) ? serialize($settings['options']) : '';
  448. $setting = isset($settings['setting']) ? $settings['setting'] : '';
  449. // Only set an auto import directory if saving a template.
  450. if (isset($settings['auto_import_directory']) && $settings['setting'] == 'template') {
  451. $auto_import_directory = $settings['auto_import_directory'];
  452. }
  453. else {
  454. $auto_import_directory = '';
  455. }
  456. $file->filename = isset($settings['filename']) ? $settings['filename'] : '';
  457. $file->oldfilename = isset($settings['oldfilename']) ? $settings['oldfilename'] : '';
  458. $file->filepath = isset($settings['filepath']) ? $settings['filepath'] : '';
  459. $settings['import_id'] = user_import_import_set($name, $file, $pointer, $processed, $valid, $field_match, $roles, $options, $setting, $auto_import_directory, $import_id);
  460. return $settings;
  461. }
  462. function user_import_import_set($name = '', $file = '', $pointer = 0, $processed = 0, $valid = 0, $field_match = '', $roles = '', $options = '', $setting = '', $auto_import_directory = '', $import_id = '') {
  463. // Update settings for existing import.
  464. if (!empty($import_id)) {
  465. db_update('user_import')
  466. ->fields(array(
  467. 'name' => $name,
  468. 'auto_import_directory' => $auto_import_directory,
  469. 'filename' => $file->filename,
  470. 'oldfilename' => $file->oldfilename,
  471. 'filepath' => $file->filepath,
  472. 'pointer' => $pointer,
  473. 'processed' => $processed,
  474. 'valid' => $valid,
  475. 'field_match' => $field_match,
  476. 'roles' => $roles,
  477. 'options' => $options,
  478. 'setting' => $setting
  479. ))
  480. ->condition('import_id', $import_id)
  481. ->execute();
  482. }
  483. else {
  484. $import_id = db_insert('user_import')
  485. ->fields(array(
  486. 'name' => $name,
  487. 'auto_import_directory' => $auto_import_directory,
  488. 'filename' => $file->filename,
  489. 'oldfilename' => $file->oldfilename,
  490. 'filepath' => $file->filepath,
  491. 'started' => time(),
  492. 'pointer' => $pointer,
  493. 'processed' => $processed,
  494. 'valid' => $valid,
  495. 'field_match' => $field_match,
  496. 'roles' => $roles,
  497. 'options' => $options,
  498. 'setting' => $setting
  499. ))
  500. ->execute();
  501. }
  502. return $import_id;
  503. }
  504. /**
  505. * Return either a single import setting, or all template, or all non-template settings.
  506. */
  507. function _user_import_settings_select($import_id = NULL, $template = FALSE) {
  508. $import = array();
  509. if (!empty($import_id) && !is_numeric($import_id)) return;
  510. if (!empty($import_id)) {
  511. $sql = 'SELECT * FROM {user_import} WHERE import_id = :import_id';
  512. if ($template) $sql .= " AND setting = 'template'";
  513. $import = (array)db_query_range($sql, 0, 1, array(':import_id' => $import_id))->fetchObject();
  514. if (empty($import)) return FALSE;
  515. $import['field_match'] = unserialize($import['field_match']);
  516. $import['roles'] = unserialize($import['roles']);
  517. $import['options'] = unserialize($import['options']);
  518. if (is_array($import['options'])) {
  519. foreach ($import['options'] as $key => $value) {
  520. $import[$key] = $value;
  521. }
  522. }
  523. }
  524. else {
  525. $query = ($template) ? "SELECT * FROM {user_import} WHERE setting = 'template'" : "SELECT * FROM {user_import} WHERE setting <> 'template' ORDER BY started DESC";
  526. $result = db_query($query);
  527. foreach ($result as $row_data) {
  528. $row = (array)$row_data;
  529. $row['field_match'] = unserialize($row['field_match']);
  530. $row['roles'] = unserialize($row['roles']);
  531. $row['options'] = unserialize($row['options']);
  532. foreach ($row['options'] as $key => $value) {
  533. $row[$key] = $value;
  534. }
  535. $import[] = $row;
  536. }
  537. }
  538. return $import;
  539. }
  540. function _user_import_settings_deletion($import_id) {
  541. $sql = 'SELECT auto_import_directory FROM {user_import} WHERE import_id = :import_id';
  542. $auto_import_directory = db_query_range($sql, 0, 1, array(':import_id' => $import_id))->fetchField();
  543. if (!empty($auto_import_directory)) {
  544. $deleted = file_unmanaged_delete_recursive('private://user_import/uploads/' . $auto_import_directory);
  545. if ($deleted) {
  546. watchdog('Usr Import', t("Directory '%directory' has been deleted."), array('%directory' => $auto_import_directory));
  547. }
  548. }
  549. db_delete('user_import')
  550. ->condition('import_id', $import_id)
  551. ->execute();
  552. db_delete('user_import_errors')
  553. ->condition('import_id', $import_id)
  554. ->execute();
  555. return;
  556. }
  557. // Used by user_import_og.module
  558. function user_import_profile_load($user) {
  559. $result = db_query('SELECT f.name, f.type, f.fid, v.value FROM {profile_field} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
  560. foreach ($result as $field) {
  561. if (empty($profile[$field->fid])) {
  562. $profile[$field->fid] = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
  563. }
  564. }
  565. return $profile;
  566. }
  567. function _user_import_unconcatenate_field_match($settings) {
  568. $settings_updated = array();
  569. foreach ($settings as $column_id => $values) {
  570. if (!empty($values['field_match']) || !empty($values['username'])) {
  571. // If we have a username but no field_match, set a special type.
  572. // This allows us to skip saving the field but still use it in
  573. // concatenating a username value.
  574. if (empty($values['field_match'])) {
  575. $values['type'] = 'username_part';
  576. $values['field_id'] = 'username_part_' . $column_id;
  577. }
  578. else {
  579. $key_parts = explode('-', $values['field_match']);
  580. $values['type'] = array_shift($key_parts);
  581. $values['field_id'] = implode('-', $key_parts);
  582. }
  583. unset($values['field_match']);
  584. $settings_updated[$column_id] = $values;
  585. }
  586. }
  587. return $settings_updated;
  588. }
  589. /**
  590. * Loads the hooks for the supported modules.
  591. */
  592. function user_import_load_supported() {
  593. static $loaded = FALSE;
  594. if (!$loaded) {
  595. $path = drupal_get_path('module', 'user_import') . '/supported';
  596. $files = drupal_system_listing("/\.inc$/", $path, 'name', 0);
  597. foreach ($files as $module_name => $file) {
  598. if (module_exists($module_name)) {
  599. include_once($file->uri);
  600. }
  601. }
  602. $loaded = TRUE;
  603. }
  604. }
  605. /**
  606. * Implementation of hook_simpletest().
  607. */
  608. function user_import_simpletest() {
  609. $module_name = 'user_import';
  610. $dir = drupal_get_path('module', $module_name) . '/tests';
  611. $tests = file_scan_directory($dir, '\.test$');
  612. return array_keys($tests);
  613. }
  614. /**
  615. * @param $filepath
  616. * @param $filename
  617. * @param $old_filename
  618. * @param $ftp
  619. * @param bool $message
  620. */
  621. function _user_import_file_deletion($filepath, $filename, $old_filename, $ftp, $message = TRUE) {
  622. $path_parts = explode(':', $filepath);
  623. if ($path_parts[0] == 'private') {
  624. // Delete files in the private files directory that have been uploaded by sftp/ftp.
  625. file_unmanaged_delete($filepath);
  626. }
  627. else {
  628. // Delete files uploaded through browser.
  629. $file = new stdClass();
  630. $file->uri = $filepath;
  631. $file->filename = $filename;
  632. $file->fid = db_query("SELECT fid FROM {file_managed} WHERE uri = :filepath", array(':filepath' => $filepath))->fetchField();
  633. ;
  634. $removed = file_delete($file);
  635. }
  636. if (!$message) return;
  637. if (empty($removed)) {
  638. drupal_set_message(t("File error: file '%old_filename' (%filename) could not be deleted.", array('%old_filename' => $oldfilename, '%filename' => $filename)), 'error');
  639. }
  640. else {
  641. drupal_set_message(t("File '%old_filename' was deleted.", array('%old_filename' => $old_filename)));
  642. }
  643. return;
  644. }