objectManager: Fix lower-case letter in 'email Text'
[NewAppDB.git] / preferences.php
blob962939a838bdcc71ca22fa49f424cc14245f9351
1 <?php
2 /**
3 * User's role and preferences editor.
5 * Optional parameters:
6 * - iUserId, user identifier (when an administrator edits another user)
7 * - iLimit
8 * - sOrderBy
9 * - sUserPassword, new password
10 * - sUserPassword2, new password confirmation
11 * - sUserEmail, e-mail address
12 * - sUserRealname, user's real name
13 * - sWineRelease, user's Wine release
14 * - bIsAdmin, true if user is an administrator
16 * TODO:
17 * - document iLimit and sOrderBy
18 * - replace sOrderBy with iOrderBy and use constants for each accepted value
19 * - add a field to prefs_list to flag the user level for the pref
20 * - move and rename functions in their respective modules
23 // application environment
24 require("path.php");
25 require(BASE."include/incl.php");
26 require(BASE."include/form_edit.php");
29 // returns an array of TableRow instances
30 function build_prefs_list($oUser)
32 $aTableRows = array();
34 $hResult = query_parameters("SELECT * FROM prefs_list ORDER BY id");
35 while($hResult && $r = query_fetch_object($hResult))
37 // skip admin options
38 if(!$_SESSION['current']->hasPriv("admin"))
40 if($r->name == "query:mode")
41 continue;
42 if($r->name == "sidebar")
43 continue;
44 if($r->name == "window:query")
45 continue;
46 if($r->name == "query:hide_header")
47 continue;
48 if($r->name == "query:hide_sidebar")
49 continue;
50 if($r->name == "debug")
51 continue;
54 $input = html_select("pref_$r->name", explode('|', $r->value_list),
55 $oUser->getpref($r->name, $r->def_value));
57 $oTableRow = new TableRow();
58 $oTableCell = new TableCell("&nbsp; $r->description");
59 $oTableRow->AddCell($oTableCell);
60 $oTableCell = new TableCell($input);
61 $oTableRow->AddCell($oTableCell);
63 $aTableRows[] = $oTableRow;
66 return $aTableRows;
69 // returns an array of TableRow instances
70 function show_user_fields($oUser)
72 $aTableRows = array();
74 $sWineRelease = $oUser->sWineRelease;
75 if($oUser->hasPriv("admin"))
76 $sAdminChecked = 'checked="true"';
77 else
78 $sAdminChecked = "";
80 // Edit admin privilege
81 if($_SESSION['current']->hasPriv("admin"))
83 $oTableRow = new TableRow();
84 $oTableRow->AddTextCell("&nbsp; Administrator");
85 $oTableRow->AddTextCell("<input type=\"checkbox\"".
86 " name=\"bIsAdmin\" value=\"true\" ".
87 "$sAdminChecked>");
89 $aTableRows[] = $oTableRow;
93 $oTableRow = new TableRow();
94 $oTableRow->AddTextCell("&nbsp; Wine version");
96 $sBugzillaVersionList = make_bugzilla_version_list("sWineRelease",
97 $sWineRelease);
98 $oTableRow->AddCell(new TableCell($sBugzillaVersionList));
99 $aTableRows[] = $oTableRow;
101 // return the table rows
102 return $aTableRows;
106 if(!$_SESSION['current']->isLoggedIn())
107 util_show_error_page_and_exit("You must be logged in to edit preferences");
109 // we come from the administration to edit an user
110 if($_SESSION['current']->hasPriv("admin") &&
111 isset($aClean['iUserId']) &&
112 isset($aClean['iLimit']) &&
113 isset($aClean['sOrderBy']) &&
114 in_array($aClean['sOrderBy'],array("email","realname","created"))
117 $oUser = new User($aClean['iUserId']);
118 } else
120 $oUser = &$_SESSION['current'];
123 if(isset($aClean['sSubmit']) && $aClean['sSubmit'] == "Update")
125 while(list($sKey, $sValue) = each($aClean))
127 /* if a parameter lacks 'pref_' at its head it isn't a */
128 /* preference so skip over processing it */
129 if(!ereg("^pref_(.+)$", $sKey, $arr))
130 continue;
131 $oUser->setPref($arr[1], $sValue);
134 /* make sure the user enters the same password twice */
135 if ($aClean['sUserPassword'] == $aClean['sUserPassword2'])
137 $str_passwd = $aClean['sUserPassword'];
139 else if ($aClean['sUserPassword'])
141 addmsg("The Passwords you entered did not match.", "red");
144 /* update user data fields */
145 $oUser->sEmail = $aClean['sUserEmail'];
146 $oUser->sRealname = $aClean['sUserRealname'];
147 $oUser->sWineRelease = $aClean['sWineRelease'];
149 /* if the password was empty in both cases then skip updating the users password */
150 if($str_passwd != "")
152 if(!$oUser->update_password($str_passwd))
153 addmsg("Failed to update password", "red");
156 if ($oUser->update() == SUCCESS)
158 addmsg("Preferences Updated", "green");
159 // we were managing an user, let's go back to the admin after
160 // updating tha admin status
161 if($oUser->iUserId == $aClean['iUserId'] &&
162 $_SESSION['current']->hasPriv("admin"))
164 if($aClean['bIsAdmin'] == "true")
165 $oUser->addPriv("admin");
166 else
167 $oUser->delPriv("admin");
168 util_redirect_and_exit(BASE."admin/adminUsers.php?iUserId=".$oUser->iUserId.
169 "&amp;sSearch=".$aClean['sSearch']."&amp;iLimit=".$aClean['iLimit'].
170 "&amp;sOrderBy=".$aClean['sOrderBy']."&amp;sSubmit=true");
173 else
175 addmsg("There was a problem updating your user info", "red");
179 apidb_header("User Preferences");
181 echo "<div class='default_container'>\n";
183 echo "<form method=\"post\" action=\"preferences.php\">\n";
185 // if we manage another user we give the parameters to go back to the admin
186 if( isset($aClean['iUserId']) && $oUser->iUserId == $aClean['iUserId'])
188 echo "<input type=\"hidden\" name=\"iLimit\" value=\"".$aClean['iLimit']."\">\n";
189 echo "<input type=\"hidden\" name=\"sOrderBy\" value=\"".$aClean['sOrderBy']."\">\n";
190 echo "<input type=\"hidden\" name=\"sSearch\" value=\"".$aClean['sSearch']."\">\n";
191 echo "<input type=\"hidden\" name=\"iUserId\" value=\"".$aClean['iUserId']."\">\n";
194 echo html_frame_start("Preferences for ".$oUser->sRealname, "80%");
196 // build a table
197 $oTable = new Table();
198 $oTable->SetWidth("100%");
199 $oTable->SetAlign("left");
200 $oTable->SetCellSpacing(0);
201 $oTable->SetClass("box-body");
203 // retrieve the form editing rows
204 $aTableRows = GetEditAccountFormRows($oUser->sEmail, $oUser->sRealname);
205 foreach($aTableRows as $oTableRow)
206 $oTable->AddRow($oTableRow);
208 // retrieve the user fields
209 $aTableRows = show_user_fields($oUser);
210 foreach($aTableRows as $oTableRow)
211 $oTable->AddRow($oTableRow);
213 // if we don't manage another user
214 if( !isset($aClean['iUserId']) || $oUser->iUserId != $aClean['iUserId'])
216 $aTableRows = build_prefs_list($oUser);
217 foreach($aTableRows as $oTableRow)
219 $oTable->AddRow($oTableRow);
222 echo $oTable->GetString();
224 echo html_frame_end();
225 echo "<br> <div align=center> <input type=\"submit\" name='sSubmit' value=\"Update\"> </div> <br>\n";
226 echo "</form>\n";
228 echo "</div>\n";
230 apidb_footer();