og_ui.admin.inc

  1. cis7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  2. cle7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  3. ecd7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  4. elmsmedia7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  5. harmony7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  6. icor7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  7. meedjum_blog7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
  8. mooc7 sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc

Admin settings for Organic groups module.

Functions

Namesort descending Description
og_ui_add_users Add users to group form.
og_ui_add_users_submit Submit handler; Add users to group.
og_ui_add_users_validate Validate handler; Add users to group.
og_ui_admin_account Form builder; OG user administration page.
og_ui_admin_add_user_ajax_callback AJAX callback to attach the group membership type fields to the form.
og_ui_admin_fields_ajax_callback AJAX callback to to return the field name if the field supports "mulitple" instances, on the same bundle (e.g. OG_AUDIENCE_FIELD).
og_ui_admin_permissions Menu callback: administer permissions.
og_ui_admin_permissions_after_build Helper function to disable the permissions checkboxes.
og_ui_admin_permissions_submit Save permissions selected on the administer permissions page.
og_ui_admin_roles Menu callback: administer roles.
og_ui_admin_roles_submit Submit handler; OG roles.
og_ui_admin_roles_validate Validate handler; OG roles.
og_ui_admin_settings Menu callback; Admin settings form.
og_ui_delete_membership Delete a single membership from group.
og_ui_delete_membership_submit Submit handler;Delete the membership.
og_ui_edit_membership Add Edit membership form.
og_ui_edit_membership_submit Submit handler; Edit membership in group.
og_ui_edit_membership_validate Validate handler; Edit membership in group.
og_ui_field_delete_form Menu callback; present a form for removing a field instance from a bundle.
og_ui_field_delete_form_submit
og_ui_field_settings Allow site admin to add or remove group fields from fieldable entities.
og_ui_field_settings_submit Submit handler; Attach field can to bundle.
og_ui_field_settings_validate Validate handler; Check if field can be attached to bundle.
og_ui_group_admin_overview Provide an overview of the administrator menu items.
og_ui_group_types_overview Display an overview of group types with edit link.
theme_og_ui_admin_new_role Theme the new role form.
theme_og_ui_admin_permissions Theme the administer permissions page.
_og_ui_get_role_names Helper function to get role names.

File

