You are here

function _homebox_apply_user_settings in Homebox 6

Apply user defined settings like positionning bloxes sorting

Parameters

$blocks: An array of blocks

Return value

The same array, but sorted with user defined settings if any

1 call to _homebox_apply_user_settings()
homebox_load_blocks_in_regions in ./homebox.module
Loads available blocks for user

File

./homebox.module, line 385
Home box main file, takes care of global functions settings constants, etc.

Code

function _homebox_apply_user_settings($blocks, $pid) {
  global $user;

  // Blocks that the user already had in his settings
  $user_blocks = _homebox_db_fetch_all_blocks(db_query("SELECT * FROM {homebox_users} WHERE uid = %d AND pid = %d ORDER BY region ASC, weight ASC", $user->uid, $pid));

  // Blocks that $user has in his table but is not a 'default' one, aka deprecated block
  $deprecated_blocks = array_diff_key($user_blocks, $blocks);
  foreach ($deprecated_blocks as $key => $block) {
    db_query("DELETE FROM {homebox_users} WHERE uid = %d AND pid = %d AND bid = %d", $user->uid, $pid, $block['bid']);
    unset($user_blocks[$key]);
  }

  // Blocks that $user doesn't have yet in his table ('default')
  $new_blocks = array_diff_key($blocks, $user_blocks);

  // We insert those new blocks in the user table
  foreach ($new_blocks as $key => $block) {
    $block['uid'] = $user->uid;
    $block['pid'] = $pid;
    drupal_write_record('homebox_users', $block);
  }

  // Merge default blocks settings with user defined settings
  // we don't need to merge newly inserted ones since there set to default values
  $blocks = array_merge($blocks, $user_blocks);
  return $blocks;
}