/purpool/wkplaceadmin.php

https://github.com/purpool/purpool · PHP · 285 lines · 160 code · 61 blank · 64 comment · 16 complexity · e9f2c0855c3f5823b338613e3b19d4f8 MD5 · raw file

  1. <?php
  2. #################################################################
  3. # Name: events.php #
  4. # Author: John Kuiphoff #
  5. # Description: Allows users to create events #
  6. #################################################################
  7. // Include configuration file
  8. include_once('config_path.php'); include_once($config_path.'config.php');
  9. // Include common utility library
  10. include_once($DIR['inc'] . 'Utils.class.php');
  11. // Include database package
  12. include_once($DIR['pear'] . 'MDB2.php');
  13. // Include SMARTY templating engine
  14. include_once($DIR['smarty'] . 'Smarty.class.php');
  15. // Initialize database connection
  16. $dbh = Utils::initDB();
  17. // Initialize templating engine
  18. $tpl = Utils::initTPL();$tpl->assign('site_url', $MISC['site_url']);
  19. // Start new session
  20. session_start();
  21. // Validate user (make sure this is the admin user)
  22. $sql = "SELECT isworkplaceadmin FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  23. $row = $dbh->queryRow($sql);
  24. if($row['isworkplaceadmin'] != '1')
  25. {
  26. header("Location: index.php");
  27. }
  28. // Switch state
  29. switch($_GET['state'])
  30. {
  31. # MANAGE ANNOUNCEMENTS
  32. case "manageannouncements":
  33. // Get workplace
  34. $sql = "SELECT workplace FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  35. $row = $dbh->queryRow($sql);
  36. $workplace = $row['workplace'];
  37. // If the submit button has been pressed
  38. if(isset($_POST['submit']))
  39. {
  40. // Check for a startdate
  41. if(empty($_POST['startdate']))
  42. {
  43. $error['startdate'] = 'A startdate is required.';
  44. } else {
  45. $tpl->assign('startdate', $_POST['startdate']);
  46. }
  47. // Check for an enddate
  48. if(empty($_POST['enddate']))
  49. {
  50. $error['enddate'] = 'An enddate is required.';
  51. } else {
  52. $tpl->assign('enddate', $_POST['enddate']);
  53. }
  54. // Check for an announcement
  55. if(empty($_POST['announcement']))
  56. {
  57. $error['announcement'] = 'An announcement is required.';
  58. } else {
  59. $tpl->assign('announcement', $_POST['announcement']);
  60. }
  61. // Clean startdate and enddate (convert from mm-dd-yyyy to yyyy-mm-dd)
  62. $startdate = explode('-', $_POST['startdate']);
  63. $cstartdate = $startdate[2] . '-' . $startdate[0] . '-' . $startdate[1];
  64. $enddate = explode('-', $_POST['enddate']);
  65. $cenddate = $enddate[2] . '-' . $enddate[0] . '-' . $enddate[1];
  66. // E-mail blast
  67. $tpl->assign('emailblast', $_POST['emailblast']);
  68. // If there are no errors
  69. if(sizeof($error) == 0)
  70. {
  71. // Insert message into database
  72. $sql = "INSERT INTO announcements (announcement_id, announcement, startdate, enddate, workplace) VALUES (null, '{$_POST['announcement']}', '$cstartdate', '$cenddate', '$workplace')";
  73. $dbh->query($sql);
  74. // Check for an e-mail blast
  75. if($_POST['emailblast'] == 'y')
  76. {
  77. // Get all users in the workplace
  78. $sql = "SELECT firstname, lastname, email FROM users WHERE hasloggedin = '1' AND workplace = '$workplace' ORDER BY user_id";
  79. $result = $dbh->query($sql);
  80. while($row = $result->fetchRow())
  81. {
  82. // Compose message
  83. mail($row['email'], 'Purpool Announcement', stripslashes($_POST['announcement']), "From: ".$MISC['admin_email']);
  84. }
  85. }
  86. }
  87. // Redirect user
  88. $redirect = 'wkplaceadmin.php?state=manageannouncements&confirmation=addannouncement';
  89. header("Location: $redirect");
  90. // Disconnect from database
  91. $dbh->disconnect();
  92. exit();
  93. }
  94. // Get all announcements for workplace
  95. $sql = "SELECT announcement_id, announcement, startdate, DATE_FORMAT(startdate, '%M %d, %Y') AS cstartdate, DATE_FORMAT(enddate, '%M %d, %Y') AS cenddate FROM announcements WHERE workplace = '$workplace' ORDER BY startdate";
  96. $result = $dbh->query($sql);
  97. while($row = $result->fetchRow())
  98. {
  99. // Create announcements array
  100. $announcements[] = array(
  101. 'announcement_id' => $row['announcement_id'],
  102. 'announcement' => $row['announcement'],
  103. 'startdate' => $row['cstartdate'],
  104. 'enddate' => $row['cenddate']
  105. );
  106. }
  107. $tpl->assign('announcements', $announcements);
  108. // Assign formaction
  109. $formaction = 'wkplaceadmin.php?state=manageannouncements';
  110. $tpl->assign('formaction', $formaction);
  111. // Display Template
  112. $tpl->display('wkplaceadmin-manageannouncements.tpl');
  113. // Disconnect from database
  114. $dbh->disconnect();
  115. exit();
  116. break;
  117. # DELETE ANNOUNCEMENT
  118. case "deleteannouncement":
  119. // Get workplace
  120. $sql = "SELECT workplace FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  121. $row = $dbh->queryRow($sql);
  122. $workplace = $row['workplace'];
  123. // Delete announcement from database
  124. $sql = "DELETE FROM announcements WHERE announcement_id = '{$_GET['announcement']}' AND workplace = '$workplace' LIMIT 1";
  125. $dbh->query($sql);
  126. // Redirect user
  127. $redirect = 'wkplaceadmin.php?state=manageannouncements&confirmation=deleteannouncement';
  128. header("Location: $redirect");
  129. // Disconnect from database
  130. $dbh->disconnect();
  131. exit();
  132. break;
  133. # VIEW USERS
  134. case "viewusers":
  135. // Get workplace
  136. $sql = "SELECT workplace FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  137. $row = $dbh->queryRow($sql);
  138. $workplace = $row['workplace'];
  139. // Get all users
  140. $sql = "SELECT user_id, workplace, firstname, lastname, email, hasloggedin FROM users WHERE workplace = '$workplace' ORDER BY lastname";
  141. $result = $dbh->query($sql);
  142. $counter = 1;
  143. while($row = $result->fetchRow())
  144. {
  145. // Create users array
  146. $users[] = array(
  147. 'user_id' => $row['user_id'],
  148. 'firstname' => $row['firstname'],
  149. 'lastname' => $row['lastname'],
  150. 'email' => $row['email'],
  151. 'hasloggedin' => $row['hasloggedin'],
  152. 'counter' => $counter,
  153. );
  154. $counter++;
  155. }
  156. $tpl->assign('users', $users);
  157. // Display Template
  158. $tpl->display('wkplaceadmin-viewusers.tpl');
  159. // Disconnect from database
  160. $dbh->disconnect();
  161. exit();
  162. break;
  163. # DELETE USER
  164. case "deleteuser":
  165. // Assign warning message
  166. $tpl->assign('warning', 'user');
  167. // Get workplace
  168. $sql = "SELECT workplace FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  169. $row = $dbh->queryRow($sql);
  170. $workplace = $row['workplace'];
  171. // Assign formaction
  172. $formaction = 'wkplaceadmin.php?state=deleteuser&user=' . $_GET['user'];
  173. $tpl->assign('formaction', $formaction);
  174. // If the yes button has been pressed
  175. if(isset($_POST['yes']))
  176. {
  177. // Check to make sure that the user belongs to this workplace
  178. $sql = "SELECT user_id FROM users WHERE user_id = '{$_GET['user']}' AND workplace = '$workplace'";
  179. $row = $dbh->queryRow($sql);
  180. if($row)
  181. {
  182. // Delete the user from the users table
  183. $sql = "DELETE FROM users WHERE user_id = '{$_GET['user']}' LIMIT 1";
  184. $dbh->query($sql);
  185. // Delete the user from the pool members table
  186. $sql = "DELETE FROM poolmembers WHERE user_id = '{$_GET['user']}' LIMIT 1";
  187. $dbh->query($sql);
  188. // Delete the user from the pool passengers table
  189. $sql = "DELETE FROM poolpassengers WHERE user_id = '{$_GET['user']}' LIMIT 1";
  190. $dbh->query($sql);
  191. }
  192. // Redirect user
  193. $redirect = 'wkplaceadmin.php?state=viewusers&confirmation=deleteuser';
  194. header("Location: $redirect");
  195. // Disconnect from database
  196. $dbh->disconnect();
  197. exit();
  198. }
  199. // If the yes button has been pressed
  200. if(isset($_POST['no']))
  201. {
  202. // Redirect user
  203. $redirect = 'wkplaceadmin.php?state=viewusers';
  204. header("Location: $redirect");
  205. // Disconnect from database
  206. $dbh->disconnect();
  207. exit();
  208. }
  209. // Display Template
  210. $tpl->display('deleteconfirm.tpl');
  211. // Disconnect from database
  212. $dbh->disconnect();
  213. exit();
  214. break;
  215. # SHOW OPTIONS
  216. default:
  217. // Display Template
  218. $tpl->display('wkplaceadmin.tpl');
  219. // Disconnect from database
  220. $dbh->disconnect();
  221. exit();
  222. break;
  223. }
  224. ?>