PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/controller/maintain/grants.php

https://bitbucket.org/kandsten/hitta.sverok.se
PHP | 128 lines | 89 code | 19 blank | 20 comment | 21 complexity | 6379d440574c11a2a4f9628941cd3de2 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. // We get post, we save.
  15. if(count($_POST)) {
  16. $fieldnames = array('attended','report','approved');
  17. // Since we will be updating several tables we need to map the field to the table.
  18. $table_field_table = array('attended' => 'event', 'report' => 'event', 'approved' => 'event_grant');
  19. $updatery = array();
  20. $grants = array();
  21. foreach($_POST as $key => $value) {
  22. list($field, $id, $grantid) = explode('-', $key);
  23. if(in_array($field, $fieldnames) && $value != '' && is_numeric($id) && (is_null($grantid) || is_numeric($grantid))) {
  24. if($field == 'approved') {
  25. if($value == 'approved') {
  26. $value = 'yes';
  27. } else {
  28. $value = 'no';
  29. }
  30. if(!is_null($grantid)) {
  31. $grants[$id][] = $grantid;
  32. }
  33. } // if $field == approved
  34. $tf = $table_field_table[$field] . '.' . $field;
  35. $updatery[$id][] = "$tf=" . $dbh->quote($value);
  36. }
  37. } // foreach
  38. foreach($updatery as $id => $u) {
  39. $grcon = '';
  40. if(isset($grants[$id])) {
  41. $grcon = " AND event_grant.grantid IN(" . implode(', ', $grants[$id]) . ")";
  42. }
  43. $dbh->exec("UPDATE event, event_grant SET " . implode(', ', $u) . " WHERE event_grant.event=event.id AND event.id=$id" . $grcon);
  44. }
  45. } // if count $_POST
  46. // // // //
  47. // Type 1: Show all unapproved grants for an entity.
  48. $view = new Opt_View('admin/maintain/grants/grant-listing.tpl');
  49. $view->site = $site;
  50. $grantView = new Opt_View('admin/maintain/grants/grant-list-entry.tpl');
  51. $grantView->grantdata = getUnApprovedGrants($site['requestPath'][2]);
  52. $grantView->site = $site;
  53. $view->grants = $grantView;
  54. $view->entityId = $site['requestPath'][2];
  55. } else {
  56. // // // //
  57. // Type 2: Show all associations with unapproved grants.
  58. $view = new Opt_View('admin/maintain/grants/entity-listing.tpl');
  59. $view->site = $site;
  60. $assView = new Opt_View('admin/maintain/grants/entity-list-entry.tpl');
  61. $assView->associationdata = getAssociationsWithUnApprovedGrants();
  62. $assView->site = $site;
  63. $view->associations = $assView;
  64. }
  65. $view->page = array(title => 'Godk?nn tr?ffbidrag');
  66. $view->menu = new Opt_View('support/menu.tpl');
  67. $view->menu->items = getMenuItems('main');
  68. $output = new Opt_Output_Http;
  69. $output->setContentType(Opt_Output_Http::HTML, 'utf-8');
  70. $output->render($view);
  71. // // // //
  72. // Functions
  73. function getUnApprovedGrants($id) {
  74. global $dbh;
  75. // All ids are numbers. All other input is silly.
  76. if (!is_numeric($id)) {
  77. return array();
  78. }
  79. // Show all the unapproved and not processed events for $id that have happened and are not Lilla tr?ffbidraget, Stora tr?ffbidraget nor Webannonsering. Order by the end of the event.
  80. $q = $dbh->prepare("SELECT ev.id, en.name as entity_name, ev.name, ev.start, ev.end, ev.attended, ev.report, egr.approved, egr.grantid, gr.grantname FROM event ev, entity en, event_grant egr, grants gr WHERE egr.approved <> 'yes' AND en.id=ev.entity AND egr.event=ev.id AND ev.end < CURDATE() AND egr.grantid=gr.id AND egr.grantid > 3 AND en.id=? ORDER BY ev.end");
  81. $q->execute(array($id));
  82. // Do some post fetch preparation
  83. $result = array();
  84. foreach($q->fetchAll(PDO::FETCH_ASSOC) as $row) {
  85. $row['attended'] = is_null($row['attended']) ? '' : $row['attended'];
  86. $row['approved'] = ($row['approved'] == 'not processed' || $row['approved'] == 'no') ? 0 : 1;
  87. $result[$row['id']][] = $row;
  88. }
  89. // Group all grants under one event.
  90. foreach($result as $k => $v) {
  91. $grants = array();
  92. $first = $v[0];
  93. foreach($v as $x) {
  94. $grants[] = array('id' => $x['grantid'], 'name' => $x['grantname']);
  95. }
  96. unset($first['grantid'], $first['grantname']);
  97. $first['grants'] = $grants;
  98. $result[$k] = $first;
  99. }
  100. return array_values($result);
  101. }
  102. function getAssociationsWithUnApprovedGrants() {
  103. global $dbh;
  104. $q = $dbh->prepare("SELECT en.id, en.name AS entity_name, COUNT(ev.id) AS number FROM event ev, entity en, event_grant egr WHERE egr.approved <> 'yes' AND en.id=ev.entity AND egr.event=ev.id AND egr.grantid = 4 AND ev.end < CURDATE() GROUP BY entity_name ORDER BY entity_name");
  105. $q->execute();
  106. return $q->fetchAll(PDO::FETCH_ASSOC);
  107. }
  108. ?>