og.rules.inc 15.5 KB
Newer Older
1
<?php
2

3
/**
4 5
 * @file
 * Rules integration for the Organic groups module.
6 7 8 9 10
 *
 * @addtogroup rules
 * @{
 */

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/**
 * Implementation of hook_rules_event_info().
 *
 * Adds some additional event that make reacting on user subscriptions easier.
 */
function og_rules_event_info() {
  $defaults = array(
    'variables' => array(
      'og_membership' => array(
        'type' => 'og_membership',
        'label' => t("The user's OG membership"),
      ),
      'account' => array(
        'type' => 'user',
        'label' => t('User'),
      ),
    ),
    'group' => t('OG membership'),
  );
  return array(
    'og_user_insert' => $defaults + array(
      'label' => t('User has become a group member'),
      'help' => t("A user has become a group member, but might have been not yet approved."),
    ),
    'og_user_approved' => $defaults + array(
      'label' => t('User membership has been approved'),
    ),
    'og_user_blocked' => $defaults + array(
      'label' => t('User membership has been blocked'),
    ),
    'og_user_delete' => $defaults + array(
      'label' => t('User has been removed from group'),
      'help' => t("A user has been removed from group and is no longer a group member."),
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    ),
    'og_role_grant' => array_merge_recursive($defaults, array(
      'label' => t('OG role granted to user'),
      'help' => t("An OG role has been granted to this user."),
      'variables' => array(
        'og_rid' => array(
          'type' => 'integer',
          'label' => t('OG role ID'),
          'description' => t('The id of the OG user role.'),
        ),
      ),
    )),
    'og_role_revoke' => array_merge_recursive($defaults, array(
      'label' => t('OG role revoked from user'),
      'help' => t("An OG role has been revoked from this user."),
      'variables' => array(
        'og_rid' => array(
          'type' => 'integer',
          'label' => t('OG role ID'),
          'description' => t('The id of the OG user role.'),
        ),
      ),
    )),
  );
68 69
}

70
/**
71
 * Implements hook_rules_action_info().
72 73 74 75 76 77 78
 */
function og_rules_action_info() {
  $items = array();
  $items['og_get_members'] = array(
    'label' => t('Get group members from group audience'),
    'group' => t('Organic groups'),
    'parameter' => array(
79 80 81 82
      'group_content' => array(
        'type' => 'entity',
        'label' => t('Group content'),
        'description' => t('The group content determining the group audience.'),
83
      ),
84
      // @todo: Add option to filter by member-state?
85 86 87 88 89
    ),
    'provides' => array(
      'group_members' => array('type' => 'list<user>', 'label' => t('List of group members')),
    ),
    'base' => 'og_rules_get_members',
90
    'access callback' => 'og_rules_integration_access',
91
  );
92 93

  $items['og_get_managers'] = array(
94
    'label' => t('Get group managers from group audience'),
95 96
    'group' => t('Organic groups'),
    'parameter' => array(
97 98 99 100
      'group_content' => array(
        'type' => 'entity',
        'label' => t('Group content'),
        'description' => t('The group content determining the group audience.'),
101 102 103 104 105 106
      ),
    ),
    'provides' => array(
      'group_managers' => array('type' => 'list<user>', 'label' => t('List of group managers')),
    ),
    'base' => 'og_rules_get_managers',
107
    'access callback' => 'og_rules_integration_access',
108 109
  );

110 111
  $items['og_get_group_content'] = array(
    'label' => t('Get group content from a group'),
112 113 114
    'group' => t('Organic groups'),
    'parameter' => array(
      'group' => array(
115
        'type' => array_keys(og_get_all_group_entity()),
116
        'label' => t('Group'),
117
        'description' => t('The group for which to fetch content.'),
118
        'wrapped' => TRUE,
119 120 121 122 123 124
      ),
      'entity_type' => array(
        'type' => 'token',
        'label' => t('Entity type'),
        'description' => t('The entity type of the content which is to be fetched.'),
        'options list' => 'og_get_fieldable_entity_list',
125 126 127
      ),
    ),
    'provides' => array(
128 129 130 131 132 133 134
      'group_content' => array('type' => 'list<entity>', 'label' => t('Group content')),
    ),
    'base' => 'og_rules_get_group_content',
    'access callback' => 'og_rules_integration_access',
  );

  $items['og_group_content_add'] = array(
135
    'label' => t('Add entity to group'),
136 137 138 139 140
    'group' => t('Organic groups'),
    'parameter' => array(
      'entity' => array(
        // Do not use type 'entity' but restrict the type to group content type.
        'type' => array_keys(og_get_all_group_content_entity()),
141
        'label' => t('Entity'),
142 143 144 145
        'description' => t('The group content which is to be added to a group.'),
        'wrapped' => TRUE,
      ),
      'group' => array(
146
        'type' => array_keys(og_get_all_group_entity()),
147
        'label' => t('Group'),
148
        'wrapped' => TRUE,
149
      ),
150
    ),
151 152 153 154 155
    'base' => 'og_rules_add_entity_to_group',
    'access callback' => 'og_rules_integration_access',
  );

  $items['og_group_content_remove'] = array(
156
    'label' => t('Remove entity from group'),
157 158 159 160 161
    'group' => t('Organic groups'),
    'parameter' => array(
      'entity' => array(
        // Do not use type 'entity' but restrict the type to group content type.
        'type' => array_keys(og_get_all_group_content_entity()),
162
        'label' => t('Entity'),
163 164 165 166
        'description' => t('The entity which is to be removed from a group.'),
        'wrapped' => TRUE,
      ),
      'group' => array(
167
        'type' => array_keys(og_get_all_group_entity()),
168
        'label' => t('Group'),
169
        'wrapped' => TRUE,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      ),
    ),
    'base' => 'og_rules_remove_entity_from_group',
    'access callback' => 'og_rules_integration_access',
  );

  // For UX also provide separate actions for user subcriptions although it is
  // technically the same as adding entities to groups.

  $items['og_subcribe_user'] = array(
    'label' => t('Subscribe user to group'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'user' => array(
        'type' => 'user',
        'label' => t('User'),
        'description' => t('The user who will be subscribed.'),
        'wrapped' => TRUE,
      ),
      'group' => array(
190
        'type' => array_keys(og_get_all_group_entity()),
191
        'label' => t('Group'),
192
        'wrapped' => TRUE,
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      ),
      // @todo: Add membership-type setting + add in the membership-entity
      // fields via the info_alter callback + reload the form once the
      // membership-type has been chosen.
      // Then, we probably also want to provide the newly created membership
      // entity.
    ),
    'base' => 'og_rules_add_entity_to_group',
    'access callback' => 'og_rules_integration_access',
  );

  $items['og_unsubscribe_user'] = array(
    'label' => t('Unsubscribe user from group'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'user' => array(
        'type' => 'user',
        'label' => t('User'),
        'description' => t('The user who will be unsubscribed.'),
        'wrapped' => TRUE,
      ),
      'group' => array(
215
        'type' => array_keys(og_get_all_group_entity()),
216
        'label' => t('Group'),
217
        'wrapped' => TRUE,
218 219 220 221
      ),
    ),
    'base' => 'og_rules_remove_entity_from_group',
    'access callback' => 'og_rules_integration_access',
222 223
  );

224 225 226 227
  return $items;
}

/**
228
 * Action: Get group members from a group content.
229
 */
230
function og_rules_get_members($group_content) {
231 232
  if (!isset($group_content->og_membership)) {
    // Not a group content.
233
    return;
234 235
  }

236
  $members = array();
237
  foreach ($group_content->og_membership->value() as $og_membership) {
238 239 240 241 242 243 244 245
    // Get the group members the group content belongs to.
    $current_members = db_select('og_membership', 'om')
      ->fields('om', array('etid'))
      ->condition('om.gid', $og_membership->gid)
      ->condition('om.group_type', $og_membership->group_type)
      ->condition('om.entity_type', 'user')
      ->execute()
      ->fetchCol();
246

247
    $members = array_merge($members, $current_members);
248
  }
249 250
  // Remove duplicate items.
  $members = array_keys(array_flip($members));
251
  return array('group_members' => $members);
252 253
}

