PluginProbe
UpStream: a Project Management Plugin for WordPress / 2.1.0
UpStream: a Project Management Plugin for WordPress v2.1.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / model / class-upstream-model-client.php

class-upstream-model-client.php in UpStream: a Project Management Plugin for WordPress 2.1.0, at includes/model/class-upstream-model-client.php

360 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UpStream_Model_Client
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_Client
19 */
20 class UpStream_Model_Client extends UpStream_Model_Post_Object {
21
22 /**
23 * UserAssignments
24 *
25 * @var array
26 */
27 protected $userAssignments = array(); // phpcs:ignore
28
29 /**
30 * Website
31 *
32 * @var undefined
33 */
34 protected $website = null;
35
36 /**
37 * Address
38 *
39 * @var undefined
40 */
41 protected $address = null;
42
43 /**
44 * Phone
45 *
46 * @var undefined
47 */
48 protected $phone = null;
49
50 /**
51 * PostType
52 *
53 * @var string
54 */
55 protected $postType = 'client'; // phpcs:ignore
56
57 /**
58 * Type
59 *
60 * @var undefined
61 */
62 protected $type = UPSTREAM_ITEM_TYPE_CLIENT;
63
64 /**
65 * UpStream_Model_Client constructor.
66 *
67 * @param int $id Client Id.
68 */
69 public function __construct( $id ) {
70 if ( $id > 0 ) {
71 parent::__construct(
72 $id,
73 array(
74 'website' => '_upstream_client_website',
75 'address' => '_upstream_client_address',
76 'phone' => '_upstream_client_phone',
77 'userAssignments' => function ( $m ) {
78
79 if ( isset( $m['_upstream_new_client_users'][0] ) ) {
80 $arr = unserialize( $m['_upstream_new_client_users'][0] );
81
82 $out = array();
83 foreach ( $arr as $item ) {
84 $s = new stdClass();
85 $s->id = $item['user_id'];
86 $s->assignedBy = $item['assigned_by']; // phpcs:ignore
87 $s->assignedAt = $item['assigned_at']; // phpcs:ignore
88 $out[] = $s;
89 }
90 return $out;
91
92 }
93 return array();
94
95 },
96 )
97 );
98
99 } else {
100 parent::__construct( 0, array() );
101 }
102
103 $this->type = UPSTREAM_ITEM_TYPE_CLIENT;
104 }
105
106 /**
107 * Fields
108 */
109 public static function fields() {
110 $fields = parent::fields();
111
112 $fields['website'] = array(
113 'type' => 'string',
114 'title' => __( 'Website' ),
115 'search' => true,
116 'display' => true,
117 );
118 $fields['address'] = array(
119 'type' => 'string',
120 'title' => __( 'Address' ),
121 'search' => true,
122 'display' => true,
123 );
124 $fields['phone'] = array(
125 'type' => 'string',
126 'title' => __( 'Phone' ),
127 'search' => true,
128 'display' => true,
129 );
130 $fields['userIds'] = array(
131 'type' => 'user_id',
132 'is_array' => true,
133 'title' => __( 'Users' ),
134 'search' => true,
135 'display' => true,
136 );
137
138 return $fields;
139 }
140
141 /**
142 * Store
143 *
144 * @return void
145 */
146 public function store() {
147 parent::store();
148
149 if ( null !== $this->phone ) {
150 update_post_meta( $this->id, '_upstream_client_phone', $this->phone );
151 }
152 if ( null !== $this->address ) {
153 update_post_meta( $this->id, '_upstream_client_address', $this->address );
154 }
155 if ( null !== $this->website ) {
156 update_post_meta( $this->id, '_upstream_client_website', $this->website );
157 }
158
159 if ( null !== $this->userAssignments ) { // phpcs:ignore
160
161 $arr = array();
162 foreach ( $this->userAssignments as $assignment ) { // phpcs:ignore
163 $arr[] = array(
164 'user_id' => $assignment->id,
165 'assigned_by' => $assignment->assignedBy, // phpcs:ignore
166 'assigned_at' => $assignment->assignedAt, // phpcs:ignore
167 );
168 }
169 update_post_meta( $this->id, '_upstream_new_client_users', $arr );
170
171 }
172 }
173
174 /**
175 * AddUser
176 *
177 * @param mixed $user_id userId.
178 * @param mixed $assigned_by assignedBy.
179 * @throws UpStream_Model_ArgumentException Exception.
180 * @return void
181 */
182 public function addUser( $user_id, $assigned_by ) {
183 if ( get_userdata( $user_id ) === false ) {
184 throw new UpStream_Model_ArgumentException(
185 sprintf(
186 // translators: %s: user id.
187 __( 'User ID %s does not exist.', 'upstream' ),
188 $user_id
189 )
190 );
191 }
192
193 if ( get_userdata( $assigned_by ) === false ) {
194 throw new UpStream_Model_ArgumentException(
195 sprintf(
196 // translators: %s: user id.
197 __( 'User ID %s does not exist.', 'upstream' ),
198 $assigned_by
199 )
200 );
201 }
202
203 foreach ( $this->userAssignments as $assignment ) { // phpcs:ignore
204 if ( $assignment->id === $user_id ) {
205 throw new UpStream_Model_ArgumentException(
206 sprintf(
207 // translators: %s: user id.
208 __( 'User ID %s is already attached.', 'upstream' ),
209 $assigned_by
210 )
211 );
212 }
213 }
214
215 $assignment = new stdClass();
216 $assignment->id = $user_id;
217 $assignment->assignedBy = $assigned_by; // phpcs:ignore
218 $assignment->assignedAt = gmdate( 'Y-m-d H:i:s' ); // phpcs:ignore
219 $this->userAssignments[] = $assignment; // phpcs:ignore
220 }
221
222 /**
223 * RemoveUser
224 *
225 * @param mixed $user_id userId.
226 * @throws UpStream_Model_ArgumentException Exception.
227 * @return void
228 */
229 public function removeUser( $user_id ) {
230 $count_user_assignments = count( $this->userAssignments ); // phpcs:ignore
231 for ( $i = 0; $i < $count_user_assignments; $i++ ) {
232 if ( $this->userAssignments[ $i ]->id === $user_id ) { // phpcs:ignore
233 array_splice( $this->userAssignments, $i, 1 ); // phpcs:ignore
234 return;
235 }
236 }
237
238 throw new UpStream_Model_ArgumentException(
239 sprintf(
240 // translators: %s: user id.
241 __( 'User ID %s is not attached.', 'upstream' ),
242 $user_id
243 )
244 );
245 }
246
247 /**
248 * IncludesUser
249 *
250 * @param mixed $user_id userId.
251 */
252 public function includesUser( $user_id ) {
253 $count_user_assignments = count( $this->userAssignments ); // phpcs:ignore
254 for ( $i = 0; $i < $count_user_assignments; $i++ ) {
255 if ( $this->userAssignments[ $i ]->id === $user_id ) { // phpcs:ignore
256 return true;
257 }
258 }
259
260 return false;
261 }
262
263 /**
264 * Get
265 *
266 * @param mixed $property property.
267 */
268 public function __get( $property ) {
269 $property = apply_filters( 'upstream_wcs_model_variable', $property );
270
271 switch ( $property ) {
272
273 case 'phone':
274 case 'website':
275 case 'address':
276 return $this->{$property};
277
278 case 'userIds':
279 if ( null === $this->userAssignments ) { // phpcs:ignore
280 return array();
281 }
282
283 $user_ids = array();
284 foreach ( $this->userAssignments as $assignment ) { // phpcs:ignore
285 $user_ids[] = $assignment->id;
286 }
287
288 return $user_ids;
289
290 default:
291 return parent::__get( $property );
292
293 }
294 }
295
296 /**
297 * Set
298 *
299 * @param mixed $property property.
300 * @param mixed $value value.
301 * @return void
302 */
303 public function __set( $property, $value ) {
304 $property = apply_filters( 'upstream_wcs_model_variable', $property );
305
306 switch ( $property ) {
307
308 case 'phone':
309 case 'website':
310 $this->{$property} = sanitize_text_field( $value );
311 break;
312
313 case 'address':
314 $this->{$property} = sanitize_textarea_field( $value );
315 break;
316
317 case 'userIds':
318 if ( ! is_array( $value ) ) {
319 $value = array( $value );
320 }
321
322 foreach ( $value as $user_id ) {
323 $exception = null;
324 try {
325 $this->addUser( $user_id, get_current_user_id() );
326 } catch ( \UpStream_Model_ArgumentException $e ) {
327 $exception = $e; // ignore errors here.
328 }
329 }
330 break;
331
332 default:
333 parent::__set( $property, $value );
334 break;
335
336 }
337 }
338
339 /**
340 * Create
341 *
342 * @param mixed $title title.
343 * @param mixed $created_by created_by.
344 * @throws UpStream_Model_ArgumentException Exception.
345 */
346 public static function create( $title, $created_by ) {
347 if ( get_userdata( $created_by ) === false ) {
348 throw new UpStream_Model_ArgumentException( __( 'User ID does not exist.', 'upstream' ) );
349 }
350
351 $item = new \UpStream_Model_Client( 0 );
352
353 $item->title = sanitize_text_field( $title );
354 $item->createdBy = $created_by; // phpcs:ignore
355
356 return $item;
357 }
358
359 }
360