You are here

function defaultcontent_cron in Default Content 7

Same name and namespace in other branches
  1. 7.2 defaultcontent.module \defaultcontent_cron()

Implements hook_cron().

we check to see if there are any defaults that have not been load in the db. if we have any we load them

1 call to defaultcontent_cron()
defaultcontent_modules_enabled in ./defaultcontent.module
Implements hook_modules_enable().

File

./defaultcontent.module, line 124
Module file for the Default content module which allow export and import of default content in a Drupal site.

Code

function defaultcontent_cron($allow_first_content = TRUE) {
  if ($allow_first_content) {
    variable_set('node_content_enabled', TRUE);
  }

  // I have to load the freature files
  // I am thinking there should be a way to have features do this but I do not
  // know what it would be
  $features = features_get_features();
  foreach ($features as $f_name => $feature) {
    if (isset($feature->info['features']['content'])) {
      module_load_include('inc', $f_name, $f_name . '.features.content');
    }
    if (isset($feature->info['features']['content_menu_links'])) {
      module_load_include('inc', $f_name, $f_name . '.features.content_menu_links');
    }
  }

  // features_include_defaults();
  $defaults = module_invoke_all('content_defaults');
  usort($defaults, 'defaultcontent_import_sort');
  $records = defaultcontent_get_default();
  $current = array();
  foreach ($records as $record) {
    $current[] = $record->name;
  }
  foreach ($defaults as $default) {

    // we check to see if that default has been loaded (even if the node
    // has been deleted) before we do a load
    if (!in_array($default->machine_name, $current)) {
      defaultcontent_import_node($default);
    }
  }

  //Turns out if this isn't called only once after EACH install then we get ourselves in trouble - so

  //we'll just run one time for each change - Major issue with this approach is that install->uninstalling and reinstalling an app will result in no menu item

  //added

  //Yes, its hacky, sorry :(
  $default_menu_links = module_invoke_all('content_menu_links_defaults');
  $hashes = variable_get('defaultcontent_hashes', array());

  //arrays are value copied when assigned in php - so this is safe
  $inverse = $hashes;
  foreach ($default_menu_links as $key => $default) {
    $str = implode(array_keys($default), ',');
    $hash = md5($key . $str);
    $run = variable_get('defaultcontent_run_' . $hash, FALSE);
    if (!$run) {
      $try = defaultcontent_import_menu_link($key, $default);
      if ($try) {
        variable_set('defaultcontent_run_' . $hash, TRUE);
        array_push($hashes, $hash);
      }
    }
    $key = array_search($hash, $inverse);
    unset($inverse[$key]);
  }
  foreach ($inverse as $i => $hash) {
    variable_del('defaultcontent_run_' . $hash);
    unset($hashes[$i]);
  }
  $hashes = array_values($hashes);
  variable_set('defaultcontent_hashes', $hashes);
}