PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/admin/cal.php

https://bitbucket.org/d3bugg3r/shiftsystem
PHP | 372 lines | 347 code | 20 blank | 5 comment | 73 complexity | 3d10f563ca31187c663691ce91608174 MD5 | raw file
  1. <?
  2. if ($w_id = $_GET['rem_diff']) { //here we set the week back to the default structure
  3. mysql_query("UPDATE weeks SET standard='1' WHERE ID='$w_id'"); //set the week to standard
  4. mysql_query("DELETE FROM diff_weeks WHERE week_id='$w_id'"); //Remove all the entries for the custom week
  5. }
  6. elseif (($w_id = $_GET['add_diff']) || ($w_id=$_GET['edit_diff'])) {
  7. $week = mysql_query("SELECT * FROM weeks WHERE ID='$w_id'");
  8. $week = mysql_fetch_array($week);
  9. echo "<br /><h1>Editing weekly structure for week beginning $week[1]</h1>";
  10. echo "<h3>Choose how many workers can be scheduled for this week. A '0' value will mean no shifts available that hour</h3>";
  11. //now we will see if this week already has a custom structure
  12. $wq = mysql_query("SELECT * FROM diff_weeks WHERE week_id='$w_id'");
  13. if (mysql_num_rows($wq)==0) {//this table does not have a custom structure, we need to create a framework
  14. include ('./includes/create.php');
  15. mysql_query("$create");
  16. echo mysql_error();
  17. }
  18. //Now we must create a table allowing the admin to set which hours are available for this week
  19. echo "<form method='post' action='?p=admin&a=set'><table id='weekly'>
  20. <tr class='odd'>
  21. <td>
  22. </td>
  23. <td>
  24. Monday
  25. </td>
  26. <td>
  27. Tuesday
  28. </td>
  29. <td>
  30. Wednesday
  31. </td>
  32. <td>
  33. Thursday
  34. </td>
  35. <td>
  36. Friday
  37. </td>
  38. <td>
  39. Saturday
  40. </td>
  41. <td>
  42. Sunday
  43. </td>
  44. </tr>"; //This has created the top row of the table
  45. $h = 0;
  46. $tab=1; //used for setting the tab index
  47. while ($h <= 23) {
  48. if ($h&1) $odd = true; else $odd=false;
  49. $h2 = $h+1;
  50. if ($h<9) $h_str = "0$h:00-0$h2:00";
  51. elseif ($h==9) $h_str = "0$h:00-$h2:00";
  52. elseif (($h>9)&&($h<23)) $h_str = "$h:00-$h2:00";
  53. else $h_str = "23:00-00:00";
  54. if ($odd) {
  55. echo "<tr class='odd'>";
  56. }
  57. else {
  58. echo "<tr class='even'>";
  59. }
  60. echo "<td class='hour'>
  61. $h_str
  62. </td>";
  63. $d=1;
  64. $t=$tab;
  65. while ($d<8) {
  66. $r = mysql_query("SELECT * FROM diff_weeks WHERE week_id='$w_id' AND day='$d' AND hour='$h'");
  67. $r = mysql_fetch_array($r);
  68. echo "<td class='input'>
  69. <input type='text' class='hour' name='$r[0]' value='$r[4]' tabindex='$t' />
  70. </td>";
  71. $d++;
  72. $t=$t+24;
  73. }
  74. $h++;
  75. $tab++;
  76. echo "</tr>";
  77. }
  78. echo "<tr>
  79. <td colspan='8'>
  80. <input type='hidden' name='type' value='non' />
  81. <input type='hidden' name='w_id' value='$w_id' />
  82. <input type='submit' value='Submit Changes' />
  83. </td>
  84. </tr>";
  85. die();
  86. }
  87. ?>
  88. <h3>Shift Management</h3>
  89. <a href="?p=admin&a=cal&c=1">Set Weekly Hours</a> | <a href="?p=admin&a=cal&c=2">Activate Weeks for Scheduling</a> | <a href="?p=admin&a=cal&c=3">Schedule Users</a>
  90. <?
  91. if ($_GET['c']=="1") {
  92. echo "<h3>Choose how many workers can be scheduled for each standard week. A '0' value will mean no shifts available that hour</h3>";
  93. echo "<h4>This will not affect weeks already active</h4>";
  94. //Now we must create a table allowing the admin to set which hours are available as standard each week
  95. echo "<form method='post' action='?p=admin&a=set'><table id='weekly'>
  96. <tr class='odd'>
  97. <td>
  98. </td>
  99. <td>
  100. Monday
  101. </td>
  102. <td>
  103. Tuesday
  104. </td>
  105. <td>
  106. Wednesday
  107. </td>
  108. <td>
  109. Thursday
  110. </td>
  111. <td>
  112. Friday
  113. </td>
  114. <td>
  115. Saturday
  116. </td>
  117. <td>
  118. Sunday
  119. </td>
  120. </tr>"; //This has created the top row of the table
  121. $h = 0;
  122. $tab=1; //used for setting the tab index
  123. while ($h <= 23) {
  124. if ($h&1) $odd = true; else $odd=false;
  125. $h2 = $h+1;
  126. if ($h<9) $h_str = "0$h:00-0$h2:00";
  127. elseif ($h==9) $h_str = "0$h:00-$h2:00";
  128. elseif (($h>9)&&($h<23)) $h_str = "$h:00-$h2:00";
  129. else $h_str = "23:00-00:00";
  130. if ($odd) {
  131. echo "<tr class='odd'>";
  132. }
  133. else {
  134. echo "<tr class='even'>";
  135. }
  136. echo "<td class='hour'>
  137. $h_str
  138. </td>";
  139. $d=1;
  140. $t=$tab;
  141. while ($d<8) {
  142. $r = mysql_query("SELECT * FROM week WHERE Day='$d' AND Hour='$h'");
  143. $r = mysql_fetch_array($r);
  144. echo "<td class='input'>
  145. <input type='text' class='hour' name='$r[0]' value='$r[3]' tabindex='$t' />
  146. </td>";
  147. $d++;
  148. $t=$t+24;
  149. }
  150. $h++;
  151. $tab++;
  152. echo "</tr>";
  153. }
  154. echo "<tr>
  155. <td colspan='8'>
  156. <input type='hidden' name='type' value='standard' />
  157. <input type='submit' value='Submit Changes' />
  158. </td>
  159. </tr>";
  160. }
  161. if ($_GET['c']=="2") {
  162. echo "<h4>Here you can choose weeks that can be viewed by users</h4>";
  163. //This will make the current and next year become available automatically for viewing
  164. echo "<form method='post' action='?p=admin&a=cal&c=2'>";
  165. echo "Select a Year to Edit: <select name='year'>";
  166. $current = date("Y");
  167. echo "<option value='$current' selected='selected'>$current</option>";
  168. $next = date("Y", strtotime("next year"));
  169. echo "<option name='$next'>$next</option>";
  170. echo "</select>";
  171. echo "<input type='submit' value='Select Year' /><br />";
  172. if ($y = $_POST['year']){ //the year form has been submitted now we will select the month to edit
  173. if (date("Y") != $y) { //A different year is selected
  174. $month = strtotime("01-01-$y");
  175. } else { $month=strtotime("last month"); } //gives a timestamp for the previous month
  176. echo "Select a Month to Edit: <select name='month'>";
  177. $i = 0;
  178. while (date("Y",$month)==escape($_POST['year'])) { //while the loop is in the selected year
  179. $i = $i+1;
  180. $month_num = date("n", $month);
  181. $month_name = date("F", $month);
  182. echo "<option name='$month_num'";
  183. if ($i==2) echo " selected='selected'";
  184. echo ">$month_name</option>";
  185. $month = strtotime("next month", $month);
  186. }
  187. echo "<input type='submit' value='Select Month' /></form>";
  188. }
  189. if ($begin = escape($_POST['wb'])) { //A week has been edited
  190. if (escape($_POST['active']) == "Yes") $active=1;
  191. else $active=0;
  192. $r = mysql_query("SELECT * from weeks WHERE begin='$begin'");
  193. if (mysql_num_rows($r)==0) { //The week doesn't exist yet, so we create it
  194. mysql_query("INSERT INTO weeks VALUES('','$begin', '0', '1')");
  195. $r = mysql_query("SELECT * from weeks WHERE begin='$begin'"); //repopulate $r so the script can continue
  196. }
  197. $r = mysql_fetch_row($r);
  198. $week_id = $r[0];
  199. $standard = $r[3];
  200. $prev_a = $r[2]; //the previous active setting
  201. if (($prev_a=="1") && ($active==0)) { //this means the week was active, but is not any longer, so we must remove all the shifts from that week
  202. $i=0;
  203. $begin = strtotime($begin);
  204. while ($i<=6) { //we will now cycle through the days of this week, removing all the shifts for each day
  205. $rem = strtotime("+$i days", $begin);
  206. $day = date("j", $rem); //the day value for this day
  207. $month = date("n", $rem);
  208. $year = date("Y", $rem);
  209. mysql_query("DELETE FROM hours WHERE day='$day' AND month='$month' AND year='$year'");
  210. $i++;
  211. }
  212. }
  213. if (($prev_a=="0") && ($active=1)) { //this means the week is becoming active, so we need to add the shifts to the database
  214. $begin = strtotime($begin);
  215. $i = 0;
  216. while ($i<=6) { //cycling through the days of the week
  217. $add = strtotime("+$i days", $begin);
  218. $day = date("j", $add); //the day value for this day
  219. $day_num = date("N", $add);
  220. $month = date("n", $add);
  221. $year = date("Y", $add);
  222. if ($standard == 1) {
  223. $hours = mysql_query("SELECT * FROM week WHERE Day='$day_num'");
  224. while ($hour_row = mysql_fetch_row($hours)) { //Here we retrieve the data from the week structure and add the correct amount of shifts to each hour for the day
  225. $c = 1;
  226. while ($c<=$hour_row[3]) {
  227. mysql_query("INSERT INTO hours VALUES('','$year','$month','$day','$hour_row[2]','0')");
  228. $c++;
  229. }
  230. }
  231. }
  232. else {
  233. $hours = mysql_query("SELECT * FROM diff_weeks WHERE week_id='$week_id' AND Day='$day_num'");
  234. while ($hour_row = mysql_fetch_row($hours)) { //Here we retrieve the data from the week structure and add the correct amount of shifts to each hour for the day
  235. $c = 1;
  236. while ($c<=$hour_row[4]) {
  237. mysql_query("INSERT INTO hours VALUES('','$year','$month','$day','$hour_row[3]','0')");
  238. $c++;
  239. }
  240. }
  241. }
  242. $i++;
  243. }
  244. }
  245. $week_id = $r[0]; //ID of the selected week
  246. mysql_query("UPDATE weeks SET active='$active', standard='$standard' WHERE ID='$week_id'");
  247. if ($standard == 0) { //the admin has chosen to have a non-standard working week
  248. }
  249. }
  250. if ($m = escape($_POST['month'])) { //A month has been selected for viewing
  251. //Now we want a list of the weeks starting in the selected month, with an option to activate/deactivate them and select a different week to the standard setup
  252. $monday = strtotime("01-$m-$y"); //this gives the first day of the selected month
  253. while (date("D", $monday) != "Mon") { //this will cycle until it finds a monday
  254. $monday = strtotime("+1 day", $monday);
  255. }
  256. echo "<table id='admin'>
  257. <tr>
  258. <td>
  259. Week beginning
  260. </td>
  261. <td>
  262. Activated
  263. </td>
  264. <td>
  265. Different Shift Pattern
  266. </td>
  267. <td>
  268. </td>
  269. </tr>";
  270. while (date("F", $monday) == $m) {//while we are in the selected month
  271. $seven = strtotime("-7 days");
  272. if ($monday < $seven) { //if the week began more than 7 days ago
  273. $disable = "disabled ='disabled'";
  274. } else {
  275. $disable = "/";
  276. }
  277. $wb = date("d-m-Y", $monday); //week beginning date string
  278. $q = mysql_query("SELECT * from weeks WHERE begin='$wb'");
  279. if ($r = mysql_fetch_array($q)) {
  280. $w_id=$r[0];
  281. $active = $r[2];
  282. $standard = $r[3];
  283. }
  284. else {
  285. mysql_query("INSERT INTO weeks VALUES('','$wb', '0', '1')"); //create the week we are viewing with default attributes
  286. $w_id = mysql_insert_id();
  287. $active = "0";
  288. $standard = "1";
  289. }
  290. echo "<form method='post' action='?p=admin&a=cal&c=2'>";
  291. echo "<input type='hidden' name='week_id' value'$w_id' />";
  292. echo "<input type='hidden' name='month' value='$m' />";
  293. echo "<input type='hidden' name='year' value='$y' />";
  294. echo "<input type='hidden' name='wb' value='$wb' />";
  295. echo "<tr>
  296. <td>
  297. $wb
  298. </td>
  299. <td>
  300. <input type='checkbox' name='active' $disable value='Yes' ";
  301. if ($active==1) echo "checked='yes'";
  302. echo " />
  303. </td>
  304. <td>";
  305. if ($disable == "disabled ='disabled'") {
  306. echo "Cannot edit weeks in the past";
  307. }
  308. elseif ($active ==1) echo "Unable to edit an active week";
  309. elseif ($standard == 0) {
  310. echo "<a href='http://rattypig.co.uk/shifts/index.php?p=admin&a=cal&c=2&edit_diff=$w_id'>Edit Weekly Hours</a>|<a href='http://rattypig.co.uk/shifts/index.php?p=admin&a=cal&c=2&rem_diff=$w_id'>Set to Default Week</a>";
  311. }
  312. else {
  313. echo "<a href='http://rattypig.co.uk/shifts/index.php?p=admin&a=cal&c=2&add_diff=$w_id'>Set Custom Weekly Hours</a>";
  314. }
  315. echo "</td>
  316. <td>
  317. <input type='submit' value='Edit Week' $disable/>
  318. </td>
  319. </tr>
  320. </form>";
  321. $monday = strtotime("+7 days", $monday);
  322. unset($disable);
  323. }
  324. }
  325. }
  326. if ($_GET['c'] == "3") {
  327. if ($_GET['u']=="1") {
  328. $u = $_POST['user'];
  329. $name = mysql_query("SELECT * FROM users WHERE ID='$u'");
  330. $name = mysql_fetch_array($name);
  331. $name = $name[1];
  332. echo "<h2>You are now able to edit the shifts of user '$name' through the main calendar section. Please user the 'Unset' link in the navigation panel to unset this</h2>";
  333. $_SESSION['sched'] = $u;
  334. }
  335. else {
  336. if ($_GET['u'] == "2") {
  337. unset($_SESSION['sched']);
  338. echo "<br /><h1>User Unset</h1><br />";
  339. }
  340. echo "<h2>Please select a user from the list. Once the user is selected you can edit the users shifts as if logged in as them. Please remember to unset this once you are finished</h2>";
  341. echo "<form action='?p=admin&a=cal&c=3&u=1' method='post'>";
  342. $users = mysql_query("SELECT * FROM users WHERE 1=1");
  343. echo "<select name='user'>";
  344. while ($user_row = mysql_fetch_row($users)) {
  345. $u = $user_row[1];
  346. $u_id = $user_row[0];
  347. echo "<option value='$u_id'>$u</option>";
  348. }
  349. echo "</select>";
  350. echo "<input type='submit' value='Select User' />";
  351. }
  352. }