PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/calendar.php

https://bitbucket.org/d3bugg3r/shiftsystem
PHP | 173 lines | 157 code | 12 blank | 4 comment | 31 complexity | 6ba2181cd73ff26eff70898c318409cc MD5 | raw file
  1. <?
  2. if (isset($_GET['date'])) {
  3. $new = escape($_GET['date']);
  4. $_SESSION['nav_date'] = strtotime($new);
  5. }
  6. $month = date("01-m-Y", $_SESSION['nav_date']);
  7. $d = date("d", $_SESSION['nav_date']);
  8. $m = date("m", $_SESSION['nav_date']);
  9. $y = date("Y", $_SESSION['nav_date']); //these are the numerical values
  10. $mon = date("F", $_SESSION['nav_date']);
  11. if (isset($_SESSION['sched'])) { $c_user = $_SESSION['sched']; }
  12. else { $c_user=$g_user; }
  13. $dmonth = strtotime($month); //returns a timestamp for the first day of the selected month
  14. ?>
  15. <div id="cal_wrapper">
  16. <?
  17. echo "<h2>Displaying days for $mon $y</h2><br />";
  18. ?>
  19. <table id="month">
  20. <tr>
  21. <?
  22. while (date("n", $dmonth) == $m) { //while the loop is in the correct month, this will print the days along the top of the table
  23. echo "<td>";
  24. echo date("j", $dmonth);
  25. echo "</td>";
  26. $dmonth = $dmonth + 86400; //adds 1 day to the datestamp
  27. }
  28. $dmonth = strtotime($month); //reset $dmonth
  29. ?>
  30. </tr>
  31. <tr>
  32. <?
  33. //we shall now populate the second row with the correct days, seeing if there are any unassigned hours available or if the user is already scheduled to work that date
  34. while (date("n", $dmonth) == $m) {
  35. $d = date("j", $dmonth);
  36. $result = mysql_query("SELECT * FROM hours WHERE worker='$c_user' AND day='$d' AND month='$m' AND year='$y'"); // this will select all entries from 'hours' where 'worker' is the same as the user's ID on this day
  37. if (mysql_num_rows($result) != 0){ //this means the user is working this day
  38. echo "<td class='working'>
  39. <a class='cell' href='?p=cal&month=$m&year=$y&day=$d'></a>
  40. </td>";
  41. }
  42. else {
  43. $result = mysql_query("SELECT * FROM hours WHERE worker='0' AND day='$d' AND month='$m' AND year='$y'"); // this will select all entries for this day where there is no user assigned
  44. if (mysql_num_rows($result) != 0){
  45. echo "<td class='available'>
  46. <a class='cell' href='?p=cal&month=$m&year=$y&day=$d'></a>
  47. </td>";
  48. }
  49. else //getting to this point means there are no shifts available to this user on this day
  50. echo "<td class='none'>";
  51. }
  52. echo "</td>";
  53. $dmonth = $dmonth + 86400; //adds 1 day to the datestamp
  54. }
  55. ?>
  56. </tr>
  57. </table>
  58. <br />
  59. <br />
  60. <table id="key">
  61. <tr>
  62. <td id="n"></td><td>No shifts Available</td><td id="w"></td><td>Shifts assigned</td><td id="a"></td><td>Shifts available</td>
  63. </tr>
  64. <!--Here the page will end until the user selects a day to view-->
  65. <?
  66. if (isset($_GET['day'])) {
  67. $d = escape($_GET['day']);
  68. $date = $d . "-" . $m . "-" . $y;
  69. echo "<br /><h3>Details for $date</h3>";
  70. echo "<table id='hours'>";
  71. echo "<tr>
  72. <td class='title'>
  73. Hour
  74. </td>
  75. <td class='title'>
  76. Available/Assigned
  77. </td>
  78. <td class='title'>
  79. Others Working
  80. </td>
  81. </tr>";
  82. //this table will display each hour in the day, showing if it is available to work or if the user is already working that hour
  83. $h = 0;
  84. while ($h <= 23) {
  85. $h2 = $h+1;
  86. if ($h<9) $h_str = "0$h:00-0$h2:00";
  87. elseif ($h==9) $h_str = "0$h:00-$h2:00";
  88. elseif (($h>9)&&($h<23)) $h_str = "$h:00-$h2:00";
  89. else $h_str = "23:00-00:00";
  90. echo "<tr>
  91. <td>
  92. $h_str
  93. </td>";
  94. $h_result = mysql_query("SELECT * FROM hours WHERE worker='$c_user' AND day='$d' AND month='$m' AND year='$y' AND hour='$h'");
  95. if (mysql_num_rows($h_result) == 0) { //This means that the user is not working this hour
  96. $hr2 = mysql_query("SELECT * FROM hours WHERE worker='0' AND day='$d' AND month='$m' AND year='$y' AND hour='$h'");
  97. if (mysql_num_rows($hr2) != 0) { //This means that this hour is available to work
  98. $h_a = mysql_fetch_array($hr2);
  99. $h_id = $h_a[0];
  100. echo "<td class='available'>";
  101. echo "<form name='$h_id' method='post' action='?p=assign'>
  102. <input type='hidden' name='c_id' value='".$c_user."' />
  103. <input type='hidden' name='id' value='".$h_id."' />
  104. <input type='hidden' name='return' value='month=$m&year=$y&day=$d' />";
  105. echo "<input type='submit' class='available_button' value='' />";
  106. echo "</form>";
  107. } else { //the hour is unavailable
  108. echo "<td class='none'>";
  109. }
  110. } else { //The user is working this hour
  111. $h_a = mysql_fetch_array($h_result);
  112. $h_id = $h_a[0];
  113. echo "<td class='working'>";
  114. echo "<form name='$h_id' method='post' action='?p=remove'>
  115. <input type='hidden' name='id' value='$h_id' />
  116. <input type='hidden' name='c_id' value='".$c_user."' />
  117. <input type='hidden' name='return' value='month=$m&year=$y&day=$d' />";
  118. echo "<input type='submit' class='working_button' value='' />";
  119. echo "</form>";
  120. }
  121. echo "</td>";
  122. //now we will populate the final column with the usernames of people already working that hour, if any
  123. echo "<td>";
  124. $h_result = mysql_query("SELECT * FROM hours WHERE day='$d' AND month='$m' AND year='$y' AND hour='$h' AND worker<>'$c_user' AND worker<>'0'");
  125. $c = 0;
  126. while ($h_row = mysql_fetch_row($h_result)) {
  127. if ($c > 0) echo ", ";
  128. $c++;
  129. $w_id = $h_row[5]; //id of the worker for this one
  130. $w = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE ID='$w_id'"));
  131. echo "<span style='color:$w[5]; font-weight:bold;'>$w[1]</span>";
  132. }
  133. $h = $h+1;
  134. }
  135. }
  136. echo "</table>";
  137. // the nav bar wil display each month of the current year with options to go forwards and back a year ?>
  138. <div id="footer_bg">
  139. <div class="footer">
  140. <div align="center" class="month_nav">
  141. <?
  142. $prev_year = date("Y", strtotime("-1 year", $_SESSION['nav_date']));
  143. $next_year = date("Y", strtotime("+1 year", $_SESSION['nav_date']));
  144. echo "<a class='nav' href='index.php?p=cal&date=01-01-$prev_year'>$prev_year &lt;&lt; -</a>"; //this displays a link to the previous year
  145. $i = 1;
  146. while ($i <=12 ){
  147. $str = "01-$i-$y";
  148. $m = date("F", strtotime($str)); //$m is the textual representation of the current month being used
  149. if (date("n", $_SESSION['nav_date']) == $i) {
  150. echo "<i> $m </i>-";
  151. } else {
  152. echo "<a class='nav' href='index.php?p=cal&date=$str'> $m -</a>";
  153. }
  154. $i++;
  155. }
  156. echo "<a class='nav' href='index.php?p=cal&date=01-01-$next_year'> &gt;&gt; $next_year</a>"; //this displays a link to the next year
  157. ?>
  158. </div>
  159. </div>
  160. </div>
  161. </div>