You are here

function fusion_apply_rule_is_visible in Fusion Accelerator 7.2

Same name and namespace in other branches
  1. 7 fusion_apply/fusion_apply_rules.module \fusion_apply_rule_is_visible()

Determines if the rule should be visible for a given path.

Parameters

$rid: The rule ID.

$path: (optional) The path to check. Defaults to the path of the current page.

$account: (optional) The account to check. Defaults to currently logged in user.

Return value

TRUE if the rule should be visible, FALSE otherwise.

3 calls to fusion_apply_rule_is_visible()
FusionApplyRulesApiTestCase::testFusionApplyRulesVisibility in fusion_apply/tests/fusion_apply.test
Tests visibility of rules.
rules_fusion_apply_contextual_links in fusion_apply/fusion_apply_rules.inc
Fusion Apply contextual links handler.
rules_fusion_apply_preprocess_index_handler in fusion_apply/fusion_apply_rules.inc
Fusion Apply preprocess index handler.

File

fusion_apply/fusion_apply_rules.module, line 160

Code

function fusion_apply_rule_is_visible($rid, $path = NULL, $account = NULL) {
  global $user;
  if (!isset($account)) {
    $account = $user;
  }
  if ($rule = fusion_apply_rule_load($rid)) {
    if (!isset($path)) {
      $path = $_GET['q'];
    }

    // Check role visibility.
    if (!empty($rule->roles) && $account->uid != 1 && !count(array_intersect(array_keys($account->roles), $rule->roles))) {
      return FALSE;
    }

    // Check content type visibility.
    // If a rule has no node types associated, it is displayed for every type.
    // For rules with node types associated, if the node type does not match
    // the settings from this rule, return FALSE.
    if (!empty($rule->node_types)) {
      $node = menu_get_object('node', 1, $path);
      $node_types = node_type_get_types();
      if (arg(0, $path) == 'node' && arg(1, $path) == 'add' && arg(2, $path)) {
        $node_add_arg = strtr(arg(2), '-', '_');
      }
      if (!empty($node)) {

        // This is a node or node edit page.
        if (empty($rule->node_types[$node->type])) {

          // This rule should not be displayed for this node type.
          return FALSE;
        }
      }
      elseif (isset($node_add_arg) && isset($node_types[$node_add_arg])) {

        // This is a node creation page.
        if (!isset($rule->node_types[$node_add_arg]) || !$rule->node_types[$node_add_arg]) {

          // This rule should not be displayed for this node type.
          return FALSE;
        }
      }
      else {

        // This is not a node page, remove the rule.
        return FALSE;
      }
    }

    // Match path if necessary.
    if ($rule->pages) {

      // Convert path to lowercase. This allows comparison of the same path
      // with different case. Ex: /Page, /page, /PAGE.
      $pages = drupal_strtolower($rule->pages);
      if ($rule->visibility < FUSION_APPLY_RULE_VISIBILITY_PHP) {

        // Convert the Drupal path to lowercase
        $path = drupal_strtolower(drupal_get_path_alias($path));

        // Compare the lowercase internal and lowercase path alias (if any).
        $page_match = drupal_match_path($path, $pages);
        if ($path != $_GET['q']) {
          $page_match = $page_match || drupal_match_path($path, $pages);
        }

        // When $rule->visibility has a value of 0 (FUSION_APPLY_RULE_VISIBILITY_NOTLISTED),
        // the rule is displayed on all pages except those listed in $rule->pages.
        // When set to 1 (FUSION_APPLY_RULE_VISIBILITY_LISTED), it is displayed only on those
        // pages listed in $rule->pages.
        $page_match = !($rule->visibility xor $page_match);
      }
      elseif (module_exists('php')) {
        $page_match = php_eval($rule->pages);
      }
      else {
        $page_match = FALSE;
      }
    }
    else {
      $page_match = TRUE;
    }
    return $page_match;
  }
  return FALSE;
}