Made quite a few changes so that the code works in the server
[vanilla-miry.git] / ajax / switch.php
blob2a37027cfd761b921caf327cfdad9e45f412c74c
1 <?php
2 /*
3 * Copyright 2003 Mark O'Sullivan
4 * This file is part of Vanilla.
5 * Vanilla is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
6 * Vanilla is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
7 * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8 * The latest source code for Vanilla is available at www.lussumo.com
9 * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
11 * Description: File used by Dynamic Data Management object to handle any type of boolean switch
14 include('../appg/settings.php');
15 include('../appg/init_ajax.php');
17 $PostBackKey = ForceIncomingString('PostBackKey', '');
18 $ExtensionKey = ForceIncomingString('ExtensionKey', '');
19 if ($PostBackKey != '' && $PostBackKey == $Context->Session->GetCsrfValidationKey()) {
20 $Type = ForceIncomingString('Type', '');
21 if ($Type == 'Move') {
22 $Switch = ForceIncomingInt('Switch', 0);
23 } else {
24 $Switch = ForceIncomingBool('Switch', 0);
26 $DiscussionID = ForceIncomingInt('DiscussionID', 0);
27 $CommentID = ForceIncomingInt('CommentID', 0);
29 // Don't create unnecessary objects
30 if (in_array($Type, array('Active', 'Closed', 'Sticky', 'Sink', 'Move'))) {
31 $dm = $Context->ObjectFactory->NewContextObject($Context, 'DiscussionManager');
32 } elseif ($Type == 'Comment') {
33 $cm = $Context->ObjectFactory->NewContextObject($Context, 'CommentManager');
34 } else {
35 // This will allow the switch class to be used to add new custom user settings
36 $um = $Context->ObjectFactory->NewContextObject($Context, 'UserManager');
38 // Handle the switches
39 if ($Type == 'Bookmark' && $DiscussionID > 0) {
40 if ($Context->Session->UserID == 0) die();
41 if ($Switch) {
42 $um->AddBookmark($Context->Session->UserID, $DiscussionID);
43 } else {
44 $um->RemoveBookmark($Context->Session->UserID, $DiscussionID);
46 } elseif ($DiscussionID > 0 && (
47 ($Type == 'Active' && $Context->Session->User->Permission('PERMISSION_HIDE_DISCUSSIONS'))
48 || ($Type == 'Closed' && $Context->Session->User->Permission('PERMISSION_CLOSE_DISCUSSIONS'))
49 || ($Type == 'Sticky' && $Context->Session->User->Permission('PERMISSION_STICK_DISCUSSIONS'))
50 || ($Type == 'Sink' && $Context->Session->User->Permission('PERMISSION_SINK_DISCUSSIONS'))
51 )) {
52 $dm->SwitchDiscussionProperty($DiscussionID, $Type, $Switch);
53 } elseif ($Type == 'Move') {
54 $Discussion = $dm->GetDiscussionById($DiscussionID);
55 if ($Context->Session->User->Permission('PERMISSION_MOVE_ANY_DISCUSSIONS')
56 || $Discussion->AuthUserID == $Context->Session->UserID
57 ) {
58 $Result = $dm->MoveDiscussion($DiscussionID, $Switch);
60 } elseif ($Type == 'Comment' && $CommentID > 0 && $DiscussionID > 0 && $Context->Session->User->Permission('PERMISSION_HIDE_COMMENTS')) {
61 $cm->SwitchCommentProperty($CommentID, $DiscussionID, $Switch);
62 } elseif ($Type == 'SendNewApplicantNotifications') {
63 $um->SwitchUserProperty($Context->Session->UserID, $Type, $Switch);
64 } elseif ($Type != '') {
65 $um->SwitchUserPreference($Type, $Switch);
68 echo 'Complete';
69 } else {
70 echo $Context->GetDefinition('ErrPostBackKeyInvalid');
72 $Context->Unload();