manifest.module 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @file
  4. * Manifest module.
  5. * Keep lists of users.
  6. *
  7. */
  8. module_load_include('inc', 'manifest', 'manifest.crud');
  9. /**
  10. * Implements hook_permission().
  11. */
  12. function manifest_permission() {
  13. return array(
  14. 'administer manifest' => array(
  15. 'title' => t('Administer manifest'),
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_menu().
  21. */
  22. function manifest_menu() {
  23. $items = array();
  24. // Level 1
  25. $items['admin/people/manifest'] = array(
  26. 'title' => 'Manifests',
  27. 'page callback' => 'manifest_admin_list',
  28. 'access arguments' => array('administer manifest'),
  29. 'type' => MENU_LOCAL_TASK,
  30. 'description' => 'Configure Manifest user lists.',
  31. 'weight' => 0,
  32. 'file' => 'manifest.pages.inc',
  33. );
  34. // Level 2
  35. $items['admin/people/manifest/list'] = array(
  36. 'title' => 'Manifests',
  37. 'page callback' => 'manifest_admin_list',
  38. 'access arguments' => array('administer manifest'),
  39. 'type' => MENU_DEFAULT_LOCAL_TASK,
  40. 'description' => 'Configure Manifest user lists.',
  41. 'weight' => 1,
  42. 'file' => 'manifest.pages.inc',
  43. );
  44. $items['admin/people/manifest/add'] = array(
  45. 'title' => 'Add manifest',
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('manifest_admin_edit'),
  48. 'access arguments' => array('administer manifest'),
  49. 'type' => MENU_LOCAL_ACTION,
  50. 'description' => 'Create Manifest user list.',
  51. 'weight' => 2,
  52. 'file' => 'manifest.pages.inc',
  53. 'context' => MENU_CONTEXT_PAGE,
  54. );
  55. // Level 3
  56. $items['admin/people/manifest/%manifest'] = array(
  57. 'title' => 'Edit manifest',
  58. 'page callback' => 'drupal_get_form',
  59. 'page arguments' => array('manifest_admin_edit', 3),
  60. 'access arguments' => array('administer manifest'),
  61. 'type' => MENU_NORMAL_ITEM,
  62. 'weight' => 7,
  63. 'file' => 'manifest.pages.inc',
  64. );
  65. $items['admin/people/manifest/%manifest/edit'] = array(
  66. 'title' => 'Edit manifest',
  67. 'page callback' => 'drupal_get_form',
  68. 'page arguments' => array('manifest_admin_edit', 3),
  69. 'access arguments' => array('administer manifest'),
  70. 'type' => MENU_DEFAULT_LOCAL_TASK,
  71. 'weight' => 7,
  72. 'file' => 'manifest.pages.inc',
  73. 'context' => MENU_CONTEXT_PAGE,
  74. );
  75. $items['admin/people/manifest/%manifest/role'] = array(
  76. 'title' => 'Manifest roles',
  77. 'page callback' => 'drupal_get_form',
  78. 'page arguments' => array('manifest_admin_role', 3),
  79. 'access arguments' => array('administer manifest'),
  80. 'type' => MENU_LOCAL_TASK,
  81. 'weight' => 4,
  82. 'file' => 'manifest.pages.inc',
  83. 'context' => MENU_CONTEXT_PAGE,
  84. );
  85. $items['admin/people/manifest/%manifest/delete'] = array(
  86. 'title' => 'Delete manifest',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('manifest_admin_delete', 3),
  89. 'access arguments' => array('administer manifest'),
  90. 'type' => MENU_LOCAL_TASK,
  91. 'weight' => 8,
  92. 'file' => 'manifest.pages.inc',
  93. 'context' => MENU_CONTEXT_PAGE,
  94. );
  95. $items['admin/people/manifest/%manifest/ip'] = array(
  96. 'title' => 'Manifest IP addresses',
  97. 'page callback' => 'drupal_get_form',
  98. 'page arguments' => array('manifest_admin_ip', 3),
  99. 'access arguments' => array('administer manifest'),
  100. 'type' => MENU_LOCAL_TASK,
  101. 'weight' => 5,
  102. 'file' => 'manifest.pages.inc',
  103. 'context' => MENU_CONTEXT_PAGE,
  104. );
  105. // Level 4
  106. $items['admin/people/manifest/%manifest/ip/%'] = array(
  107. 'title' => 'Manifest IP addresses',
  108. 'page callback' => 'drupal_get_form',
  109. 'page arguments' => array('manifest_admin_ip', 3),
  110. 'access arguments' => array('administer manifest'),
  111. 'type' => MENU_NORMAL_ITEM,
  112. 'weight' => 6,
  113. 'file' => 'manifest.pages.inc',
  114. 'context' => MENU_CONTEXT_PAGE,
  115. );
  116. $items['admin/people/manifest/%manifest/ip/%/delete'] = array(
  117. 'title' => 'Delete IP address',
  118. 'page callback' => 'drupal_get_form',
  119. 'page arguments' => array('manifest_admin_ip_delete', 3, 5),
  120. 'access arguments' => array('administer manifest'),
  121. 'type' => MENU_LOCAL_TASK,
  122. 'weight' => 6,
  123. 'file' => 'manifest.pages.inc',
  124. 'context' => MENU_CONTEXT_PAGE,
  125. );
  126. return $items;
  127. }
  128. /**
  129. * Implements hook_user_operations().
  130. */
  131. function manifest_user_operations() {
  132. $operations = array();
  133. $manifests = manifest_load_multiple();
  134. if ($manifests) {
  135. foreach ($manifests as $manifest) {
  136. if (!empty($manifest->settings['user']['operations'])) {
  137. $operations['manifest_add_' . $manifest->name] = array(
  138. 'label' => t('Add users to !manifest manifest', array('!manifest' => manifest_title($manifest))),
  139. 'callback' => 'manifest_user_operations_add',
  140. 'callback arguments' => array($manifest->name),
  141. );
  142. $operations['manifest_remove_' . $manifest->name] = array(
  143. 'label' => t('Remove users from !manifest manifest', array('!manifest' => manifest_title($manifest))),
  144. 'callback' => 'manifest_user_operations_remove',
  145. 'callback arguments' => array($manifest->name),
  146. );
  147. }
  148. }
  149. }
  150. return $operations;
  151. }
  152. /**
  153. * Callback function for hook_user_operations().
  154. */
  155. function manifest_user_operations_add($accounts, $manifest_name) {
  156. foreach ($accounts as $uid) {
  157. manifest_account_save($uid, $manifest_name);
  158. }
  159. }
  160. /**
  161. * Callback function for hook_user_operations().
  162. */
  163. function manifest_user_operations_remove($accounts, $manifest_name) {
  164. foreach ($accounts as $uid) {
  165. manifest_account_delete($uid, $manifest_name);
  166. }
  167. }
  168. /**
  169. * Implements hook_form_FORM_ID_alter().
  170. */
  171. function manifest_form_user_account_form_alter(&$form, &$form_state) {
  172. if (user_access('administer manifest')) {
  173. $account = $form['#user'];
  174. $manifests = manifest_load_multiple();
  175. $manifest_options = array();
  176. if ($manifests) {
  177. foreach ($manifests as $manifest) {
  178. if (!empty($manifest->settings['user']['profile'])) {
  179. $manifest_options[$manifest->name] = manifest_title($manifest);
  180. }
  181. }
  182. }
  183. if (!empty($manifest_options)) {
  184. $default = array();
  185. $entries = manifest_account_load($account->uid);
  186. if ($entries) {
  187. foreach ($entries as $entry) {
  188. $default[] = $entry->manifest;
  189. }
  190. }
  191. $form['manifest'] = array(
  192. '#type' => 'fieldset',
  193. '#title' => t('Manifest'),
  194. );
  195. $form['manifest']['manifests'] = array(
  196. '#type' => 'checkboxes',
  197. '#title' => t('Manifests'),
  198. '#options' => $manifest_options,
  199. '#default_value' => $default,
  200. '#description' => t('Add this user to manifests.'),
  201. );
  202. }
  203. }
  204. }
  205. /**
  206. * Implements hook_user_update().
  207. */
  208. function hook_user_update(&$edit, $account, $category) {
  209. if (!empty($edit['manifests'])) {
  210. if (is_string($edit['manifests'])) {
  211. $edit['manifests'] = array($edit['manifests'] => $edit['manifests']);
  212. }
  213. foreach ($edit['manifests'] as $manifest_name => $manifest) {
  214. if ($manifest) {
  215. manifest_account_save($account->uid, $manifest_name);
  216. }
  217. else {
  218. manifest_account_delete($account->uid, $manifest_name);
  219. }
  220. }
  221. unset($edit['manifests']);
  222. }
  223. }
  224. /**
  225. * Get the display title of a manifest.
  226. */
  227. function manifest_title($manifest) {
  228. if (!empty($manifest->settings['title'])) {
  229. return check_plain($manifest->settings['title']);
  230. }
  231. return drupal_ucfirst(check_plain($manifest->name));
  232. }
  233. // @todo Make a page that lists users.