user_import.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * Implementation of hook_user_import_form_fieldset().
  4. * Add fieldsets to an import settings form.
  5. */
  6. function user_import_user_import_form_fieldset($import, $collapsed) {
  7. $form = array();
  8. _user_import_edit_template_fields($form, $import);
  9. _user_import_edit_settings_fields($form, $import, $collapsed);
  10. _user_import_edit_remove_fields($form, $import);
  11. return $form;
  12. }
  13. /**
  14. * Implementation of hook_user_import_after_save().
  15. */
  16. function user_import_user_import_after_save($settings, $account, $password, $fields, $updated, $update_setting_per_module) {
  17. if (!empty($settings['send_email']) && !$updated) {
  18. $subscribed = isset($settings['subscribed']) ? $settings['subscribed'] : NULL;
  19. _user_import_send_email($account,
  20. $password,
  21. $fields,
  22. $settings['subject'],
  23. $settings['message'],
  24. $settings['message_format'],
  25. $settings['message_css'],
  26. $subscribed
  27. );
  28. }
  29. }
  30. /**
  31. * Implementation of hook_user_import_imported().
  32. */
  33. function user_import_user_import_imported($import_id, $settings) {
  34. // Delete file after it's been processed,
  35. //_user_import_file_deletion($settings['filepath'], $settings['filename'], $settings['oldfilename'], FALSE, FALSE);
  36. file_unmanaged_delete($settings['filepath']);
  37. }
  38. // Send email when account is created
  39. function _user_import_send_email($account, $password, $profile, $subject, $body, $format, $css, $subscribed) {
  40. global $base_url;
  41. // All system mails need to specify the module and template key (mirrored from
  42. // hook_mail()) that the message they want to send comes from.
  43. $module = 'user_import';
  44. $key = 'welcome';
  45. // Specify 'to' and 'from' addresses.
  46. $to = $account->mail;
  47. $from = variable_get('site_mail', NULL);
  48. $params = array(
  49. '!username' => $account->name,
  50. '!uid' => $account->uid,
  51. '!site' => variable_get('site_name', 'drupal'),
  52. '!login_url' => user_pass_reset_url($account),
  53. '!password' => $password,
  54. '!uri' => $base_url,
  55. '!uri_brief' => drupal_substr($base_url, drupal_strlen('http://')),
  56. '!mailto' => $account->mail,
  57. '!date' => format_date(time()),
  58. '!login_uri' => url('user', array('absolute' => TRUE)),
  59. '!edit_uri' => url('user/' . $account->uid . '/edit', array('absolute' => TRUE)),
  60. 'subject' => $subject,
  61. 'body' => $body,
  62. 'email_format' => $format,
  63. 'css' => $css,
  64. );
  65. _user_import_publication_email($params, $account, $subscribed, $format);
  66. // import info to profile
  67. if (module_exists('profile') && is_array($profile)) {
  68. $profile_name = _user_import_profile('fid', 'name');
  69. foreach ($profile_name as $fid => $field_name) {
  70. $params['!' . $field_name] = $profile[$fid];
  71. }
  72. }
  73. $language = user_preferred_language($account);
  74. // Whether or not to automatically send the mail when drupal_mail() is
  75. // called. This defaults to TRUE, and is normally what you want unless you
  76. // need to do additional processing before drupal_mail_send() is called.
  77. $send = TRUE;
  78. $sent = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  79. return;
  80. }
  81. /**
  82. * Implementation of hook_mail().
  83. */
  84. function user_import_mail($key, &$message, $params) {
  85. switch ($key) {
  86. case 'welcome':
  87. $message['subject'] = (empty($params['subject'])) ? _user_mail_text('register_admin_created_subject', $message['language'], $params) : strtr($params['subject'], $params);
  88. $body = (empty($params['body'])) ? _user_mail_text('register_admin_created_body', $message['language'], $params) : strtr($params['body'], $params);
  89. if ($params['email_format'] == 1) {
  90. $message['headers']['Content-Type'] = 'text/html; charset=UTF-8';
  91. $body_head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  92. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  93. <head>
  94. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
  95. if (!empty($params['css'])) $body_head .= '<style type="text/css">' . check_plain($params['css']) . '</style>';
  96. $message['body'][] = $body_head . '</head><body>' . $body . '</body></html>';
  97. }
  98. else {
  99. $message['body'][] = $body;
  100. }
  101. break;
  102. }
  103. }
  104. function _user_import_edit_settings_fields(&$form, $import, $collapsed) {
  105. $form['optional'] = array(
  106. '#type' => 'fieldset',
  107. '#title' => t('Options'),
  108. '#weight' => -85,
  109. '#collapsible' => TRUE,
  110. '#collapsed' => $collapsed,
  111. );
  112. $form['optional']['first_line_skip'] = array(
  113. '#type' => 'checkbox',
  114. '#title' => t('Ignore First Line'),
  115. '#default_value' => isset($import['first_line_skip']) ? $import['first_line_skip'] : 0,
  116. '#description' => t('If the first line is the names of the data columns, set to ignore first line.'),
  117. );
  118. /**
  119. * @todo move contact options to a separate contact.inc support file
  120. */
  121. $form['optional']['contact'] = array(
  122. '#type' => 'checkbox',
  123. '#title' => t('Contact'),
  124. '#default_value' => isset($import['contact']) ? $import['contact'] : 0,
  125. '#description' => t("Set each user's personal contact form to 'allowed'."),
  126. );
  127. $form['optional']['send_email'] = array(
  128. '#type' => 'checkbox',
  129. '#title' => t('Send Email'),
  130. '#default_value' => isset($import['send_email']) ? $import['send_email'] : 0,
  131. '#description' => t('Send email to users when their account is created.'),
  132. );
  133. $form['optional']['username_space'] = array(
  134. '#type' => 'checkbox',
  135. '#title' => t('Username Space'),
  136. '#default_value' => isset($import['username_space']) ? $import['username_space'] : 0,
  137. '#description' => t("Include spaces in usernames, e.g. 'John' + 'Smith' => 'John Smith'."),
  138. );
  139. $form['optional']['activate'] = array(
  140. '#type' => 'checkbox',
  141. '#title' => t('Activate Accounts'),
  142. '#default_value' => isset($import['activate']) ? $import['activate'] : 0,
  143. '#description' => t("User accounts will not be visible to other users until their owner logs in. Select this option to make all imported user accounts visible. <strong>Note - one time login links in welcome emails will no longer work if this option is enabled.</strong>"),
  144. );
  145. $form['optional']['delimiter'] = array(
  146. '#type' => 'textfield',
  147. '#title' => t('File Delimiter'),
  148. '#size' => 4,
  149. '#default_value' => isset($import['delimiter']) ? $import['delimiter'] : ',',
  150. '#description' => t("The column delimiter for the file. Use '/t' for Tab."),
  151. );
  152. $form['optional']['multi_value_delimiter'] = array(
  153. '#type' => 'textfield',
  154. '#title' => t('Multi Value Delimiter'),
  155. '#size' => 4,
  156. '#default_value' => isset($import['multi_value_delimiter']) ? $import['multi_value_delimiter'] : '*||*',
  157. '#description' => t("Character(s) to use to split data so that it can be added to a field set to multiple values, e.g. '||', '*||*'"),
  158. );
  159. $form['optional']['email_domain'] = array(
  160. '#type' => 'textfield',
  161. '#title' => t('Email Domain'),
  162. '#description' => t("Create an email address with the combined contents of Email Address (in Field Match above) and this field. <em><br />For example if Email Address is 'Luthor' and Email Domain is '@lexcorp.com', the account address woud be 'luthor@lexcorp.com'. <br />Note that '@example.com' is recommended for dummy addresses.</em>"),
  163. '#default_value' => isset($import['email_domain']) ? $import['email_domain'] : '',
  164. );
  165. return;
  166. }
  167. function _user_import_edit_template_fields(&$form, $import) {
  168. // settings template update controls
  169. if (empty($import['name'])) {
  170. // new settings template save controls
  171. $form['save'] = array(
  172. '#type' => 'fieldset',
  173. '#title' => t('Save Settings'),
  174. '#description' => t('Save settings for re-use on other imports.'),
  175. '#weight' => 90,
  176. '#collapsible' => TRUE,
  177. '#collapsed' => FALSE,
  178. );
  179. $form['save']['name'] = array(
  180. '#type' => 'textfield',
  181. '#title' => t('Settings Name'),
  182. '#size' => 26,
  183. '#maxlength' => 25,
  184. '#description' => t('Name to identify these settings by.'),
  185. );
  186. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  187. if (!empty($auto_imports_enabled)) {
  188. $form['save']['auto_import_directory'] = array(
  189. '#type' => 'textfield',
  190. '#title' => t('Auto Import Directory Name'),
  191. '#description' => t('If this is set a directory with this name will be created, into which files can be uploaded and automatically processed, using the settings on this page to create new user accounts.'),
  192. '#default_value' => isset($import['auto_import_directory']) ? $import['auto_import_directory'] : '',
  193. );
  194. }
  195. $form['save'][] = array(
  196. '#type' => 'submit',
  197. '#value' => t('Save'),
  198. '#validate' => array('user_import_template_has_name_validate',
  199. 'user_import_template_unique_name_validate',
  200. 'user_import_edit_validate',
  201. ),
  202. '#submit' => array('user_import_template_new_submit'),
  203. );
  204. }
  205. else {
  206. $form['save'] = array(
  207. '#type' => 'fieldset',
  208. '#title' => t('Saved Settings'),
  209. '#description' => t("If changes have neen made to the settings since they where last saved you can update the saved template, or save them as a new template."),
  210. '#weight' => 90,
  211. '#collapsible' => TRUE,
  212. '#collapsed' => TRUE,
  213. );
  214. $form['#current_template'] = $import['name'];
  215. $form['save']['update'] = array(
  216. '#type' => 'fieldset',
  217. '#title' => t('Update'),
  218. '#description' => t("Update '%name' settings template", array('%name' => $import['name'])),
  219. );
  220. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  221. if (!empty($auto_imports_enabled)) {
  222. $form['save']['auto_import_directory'] = array(
  223. '#type' => 'textfield',
  224. '#title' => t('Auto Import Directory Name'),
  225. '#description' => t('If this is set a directory with this name will be created, into which files can be uploaded and automatically processed, using the settings on this page to create new user accounts.'),
  226. '#default_value' => isset($import['auto_import_directory']) ? $import['auto_import_directory'] : '',
  227. );
  228. }
  229. $form['save']['update']['submit'] = array(
  230. '#type' => 'submit',
  231. '#value' => t('Update'),
  232. '#validate' => array('user_import_edit_validate'),
  233. '#submit' => array('user_import_template_update_submit'),
  234. );
  235. $form['save']['new'] = array(
  236. '#type' => 'fieldset',
  237. '#title' => t('Create New'),
  238. '#description' => t("Save as new settings template"),
  239. );
  240. $form['save']['new']['name'] = array(
  241. '#type' => 'textfield',
  242. '#title' => t('Save As New'),
  243. '#size' => 30,
  244. '#maxlength' => 25,
  245. '#description' => t('Name to identify these settings by.'),
  246. );
  247. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  248. if (!empty($auto_imports_enabled)) {
  249. $form['save']['auto_import_directory'] = array(
  250. '#type' => 'textfield',
  251. '#title' => t('Auto Import Directory Name'),
  252. '#description' => t('If this is set a directory with this name will be created, into which files can be uploaded and automatically processed, using the settings on this page to create new user accounts.'),
  253. '#default_value' => isset($import['auto_import_directory']) ? $import['auto_import_directory'] : '',
  254. );
  255. }
  256. $form['save']['new'][] = array(
  257. '#type' => 'submit',
  258. '#value' => t('Save As New'),
  259. '#validate' => array('user_import_template_has_name_validate', 'user_import_template_unique_name_validate', 'user_import_edit_validate'),
  260. '#submit' => array('user_import_template_new_submit'),
  261. );
  262. }
  263. return;
  264. }
  265. /**
  266. * Validate that a template has a name.
  267. */
  268. function user_import_template_has_name_validate($form, &$form_state) {
  269. $template_name = trim($form_state['values']['name']);
  270. if (empty($template_name)) form_set_error('name', t('A name needs to be set to save this settings template.'));
  271. }
  272. /**
  273. * Validate that a template has a unique name.
  274. */
  275. function user_import_template_unique_name_validate($form, &$form_state) {
  276. $template_name = trim($form_state['values']['name']);
  277. $unique_name = db_query('SELECT COUNT(import_id) FROM {user_import} WHERE name = :name', array(':name' => $template_name))->fetchField();
  278. if (!empty($unique_name)) form_set_error('name', t("'!name' is already in use by another settings template.", array('!name' => $template_name)));
  279. }
  280. /**
  281. * Validate that a email subject line has been set if Send Email is enabled.
  282. */
  283. function user_import_send_email_subject_validate($element, &$form_state) {
  284. if (!empty($form_state['values']['send_email']) && empty($form_state['values']['subject'])) {
  285. form_error($element, t('If Send Email has been enabled then an <strong>email subject</strong> line must set.'));
  286. }
  287. }
  288. /**
  289. * Validate that a email message has been set if Send Email is enabled.
  290. */
  291. function user_import_send_email_message_validate($element, &$form_state) {
  292. if (!empty($form_state['values']['send_email']) && empty($form_state['values']['message'])) {
  293. form_error($element, t('If Send Email has been enabled then an <strong>email message</strong> must set.'));
  294. }
  295. }
  296. function _user_import_edit_remove_fields(&$form, $import) {
  297. $form['remove'] = array(
  298. '#type' => 'fieldset',
  299. '#title' => t('Use Different CSV File'),
  300. '#description' => t('Remove file to use a different file. All settings on this page will be deleted.'),
  301. '#weight' => -100,
  302. '#collapsible' => TRUE,
  303. '#collapsed' => TRUE,
  304. );
  305. $form['remove']['file'] = array(
  306. '#type' => 'item',
  307. '#title' => t('Uploaded file: @filename', array('@filename' => $import['filename'])),
  308. '#value' => $import['filename'],
  309. );
  310. $form['remove']['submit'] = array(
  311. '#type' => 'submit',
  312. '#value' => t('Remove file'),
  313. '#validate' => array('user_import_edit_remove_file_validate'),
  314. );
  315. return;
  316. }
  317. /**
  318. * Delete settings and uploaded file
  319. */
  320. function user_import_edit_remove_file_validate($form, &$form_state) {
  321. $settings = _user_import_settings_select($form_state['values']['import_id']);
  322. _user_import_settings_deletion($form_state['values']['import_id']);
  323. //_user_import_file_deletion($settings['filepath'], $settings['filename'], $settings['oldfilename'], $settings['options']['ftp']);
  324. file_unmanaged_delete($settings['filepath']);
  325. drupal_goto('admin/people/user_import/add');
  326. }
  327. function _user_import_publication_email(&$variables, $account, $subscribed, $format) {
  328. if (!module_exists('publication') || !module_exists('schedule') || !module_exists('identity_hash')) {
  329. return;
  330. }
  331. $id_hash = identity_hash_select_hash($account->uid);
  332. $variables['!id_hash'] = $id_hash->hash;
  333. while (list($type, $subscriptions) = each($subscribed)) {
  334. while (list($publication_id, $shedule) = each($subscriptions)) {
  335. if (!empty($shedule[0])) {
  336. $publication = publication_select_publications($type, $publication_id);
  337. $update_link = url('subscribed/preferences/' . $publication_id . '/' . $id_hash->hash, array('absolute' => TRUE));
  338. $unsubscribe_link = url('subscribed/delete/' . $publication_id . '/' . $id_hash->hash, array('absolute' => TRUE));
  339. if ($format == 1) {
  340. $variables['!subscribed_links'] .= '<strong>' . $publication->title . '</strong><br />' .
  341. '<a href="' . $update_link . '">' . t('Update Preferences') . '</a> | <a href="' . $unsubscribe_link . '">' . t('Unsubscribe') . '</a><br />';
  342. }
  343. else {
  344. $variables['!subscribed_links'] .= $publication->title . "\n" .
  345. ' - ' . t('Update Preferences') . ' ' . $update_link . '\n' .
  346. ' - ' . t('Unsubscribe') . ' ' . $unsubscribe_link . '\n';
  347. }
  348. }
  349. }
  350. }
  351. }