| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
class UpStream_Model_Client extends UpStream_Model_Post_Object |
| 10 |
{ |
| 11 |
|
| 12 |
protected $userAssignments = []; |
| 13 |
|
| 14 |
protected $website = null; |
| 15 |
|
| 16 |
protected $address = null; |
| 17 |
|
| 18 |
protected $phone = null; |
| 19 |
|
| 20 |
protected $postType = 'client'; |
| 21 |
|
| 22 |
protected $type = UPSTREAM_ITEM_TYPE_CLIENT; |
| 23 |
|
| 24 |
/** |
| 25 |
* UpStream_Model_Client constructor. |
| 26 |
*/ |
| 27 |
public function __construct($id) |
| 28 |
{ |
| 29 |
if ($id > 0) { |
| 30 |
parent::__construct($id, [ |
| 31 |
'website' => '_upstream_client_website', |
| 32 |
'address' => '_upstream_client_address', |
| 33 |
'phone' => '_upstream_client_phone', |
| 34 |
'userAssignments' => function ($m) { |
| 35 |
|
| 36 |
if (isset($m['_upstream_new_client_users'][0])) { |
| 37 |
$arr = unserialize($m['_upstream_new_client_users'][0]); |
| 38 |
|
| 39 |
$out = []; |
| 40 |
foreach ($arr as $item) { |
| 41 |
$s = new stdClass; |
| 42 |
$s->id = $item['user_id']; |
| 43 |
$s->assignedBy = $item['assigned_by']; |
| 44 |
$s->assignedAt = $item['assigned_at']; |
| 45 |
$out[] = $s; |
| 46 |
} |
| 47 |
return $out; |
| 48 |
|
| 49 |
} |
| 50 |
return []; |
| 51 |
|
| 52 |
}, |
| 53 |
]); |
| 54 |
|
| 55 |
} else { |
| 56 |
parent::__construct(0, []); |
| 57 |
} |
| 58 |
|
| 59 |
$this->type = UPSTREAM_ITEM_TYPE_CLIENT; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public static function fields() |
| 64 |
{ |
| 65 |
$fields = parent::fields(); |
| 66 |
|
| 67 |
$fields['website'] = [ 'type' => 'string', 'title' => __('Website'), 'search' => true, 'display' => true ]; |
| 68 |
$fields['address'] = [ 'type' => 'string', 'title' => __('Address'), 'search' => true, 'display' => true ]; |
| 69 |
$fields['phone'] = [ 'type' => 'string', 'title' => __('Phone'), 'search' => true, 'display' => true ]; |
| 70 |
$fields['userIds'] = [ 'type' => 'user_id', 'is_array' => true, 'title' => __('Users'), 'search' => true, 'display' => true ]; |
| 71 |
|
| 72 |
return $fields; |
| 73 |
} |
| 74 |
|
| 75 |
public function store() |
| 76 |
{ |
| 77 |
parent::store(); |
| 78 |
|
| 79 |
if ($this->phone != null) update_post_meta($this->id, '_upstream_client_phone', $this->phone); |
| 80 |
if ($this->address != null) update_post_meta($this->id, '_upstream_client_address', $this->address); |
| 81 |
if ($this->website != null) update_post_meta($this->id, '_upstream_client_website', $this->website); |
| 82 |
|
| 83 |
if ($this->userAssignments != null) { |
| 84 |
|
| 85 |
$arr = []; |
| 86 |
foreach ($this->userAssignments as $assignment) { |
| 87 |
$arr[] = [ |
| 88 |
'user_id' => $assignment->id, |
| 89 |
'assigned_by' => $assignment->assignedBy, |
| 90 |
'assigned_at' => $assignment->assignedAt |
| 91 |
]; |
| 92 |
} |
| 93 |
update_post_meta($this->id, '_upstream_new_client_users', $arr); |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public function addUser($userId, $assignedBy) |
| 99 |
{ |
| 100 |
if (get_userdata($userId) === false) |
| 101 |
throw new UpStream_Model_ArgumentException(sprintf(__('User ID %s does not exist.', 'upstream'), $userId)); |
| 102 |
|
| 103 |
if (get_userdata($assignedBy) === false) |
| 104 |
throw new UpStream_Model_ArgumentException(sprintf(__('User ID %s does not exist.', 'upstream'), $assignedBy)); |
| 105 |
|
| 106 |
foreach ($this->userAssignments as $assignment) { |
| 107 |
if ($assignment->id == $userId) |
| 108 |
throw new UpStream_Model_ArgumentException(sprintf(__('User ID %s is already attached.', 'upstream'), $assignedBy)); |
| 109 |
} |
| 110 |
|
| 111 |
$assignment = new stdClass; |
| 112 |
$assignment->id = $userId; |
| 113 |
$assignment->assignedBy = $assignedBy; |
| 114 |
$assignment->assignedAt = date('Y-m-d H:i:s'); |
| 115 |
|
| 116 |
$this->userAssignments[] = $assignment; |
| 117 |
} |
| 118 |
|
| 119 |
public function removeUser($userId) |
| 120 |
{ |
| 121 |
for ($i = 0; $i < count($this->userAssignments); $i++) { |
| 122 |
if ($this->userAssignments[$i]->id == $userId) { |
| 123 |
array_splice($this->userAssignments, $i, 1); |
| 124 |
return; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
throw new UpStream_Model_ArgumentException(sprintf(__('User ID %s is not attached.', 'upstream'), $userId)); |
| 129 |
} |
| 130 |
|
| 131 |
public function includesUser($userId) |
| 132 |
{ |
| 133 |
for ($i = 0; $i < count($this->userAssignments); $i++) { |
| 134 |
if ($this->userAssignments[$i]->id == $userId) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
public function __get($property) |
| 143 |
{ |
| 144 |
switch ($property) { |
| 145 |
|
| 146 |
case 'phone': |
| 147 |
case 'website': |
| 148 |
case 'address': |
| 149 |
return $this->{$property}; |
| 150 |
|
| 151 |
case 'userIds': |
| 152 |
if ($this->userAssignments == null) |
| 153 |
return []; |
| 154 |
|
| 155 |
$user_ids = []; |
| 156 |
foreach ($this->userAssignments as $assignment) { |
| 157 |
$user_ids[] = $assignment->id; |
| 158 |
} |
| 159 |
|
| 160 |
return $user_ids; |
| 161 |
|
| 162 |
default: |
| 163 |
return parent::__get($property); |
| 164 |
|
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public function __set($property, $value) |
| 169 |
{ |
| 170 |
switch ($property) { |
| 171 |
|
| 172 |
case 'phone': |
| 173 |
case 'website': |
| 174 |
$this->{$property} = sanitize_text_field($value); |
| 175 |
break; |
| 176 |
|
| 177 |
case 'address': |
| 178 |
$this->{$property} = sanitize_textarea_field($value); |
| 179 |
break; |
| 180 |
|
| 181 |
case 'userIds': |
| 182 |
if (!is_array($value)) |
| 183 |
$value = [$value]; |
| 184 |
|
| 185 |
foreach ($value as $user_id) { |
| 186 |
try { |
| 187 |
$this->addUser($user_id, get_current_user_id()); |
| 188 |
} catch (\UpStream_Model_ArgumentException $e) { |
| 189 |
// ignore errors here |
| 190 |
} |
| 191 |
} |
| 192 |
break; |
| 193 |
|
| 194 |
default: |
| 195 |
parent::__set($property, $value); |
| 196 |
break; |
| 197 |
|
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
public static function create($title, $createdBy) |
| 203 |
{ |
| 204 |
if (get_userdata($createdBy) === false) |
| 205 |
throw new UpStream_Model_ArgumentException(__('User ID does not exist.', 'upstream')); |
| 206 |
|
| 207 |
$item = new \UpStream_Model_Client(0); |
| 208 |
|
| 209 |
$item->title = sanitize_text_field($title); |
| 210 |
$item->createdBy = $createdBy; |
| 211 |
|
| 212 |
return $item; |
| 213 |
} |
| 214 |
|
| 215 |
} |