Dotclear


Ignore:
Timestamp:
03/08/18 17:58:39 (8 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Code formatting (PSR-2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.prefs.php

    r2566 r3730  
    1010# 
    1111# -- END LICENSE BLOCK ----------------------------------------- 
    12 if (!defined('DC_RC_PATH')) { return; } 
     12if (!defined('DC_RC_PATH')) {return;} 
    1313 
    1414/** 
     
    1919dcAuth $prefs property. You should create a new prefs instance when 
    2020updating another user prefs. 
    21 */ 
     21 */ 
    2222class dcPrefs 
    2323{ 
    24      protected $con;          ///< <b>connection</b> Database connection object 
    25      protected $table;        ///< <b>string</b> Prefs table name 
    26      protected $user_id;      ///< <b>string</b> User ID 
     24    protected $con;     ///< <b>connection</b> Database connection object 
     25    protected $table;   ///< <b>string</b> Prefs table name 
     26    protected $user_id; ///< <b>string</b> User ID 
    2727 
    28      protected $workspaces = array();        ///< <b>array</b> Associative workspaces array 
     28    protected $workspaces = array(); ///< <b>array</b> Associative workspaces array 
    2929 
    30      protected $ws;           ///< <b>string</b> Current workspace 
     30    protected $ws; ///< <b>string</b> Current workspace 
    3131 
    32      /** 
    33      Object constructor. Retrieves user prefs and puts them in $workspaces 
    34      array. Local (user) prefs have a highest priority than global prefs. 
     32    /** 
     33    Object constructor. Retrieves user prefs and puts them in $workspaces 
     34    array. Local (user) prefs have a highest priority than global prefs. 
    3535 
    36      @param    core      <b>dcCore</b>       dcCore object 
    37      @param    user_id   <b>string</b>       User ID 
    38      */ 
    39      public function __construct($core,$user_id) 
    40      { 
    41           $this->con =& $core->con; 
    42           $this->table = $core->prefix.'pref'; 
    43           $this->user_id =& $user_id; 
    44           try {$this->loadPrefs();} catch (Exception $e) { 
    45                if (version_compare($core->getVersion('core'),'2.3','>')) { 
    46                     trigger_error(__('Unable to retrieve workspaces:').' '.$this->con->error(), E_USER_ERROR); 
    47                } 
    48           } 
    49      } 
     36    @param    core        <b>dcCore</b>        dcCore object 
     37    @param    user_id    <b>string</b>        User ID 
     38     */ 
     39    public function __construct($core, $user_id) 
     40    { 
     41        $this->con     = &$core->con; 
     42        $this->table   = $core->prefix . 'pref'; 
     43        $this->user_id = &$user_id; 
     44        try { $this->loadPrefs();} catch (Exception $e) { 
     45            if (version_compare($core->getVersion('core'), '2.3', '>')) { 
     46                trigger_error(__('Unable to retrieve workspaces:') . ' ' . $this->con->error(), E_USER_ERROR); 
     47            } 
     48        } 
     49    } 
    5050 
    51      /** 
    52      Retrieves all workspaces (and their prefs) from database, with one query. 
    53      */ 
    54      private function loadPrefs() 
    55      { 
    56           $strReq = 'SELECT user_id, pref_id, pref_value, '. 
    57                     'pref_type, pref_label, pref_ws '. 
    58                     'FROM '.$this->table.' '. 
    59                     "WHERE user_id = '".$this->con->escape($this->user_id)."' ". 
    60                     'OR user_id IS NULL '. 
    61                     'ORDER BY pref_ws ASC, pref_id ASC'; 
    62           try { 
    63                $rs = $this->con->select($strReq); 
    64           } catch (Exception $e) { 
    65                throw $e; 
    66           } 
     51    /** 
     52    Retrieves all workspaces (and their prefs) from database, with one query. 
     53     */ 
     54    private function loadPrefs() 
     55    { 
     56        $strReq = 'SELECT user_id, pref_id, pref_value, ' . 
     57        'pref_type, pref_label, pref_ws ' . 
     58        'FROM ' . $this->table . ' ' . 
     59        "WHERE user_id = '" . $this->con->escape($this->user_id) . "' " . 
     60            'OR user_id IS NULL ' . 
     61            'ORDER BY pref_ws ASC, pref_id ASC'; 
     62        try { 
     63            $rs = $this->con->select($strReq); 
     64        } catch (Exception $e) { 
     65            throw $e; 
     66        } 
    6767 
    68           /* Prevent empty tables (install phase, for instance) */ 
    69           if ($rs->isEmpty()) { 
    70                return; 
    71           } 
     68        /* Prevent empty tables (install phase, for instance) */ 
     69        if ($rs->isEmpty()) { 
     70            return; 
     71        } 
    7272 
    73           do { 
    74                $ws = trim($rs->f('pref_ws')); 
    75                if (!$rs->isStart()) { 
    76                     // we have to go up 1 step, since workspaces construction performs a fetch() 
    77                     // at very first time 
    78                     $rs->movePrev(); 
    79                } 
    80                $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws,$rs); 
    81           } while(!$rs->isStart()); 
    82      } 
     73        do { 
     74            $ws = trim($rs->f('pref_ws')); 
     75            if (!$rs->isStart()) { 
     76                // we have to go up 1 step, since workspaces construction performs a fetch() 
     77                // at very first time 
     78                $rs->movePrev(); 
     79            } 
     80            $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws, $rs); 
     81        } while (!$rs->isStart()); 
     82    } 
    8383 
     84    /** 
     85    Create a new workspace. If the workspace already exists, return it without modification. 
    8486 
    85      /** 
    86      Create a new workspace. If the workspace already exists, return it without modification. 
     87    @param    ws    <b>string</b>        Workspace name 
     88    @return    <b>dcWorkspace</b>    The workspace created 
     89     */ 
     90    public function addWorkspace($ws) 
     91    { 
     92        if (!array_key_exists($ws, $this->workspaces)) { 
     93            $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws); 
     94        } 
     95        return $this->workspaces[$ws]; 
     96    } 
    8797 
    88      @param    ws   <b>string</b>       Workspace name 
    89      @return   <b>dcWorkspace</b>  The workspace created 
    90      */ 
    91      public function addWorkspace($ws) 
    92      { 
    93           if (!array_key_exists($ws, $this->workspaces)) { 
    94                $this->workspaces[$ws] = new dcWorkspace($GLOBALS['core'], $this->user_id, $ws); 
    95           } 
    96           return $this->workspaces[$ws]; 
    97      } 
     98    /** 
     99    Rename a workspace. 
    98100 
    99      /** 
    100      Rename a workspace. 
     101    @param    oldWs     <b>string</b>     Old workspace name 
     102    @param    newws     <b>string</b>     New workspace name 
     103    @return     <b>boolean</b> 
     104     */ 
     105    public function renWorkspace($oldNs, $newNs) 
     106    { 
     107        if (!array_key_exists($oldWs, $this->workspaces) || array_key_exists($newWs, $this->workspaces)) { 
     108            return false; 
     109        } 
    101110 
    102      @param    oldWs     <b>string</b>  Old workspace name 
    103      @param    newws     <b>string</b>  New workspace name 
    104      @return   <b>boolean</b> 
    105      */ 
    106      public function renWorkspace($oldNs,$newNs) 
    107      { 
    108           if (!array_key_exists($oldWs, $this->workspaces) || array_key_exists($newWs, $this->workspaces)) { 
    109                return false; 
    110           } 
     111        // Rename the workspace in the workspace array 
     112        $this->workspaces[$newWs] = $this->workspaces[$oldWs]; 
     113        unset($this->workspaces[$oldWs]); 
    111114 
    112           // Rename the workspace in the workspace array 
    113           $this->workspaces[$newWs] = $this->workspaces[$oldWs]; 
    114           unset($this->workspaces[$oldWs]); 
     115        // Rename the workspace in the database 
     116        $strReq = 'UPDATE ' . $this->table . 
     117        " SET pref_ws = '" . $this->con->escape($newWs) . "' " . 
     118        " WHERE pref_ws = '" . $this->con->escape($oldWs) . "' "; 
     119        $this->con->execute($strReq); 
     120        return true; 
     121    } 
    115122 
    116           // Rename the workspace in the database 
    117           $strReq = 'UPDATE '.$this->table. 
    118                " SET pref_ws = '".$this->con->escape($newWs)."' ". 
    119                " WHERE pref_ws = '".$this->con->escape($oldWs)."' "; 
    120           $this->con->execute($strReq); 
    121           return true; 
    122      } 
     123    /** 
     124    Delete a whole workspace with all preferences pertaining to it. 
    123125 
    124      /** 
    125      Delete a whole workspace with all preferences pertaining to it. 
     126    @param     ws     <b>string</b>     Workspace name 
     127    @return     <b>boolean</b> 
     128     */ 
     129    public function delWorkspace($ws) 
     130    { 
     131        if (!array_key_exists($ws, $this->workspaces)) { 
     132            return false; 
     133        } 
    126134 
    127      @param    ws   <b>string</b>  Workspace name 
    128      @return   <b>boolean</b> 
    129      */ 
    130      public function delWorkspace($ws) 
    131      { 
    132           if (!array_key_exists($ws, $this->workspaces)) { 
    133                return false; 
    134           } 
     135        // Remove the workspace from the workspace array 
     136        unset($this->workspaces[$ws]); 
    135137 
    136           // Remove the workspace from the workspace array 
    137           unset($this->workspaces[$ws]); 
     138        // Delete all preferences from the workspace in the database 
     139        $strReq = 'DELETE FROM ' . $this->table . 
     140        " WHERE pref_ws = '" . $this->con->escape($ws) . "' "; 
     141        $this->con->execute($strReq); 
     142        return true; 
     143    } 
    138144 
    139           // Delete all preferences from the workspace in the database 
    140           $strReq = 'DELETE FROM '.$this->table. 
    141                " WHERE pref_ws = '".$this->con->escape($ws)."' "; 
    142           $this->con->execute($strReq); 
    143           return true; 
    144      } 
     145    /** 
     146    Returns full workspace with all prefs pertaining to it. 
    145147 
    146      /** 
    147      Returns full workspace with all prefs pertaining to it. 
     148    @param    ws    <b>string</b>        Workspace name 
     149    @return    <b>dcWorkspace</b> 
     150     */ 
     151    public function get($ws) 
     152    { 
     153        if (array_key_exists($ws, $this->workspaces)) { 
     154            return $this->workspaces[$ws]; 
     155        } 
    148156 
    149      @param    ws   <b>string</b>       Workspace name 
    150      @return   <b>dcWorkspace</b> 
    151      */ 
    152      public function get($ws) 
    153      { 
    154           if (array_key_exists($ws, $this->workspaces)) { 
    155                return $this->workspaces[$ws]; 
    156           } 
     157        return; 
     158    } 
    157159 
    158           return null; 
    159      } 
     160    /** 
     161    Magic __get method. 
     162    @copydoc ::get 
     163     */ 
     164    public function __get($n) 
     165    { 
     166        return $this->get($n); 
     167    } 
    160168 
    161      /** 
    162      Magic __get method. 
    163      @copydoc ::get 
    164      */ 
    165      public function __get($n) 
    166      { 
    167           return $this->get($n); 
    168      } 
     169    /** 
     170    Returns $workspaces property content. 
    169171 
    170      /** 
    171      Returns $workspaces property content. 
    172  
    173      @return   <b>array</b> 
    174      */ 
    175      public function dumpWorkspaces() 
    176      { 
    177           return $this->workspaces; 
    178      } 
     172    @return    <b>array</b> 
     173     */ 
     174    public function dumpWorkspaces() 
     175    { 
     176        return $this->workspaces; 
     177    } 
    179178 
    180179} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map