root/trunk/lib/SGL/CategoryPerms.php

Revision 3124, 6.0 kB (checked in by demian, 3 weeks ago)

bugfix to trunk merge -2

Line 
1 <?php
2 /* Reminder: always indent with 4 spaces (no tabs). */
3 // +---------------------------------------------------------------------------+
4 // | Copyright (c) 2006, Demian Turner                                         |
5 // | All rights reserved.                                                      |
6 // |                                                                           |
7 // | Redistribution and use in source and binary forms, with or without        |
8 // | modification, are permitted provided that the following conditions        |
9 // | are met:                                                                  |
10 // |                                                                           |
11 // | o Redistributions of source code must retain the above copyright          |
12 // |   notice, this list of conditions and the following disclaimer.           |
13 // | o Redistributions in binary form must reproduce the above copyright       |
14 // |   notice, this list of conditions and the following disclaimer in the     |
15 // |   documentation and/or other materials provided with the distribution.    |
16 // | o The names of the authors may not be used to endorse or promote          |
17 // |   products derived from this software without specific prior written      |
18 // |   permission.                                                             |
19 // |                                                                           |
20 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       |
21 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         |
22 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     |
23 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      |
24 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     |
25 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          |
26 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     |
27 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
28 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
29 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE     |
30 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.      |
31 // |                                                                           |
32 // +---------------------------------------------------------------------------+
33 // | Seagull 0.6                                                               |
34 // +---------------------------------------------------------------------------+
35 // | CategoryPerms.php                                                         |
36 // +---------------------------------------------------------------------------+
37 // | Author:   Demian Turner <demian@phpkitchen.com>                           |
38 // +---------------------------------------------------------------------------+
39 // $Id: Permissions.php,v 1.5 2005/02/03 11:29:01 demian Exp $
40
41 require_once SGL_CORE_DIR . '/Category.php';
42
43 /**
44  * Basic Permission object.
45  *
46  * @package SGL
47  * @author  Demian Turner <demian@phpkitchen.com>
48  * @version $Revision: 1.5 $
49  */
50 class SGL_CategoryPerms extends SGL_Category
51 {
52     var $sPerms = '';
53     var $aPerms = array();
54     var $catID  = 0;
55
56     function SGL_CategoryPerms($catID = -1)
57     {
58         SGL::logMessage(null, PEAR_LOG_DEBUG);
59         
60         if (isset($catID) && $catID >= 0) {
61             $this->init($catID);
62         } else {
63             SGL::raiseError('No category ID passed', SGL_ERROR_INVALIDARGS);
64         }
65         parent::SGL_Category();
66         $c = &SGL_Config::singleton();
67         $this->conf = $c->getAll();
68     }
69
70     function init($catID)
71     {
72         SGL::logMessage(null, PEAR_LOG_DEBUG);
73         $this->set('catID', $catID);
74     }
75
76     function update()
77     {
78         SGL::logMessage(null, PEAR_LOG_DEBUG);
79
80         //  only grab keys where perms = 0
81         //  outputs an 1D array of GIDs where viewing is NOT permitted
82         $aKeys = array_keys($this->aPerms, 0);
83
84         //  generate perms string from array
85         $this->sPerms = implode(',', $aKeys);
86
87         //  notice null is a mysql null which is not the same as a PHP null
88         $permsStr = count($aKeys) ? "perms = '$this->sPerms'" : 'perms = NULL';
89         $dbh = & SGL_DB::singleton();
90         $query = "
91                     UPDATE  " . $this->conf['table']['category'] . "
92                     SET     $permsStr
93                     WHERE   category_id = $this->catID
94                 ";
95         $result = $dbh->query($query);
96         
97         //  recurse to update children
98         if ($this->isBranch($this->catID)) {           
99             $childNodeArray = $this->getChildren($this->catID);
100             $this->_updateChildNodes($childNodeArray);
101         }
102     }
103
104     function _updateChildNodes($childNodeArray)
105     {
106         SGL::logMessage(null, PEAR_LOG_DEBUG);
107         $aKeys = array_keys($this->aPerms, 0);
108
109         //  generate perms string from array
110         $this->sPerms = implode(',', $aKeys);
111
112         //  notice null is a mysql null which is not comparable with a PHP null
113         $permsStr = count($aKeys) ? "perms = '$this->sPerms'" : 'perms = NULL';
114         $dbh = & SGL_DB::singleton();
115         for ($x=0; $x < count($childNodeArray); $x++) {
116             $query = "
117                         UPDATE  " . $this->conf['table']['category'] . "
118                         SET     $permsStr
119                         WHERE   category_id = {$childNodeArray[$x]['category_id']}
120                     ";
121             $result = $dbh->query($query);
122
123             //  recurse if child links detected
124             if ($this->isBranch($childNodeArray[$x]['category_id'])) {
125                 $childNodeArrayInner = $this->getChildren($childNodeArray[$x]['category_id']);
126                 $this->_updateChildNodes($childNodeArrayInner);
127             }
128         }
129     }
130
131     function set($attributeName, $attributeValue)
132     {
133         $this->$attributeName = $attributeValue;
134     }
135
136     function get($attribute)
137     {
138         return $this->$attribute;
139     }
140 }
141 ?>
Note: See TracBrowser for help on using the browser.