You are here

function user_dashboard_update in UserDashboard 7

AJAX callback.

Set the new weight of each region according to the drag-and-drop order.

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

File

./user_dashboard.module, line 544
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_update() {
  drupal_theme_initialize();
  global $theme_key;

  // 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')) {
    parse_str($_REQUEST['regions'], $regions);

    // Get a list of enabled modules with values that matches the
    // block_string from the block id.
    $module_list = module_list();
    foreach ($module_list as $module) {
      $module_map[$module] = str_replace('_', '-', $module);
    }
    foreach ($regions as $region_name => $blocks) {
      foreach ($blocks as $weight => $block_string) {

        // Replaces any malformed module name containing more than one word.
        foreach ($module_map as $module_name => $module) {
          $block_string = str_replace($module, $module_name, $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;

        // 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 LIMIT 1", array(
          ':module' => $block->module,
          ':delta' => preg_replace('/[-_]/', '', $block->delta),
        ))
          ->fetchObject();
        $block->delta = $delta_obj->delta;
        if ($block->region !== 'disabled_blocks') {
          db_merge('user_dashboard_block')
            ->key(array(
            'module' => $block->module,
            'delta' => $block->delta,
            'theme' => $theme_key,
            'uid' => (int) arg(1),
          ))
            ->fields(array(
            'status' => $block->status,
            'weight' => $block->weight,
            'region' => $block->region,
            'pages' => '',
          ))
            ->execute();
        }
        else {
          db_delete('user_dashboard_block')
            ->condition('uid', (int) arg(1))
            ->condition('module', $block->module)
            ->condition('delta', $block->delta)
            ->execute();
        }
      }
    }
    drupal_set_message(t('The configuration options have been saved.'), 'status', FALSE);
  }
  drupal_exit();
}