PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/controller/maintain/events.php

https://bitbucket.org/kandsten/hitta.sverok.se
PHP | 243 lines | 165 code | 38 blank | 40 comment | 24 complexity | 5444b18cf7b608d75659cba48922a1a9 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php if (! constant("INSYSTEM")) { echo "Permission denied"; exit; } ?>
  2. <?php
  3. /*
  4. hitta.sverok.se site code
  5. http://hitta.sverok.se
  6. Copyright (c) 2010 Kriss Andsten
  7. Dual licensed under the MIT and GPL licenses.
  8. http://hitta.sverok.se/License
  9. */
  10. global $settings, $site;
  11. assertLogin();
  12. assertMaintainerPermission();
  13. if (isset($site['requestPath'][2])) {
  14. list($id, $bla) = explode('#',$site['requestPath'][2]); // We want the hash for another purpose... ignore it here.
  15. // We get post, we save.
  16. if(count($_POST)) {
  17. // Update grants.
  18. $grantTypes = getGrantTypes();
  19. foreach($grantTypes as &$gr) {
  20. $gr = $gr['id'];
  21. }
  22. $grantIds = array();
  23. $eventId = -1;
  24. foreach($_POST as $key => $value) {
  25. list($name, $eventId, $grantId) = explode('_', $key);
  26. if($name == 'grant' && is_numeric($eventId) && is_numeric($grantId)) {
  27. $grantIds[] = $grantId;
  28. }
  29. }
  30. /* At the moment you can only add new grants, because we want to save the data in the event_grant-connection table. In the future you might be able to remove grants.
  31. $diffIds = array_diff($grantTypes, $grantIds); // Get the ids not present in the input data.
  32. foreach($diffIds as $d) {
  33. // Delete the grants that weren't selected in the form.
  34. $dbh->exec("DELETE FROM event_grant WHERE event=$eventId AND grantid=$d");
  35. }
  36. */
  37. // Create new grant bindings
  38. foreach($grantIds as $g) {
  39. $dbh->exec("INSERT INTO event_grant(event, grantid) VALUES($eventId, $g)");
  40. }
  41. header('Location: ' . '#event_' . $eventId);
  42. } // if count $_POST
  43. // // // //
  44. // Type 1: Show entity information including events.
  45. $view = new Opt_View('admin/maintain/events/event-listing.tpl');
  46. $view->entitydata = maintainGetEntity($id);
  47. $view->site = $site;
  48. // OPT is frustrating at the moment, so I get my revenge by doing really ugly hacks.
  49. // Please change this solution if you know a better one.
  50. $events = array_values(maintainGetEvents($id));
  51. $grantTypes = getGrantTypes();
  52. foreach($events as &$e) {
  53. $newTypes = $grantTypes; // Create a copy. We will add fields to the copy soon.
  54. if(!is_null($e['grants'])) {
  55. foreach($e['grants'] as $g) {
  56. foreach($newTypes as &$gt) { // Add checked if we got the grant.
  57. if($g['id'] == $gt['id']) {
  58. $gt['checked'] = true;
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. $e['editGrantsList'] = $newTypes;
  65. }
  66. $eventView = new Opt_View('admin/maintain/events/event-list-entry.tpl');
  67. $eventView->site = $site;
  68. $eventView->eventdata = $events;
  69. $eventView->eventstats = maintainGetNiceEventStats($id);
  70. $view->events = $eventView;
  71. $view->grantStats = getApprovedGrantSums($id);
  72. $title = "Föreningar";
  73. } else {
  74. // // // //
  75. // Type 2: Show all entities with events.
  76. $view = new Opt_View('admin/maintain/events/entity-listing.tpl');
  77. $view->site = $site;
  78. $assView = new Opt_View('admin/maintain/events/entity-list-entry.tpl');
  79. $assView->associationdata = getEntitiesWithEvents();
  80. $assView->site = $site;
  81. $view->associations = $assView;
  82. $view->grantStats = getApprovedGrantSums();
  83. $title = 'Förening med träffar';
  84. }
  85. $view->page = array(title => $title);
  86. $view->menu = new Opt_View('support/menu.tpl');
  87. $view->menu->items = getMenuItems('main');
  88. $output = new Opt_Output_Http;
  89. $output->setContentType(Opt_Output_Http::HTML, 'utf-8');
  90. $output->render($view);
  91. // // // //
  92. // Functions
  93. // Show an entity in the event listing
  94. function maintainGetEntity($id) {
  95. global $dbh;
  96. // All ids are numbers. All other input is silly.
  97. if (!is_numeric($id)) {
  98. return array();
  99. }
  100. $q = $dbh->prepare("SELECT en.id, en.name as name, en.groupkey, en.email, en.website, en.shortDesc, et.name as type, us.username FROM entity en LEFT JOIN(entity_type et, user us, user_entity ue) ON (et.id=en.etype AND us.id=ue.user AND ue.entity=en.id) WHERE en.id=?");
  101. $q->execute(array($id));
  102. return $q->fetch(PDO::FETCH_ASSOC);
  103. }
  104. // Get the number of not processed grants, grants applied for and the total of events.
  105. function maintainGetNiceEventStats($id) {
  106. global $dbh;
  107. // All ids are numbers. All other input is silly.
  108. if (!is_numeric($id)) {
  109. return array();
  110. }
  111. $q = $dbh->prepare("select ".
  112. "(select count(evgr.approved) from event_grant evgr, event ev where entity=? AND approved='Not processed' AND curdate() > end AND evgr.event=ev.id) as not_processed, ".
  113. "(select count(distinct ev.id) from event ev, grants gr, event_grant evgr where ev.entity=? and ev.id=evgr.event and gr.id=evgr.grantid) as grants, ".
  114. "(select count(*) from event where entity=?) as total");
  115. $q->execute(array($id, $id, $id));
  116. return $q->fetch(PDO::FETCH_ASSOC);
  117. }
  118. // Get events
  119. function maintainGetEvents($id) {
  120. global $dbh;
  121. // All ids are numbers. All other input is silly.
  122. if (!is_numeric($id)) {
  123. return array();
  124. }
  125. $q = $dbh->prepare("SELECT ev.id, ev.created, ev.start, ev.end, ev.name, ev.shortDesc, ev.fullDesc, ev.instanceOf, ev.website, ev.email, ev.phone, ev.attended, ev.report, egr.approved, ev.paid, gr.id as grantid, gr.grantname FROM event ev LEFT JOIN (event_grant egr, grants gr) ON (ev.id=egr.event AND egr.grantid=gr.id) WHERE ev.entity=? ORDER BY ev.end");
  126. $q->execute(array($id));
  127. // An event may have many grants, which gives copies of the event in the outdata. So we reorder that data in a nice fashion.
  128. // I think this loop does things a bit backwards. But it's okay. :)
  129. $data = $q->fetchAll(PDO::FETCH_ASSOC);
  130. $output = array();
  131. foreach($data as $row) {
  132. $x = array('name' => $row['grantname'], 'id' => $row['grantid'], 'approved' => $row['approved']);
  133. if(array_key_exists($row['id'], $output)) {
  134. $output[$row['id']]['grants'][] = $x;
  135. } else {
  136. $row['grants'] = array($x);
  137. unset($row['grantname'], $row['grantid'], $row['approved']);
  138. $output[$row['id']] = $row;
  139. }
  140. }
  141. return $output;
  142. }
  143. // Get all the grant types: id, name
  144. function getGrantTypes() {
  145. global $dbh;
  146. $q = $dbh->prepare("SELECT id, grantname AS name FROM grants");
  147. $q->execute();
  148. return $q->fetchAll(PDO::FETCH_ASSOC);
  149. }
  150. // For the entity listing
  151. function getEntitiesWithEvents() {
  152. global $dbh;
  153. $q = $dbh->prepare("SELECT en.id, en.name AS entity_name, COUNT(ev.id) AS number FROM event ev, entity en WHERE en.id=ev.entity GROUP BY entity_name ORDER BY entity_name");
  154. $q->execute();
  155. return $q->fetchAll(PDO::FETCH_ASSOC);
  156. }
  157. // How much money has the association had approved events for?
  158. // Input is not mandatory.
  159. function getApprovedGrantSums($id=NULL) {
  160. global $dbh;
  161. // All ids are numbers. All other input is silly.
  162. if (!is_numeric($id) && !is_null($id)) {
  163. return array();
  164. }
  165. $qs = "SELECT YEAR(ev.end) as year, ev.attended, evgr.grantid, en.id as enid FROM event ev, entity en, event_grant evgr WHERE ev.entity=en.id AND ev.id=evgr.event AND (evgr.approved='Yes' OR evgr.grantid = 1) AND evgr.grantid IN (1,2,4)";
  166. if(!is_null($id)) {
  167. $qs .= ' AND en.id=?';
  168. }
  169. $q = $dbh->prepare($qs);
  170. if(!is_null($id)) {
  171. $q->execute(array($id));
  172. } else {
  173. $q->execute();
  174. }
  175. $result = array();
  176. $data = $q->fetchAll(PDO::FETCH_ASSOC);
  177. // These are basically the rules for the grants.
  178. foreach($data as $row) {
  179. $x = array('year' => $row['year']);
  180. switch($row['grantid']) {
  181. case 1 : $x['grant'] = 200;
  182. break;
  183. case 2 : $x['grant'] = (40 * $row['attended']) > 8000 ? 8000 : (40 * $row['attended']); // Max 8000 kr per event
  184. break;
  185. case 4 : $x['grant'] = (20 * $row['attended']);
  186. break;
  187. }
  188. $result[$row['enid']][] = $x;
  189. }
  190. $years = array();
  191. foreach($result as $enid => $arr) {
  192. $total = array();
  193. foreach($arr as $x) {
  194. $total[$x['year']] += $x['grant'];
  195. }
  196. foreach($total as $y => $g) {
  197. if($g > 8000 && $y == 2011) {
  198. $total[$y] = 8000;
  199. }
  200. $years[$y] += $total[$y];
  201. }
  202. }
  203. return $years;
  204. }