-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_table.php
321 lines (292 loc) · 9.29 KB
/
log_table.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
<?php
/**
* defines a log table form_element type
*
* this creates a simple table element with named columns
* the columns are defined in the "values" area of the field definition.
* For writing, the element only allows data in one new row to be saved, old rows
* can't be edited: that makes it a "log" type field as opposed to a "spreadsheet"
* type field where all cells are editable
*
* @package WordPress
* @subpackage Participants Database Plugin
* @author Roland Barker <[email protected]>
* @copyright 2016 xnau webdesign
* @license GPL3
* @version 0.1
* @link http://xnau.com/wordpress-plugins/
* @depends
*/
namespace pdbmps\fields;
class log_table extends pdb_custom_field {
/**
* @var array of column names in $name => $title format
*/
private $columns;
/**
* @var array of stored values
*/
private $values;
/**
* intantiates the field
*
* @param string $name name of the field element
* @param string $title title of the field element
*/
public function __construct( $title = '' )
{
parent::__construct( 'log-table', empty( $title ) ? 'Log Table' : $title );
add_filter('pdb-before_submit_signup', array( $this, 'save_data' ) );
add_filter('pdb-before_submit_update', array( $this, 'save_data' ) );
}
/**
* display the field value in a read context
*
* @return string
*/
public function display_value()
{
$table = sprintf( apply_filters( 'pdb-member_payments_table_html_wrap', '<table class="%1$s-%2$s"><thead>%3%s</thead><tbody>%4$s</tbody></table>' ), $this->field->name, $this->name, $this->table_header(), $this->table_body() );
return sprintf( '<div class="table-form-element">%s</div>', $table );
}
/**
* prepares the data for saving to the database
*
* gets the previous rows from the record and adds the incoming row
*
* @param array the incoming post data
*
* @return array the modified data array
*/
public function save_data( $post )
{
$saved_data = $this->participant_values( $post['id'] );
foreach( $this->table_fields() as $fieldname ) {
if ( array_key_exists( $fieldname, $post ) ) {
$table_value = isset( $saved_data[$fieldname] ) && ! empty( $saved_data[$fieldname] ) ? maybe_unserialize( $saved_data[$fieldname] ) : array();
if ( $this->array_has_values( $post[$fieldname] ) ) {
(array) $table_value[] = filter_var_array( $post[$fieldname], FILTER_SANITIZE_STRING );
}
$post[$fieldname] = serialize($table_value);
}
}
return $post;
}
/**
* provides the form element HTML
*
* @return null
*/
protected function form_element_html()
{
$html = '';
$this->field->output = $this->editable_table();
}
/**
* supplies the editable table element
*
* @return string HTML
*/
private function editable_table()
{
return sprintf( apply_filters( 'pdb-member_payments_table_html_wrap', '<table class="%1$s-%2$s"><thead>%3$s</thead><tbody>%4$s</tbody></table>' ), $this->field->name, $this->name, $this->table_header(), $this->table_body() . $this->editable_row() );
}
/**
* supplies the editable row
*
* @return string HTML
*/
private function editable_row()
{
if ( isset( $this->field->attributes['readonly'] ) && $this->field->attributes['readonly'] === 'readonly' && ! \Participants_Db::current_user_has_plugin_role('editor', __METHOD__) ) return '';
$row = '';
$index = $this->value_row_count() + 1;
$values = $this->post_data_array();
$row_template = apply_filters( 'pdb-member_payments_table_body_row', '<tr data-index="%1$s">%2$s</tr>' );
$cell_template = apply_filters( 'pdb-member_payments_table_body_cell', '<th class="%1$s-column">%2$s</th>' );
foreach ( array_keys( $this->columns ) as $name ) {
$input = \PDb_FormElement::get_element(array(
'type' => 'text-line',
//'name' => $this->field->name . '[' . $index . '][' . $name . ']',
'name' => $this->field->name . '[' . $name . ']',
'value' => isset( $values[$name] ) ? $values[$name] : '',
));
$row .= sprintf( $cell_template, $name, $input );
}
return sprintf( $row_template, $index, $row );
}
/**
* provides the table body HTML
*
* @return string HTML
*/
private function table_body()
{
if ( $this->value_row_count() === 0 ) {
// dont show if there are no rows in the data
return '';
}
$rows = array();
$row_template = apply_filters( 'pdb-member_payments_table_body_row', '<tr data-index="%1$s">%2$s</tr>' );
$cell_template = apply_filters( 'pdb-member_payments_table_body_cell', '<td class="%1$s-column">%2$s</td>' );
foreach ( $this->values as $i => $row ) {
$cells = '';
foreach ( array_keys( $this->columns ) as $name ) {
$cells .= sprintf( $cell_template, $name, isset( $row[$name] ) ? $row[$name] : '' );
}
$rows[] = sprintf( $row_template, $i, $cells );
}
return implode( "\n", $rows );
}
/**
* provides the table header row
*
* @return string HTML
*/
private function table_header()
{
$header = '';
$row_template = apply_filters( 'pdb-member_payments_table_header_row', '<tr>%s</tr>' );
$cell_template = apply_filters( 'pdb-member_payments_table_header_cell', '<th class="%1$s-column">%2$s</th>' );
foreach ( $this->columns as $name => $title ) {
$header .= sprintf( $cell_template, $name, $title );
}
return sprintf( $row_template, $header );
}
/**
* sets up the columns
*
*/
protected function setup_columns()
{
foreach ( $this->field->options as $name => $title ) {
/**
* @filter pdb-member_payments_non_column_options
* @param array
*
* provides a way to add field definition options that are not table columns
*/
if ( !in_array( $name, apply_filters( 'pdb-member_payments_non_column_options', array() ) ) ) {
$this->columns[$name] = $title;
}
}
}
/**
* sets up the field object and columns
*
* @param object $field the incoming object
*/
protected function setup_field( $field )
{
$this->field = $field;
$this->setup_columns();
$this->setup_value();
}
/**
* sets the field value property
*
* this gets the values from the database if there is a new value in the $_POST
* array because the Shortcode class overrides the stored value with the POST
* value if it exists. We need to add the stored value to the incoming value in
* the POST to get the complete value for the field.
*
*/
private function setup_value()
{
$values = maybe_unserialize( $this->field->value );
if ( array_key_exists( $this->field->name, $_POST ) ) {
$values = $this->stored_value( filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ) );
if ( end( $values ) === filter_var_array( $_POST[$this->field->name], FILTER_SANITIZE_STRING ) ) {
unset( $_POST[$this->field->name]);
}
}
$this->values = !is_array( $values ) ? array() : $values;
}
/**
* provides the form element's mysql datatype
*
* @return string
*/
protected function element_datatype()
{
return 'TEXT'; // ought to be big enough
}
/**
* supplies the post values for a row
*
* @return array the data array
*/
private function post_data_array()
{
if ( array_key_exists( $this->field->name, $_POST ) ) {
return filter_var_array( $_POST[$this->field->name], FILTER_SANITIZE_STRING );
}
return array();
}
/**
* supplies a list of field names that are using this form element
*
* @return array
*/
private function table_fields()
{
$table_fields = wp_cache_get('table_fields_list', self::label );
if ( $table_fields === false ) {
$table_fields = array();
foreach( \Participants_Db::$fields as $field ) {
if ( $field->form_element === $this->name ) {
$table_fields[] = $field->name;
}
}
wp_cache_add('table_fields_list', self::label );
}
return $table_fields;
}
/**
* provides a count of the number of rows in the data
*
* ignores an empty row at the end of the array
*
* @return int
*/
private function value_row_count()
{
$count = count( $this->values );
if ( $count > 0 && empty( end( $this->values ) ) ) {
return $count - 1;
}
return $count;
}
/**
* tells if an array has non-empty values
*
* @param array $array the array to test
* @return bool true if the array has values
*/
private function array_has_values( $array )
{
$test = implode( '', (array) $array );
return strlen( $test ) > 0;
}
/**
* provides the field value from the database
*
* @param int $id the record id
* @return array the stored value
*/
private function participant_values( $id )
{
return \Participants_Db::get_participant( filter_var($id, FILTER_SANITIZE_NUMBER_INT ) );
}
/**
* supplies the stored value for the field
*
* @param int $id current record id
* @return array
*/
private function stored_value( $id )
{
$record_values = $this->participant_values($id);
return array_key_exists($this->field->name, $record_values) ? maybe_unserialize( $record_values[$this->field->name] ) : array();
}
}