-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
164 lines (143 loc) · 5.07 KB
/
admin.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
<?php
add_action( 'wp_dashboard_setup', 'mf_dashboard_widgets' );
function mf_dashboard_widgets() {
wp_add_dashboard_widget( 'money_flow_widget', 'Money Flow', 'mf_dashboard_widget_content' );
}
function mf_dashboard_widget_content() {
global $current_user;
get_currentuserinfo();
if ( isset( $_POST['moneyflow'] ) ) {
foreach( $_POST['moneyflow'] as $type => $records ) {
foreach( $records as $person ) {
// Sanitization of name
if ( ! ctype_alnum( str_replace( ' ', '', $person['name'] ) ) )
$person['name'] = 'Dummy';
// Sanitization of amount
$person['amount'] = absint( $person['amount'] );
if ( $person['amount'] != 0 )
$valid[$type][] = array( 'name' => $person['name'], 'amount' => $person['amount'] );
}
}
update_user_meta( $current_user->ID, 'moneyflow_data', $valid );
?>
<script>
(function($){
$(document).ready(function(){
// show saved message
$('#money_flow_widget h3 span').html('Money Flow <span class="red">Saved!</span>');
// hide message after timeout
setTimeout(function(){
$('#money_flow_widget h3 span').html('Money Flow');
},2000);
});
})(jQuery);
</script>
<?php
}
$moneyflow = get_user_meta( $current_user->ID, 'moneyflow_data', true );
?>
<style type="text/css">
#money_flow_widget table { margin:0; padding:0; }
#money_flow_widget .widefat td, #money_flow_widget .widefat th { padding:5px 10px 3px; color:#8F8F8F; font-size:15px; border-bottom-color:#FFF; vertical-align:baseline; }
#money_flow_widget input[type=submit] { margin:0 0 7px 10px; }
#money_flow_widget input[type=text] { font-size:13px; }
#money_flow_widget span.red {color:#c30304;}
</style>
<script type="text/javascript">
(function($){
$(document).ready(function(){
var money_flow_widget = $('#money_flow_widget');
// Hide submit button initially
money_flow_widget.find('.submit-row').hide();
// As soon as any field changes, show the submit button
money_flow_widget.find('.small-text').change(function(){
money_flow_widget.find('.submit-row').slideDown('slow');
});
// Add button calls
money_flow_widget.find('thead a').click(function(){
var type = $(this).parent().attr('data-type');
var count = money_flow_widget.find('.take-col tr').length + 1;
money_flow_widget.find('.'+type+'-col table').append('<tr data-count="'+count+'"><td><p>Name (Click to edit)</p><input type="hidden" name="moneyflow['+type+']['+parseInt(count)+'][name]" value="Name (Click to edit)" /></td><td><input class="small-text" type="text" name="moneyflow['+type+']['+parseInt(count)+'][amount]" value="0" /></td></tr>');
money_flow_widget.find('.submit-row').slideDown('slow');
return false;
});
// Make name editable
money_flow_widget.find('p').live('click',function(){
var p = $(this);
var nameField = p.next();
var name = $(nameField).val();
// hide <p>
p.hide();
// insert a input field to modify name
p.after('<input type="text" class="name-edit" value="'+name+'" />');
// cache temp field
var tempField = p.next('.name-edit');
// set focus
tempField.focus();
// save on blur
tempField.blur(function(){
// save name to hidden field
nameField.val( tempField.val() );
// remove temp fiels
tempField.remove();
// show new value in <p>
p.html( tempField.val() );
// make <p> visible
p.show();
// show submit button
money_flow_widget.find('.submit-row').slideDown('slow');
});
});
});
})(jQuery);
</script>
<form action="" method="POST">
<table id="moneyflow" class="widefat">
<thead>
<tr>
<th data-type="take">Take Money (<a href="#">Add</a>)</th>
<th data-type="give">Give Money (<a href="#">Add</a>)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="take-col">
<table>
<?php foreach ( $moneyflow['take'] as $key => $person ) { ?>
<tr data-count="<?php echo $key; ?>">
<td>
<p><?php echo $person['name']; ?></p>
<input type="hidden" name="moneyflow[take][<?php echo $key; ?>][name]" value="<?php echo $person['name']; ?>" />
</td>
<td>
<input class="small-text" type="text" name="moneyflow[take][<?php echo $key; ?>][amount]" value="<?php echo $person['amount']; ?>" />
</td>
</tr>
<?php } ?>
</table>
</td>
<td class="give-col">
<table>
<?php foreach ( $moneyflow['give'] as $key => $person ) { ?>
<tr data-count="<?php echo $key; ?>">
<td>
<p><?php echo $person['name']; ?></p>
<input type="hidden" name="moneyflow[give][<?php echo $key; ?>][name]" value="<?php echo $person['name']; ?>" />
</td>
<td>
<input class="small-text" type="text" name="moneyflow[give][<?php echo $key; ?>][amount]" value="<?php echo $person['amount']; ?>" />
</td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<tr class="submit-row">
<td><input class="button-primary" type="submit" value="Submit" /></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<?php
}