PluginProbe
Time Clock – A WordPress Employee & Volunteer Time Clock Plugin / 1.2
Time Clock – A WordPress Employee & Volunteer Time Clock Plugin v1.2
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.2
time-clock / includes / functions.php

functions.php in Time Clock – A WordPress Employee & Volunteer Time Clock Plugin 1.2, at includes/functions.php

259 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5
6 // This page is for general functions
7
8
9 // get options with defaults - used in settings_api.php to load defaults for settings page
10 function etimeclockwp_get_option($key) {
11
12 $etimeclockwp_options = get_option('etimeclockwp_settings');
13
14 $result = '';
15
16 // check if option has been saved
17 if (isset($etimeclockwp_options[$key])) {
18 // get option from saved options
19 $result = $etimeclockwp_options[$key];
20 } else {
21 // get option from default in settings array
22 $settings = etimeclockwp_settings();
23
24 // loop through remaining values to get default
25 foreach ($settings as $tabs ) {
26 foreach ($tabs as $page) {
27 foreach ($page as $option) {
28 if ($option['name'] == $key) {
29 if (isset($option['default'])) {
30 $result = $option['default'];
31 }
32 }
33 }
34 }
35 }
36
37 // save default to we don't need to search again
38 $etimeclockwp_options[$key] = $result;
39 update_option('etimeclockwp_settings',$etimeclockwp_options);
40
41 }
42
43 return $result;
44 }
45
46
47 // load and save all options - the loop should only run on install
48 function etimeclockwp_get_options() {
49
50 $etimeclockwp_options = get_option('etimeclockwp_settings');
51
52 if (!isset($etimeclockwp_options['tab'])) {
53
54 $settings = etimeclockwp_settings();
55
56 foreach ($settings as $tabs ) {
57 foreach ($tabs as $page) {
58 foreach ($page as $option) {
59 $etimeclockwp_options[$option['name']] = $option['default'];
60 if (isset($option['default'])) {
61 update_option('etimeclockwp_settings',$etimeclockwp_options);
62 }
63 }
64 }
65 }
66
67 $etimeclockwp_options['tab'] = 'tab00';;
68 update_option('etimeclockwp_settings',$etimeclockwp_options);
69
70 $etimeclockwp_options = get_option('etimeclockwp_settings');
71 return $etimeclockwp_options;
72
73 } else {
74 return $etimeclockwp_options;
75 }
76 exit;
77 }
78
79
80 // convert seconds time to hours / mins / secs
81 function etimeclockwp_convert_time($seconds) {
82 $t = round($seconds);
83 return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
84 }
85
86
87 // get total time worked
88 function etimeclockwp_get_time_worked($post,$format = true) {
89 $total = get_post_meta($post->ID,'total', true);
90
91 // this is needed due to removing the date part since version 1.2
92 if (empty($total)) {
93 $date_part = get_the_date('Y-m-d',$post->ID);
94 $total = get_post_meta($post->ID,'total_'.$date_part, true);
95 }
96
97 if ($format == true) {
98 $total = etimeclockwp_convert_time($total);
99 }
100 return $total;
101 }
102
103 // get notices for work day - used to display if someone forgot to clockout
104 function etimeclock_get_notices($post_id) {
105 $notices = get_post_meta($post_id,'notices', true);
106 if (!empty($notices)) {
107 $notices = __( 'Admin Review','etimeclockwp');
108 } else {
109 $notices = '-';
110 }
111 return $notices;
112 }
113
114
115 // caculate total time given post id
116 function etimeclockwp_caculate_total_time($post_id) {
117
118 // do a full recaculation based on entry order and don't worry about the existing total time value
119
120 $metavalue = get_post_meta($post_id);
121
122 $total_time_array = array();
123
124 $count = '0'; // this is used if the event does not have a working order, this should only happen if the user is between upgrading from version 1.1 to 1.2
125
126 foreach($metavalue as $key => $val) {
127
128 if (substr($key, 0, 5) === "etime") {
129
130 // get key
131 $key = explode('_', $key);
132 $key = $key[0];
133
134 // caculate working status
135 if ($key == 'etimeclockwp-in') {
136 $working_status = '1';
137 }
138
139 if ($key == 'etimeclockwp-breakon') {
140 $working_status = '0';
141 }
142
143 if ($key == 'etimeclockwp-breakoff') {
144 $working_status = '1';
145 }
146
147 if ($key == 'etimeclockwp-out') {
148 $working_status = '0';
149 }
150
151 $timestamp_array = explode('|', $val[0]);
152
153 if (!isset($timestamp_array[1])) {
154 $timestamp_array[1] = $count;
155 }
156
157 $total_time_array[$timestamp_array[1]] = $timestamp_array[0].'|'.$working_status;
158
159 $count++;
160 }
161
162 }
163
164 // reorder array values
165 $total_time_array = array_values($total_time_array);
166
167 $total_time = 0;
168
169 foreach ($total_time_array as $key => $value) {
170
171 $val = explode('|', $value);
172
173 if ($val[1] == 0) {
174
175 $previous = $total_time_array[$key-1];
176 $previous = explode('|', $previous);
177
178 $total_time_previous = $total_time;
179
180 $total_time += $val[0] - $previous[0];
181
182 }
183
184 }
185
186 // error in date - clock out is probably newer then clock in, so we should mark this as 00:00:00 with a review flag
187 if ($total_time < 0) {
188 $total_time = '';
189 update_post_meta($post_id,'notices', true);
190 } else {
191 // remove notices flag if it exists
192 delete_post_meta($post_id,'notices');
193 }
194
195 update_post_meta($post_id, 'total', $total_time);
196 }
197
198
199 // convert php date format to jQuery date format
200 // author Tristan Jahier
201 function etimeclockwp_dateformat_PHP_to_jQueryUI($php_format) {
202 $SYMBOLS_MATCHING = array(
203 // Day
204 'd' => 'dd',
205 'D' => 'D',
206 'j' => 'd',
207 'l' => 'DD',
208 'N' => '',
209 'S' => '',
210 'w' => '',
211 'z' => 'o',
212 // Week
213 'W' => '',
214 // Month
215 'F' => 'MM',
216 'm' => 'mm',
217 'M' => 'M',
218 'n' => 'm',
219 't' => '',
220 // Year
221 'L' => '',
222 'o' => '',
223 'Y' => 'yy',
224 'y' => 'y',
225 // Time
226 'a' => 'tt',
227 'A' => 'TT',
228 'B' => '',
229 'g' => 'h',
230 'G' => 'H',
231 'h' => 'h',
232 'H' => 'H',
233 'i' => 'mm',
234 's' => 'ss',
235 'u' => ''
236 );
237 $jqueryui_format = "";
238 $escaping = false;
239 for($i = 0; $i < strlen($php_format); $i++)
240 {
241 $char = $php_format[$i];
242 if($char === '\\') // PHP date format escaping character
243 {
244 $i++;
245 if($escaping) $jqueryui_format .= $php_format[$i];
246 else $jqueryui_format .= '\'' . $php_format[$i];
247 $escaping = true;
248 }
249 else
250 {
251 if($escaping) { $jqueryui_format .= "'"; $escaping = false; }
252 if(isset($SYMBOLS_MATCHING[$char]))
253 $jqueryui_format .= $SYMBOLS_MATCHING[$char];
254 else
255 $jqueryui_format .= $char;
256 }
257 }
258 return $jqueryui_format;
259 }