/controller/maintain/grants.php
PHP | 128 lines | 89 code | 19 blank | 20 comment | 21 complexity | 6379d440574c11a2a4f9628941cd3de2 MD5 | raw file
Possible License(s): GPL-3.0, MIT
- <?php if (! constant("INSYSTEM")) { echo "Permission denied"; exit; } ?>
- <?php
- /*
- hitta.sverok.se site code
- http://hitta.sverok.se
-
- Copyright (c) 2010 Kriss Andsten
- Dual licensed under the MIT and GPL licenses.
- http://hitta.sverok.se/License
- */
- global $settings, $site;
- assertLogin();
- assertMaintainerPermission();
- if (isset($site['requestPath'][2])) {
- // We get post, we save.
- if(count($_POST)) {
- $fieldnames = array('attended','report','approved');
- // Since we will be updating several tables we need to map the field to the table.
- $table_field_table = array('attended' => 'event', 'report' => 'event', 'approved' => 'event_grant');
- $updatery = array();
- $grants = array();
- foreach($_POST as $key => $value) {
- list($field, $id, $grantid) = explode('-', $key);
- if(in_array($field, $fieldnames) && $value != '' && is_numeric($id) && (is_null($grantid) || is_numeric($grantid))) {
- if($field == 'approved') {
- if($value == 'approved') {
- $value = 'yes';
- } else {
- $value = 'no';
- }
- if(!is_null($grantid)) {
- $grants[$id][] = $grantid;
- }
- } // if $field == approved
- $tf = $table_field_table[$field] . '.' . $field;
- $updatery[$id][] = "$tf=" . $dbh->quote($value);
- }
- } // foreach
- foreach($updatery as $id => $u) {
- $grcon = '';
- if(isset($grants[$id])) {
- $grcon = " AND event_grant.grantid IN(" . implode(', ', $grants[$id]) . ")";
- }
- $dbh->exec("UPDATE event, event_grant SET " . implode(', ', $u) . " WHERE event_grant.event=event.id AND event.id=$id" . $grcon);
- }
- } // if count $_POST
-
- // // // //
- // Type 1: Show all unapproved grants for an entity.
- $view = new Opt_View('admin/maintain/grants/grant-listing.tpl');
- $view->site = $site;
- $grantView = new Opt_View('admin/maintain/grants/grant-list-entry.tpl');
- $grantView->grantdata = getUnApprovedGrants($site['requestPath'][2]);
- $grantView->site = $site;
- $view->grants = $grantView;
- $view->entityId = $site['requestPath'][2];
- } else {
- // // // //
- // Type 2: Show all associations with unapproved grants.
- $view = new Opt_View('admin/maintain/grants/entity-listing.tpl');
- $view->site = $site;
- $assView = new Opt_View('admin/maintain/grants/entity-list-entry.tpl');
- $assView->associationdata = getAssociationsWithUnApprovedGrants();
- $assView->site = $site;
- $view->associations = $assView;
- }
-
- $view->page = array(title => 'Godk?nn tr?ffbidrag');
- $view->menu = new Opt_View('support/menu.tpl');
- $view->menu->items = getMenuItems('main');
-
- $output = new Opt_Output_Http;
- $output->setContentType(Opt_Output_Http::HTML, 'utf-8');
- $output->render($view);
- // // // //
- // Functions
- function getUnApprovedGrants($id) {
- global $dbh;
- // All ids are numbers. All other input is silly.
- if (!is_numeric($id)) {
- return array();
- }
- // 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.
- $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");
- $q->execute(array($id));
- // Do some post fetch preparation
- $result = array();
- foreach($q->fetchAll(PDO::FETCH_ASSOC) as $row) {
- $row['attended'] = is_null($row['attended']) ? '' : $row['attended'];
- $row['approved'] = ($row['approved'] == 'not processed' || $row['approved'] == 'no') ? 0 : 1;
- $result[$row['id']][] = $row;
- }
- // Group all grants under one event.
- foreach($result as $k => $v) {
- $grants = array();
- $first = $v[0];
- foreach($v as $x) {
- $grants[] = array('id' => $x['grantid'], 'name' => $x['grantname']);
- }
- unset($first['grantid'], $first['grantname']);
- $first['grants'] = $grants;
- $result[$k] = $first;
- }
-
- return array_values($result);
- }
- function getAssociationsWithUnApprovedGrants() {
- global $dbh;
-
- $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");
- $q->execute();
- return $q->fetchAll(PDO::FETCH_ASSOC);
- }
- ?>