mb_user.module 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * @file
  4. * Provides additional buttons for content in user context.
  5. *
  6. * Currently available context:
  7. * - User account
  8. *
  9. * Currently available buttons:
  10. * - Cancel
  11. * - Save and Continue
  12. * - Save and create new
  13. *
  14. * The "Save and create new" button is only usable for users wth the permission "Administer users".
  15. */
  16. /**
  17. * Implements hook_permission().
  18. */
  19. function mb_user_permission() {
  20. return array(
  21. 'access mb user' => array(
  22. 'title' => t('Use More User Buttons'),
  23. 'description' => t('Use the buttons defined by More Buttons User.')
  24. )
  25. );
  26. }
  27. /**
  28. * Implements hook_menu().
  29. */
  30. function mb_user_menu() {
  31. $items = array();
  32. $items['admin/config/mb/buttons/more-buttons-user'] = array(
  33. 'page callback' => 'drupal_get_form',
  34. 'page arguments' => array('mb_user_admin'),
  35. 'title' => 'Users',
  36. 'access arguments' => array('administer site configuration'),
  37. 'description' => 'An overview of what page types uses buttons of the MB User module.',
  38. 'file' => 'mb_user.admin.inc',
  39. 'type' => MENU_LOCAL_TASK,
  40. 'weight' => 11
  41. );
  42. $items['admin/config/mb/buttons/more-buttons-user/reset'] = array(
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('mb_user_reset'),
  45. 'access arguments' => array('administer site configuration'),
  46. 'type' => MENU_CALLBACK,
  47. 'file' => 'mb_user.admin.inc'
  48. );
  49. return $items;
  50. }
  51. /**
  52. * Implements hook_theme().
  53. */
  54. function mb_user_theme() {
  55. return array(
  56. 'mb_user_admin' => array(
  57. 'variables' => array('form' => NULL),
  58. )
  59. );
  60. }
  61. /**
  62. * Implements hook_form_alter().
  63. */
  64. function mb_user_form_alter(&$form, &$form_state, $form_id) {
  65. $module = 'mb_user';
  66. switch ($form_id) {
  67. case 'user_profile_form':
  68. $default_values = mb_default_values();
  69. $mb_user_values = mb_get_values('mb');
  70. if (!isset($form['actions']['submit']['#weight'])) {
  71. $form['actions']['submit']['#weight'] = 10;
  72. }
  73. if (isset($form['actions']['cancel'])) {
  74. $form['actions']['cancel']['#weight'] = 25;
  75. }
  76. $settings = array();
  77. $settings['cancel'] = variable_get($module . '_cancel_user_account', 0);
  78. $settings['sac'] = variable_get($module . '_sac_user_account', 0);
  79. $settings['sacn'] = variable_get($module . '_sacn_user_account', 0);
  80. // The "Cancel" form element on user account page.
  81. if ($settings['cancel'] > 0) {
  82. if ($settings['cancel'] == 1) {
  83. $weight_cancel = $form['actions']['submit']['#weight'] - 1;
  84. }
  85. elseif ($settings['cancel'] == 2) {
  86. $weight_cancel = 16;
  87. }
  88. // Define the "Cancel" form element.
  89. $form['actions']['cancel_mb'] = array(
  90. '#type' => 'submit',
  91. '#value' => isset($mb_user_values['cancel']) ? t('@cancel-value', array('@cancel-value' => t($mb_user_values['cancel']))) : t($default_values['cancel']),
  92. '#weight' => $weight_cancel,
  93. '#validate' => array('mb_user_cancel_validate')
  94. );
  95. }
  96. // The "Save and continue" form element on user account page.
  97. if ($settings['sac'] > 0) {
  98. // Left
  99. if ($settings['sac'] == 1) {
  100. $weight_sac = $form['actions']['submit']['#weight'] - 0.025;
  101. }
  102. // Right
  103. if ($settings['sac'] == 2) {
  104. $weight_sac = $form['actions']['submit']['#weight'] - 0.050;
  105. }
  106. //
  107. if ($settings['sac'] == 3) {
  108. $weight_sac = $form['actions']['submit']['#weight'] + 1.025;
  109. }
  110. //
  111. if ($settings['sac'] == 4) {
  112. $weight_sac = $form['actions']['submit']['#weight'] + 1.050;
  113. }
  114. // Define the "Save and continue" form element.
  115. $submit = $form['#submit'];
  116. $submit[] = 'mb_user_sac_submit';
  117. $form['actions']['sac'] = array(
  118. '#type' => 'submit',
  119. '#value' => isset($mb_user_values['sac']) ? t('@sac-value', array('@sac-value' => t($mb_user_values['sac']))) : t($default_values['sac']),
  120. '#weight' => $weight_sac,
  121. '#submit' => $submit
  122. );
  123. }
  124. // The "Save and create new" form element on user account page.
  125. if ($settings['sacn'] > 0 && user_access('administer users')) {
  126. //
  127. if ($settings['sacn'] == 1) {
  128. $weight_sacn = $form['actions']['submit']['#weight'] - 0.025;
  129. }
  130. //
  131. if ($settings['sacn'] == 2) {
  132. $weight_sacn = $form['actions']['submit']['#weight'] - 0.050;
  133. }
  134. //
  135. if ($settings['sacn'] == 3) {
  136. $weight_sacn = $form['actions']['submit']['#weight'] + 1.025;
  137. }
  138. //
  139. if ($settings['sacn'] == 4) {
  140. $weight_sacn = $form['actions']['submit']['#weight'] + 1.050;
  141. }
  142. // Define the "Save and create new" form element.
  143. $submit = $form['#submit'];
  144. $submit[] = 'mb_user_sacn_submit';
  145. $form['actions']['sacn'] = array(
  146. '#type' => 'submit',
  147. '#value' => isset($mb_user_values['sacn']) ? t('@sacn-value', array('@sacn-value' => t($mb_user_values['sacn']))) : t($default_values['sacn']),
  148. '#weight' => $weight_sacn,
  149. '#submit' => $submit
  150. );
  151. }
  152. break;
  153. }
  154. }
  155. /**
  156. * Implements hook_form_validate().
  157. *
  158. * Handle the "Cancel" button validation.
  159. */
  160. function mb_user_cancel_validate($form, &$form_state) {
  161. // This is the cancel action. No validation required.
  162. mb_user_cancel_action($form, $form_state);
  163. }
  164. /**
  165. * The "Cancel" button action.
  166. *
  167. * @see mb_user_cancel_validate()
  168. */
  169. function mb_user_cancel_action($form, &$form_state) {
  170. // Hide the error messages.
  171. drupal_get_messages('error');
  172. $redirect = 'user/' . $form['#user']->uid;
  173. drupal_goto($redirect);
  174. }
  175. /**
  176. * Implements hook_form_submit().
  177. *
  178. * Handle the "Save and continue" button action.
  179. */
  180. function mb_user_sac_submit($form, &$form_state) {
  181. $redirect = 'user/' . $form['#user']->uid . '/edit';
  182. drupal_goto($redirect);
  183. }
  184. /**
  185. * Implements hook_form_submit().
  186. *
  187. * Handle the "Save and create new" button action.
  188. */
  189. function mb_user_sacn_submit($form, &$form_state) {
  190. $redirect = 'admin/people/create';
  191. drupal_goto($redirect);
  192. }
  193. /**
  194. * Get the types of pages allowed to use more buttons.
  195. *
  196. * At the moment are only supported the user accounts.
  197. *
  198. * @return array
  199. */
  200. function mb_user_type_get_types() {
  201. $account = array();
  202. $account['type'] = 'user_account';
  203. $account['name'] = t('User account');
  204. $page_types = array('user_account' => $account);
  205. return $page_types;
  206. }