fusion_apply_rules.module in Fusion Accelerator 7
Same filename and directory in other branches
File
fusion_apply/fusion_apply_rules.moduleView source
<?php
/**
* Show this rule on every page except the listed pages.
*/
define('FUSION_APPLY_RULE_VISIBILITY_NOTLISTED', 0);
/**
* Show this rule on only the listed pages.
*/
define('FUSION_APPLY_RULE_VISIBILITY_LISTED', 1);
/**
* Show this rule if the associated PHP code returns TRUE.
*/
define('FUSION_APPLY_RULE_VISIBILITY_PHP', 2);
function fusion_apply_rules_init() {
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'fusion_apply') . '/fusion_apply_rules.inc';
}
// ------------------------------------------------------------------
// Rule functions.
/**
* Validate a rule object.
*
* @param $rule
* A rule object.
*
* @return
* TRUE on success, FALSE on failure.
*/
function fusion_apply_rule_validate(&$rule) {
if (empty($rule->title) || empty($rule->rule_type)) {
return FALSE;
}
if (!isset($rule->node_types)) {
$rule->node_types = array();
}
if (!isset($rule->roles)) {
$rule->roles = array();
}
if (!isset($rule->visibility)) {
$rule->visibility = 0;
}
if (!isset($rule->pages)) {
$rule->pages = '';
}
if (!is_array($rule->node_types) || !is_array($rule->roles)) {
return FALSE;
}
if ($rule->visibility !== 0 && $rule->visibility !== 1 && $rule->visibility !== 2) {
return FALSE;
}
return TRUE;
}
/**
* Save a skins rule object.
*
* @param $rule
* A rule object.
*
* @return
* The rule ID.
*/
function fusion_apply_rule_save($rule) {
// Make sure we're getting valid data.
if (!fusion_apply_rule_validate($rule)) {
return FALSE;
}
$status = drupal_write_record('fusion_apply_rules', $rule, !empty($rule->rid) ? array(
'rid',
) : array());
return $status;
}
/**
* Load a rule object.
*
* @param $rid
* (optional) The rule ID.
*
* @return
* A rule object. If no $rid is specified an array of all rules will be
* returned.
*/
function fusion_apply_rule_load($rid = NULL) {
$rids = isset($rid) ? array(
$rid,
) : array();
$rules = fusion_apply_rule_load_multiple($rids);
return $rules ? reset($rules) : FALSE;
}
/**
* Loads multiple rule objects.
*
* @param $rids
* An array of rule IDs. Optional.
* @param $conditions
* An array of conditions on the {fusion_apply_rules} table in the form 'field' =>
* $value.
*
* @return
* An array of rule objects indexed by rid. If $rids is not provided, all
* rules are returned.
*/
function fusion_apply_rule_load_multiple($rids = array(), $conditions = array()) {
$rules = array();
$select = db_select('fusion_apply_rules')
->fields('fusion_apply_rules');
if (!empty($rids)) {
$select
->condition('rid', $rids);
}
foreach ($conditions as $field => $condition) {
$select
->condition($field, $condition);
}
foreach ($select
->execute() as $rule) {
$rule->node_types = unserialize($rule->node_types);
$rule->roles = unserialize($rule->roles);
$rules[$rule->rid] = $rule;
}
return $rules;
}
/**
* Delete a skins rule object.
*
* @param $rid
* The rule ID.
*/
function fusion_apply_rule_delete($rid) {
if ($rule = fusion_apply_rule_load($rid)) {
db_delete('fusion_apply_rules')
->condition('rid', $rule->rid)
->execute();
db_delete('fusion_apply_skins')
->condition('module', 'page')
->condition('element', $rule->rid)
->execute();
}
}
/**
* Determines if the rule should be visible for a given path.
*
* @param $rid
* The rule ID.
* @param $path
* (optional) The path to check. Defaults to the path of the current page.
* @param $account
* (optional) The account to check. Defaults to currently logged in user.
*
* @return
* TRUE if the rule should be visible, FALSE otherwise.
*/
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;
}
Functions
Name | Description |
---|---|
fusion_apply_rules_init | |
fusion_apply_rule_delete | Delete a skins rule object. |
fusion_apply_rule_is_visible | Determines if the rule should be visible for a given path. |
fusion_apply_rule_load | Load a rule object. |
fusion_apply_rule_load_multiple | Loads multiple rule objects. |
fusion_apply_rule_save | Save a skins rule object. |
fusion_apply_rule_validate | Validate a rule object. |
Constants
Name | Description |
---|---|
FUSION_APPLY_RULE_VISIBILITY_LISTED | Show this rule on only the listed pages. |
FUSION_APPLY_RULE_VISIBILITY_NOTLISTED | Show this rule on every page except the listed pages. |
FUSION_APPLY_RULE_VISIBILITY_PHP | Show this rule if the associated PHP code returns TRUE. |