sTEFANs Subversion Web Contol (SWC)
Inc
[ class tree: Swc ] [ index: Swc ] [ all elements ]

Source for file user_settings.inc.php

Documentation is available at user_settings.inc.php

  1. <?php
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. SVN Web Control
  4. Copyright ©2006 by sTEFANs
  5. Created on 02.03.2006
  6. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. */
  23.  
  24. /**
  25.  * This file contains a class that encapsulates and stores / restores
  26.  * user settings of SWC.
  27.  * 
  28.  * @package Swc
  29.  * @subpackage Inc
  30.  * @author Stefan Schraml
  31.  * @copyright Copyright ©2006 by sTEFANs
  32.  * @license http://opensource.org/licenses/lgpl-license.php GNU General Public License
  33.  * @version v1.1.0
  34.  * @since v1.0.0
  35.  */
  36.  
  37. /** Common utility functions */
  38. require_once('inc/utils.inc.php');
  39. /** Common definitions */
  40. require_once('inc/var.inc.php');
  41.  
  42. /** A singleton containing settings made by the user via GUI.
  43.  * The instance of the class can be
  44.  * retrieved via <b>GetInstance</b>.
  45.  * 
  46.  * @package Swc
  47.  * @subpackage Inc
  48.  * @author Stefan Schraml
  49.  * @since v1.0.0
  50.  */
  51. class SwcUserSettings {
  52.     
  53.     /**
  54.      * The single instance.
  55.      * @since v1.0.0
  56.      */
  57.     private static $instance NULL;
  58.     
  59.     /**
  60.      * Settings array.
  61.      * @since v1.0.0
  62.      */
  63.     private $settings = NULL;
  64.     
  65.     /** 
  66.      * Returns the single instance of this class.
  67.      * @return SwcUserSettings Single instance of this class.
  68.      * 
  69.      * @since v1.0.0
  70.      */
  71.     public static function GetInstance(){
  72.         if (self::$instance == NULL){
  73.             self::$instance new SwcUserSettings();
  74.         }
  75.         return self::$instance;
  76.     }
  77.     
  78.     /** 
  79.      * Determines whether the top control bar should be minimized.
  80.      * @return boolean True if the control bar schould be minimized.
  81.      * 
  82.      * @since v1.0.0
  83.      */ 
  84.     public function IsMinimizeControlBar(){
  85.         return ($this->configs[OPT_MINIMIZE_CTRL_BAR== true false);
  86.     }
  87.     
  88.     /** 
  89.      * Sets whether the top control bar should be minimized.
  90.      * @param boolean $minimize True if the control bar schould be minimized.
  91.      * 
  92.      * @since v1.0.0
  93.      */
  94.     public function SetMinimizeControlBar($minimize){
  95.         $this->configs[OPT_MINIMIZE_CTRL_BAR($minimize 0);
  96.     }
  97.     
  98.     /** 
  99.      * Determines whether the project page should always be shown,
  100.      * regardles of the current action.
  101.      * @return boolean True if the project page should always be shown.
  102.      * 
  103.      * @since v1.0.0
  104.      */ 
  105.     public function IsPinProjectPage(){
  106.         return ($this->configs[OPT_PIN_PROJECT_PAGE!= true false);
  107.     }
  108.     
  109.     /** 
  110.      * Sets whether the project page should always be shown.
  111.      * @param boolean $pin True if project page should always be shown.
  112.      * 
  113.      * @since v1.0.0
  114.      */
  115.     public function SetPinProjectPage($pin){
  116.         $this->configs[OPT_PIN_PROJECT_PAGE($pin 0);
  117.     }
  118.     
  119.     /** 
  120.      * Determines whether the webspace should automatically
  121.      * be updated if it is outdated.
  122.      * @return boolean True if the webspace should
  123.      *  automatically be updated.
  124.      * 
  125.      * @since v1.0.0
  126.      */ 
  127.     public function IsAutoUpdate(){
  128.         return ($this->configs[OPT_WEBSPACE_AUTO_UPDATE!= true false);
  129.     }
  130.     
  131.     /** 
  132.      * Sets whether the webspace should automatically
  133.      * be updated if it is outdated.
  134.      * @param boolean $enable True if the webspace should
  135.      *  automatically be updated.
  136.      * 
  137.      * @since v1.0.0
  138.      */
  139.     public function SetAutoUpdate($enable){
  140.         $this->configs[OPT_WEBSPACE_AUTO_UPDATE($enable 0);
  141.     }
  142.     
  143.     /** 
  144.      * Determines whether command execution result
  145.      * should be opened within a separate browser window.
  146.      * @return boolean True if command execution result
  147.      *  should be opened within a separate browser window.
  148.      * 
  149.      * @since v1.0.0
  150.      */ 
  151.     public function SetSeparateResultWindow($enable){
  152.         $this->configs[OPT_SEPARATE_RESULT_WINDOW($enable 0);
  153.     }
  154.     
  155.     /** 
  156.      * Determines whether command execution result
  157.      * should be opened within a separate browser window.
  158.      * @return boolean True if command execution result
  159.      *  should be opened within a separate browser window.
  160.      * 
  161.      * @since v1.0.0
  162.      */ 
  163.     public function IsSeparateResultWindow(){
  164.         return ($this->configs[OPT_SEPARATE_RESULT_WINDOW!= true false);
  165.     }
  166.     
  167.     /** 
  168.      * Provides the width in pixel for the project frame.
  169.      * @return int Frame width in pixel.
  170.      * 
  171.      * @since v1.0.0
  172.      */ 
  173.     public function GetProjectFrameWidth(){
  174.         return $this->configs[OPT_FRAME_WIDTH];
  175.     }
  176.     
  177.     /** 
  178.      * Sets the width in pixel for the project frame.
  179.      * @param int $width Frame width in pixel.
  180.      * 
  181.      * @since v1.0.0
  182.      */
  183.     public function SetProjectFrameWidth($width){
  184.         $this->configs[OPT_FRAME_WIDTH$width;
  185.     }
  186.     
  187.     /** 
  188.      * Provides the height in pixel for the project frame.
  189.      * @return int Frame height in pixel.
  190.      * 
  191.      * @since v1.0.0
  192.      */ 
  193.     public function GetProjectFrameHeight(){
  194.         return $this->configs[OPT_FRAME_HEIGHT];
  195.     }
  196.     
  197.     /** 
  198.      * Sets the height in pixel for the project frame.
  199.      * @param int $width Frame height in pixel.
  200.      * 
  201.      * @since v1.0.0
  202.      */
  203.     public function SetProjectFrameHeight($height){
  204.         $this->configs[OPT_FRAME_HEIGHT$height;
  205.     }
  206.     
  207.     /** 
  208.      * Sets SVN command bar to visible or invisible.
  209.      * @param boolean $enable True if SVN command bar should
  210.      *  be visible.
  211.      * 
  212.      * @since v1.0.0
  213.      */
  214.     function SetSvnCommandBarVisible($visible){
  215.         $this->configs[OPT_SHOW_SVN_CMD_BAR($visible 0);
  216.     }
  217.     
  218.     /** 
  219.      * Determines whether SVN command bar is visible.
  220.      * @return boolean True if SVN command bar is visible.
  221.      * 
  222.      * @since v1.0.0
  223.      */     
  224.     function IsSvnCommandBarVisible(){
  225.         return ($this->configs[OPT_SHOW_SVN_CMD_BAR!= true false);
  226.     }
  227.     
  228.     /** 
  229.      * Sets SVN switch bar to visible or invisible.
  230.      * @param boolean $enable True if SVN switch bar should
  231.      *  be visible.
  232.      * 
  233.      * @since v1.0.0
  234.      */
  235.     function SetSvnSwitchBarVisible($visible){
  236.         $this->configs[OPT_SHOW_SVN_SWITCH_BAR($visible 0);
  237.     }
  238.     
  239.     /** 
  240.      * Determines whether SVN switch bar is visible.
  241.      * @return boolean True if SVN switch bar is visible.
  242.      * 
  243.      * @since v1.0.0
  244.      */
  245.     function IsSvnSwitchBarVisible(){
  246.         return ($this->configs[OPT_SHOW_SVN_SWITCH_BAR!= true false);
  247.     }
  248.     
  249.     /** 
  250.      * Processes the user settings. This basically
  251.      * means, that the function scans <b>$_GET</b>
  252.      * and <b>$_POST</b> (preferred) for changes
  253.      * in the settings, stores the changes and
  254.      * restores the user settings.<br/>
  255.      * <b>Important:</b>
  256.      * Because user settings are stored via cookies,
  257.      * it is important that this function is invoked
  258.      * before any HTML or whitespace is printed!
  259.      * @return boolean True if a user setting
  260.      *  has been changed within <b>$_POST</b> or
  261.      *  <b>$_GET</b>, false otherwise.
  262.      * 
  263.      * @since v1.0.0
  264.      */
  265.     public function ProcessSettings(){
  266.         $this->RestoreSettings();
  267.         $this->ScanArrayForSettings($_GET);
  268.         $this->ScanArrayForSettings($_POST);
  269.         $this->StoreSettings();
  270.     }
  271.     
  272.     /** 
  273.      * Scans the given array for user settings.
  274.      * @param array $array Array to scan.
  275.      * @return boolean True if changed settings are found.
  276.      * 
  277.      * @since v1.0.0
  278.      */
  279.     private function ScanArrayForSettings(&$array){
  280.         $idx 0;
  281.         $idxs $this->GetIndexArray();
  282.         while ($idx count($idxs)){
  283.             if (isset($array[$idxs[$idx]])){
  284.                 $this->configs[$idxs[$idx]] $array[$idxs[$idx]];
  285.             }
  286.             $idx++;
  287.         }
  288.     }
  289.     
  290.     /** 
  291.      * Stores the user settings within a cookie.
  292.      * 
  293.      * @since v1.0.0
  294.      */
  295.     private function StoreSettings(){
  296.         $vals '';
  297.         $idxs $this->GetIndexArray();
  298.         $idx 0;
  299.         while ($idx count($idxs)-1){
  300.             $vals .= $this->configs[$idxs[$idx]].';';
  301.             $idx++;
  302.         }
  303.         $vals .= $this->configs[$idxs[count($idxs)-1]];
  304.         // expires after 120 days
  305.         $expire time()+60*60*24*120;
  306.         setcookie(IDX_USER_SETTINGS$vals$expire);
  307.         // store them in the session as well in case user does not accept cookies.
  308.         $_SESSION[IDX_USER_SETTINGS$vals;
  309.     }
  310.     
  311.     /** 
  312.      * Restores the user settings from the cookie.
  313.      * 
  314.      * @since v1.0.0
  315.      */
  316.     private function RestoreSettings(){
  317.         $data NULL;
  318.         if (isset($_COOKIE[IDX_USER_SETTINGS])){
  319.             $data $_COOKIE[IDX_USER_SETTINGS];
  320.         else if (isset($_SESSION[IDX_USER_SETTINGS])){
  321.             $data $_SESSION[IDX_USER_SETTINGS];
  322.         }
  323.         if ($data != NULL){
  324.             $vals explode(';'$data);
  325.             $idxs $this->GetIndexArray();
  326.             if ($vals[0== 'v01'){
  327.                 $idx 0;
  328.                 while ($idx count($idxs)){
  329.                     $this->configs[$idxs[$idx]] $vals[$idx];
  330.                     $idx++;
  331.                 }
  332.             }
  333.         }
  334.     }
  335.     
  336.     /** 
  337.      * Returns an array that provides mapping of index. The key
  338.      * of this array is numeric and the value is the index
  339.      * for the internal array.
  340.      * @return array Key map.
  341.      * 
  342.      * @since v1.0.0
  343.      */
  344.     private function &GetIndexArray(){
  345.         $map array(
  346.             IDX_SETTINGS_VERSION,
  347.             OPT_MINIMIZE_CTRL_BAR,
  348.             OPT_PIN_PROJECT_PAGE,
  349.             OPT_WEBSPACE_AUTO_UPDATE,
  350.             OPT_SEPARATE_RESULT_WINDOW,
  351.             OPT_FRAME_WIDTH,
  352.             OPT_FRAME_HEIGHT,
  353.             OPT_SHOW_SVN_CMD_BAR,
  354.             OPT_SHOW_SVN_SWITCH_BAR
  355.             );
  356.         return $map;
  357.     }
  358.     
  359.     /** 
  360.      * Singleton c'tor. Initializes user settings with default values.
  361.      * 
  362.      * @since v1.0.0
  363.      */
  364.     private function __construct(){
  365.         $idxs $this->GetIndexArray();
  366.         $this->settings = array();
  367.         $this->configs[$idxs[0]] 'v01';
  368.         $this->configs[$idxs[1]] 0;
  369.         $this->configs[$idxs[2]] 0;
  370.         $this->configs[$idxs[3]] 0;
  371.         $this->configs[$idxs[4]] 0;
  372.         $this->configs[$idxs[5]] 1024;
  373.         $this->configs[$idxs[6]] 768;
  374.         $this->configs[$idxs[7]] 1;
  375.         $this->configs[$idxs[8]] 1;
  376.     }
  377.     
  378.     /** 
  379.      * Does nothing else as prevent from beeing cloned.
  380.      * 
  381.      * @since v1.0.0
  382.      */
  383.     public function __clone(){
  384.   }
  385. }
  386. ?>

Documentation generated on Fri, 03 Nov 2006 18:45:06 +0100 by phpDocumentor 1.3.0RC6
for sTEFANs POWERED BY eBC.bz