/
log_entry_display_functions.php
400 lines (330 loc) · 13.9 KB
/
log_entry_display_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
// Functions for displaying the add log entry page
########################
# Display HTML headers
########################
function display_add_log_entry($athlete_id, $parent_session, $split, $start_date, $error_message ) {
// Check if this is a split or a parent session
$extra = array( "" , "" , "addlogentry" );
# show_log_table($athlete_id, $split, $extra );
# if ( $split == "no" )
# // Not a split so fire up the session adding form
session_form($parent_session, $athlete_id, $error_message, $start_date );
}
function display_edit_log_entry($athlete_id, $session_id, $error_message ) {
// The $extra variable was used to allow editing of the row within the table
// but I have changed my mind about this and will now use the pop-up edit window
$extra = array( "EDIT" , "$session_id" );
# show_log_table($athlete_id, "", $extra );
session_edit_form($session_id, $athlete_id, $error_message );
}
##############
# FUNCTIONS
##############
function show_log_table($athlete_id, $split, $extra ) {
include( "log_table.php" ) ;
}
function get_column_info($colname) {
// See if this column is listed in log_field_names
// if it is then get data_type info from here
// otherwise just use the actual data type
$query = "select * from log_field_names where log_column='$colname'";
#$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$result = do_sql($query) ;
if (pg_num_rows($result) > 0 ) {
// the result should only contain 1 row,
// we'll only use the first anyway.
$row = pg_fetch_array($result, null, PGSQL_ASSOC);
}
else{
// fake it
$row['log_col_short_name'] = $colname;
$row['log_col_long_name'] = $colname;
$row['log_col_description'] = $colname;
$query = "SELECT $colname from log limit 1";
#$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$result = do_sql($query) ;
$row['data_type'] = pg_field_type($result,0);
}
return $row ;
}
function session_form($parent_session, $athlete_id, $error_message, $start_date) {
// If start_date is not already set then use current date
$current_date=convert_date_from_pg($start_date);
if ( "X$start_date" == "X" )
$current_date= date("j/n/Y") ;
// or if parent session is set (ie this is a split) then use the parent's date
if ( $parent_session > 0 ) {
$result = do_sql("SELECT start_date,exercise_type FROM log WHERE session_id = '$parent_session'");
$row = pg_fetch_array($result, null, PGSQL_ASSOC);
$start_date = $row['start_date'];
$current_date = convert_date_from_pg($start_date) ;
$parent_ex_type = $row['exercise_type'];
}
# Put columns we never want to show in this array:
$do_not_show = array ("athlete_id", "session_id", "parent_session" ) ;
$columns = get_column_names() ;
# Get an array of fields user does want to display
$display_details = array();
$query = "SELECT detail from athlete_log_preferences
where athlete_id=$athlete_id ";
# ORDER BY display_sequence";
$result = do_sql($query) or die('Query failed: ' . pg_last_error());
# pg_fetch_array returns the next whole row as an array at each iteration
while ( $row = pg_fetch_array($result, null, PGSQL_ASSOC) )
{
foreach ( $row as $prev_detail ){
array_push( $display_details, $prev_detail ) ;
}
}
echo "\n<DIV id=mainlogentry name=addlogentry >\n";
echo "$error_message \n";
# Start Form and Table
echo "<a name=log_entry_form></a>";
echo "<FORM action=add_log_entry2.php method=post >";
echo "<input type=hidden name='parent_session' value=$parent_session>";
echo "<H2>\n";
if ( $parent_session > 0 ) {
echo "Splitting log entry for $current_date $parent_ex_type" ;
} else {
echo "Add Log Entry Details";
}
echo "</H2>\n";
echo "\n<TABLE class=mainlogentry ><TR><TD COLSPAN=4 >\n\n";
echo "<TR>\n";
// Display Entry fields
$notes_fields="";
$maxnumcols = 6 ;
$colcount = $maxnumcols ;
#foreach ( $columns as $colname ){
foreach ( $display_details as $colname ){
if ( (! in_array( $colname , $do_not_show ) ) && ( in_array( $colname , $display_details ) ) ) {
if ( $colcount-- == 0 ) { echo "\n</TR>\n" ; $colcount = $maxnumcols-1 ; }
$colinfo = get_column_info($colname);
$input_type = "text" ;
$default_value = "" ;
if ( preg_match("/^var/", $colinfo['data_type']) ) {
// Get the var length
$typearray = explode(" ",$colinfo['data_type'] ) ;
$varlen = $typearray[1];
}
if ( $colinfo['data_type'] == "date" ) {
$default_value=$current_date ;
$datepicker_number=$datepicker_number+1 ;
$input_id="\"cal".$datepicker_number."_container\"" ;
}
if ( preg_match("/^select/", $colinfo['data_type']) ) {
// We have a select list type. There should be a corresponding table
// which lists the select info
echo " <td align=center ><font size=1 > \n";
echo $colinfo['log_col_long_name']." <BR>\n";
generate_select_form( $colname, $colinfo['data_type'], "X" );
}
elseif ( preg_match("/^pref/", $colinfo['data_type']) ) {
// We have a prefer select list type. There should be a corresponding table
// which lists the prefered select info
echo " <td align=center ><font size=1 > \n";
echo $colinfo['log_col_long_name']." <BR>\n";
generate_prefer_select_form( $colname, $colinfo['data_type'], $value, $athlete_id);
}
// Generate Notes fields and add them beneath the smaller table entries
elseif ( ( $colinfo['data_type'] == "text" ) || ( $varlen >= 30 ) ) {
// Set maximum field size to 80
if ( $varlen <= 80 ) { $size = $varlen; } else { $size = 80 ; }
$notes_fields = "$notes_fields <TR><TD> ".$colinfo['log_col_long_name']."</TD> <TD colspan=20 ><INPUT type=text name =${colname} value='$value' size=$size > </TD></TR>\n";
}
else {
echo " <td align=center ><font size=1 > \n";
echo $colinfo['log_col_long_name']." <BR>\n";
echo "<INPUT type=$input_type name=${colname} id=$input_id value='$default_value' size=10>\n";
unset($input_id);
}
echo "</font> </td>" ;
}
}
echo "</TR>\n";
echo "$notes_fields\n";
echo "\n</TABLE>\n\n";
#echo "<input type=submit value='Submit entries' onclick='refreshParent();' >\n";
echo "<input type=hidden name=parent_session value='$parent_session' >\n";
echo "<input type=submit value='Submit entries' >\n";
echo "</FORM>\n</DIV>\n\n";
## END of FUNCTION
}
function generate_select_form($colname , $data_type, $value ) {
// $value is the default selected value.
// the second part of the data_type field should have the name of
// the table which contains the list info
preg_match_all("/\w+/", $data_type , $matches );
$array=$matches[0];
$list_table="select_$array[1]" ;
$query = "SELECT list_data from $list_table " ;
$result = do_sql($query) or die('Query failed: ' . pg_last_error());
$list_data=array();
while ( $row = pg_fetch_array($result, null, PGSQL_ASSOC) ) {
array_push( $list_data , $row['list_data'] ) ;
}
//See if $value is set to something other than X, or nothing.
// if not then set a few common defaults
if ( "$value" == "X" || "Z$value" == "Z" ) {
switch($colname){
case "entry_type": $value="planned - done" ; break ;
case "effort_notes": $value="medium" ; break ;
case "effort_notes": $value="medium" ; break ;
case "session_type": $value="training" ; break ;
}
}
echo " <SELECT NAME='${colname}' >\n" ;
foreach ( $list_data as $list_item ) {
$selected = "" ;
if ( "$list_item" == "$value" ) { $selected = "SELECTED" ; }
echo "<OPTION VALUE='$list_item' $selected > $list_item </OPTION>\n";
}
echo "</SELECT>\n";
## END of FUNCTION
}
function generate_prefer_select_form($colname , $data_type, $value, $athlete_id ) {
// This is used for generating a select list according to preferences
// selected by the user, when the log_field_names table col data_type
// starts with 'pref'
// $value is the default selected value.
// the second part of the data_type field should have the name of
// the table which contains the preferences list info
preg_match_all("/\w+/", $data_type , $matches );
$array=$matches[0];
$table="$array[1]" ;
$query = "SELECT detail from $table where athlete_id = $athlete_id " ;
$result = do_sql($query) or die('Query failed: ' . pg_last_error());
$list_data=array();
while ( $row = pg_fetch_array($result, null, PGSQL_ASSOC) ) {
array_push( $list_data , $row['detail'] ) ;
}
echo " <SELECT NAME='${colname}' >\n" ;
foreach ( $list_data as $list_item ) {
$selected = "" ;
if ( "$list_item" == "$value" ) { $selected = "SELECTED" ; }
echo "<OPTION VALUE='$list_item' $selected > $list_item </OPTION>\n";
}
echo "</SELECT>\n";
## END of FUNCTION
}
function convert_date_from_pg($pg_date) {
// Convert a pg date YYYY-MM-DD to nice date d/m/y
// first check it is actually in this format
if ( ! preg_match("/\d\d\d\d-\d\d-\d\d/", $pg_date) )
return $pg_date;
preg_match_all( "/\d+/", $pg_date, $matches);
$parts=$matches[0];
$yyyy = $parts[0];
$mm = $parts[1];
$dd = $parts[2];
$formatted_date = "$dd/$mm/$yyyy";
return $formatted_date ;
## END of FUNCTION
}
function session_edit_form($session_id, $athlete_id, $error_message) {
# Put columns we never want to show in this array:
$do_not_show = array ("athlete_id", "session_id", "parent_session" ) ;
$columns = get_column_names() ;
# Get an array of fields user does want to display
$display_details = array();
$query = "SELECT detail from athlete_log_preferences
where athlete_id=$athlete_id ";
# ORDER BY display_sequence";
$result = do_sql($query) or die('Query failed: ' . pg_last_error());
// pg_fetch_array returns the next whole row as an array at each iteration
while ( $row = pg_fetch_array($result, null, PGSQL_ASSOC) )
{
foreach ( $row as $prev_detail ){
array_push( $display_details, $prev_detail ) ;
}
}
echo "$error_message \n";
# Start Form and Table
echo "<DIV id=mainlogentry > <a name=log_entry_form></a>";
echo "<FORM action=update_log_entry.php method=post >\n";
echo "<input type=hidden name='session_id' value=$session_id>\n";
echo "\n\n<TABLE class=mainlogentry ><TR><TD>\n\n";
// Get Current row data
$query = "SELECT * from log
where session_id=$session_id ";
$result = do_sql($query) or die('Query failed: ' . pg_last_error());
// pg_fetch_array returns the next whole row as an array at each iteration
// there really should only be 1 row returned here.
// Check for date fields because we need to convert them
$date_fields = array() ;
$num = pg_num_fields($result) ;
for ( $fieldnum = 0 ; $fieldnum < $num ; $fieldnum++ ) {
if ( pg_field_type($result, $fieldnum) == 'date' ) {
// put names of all date fields into $date_fields
$fieldname = pg_field_name($result, $fieldnum);
array_push($date_fields, $fieldname);
}
}
// Now stick the info for the row the user wants to edit into an array
$session_details = pg_fetch_array($result, null, PGSQL_ASSOC);
// Check for date fields and convert them
// Get the fields names
foreach ( array_keys($session_details) as $field_name ) {
// and see if they are in the date_fields array
if ( in_array($field_name, $date_fields ) ) {
// If they are then convert the date from pg format to d/m/y
$session_details[$field_name] = convert_date_from_pg($session_details[$field_name]);
}
}
// Dates are converted //
// Display Entry fields
// Save text/notes fields (ie data_type text or var > 50 ) in a variable to
// display last
$notes_fields="";
echo "<TR>\n";
$maxnumcols = 6 ;
$colcount = $maxnumcols ;
foreach ( $display_details as $colname ){
if ( (! in_array( $colname , $do_not_show ) ) && ( in_array( $colname , $display_details ) ) ) {
if ( $colcount-- == 0 ) { echo "\n</TR>\n" ; $colcount = $maxnumcols-1 ; }
$colinfo = get_column_info($colname);
$input_type = "text" ;
$value = $session_details[$colname] ;
if ( preg_match("/^var/", $colinfo['data_type']) ) {
// Get the var length
$typearray = explode(" ",$colinfo['data_type'] ) ;
$varlen = $typearray[1];
}
if ( preg_match("/^select/", $colinfo['data_type']) ) {
// We have a select list type. There should be a corresponding table
// which lists the select info
echo " <td align=center ><font size=1 > \n";
echo $colinfo['log_col_long_name']." <BR>\n";
generate_select_form( $colname, $colinfo['data_type'], $value);
}
elseif ( preg_match("/^pref/", $colinfo['data_type']) ) {
// We have a prefer select list type. There should be a corresponding table
// which lists the prefered select info
echo " <td align=center ><font size=1 > \n";
echo $colinfo['log_col_long_name']." <BR>\n";
generate_prefer_select_form( $colname, $colinfo['data_type'], $value, $athlete_id);
}
// Generate Notes fields and add them beneath the smaller table entries
elseif ( ( $colinfo['data_type'] == "text" ) || ( $varlen >= 30 ) ) {
// Set maximum field size to 80
if ( $varlen <= 80 ) { $size = $varlen; } else { $size = 80 ; }
$notes_fields = "$notes_fields <TR><TD> ".$colinfo['log_col_long_name']."</TD> <TD colspan=20 ><INPUT type=text name =${colname} value='$value' size=$size > </TD></TR>\n";
}
else {
echo " <td align=center ><font size=1 > \n";
echo $colinfo['log_col_long_name']." <BR>\n";
echo "<INPUT type=$input_type name=${colname} value='$value' size=10>\n";
}
echo "</font> </td>\n" ;
}
}
echo "</TR>\n";
echo "$notes_fields\n";
echo "</TABLE>";
echo "<input type=submit value='Submit entries'>";
echo "</FORM>";
echo "</DIV>";
## END of FUNCTION
}
?>