PluginProbe
Time Clock – A WordPress Employee & Volunteer Time Clock Plugin / 1.3
Time Clock – A WordPress Employee & Volunteer Time Clock Plugin v1.3
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.3, at includes/functions.php

262 lines 6.1 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 if($seconds > 0) {
83 $t = round($seconds);
84 return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
85 }
86 return '00:00:00';
87 }
88
89
90 // get total time worked
91 function etimeclockwp_get_time_worked($post,$format = true) {
92 $total = get_post_meta($post->ID,'total', true);
93
94 // this is needed due to removing the date part since version 1.2
95 if (empty($total)) {
96 $date_part = get_the_date('Y-m-d',$post->ID);
97 $total = get_post_meta($post->ID,'total_'.$date_part, true);
98 }
99
100 if ($format == true) {
101 $total = etimeclockwp_convert_time($total);
102 }
103 return $total;
104 }
105
106 // get notices for work day - used to display if someone forgot to clockout
107 function etimeclock_get_notices($post_id) {
108 $notices = get_post_meta($post_id,'notices', true);
109 if (!empty($notices)) {
110 $notices = __( 'Admin Review','time-clock');
111 } else {
112 $notices = '-';
113 }
114 return $notices;
115 }
116
117
118 // caculate total time given post id
119 function etimeclockwp_caculate_total_time($post_id) {
120
121 // do a full recaculation based on entry order and don't worry about the existing total time value
122
123 $metavalue = get_post_meta($post_id);
124
125 $total_time_array = array();
126
127 $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
128
129 foreach($metavalue as $key => $val) {
130
131 if (substr($key, 0, 5) === "etime") {
132
133 // get key
134 $key = explode('_', $key);
135 $key = $key[0];
136
137 // caculate working status
138 if ($key == 'etimeclockwp-in') {
139 $working_status = '1';
140 }
141
142 if ($key == 'etimeclockwp-breakon') {
143 $working_status = '0';
144 }
145
146 if ($key == 'etimeclockwp-breakoff') {
147 $working_status = '1';
148 }
149
150 if ($key == 'etimeclockwp-out') {
151 $working_status = '0';
152 }
153
154 $timestamp_array = explode('|', $val[0]);
155
156 if (!isset($timestamp_array[1])) {
157 $timestamp_array[1] = $count;
158 }
159
160 $total_time_array[$timestamp_array[1]] = $timestamp_array[0].'|'.$working_status;
161
162 $count++;
163 }
164
165 }
166
167 // reorder array values
168 $total_time_array = array_values($total_time_array);
169
170 $total_time = 0;
171
172 foreach ($total_time_array as $key => $value) {
173
174 $val = explode('|', $value);
175
176 if ($val[1] == 0) {
177
178 $previous = $total_time_array[$key-1];
179 $previous = explode('|', $previous);
180
181 $total_time_previous = $total_time;
182
183 $total_time += $val[0] - $previous[0];
184
185 }
186
187 }
188
189 // error in date - clock out is probably newer then clock in, so we should mark this as 00:00:00 with a review flag
190 if ($total_time < 0) {
191 $total_time = '';
192 update_post_meta($post_id,'notices', true);
193 } else {
194 // remove notices flag if it exists
195 delete_post_meta($post_id,'notices');
196 }
197
198 update_post_meta($post_id, 'total', $total_time);
199 }
200
201
202 // convert php date format to jQuery date format
203 // author Tristan Jahier
204 function etimeclockwp_dateformat_PHP_to_jQueryUI($php_format) {
205 $SYMBOLS_MATCHING = array(
206 // Day
207 'd' => 'dd',
208 'D' => 'D',
209 'j' => 'd',
210 'l' => 'DD',
211 'N' => '',
212 'S' => '',
213 'w' => '',
214 'z' => 'o',
215 // Week
216 'W' => '',
217 // Month
218 'F' => 'MM',
219 'm' => 'mm',
220 'M' => 'M',
221 'n' => 'm',
222 't' => '',
223 // Year
224 'L' => '',
225 'o' => '',
226 'Y' => 'yy',
227 'y' => 'y',
228 // Time
229 'a' => 'tt',
230 'A' => 'TT',
231 'B' => '',
232 'g' => 'h',
233 'G' => 'H',
234 'h' => 'h',
235 'H' => 'H',
236 'i' => 'mm',
237 's' => 'ss',
238 'u' => ''
239 );
240 $jqueryui_format = "";
241 $escaping = false;
242 for($i = 0; $i < strlen($php_format); $i++)
243 {
244 $char = $php_format[$i];
245 if($char === '\\') // PHP date format escaping character
246 {
247 $i++;
248 if($escaping) $jqueryui_format .= $php_format[$i];
249 else $jqueryui_format .= '\'' . $php_format[$i];
250 $escaping = true;
251 }
252 else
253 {
254 if($escaping) { $jqueryui_format .= "'"; $escaping = false; }
255 if(isset($SYMBOLS_MATCHING[$char]))
256 $jqueryui_format .= $SYMBOLS_MATCHING[$char];
257 else
258 $jqueryui_format .= $char;
259 }
260 }
261 return $jqueryui_format;
262 }