254
/**
255
 * Action: Get group managers from a group content.
256
 */
257
function og_rules_get_managers($group_content) {
258 259
  if (!isset($group_content->og_membership)) {
    // Not a group content.
260 261
    return;
  }
262

263
  $return = array();
264
  foreach ($group_content->og_membership->value() as $og_membership) {
265 266 267
    $group = entity_load_single($og_membership->group_type, $og_membership->gid);
    if(!empty($group->uid)){
      $return[] = $group->uid;
268 269 270
    }
  }

271 272
  // Remove duplicate items.
  $return = array_keys(array_flip($return));
273 274 275
  return array('group_managers' => $return);
}

276 277 278
/**
 * Action: Fetch group content of a certain entity type.
 */
279 280
function og_rules_get_group_content(EntityDrupalWrapper $group, $entity_type) {
  $memberships = og_membership_load_multiple(FALSE, array('gid' => $group->getIdentifier(), 'entity_type' => $entity_type));
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  $entities = array();
  foreach ($memberships as $membership) {
    $entities[] = $membership->etid;
  }
  $entities = entity_load($entity_type, $entities);
  return array('group_content' => $entities);
}

/**
 * "Fetch group content" action info alter callback.
 */
function og_rules_get_group_content_info_alter(&$element_info, $element) {
  if (isset($element->settings['entity_type']) && $entity_type = $element->settings['entity_type']) {
    $element_info['provides']['group_content']['type']  = "list<$entity_type>";
  }
}

/**
 * Action: Add entity to group.
 */
301 302 303 304
function og_rules_add_entity_to_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group) {
  // TODO: Add field-name.
  $values = array(
    'entity_type' => $entity->type(),
305
    'entity' => $entity->value(),
306 307
  );
  og_group($group->type(), $group->getIdentifier(), $values);
308 309 310
}

/**
311
 * Action: Remove entity from group.
312
 */
313 314
function og_rules_remove_entity_from_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group) {
  og_ungroup($group->type(), $group->getIdentifier(), $entity->type(), $entity->value());
315
}
316 317

/**
318
 * OG Rules integration access callback.
319
 */
