og.rules.inc

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

Rules integration for the Organic groups module.

Functions

Namesort descending Description
og_rules_action_info Implements hook_rules_action_info().
og_rules_add_entity_to_group Action: Add entity to group.
og_rules_condition_entity_in_group Condition: Entity is in group.
og_rules_condition_info Implements hook_rules_condition_info().
og_rules_condition_user_has_role Condition: User has group role.
og_rules_condition_user_in_group Condition: User is in group.
og_rules_condition_user_in_group_help Condition "User is in group" help.
og_rules_entity_is_group Condition: Entity is group.
og_rules_entity_is_group_content Condition: Entity is group content.
og_rules_entity_is_group_content_help Condition: "Entity is group content" help.
og_rules_entity_is_group_help Condition: "Entity is group" help.
og_rules_event_info Implementation of hook_rules_event_info().
og_rules_get_group_content Action: Fetch group content of a certain entity type.
og_rules_get_group_content_info_alter "Fetch group content" action info alter callback.
og_rules_get_managers Action: Get group managers from a group content.
og_rules_get_members Action: Get group members from a group content.
og_rules_grant_og_role Action: Grant OG role.
og_rules_group_roles_options_list Options list callback for group roles.
og_rules_integration_access OG Rules integration access callback.
og_rules_remove_entity_from_group Action: Remove entity from group.
og_rules_revoke_og_role Action: Revoke OG role.
og_rules_user_has_permission Condition: User has group permisison.
og_rules_user_has_permission_options_list Condition user has group permission options list callback for permissions.

File

