1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8/** \file 9 * \brief Manage user assigned modules 10 */ 11 12//this script may only be included - so its better to die if called directly. 13if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) { 14 header("location: index.php"); 15 exit; 16} 17 18/** 19 * \brief Class to manage user assigned modules 20 * 21 * Useful only if the feature "A user can assign modules has been set" ($prefs['user_assigned_modules']) 22 * 23 * The first time, a user displays the page to assign modules(tiki-user_assigned_modules.php), 24 * the list of modules are copied from tiki_modules to tiki_user_assigned_modules 25 * This list is rebuilt if the user asks for a "restore default" 26 * 27 */ 28class UserModulesLib extends TikiLib 29{ 30 /** 31 * @param $moduleId 32 * @param $user 33 * 34 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result 35 */ 36 function unassign_user_module($moduleId, $user) 37 { 38 $query = "delete from `tiki_user_assigned_modules` where `moduleId`=? and `user`=?"; 39 return $this->query($query, [$moduleId, $user]); 40 } 41 42 /** 43 * @param $moduleId 44 * @param $user 45 * 46 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result 47 */ 48 function up_user_module($moduleId, $user) 49 { 50 $query = "update `tiki_user_assigned_modules` set `ord`=`ord`-1 where `moduleId`=? and `user`=?"; 51 return $this->query($query, [$moduleId, $user]); 52 } 53 54 /** 55 * @param $moduleId 56 * @param $user 57 * 58 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result 59 */ 60 function down_user_module($moduleId, $user) 61 { 62 $query = "update `tiki_user_assigned_modules` set `ord`=`ord`+1 where `moduleId`=? and `user`=?"; 63 return $this->query($query, [$moduleId, $user]); 64 } 65 66 /** 67 * @param $moduleId 68 * @param $user 69 * @param $position 70 * 71 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result 72 */ 73 function set_column_user_module($moduleId, $user, $position) 74 { 75 $query = "update `tiki_user_assigned_modules` set `position`=? where `moduleId`=? and `user`=?"; 76 return $this->query($query, [$position, $moduleId, $user]); 77 } 78 79 /** 80 * @param $moduleId 81 * @param $position 82 * @param $order 83 * @param $user 84 * 85 * @return TikiDb_Pdo_Result|TikiDb_Adodb_Result 86 */ 87 function assign_user_module($moduleId, $position, $order, $user) 88 { 89 $query = "select * from `tiki_modules` where `moduleId`=?"; 90 $result = $this->query($query, [$moduleId]); 91 $res = $result->fetchRow(); 92 $query = "delete from `tiki_user_assigned_modules` where `moduleId`=? and `user`=?"; 93 $this->query($query, [$moduleId,$user]); 94 $query = 'insert into `tiki_user_assigned_modules`(`moduleId`, `user`,`name`,`position`,`ord`,`type`) values(?,?,?,?,?,?)'; 95 $bindvars = [$moduleId, $user,$res['name'],$position,(int) $order,$res['type']]; 96 return $this->query($query, $bindvars); 97 } 98 99 function get_user_assigned_modules($user) 100 { 101 $query = "select * from `tiki_user_assigned_modules` where `user`=? order by `position` asc,`ord` asc"; 102 103 $result = $this->query($query, [$user]); 104 $ret = []; 105 106 while ($res = $result->fetchRow()) { 107 $ret[] = $res; 108 } 109 110 return $ret; 111 } 112 113 function get_user_assigned_modules_pos($user, $pos) 114 { 115 $query = "select * from `tiki_user_assigned_modules` where `user`=? and `position`=? order by `ord` asc"; 116 117 $result = $this->query($query, [$user, $pos]); 118 $ret = []; 119 120 while ($res = $result->fetchRow()) { 121 $ret[] = $res; 122 } 123 124 return $ret; 125 } 126 127 function get_assigned_modules_user($user, $position) 128 { 129 $query = "select `umod`.`name`, `umod`.`position`, `umod`.`ord`, `umod`.`type`, 130 `mod`.`title`, `mod`.`cache_time`, `mod`.`rows`, `mod`.`params`, 131 `mod`.`groups`, `umod`.`user`, `mod`.`moduleId` 132 from `tiki_user_assigned_modules` `umod`, `tiki_modules` `mod` 133 where `umod`.`moduleId`=`mod`.`moduleId` and `umod`.`user`=? and `umod`.`position`=? order by `umod`.`ord` asc"; 134 135 $result = $this->query($query, [$user, $position]); 136 $ret = []; 137 138 while ($res = $result->fetchRow()) { 139 $ret[] = $res; 140 } 141 142 return $ret; 143 } 144 145 function user_has_assigned_modules($user) 146 { 147 $query = "select count(`moduleId`) from `tiki_user_assigned_modules` where `user`=?"; 148 149 $result = $this->getOne($query, [$user]); 150 return $result; 151 } 152 153 // Creates user assigned modules copying from tiki_modules 154 155 /** 156 * @param $user 157 * 158 * @return bool|TikiDb_Pdo_Result|TikiDb_Adodb_Result 159 */ 160 function create_user_assigned_modules($user) 161 { 162 $query = "delete from `tiki_user_assigned_modules` where `user`=?"; 163 164 $this->query($query, [$user]); 165 global $prefs; 166 $query = "select * from `tiki_modules`"; 167 $result = $this->query($query, []); 168 $user_groups = $this->get_user_groups($user); 169 170 while ($res = $result->fetchRow()) { 171 $mod_ok = 0; 172 if ($res['type'] != "h") { 173 if ($res["groups"] && $prefs['modallgroups'] != 'y') { 174 $groups = unserialize($res["groups"]); 175 176 $ins = array_intersect($groups, $user_groups); 177 178 if (count($ins) > 0) { 179 $mod_ok = 1; 180 } 181 } else { 182 $mod_ok = 1; 183 } 184 } 185 186 if ($mod_ok) { 187 $query = "delete from `tiki_user_assigned_modules` where `moduleId`=? and `user`=?"; 188 $this->query($query, [$res['moduleId'],$user]); 189 190 $query = "insert into `tiki_user_assigned_modules` 191 (`moduleId`, `user`,`name`,`position`,`ord`,`type`) values(?,?,?,?,?,?)"; 192 $bindvars = [$res['moduleId'], $user,$res['name'],$res['position'],$res['ord'],$res['type']]; 193 $result2 = $this->query($query, $bindvars); 194 } 195 } 196 return isset($result2) ? $result2 : false; 197 } 198 // Return the list of modules that can be assigned by the user 199 function get_user_assignable_modules($user) 200 { 201 global $prefs; 202 $userlib = TikiLib::lib('user'); 203 204 $query = "select * from `tiki_modules`"; 205 $result = $this->query($query, []); 206 $ret = []; 207 $user_groups = $this->get_user_groups($user); 208 209 while ($res = $result->fetchRow()) { 210 $mod_ok = 0; 211 212 // The module must not be assigned 213 $isas = $this->getOne("select count(*) from `tiki_user_assigned_modules` where `moduleId`=? and `user`=?", [$res['moduleId'],$user]); 214 215 if (! $isas) { 216 if ($res["groups"] && $prefs['modallgroups'] != 'y' && (! $userlib->user_has_permission($user, 'tiki_p_admin'))) { 217 $groups = unserialize($res["groups"]); 218 219 $ins = array_intersect($groups, $user_groups); 220 221 if (count($ins) > 0) { 222 $mod_ok = 1; 223 } 224 } else { 225 $mod_ok = 1; 226 } 227 228 if ($mod_ok) { 229 $ret[] = $res; 230 } 231 } 232 } 233 234 return $ret; 235 } 236 237 /** 238 * Swap current module and above one 239 * 240 * @param $moduleId 241 * @param $user 242 * 243 * @return bool|TikiDb_Adodb_Result|TikiDb_Pdo_Result 244 */ 245 function swap_up_user_module($moduleId, $user) 246 { 247 return $this->swap_adjacent($moduleId, $user, '<'); 248 } 249 250 /** 251 * Swap current module and below one 252 * 253 * @param $moduleId 254 * @param $user 255 * 256 * @return bool|TikiDb_Adodb_Result|TikiDb_Pdo_Result 257 */ 258 function swap_down_user_module($moduleId, $user) 259 { 260 return $this->swap_adjacent($moduleId, $user, '>'); 261 } 262 263 /** 264 * Swap (up/down) two adjacent modules 265 * 266 * @param $moduleId 267 * @param $user 268 * @param $op 269 * 270 * @return bool|TikiDb_Pdo_Result|TikiDb_Adodb_Result 271 */ 272 function swap_adjacent($moduleId, $user, $op) 273 { 274 // Get position and order of module to swap 275 $query = "select `ord`,`position` from `tiki_user_assigned_modules` where `moduleId`=? and user=?"; 276 $r = $this->query($query, [$moduleId, $user]); 277 $cur = $r->fetchRow(); 278 // Get name and order of module to swap with 279 $query = "select `moduleId`, `name`,`ord` from `tiki_user_assigned_modules` where `position`=? and `ord`" . $op . "=? and `user`=? and `moduleId` != ? order by `ord` " . ($op == '<' ? 'desc' : ''); 280 $r = $this->query($query, [$cur['position'], $cur['ord'], $user, $moduleId]); 281 $swap = $r->fetchRow(); 282 if (! empty($swap)) { 283 // Swap 2 adjacent modules 284 if ($swap['ord'] == $cur['ord']) { 285 $swap['ord'] += ($op == '<') ? -1 : +1; 286 } 287 $query = "update `tiki_user_assigned_modules` set `ord`=? where `moduleId`=? and `user`=?"; 288 $this->query($query, [$swap['ord'], $moduleId, $user]); 289 $query = "update `tiki_user_assigned_modules` set `ord`=? where `moduleId`=? and `user`=?"; 290 return $this->query($query, [$cur['ord'], $swap['moduleId'], $user]); 291 } else { 292 return false; 293 } 294 } 295 296 /** 297 * Toggle module position 298 * 299 * @param $moduleId 300 * @param $user 301 * 302 * @return TikiDb_Adodb_Result|TikiDb_Pdo_Result 303 */ 304 function move_module($moduleId, $user) 305 { 306 // Get current position 307 $query = "select `position` from `tiki_user_assigned_modules` where `moduleId`=? and `user`=?"; 308 $r = $this->query($query, [$moduleId, $user]); 309 $res = $r->fetchRow(); 310 return $this->set_column_user_module($moduleId, $user, ($res['position'] == 'right' ? 'left' : 'right')); 311 } 312 /// Add a module to all the user who have assigned module and who don't have already this module 313 function add_module_users($moduleId, $name, $title, $position, $order, $cache_time, $rows, $groups, $params, $type) 314 { 315 // for the user who already has this module, update only the type 316 $this->query('update `tiki_user_assigned_modules` set `type`=? where `moduleId`=?', [$type,$name]); 317 // for the user who doesn't have this module 318 $query = "select distinct t1.`user` from `tiki_user_assigned_modules` as t1 left join `tiki_user_assigned_modules` as t2 on t1.`user`=t2.`user` and t2.`moduleId`=? where t2.`moduleId` is null"; 319 $result = $this->query($query, [$moduleId]); 320 while ($res = $result->fetchRow()) { 321 $user = $res["user"]; 322 $query = "insert into `tiki_user_assigned_modules`(`moduleId`, `user`,`name`,`position`,`ord`,`type`) 323 values(?,?,?,?,?,?)"; 324 $this->query($query, [$moduleId, $user,$name,$position,(int) $order,$type]); 325 } 326 } 327} 328