rsys.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. The class-library with all reservation-system specific functionality
  5. */
  6. class Rsys {
  7. /**
  8. * Get required database-vars from inc/lib/database.lib.php and load them into the $GLOBALS['_rsys']-array
  9. *
  10. */
  11. function init() {
  12. // reservation database tables
  13. $GLOBALS['_rsys']['dbtables']['item'] = Database :: get_main_table(TABLE_MAIN_RESERVATION_ITEM);
  14. $GLOBALS['_rsys']['dbtables']['reservation'] = Database :: get_main_table(TABLE_MAIN_RESERVATION_RESERVATION);
  15. $GLOBALS['_rsys']['dbtables']['subscription'] = Database :: get_main_table(TABLE_MAIN_RESERVATION_SUBSCRIBTION);
  16. $GLOBALS['_rsys']['dbtables']['category'] = Database :: get_main_table(TABLE_MAIN_RESERVATION_CATEGORY);
  17. $GLOBALS['_rsys']['dbtables']['item_rights'] = Database :: get_main_table(TABLE_MAIN_RESERVATION_ITEM_RIGHTS);
  18. }
  19. /**
  20. * Get the full tag for a reservation specific database table
  21. *
  22. * @param - String $table The table-name
  23. */
  24. function getTable($table) {
  25. return $GLOBALS['_rsys']['dbtables'][$table];
  26. }
  27. /**
  28. * Get number of subscriptions of a reservationperiod
  29. *
  30. * @return - int The amount of subscriptions
  31. */
  32. function get_num_subscriptions_reservationperiods($res_id) {
  33. $sql = "SELECT COUNT(*) FROM ".Rsys :: getTable("subscription")." s
  34. WHERE s.reservation_id = '".Database::escape_string($res_id)."'";
  35. return @ Database::result(Database::query($sql), 0, 0);
  36. }
  37. /**
  38. * Validates the access to a certain reservation-script
  39. *
  40. * @param - String $section The section (= script-file)
  41. * @param - int $id An id (sometimes this is required to get rights for a unique row in the database)
  42. */
  43. function protect_script($section, $id = null) {
  44. $uid = api_get_user_id();
  45. switch ($section) {
  46. case 'm_item' :
  47. if (!api_is_platform_admin() && Rsys :: check_user_status() <> 1)
  48. api_protect_admin_script();
  49. break;
  50. case 'm_category' :
  51. api_protect_admin_script();
  52. break;
  53. case 'm_reservation' :
  54. if (!api_is_platform_admin() && Rsys :: check_user_status() <> 1)
  55. api_protect_admin_script();
  56. break;
  57. }
  58. }
  59. /**
  60. * Formats a message with a goto-link
  61. *
  62. * @param - String $msg The message
  63. * @param - String $page The page-script
  64. * @param - String $pageheader The tag to display as link
  65. */
  66. function get_return_msg($msg, $page, $pageheader) {
  67. $target_url = api_get_path(WEB_PATH).'main/reservation/'.$page;
  68. $return = get_lang('GoTo');
  69. return $msg."<br /><br /><a href=\"$target_url\">$return $pageheader</a>";
  70. }
  71. /**
  72. * Formats a message with a goto-link
  73. *
  74. * @param - String $msg The message
  75. * @param - String $page The page-script
  76. * @param - String $pageheader The tag to display as link
  77. */
  78. function get_return_msg2($msg, $page, $pageheader) {
  79. $return = get_lang('GoTo');
  80. return $msg."<br /><br /><a href=\"$page\">$return $pageheader</a>";
  81. }
  82. /**
  83. * Returns a timestamp from a mysql DATETIME
  84. *
  85. * @param - String $dt DATETIME (0000-00-00 00:00:00)
  86. * @return - int timestamp
  87. */
  88. function mysql_datetime_to_timestamp($dt) {
  89. $yr = strval(substr($dt, 0, 4));
  90. $mo = strval(substr($dt, 5, 2));
  91. $da = strval(substr($dt, 8, 2));
  92. $hr = strval(substr($dt, 11, 2));
  93. $mi = strval(substr($dt, 14, 2));
  94. $se = strval(substr($dt, 17, 2));
  95. return mktime($hr, $mi, $se, $mo, $da, $yr);
  96. }
  97. function mysql_datetime_to_array($dt) {
  98. $offset = strpos($dt, '-');
  99. $dat['year'] = strval(substr($dt, 0, $offset));
  100. $dat['month'] = strval(substr($dt, $offset +1, strpos($dt, '-', $offset +1) - ($offset +1)));
  101. $offset = strpos($dt, '-', $offset +1);
  102. $dat['day'] = strval(substr($dt, $offset +1, strpos($dt, ' ', $offset +1) - ($offset +1)));
  103. $offset = strpos($dt, ' ', $offset +1);
  104. $dat['hour'] = strval(substr($dt, $offset +1, strpos($dt, ':', $offset +1) - ($offset +1)));
  105. $offset = strpos($dt, ':', $offset +1);
  106. $dat['minute'] = strval(substr($dt, $offset +1, strpos($dt, ':', $offset +1) - ($offset +1)));
  107. $offset = strpos($dt, ':', $offset +1);
  108. $dat['second'] = strval(substr($dt, $offset +1, strlen($dt) - $offset +1));
  109. return $dat;
  110. }
  111. function timestamp_to_datetime($timestamp) {
  112. return date('Y-m-d H:i:s', $timestamp);
  113. }
  114. function check_user_status() {
  115. $user_info = api_get_user_info(api_get_user_id());
  116. return $user_info['status'];
  117. }
  118. /*
  119. CATEGORIES
  120. */
  121. /**
  122. * Adds a category
  123. *
  124. * @param - String $name The name
  125. * @return - int The id
  126. */
  127. function add_category($naam) {
  128. if (Rsys :: check_category($naam)) {
  129. $sql = "INSERT INTO ".Rsys :: getTable("category")." (name) VALUES ('".Database::escape_string($naam)."')";
  130. Database::query($sql);
  131. return Database::insert_id();
  132. }
  133. return false;
  134. }
  135. /**
  136. * Controls if the category already exists
  137. *
  138. * @param - String $name The name
  139. * @return - boolean True or False
  140. */
  141. function check_category($name, $id=0) {
  142. $sql = "SELECT name FROM ".Rsys :: getTable("category")." WHERE LCASE(name)='".strtolower(Database::escape_string($name))."' AND id<>".Database::escape_string($id)."";
  143. $Result = Database::query($sql);
  144. return (Database::num_rows($Result) == 0);
  145. }
  146. /**
  147. * Edits a category
  148. *
  149. * @param - String $name The name
  150. * @param - int $id The id
  151. */
  152. function edit_category($id, $name) {
  153. if (Rsys :: check_category($name, $id)) {
  154. $sql = "UPDATE ".Rsys :: getTable("category")." SET name = '".Database::escape_string($name)."' WHERE id =".Database::escape_string($id)."";
  155. Database::query($sql);
  156. return $id;
  157. }
  158. return false;
  159. }
  160. /**
  161. * Deletes a category
  162. *
  163. * @param - int $id The id
  164. */
  165. function delete_category($id) {
  166. $sql = "SELECT id FROM ".Rsys :: getTable("item")." WHERE category_id=".Database::escape_string($id)."";
  167. $result = Database::query($sql);
  168. if (Database::num_rows($result) == 0) {
  169. $sql2 = "DELETE FROM ".Rsys :: getTable("category")." WHERE id =".Database::escape_string($id)."";
  170. Database::query($sql2);
  171. return 0;
  172. } else {
  173. return Database::num_rows($result);
  174. }
  175. }
  176. /**
  177. * Gets a category from database (give no param to get ALL categories)
  178. *
  179. * @param - int $id The id of the category
  180. * @param - String $orderby (sql) ORDER BY $orderby
  181. * @return - Array One or all rows of the category-table
  182. */
  183. function get_category($id = null, $orderby = "name ASC") {
  184. $sql = "SELECT * FROM ".Rsys :: getTable("category");
  185. if (!empty ($id))
  186. $sql .= " WHERE id = ".Database::escape_string($id)."";
  187. else
  188. $sql .= " ORDER BY ".$orderby;
  189. $arr = Database::store_result(Database::query($sql));
  190. if (!empty ($id))
  191. return $arr[0];
  192. else
  193. return $arr;
  194. }
  195. /**
  196. * Gets all categories that have items in them (for the current user)
  197. * @param - String $orderby (sql) ORDER BY $orderby
  198. * @return - Array All rows of the category-table that have items
  199. */
  200. function get_category_with_items($orderby = "c.name ASC") {
  201. $sql = "SELECT c.* FROM ".Rsys :: getTable("category")." c
  202. INNER JOIN ".Rsys :: getTable("item")." i ON i.category_id =c.id
  203. LEFT JOIN ".Rsys :: getTable("item_rights")." ir ON ir.item_id=i.id
  204. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." cl ON ir.class_id=cl.id AND ir.item_id = i.id
  205. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = cl.id
  206. WHERE (cu.user_id='".api_get_user_id()."' AND ir.view_right=1) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0)."
  207. GROUP BY c.id ORDER BY ".$orderby;
  208. $arr = Database::store_result(Database::query($sql));
  209. return $arr;
  210. }
  211. /**
  212. * Gets all categories that have items in them (for the current user)
  213. * @param - String $orderby (sql) ORDER BY $orderby
  214. * @return - Array All rows of the category-table that have items
  215. */
  216. function get_category_with_items_manager($orderby = "c.name ASC") {
  217. $sql = "SELECT c.* FROM ".Rsys :: getTable("category")." c
  218. INNER JOIN ".Rsys :: getTable("item")." i ON i.category_id =c.id
  219. LEFT JOIN ".Rsys :: getTable("item_rights")." ir ON ir.item_id=i.id
  220. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." cl ON ir.class_id=cl.id AND ir.item_id = i.id
  221. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = cl.id
  222. WHERE (cu.user_id='".api_get_user_id()."' AND (ir.edit_right=1 OR ir.delete_right=1)) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0)."
  223. GROUP BY c.id ORDER BY ".$orderby;
  224. $arr = Database::store_result(Database::query($sql));
  225. return $arr;
  226. }
  227. /**
  228. * Returns categories for a sortable table based on the params
  229. *
  230. * @param - int $from Index of the first item to return.
  231. * @param - int $per_page The number of items to return
  232. * @param - int $column The number of the column on which the data should be sorted
  233. * @param - String $direction In which order should the data be sorted (ASC or DESC)
  234. */
  235. function get_table_categories($from, $per_page, $column, $direction) {
  236. $sql = "SELECT id AS col0, name as col1, id AS col2 FROM ".Rsys :: getTable("category");
  237. if (isset ($_GET['keyword'])) {
  238. $keyword = Database::escape_string(trim($_GET['keyword']));
  239. $sql .= " WHERE name LIKE '%".$keyword."%' OR id LIKE '%".$keyword."%'";
  240. }
  241. $from = intval($from);
  242. $per_page = intval($per_page);
  243. $column = intval($column);
  244. if(!in_array($direction, array('ASC','DESC'))) {
  245. $direction = 'ASC';
  246. }
  247. $sql .= " ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;
  248. $result = Database::query($sql);
  249. while ($array = Database::fetch_array($result, 'NUM'))
  250. $arr[] = $array;
  251. return $arr;
  252. }
  253. /**
  254. * Get number of categories
  255. *
  256. * @return - int The amount of categories
  257. */
  258. function get_num_categories() {
  259. $sql = "SELECT COUNT(id) FROM ".Rsys :: getTable("category");
  260. if (isset ($_GET['keyword'])) {
  261. $keyword = Database::escape_string(trim($_GET['keyword']));
  262. $sql .= " WHERE name LIKE '%".$keyword."%' OR id LIKE '%".$keyword."%'";
  263. }
  264. return @ Database::result(Database::query($sql), 0, 0);
  265. }
  266. /*
  267. ============================================================================================
  268. ITEMS
  269. ============================================================================================
  270. */
  271. /**
  272. * Controls if an item in a certain category already exist
  273. *
  274. * @param - String $name The name
  275. * @param - String $category The category id
  276. * @return - Boolean True or false
  277. */
  278. function check_item($item, $category, $id=0) {
  279. $sql = "SELECT name FROM ".Rsys :: getTable("item")."
  280. WHERE LCASE(name)='".strtolower(Database::escape_string($item))."'
  281. AND category_id=".Database::escape_string($category)."
  282. AND id<>".Database::escape_string($id)."";
  283. $Result = Database::query($sql);
  284. return (Database::num_rows($Result) == 0);
  285. }
  286. /**
  287. * Adds an item
  288. *
  289. * @param - String $name The name
  290. * @param - String $description The description
  291. * @param - int $category The category-ID
  292. * @param - String $courseCode The course-Code (optional)
  293. * @return - int The id
  294. */
  295. function add_item($name, $description, $category, $course = "") {
  296. if (Rsys :: check_item($name, $category)) {
  297. $sql = "INSERT INTO ".Rsys :: getTable("item")." (category_id,course_code,name,description,creator) VALUES ('".Database::escape_string($category)."','".Database::escape_string($course)."','".Database::escape_string($name)."','".Database::escape_string($description)."','".api_get_user_id()."')";
  298. Database::query($sql);
  299. return Database::insert_id();
  300. }
  301. return false;
  302. }
  303. /**
  304. * Edits an item
  305. *
  306. * @param - int $id The id
  307. * @param - String $name The name
  308. * @param - String $description The description
  309. * @param - int $category The category-ID
  310. * @param - String $courseCode The course-Code (optional)
  311. */
  312. function edit_item($id, $name, $description, $category, $course = "") {
  313. if (!Rsys :: item_allow($id, 'edit'))
  314. return false;
  315. if (!Rsys :: check_item($name, $category, $id))
  316. return false;
  317. $sql = "UPDATE ".Rsys :: getTable("item")." SET category_id='".Database::escape_string($category)."',course_code='".Database::escape_string($course)."',name='".Database::escape_string($name)."',description='".Database::escape_string($description)."' " .
  318. "WHERE id =".Database::escape_string($id)."";
  319. Database::query($sql);
  320. return $id;
  321. }
  322. /**
  323. * Deletes an item and all linked item-rights
  324. *
  325. * @param - int $id The id
  326. */
  327. function delete_item($id) {
  328. if (!Rsys :: item_allow($id, 'delete'))
  329. return false;
  330. $sql = "SELECT id,end_at FROM".Rsys :: getTable('reservation')." WHERE item_id=".Database::escape_string($id)."";
  331. $result = Database::query($sql);
  332. while ($array = Database::fetch_array($result)) {
  333. if (Rsys :: mysql_datetime_to_timestamp(date('Y-m-d H:i:s')) <= Rsys :: mysql_datetime_to_timestamp($array[1]))
  334. $checked = true;
  335. }
  336. if (!$checked) {
  337. $sql = "DELETE FROM ".Rsys :: getTable("item")." WHERE id =".Database::escape_string($id)."";
  338. Database::query($sql);
  339. $sql = "DELETE FROM ".Rsys :: getTable("item_rights")." WHERE item_id =".Database::escape_string($id)."";
  340. Database::query($sql);
  341. $sql = "DELETE FROM ".Rsys :: getTable("reservation")." WHERE item_id =".Database::escape_string($id)."";
  342. Database::query($sql);
  343. return '0';
  344. } else {
  345. return Database::num_rows($result);
  346. }
  347. }
  348. function item_allow($item_id, $right) {
  349. // Creator heeft alle rechten
  350. switch ($right) {
  351. case 'm_rights' : // manage rights of item (only for creator and admin)
  352. $x = '';
  353. break;
  354. case 'edit' :
  355. $x = ' ir.edit_right=1 ';
  356. break;
  357. case 'delete' :
  358. $x = ' ir.delete_right=1 ';
  359. break;
  360. case 'm_reservation' :
  361. $x = ' ir.m_reservation=1 ';
  362. break;
  363. case 'view' :
  364. $x = ' ir.view_right=1 ';
  365. break;
  366. }
  367. $sql = "SELECT i.id FROM ".Rsys :: getTable("item")." i
  368. LEFT JOIN ".Rsys :: getTable("item_rights")." ir ON ir.item_id=i.id
  369. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  370. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  371. WHERE i.id='".Database::escape_string($item_id)."' AND (". (!empty ($x) ? "(cu.user_id='".api_get_user_id()."' AND ".$x.") OR " : '')." i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0).")";
  372. return Database::num_rows(Database::query($sql)) > 0;
  373. }
  374. /**
  375. * Gets an item from the database (give no param to get ALL items)
  376. *
  377. * @param - int $id The id of the item
  378. * @param - String $orderby (sql) ORDER BY $orderby
  379. * @return - Array The returned rows
  380. */
  381. function get_item($id = null, $orderby = "name ASC") {
  382. $id = Database::escape_string($id);
  383. $sql = "SELECT i.* FROM ".Rsys :: getTable("item")." i";
  384. if (!empty ($id)) {
  385. if (!Rsys :: item_allow($id, 'view'))
  386. return false;
  387. $sql .= " WHERE i.id = '".$id."'";
  388. } else
  389. $sql .= " LEFT JOIN ".Rsys :: getTable("item_rights")." ir ON ir.item_id=i.id LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id WHERE (cu.user_id='".api_get_user_id()."' AND ir.view_right=1) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0)." ORDER BY ".$orderby;
  390. $arr = Database::store_result(Database::query($sql));
  391. if (!empty ($id))
  392. return $arr[0]; // Return one row only
  393. else
  394. return $arr;
  395. }
  396. /**
  397. * Returns the blackout-status for an item
  398. *
  399. * @param - int $itemid The id of the item
  400. * @return - boolean true if blackout, false if not
  401. */
  402. function is_blackout($itemid) {
  403. $sql = "SELECT id FROM ".Rsys :: getTable("item");
  404. $sql .= " WHERE id = ".Database::escape_string($itemid)." AND blackout=1";
  405. return Database::num_rows(Database::query($sql)) == 1;
  406. }
  407. /**
  408. * Gets all items of a certain category from the database
  409. *
  410. * @param - int $id The id of the category
  411. * @param - String $orderby (sql) ORDER BY $orderby
  412. * @return - Array The returned rows
  413. */
  414. function get_category_items($id, $orderby = "name ASC") {
  415. $sql = "SELECT * FROM ".Rsys :: getTable("item")." WHERE category_id = ".Database::escape_string($id)." ORDER BY ".$orderby;
  416. $arr = Database::store_result(Database::query($sql));
  417. return $arr;
  418. }
  419. /**
  420. * Gets all items of a certain course from the database
  421. *
  422. * @param - int $id The id of the course
  423. * @param - String $orderby (sql) ORDER BY $orderby
  424. * @return - Array The returned rows
  425. */
  426. function get_course_items($id, $orderby = "name ASC") {
  427. $sql = "SELECT * FROM ".Rsys :: getTable("item")." WHERE course_id = ".Database::escape_string($id)." ORDER BY ".$orderby;
  428. $arr = Database::store_result(Database::query($sql));
  429. return $arr;
  430. }
  431. /**
  432. * Returns items for a sortable table based on the params
  433. *
  434. * @param - int $from Index of the first item to return.
  435. * @param - int $per_page The number of items to return
  436. * @param - int $column The number of the column on which the data should be sorted
  437. * @param - String $direction In which order should the data be sorted (ASC or DESC)
  438. * @return - Array The returned rows
  439. */
  440. function get_table_items($from, $per_page, $column, $direction) {
  441. $sql = "SELECT i.id AS col0, i.name as col1, i.description AS col2, ca.name AS col3, IF(i.creator='".api_get_user_id()."','".get_lang('Yes')."','".get_lang('No')."') AS col4, i.id AS col5
  442. FROM ".Rsys :: getTable("item")." i INNER JOIN ".Rsys :: getTable("category")." ca ON i.category_id = ca.id
  443. LEFT JOIN ".Rsys :: getTable("item_rights")." ir ON ir.item_id=i.id
  444. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  445. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  446. WHERE ((cu.user_id='".api_get_user_id()."' AND (ir.edit_right=1 OR ir.delete_right=1)) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0).")";
  447. if (!empty ($_GET['cat']) && $_GET['cat'] <> 0) {
  448. $sql .= " AND ca.id = '".Database::escape_string($_GET['cat'])."' ";
  449. }
  450. $from = intval($from);
  451. $per_page = intval($per_page);
  452. $column = intval($column);
  453. if(!in_array($direction, array('ASC','DESC'))) {
  454. $direction = 'ASC';
  455. }
  456. $sql .= " GROUP BY i.id ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;
  457. $result = Database::query($sql);
  458. while ($array = Database::fetch_array($result, 'NUM')) {
  459. if (!$array[4])
  460. $array[4] = '-';
  461. $arr[] = $array;
  462. }
  463. return $arr;
  464. }
  465. /**
  466. * Get number of items
  467. *
  468. * @return - int The amount of items
  469. */
  470. function get_num_items() {
  471. $sql = "SELECT COUNT(DISTINCT i.id) FROM ".Rsys :: getTable("item")." i
  472. LEFT JOIN ".Rsys :: getTable("item_rights")." ir ON ir.item_id=i.id
  473. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  474. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  475. WHERE ( 1=". (api_is_platform_admin() ? 1 : 0)."
  476. OR ((cu.user_id='".api_get_user_id()."' AND (ir.edit_right=1 OR ir.delete_right=1)) OR i.creator='".api_get_user_id()."' ))";
  477. return @ Database::result(Database::query($sql), 0, 0);
  478. }
  479. /**
  480. * Returns the rights for an item for sortable table based on the params
  481. *
  482. * @param - int $from Index of the first item to return.
  483. * @param - int $per_page The number of items to return
  484. * @param - int $column The number of the column on which the data should be sorted
  485. * @param - String $direction In which order should the data be sorted (ASC or DESC)
  486. * @return - Array The returned rows
  487. */
  488. function get_table_itemrights($from, $per_page, $column, $direction) {
  489. $itemid = Database::escape_string($_GET['item_id']);
  490. $sql = "SELECT id, name FROM ".Database :: get_main_table(TABLE_MAIN_CLASS);
  491. $result = Database::query($sql);
  492. while ($array = Database::fetch_array($result, 'NUM')) {
  493. $arr[] = $array;
  494. }
  495. $sql = "SELECT item_id, class_id,edit_right,delete_right,m_reservation,view_right
  496. FROM ".Rsys :: getTable("item_rights")." WHERE item_id=".$itemid;
  497. $result = Database::query($sql);
  498. while ($array = Database::fetch_array($result, 'NUM')) {
  499. $arr1[] = $array;
  500. }
  501. $count = -1;
  502. if (count($arr)>0) {
  503. foreach ($arr as $lijn) {
  504. $count ++;
  505. $controle = false;
  506. $tabel[$count][0] = $itemid."-".$lijn[0];
  507. $tabel[$count][1] = $lijn[1];
  508. foreach ($arr1 as $lijn2) {
  509. if ($lijn2[1] == $lijn[0]) {
  510. if ($lijn2[2] == 0) {
  511. $tabel[$count][2] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=edit&set=1\'" />';
  512. } else {
  513. $tabel[$count][2] = '<img src="../img/right.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=edit&set=0\'" />';
  514. }
  515. if ($lijn2[3] == 0) {
  516. $tabel[$count][3] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=delete&set=1\'" />';
  517. } else {
  518. $tabel[$count][3] = '<img src="../img/right.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=delete&set=0\'" />';
  519. }
  520. if ($lijn2[4] == 0) {
  521. $tabel[$count][4] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=manage&set=1\'" />';
  522. } else {
  523. $tabel[$count][4] = '<img src="../img/right.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=manage&set=0\'" />';
  524. }
  525. if ($lijn2[5] == 0) {
  526. $tabel[$count][5] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=view&set=1\'" />';
  527. } else {
  528. $tabel[$count][5] = '<img src="../img/right.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=view&set=0\'" />';
  529. }
  530. $controle = true;
  531. }
  532. }
  533. if (!$controle) {
  534. $tabel[$count][2] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=edit&set=1\'" />';
  535. $tabel[$count][3] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=delete&set=1\'" />';
  536. $tabel[$count][4] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=manage&set=1\'" />';
  537. $tabel[$count][5] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_item.php?action=m_rights&subaction=switch&class_id='.$lijn[0].'&item_id='.$itemid.'&switch=view&set=1\'" />';
  538. }
  539. $tabel[$count][6] = $itemid."-".$lijn[0];
  540. }
  541. }
  542. return $tabel;
  543. }
  544. function set_new_right($item_id, $class_id, $column, $value) {
  545. $item_id = Database::escape_string($item_id);
  546. $class_id = Database::escape_string($class_id);
  547. $value = Database::escape_string($value);
  548. $column = Database::escape_string($column);
  549. $sql = "SELECT item_id FROM ".Rsys :: getTable("item_rights")."WHERE item_id=".$item_id." AND class_id=".$class_id;
  550. $result = Database::query($sql);
  551. $switcher = Database::num_rows($result);
  552. if ($switcher > 0) {
  553. $sql = $sql = "UPDATE ".Rsys :: getTable("item_rights")." SET ".$column."='".$value."' WHERE class_id = '".$class_id."' AND item_id ='".$item_id."'";
  554. Database::query($sql);
  555. } else {
  556. $sql = "INSERT INTO ".Rsys :: getTable("item_rights")." (item_id,class_id,".$column.") VALUES ('".$item_id."','".$class_id."','".$value."')";
  557. Database::query($sql);
  558. }
  559. }
  560. /**
  561. * Get number of itemrights
  562. *
  563. * @return - int The amount of itemrights
  564. */
  565. function get_num_itemrights() {
  566. $sql = "SELECT COUNT(id) FROM ".Database :: get_main_table(TABLE_MAIN_CLASS);
  567. return @ Database::result(Database::query($sql), 0, 0);
  568. }
  569. /**
  570. * Get all classes where the item hasn't already defined rights for
  571. *
  572. * @param - int $item_id The id of the item
  573. * @return - Array The returned rows
  574. */
  575. function get_itemfiltered_class($item_id) {
  576. $item_id = Database::escape_string($item_id);
  577. $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_CLASS)."
  578. WHERE id NOT IN (SELECT class_id FROM ".Rsys :: getTable("item_rights")." WHERE item_id='".$item_id."') ORDER BY name ASC, code ASC";
  579. $arr = Database::store_result(Database::query($sql));
  580. return $arr;
  581. }
  582. /**
  583. * Get number of classes where the item hasn't already defined rights for
  584. *
  585. * @param - int $item_id The id of the item
  586. * @return - int The amount
  587. */
  588. function get_num_itemfiltered_class($item_id) {
  589. $item_id = Database::escape_string($item_id);
  590. $sql = "SELECT COUNT(id) FROM ".Database :: get_main_table(TABLE_MAIN_CLASS)." WHERE id NOT IN (SELECT class_id FROM ".Rsys :: getTable("item_rights")." WHERE item_id='".$item_id."') ORDER BY name ASC, code ASC";
  591. return Database::result(Database::query($sql), 0, 0);
  592. }
  593. /**
  594. * Adds an item-right
  595. *
  596. * @param - int $item_id Item-ID
  597. * @param - int $class_id Class-ID
  598. * @param - int $edit Edit Right
  599. * @param - int $delete Delete Right
  600. * @param - int $m_reservation Manage reservations Right
  601. */
  602. function add_item_right($item_id, $class_id, $edit, $delete, $m_reservation) {
  603. if (!Rsys :: item_allow($item_id, 'm_rights'))
  604. return false;
  605. $sql = "INSERT INTO ".Rsys :: getTable("item_rights")." (item_id,class_id,edit_right,delete_right,m_reservation) VALUES ('".Database::escape_string($item_id)."','".Database::escape_string($class_id)."','".Database::escape_string($edit)."','".Database::escape_string($delete)."','".Database::escape_string($m_reservation)."')";
  606. Database::query($sql);
  607. }
  608. /**
  609. * Edits an item-right
  610. *
  611. * @param - int $item_id Item-ID
  612. * @param - int $class_id Class-ID
  613. * @param - int $edit Edit Right
  614. * @param - int $delete Delete Right
  615. * @param - int $m_reservation Manage reservations Right
  616. * @return - int The id
  617. */
  618. function edit_item_right($item_id, $class_id, $edit, $delete, $m_reservation) {
  619. $item_id = Database::escape_string($item_id);
  620. $class_id = Database::escape_string($class_id);
  621. if (!Rsys :: item_allow($item_id, 'm_rights'))
  622. return false;
  623. $sql = "UPDATE ".Rsys :: getTable("item_rights")." SET edit_right='".Database::escape_string($edit)."', delete_right='".Database::escape_string($delete)."', m_reservation='".Database::escape_string($m_reservation)."' WHERE class_id = '".$class_id."' AND item_id ='".$item_id."'";
  624. Database::query($sql);
  625. }
  626. /**
  627. * Deletes an item-right
  628. *
  629. * @param - int $id The id
  630. */
  631. function delete_item_right($item_id, $class_id) {
  632. $item_id = Database::escape_string($item_id);
  633. $class_id = Database::escape_string($class_id);
  634. if (!Rsys :: item_allow($item_id, 'm_rights'))
  635. return false;
  636. $sql = "DELETE FROM ".Rsys :: getTable("item_rights")." WHERE item_id='".$item_id."' AND class_id='".$class_id."'";
  637. Database::query($sql);
  638. }
  639. function get_class_group($class_id) {
  640. $class_id = Database::escape_string($class_id);
  641. $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_CLASS)." WHERE id='".$class_id."'";
  642. $arr = Database::store_result(Database::query($sql));
  643. return $arr;
  644. }
  645. function get_item_rights($item_id, $class_id) {
  646. $item_id = Database::escape_string($item_id);
  647. $class_id = Database::escape_string($class_id);
  648. $sql = "SELECT * FROM ".Rsys :: getTable('item_rights')." WHERE item_id='".$item_id."' AND class_id='".$class_id."'";
  649. $arr = Database::store_result(Database::query($sql));
  650. return $arr;
  651. }
  652. function black_out_changer($item_id) {
  653. $item_id = Database::escape_string($item_id);
  654. $sql = "SELECT blackout FROM ".Rsys :: getTable("item")." WHERE id='".$item_id."'";
  655. $Value = Database::store_result(Database::query($sql));
  656. ($Value[0][0] == 0 ? $changedValue = 1 : $changedValue = 0);
  657. $sql = "UPDATE ".Rsys :: getTable("item")." SET blackout='".$changedValue."' WHERE id = '".$item_id."'";
  658. Database::query($sql);
  659. Rsys :: black_out_notifier($item_id, $Value[0][0]);
  660. return $changedValue;
  661. }
  662. function black_out_notifier($item_id, $value) {
  663. $item_id = Database::escape_string($item_id);
  664. $value = Database::escape_string($value);
  665. $sql = "SELECT id, timepicker FROM ".Rsys :: getTable('reservation')."
  666. WHERE item_id='".$item_id."' AND subscribers > '0'";
  667. $value == 1 ? $sql .= " AND end_at >= (NOW()-7000000) " : $sql .= " AND end_at >= NOW()";
  668. $reservations = Database::query($sql);
  669. while ($reservation = Database::fetch_array($reservations)) {
  670. $sql = "SELECT user_id FROM ".Rsys :: getTable('subscription')." WHERE reservation_id='".$reservation[0]."'";
  671. if ($reservation[1] == 1) {
  672. $sql .= " AND end_at >= NOW() ";
  673. }
  674. $subscriptions = Database::query($sql);
  675. while ($subscription = Database::fetch_array($subscriptions)) {
  676. $user_info = api_get_user_info($subscription[0]);
  677. $sql2 = "SELECT name FROM ".Rsys :: getTable('item')." WHERE id='".$item_id."'";
  678. $items = Database::query($sql2);
  679. $item = Database::fetch_array($items);
  680. $item_name=$item['name'];
  681. if ($reservation[1] == 0)
  682. {
  683. //er wordt geen gebruik gemaakt van een timepicker dus begin en einddatum kan opgehaald worden uit reservation
  684. $sql2 = "SELECT start_at,end_at FROM ".Rsys :: getTable('reservation')." WHERE id='".$reservation[0]."'";
  685. }
  686. else
  687. {
  688. //er wordt gebruik gemaakt van een timepicker dus begin en einddatum kan opgehaald worden uit subscriptions
  689. $sql2 = "SELECT start_at,end_at FROM ".Rsys :: getTable('subscription')." WHERE reservation_id='".$reservation[0]."'";
  690. }
  691. $items = Database::query($sql2);
  692. $item = Database::fetch_array($items);
  693. $begindatum = $item['start_at'];
  694. $einddatum = $item['end_at'];
  695. if ($value==1) {
  696. $inhoud = str_replace('#NAME#', $item_name, get_lang('ReservationActive'));
  697. $inhoud = str_replace('#BEGIN#', $begindatum, $inhoud);
  698. $inhoud = str_replace('#BEGIN#', $einddatum, $inhoud);
  699. $titel = str_replace('#NAME#', $item_name, get_lang('ReservationAvailable'));
  700. } else {
  701. $inhoud = str_replace('#NAME#', $item_name, get_lang('ReservationCancelled'));
  702. $inhoud = str_replace('#BEGIN#', $begindatum, $inhoud);
  703. $inhoud = str_replace('#BEGIN#', $einddatum, $inhoud);
  704. $titel = str_replace('#NAME#', $item_name, get_lang('ReservationUnavailable'));
  705. }
  706. //api_send_mail($user_info['mail'], $titel, $inhoud);
  707. api_mail_html(null, $user_info['mail'], $titel, $inhoud);
  708. }
  709. }
  710. }
  711. /*
  712. ============================================================================================
  713. RESERVATION PERIODS
  714. ============================================================================================
  715. */
  716. function recurrence_list() {
  717. $arr['1'] = get_lang('EveryDay');
  718. $arr['7'] = get_lang('EveryWeek');
  719. //$arr['month'] = get_lang('EveryMonth');
  720. return $arr;
  721. }
  722. function check_date($item_id, $start_date, $end_date, $start_at, $end_at) {
  723. $item_id = Database::escape_string($item_id);
  724. $start_date = Database::escape_string($start_date);
  725. $end_date = Database::escape_string($end_date);
  726. $start_at = Database::escape_string($start_at);
  727. $end_at = Database::escape_string($end_at);
  728. $sql = "SELECT * FROM ".Rsys :: getTable('reservation')." WHERE item_id='".$item_id."' ORDER BY start_at";
  729. $result = Database::query($sql);
  730. while ($array = Database::fetch_array($result)) {
  731. $GLOBALS['start_date'] = $array[5];
  732. $GLOBALS['end_date'] = $array[6];
  733. if (Rsys :: mysql_datetime_to_timestamp($array[5]) <= $start_date && Rsys :: mysql_datetime_to_timestamp($array[6]) >= $start_date) {
  734. return $array[0];
  735. }
  736. if (Rsys :: mysql_datetime_to_timestamp($array[5]) <= $end_date && Rsys :: mysql_datetime_to_timestamp($array[6]) >= $end_date)
  737. return $array[0];
  738. }
  739. $sql = "SELECT id, start_at, end_at FROM ".Rsys :: getTable('reservation')."
  740. WHERE ((start_at > '".$start_at."' AND
  741. start_at < '".$end_at."') OR
  742. (end_at > '".$start_at."' AND
  743. end_at < '".$end_at."') OR (start_at <= '".$start_at."' AND end_at >= '".$end_at."')) AND item_id='".$item_id."'";
  744. $result = Database::fetch_array(Database::query($sql));
  745. if (count($result) != 0){
  746. $GLOBALS['start_date'] = $result[1];
  747. $GLOBALS['end_date'] = $result[2];
  748. return $result[0];
  749. }
  750. return 0;
  751. }
  752. function check_date_edit($item_id, $start_date, $end_date, $start_at, $end_at, $reservation_id) {
  753. $item_id = Database::escape_string($item_id);
  754. $reservation_id = Database::escape_string($reservation_id);
  755. $start_date = Database::escape_string($start_date);
  756. $end_date = Database::escape_string($end_date);
  757. $start_at = Database::escape_string($start_at);
  758. $end_at = Database::escape_string($end_at);
  759. $sql = "SELECT * FROM ".Rsys :: getTable('reservation')." WHERE item_id='".$item_id."' AND id <> '".$reservation_id."' ORDER BY start_at";
  760. $result = Database::query($sql);
  761. while ($array = Database::fetch_array($result)) {
  762. $GLOBALS['start_date'] = $array[5];
  763. $GLOBALS['end_date'] = $array[6];
  764. if (Rsys :: mysql_datetime_to_timestamp($array[5]) < $start_date && Rsys :: mysql_datetime_to_timestamp($array[6]) > $start_date) {
  765. return $array[0];
  766. }
  767. if (Rsys :: mysql_datetime_to_timestamp($array[5]) < $end_date && Rsys :: mysql_datetime_to_timestamp($array[6]) > $end_date)
  768. return $array[0];
  769. }
  770. $sql = "SELECT id FROM ".Rsys :: getTable('reservation')."
  771. WHERE ((start_at > '".$start_at."' AND
  772. start_at < '".$end_at."') OR
  773. (end_at > '".$start_at."' AND
  774. end_at < '".$end_at."') OR
  775. (start_at <= '".$start_at."' AND
  776. end_at >= '".$end_at."')) AND item_id='".$item_id."' AND id <> '".$reservation_id."'";
  777. $result = Database::fetch_array(Database::query($sql));
  778. if (count($result) != 0){
  779. $GLOBALS['start_date'] = $result[1];
  780. $GLOBALS['end_date'] = $result[2];
  781. return $result[0];
  782. }
  783. return 0;
  784. }
  785. function get_category_rights() {
  786. $sql = "SELECT cat.id as catid,cat.name as catname
  787. FROM ".Rsys :: getTable('category')." cat
  788. LEFT JOIN ".Rsys :: getTable('item')." i ON cat.id=i.category_id
  789. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  790. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  791. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  792. WHERE (cu.user_id='".api_get_user_id()."' AND ir.m_reservation=1 ) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0)." ORDER BY cat.name ASC";
  793. $result = Database::query($sql);
  794. while ($array = Database::fetch_array($result))
  795. $arr[$array['catid']] = $array['catname'];
  796. return $arr;
  797. }
  798. /**
  799. * Returns an array with items from a category linked to rights(used by m_reservations.php)
  800. */
  801. function get_cat_r_items($category) {
  802. $category = Database::escape_string($category);
  803. $sql = "SELECT i.id,i.name as catitem
  804. FROM ".Rsys :: getTable('item')." i
  805. INNER JOIN ".Rsys :: getTable('category')." cat ON cat.id=i.category_id
  806. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  807. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  808. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  809. WHERE ((cu.user_id='".api_get_user_id()."' AND ir.m_reservation=1 ) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0).") AND (category_id =".$category.")
  810. ORDER BY cat.name ASC, i.name ASC";
  811. $result = Database::query($sql);
  812. while ($array = Database::fetch_array($result))
  813. $arr[$array['id']] = $array['catitem'];
  814. return $arr;
  815. }
  816. /**
  817. * Returns an array with [ itemID => "category/item" ] with view_rights (used by reservation.php)
  818. */
  819. function get_cat_items($category) {
  820. $category = Database::escape_string($category);
  821. $sql = "SELECT i.id,i.name as catitem
  822. FROM ".Rsys :: getTable('item')." i
  823. INNER JOIN ".Rsys :: getTable('category')." cat ON cat.id=i.category_id
  824. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  825. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  826. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  827. WHERE ((cu.user_id='".api_get_user_id()."' AND ir.view_right=1 ) OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0).") AND (category_id =".$category.")
  828. ORDER BY cat.name ASC, i.name ASC";
  829. $result = Database::query($sql);
  830. while ($array = Database::fetch_array($result))
  831. $arr[$array['id']] = $array['catitem'];
  832. return $arr;
  833. }
  834. /**
  835. * Returns the reservations for sortable table based on the params
  836. *
  837. * @param - int $from Index of the first item to return.
  838. * @param - int $per_page The number of items to return
  839. * @param - int $column The number of the column on which the data should be sorted
  840. * @param - String $direction In which order should the data be sorted (ASC or DESC)
  841. * @return - Array The returned rows
  842. */
  843. function get_table_reservations($from, $per_page, $column, $direction) {
  844. $sql = "SELECT DISTINCT r.id AS col0, i.name AS col1, DATE_FORMAT(r.start_at,'%Y-%m-%d %H:%i') AS col2, DATE_FORMAT(r.end_at,'%Y-%m-%d %H:%i') AS col3," .
  845. "DATE_FORMAT(r.subscribe_from,'%Y-%m-%d %k:%i') AS col4, DATE_FORMAT(r.subscribe_until,'%Y-%m-%d %k:%i') AS col5,IF(timepicker <> 0, '".get_lang('TimePicker')."',CONCAT(r.subscribers,'/',r.max_users)) AS col6, r.notes AS col7, r.id as col8
  846. FROM ".Rsys :: getTable('reservation')." r
  847. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id=i.id
  848. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  849. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  850. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  851. WHERE ((ir.m_reservation=1 AND cu.user_id='".api_get_user_id()."') OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0).")";
  852. if (isset ($_GET['keyword'])) {
  853. $keyword = Database::escape_string(trim($_GET['keyword']));
  854. $sql .= "AND (i.name LIKE '%".$keyword."%' OR i.description LIKE '%".$keyword."%' OR r.notes LIKE '%".$keyword."%')";
  855. }
  856. $from = intval($from);
  857. $per_page = intval($per_page);
  858. $column = intval($column);
  859. if(!in_array($direction, array('ASC','DESC'))) {
  860. $direction = 'ASC';
  861. }
  862. $sql .= " ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;
  863. $result = Database::query($sql);
  864. while ($array = Database::fetch_array($result, 'NUM')) {
  865. $arr[] = $array;
  866. }
  867. return $arr;
  868. }
  869. function check_edit_right($id) {
  870. $id = Database::escape_string($id);
  871. $sql = "SELECT r.id
  872. FROM ".Rsys :: getTable('reservation')." r
  873. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id=i.id
  874. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  875. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  876. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  877. WHERE ((cu.user_id='".api_get_user_id()."'AND ir.edit_right=1) OR 1=". (api_is_platform_admin() ? 1 : 0).") AND r.id='".$id."'";
  878. $result = Database::query($sql);
  879. while ($array = Database::fetch_array($result, 'NUM')) {
  880. $arr[] = $array;
  881. }
  882. return $result;
  883. }
  884. function check_delete_right($id) {
  885. $id = Database::escape_string($id);
  886. $sql = "SELECT r.id
  887. FROM ".Rsys :: getTable('reservation')." r
  888. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id=i.id
  889. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  890. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  891. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  892. WHERE ((cu.user_id='".api_get_user_id()."'AND ir.delete_right=1) OR 1=". (api_is_platform_admin() ? 1 : 0).") AND r.id='".$id."'";
  893. $result = Database::query($sql);
  894. while ($array = Database::fetch_array($result, 'NUM')) {
  895. $arr[] = $array;
  896. }
  897. return $arr;
  898. }
  899. function check_auto_accept($id) {
  900. $id = Database::escape_string($id);
  901. $sql = "SELECT auto_accept FROM ".Rsys :: getTable('reservation')." WHERE id='".$id."'";
  902. return Database::result(Database::query($sql), 0, 0);
  903. }
  904. /**
  905. * Get number of reservations
  906. *
  907. * @return - int The amount
  908. */
  909. function get_num_reservations() {
  910. $sql = "SELECT COUNT(DISTINCT r.id)
  911. FROM ".Rsys :: getTable('reservation')." r
  912. LEFT JOIN ".Rsys :: getTable('item')." i ON i.id=r.item_id
  913. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=r.item_id
  914. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = r.item_id
  915. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  916. WHERE ((ir.m_reservation=1 AND cu.user_id='".api_get_user_id()."') OR i.creator='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0).')';
  917. if (isset ($_GET['keyword'])) {
  918. $keyword = Database::escape_string(trim($_GET['keyword']));
  919. $sql .= " AND (i.name LIKE '%".$keyword."%' OR i.description LIKE '%".$keyword."%' OR r.notes LIKE '%".$keyword."%')";
  920. }
  921. return Database::result(Database::query($sql), 0, 0);
  922. }
  923. /**
  924. * Adds a reservation
  925. *
  926. * @param - $item_id,$auto_accept,$max_users,$start_at,$end_at,$subscribe_until,$notes
  927. * @return - FALSE if there is something wrong with the dates, a mysql_insert_id() if everything went perfectly
  928. */
  929. function add_reservation($item_id, $auto_accept, $max_users, $start_at, $end_at, $subscribe_from, $subscribe_until, $notes, $timepicker, $min, $max,$subid) {
  930. $stamp_start = Rsys :: mysql_datetime_to_timestamp($start_at);
  931. $stamp_end = Rsys :: mysql_datetime_to_timestamp($end_at);
  932. $stamp_start_date = date( 'Y-m-d',$stamp_start);
  933. $stamp_end_date = date( 'Y-m-d',$stamp_end);
  934. if (Rsys :: check_date($item_id, $stamp_start, $stamp_end, $start_at, $end_at) <> 0)
  935. return 1;
  936. if ($subscribe_until != 0) {
  937. $stamp_until = Rsys :: mysql_datetime_to_timestamp($subscribe_until);
  938. if ($stamp_until > $stamp_start)
  939. return 2;
  940. }
  941. if ($start_at < (date( 'Y-m-d H:i:s',time())))
  942. return 3;
  943. if (($stamp_start_date != $stamp_end_date) && $timepicker == '1')
  944. {
  945. return 4;
  946. }
  947. if($timepicker == '0')
  948. {
  949. if ($min != '0' || $max != '0')
  950. {
  951. //kan niet verschillen van 0!
  952. return 5;
  953. }
  954. }
  955. else
  956. {
  957. if (!($max==0 && $min==0))
  958. {
  959. if ($max < $min)
  960. {
  961. return 6;
  962. //maximum kan niet kleiner zijn dan minimum
  963. }
  964. else
  965. {
  966. $stamp = ($stamp_end - $stamp_start)/ 60;
  967. if (($stamp/$max)<1)
  968. {
  969. return 7;
  970. // er past geen blok van het tijdverschil
  971. }
  972. }
  973. }
  974. }
  975. $sql = "INSERT INTO ".Rsys :: getTable("reservation")." (item_id,auto_accept,max_users,start_at,end_at,subscribe_from,subscribe_until,notes,timepicker,timepicker_min,timepicker_max,subid) VALUES ('".Database::escape_string($item_id)."','".Database::escape_string($auto_accept)."','". (intval($max_users) > 1 ? $max_users : 1)."','".Database::escape_string($start_at)."','".Database::escape_string($end_at)."','".Database::escape_string($subscribe_from)."','".Database::escape_string($subscribe_until)."','".Database::escape_string($notes)."','".$timepicker."','".$min."','".$max."','". ($subid == 0 ? 0 : $subid)."')";
  976. Database::query($sql);
  977. return 0;
  978. }
  979. /**
  980. * Edits a reservation
  981. *
  982. * @param - int $id The reservation-ID
  983. * @param - $item_id,$auto_accept,$max_users,$start_at,$end_at,$subscribe_until,$notes
  984. * @return - FALSE if there is something wrong with the dates, TRUE if everything went perfectly
  985. *
  986. */
  987. function edit_reservation($id, $item_id, $auto_accept, $max_users, $start_at, $end_at, $subscribe_from, $subscribe_until, $notes, $timepicker) {
  988. $id = Database::escape_string($id);
  989. if (!Rsys :: item_allow($item_id, 'm_reservation'))
  990. return false;
  991. $stamp_start = Rsys :: mysql_datetime_to_timestamp($start_at);
  992. $stamp_end = Rsys :: mysql_datetime_to_timestamp($end_at);
  993. $stamp_start_date = date( 'Y-m-d',$stamp_start);
  994. $stamp_end_date = date( 'Y-m-d',$stamp_end);
  995. if (Rsys :: check_date_edit($item_id, $stamp_start, $stamp_end, $start_at, $end_at, $id) <> 0)
  996. return 1;
  997. if ($subscribe_until != 0) {
  998. $stamp_until = Rsys :: mysql_datetime_to_timestamp($subscribe_until);
  999. if ($stamp_until > $stamp_start)
  1000. return 2;
  1001. }
  1002. $sql = "SELECT timepicker, subscribers FROM ".Rsys :: getTable("reservation")." WHERE id='".$id."'";
  1003. $result = Database::fetch_array(Database::query($sql));
  1004. if ($result[0] == 0 && $result[1] > $max_users) {
  1005. return 3;
  1006. }
  1007. if (($stamp_start_date != $stamp_end_date) && $timepicker == '1')
  1008. {
  1009. return 4;
  1010. }
  1011. if ($auto_accept == 1) {
  1012. $sql = "SELECT dummy FROM ".Rsys :: getTable("subscription")." WHERE reservation_id='".$id."'";
  1013. $result = Database::query($sql);
  1014. while ($array = Database::fetch_array($result, 'NUM')) {
  1015. Rsys :: set_accepted($array[0], 1);
  1016. }
  1017. } else {
  1018. $auto_accept = 0;
  1019. }
  1020. $sql = "UPDATE ".Rsys :: getTable("reservation")." SET item_id='".Database::escape_string($item_id)."',auto_accept='".Database::escape_string($auto_accept)."',max_users='". ($max_users > 1 ? $max_users : 1)."',start_at='".Database::escape_string($start_at)."',end_at='".Database::escape_string($end_at)."',subscribe_from='".Database::escape_string($subscribe_from)."',subscribe_until='".Database::escape_string($subscribe_until)."',notes='".Database::escape_string($notes)."' WHERE id='".$id."'";
  1021. Database::query($sql);
  1022. return 0;
  1023. }
  1024. /**
  1025. * Deletes a reservation
  1026. */
  1027. function delete_reservation($id) {
  1028. $id = Database::escape_string($id);
  1029. $sql = "SELECT id FROM ".Rsys :: getTable("reservation")."WHERE id='".$id."' OR subid='".$id."'";
  1030. $result2 = Database::query($sql);
  1031. while ($arr = Database::fetch_array($result2, 'NUM')) {
  1032. $sql = "SELECT s.dummy, s.user_id, i.name, r.start_at, r.end_at
  1033. FROM ".Rsys :: getTable("subscription")." s
  1034. INNER JOIN ".Rsys :: getTable("reservation")." r ON s.reservation_id = r.id
  1035. INNER JOIN ".Rsys :: getTable("item")." i ON r.item_id = i.id
  1036. WHERE s.reservation_id='".$arr[0]."'";
  1037. $result = Database::query($sql);
  1038. while ($array = Database::fetch_array($result, 'NUM')) {
  1039. $user_info = api_get_user_info($array[1]);
  1040. //api_send_mail($user_info['mail'], str_replace('#NAME#', $array[2], get_lang("ReservationDeleteTitle")), str_replace('#START#', $array[3], str_replace('#END#', $array[4], str_replace('#NAME#', $array[2], get_lang("ReservationDeleteMessage")))));
  1041. $sql = "DELETE FROM ".Rsys :: getTable("subscription")." WHERE dummy='".$array[0]."'";
  1042. Database::query($sql);
  1043. }
  1044. $sql = "DELETE FROM ".Rsys :: getTable("reservation")." WHERE id='".$arr[0]."'";
  1045. Database::query($sql);
  1046. }
  1047. }
  1048. function is_owner_item($id) {
  1049. $id = Database::escape_string($id);
  1050. $sql = "SELECT creator FROM ".Rsys :: getTable('item')." i ,".Rsys :: getTable('reservation')." r
  1051. where i.id = r.item_id
  1052. and r.id = '".$id."'
  1053. and i.creator ='".api_get_user_id()."'";
  1054. $result = Database::query($sql);
  1055. if (Database::num_rows($result) != 0)
  1056. return 1;
  1057. return 0;
  1058. }
  1059. function get_reservation($id) {
  1060. $id = Database::escape_string($id);
  1061. $sql = "SELECT *
  1062. FROM ".Rsys :: getTable('reservation')." r
  1063. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id=i.id
  1064. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  1065. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  1066. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  1067. WHERE (cu.user_id='".api_get_user_id()."' OR 1=". (api_is_platform_admin() ? 1 : 0)." OR 1=".(Rsys :: is_owner_item("$id")? 1 : 0).") AND r.id='".$id."'";
  1068. $result = Database::query($sql);
  1069. while ($array = Database::fetch_array($result, 'NUM'))
  1070. $arr[] = $array;
  1071. return $arr;
  1072. }
  1073. function get_num_subscriptions_overview() {
  1074. $sql = "SELECT COUNT(s.reservation_id)
  1075. FROM ".Rsys :: getTable('subscription')." s, ".Rsys :: getTable('reservation')." r1, ".Database :: get_main_table(TABLE_MAIN_USER)." u," .Rsys :: getTable('item')." i1
  1076. where r1.id = s.reservation_id
  1077. and i1.id = r1.item_id
  1078. and u.user_id = s.user_id
  1079. and s.reservation_id IN
  1080. (SELECT DISTINCT(r2.id)
  1081. FROM ".Rsys :: getTable('reservation')." r2
  1082. LEFT JOIN ".Rsys :: getTable('item')." i2 ON i2.id=r2.item_id
  1083. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=r2.item_id
  1084. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = r2.item_id
  1085. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  1086. WHERE ((ir.m_reservation=1 AND cu.user_id='".api_get_user_id()."')
  1087. OR i2.creator='".api_get_user_id()."'
  1088. OR 1=". (api_is_platform_admin() ? 1 : 0)."))";
  1089. if (isset ($_GET['keyword'])) {
  1090. $keyword = Database::escape_string(trim($_GET['keyword']));
  1091. $sql .= " AND (i1.name LIKE '%".$keyword."%' or r1.start_at LIKE '%".$keyword."%' or r1.end_at LIKE '%".$keyword."%' or u.lastname LIKE '%".$keyword."%' or u.firstname LIKE '%".$keyword."%' or s.start_at LIKE '%".$keyword."%' or s.end_at LIKE '%".$keyword."%')";
  1092. }
  1093. return Database::result(Database::query($sql), 0, 0);
  1094. }
  1095. function get_table_subcribed_reservations($from, $per_page, $column, $direction) {
  1096. $from = intval($from);
  1097. $per_page = intval($per_page);
  1098. $column = intval($column);
  1099. if(!in_array($direction, array('ASC','DESC'))) {
  1100. $direction = 'ASC';
  1101. }
  1102. $sql = "SELECT i1.name as col0,c.name as col1,
  1103. DATE_FORMAT(r1.start_at ,'%Y-%m-%d %H:%i') as col2,
  1104. DATE_FORMAT(r1.end_at ,'%Y-%m-%d %H:%i') as col3, ".(api_is_western_name_order() ? "CONCAT(u.firstname,' ',u.lastname)" : "CONCAT(u.lastname,' ',u.firstname)")." as col4,
  1105. DATE_FORMAT(s.start_at ,'%Y-%m-%d %H:%i') as col5,
  1106. DATE_FORMAT(s.end_at ,'%Y-%m-%d %H:%i') as col6, s.accepted as col7
  1107. FROM ".Rsys :: getTable('subscription')." s, ".Rsys :: getTable('reservation')." r1, ".Database :: get_main_table(TABLE_MAIN_USER)." u," .Rsys :: getTable('item')." i1,".Rsys :: getTable('category')." c
  1108. WHERE r1.id = s.reservation_id
  1109. and c.id = i1.category_id
  1110. and i1.id = r1.item_id
  1111. and u.user_id = s.user_id
  1112. and s.reservation_id IN
  1113. (SELECT DISTINCT(r2.id)
  1114. FROM ".Rsys :: getTable('reservation')." r2
  1115. LEFT JOIN ".Rsys :: getTable('item')." i2 ON i2.id=r2.item_id
  1116. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=r2.item_id
  1117. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = r2.item_id
  1118. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  1119. WHERE ((ir.m_reservation=1 AND cu.user_id='".api_get_user_id()."')
  1120. OR i2.creator='".api_get_user_id()."'
  1121. OR 1=". (api_is_platform_admin() ? 1 : 0)."))";
  1122. if (isset ($_GET['keyword'])) {
  1123. $keyword = Database::escape_string(trim($_GET['keyword']));
  1124. $sql .= " AND (i1.name LIKE '%".$keyword."%' or c.name LIKE '%".$keyword."%' or r1.start_at LIKE '%".$keyword."%' or r1.end_at LIKE '%".$keyword."%' or u.lastname LIKE '%".$keyword."%' or u.firstname LIKE '%".$keyword."%' or s.start_at LIKE '%".$keyword."%' or s.end_at LIKE '%".$keyword."%')";
  1125. }
  1126. $sql .= " ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;
  1127. /*$result = Database::query($sql);
  1128. while ($array = Database::fetch_array($result, 'NUM'))
  1129. $arr[] = $array;*/
  1130. $result = Database::query($sql);
  1131. while ($array = Database::fetch_array($result, 'NUM')) {
  1132. $row = array();
  1133. $row[] = $array[0];
  1134. $row[] = $array[1];
  1135. $row[] = $array[2];
  1136. $row[] = $array[3];
  1137. $row[] = $array[4];
  1138. if ($array[5]=='0000-00-00 00:00') {
  1139. $row[] = $array[2];
  1140. }
  1141. else {
  1142. $row[] = $array[5];
  1143. }
  1144. if ($array[6]=='0000-00-00 00:00') {
  1145. $row[] = $array[3];
  1146. }
  1147. else {
  1148. $row[] = $array[6];
  1149. }
  1150. if ($array[7]=='1')
  1151. {
  1152. $row[] = get_lang('Yes');
  1153. }
  1154. else {
  1155. $row[] = get_lang('No');
  1156. }
  1157. $arr[] = $row;
  1158. }
  1159. return $arr;
  1160. }
  1161. function get_num_waiting_users() {
  1162. $sql = "SELECT COUNT(DISTINCT dummy) FROM ".Rsys :: getTable('subscription');
  1163. if (isset ($_GET['rid'])) {
  1164. $sql .= " WHERE reservation_id = '".intval($_GET['rid'])."'";
  1165. }
  1166. return Database::result(Database::query($sql), 0, 0);
  1167. }
  1168. function get_table_waiting_users($from, $per_page, $column, $direction) {
  1169. $from = intval($from);
  1170. $per_page = intval($per_page);
  1171. $column = intval($column);
  1172. if(!in_array($direction, array('ASC','DESC'))) {
  1173. $direction = 'ASC';
  1174. }
  1175. /*$sql = "SELECT dummy AS col0, ".(api_is_western_name_order() ? "CONCAT(u.firstname,' ',u.lastname)" : "CONCAT(u.lastname,' ',u.firstname)")." AS col1, s.user_id AS col2, accepted AS col3
  1176. FROM ".Rsys :: getTable('subscription')." s
  1177. INNER JOIN ".Database :: get_main_table(TABLE_MAIN_USER)." u ON s.user_id = u.user_id ";
  1178. if (!empty ($_GET['rid'])) {
  1179. $sql .= " WHERE s.reservation_id = '".$_GET['rid']."'";
  1180. }
  1181. $sql .= " ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;*/
  1182. $sql = "SELECT dummy AS col0, ".(api_is_western_name_order() ? "CONCAT(u.firstname,' ',u.lastname)" : "CONCAT(u.lastname,' ',u.firstname)")." AS col1, s.user_id AS col2, accepted AS col3, r.start_at, r.end_at, s.start_at, s.end_at
  1183. FROM ".Rsys :: getTable('subscription')." s,".Database :: get_main_table(TABLE_MAIN_USER)." u,".Database :: get_main_table(TABLE_MAIN_RESERVATION_RESERVATION)." r
  1184. where u.user_id = s.user_id
  1185. and s.reservation_id = r.id";
  1186. if (!empty ($_GET['rid'])) {
  1187. $sql .= " and r.id = '".Database::escape_string($_GET['rid'])."'";
  1188. }
  1189. $sql .= " ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;
  1190. $result = Database::query($sql);
  1191. while ($array = Database::fetch_array($result, 'NUM')) {
  1192. $arr[] = $array;
  1193. }
  1194. $count = 0;
  1195. $x = count($arr);
  1196. while ($count < $x) {
  1197. $sql = "SELECT name
  1198. FROM ".Database :: get_main_table(TABLE_MAIN_CLASS)." cl
  1199. INNER JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = cl.id
  1200. WHERE cu.user_id=".$arr[$count][2]." LIMIT 1";
  1201. $result = Database::query($sql);
  1202. while ($array = Database::fetch_array($result, 'NUM')) {
  1203. $arr2[] = $array;
  1204. }
  1205. $arr[$count][2] = $arr2[0][0];
  1206. $count ++;
  1207. }
  1208. $count = -1;
  1209. if (is_array($arr)) {
  1210. foreach ($arr as $lijn) {
  1211. $count ++;
  1212. $controle = false;
  1213. $tabel[$count][0] = $lijn[0];
  1214. $tabel[$count][1] = $lijn[1];
  1215. if ($lijn[3] == 0) {
  1216. $tabel[$count][5] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_reservation.php?action=accept&rid='.$_GET['rid'].'&amp;dummy='.$lijn[0].'&switch=edit&set=1\'" />';
  1217. } else {
  1218. $tabel[$count][5] = '<img src="../img/right.gif" onclick="document.location.href=\'m_reservation.php?action=accept&rid='.$_GET['rid'].'&amp;dummy='.$lijn[0].'&switch=edit&set=0\'" />';
  1219. }
  1220. $tabel[$count][2] = $lijn[2];
  1221. if ($lijn[6] == '0000-00-00 00:00:00' && $lijn[7] == '0000-00-00 00:00:00')
  1222. {
  1223. $tabel[$count][3] = $lijn[4];
  1224. $tabel[$count][4] = $lijn[5];
  1225. }
  1226. else
  1227. {
  1228. $tabel[$count][3] = $lijn[6];
  1229. $tabel[$count][4] = $lijn[7];
  1230. }
  1231. $tabel[$count][6] = '<img src="../img/wrong.gif" onclick="document.location.href=\'m_reservation.php?action=accept&rid='.$_GET['rid'].'&amp;dummy='.$lijn[0].'&switch=delete\'" />';
  1232. }
  1233. }
  1234. return $tabel;
  1235. }
  1236. function set_accepted($id, $value) {
  1237. global $subscription;
  1238. $id = Database::escape_string($id);
  1239. $value = Database::escape_string($value);
  1240. $sql = "UPDATE ".Rsys :: getTable('subscription')." SET ACCEPTED='".$value."' WHERE dummy='".$id."'";
  1241. Database::query($sql);
  1242. $user_info = api_get_user_info($subscription[0]);
  1243. $sql = "SELECT name FROM ".Rsys :: getTable('subscription')." s
  1244. INNER JOIN ".Rsys :: getTable('reservation')." r ON s.reservation_id = r.id
  1245. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id = i.id
  1246. WHERE dummy='".$id."'";
  1247. $items = Database::query($sql);
  1248. $item = Database::fetch_array($items);
  1249. $item_name = $item[0];
  1250. $sql = "SELECT start_at, end_at, timepicker
  1251. from ".Rsys :: getTable('reservation')."
  1252. where id in ( SELECT reservation_id
  1253. from ".Rsys :: getTable('subscription')."
  1254. where dummy ='".$id."')";
  1255. $items = Database::query($sql);
  1256. $item = Database::fetch_array($items);
  1257. if ($item['timepicker'] == '1')
  1258. {
  1259. $sql = "SELECT start_at, end_at
  1260. from ".Rsys :: getTable('subscription')."
  1261. where dummy ='".$id."'";
  1262. $items = Database::query($sql);
  1263. $item = Database::fetch_array($items);
  1264. }
  1265. $begin_datum = $item['start_at'];
  1266. $eind_datum = $item['end_at'];
  1267. if ($value==1) {
  1268. $titel = str_replace('#ITEM#', $item_name, get_lang('ReservationAccepted'));
  1269. $inhoud = str_replace('#ITEM#', $item_name, get_lang('ReservationForItemAccepted'));
  1270. } else {
  1271. $titel = str_replace('#ITEM#', $item_name, get_lang('ReservationDenied'));
  1272. $inhoud = str_replace('#ITEM#', $item_name, get_lang('ReservationForDenied'));
  1273. }
  1274. $inhoud = str_replace('#BEGIN', $begin_datum, $inhoud);
  1275. $inhoud = str_replace('#END', $eind_datum, $inhoud);
  1276. //api_send_mail($user_info['mail'], $titel, $inhoud);
  1277. }
  1278. /*
  1279. ============================================================================================
  1280. RESERVATION
  1281. ============================================================================================
  1282. */
  1283. function check_date_subscription($reservation_id, $start_at, $end_at) {
  1284. $reservation_id = Database::escape_string($reservation_id);
  1285. $start_at = Database::escape_string($start_at);
  1286. $end_at = Database::escape_string($end_at);
  1287. $sql = "SELECT id, start_at, end_at FROM ".Rsys :: getTable('reservation')."
  1288. WHERE start_at > '".$start_at."' AND id='".$reservation_id."' ";
  1289. $result = Database::query($sql);
  1290. if (Database::num_rows($result) != 0){
  1291. $result2 = Database::fetch_array($result);
  1292. $GLOBALS['start_date'] = $result2[1];
  1293. $GLOBALS['end_date'] = $result2[2];
  1294. return 1;
  1295. }
  1296. $sql = "SELECT id, start_at, end_at FROM ".Rsys :: getTable('reservation')."
  1297. WHERE end_at < '".$end_at."' AND id='".$reservation_id."' ";
  1298. $result = Database::query($sql);
  1299. if (Database::num_rows($result) != 0){
  1300. $result2 = Database::fetch_array($result);
  1301. $GLOBALS['start_date'] = $result2[1];
  1302. $GLOBALS['end_date'] = $result2[2];
  1303. return 1;
  1304. }
  1305. $sql = "SELECT * FROM ".Rsys :: getTable('subscription')." WHERE reservation_id='".$reservation_id."' ORDER BY start_at";
  1306. $result = Database::query($sql);
  1307. while ($array = Database::fetch_array($result)) {
  1308. $GLOBALS['start_date'] = $array[4];
  1309. $GLOBALS['end_date'] = $array[5];
  1310. if (Rsys :: mysql_datetime_to_timestamp($array[4]) < Rsys :: mysql_datetime_to_timestamp($start_at) && Rsys :: mysql_datetime_to_timestamp($array[5]) > Rsys :: mysql_datetime_to_timestamp($start_at)) {
  1311. return $array[0];
  1312. }
  1313. if (Rsys :: mysql_datetime_to_timestamp($array[4]) < Rsys :: mysql_datetime_to_timestamp($end_at) && Rsys :: mysql_datetime_to_timestamp($array[5]) > Rsys :: mysql_datetime_to_timestamp($end_at))
  1314. return $array[0];
  1315. }
  1316. $sql = "SELECT dummy, start_at ,end_at FROM ".Rsys :: getTable('subscription')."
  1317. WHERE ((start_at > '".$start_at."' AND
  1318. start_at < '".$end_at."') OR
  1319. (end_at > '".$start_at."' AND
  1320. end_at < '".$end_at."')OR
  1321. (start_at <= '".$start_at."' AND
  1322. end_at >= '".$end_at."')) AND reservation_id='".$reservation_id."' ";
  1323. $result = Database::fetch_array(Database::query($sql));
  1324. if (count($result) != 0){
  1325. $GLOBALS['start_date'] = $result[1];
  1326. $GLOBALS['end_date'] = $result[2];
  1327. return $result[0];
  1328. }
  1329. return 0;
  1330. }
  1331. function check_date_month_calendar($date, $itemid) {
  1332. $itemid = Database::escape_string($itemid);
  1333. $date = Database::escape_string($date);
  1334. $sql = "SELECT id FROM ".Rsys :: getTable('reservation')."
  1335. WHERE ((DATE_FORMAT(start_at, '%Y-%m-%e') = '".$date."' OR DATE_FORMAT(end_at, '%Y-%m-%e') = '".$date."'
  1336. OR (start_at <= '".$date." 00:00:00' AND end_at >= '".$date." 00:00:00' ) OR (start_at>='".$date." 00:00:00' AND start_at<='".$date." 23:59:59')) AND (subscribers < max_users OR timepicker=1)) AND item_id= '".$itemid."'";
  1337. /*
  1338. WHERE item_id='".$itemid."' AND
  1339. ((start_at<='".$date."' AND end_at>='".$date."') OR (start_at>='".$date."' AND start_at<='".$date."'))";
  1340. */
  1341. $result = Database::query($sql);
  1342. if (Database::num_rows($result) != 0)
  1343. return true;
  1344. return false;
  1345. }
  1346. /**
  1347. * With this you make a reservartion
  1348. *
  1349. * @param - int $reservation_id The id off the reservation
  1350. */
  1351. function add_subscription($reservation_id, $user_id, $accepted) {
  1352. $reservation_id = Database::escape_string($reservation_id);
  1353. $user_id = Database::escape_string($user_id);
  1354. $accepted = Database::escape_string($accepted);
  1355. $sql = "SELECT user_id FROM ".Rsys :: getTable("subscription")." WHERE user_id='".$user_id."' AND reservation_id='".$reservation_id."'";
  1356. if (Database::num_rows(Database::query($sql)) == 0) {
  1357. $sql = "INSERT INTO ".Rsys :: getTable("subscription")." (user_id,reservation_id,accepted) VALUES ('".Database::escape_string($user_id)."','".Database::escape_string($reservation_id)."','". ($accepted ? '1' : '0')."')";
  1358. Database::query($sql);
  1359. $sql = "UPDATE ".Rsys :: getTable("reservation")." SET subscribers=subscribers+1 WHERE id='".$reservation_id."'";
  1360. Database::query($sql);
  1361. $sql = "SELECT s.user_id, i.name, r.start_at, r.end_at
  1362. FROM ".Rsys :: getTable("subscription")." s
  1363. INNER JOIN ".Rsys :: getTable("reservation")." r ON s.reservation_id = r.id
  1364. INNER JOIN ".Rsys :: getTable("item")." i ON r.item_id = i.id
  1365. WHERE reservation_id='".$reservation_id."' AND user_id='".$user_id."'";
  1366. $result = Database::store_result(Database::query($sql));
  1367. $user_info = api_get_user_info();
  1368. $titel = str_replace('#ITEM#', $result[0][1], get_lang("ReservationMadeTitle"));
  1369. $inhoud = str_replace('#ITEM#', $result[0][1], str_replace('#START#', $result[0][2], str_replace('#END#', $result[0][3], get_lang("ReservationMadeMessage"))));
  1370. //api_send_mail($user_info['mail'], $titel, $inhoud);
  1371. return 0;
  1372. }
  1373. return 1;
  1374. }
  1375. /**
  1376. * With this you make a reservartion
  1377. *
  1378. * @param - int $reservation_id The id off the reservation
  1379. */
  1380. function add_subscription_timepicker($reservation_id, $user_id, $start_date, $end_date, $accepted, $min, $max) {
  1381. $start_date = Database::escape_string($start_date);
  1382. $end_date = Database::escape_string($end_date);
  1383. if (Rsys :: check_date_subscription($reservation_id, $start_date, $end_date) <> 0)
  1384. return 1;
  1385. if (!($min==0 && $max ==0)){
  1386. if ((Rsys :: mysql_datetime_to_timestamp($end_date)-Rsys :: mysql_datetime_to_timestamp($start_date)) < ($min*60))
  1387. return 2;
  1388. if ((Rsys :: mysql_datetime_to_timestamp($end_date)-Rsys :: mysql_datetime_to_timestamp($start_date)) > ($max*60))
  1389. return 3;
  1390. }
  1391. $sql = "INSERT INTO ".Rsys :: getTable("subscription")." (user_id,reservation_id,accepted,start_at,end_at) VALUES ('".Database::escape_string($user_id)."','".Database::escape_string($reservation_id)."','". ($accepted ? '1' : '0')."','".$start_date."','".$end_date."')";
  1392. Database::query($sql);
  1393. return 0;
  1394. }
  1395. /**
  1396. * Delete subscription
  1397. */
  1398. function delete_subscription($reservation_id, $dummy) {
  1399. $sql = "DELETE FROM ".Rsys :: getTable("subscription")." WHERE dummy='".Database::escape_string($dummy)."'";
  1400. Database::query($sql);
  1401. $sql = "UPDATE ".Rsys :: getTable("reservation")." SET subscribers=subscribers-1 WHERE id='".Database::escape_string($reservation_id)."'";
  1402. Database::query($sql);
  1403. }
  1404. /**
  1405. * Returns the subscriptions of the user for a sortable table based on the params
  1406. *
  1407. * @param - int $from Index of the first item to return.
  1408. * @param - int $per_page The number of items to return
  1409. * @param - int $column The number of the column on which the data should be sorted
  1410. * @param - String $direction In which order should the data be sorted (ASC or DESC)
  1411. * @return - Array The returned rows
  1412. */
  1413. function get_table_subscriptions($from, $per_page, $column, $direction) {
  1414. $from = intval($from);
  1415. $per_page = intval($per_page);
  1416. $column = intval($column);
  1417. if(!in_array($direction, array('ASC','DESC'))) {
  1418. $direction = 'ASC';
  1419. }
  1420. $sql = "SELECT CONCAT(s.reservation_id,'-',s.dummy) AS col0, i.name AS col1, DATE_FORMAT(s.start_at ,'%Y-%m-%d %H:%i') AS col2, DATE_FORMAT(s.end_at ,'%Y-%m-%d %H:%i') AS col3, CONCAT(s.reservation_id,'-',s.dummy) AS col4, DATE_FORMAT(r.start_at ,'%Y-%m-%d %H:%i') , DATE_FORMAT(r.end_at ,'%Y-%m-%d %H:%i') , s.accepted,i.blackout
  1421. FROM ".Rsys :: getTable("subscription")." s
  1422. INNER JOIN ".Rsys :: getTable("reservation")." r ON r.id = s.reservation_id
  1423. INNER JOIN ".Rsys :: getTable("item")." i ON i.id=r.item_id
  1424. WHERE s.user_id = '".api_get_user_id()."'";
  1425. $sql .= "ORDER BY col".$column." ".$direction." LIMIT ".$from.",".$per_page;
  1426. $result = Database::query($sql);
  1427. while ($array = Database::fetch_array($result, 'NUM'))
  1428. { $row = array();
  1429. $row[] = $array[0];
  1430. $row[] = $array[1];
  1431. if($array[2]=='0000-00-00 00:00' && $array[3]=='0000-00-00 00:00')
  1432. {
  1433. $row[] = $array[5];
  1434. $row[] = $array[6];
  1435. }
  1436. else
  1437. {
  1438. $row[] = $array[2];
  1439. $row[] = $array[3];
  1440. }
  1441. if ($array[8]=='1')
  1442. {
  1443. $row[] = get_lang('Blackout');
  1444. }
  1445. else
  1446. {
  1447. if ($array[7]=='1')
  1448. {
  1449. $row[] = get_lang('Yes');
  1450. }else
  1451. {
  1452. $row[] = get_lang('No');
  1453. }
  1454. }
  1455. $row[] = $array[4];
  1456. $arr[] = $row;
  1457. }
  1458. return $arr;
  1459. }
  1460. /**
  1461. * Get number of subscriptions of the user
  1462. *
  1463. * @return - int The amount of itemrights
  1464. */
  1465. function get_num_subscriptions() {
  1466. $sql = "SELECT COUNT(*) FROM ".Rsys :: getTable("subscription")." s
  1467. INNER JOIN ".Rsys :: getTable("reservation")." r ON r.id = s.reservation_id
  1468. INNER JOIN ".Rsys :: getTable("item")." i ON i.id=r.item_id
  1469. WHERE s.user_id = '".api_get_user_id()."'";
  1470. return @ Database::result(Database::query($sql), 0, 0);
  1471. }
  1472. /**
  1473. * Returns $reservation_id=>"START_AT - END_AT"
  1474. */
  1475. /*function get_item_reservations($item_id){
  1476. $sql="SELECT r.id AS reservation_id, r.start_at, r.end_at
  1477. FROM ".Rsys::getTable('reservation')." r
  1478. INNER JOIN ".Rsys::getTable('item')." i ON r.item_id=i.id
  1479. WHERE i.id='".$item_id."'"; // AND r.subscribe_until < NOW() // TODO: subscribe_until controle
  1480. $result=Database::query($sql);
  1481. while($array=Database::fetch_array($result))
  1482. $arr[$array['reservation_id']]=$array['start_at'].' - '.$array['end_at'];
  1483. return $arr;
  1484. }*/
  1485. /**
  1486. * Returns ALL reservations of a certain item with start_date between $from and $till
  1487. *
  1488. * @param - String $from DateTime
  1489. * @param - String $till DateTime
  1490. * @param - int $itemid The itemId
  1491. * @return - Array ['reservations'][RESERVATION_ID]=all info + array with all corresponding subscriptions
  1492. * ['min_start_at'] = the minimal start_at in all reservations (usefull to build table)
  1493. * ['max_end_at'] = the maximal end_at in all reservations (usefull to build table)
  1494. */
  1495. function get_item_reservations($from, $till, $itemid) {
  1496. $itemid = Database::escape_string($itemid);
  1497. $till = Database::escape_string($till);
  1498. $from = Database::escape_string($from);
  1499. $sql = "SELECT r.*,i.name as item_name FROM ".Rsys :: getTable('reservation')." r
  1500. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id=i.id
  1501. LEFT JOIN ".Rsys :: getTable('item_rights')." ir ON ir.item_id=i.id
  1502. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS)." c ON ir.class_id=c.id AND ir.item_id = i.id
  1503. LEFT JOIN ".Database :: get_main_table(TABLE_MAIN_CLASS_USER)." cu ON cu.class_id = c.id
  1504. WHERE r.item_id='".$itemid."' AND (((cu.user_id='".api_get_user_id()."' AND ir.view_right=1) OR 1=". (api_is_platform_admin() ? 1 : 0).") AND
  1505. (r.start_at<='".$from."' AND r.end_at>='".$from."') OR (r.start_at>='".$from."' AND r.start_at<='".$till."')) ORDER BY start_at ASC";
  1506. $result = Database::query($sql);
  1507. $max_start_at = -1;
  1508. $min_end_at = -1;
  1509. $ids = '';
  1510. $from_stamp = Rsys :: mysql_datetime_to_timestamp($from);
  1511. $till_stamp = Rsys :: mysql_datetime_to_timestamp($till);
  1512. if (Database::num_rows($result) == 0)
  1513. return false;
  1514. while ($array = Database::fetch_array($result)) {
  1515. $ids .= $array['id'].',';
  1516. $current_start_at = Rsys :: mysql_datetime_to_timestamp($array['start_at']);
  1517. $current_end_at = Rsys :: mysql_datetime_to_timestamp($array['end_at']);
  1518. if ($from_stamp > $current_start_at)
  1519. $array['start_at'] = $from;
  1520. $rarr['reservations'][$array['id']]['info'] = $array;
  1521. /*
  1522. if ($max_start_at == -1 || $current_start_at < $rarr['min_start_at'])
  1523. $rarr['min_start_at'] = $current_start_at;
  1524. if ($current_end_at > $rarr['max_end_at'])
  1525. $rarr['max_end_at'] = $current_end_at;*/
  1526. }
  1527. $ids = substr($ids, 0, -1);
  1528. $sql = "SELECT * FROM ".Rsys :: getTable('subscription')." WHERE reservation_id IN (".$ids.") AND (start_at='0000-00-00 00:00:00' OR (start_at<='".$from."' AND end_at>='".$from."') OR (start_at>='".$from."' AND start_at<='".$till."')) ORDER BY start_at ASC";
  1529. $result = Database::query($sql);
  1530. while ($array = Database::fetch_array($result, 'ASSOC')) {
  1531. // echo $array['reservation_id'].': '.$array['start_at'].'-'.$array['end_at'].'<br />';
  1532. if ($rarr['reservations'][$array['reservation_id']]['info']['timepicker']) {
  1533. $current_start_at = Rsys :: mysql_datetime_to_timestamp($array['start_at']);
  1534. $current_end_at = Rsys :: mysql_datetime_to_timestamp($array['end_at']);
  1535. if ($current_start_at < $from_stamp) //&& $current_end_at>=$from_stamp) || ($current_start_at>=$from_stamp && $current_start_at<=$till_stamp)))
  1536. $array['start_at'] = $from;
  1537. if ($current_end_at > $till_stamp)
  1538. $array['end_at'] = $till;
  1539. }
  1540. $rarr['reservations'][$array['reservation_id']]['subscriptions'][] = $array;
  1541. }
  1542. return $rarr;
  1543. }
  1544. /**
  1545. * Returns $reservation_id=>"START_AT - END_AT"
  1546. */
  1547. function get_item_subfiltered_reservations($item_id) {
  1548. $itemid = Database::escape_string($itemid);
  1549. $sql = "SELECT r.id AS reservation_id, r.start_at, r.end_at
  1550. FROM ".Rsys :: getTable('reservation')." r
  1551. INNER JOIN ".Rsys :: getTable('item')." i ON r.item_id=i.id
  1552. WHERE r.id NOT IN (SELECT s.reservation_id FROM ".Rsys :: getTable('subscription')." s WHERE r.id=s.reservation_id AND s.user_id='".api_get_user_id()."') AND i.id='".$item_id."'"; // AND r.subscribe_until < NOW() // TODO: subscribe_until controle
  1553. $result = Database::query($sql);
  1554. while ($array = Database::fetch_array($result))
  1555. $arr[$array['reservation_id']] = $array['start_at'].' - '.$array['end_at'];
  1556. return $arr;
  1557. }
  1558. /**
  1559. * Returns ALL subscriptions between $from and $till
  1560. *
  1561. * @param - String $from DateTime
  1562. * @param - String $till DateTime
  1563. */
  1564. function get_subscriptions($from, $till) {
  1565. $till = Database::escape_string($till);
  1566. $from = Database::escape_string($from);
  1567. // TODO: only return for current user...
  1568. $sql = "SELECT r.*,s.start_at AS tp_start,s.end_at AS tp_end,s.accepted FROM ".Rsys :: getTable('subscription')." s INNER JOIN ".Rsys :: getTable('reservation')." r ON s.reservation_id = r.id WHERE ((r.timepicker=0 AND r.start_at>='".$from."' AND r.end_at<='".$till."') OR (s.start_at>='".$from."' AND s.end_at<='".$till."'))";
  1569. $result = Database::query($sql);
  1570. while ($array = Database::fetch_array($result)) {
  1571. $arr[] = $array;
  1572. if ($arr['timepicker'] == 1) {
  1573. $arr['start_at'] = $arr['tp_start'];
  1574. $arr['end_at'] = $arr['tp_end'];
  1575. }
  1576. }
  1577. return $arr;
  1578. }
  1579. function get_item_id($item_name)
  1580. {
  1581. $item_name = Database::escape_string($item_name);
  1582. $sql = "SELECT id FROM ".Rsys :: getTable('item')." WHERE name='".$item_name."'";
  1583. $result = Database::query($sql);
  1584. $result_array = Database::fetch_array($result);
  1585. return $result_array['id'];
  1586. }
  1587. }
  1588. $language_file = 'reservation';
  1589. $cidReset = true;
  1590. require_once '../inc/global.inc.php';
  1591. Rsys :: init();
  1592. require_once 'rcalendar.php';