| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if (! defined('UPSTREAM_ITEM_TYPE_PROJECT')) { |
| 9 |
|
| 10 |
define('UPSTREAM_ITEM_TYPE_PROJECT', 'project'); |
| 11 |
define('UPSTREAM_ITEM_TYPE_MILESTONE', 'milestone'); |
| 12 |
define('UPSTREAM_ITEM_TYPE_CLIENT', 'client'); |
| 13 |
define('UPSTREAM_ITEM_TYPE_TASK', 'task'); |
| 14 |
define('UPSTREAM_ITEM_TYPE_BUG', 'bug'); |
| 15 |
define('UPSTREAM_ITEM_TYPE_FILE', 'file'); |
| 16 |
define('UPSTREAM_ITEM_TYPE_DISCUSSION', 'discussion'); |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
class UpStream_Model_Object |
| 21 |
{ |
| 22 |
|
| 23 |
protected $id = 0; |
| 24 |
|
| 25 |
protected $type = null; |
| 26 |
|
| 27 |
protected $title = null; |
| 28 |
|
| 29 |
protected $assignedTo = []; |
| 30 |
|
| 31 |
protected $createdBy = 0; |
| 32 |
|
| 33 |
protected $description = null; |
| 34 |
|
| 35 |
protected $additionalFields = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* UpStream_Model_Object constructor. |
| 39 |
* @param $id |
| 40 |
*/ |
| 41 |
public function __construct($id = 0) |
| 42 |
{ |
| 43 |
if (!preg_match('/^[a-zA-Z0-9]+$/', $id)) |
| 44 |
throw new UpStream_Model_ArgumentException(sprintf(__('ID %s must be a valid alphanumeric.', 'upstream'), $id)); |
| 45 |
|
| 46 |
$this->id = $id; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param $user_id The user_id of the user to check |
| 51 |
* @return bool True if this object is assigned to user_id, or false otherwise |
| 52 |
*/ |
| 53 |
public function isAssignedTo($user_id) |
| 54 |
{ |
| 55 |
foreach ($this->assignedTo as $a) { |
| 56 |
if ($a == $user_id) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
} |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
public static function fields() |
| 64 |
{ |
| 65 |
return [ |
| 66 |
'id' => [ 'type' => 'id', 'title' => __('ID'), 'search' => false, 'display' => true ], |
| 67 |
'title' => [ 'type' => 'string', 'title' => __('Title'), 'search' => true, 'display' => true ], |
| 68 |
'description' => [ 'type' => 'text', 'title' => __('Description'), 'search' => true, 'display' => true ], |
| 69 |
'createdBy' => [ 'type' => 'user_id', 'title' => __('Created By'), 'search' => true, 'display' => true ], |
| 70 |
'assignedTo' => [ 'type' => 'user_id', 'is_array' => true, 'title' => __('Assigned To'), 'search' => true, 'display' => true ], |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
public static function customFields($fields, $type, $id = 0) |
| 75 |
{ |
| 76 |
$list = apply_filters('upstream_model_list_properties', [], $type, $id); |
| 77 |
return array_merge($fields, $list); |
| 78 |
} |
| 79 |
|
| 80 |
public function __get($property) |
| 81 |
{ |
| 82 |
switch ($property) { |
| 83 |
|
| 84 |
case 'id': |
| 85 |
case 'title': |
| 86 |
case 'assignedTo': |
| 87 |
case 'createdBy': |
| 88 |
case 'type': |
| 89 |
case 'description': |
| 90 |
return $this->{$property}; |
| 91 |
|
| 92 |
default: |
| 93 |
|
| 94 |
if (array_key_exists($property, $this->additionalFields)) { |
| 95 |
|
| 96 |
$value = apply_filters('upstream_model_get_property_value', $this->additionalFields[$property], $this->type, $this->id, $property); |
| 97 |
|
| 98 |
} else { |
| 99 |
|
| 100 |
$propertyExists = apply_filters('upstream_model_property_exists', false, $this->type, $this->id, $property); |
| 101 |
if (!$propertyExists) { |
| 102 |
throw new UpStream_Model_ArgumentException(sprintf(__('This (%s) is not a valid property.', 'upstream'), $property)); |
| 103 |
} |
| 104 |
|
| 105 |
$this->additionalFields[$property] = null; |
| 106 |
|
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
return $value; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public function __set($property, $value) |
| 115 |
{ |
| 116 |
switch ($property) { |
| 117 |
|
| 118 |
case 'id': |
| 119 |
if (!preg_match('/^[a-zA-Z0-9]+$/', $value)) |
| 120 |
throw new UpStream_Model_ArgumentException(sprintf(__('ID %s must be a valid alphanumeric.', 'upstream'), $value)); |
| 121 |
$this->{$property} = $value; |
| 122 |
break; |
| 123 |
|
| 124 |
case 'title': |
| 125 |
if (trim(sanitize_text_field($value)) == '') |
| 126 |
throw new UpStream_Model_ArgumentException(__('You must enter a title.', 'upstream')); |
| 127 |
|
| 128 |
$this->{$property} = trim(sanitize_text_field($value)); |
| 129 |
break; |
| 130 |
|
| 131 |
case 'description': |
| 132 |
$this->{$property} = wp_kses_post($value); |
| 133 |
break; |
| 134 |
|
| 135 |
case 'assignedTo': |
| 136 |
case 'assignedTo:byUsername': |
| 137 |
case 'assignedTo:byEmail': |
| 138 |
if (!is_array($value)) |
| 139 |
$value = explode(',', $value); |
| 140 |
|
| 141 |
$new_value = []; |
| 142 |
|
| 143 |
foreach ($value as $uid) { |
| 144 |
$user = false; |
| 145 |
if ($property === 'assignedTo') |
| 146 |
$user = get_user_by('id', $uid); |
| 147 |
if ($property === 'assignedTo:byUsername') |
| 148 |
$user = get_user_by('login', trim($uid)); |
| 149 |
if ($property === 'assignedTo:byEmail') |
| 150 |
$user = get_user_by('email', trim($uid)); |
| 151 |
|
| 152 |
if ($user === false) |
| 153 |
throw new UpStream_Model_ArgumentException(sprintf(__('User "%s" (for field %s) does not exist.', 'upstream'), $uid, $property)); |
| 154 |
|
| 155 |
$new_value[] = $user->ID; |
| 156 |
} |
| 157 |
|
| 158 |
$this->assignedTo = $new_value; |
| 159 |
break; |
| 160 |
|
| 161 |
case 'createdBy': |
| 162 |
if (get_userdata($value) === false) |
| 163 |
throw new UpStream_Model_ArgumentException(sprintf(__('User ID %s does not exist.', 'upstream'), $value)); |
| 164 |
|
| 165 |
$this->{$property} = $value; |
| 166 |
break; |
| 167 |
|
| 168 |
default: |
| 169 |
$orig_value = (array_key_exists($property, $this->additionalFields)) ? $this->additionalFields[$property] : null; |
| 170 |
|
| 171 |
$propertyExists = apply_filters('upstream_model_property_exists', false, $this->type, $this->id, $property); |
| 172 |
if (!$propertyExists) { |
| 173 |
throw new UpStream_Model_ArgumentException(sprintf(__('This (%s) is not a valid property.', 'upstream'), $property)); |
| 174 |
} |
| 175 |
|
| 176 |
$new_value = apply_filters('upstream_model_set_property_value', $orig_value, $this->type, $this->id, $property, $value); |
| 177 |
$this->additionalFields[$property] = $new_value; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public static function loadDate($data, $field) |
| 182 |
{ |
| 183 |
if (!empty($data[$field . '.YMD']) && self::isValidDate($data[$field . '.YMD'])) { |
| 184 |
return $data[$field . '.YMD']; |
| 185 |
} else if (!empty($data[$field])) { |
| 186 |
return self::timestampToYMD($data[$field]); |
| 187 |
} |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
public static function timestampToYMD($timestamp) |
| 192 |
{ |
| 193 |
$offset = get_option( 'gmt_offset' ); |
| 194 |
$sign = $offset < 0 ? '-' : '+'; |
| 195 |
$hours = (int) $offset; |
| 196 |
$minutes = abs( ( $offset - (int) $offset ) * 60 ); |
| 197 |
$offset = (int)sprintf( '%s%d%02d', $sign, abs( $hours ), $minutes ); |
| 198 |
$calc_offset_seconds = $offset < 0 ? $offset * -1 * 60 : $offset * 60; |
| 199 |
|
| 200 |
$date = date_i18n('Y-m-d', $timestamp + $calc_offset_seconds); |
| 201 |
return $date; |
| 202 |
} |
| 203 |
|
| 204 |
public static function ymdToTimestamp($ymd) |
| 205 |
{ |
| 206 |
// TODO: check timezones with this |
| 207 |
return date_create_from_format('Y-m-d', $ymd)->getTimestamp(); |
| 208 |
} |
| 209 |
|
| 210 |
public static function isValidDate($ymd) |
| 211 |
{ |
| 212 |
$d = DateTime::createFromFormat('Y-m-d', $ymd); |
| 213 |
return $d && $d->format('Y-m-d') == $ymd; |
| 214 |
} |
| 215 |
|
| 216 |
} |