| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
class UpStream_Model_Milestone extends UpStream_Model_Post_Object |
| 10 |
{ |
| 11 |
|
| 12 |
protected $progress = 0; |
| 13 |
|
| 14 |
protected $startDate = null; |
| 15 |
|
| 16 |
protected $endDate = null; |
| 17 |
|
| 18 |
protected $color = null; |
| 19 |
|
| 20 |
protected $reminders = []; |
| 21 |
|
| 22 |
protected $postType = 'upst_milestone'; |
| 23 |
|
| 24 |
protected $type = UPSTREAM_ITEM_TYPE_MILESTONE; |
| 25 |
|
| 26 |
/** |
| 27 |
* UpStream_Model_Milestone constructor. |
| 28 |
*/ |
| 29 |
public function __construct($id) |
| 30 |
{ |
| 31 |
if ($id > 0) { |
| 32 |
parent::__construct($id, [ |
| 33 |
'progress' => 'upst_progress', |
| 34 |
'color' => 'upst_color', |
| 35 |
'startDate' => 'upst_start_date', |
| 36 |
'endDate' => 'upst_end_date', |
| 37 |
'parentId' => 'upst_project_id' |
| 38 |
]); |
| 39 |
|
| 40 |
$this->loadCategories(); |
| 41 |
|
| 42 |
$res = get_post_meta($id, 'upst_assigned_to'); |
| 43 |
foreach ($res as $r) $this->assignedTo[] = (int)$r; |
| 44 |
|
| 45 |
$res = get_post_meta($id, 'upst_reminders'); |
| 46 |
if (!empty($res)) { |
| 47 |
foreach ($res as $reminder_data) { |
| 48 |
$reminder = new UpStream_Model_Reminder((array)$reminder_data); |
| 49 |
$this->reminders[] = $reminder; |
| 50 |
} |
| 51 |
} |
| 52 |
} else { |
| 53 |
parent::__construct(0, []); |
| 54 |
} |
| 55 |
|
| 56 |
$this->type = UPSTREAM_ITEM_TYPE_MILESTONE; |
| 57 |
} |
| 58 |
|
| 59 |
protected function loadCategories() |
| 60 |
{ |
| 61 |
if (upstream_disable_milestone_categories()) { |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
$categories = wp_get_object_terms($this->id, 'upst_milestone_category'); |
| 66 |
|
| 67 |
$categoryIds = []; |
| 68 |
if (!isset($categories->errors)) { |
| 69 |
foreach ($categories as $category) { |
| 70 |
$categoryIds[] = $category->term_id; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$this->categoryIds = $categoryIds; |
| 75 |
} |
| 76 |
|
| 77 |
protected function storeCategories() |
| 78 |
{ |
| 79 |
if (upstream_disable_milestone_categories()) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$res = wp_set_object_terms($this->id, $this->categoryIds, 'upst_milestone_category'); |
| 84 |
|
| 85 |
if ($res instanceof \WP_Error) { |
| 86 |
// TODO: throw |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
public function store() |
| 92 |
{ |
| 93 |
parent::store(); |
| 94 |
|
| 95 |
if ($this->parentId > 0) update_post_meta($this->id, 'upst_project_id', $this->parentId); |
| 96 |
if ($this->progress > 0) update_post_meta($this->id, 'upst_progress', $this->progress); |
| 97 |
if ($this->color != null) update_post_meta($this->id, 'upst_color', $this->color); |
| 98 |
if ($this->startDate != null) update_post_meta($this->id, 'upst_start_date', $this->startDate); |
| 99 |
if ($this->endDate != null) update_post_meta($this->id, 'upst_end_date', $this->endDate); |
| 100 |
if ($this->startDate != null) update_post_meta($this->id, 'upst_start_date.YMD', $this->startDate); |
| 101 |
if ($this->endDate != null) update_post_meta($this->id, 'upst_end_date.YMD', $this->endDate); |
| 102 |
|
| 103 |
delete_post_meta($this->id, 'upst_assigned_to'); |
| 104 |
foreach ($this->assignedTo as $a) add_post_meta($this->id, 'upst_assigned_to', $a); |
| 105 |
|
| 106 |
$this->storeCategories(); |
| 107 |
} |
| 108 |
|
| 109 |
public function calculateElapsedTime() |
| 110 |
{ |
| 111 |
$total = 0; |
| 112 |
$tasks = &$this->tasks(); |
| 113 |
|
| 114 |
foreach ($tasks as $task) { |
| 115 |
$total += $task->calculateElapsedTime(); |
| 116 |
} |
| 117 |
|
| 118 |
return $total; |
| 119 |
} |
| 120 |
|
| 121 |
public function calculateBudgeted() |
| 122 |
{ |
| 123 |
$total = 0; |
| 124 |
$tasks = &$this->tasks(); |
| 125 |
|
| 126 |
foreach ($tasks as $task) { |
| 127 |
$total += $task->calculateBudgeted(); |
| 128 |
} |
| 129 |
|
| 130 |
return $total; |
| 131 |
} |
| 132 |
|
| 133 |
public function calculateSpent() |
| 134 |
{ |
| 135 |
$total = 0; |
| 136 |
$tasks = &$this->tasks(); |
| 137 |
|
| 138 |
foreach ($tasks as $task) { |
| 139 |
$total += $task->calculateSpent(); |
| 140 |
} |
| 141 |
|
| 142 |
return $total; |
| 143 |
} |
| 144 |
|
| 145 |
public function __get($property) |
| 146 |
{ |
| 147 |
switch ($property) { |
| 148 |
|
| 149 |
case 'notes': |
| 150 |
return $this->description; |
| 151 |
|
| 152 |
case 'progress': |
| 153 |
case 'categoryIds': |
| 154 |
case 'startDate': |
| 155 |
case 'endDate': |
| 156 |
case 'color': |
| 157 |
return $this->{$property}; |
| 158 |
|
| 159 |
case 'elapsedTime': |
| 160 |
return $this->calculateElapsedTime(); |
| 161 |
case 'budgeted': |
| 162 |
return $this->calculateBudgeted(); |
| 163 |
case 'spent': |
| 164 |
return $this->calculateSpent(); |
| 165 |
|
| 166 |
case 'categories': |
| 167 |
$categories = []; |
| 168 |
foreach ($this->categoryIds as $tid) { |
| 169 |
$term = get_term_by('id', $tid, 'upst_milestone_category'); |
| 170 |
$categories[] = $term; |
| 171 |
} |
| 172 |
return $categories; |
| 173 |
|
| 174 |
case 'tasks': |
| 175 |
throw new UpStream_Model_ArgumentException(__('Not implemented. Use &tasks().', 'upstream')); |
| 176 |
|
| 177 |
default: |
| 178 |
return parent::__get($property); |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public function __set($property, $value) |
| 184 |
{ |
| 185 |
switch ($property) { |
| 186 |
|
| 187 |
case 'parentId': |
| 188 |
$project = \UpStream_Model_Manager::get_instance()->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $value); |
| 189 |
$this->parentId = $project->id; |
| 190 |
break; |
| 191 |
|
| 192 |
case 'categoryIds': |
| 193 |
if (!is_array($value)) |
| 194 |
$value = [$value]; |
| 195 |
|
| 196 |
$categoryIds = []; |
| 197 |
foreach ($value as $tid) { |
| 198 |
$term = get_term_by('id', $tid, 'upst_milestone_category'); |
| 199 |
if ($term === false) |
| 200 |
throw new UpStream_Model_ArgumentException(sprintf(__('Term ID %s is invalid.', 'upstream'), $tid)); |
| 201 |
$categoryIds[] = $term->term_id; |
| 202 |
} |
| 203 |
|
| 204 |
$this->categoryIds = $categoryIds; |
| 205 |
|
| 206 |
break; |
| 207 |
|
| 208 |
case 'startDate': |
| 209 |
case 'endDate': |
| 210 |
if (!self::isValidDate($value)) |
| 211 |
throw new UpStream_Model_ArgumentException(__('Argument is not a valid date of the form YYYY-MM-DD.', 'upstream')); |
| 212 |
|
| 213 |
$this->{$property} = $value; |
| 214 |
break; |
| 215 |
|
| 216 |
case 'color': |
| 217 |
if (!preg_match('/\#[a-zA-Z0-9]{6}/', $value)) |
| 218 |
throw new UpStream_Model_ArgumentException(sprintf(__('%s is not a valid hex string.', 'upstream'), $value)); |
| 219 |
|
| 220 |
$this->{$property} = $value; |
| 221 |
break; |
| 222 |
|
| 223 |
case 'notes': |
| 224 |
$this->description = wp_kses_post($value); |
| 225 |
break; |
| 226 |
|
| 227 |
default: |
| 228 |
parent::__set($property, $value); |
| 229 |
break; |
| 230 |
|
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
public static function getCategories() |
| 235 |
{ |
| 236 |
$tid_to_term = []; |
| 237 |
$terms = get_terms(array('taxonomy' => 'upst_milestone_category', 'hide_empty' => true)); |
| 238 |
|
| 239 |
foreach ($terms as $term) { |
| 240 |
$tid_to_term[$term->term_id] = $term->name; |
| 241 |
} |
| 242 |
|
| 243 |
return $tid_to_term; |
| 244 |
} |
| 245 |
|
| 246 |
public function &tasks() |
| 247 |
{ |
| 248 |
if ($this->parentId == 0) { |
| 249 |
return []; |
| 250 |
} |
| 251 |
|
| 252 |
$my_tasks = []; |
| 253 |
$project = \UpStream_Model_Manager::get_instance()->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $this->parentId); |
| 254 |
$tasks = &$project->tasks(); |
| 255 |
|
| 256 |
foreach ($tasks as $task) { |
| 257 |
if ($task->milestoneId == $this->id) { |
| 258 |
$my_tasks[] = $task; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return $my_tasks; |
| 263 |
} |
| 264 |
|
| 265 |
public static function fields() |
| 266 |
{ |
| 267 |
$fields = parent::fields(); |
| 268 |
|
| 269 |
$fields['description'] = [ 'type' => 'text', 'title' => __('Notes'), 'search' => true, 'display' => true ]; |
| 270 |
$fields['color'] = [ 'type' => 'color', 'title' => __('Color'), 'search' => false, 'display' => true ]; |
| 271 |
$fields['startDate'] = [ 'type' => 'date', 'title' => __('Start Date'), 'search' => true, 'display' => true ]; |
| 272 |
$fields['endDate'] = [ 'type' => 'date', 'title' => __('End Date'), 'search' => true, 'display' => true ]; |
| 273 |
$fields['categoryIds'] = [ 'type' => 'select', 'title' => __('Categories'), 'search' => true, 'display' => true, 'options_cb' => 'UpStream_Model_Milestone::getCategories', 'is_array' => 'true' ]; |
| 274 |
$fields['progress'] = [ 'type' => 'number', 'title' => __('Progress (%)'), 'search' => true, 'display' => true ]; |
| 275 |
|
| 276 |
$fields = self::customFields($fields, UPSTREAM_ITEM_TYPE_MILESTONE); |
| 277 |
|
| 278 |
return $fields; |
| 279 |
} |
| 280 |
|
| 281 |
public static function create($title, $createdBy, $parentId = 0) |
| 282 |
{ |
| 283 |
if (get_userdata($createdBy) === false) |
| 284 |
throw new UpStream_Model_ArgumentException(__('User ID does not exist.', 'upstream')); |
| 285 |
|
| 286 |
$item = new \UpStream_Model_Milestone(0); |
| 287 |
|
| 288 |
$item->title = sanitize_text_field($title); |
| 289 |
$item->createdBy = $createdBy; |
| 290 |
|
| 291 |
if ($parentId > 0) { |
| 292 |
$project = \UpStream_Model_Manager::get_instance()->getByID(UPSTREAM_ITEM_TYPE_PROJECT, $parentId); |
| 293 |
$item->parentId = $project->id; |
| 294 |
} |
| 295 |
|
| 296 |
return $item; |
| 297 |
} |
| 298 |
|
| 299 |
} |