AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / DAO / UserGroupsDAO.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14  * DAO for "user_groups" table
15  * @access      public
16  * @author      Cindy Qi Li
17  * @package     DAO
18  */
19
20 if (!defined('TR_INCLUDE_PATH')) exit;
21
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
23
24 class UserGroupsDAO extends DAO {
25
26         /**
27          * Create a new user group
28          * @access  public
29          * @param   title
30          *          description
31          * @return  user id, if successful
32          *          false and add error into global var $msg, if unsuccessful
33          * @author  Cindy Qi Li
34          */
35         public function Create($title, $description)
36         {
37                 global $addslashes, $msg;
38
39                 $missing_fields = array();
40
41                 /* email check */
42                 $title = $addslashes(trim($title));
43
44                 /* login name check */
45                 if ($title == '')
46                 {
47                         $missing_fields[] = _AT('title');
48                 }
49
50                 if ($missing_fields)
51                 {
52                         $missing_fields = implode(', ', $missing_fields);
53                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
54                 }
55
56                 if (!$msg->containsErrors())
57                 {
58                         /* insert into the db */
59                         $sql = "INSERT INTO ".TABLE_PREFIX."user_groups
60                                       (title,
61                                        description,
62                                        create_date
63                                        )
64                                VALUES ('".$title."',
65                                        '".$description."',
66                                        now()
67                                       )";
68
69                         if (!$this->execute($sql))
70                         {
71                                 $msg->addError('DB_NOT_UPDATED');
72                                 return false;
73                         }
74                         else
75                         {
76                                 return mysql_insert_id();
77                         }
78                 }
79                 else
80                 {
81                         return false;
82                 }
83         }
84
85         /**
86          * Update an existing user group
87          * @access  public
88          * @param   user_group_id
89          *          title
90          *          description
91          * @return  user id, if successful
92          *          false and add error into global var $msg, if unsuccessful
93          * @author  Cindy Qi Li
94          */
95         public function Update($user_group_id, $title, $description)
96         {
97                 global $addslashes, $msg;
98
99                 $missing_fields = array();
100
101                 /* email check */
102                 $title = $addslashes(trim($title));
103
104                 /* login name check */
105                 if ($title == '')
106                 {
107                         $missing_fields[] = _AT('title');
108                 }
109
110                 if ($missing_fields)
111                 {
112                         $missing_fields = implode(', ', $missing_fields);
113                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
114                 }
115
116                 if (!$msg->containsErrors())
117                 {
118                         /* insert into the db */
119                         $sql = "UPDATE ".TABLE_PREFIX."user_groups
120                                    SET title = '".$title."',
121                                        description = '".$description."',
122                                        last_update = now()
123                                  WHERE user_group_id = ".$user_group_id;
124
125                         return $this->execute($sql);
126                 }
127         }
128
129         /**
130          * Update an existing user group record
131          * @access  public
132          * @param   userGroupID: user group ID
133          *          fieldName: the name of the table field to update
134          *          fieldValue: the value to update
135          * @return  true if successful
136          *          error message array if failed; false if update db failed
137          * @author  Cindy Qi Li
138          */
139         public function UpdateField($userGroupID, $fieldName, $fieldValue)
140         {
141                 global $addslashes;
142
143                 // check if the required fields are filled
144                 if ($fieldName == 'title' && $fieldValue == '') return array(_AT('TR_ERROR_EMPTY_FIELD'));
145                 
146                 $sql = "UPDATE ".TABLE_PREFIX."user_groups 
147                            SET ".$fieldName."='".$addslashes($fieldValue)."'
148                          WHERE user_group_id = ".$userGroupID;
149                 
150                 return $this->execute($sql);
151         }
152         
153         /**
154          * delete user group by given user id
155          * @access  public
156          * @param   user group id
157          * @return  true / false
158          * @author  Cindy Qi Li
159          */
160         public function Delete($userGroupID)
161         {
162                 // delete user_group_privilege
163                 include_once(TR_INCLUDE_PATH.'classes/DAO/UserGroupPrivilegeDAO.class.php');
164                 
165                 $userGroupPrivilegeDAO = new UserGroupPrivilegeDAO();
166                 $userGroupPrivilegeDAO->DeleteByUserGroupID($userGroupID);
167                 
168                 // delete user_groups
169                 $sql = 'DELETE FROM '.TABLE_PREFIX.'user_groups WHERE user_group_id = '.$userGroupID;
170                 
171                 return $this->execute($sql);
172         }
173         
174         /**
175          * Return all user groups' information
176          * @access  public
177          * @param   none
178          * @return  user rows
179          * @author  Cindy Qi Li
180          */
181         public function getAll()
182         {
183                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'user_groups ORDER BY title';
184                 return $this->execute($sql);
185         }
186
187         /**
188          * Return user information by given user id
189          * @access  public
190          * @param   user group id
191          * @return  user row
192          * @author  Cindy Qi Li
193          */
194         public function getUserGroupByID($user_group_id)
195                 {
196                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'user_groups WHERE user_group_id='.$user_group_id;
197                 if ($rows = $this->execute($sql))
198                 {
199                         return $rows[0];
200                 }
201         }
202 }
203 ?>