| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Model_Bug |
| 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_Bug |
| 19 |
*/ |
| 20 |
class UpStream_Model_Bug extends UpStream_Model_Meta_Object { |
| 21 |
|
| 22 |
/** |
| 23 |
* FileId |
| 24 |
* |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
protected $fileId = 0; // phpcs:ignore |
| 28 |
|
| 29 |
/** |
| 30 |
* SeverityCode |
| 31 |
* |
| 32 |
* @var undefined |
| 33 |
*/ |
| 34 |
protected $severityCode = null; // phpcs:ignore |
| 35 |
|
| 36 |
/** |
| 37 |
* StatusCode |
| 38 |
* |
| 39 |
* @var undefined |
| 40 |
*/ |
| 41 |
protected $statusCode = null; // phpcs:ignore |
| 42 |
|
| 43 |
/** |
| 44 |
* DueDate |
| 45 |
* |
| 46 |
* @var undefined |
| 47 |
*/ |
| 48 |
protected $dueDate = null; // phpcs:ignore |
| 49 |
|
| 50 |
/** |
| 51 |
* Reminders |
| 52 |
* |
| 53 |
* @var array |
| 54 |
*/ |
| 55 |
protected $reminders = array(); |
| 56 |
|
| 57 |
/** |
| 58 |
* TimeRecords |
| 59 |
* |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
protected $timeRecords = array(); // phpcs:ignore |
| 63 |
|
| 64 |
/** |
| 65 |
* MetadataKey |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
protected $metadataKey = '_upstream_project_bugs'; // phpcs:ignore |
| 70 |
|
| 71 |
/** |
| 72 |
* Type |
| 73 |
* |
| 74 |
* @var undefined |
| 75 |
*/ |
| 76 |
protected $type = UPSTREAM_ITEM_TYPE_BUG; |
| 77 |
|
| 78 |
/** |
| 79 |
* Constructor |
| 80 |
* |
| 81 |
* @param mixed $parent parent. |
| 82 |
* @param mixed $item_metadata item_metadata. |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function __construct( $parent, $item_metadata ) { |
| 86 |
parent::__construct( $parent, $item_metadata ); |
| 87 |
|
| 88 |
$this->type = UPSTREAM_ITEM_TYPE_BUG; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* LoadFromArray |
| 93 |
* |
| 94 |
* @param mixed $item_metadata item_metadata. |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
protected function loadFromArray( $item_metadata ) { |
| 98 |
parent::loadFromArray( $item_metadata ); |
| 99 |
|
| 100 |
$this->statusCode = ! empty( $item_metadata['status'] ) ? $item_metadata['status'] : null; // phpcs:ignore |
| 101 |
$this->severityCode = ! empty( $item_metadata['severity'] ) ? $item_metadata['severity'] : null; // phpcs:ignore |
| 102 |
$this->dueDate = UpStream_Model_Object::loadDate( $item_metadata, 'due_date' ); // phpcs:ignore |
| 103 |
|
| 104 |
if ( ! empty( $item_metadata['file_id'] ) ) { |
| 105 |
$file = get_attached_file( $item_metadata['file_id'] ); |
| 106 |
if ( false !== $file ) { |
| 107 |
$this->fileId = $item_metadata['file_id']; // phpcs:ignore |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! empty( $item_metadata['reminders'] ) ) { |
| 112 |
foreach ( $item_metadata['reminders'] as $reminder_data ) { |
| 113 |
$exception = null; |
| 114 |
try { |
| 115 |
$d = json_decode( $reminder_data, true ); |
| 116 |
$reminder = new UpStream_Model_Reminder( $d ); |
| 117 |
$this->reminders[] = $reminder; |
| 118 |
} catch ( \Exception $e ) { |
| 119 |
$exception = $e; // don't add anything else. |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! empty( $item_metadata['records'] ) ) { |
| 125 |
foreach ( $item_metadata['records'] as $tr_data ) { |
| 126 |
$exception = null; |
| 127 |
try { |
| 128 |
$d = json_decode( $tr_data, true ); |
| 129 |
$time_record = new UpStream_Model_TimeRecord( $d ); |
| 130 |
$this->timeRecords[] = $time_record; // phpcs:ignore |
| 131 |
} catch ( \Exception $e ) { |
| 132 |
$exception = $e; // don't add anything else. |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* StoreToArray |
| 141 |
* |
| 142 |
* @param mixed $item_metadata item_metadata. |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function storeToArray( &$item_metadata ) { |
| 146 |
parent::storeToArray( $item_metadata ); |
| 147 |
|
| 148 |
if ( null !== $this->statusCode ) { // phpcs:ignore |
| 149 |
$item_metadata['status'] = $this->statusCode; // phpcs:ignore |
| 150 |
} |
| 151 |
if ( null !== $this->severityCode ) { // phpcs:ignore |
| 152 |
$item_metadata['severity'] = $this->severityCode; // phpcs:ignore |
| 153 |
} |
| 154 |
if ( null !== $this->dueDate ) { // phpcs:ignore |
| 155 |
$item_metadata['due_date'] = UpStream_Model_Object::ymdToTimestamp( $this->dueDate ); // phpcs:ignore |
| 156 |
} |
| 157 |
if ( null !== $this->dueDate ) { // phpcs:ignore |
| 158 |
$item_metadata['due_date.YMD'] = $this->dueDate; // phpcs:ignore |
| 159 |
} |
| 160 |
|
| 161 |
if ( $this->fileId > 0 ) { // phpcs:ignore |
| 162 |
$url = wp_get_attachment_url( $this->fileId ); // phpcs:ignore |
| 163 |
|
| 164 |
if ( false !== $url ) { |
| 165 |
$item_metadata['file'] = $url; |
| 166 |
$item_metadata['file_id'] = $this->fileId; // phpcs:ignore |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
$item_metadata['reminders'] = array(); |
| 171 |
|
| 172 |
foreach ( $this->reminders as $reminder ) { |
| 173 |
$r = array(); |
| 174 |
$reminder->storeToArray( $r ); |
| 175 |
$item_metadata['reminders'][] = wp_json_encode( $r ); |
| 176 |
} |
| 177 |
|
| 178 |
$item_metadata['time_records'] = array(); |
| 179 |
|
| 180 |
foreach ( $this->timeRecords as $tr ) { // phpcs:ignore |
| 181 |
$r = array(); |
| 182 |
$tr->storeToArray( $r ); |
| 183 |
$item_metadata['records'][] = wp_json_encode( $r ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* CalculateElapsedTime |
| 189 |
*/ |
| 190 |
public function calculateElapsedTime() { |
| 191 |
$total = 0; |
| 192 |
|
| 193 |
foreach ( $this->timeRecords as $tr ) { // phpcs:ignore |
| 194 |
$total += $tr->elapsedTime; // phpcs:ignore |
| 195 |
} |
| 196 |
|
| 197 |
return $total; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* CalculateBudgeted |
| 202 |
*/ |
| 203 |
public function calculateBudgeted() { |
| 204 |
$total = 0; |
| 205 |
|
| 206 |
foreach ( $this->timeRecords as $tr ) { // phpcs:ignore |
| 207 |
$total += $tr->budgeted; |
| 208 |
} |
| 209 |
|
| 210 |
return $total; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* CalculateSpent |
| 215 |
*/ |
| 216 |
public function calculateSpent() { |
| 217 |
$total = 0; |
| 218 |
|
| 219 |
foreach ( $this->timeRecords as $tr ) { // phpcs:ignore |
| 220 |
$total += $tr->spent; |
| 221 |
} |
| 222 |
|
| 223 |
return $total; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Get |
| 228 |
* |
| 229 |
* @param mixed $property property. |
| 230 |
*/ |
| 231 |
public function __get( $property ) { |
| 232 |
$property = apply_filters( 'upstream_wcs_model_variable', $property ); |
| 233 |
|
| 234 |
switch ( $property ) { |
| 235 |
|
| 236 |
case 'severity': |
| 237 |
$s = $this->getSeverities(); |
| 238 |
|
| 239 |
foreach ( $s as $s_key => $s_value ) { |
| 240 |
if ( $this->severityCode === $s_key ) { // phpcs:ignore |
| 241 |
return $s_value; |
| 242 |
} |
| 243 |
} |
| 244 |
return ''; |
| 245 |
|
| 246 |
case 'status': |
| 247 |
$s = $this->getStatuses(); |
| 248 |
|
| 249 |
foreach ( $s as $s_key => $s_value ) { |
| 250 |
if ( $this->statusCode === $s_key ) { // phpcs:ignore |
| 251 |
return $s_value; |
| 252 |
} |
| 253 |
} |
| 254 |
return ''; |
| 255 |
|
| 256 |
case 'elapsedTime': |
| 257 |
return $this->calculateElapsedTime(); |
| 258 |
case 'budgeted': |
| 259 |
return $this->calculateBudgeted(); |
| 260 |
case 'spent': |
| 261 |
return $this->calculateSpent(); |
| 262 |
|
| 263 |
case 'dueDate': |
| 264 |
case 'fileId': |
| 265 |
case 'timeRecords': |
| 266 |
case 'severityCode': |
| 267 |
case 'statusCode': |
| 268 |
return $this->{$property}; |
| 269 |
|
| 270 |
default: |
| 271 |
return parent::__get( $property ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Set |
| 277 |
* |
| 278 |
* @param mixed $property property. |
| 279 |
* @param mixed $value value. |
| 280 |
* @throws UpStream_Model_ArgumentException Exception. |
| 281 |
* @return void |
| 282 |
*/ |
| 283 |
public function __set( $property, $value ) { |
| 284 |
$property = apply_filters( 'upstream_wcs_model_variable', $property ); |
| 285 |
|
| 286 |
switch ( $property ) { |
| 287 |
|
| 288 |
case 'fileId': |
| 289 |
$file = get_attached_file( $value ); |
| 290 |
if ( false === $file ) { |
| 291 |
throw new UpStream_Model_ArgumentException( |
| 292 |
sprintf( |
| 293 |
// translators: %s: file id. |
| 294 |
__( 'File ID %s is invalid.', 'upstream' ), |
| 295 |
$value |
| 296 |
) |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
$this->fileId = $value; // phpcs:ignore |
| 301 |
break; |
| 302 |
|
| 303 |
case 'severity': |
| 304 |
$s = $this->getSeverities(); |
| 305 |
$sc = null; |
| 306 |
|
| 307 |
foreach ( $s as $s_key => $s_value ) { |
| 308 |
if ( $value === $s_value ) { |
| 309 |
$sc = $s_key; |
| 310 |
break; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if ( null === $sc ) { |
| 315 |
throw new UpStream_Model_ArgumentException( |
| 316 |
sprintf( |
| 317 |
// translators: %s: Severity id. |
| 318 |
__( 'Severity %s is invalid.', 'upstream' ), |
| 319 |
$value |
| 320 |
) |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
$this->severityCode = $sc; // phpcs:ignore |
| 325 |
break; |
| 326 |
|
| 327 |
case 'severityCode': |
| 328 |
$s = $this->getSeverities(); |
| 329 |
$sc = null; |
| 330 |
|
| 331 |
foreach ( $s as $s_key => $s_value ) { |
| 332 |
if ( $value === $s_key ) { |
| 333 |
$sc = $s_key; |
| 334 |
break; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if ( null === $sc ) { |
| 339 |
throw new UpStream_Model_ArgumentException( |
| 340 |
sprintf( |
| 341 |
// translators: %s: Severity code. |
| 342 |
__( 'Severity code %s is invalid.', 'upstream' ), |
| 343 |
$value |
| 344 |
) |
| 345 |
); |
| 346 |
} |
| 347 |
|
| 348 |
$this->severityCode = $sc; // phpcs:ignore |
| 349 |
break; |
| 350 |
|
| 351 |
case 'status': |
| 352 |
$s = $this->getStatuses(); |
| 353 |
$sc = null; |
| 354 |
|
| 355 |
foreach ( $s as $s_key => $s_value ) { |
| 356 |
if ( $value === $s_value ) { |
| 357 |
$sc = $s_key; |
| 358 |
break; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if ( null === $sc ) { |
| 363 |
throw new UpStream_Model_ArgumentException( |
| 364 |
sprintf( |
| 365 |
// translators: %s: Status name. |
| 366 |
__( 'Status %s is invalid.', 'upstream' ), |
| 367 |
$value |
| 368 |
) |
| 369 |
); |
| 370 |
} |
| 371 |
|
| 372 |
$this->statusCode = $sc; // phpcs:ignore |
| 373 |
|
| 374 |
break; |
| 375 |
|
| 376 |
case 'statusCode': |
| 377 |
$s = $this->getStatuses(); |
| 378 |
$sc = null; |
| 379 |
|
| 380 |
foreach ( $s as $s_key => $s_value ) { |
| 381 |
if ( $value === $s_key ) { |
| 382 |
$sc = $s_key; |
| 383 |
break; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
if ( null === $sc ) { |
| 388 |
throw new UpStream_Model_ArgumentException( |
| 389 |
sprintf( |
| 390 |
// translators: %s: Status code. |
| 391 |
__( 'Status code %s is invalid.', 'upstream' ), |
| 392 |
$value |
| 393 |
) |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
$this->statusCode = $sc; // phpcs:ignore |
| 398 |
break; |
| 399 |
|
| 400 |
case 'dueDate': |
| 401 |
if ( ! self::isValidDate( $value ) ) { |
| 402 |
throw new UpStream_Model_ArgumentException( __( 'Argument is not a valid date of the form YYYY-MM-DD.', 'upstream' ) ); |
| 403 |
} |
| 404 |
|
| 405 |
$this->{$property} = $value; |
| 406 |
break; |
| 407 |
|
| 408 |
case 'timeRecords': |
| 409 |
if ( ! is_array( $value ) ) { |
| 410 |
throw new UpStream_Model_ArgumentException( __( 'Argument must be an array of UpStream_Model_TimeRecord objects.', 'upstream' ) ); |
| 411 |
} |
| 412 |
|
| 413 |
foreach ( $value as $item ) { |
| 414 |
if ( ! $item instanceof UpStream_Model_TimeRecord ) { |
| 415 |
throw new UpStream_Model_ArgumentException( __( 'Argument must be an array of UpStream_Model_TimeRecord objects.', 'upstream' ) ); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
$this->{$property} = $value; |
| 420 |
break; |
| 421 |
|
| 422 |
default: |
| 423 |
parent::__set( $property, $value ); |
| 424 |
break; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
/** |
| 430 |
* Fields |
| 431 |
*/ |
| 432 |
public static function fields() { |
| 433 |
$fields = parent::fields(); |
| 434 |
|
| 435 |
$fields['statusCode'] = array( |
| 436 |
'type' => 'select', |
| 437 |
'title' => __( 'Status' ), |
| 438 |
'search' => true, |
| 439 |
'display' => true, |
| 440 |
'options_cb' => 'UpStream_Model_Bug::getStatuses', |
| 441 |
); |
| 442 |
$fields['severityCode'] = array( |
| 443 |
'type' => 'select', |
| 444 |
'title' => __( 'Severity' ), |
| 445 |
'search' => true, |
| 446 |
'display' => true, |
| 447 |
'options_cb' => 'UpStream_Model_Bug::getSeverities', |
| 448 |
); |
| 449 |
$fields['dueDate'] = array( |
| 450 |
'type' => 'date', |
| 451 |
'title' => __( 'Due Date' ), |
| 452 |
'search' => true, |
| 453 |
'display' => true, |
| 454 |
); |
| 455 |
|
| 456 |
$fields = self::customFields( $fields, UPSTREAM_ITEM_TYPE_BUG ); |
| 457 |
|
| 458 |
return $fields; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* GetStatuses |
| 463 |
*/ |
| 464 |
public static function getStatuses() { |
| 465 |
$option = get_option( 'upstream_bugs' ); |
| 466 |
$statuses = isset( $option['statuses'] ) ? $option['statuses'] : ''; |
| 467 |
$array = array(); |
| 468 |
if ( $statuses ) { |
| 469 |
foreach ( $statuses as $status ) { |
| 470 |
if ( isset( $status['name'] ) ) { |
| 471 |
$array[ $status['id'] ] = $status['name']; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
return $array; |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* GetSeverities |
| 481 |
*/ |
| 482 |
public static function getSeverities() { |
| 483 |
$option = get_option( 'upstream_bugs' ); |
| 484 |
$severities = isset( $option['severities'] ) ? $option['severities'] : ''; |
| 485 |
$array = array(); |
| 486 |
if ( $severities ) { |
| 487 |
foreach ( $severities as $severity ) { |
| 488 |
if ( isset( $severity['name'] ) ) { |
| 489 |
$array[ $severity['id'] ] = $severity['name']; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
return $array; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Create |
| 499 |
* |
| 500 |
* @param mixed $parent parent. |
| 501 |
* @param mixed $title title. |
| 502 |
* @param mixed $created_by created_by. |
| 503 |
*/ |
| 504 |
public static function create( $parent, $title, $created_by ) { |
| 505 |
$item_metadata = array( |
| 506 |
'title' => $title, |
| 507 |
'created_by' => $created_by, |
| 508 |
); |
| 509 |
|
| 510 |
return new self( $parent, $item_metadata ); |
| 511 |
} |
| 512 |
|
| 513 |
} |
| 514 |
|