user_import.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. function _user_import_edit_settings_fields(&$form, $import, $collapsed) {
  82. $form['optional'] = array(
  83. '#type' => 'fieldset',
  84. '#title' => t('Options'),
  85. '#weight' => -85,
  86. '#collapsible' => TRUE,
  87. '#collapsed' => $collapsed,
  88. );
  89. $form['optional']['first_line_skip'] = array(
  90. '#type' => 'checkbox',
  91. '#title' => t('Ignore First Line'),
  92. '#default_value' => isset($import['first_line_skip']) ? $import['first_line_skip'] : 0,
  93. '#description' => t('If the first line is the names of the data columns, set to ignore first line.'),
  94. );
  95. /**
  96. * @todo move contact options to a separate contact.inc support file
  97. */
  98. $form['optional']['contact'] = array(
  99. '#type' => 'checkbox',
  100. '#title' => t('Contact'),
  101. '#default_value' => isset($import['contact']) ? $import['contact'] : 0,
  102. '#description' => t("Set each user's personal contact form to 'allowed'."),
  103. );
  104. $form['optional']['send_email'] = array(
  105. '#type' => 'checkbox',
  106. '#title' => t('Send Email'),
  107. '#default_value' => isset($import['send_email']) ? $import['send_email'] : 0,
  108. '#description' => t('Send email to users when their account is created.'),
  109. );
  110. $form['optional']['username_space'] = array(
  111. '#type' => 'checkbox',
  112. '#title' => t('Username Space'),
  113. '#default_value' => isset($import['username_space']) ? $import['username_space'] : 0,
  114. '#description' => t("Include spaces in usernames, e.g. 'John' + 'Smith' => 'John Smith'."),
  115. );
  116. $form['optional']['activate'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('Activate Accounts'),
  119. '#default_value' => isset($import['activate']) ? $import['activate'] : 0,
  120. '#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>"),
  121. );
  122. $form['optional']['delimiter'] = array(
  123. '#type' => 'textfield',
  124. '#title' => t('File Delimiter'),
  125. '#size' => 4,
  126. '#default_value' => isset($import['delimiter']) ? $import['delimiter'] : ',',
  127. '#description' => t("The column delimiter for the file. Use '/t' for Tab."),
  128. );
  129. $form['optional']['multi_value_delimiter'] = array(
  130. '#type' => 'textfield',
  131. '#title' => t('Multi Value Delimiter'),
  132. '#size' => 4,
  133. '#default_value' => isset($import['multi_value_delimiter']) ? $import['multi_value_delimiter'] : '*||*',
  134. '#description' => t("Character(s) to use to split data so that it can be added to a field set to multiple values, e.g. '||', '*||*'"),
  135. );
  136. $form['optional']['email_domain'] = array(
  137. '#type' => 'textfield',
  138. '#title' => t('Email Domain'),
  139. '#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>"),
  140. '#default_value' => isset($import['email_domain']) ? $import['email_domain'] : '',
  141. );
  142. return;
  143. }
  144. function _user_import_edit_template_fields(&$form, $import) {
  145. // settings template update controls
  146. if (empty($import['name'])) {
  147. // new settings template save controls
  148. $form['save'] = array(
  149. '#type' => 'fieldset',
  150. '#title' => t('Save Settings'),
  151. '#description' => t('Save settings for re-use on other imports.'),
  152. '#weight' => 90,
  153. '#collapsible' => TRUE,
  154. '#collapsed' => FALSE,
  155. );
  156. $form['save']['name'] = array(
  157. '#type' => 'textfield',
  158. '#title' => t('Settings Name'),
  159. '#size' => 26,
  160. '#maxlength' => 25,
  161. '#description' => t('Name to identify these settings by.'),
  162. );
  163. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  164. if (!empty($auto_imports_enabled)) {
  165. $form['save']['auto_import_directory'] = array(
  166. '#type' => 'textfield',
  167. '#title' => t('Auto Import Directory Name'),
  168. '#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.'),
  169. '#default_value' => isset($import['auto_import_directory']) ? $import['auto_import_directory'] : '',
  170. );
  171. }
  172. $form['save'][] = array(
  173. '#type' => 'submit',
  174. '#value' => t('Save'),
  175. '#validate' => array('user_import_template_has_name_validate',
  176. 'user_import_template_unique_name_validate',
  177. 'user_import_edit_validate',
  178. ),
  179. '#submit' => array('user_import_template_new_submit'),
  180. );
  181. }
  182. else {
  183. $form['save'] = array(
  184. '#type' => 'fieldset',
  185. '#title' => t('Saved Settings'),
  186. '#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."),
  187. '#weight' => 90,
  188. '#collapsible' => TRUE,
  189. '#collapsed' => TRUE,
  190. );
  191. $form['#current_template'] = $import['name'];
  192. $form['save']['update'] = array(
  193. '#type' => 'fieldset',
  194. '#title' => t('Update'),
  195. '#description' => t("Update '%name' settings template", array('%name' => $import['name'])),
  196. );
  197. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  198. if (!empty($auto_imports_enabled)) {
  199. $form['save']['auto_import_directory'] = array(
  200. '#type' => 'textfield',
  201. '#title' => t('Auto Import Directory Name'),
  202. '#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.'),
  203. '#default_value' => isset($import['auto_import_directory']) ? $import['auto_import_directory'] : '',
  204. );
  205. }
  206. $form['save']['update']['submit'] = array(
  207. '#type' => 'submit',
  208. '#value' => t('Update'),
  209. '#validate' => array('user_import_edit_validate'),
  210. '#submit' => array('user_import_template_update_submit'),
  211. );
  212. $form['save']['new'] = array(
  213. '#type' => 'fieldset',
  214. '#title' => t('Create New'),
  215. '#description' => t("Save as new settings template"),
  216. );
  217. $form['save']['new']['name'] = array(
  218. '#type' => 'textfield',
  219. '#title' => t('Save As New'),
  220. '#size' => 30,
  221. '#maxlength' => 25,
  222. '#description' => t('Name to identify these settings by.'),
  223. );
  224. $auto_imports_enabled = variable_get('user_import_auto_imports_enabled', FALSE);
  225. if (!empty($auto_imports_enabled)) {
  226. $form['save']['auto_import_directory'] = array(
  227. '#type' => 'textfield',
  228. '#title' => t('Auto Import Directory Name'),
  229. '#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.'),
  230. '#default_value' => isset($import['auto_import_directory']) ? $import['auto_import_directory'] : '',
  231. );
  232. }
  233. $form['save']['new'][] = array(
  234. '#type' => 'submit',
  235. '#value' => t('Save As New'),
  236. '#validate' => array('user_import_template_has_name_validate', 'user_import_template_unique_name_validate', 'user_import_edit_validate'),
  237. '#submit' => array('user_import_template_new_submit'),
  238. );
  239. }
  240. return;
  241. }
  242. /**
  243. * Validate that a template has a name.
  244. */
  245. function user_import_template_has_name_validate($form, &$form_state) {
  246. $template_name = trim($form_state['values']['name']);
  247. if (empty($template_name)) form_set_error('name', t('A name needs to be set to save this settings template.'));
  248. }
  249. /**
  250. * Validate that a template has a unique name.
  251. */
  252. function user_import_template_unique_name_validate($form, &$form_state) {
  253. $template_name = trim($form_state['values']['name']);
  254. $unique_name = db_query('SELECT COUNT(import_id) FROM {user_import} WHERE name = :name', array(':name' => $template_name))->fetchField();
  255. if (!empty($unique_name)) form_set_error('name', t("'!name' is already in use by another settings template.", array('!name' => $template_name)));
  256. }
  257. /**
  258. * Validate that a email subject line has been set if Send Email is enabled.
  259. */
  260. function user_import_send_email_subject_validate($element, &$form_state) {
  261. if (!empty($form_state['values']['send_email']) && empty($form_state['values']['subject'])) {
  262. form_error($element, t('If Send Email has been enabled then an <strong>email subject</strong> line must set.'));
  263. }
  264. }
  265. /**
  266. * Validate that a email message has been set if Send Email is enabled.
  267. */
  268. function user_import_send_email_message_validate($element, &$form_state) {
  269. if (!empty($form_state['values']['send_email']) && empty($form_state['values']['message'])) {
  270. form_error($element, t('If Send Email has been enabled then an <strong>email message</strong> must set.'));
  271. }
  272. }
  273. function _user_import_edit_remove_fields(&$form, $import) {
  274. $form['remove'] = array(
  275. '#type' => 'fieldset',
  276. '#title' => t('Use Different CSV File'),
  277. '#description' => t('Remove file to use a different file. All settings on this page will be deleted.'),
  278. '#weight' => -100,
  279. '#collapsible' => TRUE,
  280. '#collapsed' => TRUE,
  281. );
  282. $form['remove']['file'] = array(
  283. '#type' => 'item',
  284. '#title' => t('Uploaded file: @filename', array('@filename' => $import['filename'])),
  285. '#value' => $import['filename'],
  286. );
  287. $form['remove']['submit'] = array(
  288. '#type' => 'submit',
  289. '#value' => t('Remove file'),
  290. '#validate' => array('user_import_edit_remove_file_validate'),
  291. );
  292. return;
  293. }
  294. /**
  295. * Delete settings and uploaded file
  296. */
  297. function user_import_edit_remove_file_validate($form, &$form_state) {
  298. $settings = _user_import_settings_select($form_state['values']['import_id']);
  299. _user_import_settings_deletion($form_state['values']['import_id']);
  300. //_user_import_file_deletion($settings['filepath'], $settings['filename'], $settings['oldfilename'], $settings['options']['ftp']);
  301. file_unmanaged_delete($settings['filepath']);
  302. drupal_goto('admin/people/user_import/add');
  303. }
  304. function _user_import_publication_email(&$variables, $account, $subscribed, $format) {
  305. if (!module_exists('publication') || !module_exists('schedule') || !module_exists('identity_hash')) {
  306. return;
  307. }
  308. $id_hash = identity_hash_select_hash($account->uid);
  309. $variables['!id_hash'] = $id_hash->hash;
  310. while (list($type, $subscriptions) = each($subscribed)) {
  311. while (list($publication_id, $shedule) = each($subscriptions)) {
  312. if (!empty($shedule[0])) {
  313. $publication = publication_select_publications($type, $publication_id);
  314. $update_link = url('subscribed/preferences/' . $publication_id . '/' . $id_hash->hash, array('absolute' => TRUE));
  315. $unsubscribe_link = url('subscribed/delete/' . $publication_id . '/' . $id_hash->hash, array('absolute' => TRUE));
  316. if ($format == 1) {
  317. $variables['!subscribed_links'] .= '<strong>' . $publication->title . '</strong><br />' .
  318. '<a href="' . $update_link . '">' . t('Update Preferences') . '</a> | <a href="' . $unsubscribe_link . '">' . t('Unsubscribe') . '</a><br />';
  319. }
  320. else {
  321. $variables['!subscribed_links'] .= $publication->title . "\n" .
  322. ' - ' . t('Update Preferences') . ' ' . $update_link . '\n' .
  323. ' - ' . t('Unsubscribe') . ' ' . $unsubscribe_link . '\n';
  324. }
  325. }
  326. }
  327. }
  328. }