profile.admin.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the profile module.
  5. */
  6. /**
  7. * Form builder to display a listing of all editable profile fields.
  8. *
  9. * @ingroup forms
  10. * @see profile_admin_overview_submit()
  11. */
  12. function profile_admin_overview($form) {
  13. $result = db_query('SELECT title, name, type, category, fid, weight FROM {profile_field} ORDER BY category, weight');
  14. $categories = array();
  15. foreach ($result as $field) {
  16. // Collect all category information
  17. $categories[] = $field->category;
  18. // Save all field information
  19. $form[$field->fid]['name'] = array('#markup' => check_plain($field->name));
  20. $form[$field->fid]['title'] = array('#markup' => check_plain($field->title));
  21. $form[$field->fid]['type'] = array('#markup' => $field->type);
  22. $form[$field->fid]['category'] = array(
  23. '#type' => 'select',
  24. '#title' => t('Category for @title', array('@title' => $field->title)),
  25. '#title_display' => 'invisible',
  26. '#default_value' => $field->category,
  27. '#options' => array(),
  28. );
  29. $form[$field->fid]['weight'] = array(
  30. '#type' => 'weight',
  31. '#title' => t('Weight for @title', array('@title' => $field->title)),
  32. '#title_display' => 'invisible',
  33. '#default_value' => $field->weight,
  34. );
  35. $form[$field->fid]['edit'] = array('#type' => 'link', '#title' => t('edit'), '#href' => "admin/config/people/profile/edit/$field->fid");
  36. $form[$field->fid]['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => "admin/config/people/profile/delete/$field->fid");
  37. }
  38. // Add the category combo boxes
  39. $categories = array_unique($categories);
  40. foreach ($form as $fid => $field) {
  41. foreach ($categories as $cat => $category) {
  42. $form[$fid]['category']['#options'][$category] = $category;
  43. }
  44. }
  45. // Display the submit button only when there's more than one field
  46. if (count($form) > 1) {
  47. $form['actions'] = array('#type' => 'actions');
  48. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  49. }
  50. else {
  51. // Disable combo boxes when there isn't a submit button
  52. foreach ($form as $fid => $field) {
  53. unset($form[$fid]['weight']);
  54. $form[$fid]['category']['#type'] = 'value';
  55. }
  56. }
  57. $form['#tree'] = TRUE;
  58. // @todo: Any reason this isn't done using an element with #theme = 'links'?
  59. $addnewfields = '<h2>' . t('Add new field') . '</h2>';
  60. $addnewfields .= '<ul>';
  61. foreach (_profile_field_types() as $key => $value) {
  62. $addnewfields .= '<li>' . l($value, "admin/config/people/profile/add/$key") . '</li>';
  63. }
  64. $addnewfields .= '</ul>';
  65. $form['addnewfields'] = array('#markup' => $addnewfields);
  66. return $form;
  67. }
  68. /**
  69. * Submit handler to update changed profile field weights and categories.
  70. *
  71. * @see profile_admin_overview()
  72. */
  73. function profile_admin_overview_submit($form, &$form_state) {
  74. foreach (element_children($form_state['values']) as $fid) {
  75. if (is_numeric($fid)) {
  76. $weight = $form_state['values'][$fid]['weight'];
  77. $category = $form_state['values'][$fid]['category'];
  78. if ($weight != $form[$fid]['weight']['#default_value'] || $category != $form[$fid]['category']['#default_value']) {
  79. db_update('profile_field')
  80. ->fields(array(
  81. 'weight' => $weight,
  82. 'category' => $category,
  83. ))
  84. ->condition('fid', $fid)
  85. ->execute();
  86. }
  87. }
  88. }
  89. drupal_set_message(t('Profile fields have been updated.'));
  90. cache_clear_all();
  91. menu_rebuild();
  92. }
  93. /**
  94. * Returns HTML for the profile field overview form into a drag and drop enabled table.
  95. *
  96. * @param $variables
  97. * An associative array containing:
  98. * - form: A render element representing the form.
  99. *
  100. * @see profile_admin_overview()
  101. * @ingroup themeable
  102. */
  103. function theme_profile_admin_overview($variables) {
  104. $form = $variables['form'];
  105. drupal_add_css(drupal_get_path('module', 'profile') . '/profile.css');
  106. // Add javascript if there's more than one field.
  107. if (isset($form['actions'])) {
  108. drupal_add_js(drupal_get_path('module', 'profile') . '/profile.js');
  109. }
  110. $rows = array();
  111. $categories = array();
  112. $category_number = 0;
  113. foreach (element_children($form) as $key) {
  114. // Don't take form control structures.
  115. if (isset($form[$key]['category'])) {
  116. $field = &$form[$key];
  117. $category = $field['category']['#default_value'];
  118. if (!isset($categories[$category])) {
  119. // Category classes are given numeric IDs because there's no guarantee
  120. // class names won't contain invalid characters.
  121. $categories[$category] = $category_number;
  122. $category_field['#attributes']['class'] = array('profile-category', 'profile-category-' . $category_number);
  123. $rows[] = array(array('data' => check_plain($category), 'colspan' => 7, 'class' => array('category')));
  124. $rows[] = array('data' => array(array('data' => '<em>' . t('No fields in this category. If this category remains empty when saved, it will be removed.') . '</em>', 'colspan' => 7)), 'class' => array('category-' . $category_number . '-message', 'category-message', 'category-populated'));
  125. // Make it draggable only if there is more than one field
  126. if (isset($form['actions'])) {
  127. drupal_add_tabledrag('profile-fields', 'order', 'sibling', 'profile-weight', 'profile-weight-' . $category_number);
  128. drupal_add_tabledrag('profile-fields', 'match', 'sibling', 'profile-category', 'profile-category-' . $category_number);
  129. }
  130. $category_number++;
  131. }
  132. // Add special drag and drop classes that group fields together.
  133. $field['weight']['#attributes']['class'] = array('profile-weight', 'profile-weight-' . $categories[$category]);
  134. $field['category']['#attributes']['class'] = array('profile-category', 'profile-category-' . $categories[$category]);
  135. // Add the row
  136. $row = array();
  137. $row[] = drupal_render($field['title']);
  138. $row[] = drupal_render($field['name']);
  139. $row[] = drupal_render($field['type']);
  140. if (isset($form['actions'])) {
  141. $row[] = drupal_render($field['category']);
  142. $row[] = drupal_render($field['weight']);
  143. }
  144. $row[] = drupal_render($field['edit']);
  145. $row[] = drupal_render($field['delete']);
  146. $rows[] = array('data' => $row, 'class' => array('draggable'));
  147. }
  148. }
  149. $header = array(t('Title'), t('Name'), t('Type'));
  150. if (isset($form['actions'])) {
  151. $header[] = t('Category');
  152. $header[] = t('Weight');
  153. }
  154. $header[] = array('data' => t('Operations'), 'colspan' => 2);
  155. $output = theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No fields available.'), 'attributes' => array('id' => 'profile-fields')));
  156. $output .= drupal_render_children($form);
  157. return $output;
  158. }
  159. /**
  160. * Menu callback: Generate a form to add/edit a user profile field.
  161. *
  162. * @ingroup forms
  163. * @see profile_field_form_validate()
  164. * @see profile_field_form_submit()
  165. */
  166. function profile_field_form($form, &$form_state, $arg = NULL) {
  167. if (arg(4) == 'edit') {
  168. if (is_numeric($arg)) {
  169. $fid = $arg;
  170. $edit = db_query('SELECT * FROM {profile_field} WHERE fid = :fid', array('fid' => $fid))->fetchAssoc();
  171. if (!$edit) {
  172. drupal_not_found();
  173. return;
  174. }
  175. drupal_set_title(t('Edit %title', array('%title' => $edit['title'])), PASS_THROUGH);
  176. $form['fid'] = array('#type' => 'value',
  177. '#value' => $fid,
  178. );
  179. $type = $edit['type'];
  180. }
  181. else {
  182. drupal_not_found();
  183. return;
  184. }
  185. }
  186. else {
  187. $types = _profile_field_types();
  188. if (!isset($types[$arg])) {
  189. drupal_not_found();
  190. return;
  191. }
  192. $type = $arg;
  193. drupal_set_title(t('Add new %type', array('%type' => $types[$type])), PASS_THROUGH);
  194. $edit = array('name' => 'profile_');
  195. $form['type'] = array('#type' => 'value', '#value' => $type);
  196. }
  197. $edit += array(
  198. 'category' => '',
  199. 'title' => '',
  200. 'explanation' => '',
  201. 'weight' => 0,
  202. 'page' => '',
  203. 'autocomplete' => '',
  204. 'required' => '',
  205. 'register' => '',
  206. );
  207. $form['category'] = array('#type' => 'textfield',
  208. '#title' => t('Category'),
  209. '#default_value' => $edit['category'],
  210. '#autocomplete_path' => 'admin/config/people/profile/autocomplete',
  211. '#description' => t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'),
  212. '#required' => TRUE,
  213. );
  214. $form['title'] = array('#type' => 'textfield',
  215. '#title' => t('Title'),
  216. '#default_value' => $edit['title'],
  217. '#description' => t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'),
  218. '#required' => TRUE,
  219. );
  220. $form['name'] = array('#type' => 'textfield',
  221. '#title' => t('Form name'),
  222. '#default_value' => $edit['name'],
  223. '#description' => t('The name of the field. The form name is not shown to the user but used internally in the HTML code and URLs.
  224. Unless you know what you are doing, it is highly recommended that you prefix the form name with <code>profile_</code> to avoid name clashes with other fields. Spaces or any other special characters except dash (-) and underscore (_) are not allowed. An example name is "profile_favorite_color" or perhaps just "profile_color".'),
  225. '#required' => TRUE,
  226. );
  227. $form['explanation'] = array('#type' => 'textarea',
  228. '#title' => t('Explanation'),
  229. '#default_value' => $edit['explanation'],
  230. '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'),
  231. );
  232. if ($type == 'selection') {
  233. $form['fields']['options'] = array('#type' => 'textarea',
  234. '#title' => t('Selection options'),
  235. '#default_value' => isset($edit['options']) ? $edit['options'] : '',
  236. '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'),
  237. );
  238. }
  239. $form['visibility'] = array('#type' => 'radios',
  240. '#title' => t('Visibility'),
  241. '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC,
  242. '#options' => array(PROFILE_HIDDEN => t('Hidden profile field, only accessible by administrators, modules and themes.'), PROFILE_PRIVATE => t('Private field, content only available to privileged users.'), PROFILE_PUBLIC => t('Public field, content shown on profile page but not used on member list pages.'), PROFILE_PUBLIC_LISTINGS => t('Public field, content shown on profile page and on member list pages.')),
  243. );
  244. if ($type == 'selection' || $type == 'list' || $type == 'textfield') {
  245. $form['fields']['page'] = array('#type' => 'textfield',
  246. '#title' => t('Page title'),
  247. '#default_value' => $edit['page'],
  248. '#description' => t('To enable browsing this field by value, enter a title for the resulting page. The word <code>%value</code> will be substituted with the corresponding value. An example page title is "People whose favorite color is %value" . This is only applicable for a public field.'),
  249. );
  250. }
  251. elseif ($type == 'checkbox') {
  252. $form['fields']['page'] = array('#type' => 'textfield',
  253. '#title' => t('Page title'),
  254. '#default_value' => $edit['page'],
  255. '#description' => t('To enable browsing this field by value, enter a title for the resulting page. An example page title is "People who are employed" . This is only applicable for a public field.'),
  256. );
  257. }
  258. $form['weight'] = array('#type' => 'weight',
  259. '#title' => t('Weight'),
  260. '#default_value' => $edit['weight'],
  261. '#description' => t('The weights define the order in which the form fields are shown. Lighter fields "float up" towards the top of the category.'),
  262. );
  263. $form['autocomplete'] = array('#type' => 'checkbox',
  264. '#title' => t('Form will auto-complete while user is typing.'),
  265. '#default_value' => $edit['autocomplete'],
  266. '#description' => t('For security, auto-complete will be disabled if the user does not have access to user profiles.'),
  267. );
  268. $form['required'] = array('#type' => 'checkbox',
  269. '#title' => t('The user must enter a value.'),
  270. '#default_value' => $edit['required'],
  271. );
  272. $form['register'] = array('#type' => 'checkbox',
  273. '#title' => t('Visible in user registration form.'),
  274. '#default_value' => $edit['register'],
  275. );
  276. $form['actions'] = array('#type' => 'actions');
  277. $form['actions']['submit'] = array('#type' => 'submit',
  278. '#value' => t('Save field'),
  279. );
  280. return $form;
  281. }
  282. /**
  283. * Validate profile_field_form submissions.
  284. */
  285. function profile_field_form_validate($form, &$form_state) {
  286. // Validate the 'field name':
  287. if (preg_match('/[^a-zA-Z0-9_-]/', $form_state['values']['name'])) {
  288. form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters except dash (-) and underscore (_) are not allowed.'));
  289. }
  290. $users_table = drupal_get_schema('users');
  291. if (!empty($users_table['fields'][$form_state['values']['name']])) {
  292. form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
  293. }
  294. // Validate the category:
  295. if (!$form_state['values']['category']) {
  296. form_set_error('category', t('You must enter a category.'));
  297. }
  298. if (strtolower($form_state['values']['category']) == 'account') {
  299. form_set_error('category', t('The specified category name is reserved for use by Drupal.'));
  300. }
  301. $query = db_select('profile_field');
  302. $query->fields('profile_field', array('fid'));
  303. if (isset($form_state['values']['fid'])) {
  304. $query->condition('fid', $form_state['values']['fid'], '<>');
  305. }
  306. $query_name = clone $query;
  307. $title = $query
  308. ->condition('title', $form_state['values']['title'])
  309. ->condition('category', $form_state['values']['category'])
  310. ->execute()
  311. ->fetchField();
  312. if ($title) {
  313. form_set_error('title', t('The specified title is already in use.'));
  314. }
  315. $name = $query_name
  316. ->condition('name', $form_state['values']['name'])
  317. ->execute()
  318. ->fetchField();
  319. if ($name) {
  320. form_set_error('name', t('The specified name is already in use.'));
  321. }
  322. if ($form_state['values']['visibility'] == PROFILE_HIDDEN) {
  323. if ($form_state['values']['required']) {
  324. form_set_error('required', t('A hidden field cannot be required.'));
  325. }
  326. if ($form_state['values']['register']) {
  327. form_set_error('register', t('A hidden field cannot be set to visible on the user registration form.'));
  328. }
  329. }
  330. }
  331. /**
  332. * Process profile_field_form submissions.
  333. */
  334. function profile_field_form_submit($form, &$form_state) {
  335. if (!isset($form_state['values']['options'])) {
  336. $form_state['values']['options'] = '';
  337. }
  338. if (!isset($form_state['values']['page'])) {
  339. $form_state['values']['page'] = '';
  340. }
  341. // Remove all elements that are not profile_field columns.
  342. $values = array_intersect_key($form_state['values'], array_flip(array('type', 'category', 'title', 'name', 'explanation', 'visibility', 'page', 'weight', 'autocomplete', 'required', 'register', 'options')));
  343. if (!isset($form_state['values']['fid'])) {
  344. db_insert('profile_field')
  345. ->fields($values)
  346. ->execute();
  347. drupal_set_message(t('The field has been created.'));
  348. watchdog('profile', 'Profile field %field added under category %category.', array('%field' => $form_state['values']['title'], '%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('view'), 'admin/config/people/profile'));
  349. }
  350. else {
  351. db_update('profile_field')
  352. ->fields($values)
  353. ->condition('fid', $form_state['values']['fid'])
  354. ->execute();
  355. drupal_set_message(t('The field has been updated.'));
  356. }
  357. cache_clear_all();
  358. menu_rebuild();
  359. $form_state['redirect'] = 'admin/config/people/profile';
  360. return;
  361. }
  362. /**
  363. * Menu callback; deletes a field from all user profiles.
  364. */
  365. function profile_field_delete($form, &$form_state, $fid) {
  366. $field = db_query("SELECT title FROM {profile_field} WHERE fid = :fid", array(':fid' => $fid))->fetchObject();
  367. if (!$field) {
  368. drupal_not_found();
  369. return;
  370. }
  371. $form['fid'] = array('#type' => 'value', '#value' => $fid);
  372. $form['title'] = array('#type' => 'value', '#value' => $field->title);
  373. return confirm_form($form,
  374. t('Are you sure you want to delete the field %field?', array('%field' => $field->title)), 'admin/config/people/profile',
  375. t('This action cannot be undone. If users have entered values into this field in their profile, these entries will also be deleted. If you want to keep the user-entered data, instead of deleting the field you may wish to <a href="@edit-field">edit this field</a> and change it to a hidden profile field so that it may only be accessed by administrators.', array('@edit-field' => url('admin/config/people/profile/edit/' . $fid))),
  376. t('Delete'), t('Cancel'));
  377. }
  378. /**
  379. * Process a field delete form submission.
  380. */
  381. function profile_field_delete_submit($form, &$form_state) {
  382. db_delete('profile_field')
  383. ->condition('fid', $form_state['values']['fid'])
  384. ->execute();
  385. db_delete('profile_value')
  386. ->condition('fid', $form_state['values']['fid'])
  387. ->execute();
  388. cache_clear_all();
  389. drupal_set_message(t('The field %field has been deleted.', array('%field' => $form_state['values']['title'])));
  390. watchdog('profile', 'Profile field %field deleted.', array('%field' => $form_state['values']['title']), WATCHDOG_NOTICE, l(t('view'), 'admin/config/people/profile'));
  391. $form_state['redirect'] = 'admin/config/people/profile';
  392. return;
  393. }
  394. /**
  395. * Retrieve a pipe delimited string of autocomplete suggestions for profile categories
  396. */
  397. function profile_admin_settings_autocomplete($string) {
  398. $matches = array();
  399. $result = db_select('profile_field')
  400. ->fields('profile_field', array('category'))
  401. ->condition('category', db_like($string) . '%', 'LIKE')
  402. ->range(0, 10)
  403. ->execute();
  404. foreach ($result as $data) {
  405. $matches[$data->category] = check_plain($data->category);
  406. }
  407. drupal_json_output($matches);
  408. }