You are here

function _homebox_save_user_settings in Homebox 7.2

Same name and namespace in other branches
  1. 6.3 homebox.module \_homebox_save_user_settings()
  2. 6.2 homebox.module \_homebox_save_user_settings()
  3. 7.3 homebox.module \_homebox_save_user_settings()

Save the user's page settings

Parameters

$page: A page object

$blocks: An array representing the user's page settings

4 calls to _homebox_save_user_settings()
homebox_add_block in ./homebox.module
Add a custom block to a user's page.
homebox_block_edit_form_builder_submit in ./homebox.module
Save settings for the block and render a replacement with the updated settings.
homebox_save_form_submit in ./homebox.module
_homebox_get_user_settings in ./homebox.module
Fetch user page settings, if they exist.

File

./homebox.module, line 1423
Homebox main file, takes care of global functions settings constants, etc.

Code

function _homebox_save_user_settings($page, $blocks) {
  global $user;

  // This function is only called after user status
  // and perms were checked
  if (!is_object($page)) {
    $page = homebox_load($page);
  }
  $user_blocks = _homebox_get_user_settings($page);
  $i = 0;
  foreach ($blocks as $key => $value) {

    // Add weights to blocks if there's not one already set.
    if (!isset($blocks[$key]['weight'])) {
      $blocks[$key]['weight'] = ++$i;
    }

    // Carry over existing settings for defined blocks
    if (isset($page->settings['blocks'][$key])) {
      $blocks[$key] = array_merge($page->settings['blocks'][$key], $blocks[$key]);
    }

    // Carry over settings for block copies since these do not get merged above
    if ($user_blocks !== FALSE && isset($user_blocks[$key])) {
      $blocks[$key] = array_merge($user_blocks[$key], $blocks[$key]);
    }
  }
  if ($user_blocks !== FALSE) {

    // Preserve any existing user blocks, which are page blocks, and not already
    // saved above. These are disabled page defaults, and need to remain to
    // continue overriding the page default.
    foreach (array_intersect_key($user_blocks, array_diff_key($page->settings['blocks'], $blocks)) as $key => $value) {
      $blocks[$key] = $value;
    }
  }

  // Remove any old settings
  db_delete('homebox_users')
    ->condition('uid', $user->uid)
    ->condition('name', $page->name)
    ->execute();

  // Update settings
  $data = new stdClass();
  $data->uid = $user->uid;
  $data->name = $page->name;
  $data->settings = $blocks;
  drupal_write_record('homebox_users', $data);
  _homebox_get_user_settings($page, FALSE, TRUE);
}