| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
class UpStream_Model_TimeRecord |
| 10 |
{ |
| 11 |
protected $id = 0; |
| 12 |
|
| 13 |
protected $user = 0; |
| 14 |
|
| 15 |
protected $startTimestamp = 0; |
| 16 |
|
| 17 |
protected $elapsedTime = 0; |
| 18 |
|
| 19 |
protected $budgeted = 0; |
| 20 |
|
| 21 |
protected $spent = 0; |
| 22 |
|
| 23 |
protected $note = ''; |
| 24 |
|
| 25 |
/** |
| 26 |
* UpStream_Model_Reminder constructor. |
| 27 |
*/ |
| 28 |
public function __construct($item_metadata) |
| 29 |
{ |
| 30 |
$this->loadFromArray($item_metadata); |
| 31 |
} |
| 32 |
|
| 33 |
protected function loadFromArray($item_metadata) |
| 34 |
{ |
| 35 |
$this->id = !empty($item_metadata['id']) ? $item_metadata['id'] : 0; |
| 36 |
$this->user = !empty($item_metadata['user']) ? $item_metadata['user'] : 0; |
| 37 |
$this->startTimestamp = !empty($item_metadata['startTimestamp']) ? $item_metadata['startTimestamp'] : 0; |
| 38 |
$this->elapsedTime = !empty($item_metadata['elapsedTime']) ? $item_metadata['elapsedTime'] : 0; |
| 39 |
$this->budgeted = !empty($item_metadata['budgeted']) ? $item_metadata['budgeted'] : null; |
| 40 |
$this->spent = !empty($item_metadata['spent']) ? $item_metadata['spent'] : null; |
| 41 |
$this->note = !empty($item_metadata['note']) ? $item_metadata['note'] : null; |
| 42 |
} |
| 43 |
|
| 44 |
public function storeToArray(&$item_metadata) |
| 45 |
{ |
| 46 |
if ($this->startTimestamp > 0) { |
| 47 |
$hours = round((time() - $this->startTimestamp) / 3600, 1); |
| 48 |
$this->elapsedTime = $hours; |
| 49 |
} |
| 50 |
|
| 51 |
$item_metadata['id'] = $this->id; |
| 52 |
$item_metadata['user'] = $this->user; |
| 53 |
$item_metadata['startTimestamp'] = $this->startTimestamp; |
| 54 |
$item_metadata['elapsedTime'] = $this->elapsedTime; |
| 55 |
$item_metadata['budgeted'] = $this->budgeted; |
| 56 |
$item_metadata['spent'] = $this->spent; |
| 57 |
$item_metadata['note'] = $this->note; |
| 58 |
} |
| 59 |
|
| 60 |
public static function workHoursPerDay() |
| 61 |
{ |
| 62 |
$options = get_option('upstream_general'); |
| 63 |
$optionName = 'local_work_hours_per_day'; |
| 64 |
$hrs = isset($options[$optionName]) ? (int)$options[$optionName] : 8; |
| 65 |
|
| 66 |
return $hrs; |
| 67 |
} |
| 68 |
|
| 69 |
public static function monetarySymbol() |
| 70 |
{ |
| 71 |
$options = get_option('upstream_general'); |
| 72 |
$optionName = 'local_monetary_symbol'; |
| 73 |
$sym = isset($options[$optionName]) ? $options[$optionName] : '$'; |
| 74 |
|
| 75 |
return $sym; |
| 76 |
} |
| 77 |
|
| 78 |
public function startTiming() |
| 79 |
{ |
| 80 |
$this->startTimestamp = time(); |
| 81 |
$this->elapsedTime = 0; |
| 82 |
} |
| 83 |
|
| 84 |
public function stopTiming() |
| 85 |
{ |
| 86 |
$hours = round((time() - $this->startTimestamp) / 3600, 1); |
| 87 |
$this->elapsedTime = $hours; |
| 88 |
$this->startTimestamp = 0; |
| 89 |
} |
| 90 |
|
| 91 |
public function isTiming() |
| 92 |
{ |
| 93 |
return ($this->startTimestamp != 0); |
| 94 |
} |
| 95 |
|
| 96 |
public static function formatElapsed($elapsed) |
| 97 |
{ |
| 98 |
$days = floor($elapsed / self::workHoursPerDay()); |
| 99 |
$hours = $elapsed - ($days * self::workHoursPerDay()); |
| 100 |
return round($days, 0) . ' days' . ($hours > 0 ? ', ' . round($hours, 1) . ' hours' : ''); |
| 101 |
} |
| 102 |
|
| 103 |
public static function formatBudgeted($budgeted) |
| 104 |
{ |
| 105 |
return self::monetarySymbol() . $budgeted; |
| 106 |
} |
| 107 |
|
| 108 |
public static function formatSpent($spent) |
| 109 |
{ |
| 110 |
return self::monetarySymbol() . $spent; |
| 111 |
} |
| 112 |
|
| 113 |
public function __get($property) |
| 114 |
{ |
| 115 |
switch ($property) { |
| 116 |
|
| 117 |
case 'id': |
| 118 |
case 'startTimestamp': |
| 119 |
case 'user': |
| 120 |
case 'note': |
| 121 |
case 'budgeted': |
| 122 |
case 'spent': |
| 123 |
return $this->{$property}; |
| 124 |
|
| 125 |
case 'elapsedTime': |
| 126 |
if ($this->startTimestamp > 0) { |
| 127 |
$hours = round((time() - $this->startTimestamp) / 3600, 1); |
| 128 |
$this->elapsedTime = $hours; |
| 129 |
} |
| 130 |
|
| 131 |
return $this->elapsedTime; |
| 132 |
|
| 133 |
default: |
| 134 |
throw new UpStream_Model_ArgumentException(sprintf(__('This (%s) is not a valid property.', 'upstream'), $property)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public function __set($property, $value) |
| 139 |
{ |
| 140 |
switch ($property) { |
| 141 |
|
| 142 |
case 'id': |
| 143 |
if (!preg_match('/^[a-zA-Z0-9]+$/', $value)) |
| 144 |
throw new UpStream_Model_ArgumentException(sprintf(__('ID %s must be a valid alphanumeric.', 'upstream'), $value)); |
| 145 |
$this->{$property} = $value; |
| 146 |
break; |
| 147 |
|
| 148 |
case 'note': |
| 149 |
$this->{$property} = wp_kses_post($value); |
| 150 |
break; |
| 151 |
|
| 152 |
case 'user': |
| 153 |
case 'user:byUsername': |
| 154 |
case 'user:byEmail': |
| 155 |
$uid = $value; |
| 156 |
$user = false; |
| 157 |
|
| 158 |
if ($property === 'user') |
| 159 |
$user = get_user_by('id', $uid); |
| 160 |
if ($property === 'user:byUsername') |
| 161 |
$user = get_user_by('login', $uid); |
| 162 |
if ($property === 'user:byEmail') |
| 163 |
$user = get_user_by('email', $uid); |
| 164 |
|
| 165 |
if ($user === false) |
| 166 |
throw new UpStream_Model_ArgumentException(sprintf(__('User "%s" (for field %s) does not exist.', 'upstream'), $uid, $property)); |
| 167 |
|
| 168 |
$this->user = $user->ID; |
| 169 |
break; |
| 170 |
|
| 171 |
case 'spent': |
| 172 |
case 'budgeted': |
| 173 |
if ($value && !is_numeric($value)) |
| 174 |
throw new UpStream_Model_ArgumentException(sprintf(__('This (%s) is not a number.', 'upstream'), $property)); |
| 175 |
|
| 176 |
$this->{$property} = $value; |
| 177 |
|
| 178 |
break; |
| 179 |
|
| 180 |
case 'startTimestamp': |
| 181 |
case 'elapsedTime': |
| 182 |
if (!is_numeric($value)) |
| 183 |
throw new UpStream_Model_ArgumentException(sprintf(__('This (%s) is not a number.', 'upstream'), $property)); |
| 184 |
|
| 185 |
$this->{$property} = $value; |
| 186 |
|
| 187 |
break; |
| 188 |
|
| 189 |
default: |
| 190 |
throw new UpStream_Model_ArgumentException(sprintf(__('This (%s) is not a valid property.', 'upstream'), $property)); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |