You are here

function user_dashboard_set_default in UserDashboard 7

AJAX callback.

Set the default blocks that will be initialized for new user registrations.

1 string reference to 'user_dashboard_set_default'
user_dashboard_menu in ./user_dashboard.module
Implements hook_menu().

File

./user_dashboard.module, line 620
The User Dashboard module forks Drupal 7's awesome Dashboard module to provide an individual dashboard for each user on the site.

Code

function user_dashboard_set_default() {

  // Check the form token to make sure we have a valid request.
  if (!empty($_REQUEST['form_token']) && drupal_valid_token($_REQUEST['form_token'], 'user-dashboard-update')) {
    $default_blocks = array();
    parse_str($_REQUEST['regions'], $regions);
    foreach ($regions as $region_name => $blocks) {
      foreach ($blocks as $weight => $block_string) {

        // Parse the query string to determine the block's module and delta.
        preg_match('/block-([^-]+)-(.+)/', $block_string, $matches);
        $block = new stdClass();
        $block->module = $matches[1];
        $block->delta = preg_replace('/[0-9]/', '', $matches[2]);
        $block->region = $region_name;
        $block->weight = $weight;
        $block->status = 1;
        $block->theme = variable_get('theme_default', 'garland');
        if ($block->region !== 'disabled_blocks') {

          // Certain block module/delta combinations (like from Views
          // blocks) may come in with an incorrect delta, where '-'
          // are used in place of '_', so we need to ensure we're getting
          // the right delta by querying the {blocks} table.
          $delta_obj = db_query("SELECT delta FROM {block} WHERE module = :module AND REPLACE(REPLACE(delta, '-', ''), '_', '') = :delta", array(
            ':module' => $block->module,
            ':delta' => preg_replace('/[-_]/', '', $block->delta),
          ))
            ->fetchObject();
          $block->delta = $delta_obj->delta;
          $default_blocks[] = $block;
        }
      }
    }
    variable_set('user_dashboard_default_blocks', $default_blocks);
    drupal_set_message(t('Default blocks have been set.'), 'status', FALSE);
    echo drupal_json_encode(array(
      'messages' => theme('status_messages'),
    ));
  }
  drupal_exit();
}