Formatting
[memberdb.git] / include / events.php
index 7a56de6..ff1ae9f 100644 (file)
 <?php
 
-$SQL_CREATE_EVENTS = sprintf('
-    CREATE TABLE IF NOT EXISTS `%1$s` (
-      `id` int(10) unsigned NOT NULL auto_increment,
-      `event_date` date NOT NULL,
-      `event_type` enum(\'joined\',\'changed\',\'leaved\') NOT NULL,
-      `member_id` int(10) unsigned NOT NULL,
-      `member_type` enum(\'regular\',\'patron\',\'honorary\') NULL,
-      `fee` DECIMAL(10,2) NULL,
-      `payment_interval` enum(\'monthly\', \'quarterly\', \'halfyearly\', \'yearly\') NULL,
-      `created_at` datetime NOT NULL,
-      `modified_at` datetime NOT NULL,
-      PRIMARY KEY  (`id`),
-      UNIQUE KEY `member_id_event_date` (`member_id`, `event_date`)
-    ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
-    ', DB_TABLE_EVENTS, DB_CHARSET);
-
 $EVENT_TYPES = array(
-    'joined'    => 'Eintritt',
-    'changed'   => 'Änderung',
-    'leaved'    => 'Austritt'
+    'joined'  => 'Eintritt',
+    'changed' => 'Änderung',
+    'leaved'  => 'Austritt'
 );
 
 $PAYMENT_INTERVALS = array(
-    'monthly'   => 'monatlich',
-    'quarterly' => 'vierteljährlich',
-    'halfyearly'=> 'halbjährlich',
-    'yearly'    => 'jährlich'
+    'monthly'    => 'monatlich',
+    'quarterly'  => 'vierteljährlich',
+    'halfyearly' => 'halbjährlich',
+    'yearly'     => 'jährlich'
 );
 
 $MEMBER_TYPES = array(
-    'regular'   => 'Ordentliches Mitglied',
-    'patron'    => 'Fördermitglied',
-    'honorary'  => 'Ehrenmitglied'
+    'regular'  => 'Ordentliches Mitglied',
+    'patron'   => 'Fördermitglied',
+    'honorary' => 'Ehrenmitglied'
 );
 
-/* DB functions {{{ */
-
-function db_create_event($event) {
+// DB functions
+function db_create_event($event)
+{
     unset($event['id']);
-    $event['event_date']    = db_unixtime2datetime(ui_date2unixtime($event['event_date']));
-    $event['fee']                  = ui_money2float($event['fee']);
-    $event['created_at']    = db_unixtime2datetime(time());
-    $event['modified_at']   = db_unixtime2datetime(time());
-    return (db_insert_single(DB_TABLE_EVENTS, $event));
+    $event['event_date'] = db_unixtime2datetime(ui_date2unixtime($event['event_date']));
+    $event['fee'] = ui_money2float($event['fee']);
+    $event['created_at'] = db_unixtime2datetime(time());
+    $event['modified_at'] = db_unixtime2datetime(time());
+    return (db_insert('events', $event));
 }
 
-function db_change_event($event) {
+function db_change_event($event)
+{
     $id = $event['id'];
     unset($event['id']);
     unset($event['member_id']);
-    $event['event_date']    = db_unixtime2datetime(ui_date2unixtime($event['event_date']));
-    $event['fee']                  = ui_money2float($event['fee']);
-    $event['modified_at']   = db_unixtime2datetime(time());
+    $event['event_date'] = db_unixtime2datetime(ui_date2unixtime($event['event_date']));
+    $event['fee'] = ui_money2float($event['fee']);
+    $event['modified_at'] = db_unixtime2datetime(time());
     $special = '';
-    if (!isset($event['fee']             )) { $special .= ',`fee`=NULL';              }
-    if (!isset($event['member_type']     )) { $special .= ',`member_type`=NULL';      }
-    if (!isset($event['payment_interval'])) { $special .= ',`payment_interval`=NULL'; }
-    return db_update_single(DB_TABLE_EVENTS, $event, sprintf('id=\'%1$d\'', db_escape($id)), $special);
+    if (!isset($event['fee'])) {
+        $special .= ',`fee`=NULL';
+    }
+    if (!isset($event['member_type'])) {
+        $special .= ',`member_type`=NULL';
+    }
+    if (!isset($event['payment_interval'])) {
+        $special .= ',`payment_interval`=NULL';
+    }
+    return db_update('events', $event, sprintf("id='%d'", (int)$id), $special);
 }
 
-function db_delete_event($event) {
-    return db_delete_single(DB_TABLE_EVENTS, sprintf('id=\'%1$d\'', db_escape($event['id'])));
+function db_delete_event($event)
+{
+    return db_delete('events', 'id=?', [$event['id']]);
 }
 
-function db_get_events_for_member($member_id, $unixtime_start = NULL, $unixtime_end = NULL) {
-       $conditions = array(
-               sprintf('member_id=\'%d\'', db_escape($member_id))
-       );
-       if (isset($unixtime_start)) $conditions[] = sprintf('event_date>=\'%s\'', db_unixtime2date($unixtime_start));
-       if (isset($unixtime_end)  ) $conditions[] = sprintf('event_date<=\'%s\'', db_unixtime2date($unixtime_end));
-       
-    $sql = 'SELECT * FROM %1$s WHERE %2$s ORDER BY event_date ASC';
-    return db_select_multi(sprintf($sql, DB_TABLE_EVENTS, join(' AND ', $conditions)));
-}
+function db_get_events_for_member($member_id, $unixtime_start = null, $unixtime_end = null)
+{
+    // @TODO: Change to prepared statement
+    $conditions = array(
+        sprintf("member_id=%d", (int)$member_id)
+    );
+    if (isset($unixtime_start)) {
+        $conditions[] = sprintf("event_date>='%s'", db_unixtime2date($unixtime_start));
+    }
+    if (isset($unixtime_end)) {
+        $conditions[] = sprintf("event_date<='%s'", db_unixtime2date($unixtime_end));
+    }
 
-function db_get_event_with_member_and_date($member_id, $event_date) {
-    $sql = 'SELECT * FROM %1$s WHERE member_id=\'%2$d\' AND event_date=\'%3$s\' LIMIT 1';
-    return db_select_single(sprintf($sql, DB_TABLE_EVENTS, db_escape($member_id), db_escape($event_date)));
+    $sql = 'SELECT * FROM `events` WHERE %s ORDER BY event_date ASC';
+    return db_select_multi(sprintf($sql, join(' AND ', $conditions)));
 }
 
-function db_get_event_with_id($event_id) {
-    $sql = 'SELECT * FROM %1$s WHERE id=\'%2$d\' LIMIT 1';
-    return db_select_single(sprintf($sql, DB_TABLE_EVENTS, db_escape($event_id)));
+function db_get_event_with_member_and_date($member_id, $event_date)
+{
+    $sql = 'SELECT * FROM `events` WHERE member_id=? AND event_date=? LIMIT 1';
+    return db_select_single($sql, [$member_id, $event_date]);
 }
 
-/* }}} */
-
+function db_get_event_with_id($event_id)
+{
+    $sql = 'SELECT * FROM `events` WHERE id=? LIMIT 1';
+    return db_select_single($sql, [$event_id]);
+}
 
-function validate_event($userdata, &$dbdata, &$validation) {/*{{{*/
+function validate_event($userdata, &$dbdata, &$validation)
+{
     global $EVENT_TYPES, $MEMBER_TYPES, $PAYMENT_INTERVALS;
     $fields = array(
-        'id'              => 'number',
-        'event_date'      => 'date',
-        'fee'             => 'fee',
-        'event_type'      => $EVENT_TYPES,
-        'member_type'     => $MEMBER_TYPES,
-        'payment_interval'=> $PAYMENT_INTERVALS,
+        'id'               => 'number',
+        'event_date'       => 'date',
+        'fee'              => 'fee',
+        'event_type'       => $EVENT_TYPES,
+        'member_type'      => $MEMBER_TYPES,
+        'payment_interval' => $PAYMENT_INTERVALS,
     );
     $orig = $dbdata;
     foreach ($fields as $name => $type) {
-        if (!isset($userdata[$name])) continue;
+        if (!isset($userdata[$name])) {
+            continue;
+        }
         $dbdata[$name] = $userdata[$name];
     }
 
-
-// ids must be equal
-// member_ids must be equal
-// date must be unique for given member
+    // ids must be equal
+    // member_ids must be equal
+    // date must be unique for given member
     if (isset($orig['id'])) {
         if ($dbdata['id'] != $orig['id']) {
             $validation[] = 'Irgendwas anderes ist sehr sehr komisch.';
         }
-               if (empty($dbdata['event_date'])) {
+        if (empty($dbdata['event_date'])) {
             $validation[] = 'Bitte ein Datum für das Ereignis eingeben.';
-               }
-        elseif (!validate_event_date($dbdata['member_id'], $dbdata['event_date'], $dbdata['id'])) {
+        } elseif (!validate_event_date($dbdata['member_id'], $dbdata['event_date'], $dbdata['id'])) {
             $validation[] = 'An dem angegebenen Datum existiert schon ein Ereignis.';
         }
         return (count($validation) == 0);
     }
-       if (empty($dbdata['event_date'])) {
-           $validation[] = 'Bitte ein Datum für das Ereignis eingeben.';
-       }
-    elseif (!validate_event_date($dbdata['member_id'], $dbdata['event_date'])) {
+    if (empty($dbdata['event_date'])) {
+        $validation[] = 'Bitte ein Datum für das Ereignis eingeben.';
+    } elseif (!validate_event_date($dbdata['member_id'], $dbdata['event_date'])) {
         $validation[] = 'An dem angegebenen Datum existiert schon ein Ereignis.';
     }
     return (count($validation) == 0);
-}/*}}}*/
-
-
-
+}
 
-function validate_event_date($member_id, $event_date, $event_id = NULL) {/*{{{*/
+function validate_event_date($member_id, $event_date, $event_id = null)
+{
     $event_date = db_unixtime2date(ui_date2unixtime($event_date));
-    $event      = db_get_event_with_member_and_date($member_id, $event_date);
+    $event = db_get_event_with_member_and_date($member_id, $event_date);
 
     // theres no event with this date
-    if (!isset($event)) return true;
+    if (!isset($event)) {
+        return true;
+    }
 
     // there is a event, but maybe its the one we got passed
-    if (isset($event_id) && $event_id == $event['id']) return true;
+    if (isset($event_id) && $event_id == $event['id']) {
+        return true;
+    }
 
     return false;
-}/*}}}*/
-
+}
 
-function action_list_events($member = NULL) {/*{{{*/
+function action_list_events($member = null)
+{
     global $EVENT_TYPES, $MEMBER_TYPES, $PAYMENT_INTERVALS;
 
     if (!isset($member)) {
-        if (!isset($_REQUEST['member_id'])) redirect(link_to());
+        if (!isset($_REQUEST['member_id'])) {
+            redirect(link_to());
+        }
         $member = db_get_member_with_id($_REQUEST['id']);
-        if (!isset($member)) redirect(link_to());
+        if (!isset($member)) {
+            redirect(link_to());
+        }
     }
 
     $events = db_get_events_for_member($member['id']);
@@ -160,55 +163,65 @@ function action_list_events($member = NULL) {/*{{{*/
     ?>
     <h3 id="events">Ereignisse</h3>
     <?php if (isset($events)) : ?>
-        <table>
-            <tr>
-                <th>Datum</th>
-                <th>Ereignistyp</th>
-                <th>Mitgliedstyp</th>
-                <th>Beitrag</th>
-                <th>Zahlungsintervall</th>
-            </tr>
+    <table>
+        <tr>
+            <th>Datum</th>
+            <th>Ereignistyp</th>
+            <th>Mitgliedstyp</th>
+            <th>Beitrag</th>
+            <th>Zahlungsintervall</th>
+        </tr>
         <?php foreach ($events as $event) : ?>
             <tr>
-                <td><a href="<?=html_escape(link_to('change_event', array('id' => $event['id'])))?>#event_form"><?=html_escape(format_date(db_date2unixtime($event['event_date'])))?></a></td>
-                <td><?=html_escape($EVENT_TYPES[$event['event_type']])?></td>
-                <td><?=html_escape(isset($event['member_type'])? $MEMBER_TYPES[$event['member_type']] : '-')?></td>
-                <td><?=html_escape(isset($event['fee'])? format_money($event['fee']) : '-')?></td>
-                <td><?=html_escape(isset($event['payment_interval'])? $PAYMENT_INTERVALS[$event['payment_interval']] : '-')?></td>
+                <td><a href="<?php echo html_escape(link_to('change_event',
+                        array('id' => $event['id']))) ?>#event_form"><?php echo html_escape(format_date(db_date2unixtime($event['event_date']))) ?></a>
+                </td>
+                <td><?php echo html_escape($EVENT_TYPES[$event['event_type']]) ?></td>
+                <td><?php echo html_escape(isset($event['member_type']) ? $MEMBER_TYPES[$event['member_type']] : '-') ?></td>
+                <td><?php echo html_escape(isset($event['fee']) ? format_money($event['fee']) : '-') ?></td>
+                <td><?php echo html_escape(isset($event['payment_interval']) ? $PAYMENT_INTERVALS[$event['payment_interval']] : '-') ?></td>
             </tr>
         <?php endforeach ?>
-        </table>
-    <?php else : ?>
-        <p>Bisher gibt's noch keine Ereignisse.</p>
-    <?php endif ?>
+    </table>
+<?php else : ?>
+    <p>Bisher gibt's noch keine Ereignisse.</p>
+<?php endif ?>
     <?php
-}/*}}}*/
-
-
+}
 
-function action_create_event($member = NULL) {/*{{{*/
+function action_create_event($member = null)
+{
     if (!isset($member)) {
-        if (!isset($_REQUEST['member_id'])) redirect(link_to());
+        if (!isset($_REQUEST['member_id'])) {
+            redirect(link_to());
+        }
         $member = db_get_member_with_id($_REQUEST['id']);
-        if (!isset($member)) redirect(link_to());
+        if (!isset($member)) {
+            redirect(link_to());
+        }
     }
     $event = array();
-    $event['event_date']    = format_date(time());
-    $event['member_id']     = $member['id'];
+    $event['event_date'] = format_date(time());
+    $event['member_id'] = $member['id'];
     $events = db_get_events_for_member($member['id']);
     if (empty($events)) {
         $event['member_type'] = 'regular';
-    }
-    else {
+    } else {
         $event['event_type'] = 'changed';
     }
     $log_messages = array();
     if (isset($_POST['submit'])) {
         unset($_POST['id']);
         if (validate_event($_POST, $event, $log_messages)) {
-            if (empty($event['fee']             )) unset($event['fee']             );
-            if (empty($event['member_type']     )) unset($event['member_type']     );
-            if (empty($event['payment_interval'])) unset($event['payment_interval']);
+            if (empty($event['fee'])) {
+                unset($event['fee']);
+            }
+            if (empty($event['member_type'])) {
+                unset($event['member_type']);
+            }
+            if (empty($event['payment_interval'])) {
+                unset($event['payment_interval']);
+            }
             if (db_create_event($event)) {
                 redirect(link_to('view_member', array('id' => $event['member_id'])) . '#events');
             }
@@ -216,20 +229,31 @@ function action_create_event($member = NULL) {/*{{{*/
         }
     }
     form_event($event, 'create', $log_messages);
-}/*}}}*/
+}
 
-function action_change_event() {/*{{{*/
-    if (!isset($_REQUEST['id'])) redirect(link_to());
+function action_change_event()
+{
+    if (!isset($_REQUEST['id'])) {
+        redirect(link_to());
+    }
     $event = db_get_event_with_id($_REQUEST['id']);
-    $event['event_date']    = format_date(db_date2unixtime($event['event_date']));
-    $event['fee']                  = format_money($event['fee']);
-    if (!isset($event)) redirect(link_to());
+    $event['event_date'] = format_date(db_date2unixtime($event['event_date']));
+    $event['fee'] = format_money($event['fee']);
+    if (!isset($event)) {
+        redirect(link_to());
+    }
     $log_messages = array();
     if (isset($_POST['submit'])) {
         if (validate_event($_POST, $event, $log_messages)) {
-            if (empty($event['fee']             )) unset($event['fee']             );
-            if (empty($event['member_type']     )) unset($event['member_type']     );
-            if (empty($event['payment_interval'])) unset($event['payment_interval']);
+            if (empty($event['fee'])) {
+                unset($event['fee']);
+            }
+            if (empty($event['member_type'])) {
+                unset($event['member_type']);
+            }
+            if (empty($event['payment_interval'])) {
+                unset($event['payment_interval']);
+            }
             if (db_change_event($event)) {
                 redirect(link_to('view_member', array('id' => $event['member_id'])) . '#events');
             }
@@ -238,18 +262,25 @@ function action_change_event() {/*{{{*/
     }
 
     $member = db_get_member_with_id($event['member_id']);
-    if (!isset($member)) redirect(link_to());
+    if (!isset($member)) {
+        redirect(link_to());
+    }
     form_member($member, true);
     action_list_events($member);
     form_event($event, 'change', $log_messages);
-}/*}}}*/
+}
 
-function action_delete_event() {/*{{{*/
-    if (!isset($_REQUEST['id'])) redirect(link_to());
+function action_delete_event()
+{
+    if (!isset($_REQUEST['id'])) {
+        redirect(link_to());
+    }
     $event = db_get_event_with_id($_REQUEST['id']);
-    if (!isset($event)) redirect(link_to());
-    $event['event_date']    = format_date(db_date2unixtime($event['event_date']));
-    $event['fee']                  = format_money($event['fee']);
+    if (!isset($event)) {
+        redirect(link_to());
+    }
+    $event['event_date'] = format_date(db_date2unixtime($event['event_date']));
+    $event['fee'] = format_money($event['fee']);
     $log_messages = array();
     if (isset($_POST['submit'])) {
         if (db_delete_event($event)) {
@@ -258,52 +289,57 @@ function action_delete_event() {/*{{{*/
         echo db_error();
     }
     $member = db_get_member_with_id($event['member_id']);
-    if (!isset($member)) redirect(link_to());
+    if (!isset($member)) {
+        redirect(link_to());
+    }
     form_member($member, true);
     action_list_events($member);
     form_event($event, 'delete', $log_messages);
-}/*}}}*/
-
-
+}
 
-function form_event($event = array(), $task, $log_messages) {/*{{{*/
+function form_event($event = array(), $task, $log_messages)
+{
     global $EVENT_TYPES, $MEMBER_TYPES, $PAYMENT_INTERVALS;
 
     $readonly = ($task == 'delete');
 
     ?>
     <form action="#event_form" method="post" class="clearfix" id="event_form">
-        <?php if (isset($event['id'])) : ?><?=html_hidden_field('id', $event)?><?php endif ?>
-        <?=html_hidden_field('member_id', $event)?>
+        <?php if (isset($event['id'])) : ?><?php echo html_hidden_field('id', $event) ?><?php endif ?>
+        <?php echo html_hidden_field('member_id', $event) ?>
         <fieldset>
-        <?php if     ($task == 'create') : ?>
-            <legend>Neues Ereignis</legend>
-        <?php elseif ($task == 'delete') : ?>
-            <legend>Ereignis l&ouml;schen</legend>
-        <?php elseif ($task == 'change') : ?>
-            <legend>Ereignis &auml;ndern</legend>
-        <?php endif ?>
+            <?php if ($task == 'create') : ?>
+                <legend>Neues Ereignis</legend>
+            <?php elseif ($task == 'delete') : ?>
+                <legend>Ereignis l&ouml;schen</legend>
+            <?php elseif ($task == 'change') : ?>
+                <legend>Ereignis &auml;ndern</legend>
+            <?php endif ?>
             <?php log_messages($log_messages); ?>
-            <?=html_text_field('Datum', 'event_date', $event, $readonly)?>
-            <?=html_list_box('Ereignistyp', 'event_type', $EVENT_TYPES, $event, $readonly)?>
-            <?=html_list_box('Mitgliedstyp', 'member_type', array_merge(array(0 => 'unverändert'), $MEMBER_TYPES), $event, $readonly)?>
-            <?=html_money_field('Beitrag', 'fee', $event, $readonly)?>
+            <?php echo html_text_field('Datum', 'event_date', $event, $readonly) ?>
+            <?php echo html_list_box('Ereignistyp', 'event_type', $EVENT_TYPES, $event, $readonly) ?>
+            <?php echo html_list_box('Mitgliedstyp', 'member_type',
+                array_merge(array(0 => 'unverändert'), $MEMBER_TYPES),
+                $event, $readonly) ?>
+            <?php echo html_money_field('Beitrag', 'fee', $event, $readonly) ?>
             <small>Leer lassen wenn unver&auml;ndert.</small>
-            <?=html_list_box('Zahlungsintervall', 'payment_interval', array_merge(array(0 => 'unverändert'), $PAYMENT_INTERVALS), $event, $readonly)?>
+            <?php echo html_list_box('Zahlungsintervall', 'payment_interval',
+                array_merge(array(0 => 'unverändert'), $PAYMENT_INTERVALS), $event, $readonly) ?>
         </fieldset>
-    <?php if ($task == 'delete') : ?>
-        <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
-    <?php else : ?>
-        <input class="submit" type="submit" name="submit" value="Speichern"/>
-    <?php endif ?>
+        <?php if ($task == 'delete') : ?>
+            <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
+        <?php else : ?>
+            <input class="submit" type="submit" name="submit" value="Speichern"/>
+        <?php endif ?>
     </form>
     <?php if ($task == 'change') : ?>
-        <a href="<?=html_escape(link_to('delete_event', array('id' => $event['id'])))?>#event_form">Ereignis l&ouml;schen</a>
-        <a href="<?=html_escape(link_to('view_member', array('id' => $event['member_id'])))?>#event_form">Neues Ereignis</a>
-    <?php elseif ($task == 'delete') : ?>
-        <a href="<?=html_escape(link_to('view_member', array('id' => $event['member_id'])))?>#event_form">Neues Ereignis</a>
-    <?php endif ?>
+    <a href="<?php echo html_escape(link_to('delete_event', array('id' => $event['id']))) ?>#event_form">Ereignis
+        l&ouml;schen</a>
+    <a href="<?php echo html_escape(link_to('view_member', array('id' => $event['member_id']))) ?>#event_form">Neues
+        Ereignis</a>
+<?php elseif ($task == 'delete') : ?>
+    <a href="<?php echo html_escape(link_to('view_member', array('id' => $event['member_id']))) ?>#event_form">Neues
+        Ereignis</a>
+<?php endif ?>
     <?php
-}/*}}}*/
-
-
+}