You are here

function themekey_rule_set in ThemeKey 7.2

Same name and namespace in other branches
  1. 6.4 themekey_build.inc \themekey_rule_set()
  2. 6.2 themekey_build.inc \themekey_rule_set()
  3. 6.3 themekey_build.inc \themekey_rule_set()
  4. 7.3 themekey_build.inc \themekey_rule_set()
  5. 7 themekey_build.inc \themekey_rule_set()

Stores ThemeKey rules in database. It creates a new dataset or updates an existing one.

Parameters

$item: reference to an associative array containing a ThemeKey rule structure:

  • id
  • property
  • operator
  • value
  • weight
  • theme
  • enabled
  • wildcards
  • parent

$module: name of the module that sets the item

Throws

ThemeKeyRuleConflictException

5 calls to themekey_rule_set()
ThemekeyMultipleNodePropertiesTestCase::testMultipleProperties in tests/ThemekeyMultipleNodePropertiesTestCase.test
themekey_features_save_rule_childs in ./themekey_features.module
Takes a serialized ThemeKey Rule Chain as created by themekey_features_load_rule_childs() and adds it to the current one in the database with it. Conflicting rules will be stored as well, but disabled.
themekey_rule_chain_form_submit in ./themekey_admin.inc
Form submission handler for themekey_rule_chain_form().
themekey_ui_set_path_theme in ./themekey_ui_helper.inc
Saves a theme assigned to a path alias as ThemeKey rule
themekey_update_static_rule in ./themekey_build.inc
Adds or modifies a so-called static rule in the database. Static rules can be moved around in the chain and enabled or disabled by the site administrator, but the values are immutable. There's one static rule per static property.

File

./themekey_build.inc, line 330
The functions in this file are the back end of ThemeKey which should be used only if you configure something, but not when ThemeKey switches themes.

Code

function themekey_rule_set(&$item, $module = 'themekey') {
  if ('drupal:path' == $item['property']) {
    list($item['value'], $item['wildcards']) = themekey_prepare_custom_path($item['value']);
  }
  else {
    $item['wildcards'] = array();
  }
  if (empty($item['module'])) {
    $item['module'] = trim($module);
  }

  // TRANSACTIONS - SEE http://drupal.org/node/355875
  // The transaction opens here.
  $txn = db_transaction();
  if (empty($item['id'])) {
    if ($item['enabled']) {
      $id = db_select('themekey_properties', 'tp')
        ->fields('tp', array(
        'id',
      ))
        ->condition('property', $item['property'])
        ->condition('operator', $item['operator'])
        ->condition('value', $item['value'])
        ->condition('parent', $item['parent'])
        ->condition('enabled', 1)
        ->execute()
        ->fetchField();
      if ($id) {
        throw new ThemeKeyRuleConflictException(t('New rule conflicts with an existing rule on the same level.'), $id);
      }
    }

    // new entry should be added at the end of the chain
    $result = db_select('themekey_properties', 'tp');
    $result
      ->addExpression('MAX(weight)', 'weight');
    $weight = $result
      ->execute()
      ->fetchField();

    // if query fails $weight will be FALSE which will cause $item['weight'] to be set to '1'
    $item['weight'] = 1 + $weight;
    drupal_write_record('themekey_properties', $item, array());
  }
  else {
    if ($item['enabled']) {
      $id = db_select('themekey_properties', 'tp')
        ->fields('tp', array(
        'id',
      ))
        ->condition('property', $item['property'])
        ->condition('operator', $item['operator'])
        ->condition('value', $item['value'])
        ->condition('parent', $item['parent'])
        ->condition('enabled', 1)
        ->condition('id', $item['id'], '<>')
        ->execute()
        ->fetchField();
      if ($id) {
        throw new ThemeKeyRuleConflictException(t('Updated rule conflicts with an existing rule on the same level.'), $id);
      }
    }
    drupal_write_record('themekey_properties', $item, 'id');
  }

  // $txn goes out of scope here, and the entire transaction commits.
}