manifest.pages.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * @file
  4. * Manifest module page callbacks and forms.
  5. */
  6. /**
  7. * Form builder function for manifest admin form.
  8. */
  9. function manifest_admin_list() {
  10. $output = array();
  11. $manifests = manifest_load_multiple();
  12. if (!$manifests) {
  13. $output['no-manifests'] = t('There are currently no manifests configured.');
  14. }
  15. else {
  16. $rows = array();
  17. foreach ($manifests as $manifest) {
  18. $rows[] = array(
  19. array(
  20. 'data' => '<h3>' . manifest_title($manifest) . '</h3><p>' . check_plain($manifest->settings['description']) . '</p>',
  21. 'class' => 'manifest-name',
  22. ),
  23. array(
  24. 'data' =>
  25. l(t('Roles'), 'admin/people/manifest/' . $manifest->name . '/role')
  26. . ' | ' .
  27. l(t('IP addresses'), 'admin/people/manifest/' . $manifest->name . '/ip')
  28. . ' | ' .
  29. l(t('Edit'), 'admin/people/manifest/' . $manifest->name . '/edit')
  30. . ' | ' .
  31. l(t('Delete'), 'admin/people/manifest/' . $manifest->name . '/delete'),
  32. 'class' => 'manifest-ops',
  33. 'align' => 'right',
  34. ),
  35. );
  36. }
  37. $output['manifest-table'] = array(
  38. '#theme' => 'table',
  39. '#rows' => $rows,
  40. '#attributes' => array(
  41. 'class' => 'manifest-table',
  42. ),
  43. );
  44. }
  45. return $output;
  46. }
  47. /**
  48. * Admin manifest add/edit page.
  49. */
  50. function manifest_admin_edit($form, $form_state, $manifest = NULL) {
  51. $form_state['storage']['manifest'] = $manifest;
  52. $form['#tree'] = TRUE;
  53. $machine_name = '';
  54. if (!isset($manifest->name)) {
  55. $form['new'] = array(
  56. '#type' => 'value',
  57. '#value' => TRUE,
  58. );
  59. $form['name'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Machine name'),
  62. '#default_value' => '',
  63. '#maxlength' => 32,
  64. '#size' => 32,
  65. '#required' => TRUE,
  66. '#description' => t('The machine-readable name of this manifest. This name must contain only lowercase letters, numbers, and underscores. This name must be unique.'),
  67. );
  68. }
  69. else {
  70. $form['name'] = array(
  71. '#type' => 'value',
  72. '#value' => $manifest->name,
  73. );
  74. $machine_name = ' ' . t('(machine name: %name)', array('%name' => $manifest->name));
  75. }
  76. $form['settings']['title'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Display title'),
  79. '#default_value' => isset($manifest->settings['title']) ? $manifest->settings['title'] : '',
  80. '#maxlength' => 32,
  81. '#size' => 32,
  82. '#description' => t('The human-readable title of this manifest.') . $machine_name,
  83. );
  84. $form['settings']['description'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Description'),
  87. '#default_value' => isset($manifest->settings['description']) ? $manifest->settings['description'] : '',
  88. '#description' => t('This will appear on the manifest administration page.'),
  89. );
  90. $form['settings']['user'] = array(
  91. '#type' => 'checkboxes',
  92. '#title' => t('Add user accounts'),
  93. '#default_value' => isset($manifest->settings['user']) ? $manifest->settings['user'] : array('profile', 'operations'),
  94. '#options' => array(
  95. 'profile' => t('Through the user profile page'),
  96. 'operations' => t('Through the user update options'),
  97. ),
  98. '#description' => t('How to add user accounts to this manifest.'),
  99. );
  100. $form['config'] = module_invoke_all('manifest_config', 'form', $manifest);
  101. $form['config']['#tree'] = FALSE;
  102. $form['submit'] = array(
  103. '#type' => 'submit',
  104. '#value' => t('Save manifest'),
  105. );
  106. if ($manifest) {
  107. $form['submit']['#suffix'] = l(t('Delete'), 'admin/people/manifest/' . $manifest->name . '/delete');
  108. }
  109. else {
  110. $form['submit']['#suffix'] = l(t('Cancel'), 'admin/people/manifest');
  111. }
  112. return $form;
  113. }
  114. /**
  115. * Validate function for admin manifest add/edit page.
  116. */
  117. function manifest_admin_edit_validate($form, &$form_state) {
  118. $values = &$form_state['values'];
  119. if ($values['new'] == TRUE) {
  120. $manifest_name = trim($values['name']);
  121. if (manifest_load($manifest_name) !== FALSE) {
  122. form_set_error('name', t('The machine-readable name %manifest is already taken.', array('%manifest' => $manifest_name)));
  123. }
  124. if (!preg_match('!^[a-z0-9_]+$!', $manifest_name)) {
  125. form_set_error('name', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  126. }
  127. }
  128. }
  129. /**
  130. * Submit function for admin manifest add/edit page.
  131. */
  132. function manifest_admin_edit_submit($form, &$form_state) {
  133. $values = &$form_state['values'];
  134. $manifest = (object)array(
  135. 'name' => trim($values['name']),
  136. 'settings' => $values['settings'],
  137. );
  138. $fields = module_invoke_all('manifest_config', 'fields', $manifest);
  139. manifest_save($manifest);
  140. manifest_config_delete($manifest->name);
  141. if (!empty($fields)) {
  142. foreach ($fields as $field) {
  143. manifest_config_save($manifest->name, $field, $values[$field]);
  144. }
  145. }
  146. drupal_set_message(t("The manifest was saved."));
  147. drupal_goto("admin/people/manifest");
  148. }
  149. /**
  150. * Admin finder delete page.
  151. */
  152. function manifest_admin_delete($form, $form_state, $manifest) {
  153. $form['#tree'] = TRUE;
  154. $form['name'] = array(
  155. '#type' => 'value',
  156. '#value' => $manifest->name,
  157. );
  158. return confirm_form($form,
  159. t('Are you sure you want to delete the %manifest manifest?', array('%manifest' => manifest_title($manifest))),
  160. $_GET['destination'] ? $_GET['destination'] : 'admin/people/manifest',
  161. t('This action cannot be undone.'),
  162. t('Delete'), t('Cancel'));
  163. }
  164. /**
  165. * Submit function for admin manifest delete page.
  166. */
  167. function manifest_admin_delete_submit($form, &$form_state) {
  168. $manifest_name = &$form_state['values']['name'];
  169. manifest_delete($manifest_name);
  170. drupal_set_message(t("The manifest was deleted."));
  171. drupal_goto("admin/people/manifest");
  172. }
  173. /**
  174. * Admin manifest role page.
  175. */
  176. function manifest_admin_role($form, $form_state, $manifest = NULL) {
  177. drupal_set_title(t('!manifest roles', array('!manifest' => manifest_title($manifest))));
  178. $role_entries = manifest_role_load(NULL, $manifest->name);
  179. $roles = array();
  180. if (is_array($role_entries)) {
  181. foreach ($role_entries as $role_entry) {
  182. $roles[$role_entry->rid] = $role_entry->rid;
  183. }
  184. }
  185. $form['roles'] = array(
  186. '#type' => 'checkboxes',
  187. '#title' => t('User roles'),
  188. '#default_value' => $roles,
  189. '#options' => user_roles(),
  190. '#description' => t('Users in these roles will be added to this Manifest by default.'),
  191. );
  192. $form['manifest'] = array(
  193. '#type' => 'value',
  194. '#value' => $manifest->name,
  195. );
  196. $form['submit'] = array(
  197. '#type' => 'submit',
  198. '#value' => t('Save roles'),
  199. '#suffix' => l(t('Cancel'), 'admin/people/manifest'),
  200. );
  201. return $form;
  202. }
  203. /**
  204. * Submit function for admin manifest role page.
  205. */
  206. function manifest_admin_role_submit($form, &$form_state) {
  207. $values = &$form_state['values'];
  208. $roles = $values['roles'];
  209. foreach ($roles as $key => $value) {
  210. if ($value) {
  211. manifest_role_save($key, $values['manifest']);
  212. }
  213. else {
  214. manifest_role_delete($key, $values['manifest']);
  215. }
  216. }
  217. drupal_set_message(t("The manifest roles were saved."));
  218. drupal_goto("admin/people/manifest");
  219. }
  220. /**
  221. * Admin manifest ip page.
  222. */
  223. function manifest_admin_ip($form, $form_state, $manifest = NULL) {
  224. drupal_set_title(t('!manifest IP addresses', array('!manifest' => manifest_title($manifest))));
  225. $ip_entries = manifest_ip_load(NULL, NULL, $manifest->name);
  226. $rows = array();
  227. $ips = '';
  228. if (is_array($ip_entries)) {
  229. foreach ($ip_entries as $ip_entry) {
  230. $ip = $ip_entry->ip1;
  231. if ($ip_entry->ip2 && $ip_entry->ip2 != $ip_entry->ip1) {
  232. $ip .= ' - ' . $ip_entry->ip2;
  233. }
  234. $rows[] = array(
  235. $ip,
  236. l(t('Delete'), 'admin/people/manifest/' . $manifest->name . '/ip/' . $ip . '/delete'),
  237. );
  238. }
  239. $ips .= theme('table', array(t('IP addresses'), t('Operations')), $rows);
  240. }
  241. $form['ips'] = array(
  242. '#value' => $ips,
  243. );
  244. $form['ip1'] = array(
  245. '#title' => t('IP address, or IP range start'),
  246. '#type' => 'textfield',
  247. '#size' => 15,
  248. '#maxlength' => 15,
  249. );
  250. $form['ip2'] = array(
  251. '#title' => t('IP range end'),
  252. '#type' => 'textfield',
  253. '#size' => 15,
  254. '#maxlength' => 15,
  255. '#description' => t('Leave blank if specifying a single IP address.'),
  256. );
  257. $form['manifest'] = array(
  258. '#type' => 'value',
  259. '#value' => $manifest->name,
  260. );
  261. $form['submit'] = array(
  262. '#type' => 'submit',
  263. '#value' => t('Save IP addresses'),
  264. '#suffix' => l(t('Cancel'), 'admin/people/manifest'),
  265. );
  266. return $form;
  267. }
  268. /**
  269. * Submit function for admin manifest ip page.
  270. */
  271. function manifest_admin_ip_submit($form, &$form_state) {
  272. $values = &$form_state['values'];
  273. $ip1 = $values['ip1'];
  274. $ip2 = $values['ip2'];
  275. manifest_ip_save($ip1, $ip2, $values['manifest']);
  276. drupal_set_message(t("The manifest IP addresses were saved."));
  277. drupal_goto('admin/people/manifest/' . $values['manifest'] . '/ip');
  278. }
  279. /**
  280. * Admin finder delete page.
  281. */
  282. function manifest_admin_ip_delete($form, $form_state, $manifest, $ip) {
  283. $form['#tree'] = TRUE;
  284. $form['name'] = array(
  285. '#type' => 'value',
  286. '#value' => $manifest->name,
  287. );
  288. $form['ip'] = array(
  289. '#type' => 'value',
  290. '#value' => $ip,
  291. );
  292. return confirm_form($form,
  293. t('Are you sure you want to delete %ip from the %manifest manifest?', array('%ip' => $ip, '%manifest' => manifest_title($manifest))),
  294. $_GET['destination'] ? $_GET['destination'] : 'admin/people/manifest',
  295. t('This action cannot be undone.'),
  296. t('Delete'), t('Cancel'));
  297. }
  298. /**
  299. * Submit function for admin manifest delete page.
  300. */
  301. function manifest_admin_ip_delete_submit($form, &$form_state) {
  302. $manifest_name = &$form_state['values']['name'];
  303. $manifest_ip = &$form_state['values']['ip'];
  304. $ips = explode(' - ', $manifest_ip);
  305. manifest_ip_delete($ips[0], $ips[1], $manifest_name);
  306. drupal_set_message(t("The IP address was deleted."));
  307. drupal_goto('admin/people/manifest/' . $manifest_name . '/ip');
  308. }