320 321 322
function og_rules_integration_access($type, $name) {
  // Grant everyone access to conditions.
  return $type == 'condition' || user_access('administer group');
323 324
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/**
 * Implements hook_rules_condition_info().
 */
function og_rules_condition_info() {
  $items = array();
  $items['og_user_has_permission'] = array(
    'label' => t('User has group permission'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'permission' => array(
        'type' => 'text',
        'label' => t('Permission'),
        'description' => t('The permission to check for.'),
        'options list' => 'og_rules_user_has_permission_options_list',
        'restriction' => 'input',
      ),
      'group' => array(
342
        'type' => array_keys(og_get_all_group_entity()),
343 344
        'label' => t('Group'),
        'description' => t('The group for which permission should be checked.'),
345
        'wrapped' => TRUE,
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
      ),
      'account' => array(
        'type' => 'user',
        'label' => t('User'),
      ),
    ),
    'base' => 'og_rules_user_has_permission',
    'access callback' => 'og_rules_integration_access',
  );
  $items['og_user_in_group'] = array(
    'label' => t('User is group member'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'account' => array(
        'type' => 'user',
        'label' => t('User'),
362
        'wrapped' => TRUE,
363 364
      ),
      'group' => array(
365
        'type' => array_keys(og_get_all_group_entity()),
366
        'label' => t('Group'),
367
        'wrapped' => TRUE,
368 369 370
      ),
      'states' => array(
        'type' => 'list<integer>',
371
        'label' => t('Membership states'),
372 373 374 375 376 377
        'restriction' => 'input',
        'options list' => 'og_group_content_states',
        'optional' => TRUE,
        'default value' => array(OG_STATE_ACTIVE),
      ),
    ),
378
    'base' => 'og_rules_condition_user_in_group',
379 380 381 382 383 384 385 386 387 388 389 390 391 392
    'access callback' => 'og_rules_integration_access',
  );
  $items['og_entity_in_group'] = array(
    'label' => t('Entity is in group'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'entity' => array(
        // Do not use type 'entity' but restrict the type to group content type.
        'type' => array_keys(og_get_all_group_content_entity()),
        'label' => t('Group content entity'),
        'description' => t('The group content which will be checked.'),
        'wrapped' => TRUE,
      ),
      'group' => array(
393
        'type' => array_keys(og_get_all_group_entity()),
394
        'label' => t('Group'),
395
        'wrapped' => TRUE,
396 397 398
      ),
    ),
    'base' => 'og_rules_condition_entity_in_group',
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    'access callback' => 'og_rules_integration_access',
  );
  $items['og_entity_is_group'] = array(
    'label' => t('Entity is group'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'entity' => array(
        // Do not use type 'entity' but restrict the type to group type.
        'type' => array_keys(og_get_all_group_entity()),
        'label' => t('Entity'),
        'wrapped' => TRUE,
      ),
    ),
    'base' => 'og_rules_entity_is_group',
    'access callback' => 'og_rules_integration_access',
  );
  $items['og_entity_is_group_content'] = array(
    'label' => t('Entity is group content'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'entity' => array(
        // Do not use type 'entity' but restrict the type to group content type.
        'type' => array_keys(og_get_all_group_content_entity()),
        'label' => t('Entity'),
        'wrapped' => TRUE,
      ),
    ),
    'base' => 'og_rules_entity_is_group_content',
    'access callback' => 'og_rules_integration_access',
  );
  return $items;
}

/**
 * Condition: User has group permisison.
 */
435 436
function og_rules_user_has_permission($permission, EntityDrupalWrapper $group, $account) {
  return og_user_access($group->type(), $group->getIdentifier(), $permission, $account);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
}

/**
 * Condition user has group permission options list callback for permissions.
 */
function og_rules_user_has_permission_options_list() {
  $perms = array();
  foreach (og_get_permissions() as $perm => $value) {
    // By keeping them keyed by module we can use optgroups with the
    // 'select' type.
    $perms[$value['module']][$perm] = strip_tags($value['title']);
  }
  return $perms;
}

/**
453
 * Condition: Entity is in group.
454
 */
455 456
function og_rules_condition_entity_in_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group) {
  return og_is_member($group->type(), $group->getIdentifier(), $entity->type(), $entity->value());
457 458 459 460 461
}

/**
 * Condition: User is in group.
 */
462 463
function og_rules_condition_user_in_group(EntityDrupalWrapper $entity, EntityDrupalWrapper $group, $states = array(OG_STATE_ACTIVE)) {
  return og_is_member($group->type(), $group->getIdentifier(), $entity->type(), $entity->value(), $states);
464 465 466 467 468 469 470 471 472 473 474 475 476
}

/**
 * Condition "User is in group" help.
 */
function og_rules_condition_user_in_group_help() {
  return t('Evaluates to TRUE if the user is a member of the group and the membership meets the configured valid membership state.');
}

/**
 * Condition: Entity is group.
 */
function og_rules_entity_is_group(EntityDrupalWrapper $entity) {
477
  return (bool) og_is_group($entity->type(), $entity->getIdentifier());
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
}

/**
 * Condition: "Entity is group" help.
 */
function og_rules_entity_is_group_help() {
  return t('Determines whether the entity is an active group.');
}

/**
 * Condition: Entity is group content.
 */
function og_rules_entity_is_group_content(EntityDrupalWrapper $entity) {
  return (bool) og_is_group_content_type($entity->type(), $entity->getBundle());
}

/**
 * Condition: "Entity is group content" help.
 */
function og_rules_entity_is_group_content_help() {
  return t('Determines whether the entity is group content, i.e. it can be or is used as group content.');
}
500

501 502 503
/**
 * @}
 */