7a56de6373697fd1de4a75c88ce83b68ce316560
[memberdb.git] / include / events.php
1 <?php
2
3 $SQL_CREATE_EVENTS = sprintf('
4     CREATE TABLE IF NOT EXISTS `%1$s` (
5       `id` int(10) unsigned NOT NULL auto_increment,
6       `event_date` date NOT NULL,
7       `event_type` enum(\'joined\',\'changed\',\'leaved\') NOT NULL,
8       `member_id` int(10) unsigned NOT NULL,
9       `member_type` enum(\'regular\',\'patron\',\'honorary\') NULL,
10       `fee` DECIMAL(10,2) NULL,
11       `payment_interval` enum(\'monthly\', \'quarterly\', \'halfyearly\', \'yearly\') NULL,
12       `created_at` datetime NOT NULL,
13       `modified_at` datetime NOT NULL,
14       PRIMARY KEY  (`id`),
15       UNIQUE KEY `member_id_event_date` (`member_id`, `event_date`)
16     ) ENGINE=InnoDB DEFAULT CHARSET=%2$s
17     ', DB_TABLE_EVENTS, DB_CHARSET);
18
19 $EVENT_TYPES = array(
20     'joined'    => 'Eintritt',
21     'changed'   => 'Änderung',
22     'leaved'    => 'Austritt'
23 );
24
25 $PAYMENT_INTERVALS = array(
26     'monthly'   => 'monatlich',
27     'quarterly' => 'vierteljährlich',
28     'halfyearly'=> 'halbjährlich',
29     'yearly'    => 'jährlich'
30 );
31
32 $MEMBER_TYPES = array(
33     'regular'   => 'Ordentliches Mitglied',
34     'patron'    => 'Fördermitglied',
35     'honorary'  => 'Ehrenmitglied'
36 );
37
38 /* DB functions {{{ */
39
40 function db_create_event($event) {
41     unset($event['id']);
42     $event['event_date']    = db_unixtime2datetime(ui_date2unixtime($event['event_date']));
43     $event['fee']                   = ui_money2float($event['fee']);
44     $event['created_at']    = db_unixtime2datetime(time());
45     $event['modified_at']   = db_unixtime2datetime(time());
46     return (db_insert_single(DB_TABLE_EVENTS, $event));
47 }
48
49 function db_change_event($event) {
50     $id = $event['id'];
51     unset($event['id']);
52     unset($event['member_id']);
53     $event['event_date']    = db_unixtime2datetime(ui_date2unixtime($event['event_date']));
54     $event['fee']                   = ui_money2float($event['fee']);
55     $event['modified_at']   = db_unixtime2datetime(time());
56     $special = '';
57     if (!isset($event['fee']             )) { $special .= ',`fee`=NULL';              }
58     if (!isset($event['member_type']     )) { $special .= ',`member_type`=NULL';      }
59     if (!isset($event['payment_interval'])) { $special .= ',`payment_interval`=NULL'; }
60     return db_update_single(DB_TABLE_EVENTS, $event, sprintf('id=\'%1$d\'', db_escape($id)), $special);
61 }
62
63 function db_delete_event($event) {
64     return db_delete_single(DB_TABLE_EVENTS, sprintf('id=\'%1$d\'', db_escape($event['id'])));
65 }
66
67 function db_get_events_for_member($member_id, $unixtime_start = NULL, $unixtime_end = NULL) {
68         $conditions = array(
69                 sprintf('member_id=\'%d\'', db_escape($member_id))
70         );
71         if (isset($unixtime_start)) $conditions[] = sprintf('event_date>=\'%s\'', db_unixtime2date($unixtime_start));
72         if (isset($unixtime_end)  ) $conditions[] = sprintf('event_date<=\'%s\'', db_unixtime2date($unixtime_end));
73         
74     $sql = 'SELECT * FROM %1$s WHERE %2$s ORDER BY event_date ASC';
75     return db_select_multi(sprintf($sql, DB_TABLE_EVENTS, join(' AND ', $conditions)));
76 }
77
78 function db_get_event_with_member_and_date($member_id, $event_date) {
79     $sql = 'SELECT * FROM %1$s WHERE member_id=\'%2$d\' AND event_date=\'%3$s\' LIMIT 1';
80     return db_select_single(sprintf($sql, DB_TABLE_EVENTS, db_escape($member_id), db_escape($event_date)));
81 }
82
83 function db_get_event_with_id($event_id) {
84     $sql = 'SELECT * FROM %1$s WHERE id=\'%2$d\' LIMIT 1';
85     return db_select_single(sprintf($sql, DB_TABLE_EVENTS, db_escape($event_id)));
86 }
87
88 /* }}} */
89
90
91 function validate_event($userdata, &$dbdata, &$validation) {/*{{{*/
92     global $EVENT_TYPES, $MEMBER_TYPES, $PAYMENT_INTERVALS;
93     $fields = array(
94         'id'              => 'number',
95         'event_date'      => 'date',
96         'fee'             => 'fee',
97         'event_type'      => $EVENT_TYPES,
98         'member_type'     => $MEMBER_TYPES,
99         'payment_interval'=> $PAYMENT_INTERVALS,
100     );
101     $orig = $dbdata;
102     foreach ($fields as $name => $type) {
103         if (!isset($userdata[$name])) continue;
104         $dbdata[$name] = $userdata[$name];
105     }
106
107
108 // ids must be equal
109 // member_ids must be equal
110 // date must be unique for given member
111     if (isset($orig['id'])) {
112         if ($dbdata['id'] != $orig['id']) {
113             $validation[] = 'Irgendwas anderes ist sehr sehr komisch.';
114         }
115                 if (empty($dbdata['event_date'])) {
116             $validation[] = 'Bitte ein Datum für das Ereignis eingeben.';
117                 }
118         elseif (!validate_event_date($dbdata['member_id'], $dbdata['event_date'], $dbdata['id'])) {
119             $validation[] = 'An dem angegebenen Datum existiert schon ein Ereignis.';
120         }
121         return (count($validation) == 0);
122     }
123         if (empty($dbdata['event_date'])) {
124            $validation[] = 'Bitte ein Datum für das Ereignis eingeben.';
125         }
126     elseif (!validate_event_date($dbdata['member_id'], $dbdata['event_date'])) {
127         $validation[] = 'An dem angegebenen Datum existiert schon ein Ereignis.';
128     }
129     return (count($validation) == 0);
130 }/*}}}*/
131
132
133
134
135 function validate_event_date($member_id, $event_date, $event_id = NULL) {/*{{{*/
136     $event_date = db_unixtime2date(ui_date2unixtime($event_date));
137     $event      = db_get_event_with_member_and_date($member_id, $event_date);
138
139     // theres no event with this date
140     if (!isset($event)) return true;
141
142     // there is a event, but maybe its the one we got passed
143     if (isset($event_id) && $event_id == $event['id']) return true;
144
145     return false;
146 }/*}}}*/
147
148
149 function action_list_events($member = NULL) {/*{{{*/
150     global $EVENT_TYPES, $MEMBER_TYPES, $PAYMENT_INTERVALS;
151
152     if (!isset($member)) {
153         if (!isset($_REQUEST['member_id'])) redirect(link_to());
154         $member = db_get_member_with_id($_REQUEST['id']);
155         if (!isset($member)) redirect(link_to());
156     }
157
158     $events = db_get_events_for_member($member['id']);
159
160     ?>
161     <h3 id="events">Ereignisse</h3>
162     <?php if (isset($events)) : ?>
163         <table>
164             <tr>
165                 <th>Datum</th>
166                 <th>Ereignistyp</th>
167                 <th>Mitgliedstyp</th>
168                 <th>Beitrag</th>
169                 <th>Zahlungsintervall</th>
170             </tr>
171         <?php foreach ($events as $event) : ?>
172             <tr>
173                 <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>
174                 <td><?=html_escape($EVENT_TYPES[$event['event_type']])?></td>
175                 <td><?=html_escape(isset($event['member_type'])? $MEMBER_TYPES[$event['member_type']] : '-')?></td>
176                 <td><?=html_escape(isset($event['fee'])? format_money($event['fee']) : '-')?></td>
177                 <td><?=html_escape(isset($event['payment_interval'])? $PAYMENT_INTERVALS[$event['payment_interval']] : '-')?></td>
178             </tr>
179         <?php endforeach ?>
180         </table>
181     <?php else : ?>
182         <p>Bisher gibt's noch keine Ereignisse.</p>
183     <?php endif ?>
184     <?php
185 }/*}}}*/
186
187
188
189 function action_create_event($member = NULL) {/*{{{*/
190     if (!isset($member)) {
191         if (!isset($_REQUEST['member_id'])) redirect(link_to());
192         $member = db_get_member_with_id($_REQUEST['id']);
193         if (!isset($member)) redirect(link_to());
194     }
195     $event = array();
196     $event['event_date']    = format_date(time());
197     $event['member_id']     = $member['id'];
198     $events = db_get_events_for_member($member['id']);
199     if (empty($events)) {
200         $event['member_type'] = 'regular';
201     }
202     else {
203         $event['event_type'] = 'changed';
204     }
205     $log_messages = array();
206     if (isset($_POST['submit'])) {
207         unset($_POST['id']);
208         if (validate_event($_POST, $event, $log_messages)) {
209             if (empty($event['fee']             )) unset($event['fee']             );
210             if (empty($event['member_type']     )) unset($event['member_type']     );
211             if (empty($event['payment_interval'])) unset($event['payment_interval']);
212             if (db_create_event($event)) {
213                 redirect(link_to('view_member', array('id' => $event['member_id'])) . '#events');
214             }
215             echo db_error();
216         }
217     }
218     form_event($event, 'create', $log_messages);
219 }/*}}}*/
220
221 function action_change_event() {/*{{{*/
222     if (!isset($_REQUEST['id'])) redirect(link_to());
223     $event = db_get_event_with_id($_REQUEST['id']);
224     $event['event_date']    = format_date(db_date2unixtime($event['event_date']));
225     $event['fee']                   = format_money($event['fee']);
226     if (!isset($event)) redirect(link_to());
227     $log_messages = array();
228     if (isset($_POST['submit'])) {
229         if (validate_event($_POST, $event, $log_messages)) {
230             if (empty($event['fee']             )) unset($event['fee']             );
231             if (empty($event['member_type']     )) unset($event['member_type']     );
232             if (empty($event['payment_interval'])) unset($event['payment_interval']);
233             if (db_change_event($event)) {
234                 redirect(link_to('view_member', array('id' => $event['member_id'])) . '#events');
235             }
236             echo db_error();
237         }
238     }
239
240     $member = db_get_member_with_id($event['member_id']);
241     if (!isset($member)) redirect(link_to());
242     form_member($member, true);
243     action_list_events($member);
244     form_event($event, 'change', $log_messages);
245 }/*}}}*/
246
247 function action_delete_event() {/*{{{*/
248     if (!isset($_REQUEST['id'])) redirect(link_to());
249     $event = db_get_event_with_id($_REQUEST['id']);
250     if (!isset($event)) redirect(link_to());
251     $event['event_date']    = format_date(db_date2unixtime($event['event_date']));
252     $event['fee']                   = format_money($event['fee']);
253     $log_messages = array();
254     if (isset($_POST['submit'])) {
255         if (db_delete_event($event)) {
256             redirect(link_to('view_member', array('id' => $event['member_id'])) . '#events');
257         }
258         echo db_error();
259     }
260     $member = db_get_member_with_id($event['member_id']);
261     if (!isset($member)) redirect(link_to());
262     form_member($member, true);
263     action_list_events($member);
264     form_event($event, 'delete', $log_messages);
265 }/*}}}*/
266
267
268
269 function form_event($event = array(), $task, $log_messages) {/*{{{*/
270     global $EVENT_TYPES, $MEMBER_TYPES, $PAYMENT_INTERVALS;
271
272     $readonly = ($task == 'delete');
273
274     ?>
275     <form action="#event_form" method="post" class="clearfix" id="event_form">
276         <?php if (isset($event['id'])) : ?><?=html_hidden_field('id', $event)?><?php endif ?>
277         <?=html_hidden_field('member_id', $event)?>
278         <fieldset>
279         <?php if     ($task == 'create') : ?>
280             <legend>Neues Ereignis</legend>
281         <?php elseif ($task == 'delete') : ?>
282             <legend>Ereignis l&ouml;schen</legend>
283         <?php elseif ($task == 'change') : ?>
284             <legend>Ereignis &auml;ndern</legend>
285         <?php endif ?>
286             <?php log_messages($log_messages); ?>
287             <?=html_text_field('Datum', 'event_date', $event, $readonly)?>
288             <?=html_list_box('Ereignistyp', 'event_type', $EVENT_TYPES, $event, $readonly)?>
289             <?=html_list_box('Mitgliedstyp', 'member_type', array_merge(array(0 => 'unverändert'), $MEMBER_TYPES), $event, $readonly)?>
290             <?=html_money_field('Beitrag', 'fee', $event, $readonly)?>
291             <small>Leer lassen wenn unver&auml;ndert.</small>
292             <?=html_list_box('Zahlungsintervall', 'payment_interval', array_merge(array(0 => 'unverändert'), $PAYMENT_INTERVALS), $event, $readonly)?>
293         </fieldset>
294     <?php if ($task == 'delete') : ?>
295         <input class="submit" type="submit" name="submit" value="L&ouml;schen"/>
296     <?php else : ?>
297         <input class="submit" type="submit" name="submit" value="Speichern"/>
298     <?php endif ?>
299     </form>
300     <?php if ($task == 'change') : ?>
301         <a href="<?=html_escape(link_to('delete_event', array('id' => $event['id'])))?>#event_form">Ereignis l&ouml;schen</a>
302         <a href="<?=html_escape(link_to('view_member', array('id' => $event['member_id'])))?>#event_form">Neues Ereignis</a>
303     <?php elseif ($task == 'delete') : ?>
304         <a href="<?=html_escape(link_to('view_member', array('id' => $event['member_id'])))?>#event_form">Neues Ereignis</a>
305     <?php endif ?>
306     <?php
307 }/*}}}*/
308
309