You are here

function purl_modifiers in Persistent URL 6

Same name and namespace in other branches
  1. 7 purl.module \purl_modifiers()

Queries the database & modules for valid values based on modifing method.

Modules that wish to provide in-code values should implement the hook_purl_modifiers(). Which should return an array of values by by provider.

For example:

return array( 'my_module => array( array('value' => 'foo', 'id' => 1), array('value' => 'bar', 'id' => 2), ), );

Parameters

$requested_method: A string identifier of a purl type/method

$reset: TRUE to delete static and persistent modifier caches.

Return value

Array of modifiers

6 calls to purl_modifiers()
purl_admin in ./purl.admin.inc
Page callback for the purl administration page.
purl_delete in ./purl.module
Delete a modifier entry from the database.
purl_generate_rewrite_elements in ./purl.module
Generate a purl_path_element for provider
purl_parse in ./purl.module
Parses a query string of various types (url, domain, etc.) and returns an array of any found values and their respective providers/id values.
purl_save in ./purl.module
Save modifier to database. Will insert new entry if no ID is provided and update an existing one otherwise.

... See full list

File

./purl.module, line 412

Code

function purl_modifiers($requested_method = NULL, $reset = FALSE) {
  static $values;
  if (!isset($values) || $reset) {
    $values = array();
    if ($reset) {
      cache_clear_all('purl_modifiers:', 'cache', TRUE);
    }
  }
  if (isset($requested_method) && !isset($values[$requested_method]) && !$reset) {
    $cache = cache_get("purl_modifiers:{$requested_method}");
    if ($cache) {
      $values[$requested_method] = $cache->data;
    }
  }
  if (!isset($values[$requested_method]) || $reset) {

    // Invoke purl_modifiers() and gather all values
    // provided "in code" (or stored by their respective modules)
    $providers = module_invoke_all('purl_modifiers');
    foreach ($providers as $provider => $items) {

      // Store providers for use when retrieving db values
      $method = variable_get('purl_method_' . $provider, 'path');

      // If using a value pair we don't need to cache the valid values.
      $info = ctools_get_plugins('purl', 'processor', $method);
      if (!empty($info['null_id'])) {
        $value = variable_get('purl_method_' . $provider . '_key', false);
        if ($value != false) {
          $values[$method][$value] = array(
            'provider' => $provider,
            'id' => null,
          );
        }
      }
      else {
        foreach ($items as $item) {
          if ($item['value'] && $item['id']) {
            $values[$method][$item['value']] = array(
              'provider' => $provider,
              'id' => $item['id'],
            );
          }
        }
      }
    }

    // Gather database values -- we exclude providers that we have
    // already collected values for through code.
    $providers = array_diff_key(purl_providers(), $providers);

    // Pull in all modifiers stored in the DB.
    $result = db_query("SELECT * FROM {purl}");
    $db_values = array();
    while ($row = db_fetch_object($result)) {
      $db_values[$row->provider][$row->value] = array(
        'provider' => $row->provider,
        'id' => $row->id,
      );
    }
    foreach ($providers as $provider => $info) {
      $method = variable_get('purl_method_' . $provider, 'path');
      $info = ctools_get_plugins('purl', 'processor', $method);

      // Don't load all data base values for keyed pairs.
      if (!empty($info['null_id'])) {
        $value = variable_get('purl_method_' . $provider . '_key', false);
        if ($value != false) {
          $values[$method][$value] = array(
            'provider' => $provider,
            'id' => null,
          );
        }
      }
      else {
        if (!empty($db_values[$provider])) {
          $values[$method] = isset($values[$method]) ? $values[$method] : array();
          $values[$method] = $values[$method] + $db_values[$provider];
        }
      }
    }
    if (isset($requested_method) && isset($values[$requested_method])) {
      cache_set("purl_modifiers:{$requested_method}", $values[$requested_method]);
    }
  }
  return isset($values[$requested_method]) ? $values[$requested_method] : array();
}