| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
class UpStream_Model_Bug extends UpStream_Model_Meta_Object |
| 10 |
{ |
| 11 |
protected $fileId = 0; |
| 12 |
|
| 13 |
protected $severityCode = null; |
| 14 |
|
| 15 |
protected $statusCode = null; |
| 16 |
|
| 17 |
protected $dueDate = null; |
| 18 |
|
| 19 |
protected $reminders = []; |
| 20 |
|
| 21 |
protected $timeRecords = []; |
| 22 |
|
| 23 |
protected $metadataKey = '_upstream_project_bugs'; |
| 24 |
|
| 25 |
protected $type = UPSTREAM_ITEM_TYPE_BUG; |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* UpStream_Model_Bug constructor. |
| 30 |
*/ |
| 31 |
public function __construct($parent, $item_metadata) |
| 32 |
{ |
| 33 |
parent::__construct($parent, $item_metadata); |
| 34 |
|
| 35 |
$this->type = UPSTREAM_ITEM_TYPE_BUG; |
| 36 |
} |
| 37 |
|
| 38 |
protected function loadFromArray($item_metadata) |
| 39 |
{ |
| 40 |
parent::loadFromArray($item_metadata); |
| 41 |
|
| 42 |
$this->statusCode = !empty($item_metadata['status']) ? $item_metadata['status'] : null; |
| 43 |
$this->severityCode = !empty($item_metadata['severity']) ? $item_metadata['severity'] : null; |
| 44 |
$this->dueDate = UpStream_Model_Object::loadDate($item_metadata, 'due_date'); |
| 45 |
|
| 46 |
if (!empty($item_metadata['file_id'])) { |
| 47 |
$file = get_attached_file($item_metadata['file_id']); |
| 48 |
if ($file != false) { |
| 49 |
$this->fileId = $item_metadata['file_id']; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (!empty($item_metadata['reminders'])) { |
| 54 |
foreach ($item_metadata['reminders'] as $reminder_data) { |
| 55 |
|
| 56 |
try { |
| 57 |
$d = json_decode($reminder_data, true); |
| 58 |
$reminder = new UpStream_Model_Reminder($d); |
| 59 |
$this->reminders[] = $reminder; |
| 60 |
} catch (\Exception $e) { |
| 61 |
// don't add anything else |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($item_metadata['records'])) { |
| 68 |
foreach ($item_metadata['records'] as $tr_data) { |
| 69 |
|
| 70 |
try { |
| 71 |
$d = json_decode($tr_data, true); |
| 72 |
$timeRecord = new UpStream_Model_TimeRecord($d); |
| 73 |
$this->timeRecords[] = $timeRecord; |
| 74 |
} catch (\Exception $e) { |
| 75 |
// don't add anything else |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public function storeToArray(&$item_metadata) |
| 84 |
{ |
| 85 |
parent::storeToArray($item_metadata); |
| 86 |
|
| 87 |
if ($this->statusCode != null) $item_metadata['status'] = $this->statusCode; |
| 88 |
if ($this->severityCode != null) $item_metadata['severity'] = $this->severityCode; |
| 89 |
if ($this->dueDate != null) $item_metadata['due_date'] = UpStream_Model_Object::ymdToTimestamp($this->dueDate); |
| 90 |
if ($this->dueDate != null) $item_metadata['due_date.YMD'] = $this->dueDate; |
| 91 |
|
| 92 |
if ($this->fileId > 0) { |
| 93 |
$url = wp_get_attachment_url($this->fileId); |
| 94 |
|
| 95 |
if ($url != false) { |
| 96 |
$item_metadata['file'] = $url; |
| 97 |
$item_metadata['file_id'] = $this->fileId; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$item_metadata['reminders'] = []; |
| 102 |
|
| 103 |
foreach ($this->reminders as $reminder) { |
| 104 |
$r = []; |
| 105 |
$reminder->storeToArray($r); |
| 106 |
$item_metadata['reminders'][] = json_encode($r); |
| 107 |
} |
| 108 |
|
| 109 |
$item_metadata['time_records'] = []; |
| 110 |
|
| 111 |
foreach ($this->timeRecords as $tr) { |
| 112 |
$r = []; |
| 113 |
$tr->storeToArray($r); |
| 114 |
$item_metadata['records'][] = json_encode($r); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function calculateElapsedTime() |
| 119 |
{ |
| 120 |
$total = 0; |
| 121 |
|
| 122 |
foreach ($this->timeRecords as $tr) { |
| 123 |
$total += $tr->elapsedTime; |
| 124 |
} |
| 125 |
|
| 126 |
return $total; |
| 127 |
} |
| 128 |
|
| 129 |
public function calculateBudgeted() |
| 130 |
{ |
| 131 |
$total = 0; |
| 132 |
|
| 133 |
foreach ($this->timeRecords as $tr) { |
| 134 |
$total += $tr->budgeted; |
| 135 |
} |
| 136 |
|
| 137 |
return $total; |
| 138 |
} |
| 139 |
|
| 140 |
public function calculateSpent() |
| 141 |
{ |
| 142 |
$total = 0; |
| 143 |
|
| 144 |
foreach ($this->timeRecords as $tr) { |
| 145 |
$total += $tr->spent; |
| 146 |
} |
| 147 |
|
| 148 |
return $total; |
| 149 |
} |
| 150 |
|
| 151 |
public function __get($property) |
| 152 |
{ |
| 153 |
switch ($property) { |
| 154 |
|
| 155 |
case 'severity': |
| 156 |
$s = $this->getSeverities(); |
| 157 |
|
| 158 |
foreach ($s as $sKey => $sValue) { |
| 159 |
if ($this->severityCode === $sKey) |
| 160 |
return $sValue; |
| 161 |
} |
| 162 |
return ''; |
| 163 |
|
| 164 |
case 'status': |
| 165 |
$s = $this->getStatuses(); |
| 166 |
|
| 167 |
foreach ($s as $sKey => $sValue) { |
| 168 |
if ($this->statusCode === $sKey) |
| 169 |
return $sValue; |
| 170 |
} |
| 171 |
return ''; |
| 172 |
|
| 173 |
case 'elapsedTime': |
| 174 |
return $this->calculateElapsedTime(); |
| 175 |
case 'budgeted': |
| 176 |
return $this->calculateBudgeted(); |
| 177 |
case 'spent': |
| 178 |
return $this->calculateSpent(); |
| 179 |
|
| 180 |
case 'dueDate': |
| 181 |
case 'fileId': |
| 182 |
case 'timeRecords': |
| 183 |
case 'severityCode': |
| 184 |
case 'statusCode': |
| 185 |
return $this->{$property}; |
| 186 |
|
| 187 |
default: |
| 188 |
return parent::__get($property); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public function __set($property, $value) |
| 193 |
{ |
| 194 |
switch ($property) { |
| 195 |
|
| 196 |
case 'fileId': |
| 197 |
$file = get_attached_file($value); |
| 198 |
if ($file === false) |
| 199 |
throw new UpStream_Model_ArgumentException(sprintf(__('File ID %s is invalid.', 'upstream'), $value)); |
| 200 |
|
| 201 |
$this->fileId = $value; |
| 202 |
break; |
| 203 |
|
| 204 |
case 'severity': |
| 205 |
$s = $this->getSeverities(); |
| 206 |
$sc = null; |
| 207 |
|
| 208 |
foreach ($s as $sKey => $sValue) { |
| 209 |
if ($value === $sValue) { |
| 210 |
$sc = $sKey; |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if ($sc == null) |
| 216 |
throw new UpStream_Model_ArgumentException(sprintf(__('Severity %s is invalid.', 'upstream'), $value)); |
| 217 |
|
| 218 |
$this->severityCode = $sc; |
| 219 |
break; |
| 220 |
|
| 221 |
case 'severityCode': |
| 222 |
$s = $this->getSeverities(); |
| 223 |
$sc = null; |
| 224 |
|
| 225 |
foreach ($s as $sKey => $sValue) { |
| 226 |
if ($value === $sKey) { |
| 227 |
$sc = $sKey; |
| 228 |
break; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if ($sc == null) |
| 233 |
throw new UpStream_Model_ArgumentException(sprintf(__('Severity code %s is invalid.', 'upstream'), $value)); |
| 234 |
|
| 235 |
$this->severityCode = $sc; |
| 236 |
break; |
| 237 |
|
| 238 |
case 'status': |
| 239 |
$s = $this->getStatuses(); |
| 240 |
$sc = null; |
| 241 |
|
| 242 |
foreach ($s as $sKey => $sValue) { |
| 243 |
if ($value === $sValue) { |
| 244 |
$sc = $sKey; |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if ($sc == null) |
| 250 |
throw new UpStream_Model_ArgumentException(sprintf(__('Status %s is invalid.', 'upstream'), $value)); |
| 251 |
|
| 252 |
$this->statusCode = $sc; |
| 253 |
|
| 254 |
break; |
| 255 |
|
| 256 |
case 'statusCode': |
| 257 |
$s = $this->getStatuses(); |
| 258 |
$sc = null; |
| 259 |
|
| 260 |
foreach ($s as $sKey => $sValue) { |
| 261 |
if ($value === $sKey) { |
| 262 |
$sc = $sKey; |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if ($sc == null) |
| 268 |
throw new UpStream_Model_ArgumentException(sprintf(__('Status code %s is invalid.', 'upstream'), $value)); |
| 269 |
|
| 270 |
$this->statusCode = $sc; |
| 271 |
break; |
| 272 |
|
| 273 |
case 'dueDate': |
| 274 |
if (!self::isValidDate($value)) |
| 275 |
throw new UpStream_Model_ArgumentException(__('Argument is not a valid date of the form YYYY-MM-DD.', 'upstream')); |
| 276 |
|
| 277 |
$this->{$property} = $value; |
| 278 |
break; |
| 279 |
|
| 280 |
case 'timeRecords': |
| 281 |
if (!is_array($value)) |
| 282 |
throw new UpStream_Model_ArgumentException(__('Argument must be an array of UpStream_Model_TimeRecord objects.', 'upstream')); |
| 283 |
|
| 284 |
foreach ($value as $item) { |
| 285 |
if (!$item instanceof UpStream_Model_TimeRecord) |
| 286 |
throw new UpStream_Model_ArgumentException(__('Argument must be an array of UpStream_Model_TimeRecord objects.', 'upstream')); |
| 287 |
} |
| 288 |
|
| 289 |
$this->{$property} = $value; |
| 290 |
break; |
| 291 |
|
| 292 |
default: |
| 293 |
parent::__set($property, $value); |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
public static function fields() |
| 300 |
{ |
| 301 |
$fields = parent::fields(); |
| 302 |
|
| 303 |
$fields['statusCode'] = [ 'type' => 'select', 'title' => __('Status'), 'search' => true, 'display' => true, 'options_cb' => 'UpStream_Model_Bug::getStatuses' ]; |
| 304 |
$fields['severityCode'] = [ 'type' => 'select', 'title' => __('Severity'), 'search' => true, 'display' => true, 'options_cb' => 'UpStream_Model_Bug::getSeverities' ]; |
| 305 |
$fields['dueDate'] = [ 'type' => 'date', 'title' => __('Due Date'), 'search' => true, 'display' => true ]; |
| 306 |
|
| 307 |
$fields = self::customFields($fields, UPSTREAM_ITEM_TYPE_BUG); |
| 308 |
|
| 309 |
return $fields; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public static function getStatuses() |
| 314 |
{ |
| 315 |
$option = get_option('upstream_bugs'); |
| 316 |
$statuses = isset($option['statuses']) ? $option['statuses'] : ''; |
| 317 |
$array = []; |
| 318 |
if ($statuses) { |
| 319 |
foreach ($statuses as $status) { |
| 320 |
if (isset($status['name'])) { |
| 321 |
$array[$status['id']] = $status['name']; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
return $array; |
| 327 |
} |
| 328 |
|
| 329 |
public static function getSeverities() |
| 330 |
{ |
| 331 |
$option = get_option('upstream_bugs'); |
| 332 |
$severities = isset($option['severities']) ? $option['severities'] : ''; |
| 333 |
$array = []; |
| 334 |
if ($severities) { |
| 335 |
foreach ($severities as $severity) { |
| 336 |
if (isset($severity['name'])) { |
| 337 |
$array[$severity['id']] = $severity['name']; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
return $array; |
| 343 |
} |
| 344 |
|
| 345 |
public static function create($parent, $title, $createdBy) |
| 346 |
{ |
| 347 |
$item_metadata = ['title' => $title, 'created_by' => $createdBy]; |
| 348 |
|
| 349 |
return new self($parent, $item_metadata); |
| 350 |
} |
| 351 |
|
| 352 |
} |