sites/all/modules/ulmus/og/og_ui/og_ui.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin settings for Organic groups module.
  5. */
  6. /**
  7. * Menu callback; Admin settings form.
  8. */
  9. function og_ui_admin_settings($form_state) {
  10. $form = array();
  11. $form['og_group_manager_full_access'] = array(
  12. '#type' => 'checkbox',
  13. '#title' => t('Group manager full permissions'),
  14. '#description' => t('When enabled the group manager will have all the permissions in the group.'),
  15. '#default_value' => variable_get('og_group_manager_full_access', TRUE),
  16. );
  17. $form['og_node_access_strict'] = array(
  18. '#type' => 'checkbox',
  19. '#title' => t('Strict node access permissions'),
  20. '#description' => t('When enabled Organic groups will restrict permissions for creating, updating and deleting according to the Organic groups access settings. Example: A content editor with the <em>Edit any page content</em> permission who is not a member of a group would be denied access to modifying page content in that group. (For restricting view access use the Organic groups access control module.)'),
  21. '#default_value' => variable_get('og_node_access_strict', TRUE),
  22. );
  23. $form['og_ui_admin_people_view'] = array(
  24. '#type' => 'select',
  25. '#title' => t('Admin people View'),
  26. '#description' => t('Select the View that should be used to show and control the people in the group.'),
  27. '#options' => views_get_views_as_options(),
  28. '#default_value' => variable_get('og_ui_admin_people_view', 'og_members_admin:default'),
  29. '#required' => TRUE,
  30. );
  31. if ($group_bundles = og_get_all_group_bundle()) {
  32. $form['og_group_manager_rids'] = array(
  33. '#type' => 'fieldset',
  34. '#title' => t('Group manager default roles'),
  35. '#description' => t('Select the role(s) a group manager will be granted upon creating a new group.'),
  36. );
  37. // Add group manager default roles.
  38. $entity_info = entity_get_info();
  39. foreach ($group_bundles as $entity_type => $bundles) {
  40. foreach ($bundles as $bundle_name => $bundle_label) {
  41. $og_roles = og_roles($entity_type, $bundle_name, 0, FALSE, FALSE);
  42. if (!$og_roles) {
  43. continue;
  44. }
  45. $params = array(
  46. '@entity-label' => $entity_info[$entity_type]['label'],
  47. '@bundle-label' => $bundle_label,
  48. );
  49. $name = 'og_group_manager_default_rids_' . $entity_type . '_' . $bundle_name;
  50. $form['og_group_manager_rids'][$name] = array(
  51. '#type' => 'select',
  52. '#title' => t('Roles in @entity-label - @bundle-label', $params),
  53. '#options' => $og_roles,
  54. '#multiple' => TRUE,
  55. '#default_value' => variable_get($name, array()),
  56. );
  57. }
  58. }
  59. }
  60. $form['og_features_ignore_og_fields'] = array(
  61. '#type' => 'checkbox',
  62. '#title' => t('Prevent "Features" export piping'),
  63. '#description' => t('When exporting using Features module a content-type, this will prevent from OG related fields to be exported.'),
  64. '#default_value' => variable_get('og_features_ignore_og_fields', FALSE),
  65. '#access' => module_exists('features'),
  66. );
  67. $form['og_use_queue'] = array(
  68. '#type' => 'checkbox',
  69. '#title' => t('Use queue'),
  70. '#description' => t("Use the core's queue process to operations such as deleting memberships when groups are deleted."),
  71. '#default_value' => variable_get('og_use_queue', FALSE),
  72. );
  73. $form['og_orphans_delete'] = array(
  74. '#type' => 'checkbox',
  75. '#title' => t('Delete orphans'),
  76. '#description' => t('Delete "Orphan" group-content (not including useres), when the group is deleted.'),
  77. '#default_value' => variable_get('og_orphans_delete', FALSE),
  78. '#states' => array(
  79. 'visible' => array(
  80. ':input[name="og_use_queue"]' => array('checked' => TRUE),
  81. ),
  82. ),
  83. '#attributes' => array(
  84. 'class' => array('entityreference-settings'),
  85. ),
  86. );
  87. // Re-use Entity-reference CSS for indentation.
  88. $form['#attached']['css'][] = drupal_get_path('module', 'entityreference') . '/entityreference.admin.css';
  89. return system_settings_form($form);
  90. }
  91. /**
  92. * Provide an overview of the administrator menu items.
  93. *
  94. * @param $entity_type
  95. * The entity type.
  96. * @param $etid
  97. * The entity ID.
  98. */
  99. function og_ui_group_admin_overview($entity_type, $etid) {
  100. og_set_breadcrumb($entity_type, $etid);
  101. $items = og_ui_get_group_admin($entity_type, $etid);
  102. foreach ($items as &$item) {
  103. // Re-format the URL.
  104. $item['href'] = "group/$entity_type/$etid/" . $item['href'];
  105. // Imitate the menu_get_item() content, so we can pass it to
  106. // theme_admin_block_content(), so we add the localized_options key.
  107. $item['localized_options'] = array();
  108. }
  109. $form['admin_block_content'] = array('#theme' => 'admin_block_content', '#content' => $items);
  110. return $form;
  111. }
  112. /**
  113. * Add users to group form.
  114. */
  115. function og_ui_add_users($form, &$form_state, $group_type, $gid) {
  116. og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  117. $group = entity_load_single($group_type, $gid);
  118. $label = entity_label($group_type, $group);
  119. list(,, $bundle) = entity_extract_ids($group_type, $group);
  120. $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  121. $form['gid'] = array('#type' => 'value', '#value' => $gid);
  122. $form['og_user'] = array(
  123. '#type' => 'fieldset',
  124. '#title' => t('Add a group member to %group', array('%group' => $label)),
  125. );
  126. $form['og_user']['name'] = array(
  127. '#type' => 'textfield',
  128. '#title' => t('User name'),
  129. '#autocomplete_path' => 'user/autocomplete',
  130. '#required' => TRUE,
  131. );
  132. $form['og_user']['state'] = array(
  133. '#type' => 'value',
  134. '#value' => OG_STATE_ACTIVE,
  135. );
  136. // Get all the non-default roles.
  137. if ($og_roles = og_roles($group_type, $bundle, $gid, FALSE, FALSE)) {
  138. $form['og_user']['roles'] = array(
  139. '#type' => 'checkboxes',
  140. '#options' => $og_roles,
  141. '#title' => t('Roles'),
  142. );
  143. }
  144. $field_names = og_get_group_audience_fields('user', 'user', $group_type, $bundle);
  145. $field_name = !empty($form_state['values']['field_name']) ? $form_state['values']['field_name'] : key($field_names);
  146. if (count($field_names) > 1) {
  147. $form['og_user']['field_name'] = array(
  148. '#type' => 'select',
  149. '#title' => t('Field name'),
  150. '#options' => $field_names,
  151. '#default_value' => $field_name,
  152. '#description' => t('Select the field name, the group membership should be registered in.'),
  153. '#ajax' => array(
  154. 'callback' => 'og_ui_admin_add_user_ajax_callback',
  155. 'wrapper' => 'og-ui-field-name',
  156. ),
  157. );
  158. }
  159. else {
  160. // There is only a single field name, so just pass it as a value.
  161. $form['og_user']['field_name'] = array(
  162. '#type' => 'value',
  163. '#value' => $field_name,
  164. );
  165. }
  166. // Add group membership form.
  167. $values = array();
  168. // Add group membership form. We still don't have the user or state.
  169. $og_membership = og_membership_create($group_type, $gid, 'user', 0, $field_name, $values);
  170. $form_state['og_membership'] = $og_membership;
  171. $form['membership_fields'] = array(
  172. '#prefix' => '<div id="og-ui-field-name">',
  173. '#suffix' => '</div>',
  174. '#tree' => TRUE,
  175. '#parents' => array('membership_fields'),
  176. );
  177. field_attach_form('og_membership', $og_membership, $form['membership_fields'], $form_state);
  178. $form['actions'] = array('#type' => 'actions');
  179. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add users'));
  180. return $form;
  181. }
  182. /**
  183. * AJAX callback to attach the group membership type fields to the form.
  184. *
  185. * Since the controlling logic for populating the form is in the form builder
  186. * function, all we do here is select the element and return it to be updated.
  187. */
  188. function og_ui_admin_add_user_ajax_callback(array $form, array &$form_state) {
  189. return $form['membership_fields'];
  190. }
  191. /**
  192. * Validate handler; Add users to group.
  193. */
  194. function og_ui_add_users_validate($form, &$form_state) {
  195. $account = user_load_by_name($form_state['values']['name']);
  196. $group_type = $form_state['values']['group_type'];
  197. $gid = $form_state['values']['gid'];
  198. $field_name = $form_state['values']['field_name'];
  199. $params = array('%name' => format_username($account));
  200. if (!$account) {
  201. form_set_error('name', t('You have entered an invalid user name.'));
  202. return;
  203. }
  204. elseif (og_get_membership($group_type, $gid, 'user', $account->uid)) {
  205. form_set_error('name', t('User %name is already subscribed to group.', $params));
  206. }
  207. // Check field cardinality.
  208. elseif (!og_check_field_cardinality('user', $account, $field_name)) {
  209. $instance = field_info_instance('user', $field_name, 'user');
  210. $params['%field'] = $instance['label'];
  211. form_set_error('field_name', t('%name has reached maximum values in field %field.', $params));
  212. }
  213. else {
  214. $og_membership = $form_state['og_membership'];
  215. field_attach_form_validate('og_membership', $og_membership, $form['membership_fields'], $form_state);
  216. }
  217. }
  218. /**
  219. * Submit handler; Add users to group.
  220. */
  221. function og_ui_add_users_submit($form, &$form_state) {
  222. $group_type = $form_state['values']['group_type'];
  223. $gid = $form_state['values']['gid'];
  224. $state = $form_state['values']['state'];
  225. $og_membership = $form_state['og_membership'];
  226. // Set the user and state.
  227. field_attach_submit('og_membership', $og_membership, $form['membership_fields'], $form_state);
  228. $account = user_load_by_name($form_state['values']['name']);
  229. // Ungroup user, in case they were already registered.
  230. og_ungroup($group_type, $gid, 'user', $account);
  231. $og_membership->etid = $account->uid;
  232. $og_membership->state = $state;
  233. $og_membership->save();
  234. // Assign roles.
  235. if (!empty($form_state['values']['roles'])) {
  236. foreach ($form_state['values']['roles'] as $rid) {
  237. og_role_grant($group_type, $gid, $og_membership->etid, $rid);
  238. }
  239. }
  240. $group = entity_load_single($group_type, $gid);
  241. drupal_set_message(t('%user has been added to the group %group-title.', array('%user' => format_username($account), '%group-title' => entity_label($group_type, $group))));
  242. }
  243. /**
  244. * Add Edit membership form.
  245. */
  246. function og_ui_edit_membership($form, &$form_state, $group_type, $gid, $og_membership) {
  247. og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  248. $group = entity_load_single($group_type, $gid);
  249. $label = entity_label($group_type, $group);
  250. $account = user_load($og_membership->etid);
  251. list(,, $bundle) = entity_extract_ids($group_type, $group);
  252. // Get all the non-default roles.
  253. $og_roles = og_roles($group_type, $bundle, $gid, FALSE, FALSE);
  254. $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  255. $form['gid'] = array('#type' => 'value', '#value' => $gid);
  256. $form['id'] = array('#type' => 'value', '#value' => $og_membership->id);
  257. $form['og_user'] = array(
  258. '#type' => 'fieldset',
  259. '#title' => t('Edit a group membership in %group', array('%group' => $label)),
  260. );
  261. $form['og_user']['name'] = array(
  262. '#type' => 'markup',
  263. '#title' => t('User name'),
  264. '#markup' => $account->name,
  265. );
  266. $form['og_user']['state'] = array(
  267. '#type' => 'select',
  268. '#title' => t('Status'),
  269. '#description' => t('Change the current membership status.'),
  270. '#options' => og_group_content_states(),
  271. '#default_value' => $og_membership->state,
  272. );
  273. if ($og_roles) {
  274. $form['og_user']['roles'] = array(
  275. '#type' => 'checkboxes',
  276. '#options' => $og_roles,
  277. '#title' => t('Roles'),
  278. '#default_value' => array_keys(og_get_user_roles($group_type, $gid, $account->uid)),
  279. );
  280. }
  281. // Add group membership form. We still don't have the user or state.
  282. $form_state['og_membership'] = $og_membership;
  283. $form['membership_fields'] = array(
  284. '#prefix' => '<div id="og-ui-field-name">',
  285. '#suffix' => '</div>',
  286. '#tree' => TRUE,
  287. '#parents' => array('membership_fields'),
  288. );
  289. field_attach_form('og_membership', $og_membership, $form['membership_fields'], $form_state);
  290. $form['actions'] = array('#type' => 'actions');
  291. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Update membership'));
  292. // You can't delete the group manager.
  293. if ($group->uid != $account->uid) {
  294. $form['actions']['delete'] = array(
  295. '#type' => 'markup',
  296. '#markup' => l(t('Remove'), 'group/node/' . $gid . '/admin/people/delete-membership/' . $og_membership->id),
  297. );
  298. }
  299. return $form;
  300. }
  301. /**
  302. * Validate handler; Edit membership in group.
  303. */
  304. function og_ui_edit_membership_validate($form, &$form_state) {
  305. $og_membership = $form_state['og_membership'];
  306. field_attach_form_validate('og_membership', $og_membership, $form['membership_fields'], $form_state);
  307. }
  308. /**
  309. * Submit handler; Edit membership in group.
  310. */
  311. function og_ui_edit_membership_submit($form, &$form_state) {
  312. $group_type = $form_state['values']['group_type'];
  313. $gid = $form_state['values']['gid'];
  314. $og_membership = $form_state['og_membership'];
  315. $og_membership->state = $form_state['values']['state'];
  316. field_attach_submit('og_membership', $og_membership, $form['membership_fields'], $form_state);
  317. $account = user_load($og_membership->etid);
  318. // Assign roles.
  319. $og_roles = og_get_user_roles($group_type, $gid, $account->uid);
  320. foreach (array_keys($og_roles) as $rid) {
  321. if (!in_array($rid, $form_state['values']['roles'])) {
  322. og_role_revoke($group_type, $gid, $account->uid, $rid);
  323. }
  324. }
  325. if (!empty($form_state['values']['roles'])) {
  326. foreach ($form_state['values']['roles'] as $rid) {
  327. og_role_grant($group_type, $gid, $og_membership->etid, $rid);
  328. }
  329. }
  330. // Saving should be last in the process, so Rules can get a chance to
  331. // assign or revoke roles.
  332. $og_membership->save();
  333. drupal_set_message(t('The membership has been updated.'));
  334. }
  335. /**
  336. * Delete a single membership from group.
  337. */
  338. function og_ui_delete_membership($form, &$form_state, $group_type, $gid, $og_membership) {
  339. og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  340. $group = entity_load_single($group_type, $gid);
  341. $label = entity_label($group_type, $group);
  342. $account = user_load($og_membership->etid);
  343. // You can't delete the group manager.
  344. if ($group->uid == $account->uid) {
  345. drupal_set_message(t("You can't remove membership of the group manager"));
  346. drupal_goto('group/' . $group_type . '/' . $gid . '/admin/people');
  347. }
  348. // Add group membership form. We still don't have the user or state.
  349. $form_state['og_membership'] = $og_membership;
  350. return confirm_form($form,
  351. t('Remove membership in group @group', array('@group' => $label)),
  352. 'group/' . $group_type . '/' . $gid . '/admin/people',
  353. t('Are you sure you would like to remove the membership for the user @user', array('@user' => $account->name)),
  354. t('Remove'),
  355. t('Cancel')
  356. );
  357. }
  358. /**
  359. * Submit handler;Delete the membership.
  360. */
  361. function og_ui_delete_membership_submit($form, &$form_state) {
  362. $membership = $form_state['og_membership'];
  363. $membership->delete();
  364. $form_state['redirect'] = 'group/' . $membership->group_type . '/' . $membership->gid . '/admin/people';
  365. drupal_set_message(t('The membership was removed.'));
  366. }
  367. /**
  368. * Form builder; OG user administration page.
  369. */
  370. function og_ui_admin_account($group_type, $gid) {
  371. og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  372. // $view = explode(':', variable_get)
  373. list($view_name, $display) = explode(':', variable_get('og_ui_admin_people_view', 'og_members_admin:default'));
  374. // We can't use views_embed_view() as we have exposed filter, and we need to
  375. // make sure the path is overriden to the current URL.
  376. // @see http://drupal.org/node/525592#comment-1968604
  377. $view = views_get_view($view_name, $display);
  378. $view->set_arguments(array($group_type, $gid));
  379. $view->override_url = $_GET['q'];
  380. return $view->preview();
  381. }
  382. /**
  383. * Display an overview of group types with edit link.
  384. *
  385. * @param $type
  386. * Either 'roles' or 'permissions'. Determines the edit link url.
  387. *
  388. * @return
  389. * Renderable table of group types.
  390. */
  391. function og_ui_group_types_overview($type) {
  392. $header = array(t('Group type'), t('Operations'));
  393. $rows = array();
  394. foreach (og_get_all_group_bundle() as $entity_type => $bundles) {
  395. $entity_info = entity_get_info($entity_type);
  396. foreach ($bundles as $bundle_name => $bundle_label) {
  397. $row = array();
  398. $row[] = array('data' => check_plain($entity_info['label'] . ' - ' . $bundle_label));
  399. $row[] = array('data' => l(t('edit'), "admin/config/group/$type/$entity_type/$bundle_name"));
  400. $rows[] = $row;
  401. }
  402. }
  403. $build['roles_table'] = array(
  404. '#theme' => 'table',
  405. '#header' => $header,
  406. '#rows' => $rows,
  407. '#empty' => t('No group types available. Edit or <a href="@url">add a new content type</a> and set it to behave as a group.', array('@url' => url('admin/structure/types/add'))),
  408. );
  409. return $build;
  410. }
  411. /**
  412. * Menu callback: administer roles.
  413. *
  414. * @ingroup forms
  415. * @see og_user_admin_roles_validate()
  416. * @see og_user_admin_roles_submit()
  417. * @see theme_group_user_admin_new_role()
  418. */
  419. function og_ui_admin_roles($form, $form_state, $group_type = '', $gid = 0, $bundle = '', $rid = 0) {
  420. if ($gid) {
  421. og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")), l(t('Roles'), "group/$group_type/$gid/admin/roles"));
  422. }
  423. if ($rid) {
  424. // Display the edit role form.
  425. $role = og_role_load($rid);
  426. // Get group type and bundle from role.
  427. $bundle = $role->group_bundle;
  428. $group_type = $role->group_type;
  429. $form['name'] = array(
  430. '#type' => 'textfield',
  431. '#title' => t('Role name'),
  432. '#default_value' => $role->name,
  433. '#size' => 30,
  434. '#required' => TRUE,
  435. '#maxlength' => 64,
  436. '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
  437. );
  438. $form['rid'] = array(
  439. '#type' => 'value',
  440. '#value' => $rid,
  441. );
  442. $form['actions'] = array('#type' => 'actions');
  443. $form['actions']['submit'] = array(
  444. '#type' => 'submit',
  445. '#value' => t('Save role'),
  446. );
  447. $form['actions']['delete'] = array(
  448. '#type' => 'submit',
  449. '#value' => t('Delete role'),
  450. );
  451. }
  452. else {
  453. if ($gid) {
  454. $group = entity_load_single($group_type, $gid);
  455. list(,, $bundle) = entity_extract_ids($group_type, $group);
  456. }
  457. // No role ID.
  458. if (!$gid || ($group && !og_is_group_default_access($group_type, $group))) {
  459. $form['name'] = array(
  460. '#type' => 'textfield',
  461. '#title' => t('Name'),
  462. '#title_display' => 'invisible',
  463. '#size' => 32,
  464. '#maxlength' => 64,
  465. );
  466. $form['add'] = array(
  467. '#type' => 'submit',
  468. '#value' => t('Add role'),
  469. '#validate' => array('og_ui_admin_roles_validate'),
  470. '#submit' => array('og_ui_admin_roles_submit'),
  471. );
  472. // Attach the CSS to the form.
  473. $form['#attached'] = array(
  474. 'css' => array (
  475. 'type' => 'file',
  476. 'data' => drupal_get_path('module', 'og_ui') . '/css/og_ui.css',
  477. ),
  478. );
  479. }
  480. }
  481. $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  482. $form['bundle'] = array('#type' => 'value', '#value' => $bundle);
  483. $form['gid'] = array('#type' => 'value', '#value' => $gid);
  484. $role_names = _og_ui_get_role_names($group_type, $bundle, $gid, $rid);
  485. $form['role_names'] = array('#type' => 'value', '#value' => $role_names);
  486. return $form;
  487. }
  488. /**
  489. * Validate handler; OG roles.
  490. */
  491. function og_ui_admin_roles_validate($form, &$form_state) {
  492. if (!empty($form_state['values']['name'])) {
  493. $group_type = $form['group_type']['#value'];
  494. $bundle = $form['bundle']['#value'];
  495. $gid = $form['gid']['#value'];
  496. $roles = og_roles($group_type, $bundle, $gid);
  497. $unique_name = TRUE;
  498. if (!empty($form_state['values']['rid'])) {
  499. $role = og_role_load($form_state['values']['rid']);
  500. $unique_name = $role->name != $form_state['values']['name'];
  501. }
  502. if ($unique_name && in_array($form_state['values']['name'], $roles)) {
  503. form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
  504. }
  505. }
  506. else {
  507. form_set_error('name', t('You must specify a valid role name.'));
  508. }
  509. }
  510. /**
  511. * Submit handler; OG roles.
  512. */
  513. function og_ui_admin_roles_submit($form, &$form_state) {
  514. $group_type = $form['group_type']['#value'];
  515. $bundle = $form['bundle']['#value'];
  516. $gid = $form['gid']['#value'];
  517. if (!empty($form_state['values']['rid'])) {
  518. $role = og_role_load($form_state['values']['rid']);
  519. $role->name = $form_state['values']['name'];
  520. }
  521. else {
  522. $role = og_role_create($form_state['values']['name'], $group_type, $gid, $bundle);
  523. }
  524. if ($form_state['values']['op'] == t('Save role')) {
  525. og_role_save($role);
  526. drupal_set_message(t('The role has been renamed.'));
  527. }
  528. elseif ($form_state['values']['op'] == t('Add role')) {
  529. og_role_save($role);
  530. drupal_set_message(t('The role has been added.'));
  531. }
  532. elseif ($form_state['values']['op'] == t('Delete role')) {
  533. og_role_delete($form_state['values']['rid']);
  534. drupal_set_message(t('The role has been deleted.'));
  535. }
  536. if ($gid) {
  537. $form_state['redirect'] = "group/$group_type/$gid/admin/roles";
  538. }
  539. else {
  540. $form_state['redirect'] = 'admin/config/group/roles';
  541. if ($bundle) {
  542. $form_state['redirect'] .= "/$group_type/$bundle";
  543. }
  544. }
  545. }
  546. /**
  547. * Theme the new role form.
  548. *
  549. * @ingroup themeable
  550. */
  551. function theme_og_ui_admin_new_role($variables) {
  552. $form = $variables['form'];
  553. $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
  554. $group_type = $form['group_type']['#value'];
  555. $gid = $form['gid']['#value'];
  556. $bundle = $form['bundle']['#value'];
  557. $role_names = $form['role_names']['#value'];
  558. if ($gid) {
  559. $group = entity_load_single($group_type, $gid);
  560. $default_access = $group && og_is_group_default_access($group_type, $group);
  561. }
  562. else {
  563. $default_access = FALSE;
  564. }
  565. foreach ($role_names as $rid => $name) {
  566. $text = !$default_access ? t('edit permissions') : t('view permissions');
  567. $path = $gid ? "group/$group_type/$gid/admin" : 'admin/config/group';
  568. $permissions = l($text, "$path/permission/$rid/edit");
  569. if (!$default_access && !in_array($name, array(OG_ANONYMOUS_ROLE, OG_AUTHENTICATED_ROLE))) {
  570. $rows[] = array(check_plain($name), l(t('edit role'), "$path/role/$rid/edit"), $permissions);
  571. }
  572. else {
  573. $rows[] = array(check_plain($name), t('locked'), $permissions);
  574. }
  575. }
  576. $rows[] = array(array('data' => drupal_render($form['name']) . drupal_render($form['add']), 'colspan' => 4, 'class' => 'edit-name'));
  577. hide($form['actions']);
  578. $output = drupal_render_children($form);
  579. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  580. $output .= render($form['actions']);
  581. return $output;
  582. }
  583. /**
  584. * Helper function to get role names.
  585. *
  586. * @param $group_type
  587. * Group entity type. E.g. 'node'.
  588. * @param $bundle
  589. * Group bundle.
  590. * @param $gid
  591. * Group item ID.
  592. * @param $rid
  593. * Role ID.
  594. *
  595. * @return array
  596. * Role names according to parameters.
  597. */
  598. function _og_ui_get_role_names($group_type, $bundle, $gid, $rid) {
  599. if ($gid) {
  600. $group = entity_load_single($group_type, $gid);
  601. list(,,$bundle) = entity_extract_ids($group_type, $group);
  602. $gid = og_is_group_default_access($group_type, $group) ? 0 : $gid;
  603. }
  604. $role_names = og_roles($group_type, $bundle, $gid);
  605. if ($rid && !empty($role_names[$rid])) {
  606. $role_names = array($rid => $role_names[$rid]);
  607. }
  608. return $role_names;
  609. }
  610. /**
  611. * Menu callback: administer permissions.
  612. *
  613. * @param $group_type
  614. * Group entity type. E.g. 'node'.
  615. * @param $gid
  616. * Group item ID.
  617. * @param $bundle
  618. * Group bundle.
  619. * @param $rid.
  620. * Role ID. When specified, edit only this role's permissions.
  621. *
  622. * @ingroup forms
  623. * @see og_ui_admin_permissions_submit()
  624. * @see theme_og_ui_admin_permissions()
  625. */
  626. function og_ui_admin_permissions($form, $form_state, $group_type = '', $gid = 0, $bundle = '', $rid = 0) {
  627. if ($rid) {
  628. // Get group type and bundle from role.
  629. $role = og_role_load($rid);
  630. $bundle = $role->group_bundle;
  631. $group_type = $role->group_type;
  632. }
  633. if ($gid) {
  634. og_set_breadcrumb($group_type, $gid, array(l(t('Group'), "$group_type/$gid/group")));
  635. }
  636. $form['group_type'] = array('#type' => 'value', '#value' => $group_type);
  637. $form['bundle'] = array('#type' => 'value', '#value' => $bundle);
  638. $form['gid'] = array('#type' => 'value', '#value' => $gid);
  639. $role_names = _og_ui_get_role_names($group_type, $bundle, $gid, $rid);
  640. // Fetch permissions for all roles or the one selected role.
  641. $role_permissions = og_role_permissions($role_names);
  642. // Store $role_names for use when saving the data.
  643. $form['role_names'] = array(
  644. '#type' => 'value',
  645. '#value' => $role_names,
  646. );
  647. // Render role/permission overview:
  648. $options = array();
  649. $module_info = system_get_info('module');
  650. // Get a list of all the modules implementing a hook_permission() and sort by
  651. // display name.
  652. $permissions_by_module = array();
  653. foreach (og_get_permissions() as $perm => $value) {
  654. $module = $value['module'];
  655. $permissions_by_module[$module][$perm] = $value;
  656. }
  657. asort($permissions_by_module);
  658. foreach ($permissions_by_module as $module => $permissions) {
  659. $form['permission'][] = array(
  660. '#markup' => $module_info[$module]['name'],
  661. '#id' => $module,
  662. );
  663. foreach ($permissions as $perm => $perm_item) {
  664. // Fill in default values for the permission.
  665. $perm_item += array(
  666. 'description' => '',
  667. 'restrict access' => FALSE,
  668. 'warning' => !empty($perm_item['restrict access']) ? t('Warning: Give to trusted roles only; this permission has security implications in the group context.') : '',
  669. );
  670. // If the user can manage permissions, but does not have administer
  671. // group permission, hide restricted permissions from them. This
  672. // prevents users from escalating their privileges.
  673. if ($gid && ($perm_item['restrict access'] && !og_user_access($group_type, $gid, 'administer group'))) {
  674. continue;
  675. }
  676. $options[$perm] = '';
  677. $form['permission'][$perm] = array(
  678. '#type' => 'item',
  679. '#markup' => $perm_item['title'],
  680. '#description' => theme('user_permission_description', array('permission_item' => $perm_item)),
  681. );
  682. foreach ($role_names as $rid => $name) {
  683. // Builds arrays for checked boxes for each role
  684. if (isset($role_permissions[$rid][$perm])) {
  685. $status[$rid][] = $perm;
  686. }
  687. }
  688. }
  689. }
  690. // Have to build checkboxes here after checkbox arrays are built
  691. foreach ($role_names as $rid => $name) {
  692. $form['checkboxes'][$rid] = array(
  693. '#type' => 'checkboxes',
  694. '#options' => $options,
  695. '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
  696. '#attributes' => array('class' => array('rid-' . $rid)),
  697. );
  698. $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
  699. }
  700. if (!$gid || !og_is_group_default_access($group_type, $gid)) {
  701. $form['actions'] = array('#type' => 'actions');
  702. $form['actions']['submit'] = array(
  703. '#type' => 'submit',
  704. '#value' => t('Save permissions'),
  705. '#submit' => array('og_ui_admin_permissions_submit'),
  706. );
  707. }
  708. $form['#after_build'][] = 'og_ui_admin_permissions_after_build';
  709. return $form;
  710. }
  711. /**
  712. * Helper function to disable the permissions checkboxes.
  713. *
  714. * Every permission can declare to which roles it applies, as some don't make
  715. * sense if applied to anonymous or authenticated user (e.g. subscribe to group
  716. * should appear only to anonymous members).
  717. * A second case for disabling is when using deafult roles and permissions, and
  718. * the user is browsing a group's permissions page.
  719. */
  720. function og_ui_admin_permissions_after_build($form) {
  721. $group_type = $form['group_type']['#value'];
  722. $gid = $form['gid']['#value'];
  723. $bundle = $form['bundle']['#value'];
  724. $role_names = $form['role_names']['#value'];
  725. // Indicate if all checkboxes should be disabled.
  726. $disable = $gid && og_is_group_default_access($group_type, $gid);
  727. // Authenticated roles.
  728. $roles = og_roles($group_type, $bundle, $gid);
  729. $anon_rid = array_search(OG_ANONYMOUS_ROLE, $roles);
  730. // Remove the anonymous member from the authenticated roles.
  731. unset($roles[$anon_rid]);
  732. $auth_rids = array_keys($roles);
  733. foreach (og_get_permissions() as $key => $perm) {
  734. $perm['roles'] = drupal_map_assoc($perm['roles']);
  735. if ($disable || (empty($perm['roles']) || empty($perm['roles'][OG_ANONYMOUS_ROLE]))) {
  736. if (in_array($anon_rid, array_keys($role_names))) {
  737. $form['checkboxes'][$anon_rid][$key]['#attributes']['disabled'] = TRUE;
  738. }
  739. }
  740. if ($disable || (empty($perm['roles']) || empty($perm['roles'][OG_AUTHENTICATED_ROLE]))) {
  741. foreach ($auth_rids as $auth_rid) {
  742. if (in_array($auth_rid, array_keys($role_names))) {
  743. $form['checkboxes'][$auth_rid][$key]['#attributes']['disabled'] = TRUE;
  744. }
  745. }
  746. }
  747. }
  748. return $form;
  749. }
  750. /**
  751. * Save permissions selected on the administer permissions page.
  752. *
  753. * @see og_user_admin_permissions()
  754. */
  755. function og_ui_admin_permissions_submit($form, &$form_state) {
  756. foreach ($form_state['values']['role_names'] as $rid => $name) {
  757. og_role_change_permissions($rid, $form_state['values'][$rid]);
  758. }
  759. drupal_set_message(t('The changes have been saved.'));
  760. }
  761. /**
  762. * Theme the administer permissions page.
  763. *
  764. * @ingroup themeable
  765. */
  766. function theme_og_ui_admin_permissions($variables) {
  767. $form = $variables['form'];
  768. $role_names = $form['role_names']['#value'];
  769. foreach (element_children($form['permission']) as $key) {
  770. $row = array();
  771. // Module name.
  772. if (is_numeric($key)) {
  773. $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
  774. }
  775. else {
  776. // Permission row.
  777. $row[] = array(
  778. 'data' => drupal_render($form['permission'][$key]),
  779. 'class' => array('permission'),
  780. );
  781. foreach (element_children($form['checkboxes']) as $rid) {
  782. $form['checkboxes'][$rid][$key]['#title'] = $role_names[$rid] . ': ' . $form['permission'][$key]['#markup'];
  783. $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
  784. $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
  785. }
  786. }
  787. $rows[] = $row;
  788. }
  789. $header[] = (t('Permission'));
  790. foreach (element_children($form['role_names']) as $rid) {
  791. $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
  792. }
  793. $output = '';
  794. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'permissions')));
  795. $output .= drupal_render_children($form);
  796. return $output;
  797. }
  798. /**
  799. * Allow site admin to add or remove group fields from fieldable entities.
  800. */
  801. function og_ui_field_settings($form, &$form_state) {
  802. $form = array();
  803. $options = array();
  804. $options = array();
  805. foreach (entity_get_info() as $entity_type => $entity_info) {
  806. if (empty($entity_info['fieldable'])) {
  807. continue;
  808. }
  809. foreach($entity_info['bundles'] as $bundle_name => $bundle) {
  810. // Prefix the bundle name with the entity type.
  811. $entity_name = check_plain("$entity_info[label] ($entity_type)");
  812. $options[$entity_name][$entity_type . ':' . $bundle_name] = filter_xss($bundle['label']);
  813. }
  814. }
  815. $form['bundle'] = array(
  816. '#title' => t('Bundles'),
  817. '#type' => 'select',
  818. '#options' => $options,
  819. );
  820. $options = array();
  821. foreach (og_fields_info() as $field_name => $field) {
  822. foreach ($field['type'] as $type) {
  823. $type_name = $type == 'group' ? t('Group') : t('Group content');
  824. $options[$type_name][$field_name] = filter_xss($field['instance']['label']);
  825. }
  826. }
  827. $selected_field_name = !empty($form_state['values']['field_type']) ? $form_state['values']['field_type'] : OG_AUDIENCE_FIELD;
  828. $selected_og_info = og_fields_info($selected_field_name);
  829. $form['field_info_wrapper'] = array(
  830. '#prefix' => '<div id="field-info-wrapper">',
  831. '#suffix' => '</div>',
  832. '#parents' => array('field_info_wrapper'),
  833. '#type' => 'fieldset',
  834. );
  835. $form['field_info_wrapper']['field_type'] = array(
  836. '#title' => t('Fields'),
  837. '#type' => 'select',
  838. '#options' => $options,
  839. '#required' => TRUE,
  840. '#default_value' => $selected_field_name,
  841. '#ajax' => array(
  842. 'callback' => 'og_ui_admin_fields_ajax_callback',
  843. 'wrapper' => 'field-info-wrapper',
  844. ),
  845. );
  846. $form['field_info_wrapper']['description'] = array(
  847. '#markup' => $selected_og_info['description'],
  848. );
  849. if (!empty($selected_og_info['multiple'])) {
  850. $form['field_info_wrapper']['field_name'] = array(
  851. '#type' => 'textfield',
  852. '#title' => t('Field name'),
  853. '#description' => t('This field type supports adding multiple instances on the same bundle (i.e. the field name is not hardcoded).'),
  854. '#required' => TRUE,
  855. '#maxlength' => 32,
  856. '#default_value' => $selected_field_name,
  857. );
  858. }
  859. else {
  860. // Pass the field name as a value.
  861. $form['field_name_wrapper']['field_name'] = array(
  862. '#type' => 'value',
  863. '#value' => $selected_field_name,
  864. );
  865. }
  866. $field_enabled = array();
  867. $og_fields = og_fields_info();
  868. $og_fields_name = array_keys($og_fields);
  869. $entity_info = entity_get_info();
  870. // Get the fields that exist in the bundle.
  871. foreach (field_info_fields() as $field_name => $field) {
  872. if (in_array($field_name, $og_fields_name) && !empty($field['bundles'])) {
  873. foreach ($field['bundles'] as $entity_type => $bundles) {
  874. foreach ($bundles as $bundle) {
  875. $field_enabled[$entity_type][$bundle][] = $field_name;
  876. }
  877. }
  878. }
  879. }
  880. if ($field_enabled) {
  881. $form['group_fields'] = array(
  882. '#type' => 'vertical_tabs',
  883. '#weight' => 99,
  884. );
  885. // Show all the group fields of each bundle.
  886. foreach ($field_enabled as $entity_type => $bundles) {
  887. foreach ($bundles as $bundle => $fields) {
  888. $options = array();
  889. $bundles = field_info_bundles($entity_type);
  890. $form['group_fields_' . $entity_type . '_' . $bundle] = array(
  891. '#type' => 'fieldset',
  892. '#title' => t('@bundle - @entity entity', array('@bundle' => $bundles[$bundle]['label'], '@entity' => $entity_info[$entity_type]['label'])),
  893. '#collapsible' => TRUE,
  894. '#group' => 'group_fields',
  895. );
  896. foreach ($fields as $field_name) {
  897. $options[] = array(
  898. check_plain($og_fields[$field_name]['instance']['label']),
  899. filter_xss($og_fields[$field_name]['description']),
  900. l(t('Delete'), "admin/config/group/fields/$entity_type/$bundle/$field_name/delete"),
  901. );
  902. }
  903. $header = array(t('Field'), t('Description'), t('Operations'));
  904. $form['group_fields_' . $entity_type . '_' . $bundle]['fields'] = array(
  905. '#markup' => theme('table', array('header' => $header, 'rows' => $options)),
  906. );
  907. }
  908. }
  909. }
  910. else {
  911. $form['group_fields'] = array(
  912. '#markup' => t('There are no Group fields attached to any bundle yet.'),
  913. );
  914. }
  915. $form['actions'] = array('#type' => 'actions');
  916. $form['actions']['submit'] = array(
  917. '#type' => 'submit',
  918. '#value' => t('Add field'),
  919. );
  920. return $form;
  921. }
  922. /**
  923. * AJAX callback to to return the field name if the field supports "mulitple"
  924. * instances, on the same bundle (e.g. OG_AUDIENCE_FIELD).
  925. */
  926. function og_ui_admin_fields_ajax_callback($form, &$form_state) {
  927. return $form['field_info_wrapper'];
  928. }
  929. /**
  930. * Validate handler; Check if field can be attached to bundle.
  931. */
  932. function og_ui_field_settings_validate($form, &$form_state) {
  933. list($entity_type, $bundle) = explode(':', $form_state['values']['bundle']);
  934. $field_name = $form_state['values']['field_name'];
  935. $og_field = og_fields_info($form_state['values']['field_type']);
  936. $bundles = field_info_bundles($entity_type);
  937. $entity_info = entity_get_info($entity_type);
  938. $params = array(
  939. '%field' => $og_field['instance']['label'],
  940. '%bundle' => $bundles[$bundle]['label'],
  941. '%entity' => $entity_info['label'],
  942. );
  943. if (field_info_instance($entity_type, $field_name, $bundle)) {
  944. form_set_error('bundles', t('Field %field already exists in %bundle.', $params));
  945. }
  946. // Check field can be attached to entity type.
  947. if (!empty($og_field['entity']) && !in_array($entity_type, $og_field['entity'])) {
  948. $items = array();
  949. foreach ($og_field['entity'] as $entity_type) {
  950. $info = entity_get_info($entity_type);
  951. $items[] = $info['label'];
  952. }
  953. form_set_error('bundles', t('Field %field can only be attached to %entities entity bundles.', $params + array('%entities' => implode(', ', $items))));
  954. }
  955. }
  956. /**
  957. * Submit handler; Attach field can to bundle.
  958. */
  959. function og_ui_field_settings_submit($form, &$form_state) {
  960. list($entity_type, $bundle) = explode(':', $form_state['values']['bundle']);
  961. $field_name = $form_state['values']['field_name'];
  962. $field_type = $form_state['values']['field_type'];
  963. $og_field = og_fields_info($field_type);
  964. og_create_field($field_name, $entity_type, $bundle, $og_field);
  965. $params = array(
  966. '@field-type' => $og_field['instance']['label'],
  967. '@field-name' => $field_name,
  968. '@bundle' => $bundle,
  969. );
  970. if ($field_name == $field_type) {
  971. drupal_set_message(t('Added field @field-type to @bundle.', $params));
  972. }
  973. else {
  974. drupal_set_message(t('Added field @field-type (@field-name) to @bundle.', $params));
  975. }
  976. }
  977. /**
  978. * Menu callback; present a form for removing a field instance from a bundle.
  979. */
  980. function og_ui_field_delete_form($form, &$form_state, $instance) {
  981. module_load_include('inc', 'field_ui', 'field_ui.admin');
  982. $output = field_ui_field_delete_form($form, $form_state, $instance);
  983. $output['actions']['cancel'] = array(
  984. '#markup' => l(t('Cancel'), 'admin/config/group/fields'),
  985. );
  986. return $output;
  987. }
  988. function og_ui_field_delete_form_submit($form, &$form_state) {
  989. module_load_include('inc', 'field_ui', 'field_ui.admin');
  990. field_ui_field_delete_form_submit($form, $form_state);
  991. $form_state['redirect'] = 'admin/config/group/fields';
  992. }