| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Model_TimeRecord |
| 4 |
* |
| 5 |
* WordPress Coding Standart (WCS) note: |
| 6 |
* All camelCase methods and object properties on this file are not converted to snake_case, |
| 7 |
* because it being used (heavily) on another add-on plugins. |
| 8 |
* |
| 9 |
* @package UpStream |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class UpStream_Model_TimeRecord |
| 19 |
*/ |
| 20 |
class UpStream_Model_TimeRecord { |
| 21 |
|
| 22 |
/** |
| 23 |
* Id |
| 24 |
* |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
protected $id = 0; |
| 28 |
|
| 29 |
/** |
| 30 |
* User |
| 31 |
* |
| 32 |
* @var int |
| 33 |
*/ |
| 34 |
protected $user = 0; |
| 35 |
|
| 36 |
/** |
| 37 |
* Start Timestamp |
| 38 |
* |
| 39 |
* @var int |
| 40 |
*/ |
| 41 |
protected $startTimestamp = 0; // phpcs:ignore |
| 42 |
|
| 43 |
/** |
| 44 |
* Elapsed Time |
| 45 |
* |
| 46 |
* @var int |
| 47 |
*/ |
| 48 |
protected $elapsedTime = 0; // phpcs:ignore |
| 49 |
|
| 50 |
/** |
| 51 |
* Budgeted |
| 52 |
* |
| 53 |
* @var int |
| 54 |
*/ |
| 55 |
protected $budgeted = 0; |
| 56 |
|
| 57 |
/** |
| 58 |
* Spent |
| 59 |
* |
| 60 |
* @var int |
| 61 |
*/ |
| 62 |
protected $spent = 0; |
| 63 |
|
| 64 |
/** |
| 65 |
* Note |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
protected $note = ''; |
| 70 |
|
| 71 |
/** |
| 72 |
* Construct |
| 73 |
* |
| 74 |
* @param mixed $item_metadata Item Metadata. |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function __construct( $item_metadata ) { |
| 78 |
$this->loadFromArray( $item_metadata ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Load From Array |
| 83 |
* |
| 84 |
* @param mixed $item_metadata Item Metadata. |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
protected function loadFromArray( $item_metadata ) { // phpcs:ignore |
| 88 |
$this->id = ! empty( $item_metadata['id'] ) ? $item_metadata['id'] : 0; |
| 89 |
$this->user = ! empty( $item_metadata['user'] ) ? $item_metadata['user'] : 0; |
| 90 |
$this->startTimestamp = ! empty( $item_metadata['startTimestamp'] ) ? $item_metadata['startTimestamp'] : 0; // phpcs:ignore |
| 91 |
$this->elapsedTime = ! empty( $item_metadata['elapsedTime'] ) ? $item_metadata['elapsedTime'] : 0; // phpcs:ignore |
| 92 |
$this->budgeted = ! empty( $item_metadata['budgeted'] ) ? $item_metadata['budgeted'] : null; |
| 93 |
$this->spent = ! empty( $item_metadata['spent'] ) ? $item_metadata['spent'] : null; |
| 94 |
$this->note = ! empty( $item_metadata['note'] ) ? $item_metadata['note'] : null; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Store To Array |
| 99 |
* |
| 100 |
* @param mixed $item_metadata Item Metadata. |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function storeToArray( &$item_metadata ) { // phpcs:ignore |
| 104 |
if ( $this->startTimestamp > 0 ) { // phpcs:ignore |
| 105 |
$hours = round( ( time() - $this->startTimestamp ) / 3600, 1 ); // phpcs:ignore |
| 106 |
$this->elapsedTime = $hours; // phpcs:ignore |
| 107 |
} |
| 108 |
|
| 109 |
$item_metadata['id'] = $this->id; |
| 110 |
$item_metadata['user'] = $this->user; |
| 111 |
$item_metadata['startTimestamp'] = $this->startTimestamp; // phpcs:ignore |
| 112 |
$item_metadata['elapsedTime'] = $this->elapsedTime; // phpcs:ignore |
| 113 |
$item_metadata['budgeted'] = $this->budgeted; |
| 114 |
$item_metadata['spent'] = $this->spent; |
| 115 |
$item_metadata['note'] = $this->note; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Work Hours Per Day |
| 120 |
*/ |
| 121 |
public static function workHoursPerDay() { // phpcs:ignore |
| 122 |
$options = get_option( 'upstream_general' ); |
| 123 |
$option_name = 'local_work_hours_per_day'; |
| 124 |
$hrs = isset( $options[ $option_name ] ) ? (int) $options[ $option_name ] : 8; |
| 125 |
|
| 126 |
return $hrs; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Monetary Symbol |
| 131 |
*/ |
| 132 |
public static function monetarySymbol() { // phpcs:ignore |
| 133 |
$options = get_option( 'upstream_general' ); |
| 134 |
$option_name = 'local_monetary_symbol'; |
| 135 |
$sym = isset( $options[ $option_name ] ) ? $options[ $option_name ] : '$'; |
| 136 |
|
| 137 |
return $sym; |
| 138 |
} |
| 139 |
|
| 140 |
// Phpcs ignore camelCase methods and object properties. |
| 141 |
// phpcs:disable |
| 142 |
|
| 143 |
/** |
| 144 |
* Start Timing |
| 145 |
*/ |
| 146 |
public function startTiming() { |
| 147 |
$this->startTimestamp = time(); |
| 148 |
$this->elapsedTime = 0; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Stop Timing |
| 153 |
*/ |
| 154 |
public function stopTiming() { |
| 155 |
$hours = round( ( time() - $this->startTimestamp ) / 3600, 1 ); |
| 156 |
$this->elapsedTime = $hours; |
| 157 |
$this->startTimestamp = 0; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Is Timing |
| 162 |
*/ |
| 163 |
public function isTiming() { |
| 164 |
return ( 0 !== $this->startTimestamp ); |
| 165 |
} |
| 166 |
|
| 167 |
// phpcs:enable |
| 168 |
|
| 169 |
/** |
| 170 |
* Format Elapsed |
| 171 |
* |
| 172 |
* @param mixed $elapsed Elapsed. |
| 173 |
*/ |
| 174 |
public static function formatElapsed( $elapsed ) { // phpcs:ignore |
| 175 |
$days = floor( $elapsed / self::workHoursPerDay() ); |
| 176 |
$hours = $elapsed - ( $days * self::workHoursPerDay() ); |
| 177 |
return round( $days, 0 ) . ' days' . ( $hours > 0 ? ', ' . round( $hours, 1 ) . ' hours' : '' ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Format Budgeted |
| 182 |
* |
| 183 |
* @param mixed $budgeted Budgeted. |
| 184 |
*/ |
| 185 |
public static function formatBudgeted( $budgeted ) { // phpcs:ignore |
| 186 |
return self::monetarySymbol() . $budgeted; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Format Spent |
| 191 |
* |
| 192 |
* @param mixed $spent Spent. |
| 193 |
*/ |
| 194 |
public static function formatSpent( $spent ) { // phpcs:ignore |
| 195 |
return self::monetarySymbol() . $spent; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get |
| 200 |
* |
| 201 |
* @param mixed $property Property. |
| 202 |
* @throws UpStream_Model_ArgumentException Exception. |
| 203 |
*/ |
| 204 |
public function __get( $property ) { |
| 205 |
$property = apply_filters( 'upstream_wcs_model_variable', $property ); |
| 206 |
|
| 207 |
switch ( $property ) { |
| 208 |
|
| 209 |
case 'id': |
| 210 |
case 'startTimestamp': |
| 211 |
case 'user': |
| 212 |
case 'note': |
| 213 |
case 'budgeted': |
| 214 |
case 'spent': |
| 215 |
return $this->{$property}; |
| 216 |
|
| 217 |
case 'elapsedTime': |
| 218 |
if ( $this->startTimestamp > 0 ) { // phpcs:ignore |
| 219 |
$hours = round( ( time() - $this->startTimestamp ) / 3600, 1 ); // phpcs:ignore |
| 220 |
$this->elapsedTime = $hours; // phpcs:ignore |
| 221 |
} |
| 222 |
|
| 223 |
return $this->elapsedTime; // phpcs:ignore |
| 224 |
|
| 225 |
default: |
| 226 |
throw new UpStream_Model_ArgumentException( |
| 227 |
sprintf( |
| 228 |
// translators: %s: property. |
| 229 |
__( 'This (%s) is not a valid property.', 'upstream' ), |
| 230 |
$property |
| 231 |
) |
| 232 |
); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Set |
| 238 |
* |
| 239 |
* @param mixed $property Property. |
| 240 |
* @param mixed $value Value. |
| 241 |
* @throws UpStream_Model_ArgumentException Exception. |
| 242 |
*/ |
| 243 |
public function __set( $property, $value ) { |
| 244 |
$property = apply_filters( 'upstream_wcs_model_variable', $property ); |
| 245 |
|
| 246 |
switch ( $property ) { |
| 247 |
|
| 248 |
case 'id': |
| 249 |
if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $value ) ) { |
| 250 |
throw new UpStream_Model_ArgumentException( |
| 251 |
sprintf( |
| 252 |
// translators: %s: ID. |
| 253 |
__( 'ID %s must be a valid alphanumeric.', 'upstream' ), |
| 254 |
$value |
| 255 |
) |
| 256 |
); |
| 257 |
} |
| 258 |
$this->{$property} = $value; |
| 259 |
break; |
| 260 |
|
| 261 |
case 'note': |
| 262 |
$this->{$property} = wp_kses_post( $value ); |
| 263 |
break; |
| 264 |
|
| 265 |
case 'user': |
| 266 |
case 'user:byUsername': |
| 267 |
case 'user:byEmail': |
| 268 |
$uid = $value; |
| 269 |
$user = false; |
| 270 |
|
| 271 |
if ( 'user' === $property ) { |
| 272 |
$user = get_user_by( 'id', $uid ); |
| 273 |
} |
| 274 |
if ( 'user:byUsername' === $property ) { |
| 275 |
$user = get_user_by( 'login', $uid ); |
| 276 |
} |
| 277 |
if ( 'user:byEmail' === $property ) { |
| 278 |
$user = get_user_by( 'email', $uid ); |
| 279 |
} |
| 280 |
|
| 281 |
if ( false === $user ) { |
| 282 |
throw new UpStream_Model_ArgumentException( |
| 283 |
sprintf( |
| 284 |
// translators: %1$s: user. |
| 285 |
// translators: %2$s: field. |
| 286 |
__( 'User "%1$s" (for field %2$s) does not exist.', 'upstream' ), |
| 287 |
$uid, |
| 288 |
$property |
| 289 |
) |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
$this->user = $user->ID; |
| 294 |
break; |
| 295 |
|
| 296 |
case 'spent': |
| 297 |
case 'budgeted': |
| 298 |
if ( $value && ! is_numeric( $value ) ) { |
| 299 |
throw new UpStream_Model_ArgumentException( |
| 300 |
sprintf( |
| 301 |
// translators: %s: value. |
| 302 |
__( 'This (%s) is not a number.', 'upstream' ), |
| 303 |
$property |
| 304 |
) |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
$this->{$property} = $value; |
| 309 |
|
| 310 |
break; |
| 311 |
|
| 312 |
case 'startTimestamp': |
| 313 |
case 'elapsedTime': |
| 314 |
if ( ! is_numeric( $value ) ) { |
| 315 |
throw new UpStream_Model_ArgumentException( |
| 316 |
sprintf( |
| 317 |
// translators: %s: number value. |
| 318 |
__( 'This (%s) is not a number.', 'upstream' ), |
| 319 |
$property |
| 320 |
) |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
$this->{$property} = $value; |
| 325 |
|
| 326 |
break; |
| 327 |
|
| 328 |
default: |
| 329 |
throw new UpStream_Model_ArgumentException( |
| 330 |
sprintf( |
| 331 |
// translators: %s: property. |
| 332 |
__( 'This (%s) is not a valid property.', 'upstream' ), |
| 333 |
$property |
| 334 |
) |
| 335 |
); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|