sites/all/modules/ulmus/og/og.rules.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for the Organic groups module.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * Implementation of hook_rules_event_info().
  11. *
  12. * Adds some additional event that make reacting on user subscriptions easier.
  13. */
  14. function og_rules_event_info() {
  15. $defaults = array(
  16. 'variables' => array(
  17. 'og_membership' => array(
  18. 'type' => 'og_membership',
  19. 'label' => t("The user's OG membership"),
  20. ),
  21. 'account' => array(
  22. 'type' => 'user',
  23. 'label' => t('User'),
  24. ),
  25. ),
  26. 'group' => t('OG membership'),
  27. );
  28. return array(
  29. 'og_user_insert' => $defaults + array(
  30. 'label' => t('User has become a group member'),
  31. 'help' => t("A user has become a group member, but might have been not yet approved."),
  32. ),
  33. 'og_user_approved' => $defaults + array(
  34. 'label' => t('User membership has been approved'),
  35. ),
  36. 'og_user_blocked' => $defaults + array(
  37. 'label' => t('User membership has been blocked'),
  38. ),
  39. 'og_user_delete' => $defaults + array(
  40. 'label' => t('User has been removed from group'),
  41. 'help' => t("A user has been removed from group and is no longer a group member."),
  42. ),
  43. 'og_role_grant' => array_merge_recursive($defaults, array(
  44. 'label' => t('OG role granted to user'),
  45. 'help' => t("An OG role has been granted to this user."),
  46. 'variables' => array(
  47. 'og_rid' => array(
  48. 'type' => 'integer',
  49. 'label' => t('OG role ID'),
  50. 'description' => t('The id of the OG user role.'),
  51. ),
  52. ),
  53. )),
  54. 'og_role_revoke' => array_merge_recursive($defaults, array(
  55. 'label' => t('OG role revoked from user'),
  56. 'help' => t("An OG role has been revoked from this user."),
  57. 'variables' => array(
  58. 'og_rid' => array(
  59. 'type' => 'integer',
  60. 'label' => t('OG role ID'),
  61. 'description' => t('The id of the OG user role.'),
  62. ),
  63. ),
  64. )),
  65. );
  66. }
  67. /**
  68. * Implements hook_rules_action_info().
  69. */
  70. function og_rules_action_info() {
  71. $items = array();
  72. $items['og_get_members'] = array(
  73. 'label' => t('Get group members from group audience'),
  74. 'group' => t('Organic groups'),
  75. 'parameter' => array(
  76. 'group_content' => array(
  77. 'type' => 'entity',
  78. 'label' => t('Group content'),
  79. 'description' => t('The group content determining the group audience.'),
  80. ),
  81. // @todo: Add option to filter by member-state?
  82. ),
  83. 'provides' => array(
  84. 'group_members' => array('type' => 'list<user>', 'label' => t('List of group members')),
  85. ),
  86. 'base' => 'og_rules_get_members',
  87. 'access callback' => 'og_rules_integration_access',
  88. );
  89. $items['og_get_managers'] = array(
  90. 'label' => t('Get group managers from group audience'),
  91. 'group' => t('Organic groups'),
  92. 'parameter' => array(
  93. 'group_content' => array(
  94. 'type' => 'entity',
  95. 'label' => t('Group content'),
  96. 'description' => t('The group content determining the group audience.'),
  97. ),
  98. ),
  99. 'provides' => array(
  100. 'group_managers' => array('type' => 'list<user>', 'label' => t('List of group managers')),
  101. ),
  102. 'base' => 'og_rules_get_managers',
  103. 'access callback' => 'og_rules_integration_access',
  104. );
  105. $items['og_get_group_content'] = array(
  106. 'label' => t('Get group content from a group'),
  107. 'group' => t('Organic groups'),
  108. 'parameter' => array(
  109. 'group' => array(
  110. 'type' => array_keys(og_get_all_group_entity()),
  111. 'label' => t('Group'),
  112. 'description' => t('The group for which to fetch content.'),
  113. 'wrapped' => TRUE,
  114. ),
  115. 'entity_type' => array(
  116. 'type' => 'token',
  117. 'label' => t('Entity type'),
  118. 'description' => t('The entity type of the content which is to be fetched.'),
  119. 'options list' => 'og_get_fieldable_entity_list',
  120. ),
  121. ),
  122. 'provides' => array(
  123. 'group_content' => array('type' => 'list<entity>', 'label' => t('Group content')),
  124. ),
  125. 'base' => 'og_rules_get_group_content',
  126. 'access callback' => 'og_rules_integration_access',
  127. );
  128. $items['og_group_content_add'] = array(
  129. 'label' => t('Add entity to group'),
  130. 'group' => t('Organic groups'),
  131. 'parameter' => array(
  132. 'entity' => array(
  133. // Do not use type 'entity' but restrict the type to group content type.
  134. 'type' => array_keys(og_get_all_group_content_entity()),
  135. 'label' => t('Entity'),
  136. 'description' => t('The group content which is to be added to a group.'),
  137. 'wrapped' => TRUE,
  138. ),
  139. 'group' => array(
  140. 'type' => array_keys(og_get_all_group_entity()),
  141. 'label' => t('Group'),
  142. 'wrapped' => TRUE,
  143. ),
  144. ),
  145. 'base' => 'og_rules_add_entity_to_group',
  146. 'access callback' => 'og_rules_integration_access',
  147. );
  148. $items['og_group_content_remove'] = array(
  149. 'label' => t('Remove entity from group'),
  150. 'group' => t('Organic groups'),
  151. 'parameter' => array(
  152. 'entity' => array(
  153. // Do not use type 'entity' but restrict the type to group content type.
  154. 'type' => array_keys(og_get_all_group_content_entity()),
  155. 'label' => t('Entity'),
  156. 'description' => t('The entity which is to be removed from a group.'),
  157. 'wrapped' => TRUE,
  158. ),
  159. 'group' => array(
  160. 'type' => array_keys(og_get_all_group_entity()),
  161. 'label' => t('Group'),
  162. 'wrapped' => TRUE,
  163. ),
  164. ),
  165. 'base' => 'og_rules_remove_entity_from_group',
  166. 'access callback' => 'og_rules_integration_access',
  167. );
  168. // For UX also provide separate actions for user subcriptions although it is
  169. // technically the same as adding entities to groups.
  170. $items['og_subcribe_user'] = array(
  171. 'label' => t('Subscribe user to group'),
  172. 'group' => t('Organic groups'),
  173. 'parameter' => array(
  174. 'user' => array(
  175. 'type' => 'user',
  176. 'label' => t('User'),
  177. 'description' => t('The user who will be subscribed.'),
  178. 'wrapped' => TRUE,
  179. ),
  180. 'group' => array(
  181. 'type' => array_keys(og_get_all_group_entity()),
  182. 'label' => t('Group'),
  183. 'wrapped' => TRUE,
  184. ),
  185. // @todo: Add membership-type setting + add in the membership-entity
  186. // fields via the info_alter callback + reload the form once the
  187. // membership-type has been chosen.
  188. // Then, we probably also want to provide the newly created membership
  189. // entity.
  190. ),
  191. 'base' => 'og_rules_add_entity_to_group',
  192. 'access callback' => 'og_rules_integration_access',
  193. );
  194. $items['og_unsubscribe_user'] = array(
  195. 'label' => t('Unsubscribe user from group'),
  196. 'group' => t('Organic groups'),
  197. 'parameter' => array(
  198. 'user' => array(
  199. 'type' => 'user',
  200. 'label' => t('User'),
  201. 'description' => t('The user who will be unsubscribed.'),
  202. 'wrapped' => TRUE,
  203. ),
  204. 'group' => array(
  205. 'type' => array_keys(og_get_all_group_entity()),
  206. 'label' => t('Group'),
  207. 'wrapped' => TRUE,
  208. ),
  209. ),
  210. 'base' => 'og_rules_remove_entity_from_group',
  211. 'access callback' => 'og_rules_integration_access',
  212. );
  213. $items['og_grant_og_role'] = array(
  214. 'label' => t('Grant OG role'),
  215. 'group' => t('Organic groups'),
  216. 'parameter' => array(
  217. 'account' => array(
  218. 'type' => 'user',
  219. 'label' => t('User'),
  220. 'description' => t('The user who will be granted this role.'),
  221. 'wrapped' => TRUE,
  222. ),
  223. 'group' => array(
  224. 'type' => array_keys(og_get_all_group_entity()),
  225. 'label' => t('Group'),
  226. 'wrapped' => TRUE,
  227. ),
  228. 'roles' => array(
  229. 'type' => 'list<text>',
  230. 'label' => t('Group roles'),
  231. 'options list' => 'og_rules_group_roles_options_list',
  232. 'restriction' => 'input',
  233. ),
  234. ),
  235. 'base' => 'og_rules_grant_og_role',
  236. 'access callback' => 'og_rules_integration_access',
  237. );
  238. $items['og_revoke_og_role'] = array(
  239. 'label' => t('Revoke OG role'),
  240. 'group' => t('Organic groups'),
  241. 'parameter' => array(
  242. 'account' => array(
  243. 'type' => 'user',
  244. 'label' => t('User'),
  245. 'description' => t('The user who will have the role revoked.'),
  246. 'wrapped' => TRUE,
  247. ),
  248. 'group' => array(
  249. 'type' => array_keys(og_get_all_group_entity()),
  250. 'label' => t('Group'),
  251. 'wrapped' => TRUE,
  252. ),
  253. 'roles' => array(
  254. 'type' => 'list<integer>',
  255. 'label' => t('Group roles'),
  256. 'options list' => 'og_rules_group_roles_options_list',
  257. 'restriction' => 'input',
  258. ),
  259. ),
  260. 'base' => 'og_rules_revoke_og_role',
  261. 'access callback' => 'og_rules_integration_access',
  262. );
  263. return $items;
  264. }
  265. /**
  266. * Action: Get group members from a group content.
  267. */
  268. function og_rules_get_members($group_content) {
  269. if (!isset($group_content->og_membership)) {
  270. // Not a group content.
  271. return;
  272. }
  273. $members = array();
  274. foreach ($group_content->og_membership->value() as $og_membership) {
  275. // Get the group members the group content belongs to.
  276. $current_members = db_select('og_membership', 'om')
  277. ->fields('om', array('etid'))
  278. ->condition('om.gid', $og_membership->gid)
  279. ->condition('om.group_type', $og_membership->group_type)
  280. ->condition('om.entity_type', 'user')
  281. ->execute()
  282. ->fetchCol();
  283. $members = array_merge($members, $current_members);
  284. }
  285. // Remove duplicate items.
  286. $members = array_keys(array_flip($members));
  287. return array('group_members' => $members);
  288. }
  289. /**
  290. * Action: Get group managers from a group content.
  291. */
  292. function og_rules_get_managers($group_content) {
  293. if (!isset($group_content->og_membership)) {
  294. // Not a group content.
  295. return;
  296. }
  297. $return = array();
  298. foreach ($group_content->og_membership->value() as $og_membership) {
  299. $group = entity_load_single($og_membership->group_type, $og_membership->gid);
  300. if(!empty($group->uid)){
  301. $return[] = $group->uid;
  302. }
  303. }
  304. // Remove duplicate items.
  305. $return = array_keys(array_flip($return));
  306. return array('group_managers' => $return);
  307. }
  308. /**
  309. * Action: Fetch group content of a certain entity type.
  310. */
  311. function og_rules_get_group_content(EntityDrupalWrapper $group, $entity_type) {
  312. $memberships = og_membership_load_multiple(FALSE, array('gid' => $group->getIdentifier(), 'entity_type' => $entity_type));
  313. $entities = array();
  314. foreach ($memberships as $membership) {
  315. $entities[] = $membership->etid;
  316. }
  317. $entities = entity_load($entity_type, $entities);
  318. return array('group_content' => $entities);
  319. }
  320. /**
  321. * "Fetch group content" action info alter callback.
  322. */
  323. function og_rules_get_group_content_info_alter(&$element_info, $element) {
  324. if (isset($element->settings['entity_type']) && $entity_type = $element->settings['entity_type']) {
  325. $element_info['provides']['group_content']['type'] = "list<$entity_type>";
  326. }
  327. }
  328. /**
  329. * Action: Add entity to group.
  330. */
  331. function og_rules_add_entity_to_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group) {
  332. // TODO: Add field-name.
  333. $values = array(
  334. 'entity_type' => $entity->type(),
  335. 'entity' => $entity->value(),
  336. );
  337. og_group($group->type(), $group->getIdentifier(), $values);
  338. }
  339. /**
  340. * Action: Remove entity from group.
  341. */
  342. function og_rules_remove_entity_from_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group) {
  343. og_ungroup($group->type(), $group->getIdentifier(), $entity->type(), $entity->value());
  344. }
  345. /**
  346. * Action: Grant OG role.
  347. */
  348. function og_rules_grant_og_role(EntityDrupalWrapper $account, EntityDrupalWrapper $group, $roles) {
  349. $group_roles = og_roles($group->type(), NULL, $group->getIdentifier());
  350. foreach ($roles as $role) {
  351. $rid = array_search($role, $group_roles);
  352. if ($rid !== FALSE) {
  353. og_role_grant($group->type(), $group->getIdentifier(), $account->getIdentifier(), $rid);
  354. }
  355. }
  356. }
  357. /**
  358. * Action: Revoke OG role.
  359. */
  360. function og_rules_revoke_og_role(EntityDrupalWrapper $account, EntityDrupalWrapper $group, $roles) {
  361. $group_roles = og_roles($group->type(), NULL, $group->getIdentifier());
  362. foreach ($roles as $role) {
  363. $rid = array_search($role, $group_roles);
  364. if ($rid !== FALSE) {
  365. og_role_revoke($group->type(), $group->getIdentifier(), $account->getIdentifier(), $rid);
  366. }
  367. }
  368. }
  369. /**
  370. * Options list callback for group roles.
  371. */
  372. function og_rules_group_roles_options_list($element) {
  373. $og_roles = array();
  374. $unique_roles = array_unique(og_get_user_roles_name());
  375. foreach ($unique_roles as $role) {
  376. if (!in_array($role, array(OG_ANONYMOUS_ROLE, OG_AUTHENTICATED_ROLE))) {
  377. $og_roles[$role] = $role;
  378. }
  379. }
  380. return $og_roles;
  381. }
  382. /**
  383. * OG Rules integration access callback.
  384. */
  385. function og_rules_integration_access($type, $name) {
  386. // Grant everyone access to conditions.
  387. return $type == 'condition' || user_access('administer group');
  388. }
  389. /**
  390. * Implements hook_rules_condition_info().
  391. */
  392. function og_rules_condition_info() {
  393. $items = array();
  394. $items['og_user_has_permission'] = array(
  395. 'label' => t('User has group permission'),
  396. 'group' => t('Organic groups'),
  397. 'parameter' => array(
  398. 'permission' => array(
  399. 'type' => 'text',
  400. 'label' => t('Permission'),
  401. 'description' => t('The permission to check for.'),
  402. 'options list' => 'og_rules_user_has_permission_options_list',
  403. 'restriction' => 'input',
  404. ),
  405. 'group' => array(
  406. 'type' => array_keys(og_get_all_group_entity()),
  407. 'label' => t('Group'),
  408. 'description' => t('The group for which permission should be checked.'),
  409. 'wrapped' => TRUE,
  410. ),
  411. 'account' => array(
  412. 'type' => 'user',
  413. 'label' => t('User'),
  414. ),
  415. ),
  416. 'base' => 'og_rules_user_has_permission',
  417. 'access callback' => 'og_rules_integration_access',
  418. );
  419. $items['og_user_in_group'] = array(
  420. 'label' => t('User is group member'),
  421. 'group' => t('Organic groups'),
  422. 'parameter' => array(
  423. 'account' => array(
  424. 'type' => 'user',
  425. 'label' => t('User'),
  426. 'wrapped' => TRUE,
  427. ),
  428. 'group' => array(
  429. 'type' => array_keys(og_get_all_group_entity()),
  430. 'label' => t('Group'),
  431. 'wrapped' => TRUE,
  432. ),
  433. 'states' => array(
  434. 'type' => 'list<integer>',
  435. 'label' => t('Membership states'),
  436. 'restriction' => 'input',
  437. 'options list' => 'og_group_content_states',
  438. 'optional' => TRUE,
  439. 'default value' => array(OG_STATE_ACTIVE),
  440. ),
  441. ),
  442. 'base' => 'og_rules_condition_user_in_group',
  443. 'access callback' => 'og_rules_integration_access',
  444. );
  445. $items['og_entity_in_group'] = array(
  446. 'label' => t('Entity is in group'),
  447. 'group' => t('Organic groups'),
  448. 'parameter' => array(
  449. 'entity' => array(
  450. // Do not use type 'entity' but restrict the type to group content type.
  451. 'type' => array_keys(og_get_all_group_content_entity()),
  452. 'label' => t('Group content entity'),
  453. 'description' => t('The group content which will be checked.'),
  454. 'wrapped' => TRUE,
  455. ),
  456. 'group' => array(
  457. 'type' => array_keys(og_get_all_group_entity()),
  458. 'label' => t('Group'),
  459. 'wrapped' => TRUE,
  460. ),
  461. ),
  462. 'base' => 'og_rules_condition_entity_in_group',
  463. 'access callback' => 'og_rules_integration_access',
  464. );
  465. $items['og_entity_is_group'] = array(
  466. 'label' => t('Entity is group'),
  467. 'group' => t('Organic groups'),
  468. 'parameter' => array(
  469. 'entity' => array(
  470. // Do not use type 'entity' but restrict the type to group type.
  471. 'type' => array_keys(og_get_all_group_entity()),
  472. 'label' => t('Entity'),
  473. 'wrapped' => TRUE,
  474. ),
  475. ),
  476. 'base' => 'og_rules_entity_is_group',
  477. 'access callback' => 'og_rules_integration_access',
  478. );
  479. $items['og_entity_is_group_content'] = array(
  480. 'label' => t('Entity is group content'),
  481. 'group' => t('Organic groups'),
  482. 'parameter' => array(
  483. 'entity' => array(
  484. // Do not use type 'entity' but restrict the type to group content type.
  485. 'type' => array_keys(og_get_all_group_content_entity()),
  486. 'label' => t('Entity'),
  487. 'wrapped' => TRUE,
  488. ),
  489. ),
  490. 'base' => 'og_rules_entity_is_group_content',
  491. 'access callback' => 'og_rules_integration_access',
  492. );
  493. $items['og_user_has_role'] = array(
  494. 'label' => t('User has group role'),
  495. 'group' => t('Organic groups'),
  496. 'parameter' => array(
  497. 'account' => array(
  498. 'type' => 'user',
  499. 'label' => t('User'),
  500. 'wrapped' => TRUE,
  501. ),
  502. 'group' => array(
  503. 'type' => array_keys(og_get_all_group_entity()),
  504. 'label' => t('Group'),
  505. 'wrapped' => TRUE,
  506. ),
  507. 'roles' => array(
  508. 'type' => 'list<text>',
  509. 'label' => t('Group roles'),
  510. 'options list' => 'og_rules_group_roles_options_list',
  511. 'restriction' => 'input',
  512. ),
  513. ),
  514. 'base' => 'og_rules_condition_user_has_role',
  515. 'access callback' => 'og_rules_integration_access',
  516. );
  517. return $items;
  518. }
  519. /**
  520. * Condition: User has group permisison.
  521. */
  522. function og_rules_user_has_permission($permission, EntityDrupalWrapper $group, $account) {
  523. return og_user_access($group->type(), $group->getIdentifier(), $permission, $account);
  524. }
  525. /**
  526. * Condition user has group permission options list callback for permissions.
  527. */
  528. function og_rules_user_has_permission_options_list() {
  529. $perms = array();
  530. foreach (og_get_permissions() as $perm => $value) {
  531. // By keeping them keyed by module we can use optgroups with the
  532. // 'select' type.
  533. $perms[$value['module']][$perm] = strip_tags($value['title']);
  534. }
  535. return $perms;
  536. }
  537. /**
  538. * Condition: Entity is in group.
  539. */
  540. function og_rules_condition_entity_in_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group) {
  541. return og_is_member($group->type(), $group->getIdentifier(), $entity->type(), $entity->value());
  542. }
  543. /**
  544. * Condition: User is in group.
  545. */
  546. function og_rules_condition_user_in_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group, $states = array(OG_STATE_ACTIVE)) {
  547. return og_is_member($group->type(), $group->getIdentifier(), $entity->type(), $entity->value(), $states);
  548. }
  549. /**
  550. * Condition "User is in group" help.
  551. */
  552. function og_rules_condition_user_in_group_help() {
  553. return t('Evaluates to TRUE if the user is a member of the group and the membership meets the configured valid membership state.');
  554. }
  555. /**
  556. * Condition: Entity is group.
  557. */
  558. function og_rules_entity_is_group(EntityDrupalWrapper $entity) {
  559. return (bool) og_is_group($entity->type(), $entity->getIdentifier());
  560. }
  561. /**
  562. * Condition: "Entity is group" help.
  563. */
  564. function og_rules_entity_is_group_help() {
  565. return t('Determines whether the entity is an active group.');
  566. }
  567. /**
  568. * Condition: Entity is group content.
  569. */
  570. function og_rules_entity_is_group_content(EntityDrupalWrapper $entity) {
  571. return (bool) og_is_group_content_type($entity->type(), $entity->getBundle());
  572. }
  573. /**
  574. * Condition: "Entity is group content" help.
  575. */
  576. function og_rules_entity_is_group_content_help() {
  577. return t('Determines whether the entity is group content, i.e. it can be or is used as group content.');
  578. }
  579. /**
  580. * Condition: User has group role.
  581. */
  582. function og_rules_condition_user_has_role(EntityDrupalWrapper $account, EntityDrupalWrapper $group, $roles) {
  583. $user_roles = og_get_user_roles($group->type(), $group->getIdentifier(), $account->getIdentifier());
  584. return !array_diff($roles, $user_roles);
  585. }
  586. /**
  587. * @}
  588. */