You are here

function themekey_abstract_rule_set in ThemeKey 7.3

4 calls to themekey_abstract_rule_set()
themekey_abstract_update_static_rule in ./themekey_build.inc
themekey_css_rule_set in themekey_css/themekey_css_build.inc
Stores ThemeKey rules in database. It creates a new dataset or updates an existing one.
themekey_redirect_rule_set in themekey_redirect/themekey_redirect_build.inc
Stores ThemeKey rules in database. It creates a new dataset or updates an existing one.
themekey_rule_set in ./themekey_build.inc
Stores ThemeKey rules in database. It creates a new dataset or updates an existing one.

File

./themekey_build.inc, line 349
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_abstract_rule_set($table, &$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($table, '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) {

        // Transaction is only required for a lock => no rollback
        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($table, '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;

    // It is important to use drupal_write_record() because it sets $item['id']!
    drupal_write_record($table, $item, array());
  }
  else {
    if ($item['enabled']) {
      $id = db_select($table, '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) {

        // Transaction is only required for a lock => no rollback
        throw new ThemeKeyRuleConflictException(t('Updated rule conflicts with an existing rule on the same level.'), $id);
      }
    }
    drupal_write_record($table, $item, 'id');
  }

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