| 1 | <?php |
|---|
| 2 | // imports |
|---|
| 3 | require_once(WCF_DIR.'lib/action/AbstractAction.class.php'); |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * handles box actions (asynchronous calls) |
|---|
| 7 | * |
|---|
| 8 | * @author Torben Brodt |
|---|
| 9 | * @package de.easy-coding.wcf.buddyloo |
|---|
| 10 | * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> |
|---|
| 11 | */ |
|---|
| 12 | class BuddylooSocialAction extends AbstractAction { |
|---|
| 13 | public $columns = array(); |
|---|
| 14 | public $userID = 0; |
|---|
| 15 | public $socialID = 0; |
|---|
| 16 | public $gadgetID = 0; |
|---|
| 17 | public $boxID = 0; |
|---|
| 18 | public $visible = -1; |
|---|
| 19 | public $handle = ""; |
|---|
| 20 | public $socialAccess = 0; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * |
|---|
| 24 | */ |
|---|
| 25 | public function getPermission() { |
|---|
| 26 | return WCF::getUser()->userID == $this->userID; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * @see Action::readParameters() |
|---|
| 31 | */ |
|---|
| 32 | public function readParameters() { |
|---|
| 33 | parent::readParameters(); |
|---|
| 34 | |
|---|
| 35 | if (isset($_REQUEST['gadgetID'])) $this->gadgetID = intval($_REQUEST['gadgetID']); |
|---|
| 36 | if (isset($_REQUEST['socialID'])) $this->socialID = intval($_REQUEST['socialID']); |
|---|
| 37 | if (isset($_REQUEST['userID'])) $this->userID = intval($_REQUEST['userID']); |
|---|
| 38 | if (isset($_REQUEST['handle'])) $this->handle = $_REQUEST['handle']; |
|---|
| 39 | |
|---|
| 40 | if (isset($_REQUEST['socialTitle'])) $this->socialTitle = $_REQUEST['socialTitle']; |
|---|
| 41 | if (isset($_REQUEST['socialAccess'])) $this->socialAccess = intval($_REQUEST['socialAccess']); |
|---|
| 42 | |
|---|
| 43 | // remove or hide |
|---|
| 44 | if (isset($_REQUEST['boxID'])) { |
|---|
| 45 | if(preg_match('/^box(\d+)$/', $_REQUEST['boxID'], $res)) { |
|---|
| 46 | $this->boxID = intval($res[1]); |
|---|
| 47 | } |
|---|
| 48 | if (isset($_REQUEST['visible'])) $this->visible = intval($_REQUEST['visible']); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | foreach($_POST as $key => $col) { |
|---|
| 52 | if(preg_match('/^sort(\d+)$/', $key, $res)) { |
|---|
| 53 | $column = $res[1]; |
|---|
| 54 | foreach($col as $gadget) { |
|---|
| 55 | if(preg_match('/^box(\d+)$/', $gadget, $res)) { |
|---|
| 56 | $this->columns[$column][] = $res[1]; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * @see Action::execute() |
|---|
| 65 | */ |
|---|
| 66 | public function execute() { |
|---|
| 67 | // call execute event |
|---|
| 68 | parent::execute(); |
|---|
| 69 | |
|---|
| 70 | // check global permission |
|---|
| 71 | if (!WCF::getUser()->getPermission('user.profile.canSocial') || !$this->getPermission()) { |
|---|
| 72 | require_once(WCF_DIR.'lib/system/exception/PermissionDeniedException.class.php'); |
|---|
| 73 | throw new PermissionDeniedException(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | // check users settings |
|---|
| 77 | //TODO |
|---|
| 78 | |
|---|
| 79 | switch($this->handle) { |
|---|
| 80 | case 'addSocial': |
|---|
| 81 | $sql = 'INSERT INTO wcf'.WCF_N.'_buddyloo_social |
|---|
| 82 | (socialTitle) |
|---|
| 83 | VALUES ("'.WCF::getLanguage()->get('wcf.user.profile.menu.link.social').'"); '; |
|---|
| 84 | WCF::getDB()->sendQuery($sql); |
|---|
| 85 | |
|---|
| 86 | // overwrite return address |
|---|
| 87 | $this->socialID = WCF::getDB()->getInsertID(); |
|---|
| 88 | |
|---|
| 89 | $sql = 'INSERT INTO wcf'.WCF_N.'_buddyloo_user_to_social |
|---|
| 90 | (userID, socialID, icolumn) |
|---|
| 91 | ( |
|---|
| 92 | SELECT '.$this->userID.' AS userID, |
|---|
| 93 | '.$this->socialID.' AS socialID, |
|---|
| 94 | COUNT(*)+1 AS icolumn |
|---|
| 95 | FROM wcf'.WCF_N.'_buddyloo_user_to_social |
|---|
| 96 | WHERE userID='.$this->userID.' |
|---|
| 97 | );'; |
|---|
| 98 | WCF::getDB()->sendQuery($sql); |
|---|
| 99 | |
|---|
| 100 | // forward to new social page |
|---|
| 101 | header('Location: index.php?page=Social&userID='.$this->userID.'&socialID='.$this->socialID.SID_ARG_2ND_NOT_ENCODED); |
|---|
| 102 | exit; |
|---|
| 103 | break; |
|---|
| 104 | case 'updateSocial': |
|---|
| 105 | $sql = "UPDATE wcf".WCF_N."_buddyloo_social |
|---|
| 106 | SET socialTitle = '".escapeString($this->socialTitle)."' |
|---|
| 107 | WHERE socialID = ".$this->socialID; |
|---|
| 108 | WCF::getDB()->sendQuery($sql); |
|---|
| 109 | break; |
|---|
| 110 | case 'lockSocial': |
|---|
| 111 | $sql = "UPDATE wcf".WCF_N."_buddyloo_social |
|---|
| 112 | SET socialAccess = ".$this->socialAccess." |
|---|
| 113 | WHERE socialID = ".$this->socialID; |
|---|
| 114 | echo $sql; |
|---|
| 115 | WCF::getDB()->sendQuery($sql); |
|---|
| 116 | break; |
|---|
| 117 | case 'deleteSocial'; |
|---|
| 118 | $sql = 'DELETE FROM wcf'.WCF_N.'_buddyloo_social |
|---|
| 119 | WHERE socialID = '.$this->socialID; |
|---|
| 120 | WCF::getDB()->sendQuery($sql); |
|---|
| 121 | |
|---|
| 122 | $sql = 'DELETE FROM wcf'.WCF_N.'_buddyloo_gadget_to_social |
|---|
| 123 | WHERE socialID = '.$this->socialID; |
|---|
| 124 | WCF::getDB()->sendQuery($sql); |
|---|
| 125 | |
|---|
| 126 | // finds the next best fitting socialpage to switch there |
|---|
| 127 | $sql = 'SELECT socialID, |
|---|
| 128 | IF(icolumn>icolumnOld,icolumn-icolumnOld,icolumnOld-icolumn) AS diff |
|---|
| 129 | FROM wcf'.WCF_N.'_buddyloo_user_to_social |
|---|
| 130 | JOIN ( |
|---|
| 131 | SELECT icolumn AS icolumnOld |
|---|
| 132 | FROM wcf'.WCF_N.'_buddyloo_user_to_social |
|---|
| 133 | WHERE socialID ='.$this->socialID.' |
|---|
| 134 | ) X |
|---|
| 135 | WHERE userID='.$this->userID.' |
|---|
| 136 | ORDER BY diff ASC, |
|---|
| 137 | icolumn DESC |
|---|
| 138 | LIMIT 1 |
|---|
| 139 | OFFSET 1'; |
|---|
| 140 | $row = WCF::getDB()->getFirstRow($sql); |
|---|
| 141 | echo $row ? $row['socialID'] : 0; |
|---|
| 142 | |
|---|
| 143 | $sql = 'DELETE FROM wcf'.WCF_N.'_buddyloo_user_to_social |
|---|
| 144 | WHERE socialID = '.$this->socialID; |
|---|
| 145 | WCF::getDB()->sendQuery($sql); |
|---|
| 146 | |
|---|
| 147 | $sql = 'DELETE FROM wcf'.WCF_N.'_buddyloo_gadget_variables_value |
|---|
| 148 | WHERE socialID = '.$this->socialID; |
|---|
| 149 | WCF::getDB()->sendQuery($sql); |
|---|
| 150 | |
|---|
| 151 | // finds the first socialpage to set as new default |
|---|
| 152 | $sql = 'SELECT IF(ISNULL(MIN(socialID)),0,MIN(socialID)) AS socialID |
|---|
| 153 | FROM wcf'.WCF_N.'_buddyloo_user_to_social |
|---|
| 154 | WHERE userID = '.$this->userID; |
|---|
| 155 | $row = WCF::getDB()->getFirstRow($sql); |
|---|
| 156 | $this->socialID = $row['socialID']; |
|---|
| 157 | |
|---|
| 158 | require_once(WCF_DIR.'lib/data/user/UserEditor.class.php'); |
|---|
| 159 | $user = new UserEditor($this->userID); |
|---|
| 160 | $user->updateFields(array( |
|---|
| 161 | 'socialID'=>$this->socialID |
|---|
| 162 | )); |
|---|
| 163 | break; |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | case 'addGadget': |
|---|
| 167 | if($this->gadgetID == 7 && !WCF::getUser()->getPermission('user.profile.canCustomGadget')) return; |
|---|
| 168 | $visible = true; |
|---|
| 169 | |
|---|
| 170 | // update old gadgets |
|---|
| 171 | $sql = 'UPDATE wcf'.WCF_N.'_buddyloo_gadget_to_social |
|---|
| 172 | SET irow = irow + 1 |
|---|
| 173 | WHERE socialID = '.$this->socialID.' |
|---|
| 174 | AND icolumn = 1'; |
|---|
| 175 | WCF::getDB()->sendQuery($sql); |
|---|
| 176 | |
|---|
| 177 | // insert new gadget at very first position |
|---|
| 178 | $sql = 'INSERT INTO wcf'.WCF_N.'_buddyloo_gadget_to_social |
|---|
| 179 | (socialID,gadgetID,icolumn,irow,visible) |
|---|
| 180 | VALUES ( |
|---|
| 181 | '.$this->socialID.', |
|---|
| 182 | '.$this->gadgetID.', |
|---|
| 183 | 1, |
|---|
| 184 | 1, |
|---|
| 185 | 1 |
|---|
| 186 | ); '; |
|---|
| 187 | WCF::getDB()->sendQuery($sql); |
|---|
| 188 | |
|---|
| 189 | // go back to gadget add page |
|---|
| 190 | header('Location: index.php?page=GadgetList&userID='.$this->userID.'&socialID='.$this->socialID.SID_ARG_2ND_NOT_ENCODED); |
|---|
| 191 | exit; |
|---|
| 192 | break; |
|---|
| 193 | case 'deleteGadget'; |
|---|
| 194 | $sql = 'DELETE FROM wcf'.WCF_N.'_buddyloo_gadget_to_social |
|---|
| 195 | WHERE boxID = '.$this->boxID; |
|---|
| 196 | WCF::getDB()->sendQuery($sql); |
|---|
| 197 | break; |
|---|
| 198 | case 'toggleGadget': |
|---|
| 199 | $sql = 'UPDATE wcf'.WCF_N.'_buddyloo_gadget_to_social |
|---|
| 200 | SET visible = '.$this->visible.' |
|---|
| 201 | WHERE boxID = '.$this->boxID; |
|---|
| 202 | WCF::getDB()->sendQuery($sql); |
|---|
| 203 | break; |
|---|
| 204 | case 'moveGadget': |
|---|
| 205 | foreach($this->columns as $key => $col) { |
|---|
| 206 | $j=1; |
|---|
| 207 | foreach($col as $boxID) { |
|---|
| 208 | $sql = 'UPDATE wcf'.WCF_N.'_buddyloo_gadget_to_social |
|---|
| 209 | SET |
|---|
| 210 | socialID=%d, |
|---|
| 211 | icolumn=%d, |
|---|
| 212 | irow=%d |
|---|
| 213 | WHERE boxID=%d'; |
|---|
| 214 | $sql = sprintf($sql, $this->socialID, $key, $j++, $boxID); |
|---|
| 215 | WCF::getDB()->sendQuery($sql); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | break; |
|---|
| 219 | case 'configGadget': |
|---|
| 220 | case 'configGadgetSave': |
|---|
| 221 | $sql = 'SELECT gs.gadgetID, |
|---|
| 222 | v.variableID, |
|---|
| 223 | v.variableName, |
|---|
| 224 | v.variableType, |
|---|
| 225 | IF(ISNULL(u.userID), v.variableDefault, u.variableValue) AS variableValue |
|---|
| 226 | FROM wcf'.WCF_N.'_buddyloo_gadget_to_social gs |
|---|
| 227 | |
|---|
| 228 | INNER JOIN wcf'.WCF_N.'_buddyloo_gadget_variables v |
|---|
| 229 | ON gs.boxID = '.$this->boxID.' |
|---|
| 230 | AND gs.gadgetID = v.gadgetID |
|---|
| 231 | |
|---|
| 232 | LEFT JOIN wcf'.WCF_N.'_buddyloo_gadget_variables_value u |
|---|
| 233 | ON u.userID = '.$this->userID.' |
|---|
| 234 | AND v.variableID = u.variableID |
|---|
| 235 | |
|---|
| 236 | ORDER BY v.variableName ASC'; |
|---|
| 237 | |
|---|
| 238 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 239 | $return = ''; |
|---|
| 240 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 241 | // TODO build classes for every option text,textarea,radio,checkbox,select,multiselect |
|---|
| 242 | // should allow validation, ... |
|---|
| 243 | $row['variableName'] = WCF::getLanguage()->get('wcf.buddyloo.variables.'.$row['gadgetID'].'.'.$row['variableName']); |
|---|
| 244 | switch($this->handle) { |
|---|
| 245 | case 'configGadgetSave': // save |
|---|
| 246 | if($row['variableType'] == 'text') { |
|---|
| 247 | $val = $_POST['buddyloobox'][$this->boxID][$row['variableID']]; |
|---|
| 248 | if(preg_match('/[^\(\)\{\}]+/', $val)) { |
|---|
| 249 | $sql = 'REPLACE INTO wcf'.WCF_N.'_buddyloo_gadget_variables_value |
|---|
| 250 | (variableID, boxID, userID, variableValue) |
|---|
| 251 | VALUES ('.$row['variableID'].', '.$this->boxID.', '.$this->userID.', "'.$val.'")'; |
|---|
| 252 | WCF::getDB()->sendQuery($sql); |
|---|
| 253 | |
|---|
| 254 | //TODO translation.. mark as red |
|---|
| 255 | echo 'alert("saved value for '.$row['variableName'].'");'; |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | break; |
|---|
| 259 | case 'configGadget': |
|---|
| 260 | //TODO build classes for every option text,textarea,radio,checkbox,select,multiselect |
|---|
| 261 | if($row['variableType'] == 'text') { |
|---|
| 262 | $return .= sprintf('%s: <input type="text" name="buddyloobox[%d][%d]" value="%s" /><br/>', |
|---|
| 263 | $row['variableName'], $this->boxID, $row['variableID'], $row['variableValue']); |
|---|
| 264 | } |
|---|
| 265 | break; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | echo $return; |
|---|
| 269 | break; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | // call executed event |
|---|
| 273 | $this->executed(); |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | ?> |
|---|