<?php

  /*
  
      user_preferences.php

      Changelog:
        2005-07-03 arne_sf: Replaced all instances of column name 'user' for table tbl_Users with 'username'
        2005-07-03 arne_sf: Replaced all instances of column name 'user' for table tbl_User_Preferences with 'userid'
        
      ## PAGE CONTAINS HANDYANDY MYSQL MODS ##
      ## Copyright (C) 2005 Andy Deakin (handyandy.org.uk) ##

      Imports the preferences from the tbl_User_Preferences
      table in the database.

      Copyright (C) 2003  Central Manchester CLC
      Copyright (C) 2003  David Thorne (davethorne@gmail.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

  // Security...
  if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__, '.php')) !== false) {
    header("location: index.php");
  }

  // Access DB
  require_once "system/db.php";

  // Returns all the preferences for a user (including defaults for the
  // system/domain)
  function get_user_prefs($userid) {

    // Make sure preference exists
    $user_prefs = array();

    $preflevels=array("user", "domain", "default");
    foreach ($preflevels as $level) {
      $level2 = ucwords($level);
      $sql = "SELECT * FROM tbl_{$level2}_Preferences";

      // Need to select relevant domain/user if needed
      if ($level == 'domain' && $_SESSION['_domain']) {
        $sql .= " WHERE domain={$_SESSION['_domain']}";
      } elseif ($level == 'user') {
        if ($_SESSION['_domain']) {
          $sql .= " WHERE userid=$userid AND
            domain={$_SESSION['_domain']}";
        } else {
          $sql .= " WHERE userid=$userid";
        }      
      }

      // Get all the preferences for the current level
      $$level = db_recordset($sql);
    }

    // Now get the actual preferences listed in the descriptions
    $descriptionsRS = db_recordset("SELECT * FROM tbl_Preference_Descriptions");
    foreach ($descriptionsRS as $description) {
      // Note on overridable:
      // Overridable means that a user preference can override a domain
      // preference. Domain preferences can _always_ override default
      // preferences.
      /* In other words:
              All preferences initially only have an entry in the default
              preferences table. Overridable preferences are settable by
              domain admins and users. Non-overridable preferences can only be
              set as domain preferences by domain admins, and users do not
              even know they exist.
      */
      // Work out search order for preferences
      // Note: the last level checked to contain a preference is the one used
      if ($description['overridable'] == 0) {
        // If preference can't be overriden by lower levels then the highest
        // level preference takes precendence, and default is only used if
        // a lower level preference doesn't exist
        $preflevels=array("default", "user", "domain");
      } else {
        // Lowest level takes precedence, and default values are considered
        $preflevels=array("default", "domain", "user");
      }

      // Now try to get the preference from each level
      foreach ($preflevels as $level) {
        $result = filter_records($$level, 'identifier',
        $description['identifier']);

        if (count($result) > 0) {
          $user_prefs[$result[0]['identifier']] = $result[0]['value'];
        }
      }
    }

    return $user_prefs;
  }

  function get_user_details($id) {
    // Create an associative array and populate it with the user's details
    $user = db_recordset("SELECT * FROM tbl_Users WHERE id=$id");

    $user_details = array();
    $user_details['name'] = $user[0]['name'];
    $user_details['email'] = $user[0]['email'];
    $user_details['username'] = $user[0]['username'];

    return $user_details;
  }
  
?>
