<?php

/**
 * @file
 * Profile2 og access integration module.
 */

/**
 * Implements hook_og_permission().
 */
function profile2_og_access_og_permission() {
  $permissions = array();
  // Generate per profile type permissions.
  foreach (profile2_get_types() as $type) {
    $type_name = check_plain($type->type);
    $permissions += array(
      "edit any $type_name profile" => array(
        'title' => t('%type_name: Edit any profile', array('%type_name' => $type->label)),
      ),
      "view any $type_name profile" => array(
        'title' => t('%type_name: View any profile', array('%type_name' => $type->label)),
      ),
    );
  }
  return $permissions;
}

/**
 * Implements hook_profile2_access().
 *
 * @see profile2_profile2_access()
 */
function profile2_og_access_profile2_access($op, $profile = NULL, $account = NULL) {
  if (isset($profile) && ($type_name = $profile->type) && $profile->identifier() && $op != 'delete') {
    // Only return TRUE if og grants access. So other modules may still grant
    // access in case og does not.
    if (og_user_access_entity("$op any $type_name profile", 'profile2', $profile, $account)) {
      return TRUE;
    }
  }
}