You are here

function agreement_type_get_best_match in Agreement 7.2

Get the best match agreement type for a user and path.

Parameters

object $account: The user account to check.

string $path: The path alias to check based on current path.

Return value

bool|array The agreement type or FALSE if none found that matched the criteria.

1 call to agreement_type_get_best_match()
agreement_init in ./agreement.module
Implements hook_init().

File

./agreement.module, line 417
Agreement module code - agreement.module.

Code

function agreement_type_get_best_match($account, $path) {
  $types = agreement_type_load();
  $default_exceptions = array(
    'user/logout',
    'admin/config/people/agreement',
    'admin/config/people/agreement/*',
    'admin/config/people/agreement/manage/*',
  );

  // Get a list of pages to never display agreement on.
  $exceptions = array_reduce($types, function (&$result, $item) {
    $result[] = $item['path'];
    return $result;
  }, $default_exceptions);
  $exception_string = implode("\n", $exceptions);
  if (drupal_match_path($path, $exception_string)) {
    return FALSE;
  }

  // Reduce the agreement types based on the agreement restricted roles.
  $account_roles = array_keys($account->roles);
  $role_types = array_reduce($types, function (&$result, $item) use ($account) {
    if (_agreement_user_has_role($account, $item['settings']['role'])) {
      $result[$item['name']] = $item;
    }
    return $result;
  }, array());

  // Try to find an agreement type that matches the path.
  $info = array_reduce($role_types, function (&$result, $item) use ($path, $account) {
    if ($result) {
      return $result;
    }
    $pattern = html_entity_decode(strtolower($item['settings']['visibility_pages']));
    $has_match = drupal_match_path($path, $pattern);
    $has_agreed = agreement_has_agreed($account, $item);
    if (0 === (int) $item['settings']['visibility_settings'] && FALSE === $has_match && !$has_agreed) {

      // An agreement type exists that matches any page.
      $result = $item;
    }
    elseif (1 === (int) $item['settings']['visibility_settings'] && $has_match && !$has_agreed) {

      // An agreement type exists that matches the current path.
      $result = $item;
    }
    return $result;
  }, FALSE);
  return $info;
}