Completed
Push — master ( ab60f2...c901a1 )
by Ricardo
07:00
created

Provider::getUserList()   B

Complexity

Conditions 11
Paths 18

Size

Total Lines 43

Duplication

Lines 8
Ratio 18.6 %

Importance

Changes 0
Metric Value
dl 8
loc 43
rs 7.3166
c 0
b 0
f 0
cc 11
nc 18
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Fabrica\Project;
3
4
use Fabrica\Customization\Eloquent\State;
5
use Fabrica\Customization\Eloquent\StateProperty;
6
use Fabrica\Customization\Eloquent\Resolution;
7
use Fabrica\Customization\Eloquent\ResolutionProperty;
8
use Fabrica\Customization\Eloquent\Priority;
9
use Fabrica\Customization\Eloquent\PriorityProperty;
10
use Fabrica\Customization\Eloquent\Type;
11
use Fabrica\Customization\Eloquent\Field;
12
use Fabrica\Customization\Eloquent\Screen;
13
use Fabrica\Customization\Eloquent\Events;
14
use Fabrica\Acl\Eloquent\Role;
15
use Fabrica\Acl\Eloquent\Group;
16
use Fabrica\Acl\Acl;
17
use Fabrica\Workflow\Eloquent\Definition;
18
use Fabrica\Project\Eloquent\Project;
19
use Fabrica\Project\Eloquent\UserGroupProject;
20
use Fabrica\Project\Eloquent\File;
21
use Fabrica\Project\Eloquent\Version;
22
use Fabrica\Project\Eloquent\Module;
23
use Fabrica\Project\Eloquent\Epic;
24
use Fabrica\Project\Eloquent\Sprint;
25
use Fabrica\Project\Eloquent\Labels;
26
use Fabrica\Project\Eloquent\UserIssueFilters;
27
use Fabrica\Project\Eloquent\UserIssueListColumns;
28
use Fabrica\Project\Eloquent\ProjectIssueListColumns;
29
30
use Cartalyst\Sentinel\Users\EloquentUser;
31
use Sentinel;
32
use MongoDB\BSON\ObjectID;
33
use DB;
34
35
class Provider {
36
37
    /**
38
     * get project principal.
39
     *
40
     * @param string $project_key
41
     * @return array 
42
     */
43
    public static function getProjectPrincipal($project_key)
44
    {
45
        $project = Project::where('key', $project_key)->first()->toArray();
46
        return $project && isset($project['principal']) ? $project['principal'] : [];
47
    }
48
49
    /**
50
     * get the project default filters.
51
     *
52
     * @return array
53
     */
54
    public static function getDefaultIssueFilters()
55
    {
56
        return [
57
            [ 'id' => 'all', 'name' => '全部问题', 'query' => [] ],
58
            [ 'id' => 'unresolved', 'name' => '未解决的', 'query' => [ 'resolution' => 'Unresolved' ] ],
59
            [ 'id' => 'assigned_to_me', 'name' => '分配给我的', 'query' => [ 'assignee' => 'me', 'resolution' => 'Unresolved' ] ],
60
            [ 'id' => 'watched', 'name' => '我关注的', 'query' => [ 'watcher' => 'me' ] ],
61
            [ 'id' => 'reported', 'name' => '我报告的', 'query' => [ 'reporter' => 'me' ] ],
62
            [ 'id' => 'recent_created', 'name' => '最近增加的', 'query' => [ 'created_at' => '2w' ] ],
63
            [ 'id' => 'recent_updated', 'name' => '最近更新的', 'query' => [ 'updated_at' => '2w' ] ],
64
            [ 'id' => 'recent_resolved', 'name' => '最近解决的', 'query' => [ 'resolved_at' => '2w' ] ],
65
            [ 'id' => 'recent_closed', 'name' => '最近关闭的', 'query' => [ 'closed_at' => '2w' ] ],
66
        ];
67
    }
68
69
    /**
70
     * get the project filters.
71
     *
72
     * @param  string $project_key
73
     * @param  string $user_id
74
     * @return array
75
     */
76
    public static function getIssueFilters($project_key, $user_id)
77
    {
78
        // default issue filters
79
        $filters = self::getDefaultIssueFilters();
80
81
        $res = UserIssueFilters::where('project_key', $project_key)
82
            ->where('user', $user_id)
83
            ->first(); 
84
        if ($res)
85
        {
86
            $filters = isset($res->filters) ? $res->filters : [];
87
        }
88
        return $filters;
89
    }
90
91
    /**
92
     * get the issue list default columns.
93
     *
94
     * @return array
95
     */
96
    public static function getDefaultDisplayColumns($project_key)
97
    {
98
        $res = ProjectIssueListColumns::where('project_key', $project_key)->first(); 
99
        if ($res)
100
        {
101
            $columns = isset($res->columns) ? $res->columns : [];
102
            return $columns;
103
        }
104
105
        return [
106
            [ 'key' => 'assignee', 'width' => '100' ],
107
            [ 'key' => 'priority', 'width' => '70' ],
108
            [ 'key' => 'state', 'width' => '100' ],
109
            [ 'key' => 'resolution', 'width' => '100' ],
110
        ];
111
    }
112
113
    /**
114
     * get the issue list columns.
115
     *
116
     * @param  string $project_key
117
     * @param  string $user_id
118
     * @return array
119
     */
120
    public static function getIssueDisplayColumns($project_key, $user_id)
121
    {
122
        // default issue filters
123
        $columns = self::getDefaultDisplayColumns($project_key);
124
        $res = UserIssueListColumns::where('project_key', $project_key)
125
            ->where('user', $user_id)
126
            ->first(); 
127
        if ($res)
128
        {
129
            $columns = isset($res->columns) ? $res->columns : [];
130
        }
131
        return $columns;
132
    }
133
134
    /**
135
     * get state list.
136
     *
137
     * @param string $project_key
138
     * @param array $fields
139
     * @return collection 
140
     */
141
    public static function getStateList($project_key, $fields=[])
142
    {
143
        $states = State::Where('project_key', '$_sys_$')
144
            ->orWhere('project_key', $project_key)
145
            ->orderBy('project_key', 'asc')
146
            ->orderBy('sn', 'asc')
147
            ->get($fields)
148
            ->toArray();
149
150
        $stateProperty = StateProperty::Where('project_key', $project_key)->first();
151
        if ($stateProperty)
152
        {
153
            if ($sequence = $stateProperty->sequence)
154
            {
155
                $func = function($v1, $v2) use ($sequence) {
156
                    $i1 = array_search($v1['_id'], $sequence);
157
                    $i1 = $i1 !== false ? $i1 : 998;
158
                    $i2 = array_search($v2['_id'], $sequence);
159
                    $i2 = $i2 !== false ? $i2 : 999;
160
                    return $i1 >= $i2 ? 1 : -1;
161
                };
162
                usort($states, $func);
163
            }
164
        }
165
166
        return $states;
167
    }
168
169
    /**
170
     * get state options.
171
     *
172
     * @param string $project_key
173
     * @param array $fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fields. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
174
     * @return collection
175
     */
176 View Code Duplication
    public static function getStateOptions($project_key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $states = self::getStateList($project_key);
179
180
        $options = [];
181
        foreach ($states as $state)
182
        {
183
            $tmp = [];
184
            $tmp['_id'] = isset($state['key']) && $state['key'] ? $state['key'] : $state['_id'];
185
            $tmp['name'] = isset($state['name']) ? trim($state['name']) : '';
186
            $tmp['category'] = isset($state['category']) ? $state['category'] : '';
187
            $options[] = $tmp;
188
        }
189
        return $options;
190
    }
191
192
    /**
193
     * get state options.
194
     *
195
     * @param string $project_key
196
     * @param array $fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fields. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
197
     * @return collection
198
     */
199
    public static function getLabelOptions($project_key)
200
    {
201
        $options = [];
202
203
        $labels = Labels::Where('project_key', $project_key)
204
            ->orderBy('_id', 'desc')
205
            ->get();
206
        foreach ($labels as $label)
207
        {
208
            $options[] = [ 'name' => $label->name, 'bgColor' => $label->bgColor ?: '' ];
209
        }
210
211
        return $options;
212
    }
213
214
    /**
215
     * get event list.
216
     *
217
     * @param string $project_key
218
     * @param array $fields
219
     * @return collection 
220
     */
221
    public static function getEventList($project_key, $fields=[])
222
    {
223
        $events = Events::Where('project_key', '$_sys_$')
224
            ->orWhere('project_key', $project_key)
225
            ->orderBy('project_key', 'asc')
226
            ->orderBy('_id', 'asc')
227
            ->get($fields);
228
229
        return $events;
230
    }
231
232
    /**
233
     * get event options.
234
     *
235
     * @param string $project_key
236
     * @return collection
237
     */
238
    public static function getEventOptions($project_key)
239
    {
240
        $events = self::getEventList($project_key); 
241
242
        $options = [];
243
        foreach ($events as $event)
244
        {
245
            if (!isset($event->apply) || $event->apply !== 'workflow')
246
            {
247
                continue;
248
            }
249
250
            $tmp = [];
251
            $tmp['_id'] = isset($event['key']) ? $event['key'] : $event['_id'];
252
            $tmp['name'] = isset($event['name']) ? trim($event['name']) : '';
253
            $options[] = $tmp;
254
        }
255
256
        return $options;
257
    }
258
259
    /**
260
     * get default priority.
261
     *
262
     * @param string $project_key
263
     * @return string 
264
     */
265 View Code Duplication
    public static function getDefaultPriority($project_key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
    {
267
        $priorityProperty = PriorityProperty::Where('project_key', $project_key)->first();
268
        if ($priorityProperty)
269
        {
270
            if ($defaultValue = $priorityProperty->defaultValue)
271
            {
272
                return $defaultValue;
273
            }
274
        }
275
276
        $priority = Priority::whereRaw([ 'project_key' => [ '$in' =>  [ '$_sys_$', $project_key ] ] ])
277
            ->Where('default', true)
278
            ->first();
279
280
        $default = $priority && isset($priority->key) && $priority->key ? $priority->key : $priority->id;
281
        return $default; 
282
    }
283
284
    /**
285
     * get priority list.
286
     *
287
     * @param string $project_key
288
     * @param array $fields
289
     * @return collection
290
     */
291 View Code Duplication
    public static function getPriorityList($project_key, $fields=[])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
292
    {
293
        $priorities = Priority::Where('project_key', '$_sys_$')
294
            ->orWhere('project_key', $project_key)
295
            ->orderBy('project_key', 'asc')
296
            ->orderBy('sn', 'asc')
297
            ->get($fields)
298
            ->toArray();
299
300
        $priorityProperty = PriorityProperty::Where('project_key', $project_key)->first();
301
        if ($priorityProperty)
302
        {
303
            if ($sequence = $priorityProperty->sequence)
304
            {
305
                $func = function($v1, $v2) use ($sequence) {
306
                    $i1 = array_search($v1['_id'], $sequence);
307
                    $i1 = $i1 !== false ? $i1 : 998;
308
                    $i2 = array_search($v2['_id'], $sequence);
309
                    $i2 = $i2 !== false ? $i2 : 999;
310
                    return $i1 >= $i2 ? 1 : -1;
311
                };
312
                usort($priorities, $func);
313
            }
314
315
            if ($defaultValue = $priorityProperty->defaultValue)
316
            {
317
                foreach($priorities as $key => $val)
318
                {
319
                    if ($val['_id'] == $defaultValue)
320
                    {
321
                        $priorities[$key]['default'] = true;
322
                    }
323
                    else if (isset($val['default']))
324
                    {
325
                        unset($priorities[$key]['default']);
326
                    }
327
                }
328
            }
329
        }
330
331
        return $priorities;
332
    }
333
334
    /**
335
     * get priority options.
336
     *
337
     * @param string $project_key
338
     * @return array
339
     */
340
    public static function getPriorityOptions($project_key)
341
    {
342
        $priorities = self::getPriorityList($project_key);
343
344
        $options = [];
345
        foreach ($priorities as $priority)
346
        {
347
            $tmp = [];
348
            $tmp['_id'] = isset($priority['key']) && $priority['key'] ? $priority['key'] : $priority['_id'];
349
            $tmp['name'] = isset($priority['name']) ? trim($priority['name']) : '';
350
            if (isset($priority['default']))
351
            {
352
                $tmp['default'] = $priority['default'];
353
            }
354
            if (isset($priority['color']))
355
            {
356
                $tmp['color'] = $priority['color'];
357
            }
358
            $options[] = $tmp;
359
        }
360
        return $options;
361
    }
362
363
    /**
364
     * get default resolution.
365
     *
366
     * @param string $project_key
367
     * @return string 
368
     */
369 View Code Duplication
    public static function getDefaultResolution($project_key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
370
    {
371
        $resolutionProperty = ResolutionProperty::Where('project_key', $project_key)->first();
372
        if ($resolutionProperty)
373
        {
374
            if ($defaultValue = $resolutionProperty->defaultValue)
375
            {
376
                return $defaultValue;
377
            }
378
        }
379
380
        $resolution = Resolution::whereRaw([ 'project_key' => [ '$in' =>  [ '$_sys_$', $project_key ] ] ])
381
            ->Where('default', true)
382
            ->first();
383
384
        $default = $resolution && isset($resolution->key) && $resolution->key ? $resolution->key : $resolution->id;
385
        return $default;
386
    }
387
388
    /**
389
     * get resolution list.
390
     *
391
     * @param string $project_key
392
     * @param array $fields
393
     * @return collection
394
     */
395 View Code Duplication
    public static function getResolutionList($project_key, $fields=[])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
396
    {
397
        $resolutions = Resolution::Where('project_key', '$_sys_$')
398
            ->orWhere('project_key', $project_key)
399
            ->orderBy('project_key', 'asc')
400
            ->orderBy('sn', 'asc')
401
            ->get($fields)
402
            ->toArray();
403
404
        $resolutionProperty = ResolutionProperty::Where('project_key', $project_key)->first();
405
        if ($resolutionProperty)
406
        {
407
            if ($sequence = $resolutionProperty->sequence)
408
            {
409
                $func = function($v1, $v2) use ($sequence) {
410
                    $i1 = array_search($v1['_id'], $sequence);
411
                    $i1 = $i1 !== false ? $i1 : 998;
412
                    $i2 = array_search($v2['_id'], $sequence);
413
                    $i2 = $i2 !== false ? $i2 : 999;
414
                    return $i1 >= $i2 ? 1 : -1;
415
                };
416
                usort($resolutions, $func);
417
            }
418
419
            if ($defaultValue = $resolutionProperty->defaultValue)
420
            {
421
                foreach($resolutions as $key => $val)
422
                {
423
                    if ($val['_id'] == $defaultValue)
424
                    {
425
                        $resolutions[$key]['default'] = true;
426
                    }
427
                    else if (isset($val['default']))
428
                    {
429
                        unset($resolutions[$key]['default']);
430
                    }
431
                }
432
            }
433
        }
434
435
        return $resolutions;
436
    }
437
438
    /**
439
     * get resolution options.
440
     *
441
     * @param string $project_key
442
     * @return array 
443
     */
444 View Code Duplication
    public static function getResolutionOptions($project_key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
445
    {
446
        $resolutions = self::getResolutionList($project_key);
447
448
        $options = [];
449
        foreach ($resolutions as $resolution)
450
        {
451
            $tmp = [];
452
            $tmp['_id'] = isset($resolution['key']) && $resolution['key'] ? $resolution['key'] : $resolution['_id'];
453
            $tmp['name'] = isset($resolution['name']) ? trim($resolution['name']) : '';
454
            if (isset($resolution['default']))
455
            {
456
                $tmp['default'] = $resolution['default'];
457
            }
458
            $options[] = $tmp;
459
        }
460
        return $options;
461
    }
462
463
    /**
464
     * get screen list.
465
     *
466
     * @param string $project_key
467
     * @param array $fields
468
     * @return collection
469
     */
470
    public static function getScreenList($project_key, $fields=[])
471
    {
472
        $screens = Screen::Where('project_key', '$_sys_$')
473
            ->orWhere('project_key', $project_key)
474
            ->orderBy('project_key', 'asc')
475
            ->orderBy('_id', 'asc')
476
            ->get($fields);
477
478
        return $screens;
479
    }
480
481
    /**
482
     * get field list.
483
     *
484
     * @param string $project_key
485
     * @param array $fields
486
     * @return collection
487
     */
488
    public static function getFieldList($project_key, $fields=[])
489
    {
490
        $fields = Field::Where('project_key', '$_sys_$')
491
            ->orWhere('project_key', $project_key)
492
            ->orderBy('project_key', 'asc')
493
            ->orderBy('_id', 'asc')
494
            ->get($fields);
495
496
        return $fields;
497
    }
498
499
    /**
500
     * get workflow list.
501
     *
502
     * @param string $project_key
503
     * @param array $fields
504
     * @return collection
505
     */
506
    public static function getWorkflowList($project_key, $fields=[])
507
    {
508
        $workflows = Definition::Where('project_key', '$_sys_$')
509
            ->orWhere('project_key', $project_key)
510
            ->orderBy('project_key', 'asc')
511
            ->orderBy('_id', 'asc')
512
            ->get($fields);
513
514
        return $workflows;
515
    }
516
517
518
    /**
519
     * get type list.
520
     *
521
     * @param string $project_key
522
     * @param array $fields
523
     * @return collection
524
     */
525
    public static function getTypeList($project_key, $fields=[])
526
    {
527
        $types = Type::Where('project_key', $project_key)
528
            ->orderBy('sn', 'asc')
529
            ->get($fields);
530
531
        return $types;
532
    }
533
534
    /**
535
     * get role list.
536
     *
537
     * @param string $project_key
538
     * @param array $fields
539
     * @return collection
540
     */
541
    public static function getRoleList($project_key, $fields=[])
542
    {
543
        $roles = Role::Where('project_key', '$_sys_$')
544
            ->orWhere('project_key', $project_key)
545
            ->orderBy('project_key', 'asc')
546
            ->orderBy('_id', 'asc')
547
            ->get($fields);
548
549
        return $roles;
550
    }
551
552
    /**
553
     * get user list.
554
     *
555
     * @param string $project_key
556
     * @return array
557
     */
558
    public static function getUserList($project_key)
559
    {
560
        $user_group_ids = UserGroupProject::Where('project_key', $project_key)
561
            ->Where('link_count', '>', 0)
562
            ->get(['ug_id', 'type']);
563
564
        $user_ids = [];
565
        $group_ids = [];
566
        foreach ($user_group_ids as $value)
567
        {
568
            if (isset($value->type) && $value->type === 'group')
569
            {
570
                $group_ids[] = $value->ug_id;
571
            }
572
            else
573
            {
574
                $user_ids[] = $value->ug_id;
575
            }
576
        }
577
578
        if ($group_ids)
0 ignored issues
show
Bug Best Practice introduced by
The expression $group_ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
579
        {
580
            $groups = Group::find($group_ids);
581
            foreach($groups as $group)
582
            {
583
                $user_ids = array_merge($user_ids, isset($group->users) && $group->users ? $group->users : []);
584
            }
585
        }
586
        $user_ids = array_unique($user_ids);
587
588
        $user_list = [];
589
        $users = EloquentUser::find($user_ids);
590 View Code Duplication
        foreach ($users as $user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
591
        {
592
            if (isset($user->invalid_flag) && $user->invalid_flag === 1)
593
            {
594
                continue;
595
            }
596
            $user_list[] = ['id' => $user->id, 'name' => $user->first_name, 'email' => $user->email ];
597
        }
598
599
        return $user_list;
600
    }
601
602
    /**
603
     * get assigned user list.
604
     *
605
     * @param string $project_key
606
     * @return array
607
     */
608
    public static function getAssignedUsers($project_key)
609
    {
610
        $user_ids = Acl::getUserIdsByPermission('assigned_issue', $project_key);
611
612
        $user_list = [];
613
        $users = EloquentUser::find($user_ids); 
614 View Code Duplication
        foreach ($users as $user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
615
        {
616
            if (isset($user->invalid_flag) && $user->invalid_flag === 1)
617
            {
618
                continue;
619
            }
620
            $user_list[] = [ 'id' => $user->id, 'name' => $user->first_name, 'email' => $user->email ];
621
        }
622
        return $user_list;
623
    }
624
625
    /**
626
     * get version list.
627
     *
628
     * @param string $project_key
629
     * @param array $fields
630
     * @return collection
631
     */
632
    public static function getVersionList($project_key, $fields=[])
633
    {
634
        $versions = Version::where([ 'project_key' => $project_key ])
635
            ->orderBy('status', 'desc')
636
            ->orderBy('released_time', 'desc')
637
            ->orderBy('end_time', 'desc')
638
            ->orderBy('created_at', 'desc')
639
            ->get($fields);
640
641
        return $versions;
642
    }
643
644
    /**
645
     * get module list.
646
     *
647
     * @param string $project_key
648
     * @param array $fields
649
     * @return collection
650
     */
651
    public static function getModuleList($project_key, $fields=[])
652
    {
653
        $modules = Module::where([ 'project_key' => $project_key ])
654
            ->orderBy('sn', 'asc')
655
            ->get($fields);
656
657
        return $modules;
658
    }
659
660
    /**
661
     * check if type has existed.
662
     *
663
     * @param string $project_key
664
     * @param string $name 
665
     * @return bool
666
     */
667
    public static function isTypeExisted($project_key, $name)
668
    {
669
        $isExisted = Type::Where('project_key', $project_key)
670
            ->Where('name', $name)
671
            ->exists();
672
673
        return $isExisted;
674
    }
675
676
    /**
677
     * check if type abb has existed.
678
     *
679
     * @param string $project_key
680
     * @param string $abb 
681
     * @return bool
682
     */
683
    public static function isTypeAbbExisted($project_key, $abb)
684
    {
685
        $isExisted = Type::Where('project_key', $project_key)
686
            ->Where('abb', $abb)
687
            ->exists();
688
689
        return $isExisted;
690
    }
691
692
    /**
693
     * check if state has existed.
694
     *
695
     * @param string $project_key
696
     * @param string $name 
697
     * @return bool
698
     */
699
    public static function isStateExisted($project_key, $name)
700
    {
701
        $isExisted = State::Where('project_key', '$_sys_$')
702
            ->orWhere('project_key', $project_key)
703
            ->Where('name', $name)
704
            ->exists();
705
706
        return $isExisted;
707
    }
708
709
    /**
710
     * check if priority has existed.
711
     *
712
     * @param string $project_key
713
     * @param string $name
714
     * @return bool
715
     */
716
    public static function isPriorityExisted($project_key, $name)
717
    {
718
        $isExisted = Priority::Where('project_key', '$_sys_$')
719
            ->orWhere('project_key', $project_key)
720
            ->Where('name', $name)
721
            ->exists();
722
723
        return $isExisted;
724
    }
725
726
    /**
727
     * check if resolution has existed.
728
     *
729
     * @param string $project_key
730
     * @param string $name
731
     * @return bool
732
     */
733
    public static function isResolutionExisted($project_key, $name)
734
    {
735
        $isExisted = Resolution::Where('project_key', '$_sys_$')
736
            ->orWhere('project_key', $project_key)
737
            ->Where('name', $name)
738
            ->exists();
739
740
        return $isExisted;
741
    }
742
743
    /**
744
     * check if field has existed.
745
     *
746
     * @param string $project_key
747
     * @param string $key
748
     * @return bool
749
     */
750
    public static function isFieldKeyExisted($project_key, $key)
751
    {
752
        $fields = Field::Where('project_key', '$_sys_$')
753
            ->orWhere('project_key', $project_key)
754
            ->get();
755
        foreach ($fields as $field)
756
        {
757
            if ($field->key === $key || ($field->type === 'MutiUser' && $field->key . '_ids' === $key) || ($field->type === 'TimeTracking' && $field->key . '_m' === $key))
758
            {
759
                return true;
760
            }
761
        }
762
763
        return false;
764
    }
765
766
    /**
767
     * check if Event has existed.
768
     *
769
     * @param string $project_key
770
     * @param string $name
771
     * @return bool
772
     */
773
    public static function isEventExisted($project_key, $name)
774
    {
775
        $isExisted = Events::Where('project_key', '$_sys_$')
776
            ->orWhere('project_key', $project_key)
777
            ->Where('name', $name)
778
            ->exists();
779
780
        return $isExisted;
781
    }
782
783
    /**
784
     * get issue type schema 
785
     *
786
     * @param string $project_key
787
     * @return array 
788
     */
789
    public static function getTypeListExt($project_key, $options)
790
    {
791
        $typeOptions = [];
792
        $types = self::getTypeList($project_key);
793
        foreach ($types as $key => $type)
794
        {
795
            $schema = self::_repairSchema($project_key, $type->id, $type->screen && $type->screen->schema ? $type->screen->schema : [] , $options);
796
797
            $tmp = [ 'id' => $type->id, 'name' => $type->name, 'abb' => $type->abb, 'disabled' => $type->disabled && true, 'type' => $type->type == 'subtask' ? 'subtask' : 'standard', 'schema' => $schema ];
798
            if ($type->default) 
799
            {
800
                $tmp['default'] = true;
801
            }
802
            $typeOptions[] = $tmp;
803
        }
804
        return $typeOptions;
805
    }
806
807
    /**
808
     * get issue type schema
809
     *
810
     * @param string $project_key
811
     * @return array
812
     */
813
    private static function _repairSchema($project_key, $issue_type, $schema, $options)
814
    {
815
        $new_schema = [];
816
        foreach ($schema as $key => $val)
817
        {
818 View Code Duplication
            if (isset($val['applyToTypes']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
819
            {
820
                if ($val['applyToTypes'] && !in_array($issue_type, explode(',', $val['applyToTypes'] ?: '')))
821
                {
822
                    continue;
823
                }
824
                unset($val['applyToTypes']);
825
            }
826
827
            if ($val['type'] == 'SingleVersion' || $val['type'] == 'MultiVersion')
828
            {
829
                if (!isset($options['version']))
830
                {
831
                    $options['version'] = self::getVersionList($project_key);
832
                }
833
                $val['optionValues'] = self::pluckFields($options['version'], ['_id', 'name']);
834
            }
835
            else if ($val['type'] == 'SingleUser' || $val['type'] == 'MultiUser')
836
            {
837
                $val['optionValues'] = self::pluckFields($options['user'], ['id', 'name', 'email']);
838 View Code Duplication
                foreach ($val['optionValues'] as $k => $v)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
839
                {
840
                    $val['optionValues'][$k]['name'] = $v['name'] . '(' . $v['email'] . ')';
841
                    unset($val['optionValues'][$k]['email']);
842
                }
843
            }
844
            else if ($val['key'] == 'assignee')
845
            {
846
                $val['optionValues'] = self::pluckFields($options['assignee'], ['id', 'name', 'email']);
847 View Code Duplication
                foreach ($val['optionValues'] as $k => $v)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
848
                {
849
                    $val['optionValues'][$k]['name'] = $v['name'] . '(' . $v['email'] . ')';
850
                    unset($val['optionValues'][$k]['email']);
851
                }
852
            }
853
            else if ($val['key'] == 'labels')
854
            {
855
                $couple_labels = [];
856 View Code Duplication
                foreach ($options['labels'] as $label)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
857
                {
858
                    $couple_labels[] = [ 'id' => $label['name'], 'name' => $label['name'] ];
859
                }
860
                $val['optionValues'] = $couple_labels;
861
            }
862
            else if (array_key_exists($val['key'], $options))
863
            {
864
                $val['optionValues'] = self::pluckFields($options[$val['key']], ['_id', 'name']);
865 View Code Duplication
                foreach ($options[$val['key']] as $key2 => $val2) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
866
                {
867
                    if (isset($val2['default']) && $val2['default'])
868
                    {
869
                        $val['defaultValue'] = $val2['_id'];
870
                        break;
871
                    }
872
                }
873
            }
874
            if (isset($val['_id']))
875
            {
876
                unset($val['_id']);
877
            }
878
            $new_schema[] = $val;
879
        }
880
        return $new_schema;
881
    }
882
883
    /**
884
     * filter the fields.
885
     *
886
     * @param array $srcData
887
     * @param array $fields
888
     * @return array 
889
     */
890
    public static function pluckFields($srcData, $fields)
891
    {
892
        $destData = [];
893
        foreach ($srcData as $val)
894
        {
895
            $tmp = [];
896
            foreach ($fields as $field)
897
            {
898
                if ($field === '_id') {
899
                    if (isset($val[$field]) && $val[$field] instanceof ObjectID) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\ObjectID does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
900
                        $tmp['id'] = $val[$field]->__toString();
901
                    } else {
902
                        $tmp['id'] = isset($val[$field]) ? $val[$field] : '';
903
                    }
904
                } else {
905
                    $tmp[$field] = isset($val[$field]) ? $val[$field] : '';
906
                }
907
            }
908
            $destData[] = $tmp;
909
        } 
910
        return $destData;
911
    }
912
913
    /**
914
     * get module principal.
915
     *
916
     * @param string $project_key
0 ignored issues
show
Bug introduced by
There is no parameter named $project_key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
917
     * @param string $mid
918
     * @return array 
919
     */
920
    public static function getModuleById($mid)
921
    {
922
        $module = Module::find($mid)->toArray();
923
        return $module ?: [];
924
    }
925
926
    /**
927
     * get workflow by type_id
928
     *
929
     * @param string $type_id
930
     * @return array
931
     */
932
    public static function getWorkflowByType($type_id)
933
    {
934
        $type = Type::find($type_id);
935
        return $type->workflow;
936
    }
937
938
    /**
939
     * get schema by type_id
940
     *
941
     * @param string $type_id
942
     * @return array
943
     */
944
    public static function getSchemaByType($type_id)
945
    {
946
        $type = Type::find($type_id);
947
        if (!$type)
948
        {
949
            return [];
950
        }
951
952
        $screen = $type->screen;
953
        $project_key = $type->project_key;
954
955
        return self::getScreenSchema($project_key, $type_id, $screen);
956
    }
957
958
    /**
959
     * get schema by screen_id
960
     *
961
     * @param string $project_key
962
     * @param string $type
963
     * @param string $screen_id
964
     * @return array
965
     */
966
    public static function getSchemaByScreenId($project_key, $type, $screen_id)
967
    {
968
        $screen = Screen::find($screen_id);
969
        if (!$screen)
970
        {
971
            return [];
972
        }
973
        return self::getScreenSchema($project_key, $type, $screen);
974
    }
975
976
    /**
977
     * get screen schema
978
     *
979
     * @param string $type_id
980
     * @return array
981
     */
982
    public static function getScreenSchema($project_key, $type_id, $screen)
983
    {
984
        $new_schema = [];
985
        $versions = null;
986
        $users = null;
987
        foreach ($screen->schema ?: [] as $key => $val)
988
        {
989 View Code Duplication
            if (isset($val['applyToTypes']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
990
            {
991
                if ($val['applyToTypes'] && !in_array($type_id, explode(',', $val['applyToTypes'] ?: '')))
992
                {
993
                    continue;
994
                }
995
                unset($val['applyToTypes']);
996
            }
997
998
            if ($val['key'] == 'assignee')
999
            {
1000
                $users = self::getAssignedUsers($project_key);
1001 View Code Duplication
                foreach ($users as $key => $user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1002
                {
1003
                    $users[$key]['name'] = $user['name'] . '(' . $user['email'] . ')';
1004
                } 
1005
                $val['optionValues'] = self::pluckFields($users, ['id', 'name']);
1006
            }
1007
            else if ($val['key'] == 'resolution')
1008
            {
1009
                $resolutions = self::getResolutionOptions($project_key);
1010
                $val['optionValues'] = self::pluckFields($resolutions, ['_id', 'name']);
1011 View Code Duplication
                foreach ($resolutions as $key2 => $val2)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1012
                {
1013
                    if (isset($val2['default']) && $val2['default'])
1014
                    {
1015
                        $val['defaultValue'] = $val2['_id'];
1016
                        break;
1017
                    }
1018
                }
1019
            }
1020
            else if ($val['key'] == 'priority')
1021
            {
1022
                $priorities = self::getPriorityOptions($project_key);
1023
                $val['optionValues'] = self::pluckFields($priorities, ['_id', 'name']);
1024 View Code Duplication
                foreach ($priorities as $key2 => $val2)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1025
                {
1026
                    if (isset($val2['default']) && $val2['default'])
1027
                    {
1028
                        $val['defaultValue'] = $val2['_id'];
1029
                        break;
1030
                    }
1031
                }
1032
            }
1033
            else if ($val['key'] == 'module')
1034
            {
1035
                $modules = self::getModuleList($project_key);
1036
                $val['optionValues'] = self::pluckFields($modules, ['_id', 'name']);
1037
            }
1038
            else if ($val['key'] == 'epic')
1039
            {
1040
                $epics = self::getEpicList($project_key);
1041
                $val['optionValues'] = self::pluckFields($epics, ['_id', 'name', 'bgColor']);
1042
            }
1043
            else if ($val['key'] == 'labels')
1044
            {
1045
                $labels = self::getLabelOptions($project_key);
1046
                $couple_labels = [];
1047 View Code Duplication
                foreach ($labels as $label)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1048
                {
1049
                    $couple_labels[] = [ 'id' => $label['name'], 'name' => $label['name'] ];
1050
                }
1051
                $val['optionValues'] = $couple_labels;
1052
            }
1053
            else if ($val['type'] == 'SingleVersion' || $val['type'] == 'MultiVersion')
1054
            {
1055
                $versions === null && $versions = self::getVersionList($project_key);
1056
                $val['optionValues'] = self::pluckFields($versions, ['_id', 'name']);
0 ignored issues
show
Bug introduced by
It seems like $versions defined by null on line 985 can also be of type null; however, Fabrica\Project\Provider::pluckFields() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1057
            }
1058
            else if ($val['type'] == 'SingleUser' || $val['type'] == 'MultiUser')
1059
            {
1060
                $users === null && $users = self::getUserList($project_key);
1061 View Code Duplication
                foreach ($users as $key => $user)
0 ignored issues
show
Bug introduced by
The expression $users of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1062
                {
1063
                    $users[$key]['name'] = $user['name'] . '(' . $user['email'] . ')';
1064
                }
1065
                $val['optionValues'] = self::pluckFields($users, ['id', 'name']);
0 ignored issues
show
Bug introduced by
It seems like $users defined by null on line 986 can also be of type null; however, Fabrica\Project\Provider::pluckFields() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1066
            }
1067
1068
            if (isset($val['_id']))
1069
            {
1070
                unset($val['_id']);
1071
            }
1072
1073
            $new_schema[] = $val;
1074
        }
1075
1076
        return $new_schema;
1077
    }
1078
1079
    /**
1080
     * snap issue data to history
1081
     *
1082
     * @param  string  $project_key
1083
     * @param  string  $issue_id
1084
     * @param  array  $schema
1085
     * @param  array $change_fields
1086
     * @return \Illuminate\Http\Response
1087
     */
1088
    public static function snap2His($project_key, $issue_id, $schema = [], $change_fields=[])
1089
    {
1090
        //获取问题数据
1091
        $issue = DB::collection('issue_' . $project_key)->where('_id', $issue_id)->first();
1092
1093
        $latest_ver_issue = DB::collection('issue_his_' . $project_key)->where('issue_id', $issue_id)->orderBy('_id', 'desc')->first();
1094
        if ($latest_ver_issue)
1095
        {
1096
            $snap_data = $latest_ver_issue['data'];
1097
        }
1098
        else
1099
        {
1100
            $snap_data = [];
1101
        }
1102
1103
        // fetch the schema data
1104
        if (!$schema)
0 ignored issues
show
Bug Best Practice introduced by
The expression $schema of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1105
        {
1106
            $schema = [];
1107
            if ($change_fields)
0 ignored issues
show
Bug Best Practice introduced by
The expression $change_fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1108
            {
1109
                $out_schema_fields = [ 'type', 'state', 'resolution', 'priority', 'assignee', 'labels', 'parent_id', 'progress', 'expect_start_time', 'expect_complete_time' ];
1110
                if (array_diff($change_fields, $out_schema_fields))
1111
                {
1112
                    $schema = self::getSchemaByType($issue['type']);
1113
                }
1114
            }
1115
            else
1116
            {
1117
                $schema = self::getSchemaByType($issue['type']);
1118
            }
1119
        }
1120
1121
        foreach ($schema as $field)
1122
        {
1123
            if (in_array($field['key'], [ 'assignee', 'progress' ]) || ($change_fields && !in_array($field['key'], $change_fields)))
0 ignored issues
show
Bug Best Practice introduced by
The expression $change_fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1124
            {
1125
                continue;
1126
            }
1127
1128
            if (isset($issue[$field['key']]))
1129
            {
1130
                $val = [];
1131
                $val['name'] = $field['name'];
1132
                
1133
                if ($field['type'] === 'SingleUser' || $field['type'] === 'MultiUser')
1134
                {
1135
                    if ($field['type'] === 'SingleUser')
1136
                    {
1137
                        $val['value'] = $issue[$field['key']] ? $issue[$field['key']]['name'] : $issue[$field['key']];
1138
                    }
1139
                    else
1140
                    {
1141
                        $tmp_users = [];
1142
                        if ($issue[$field['key']])
1143
                        {
1144
                            foreach ($issue[$field['key']] as $tmp_user)
1145
                            {
1146
                                $tmp_users[] = $tmp_user['name'];
1147
                            }
1148
                        }
1149
                        $val['value'] = implode(',', $tmp_users);
1150
                    }
1151
                }
1152
                else if (isset($field['optionValues']) && $field['optionValues'])
1153
                {
1154
                    $opv = [];
1155
                    
1156
                    if (!is_array($issue[$field['key']]))
1157
                    {
1158
                        $fieldValues = explode(',', $issue[$field['key']]);
1159
                    }
1160
                    else
1161
                    {
1162
                        $fieldValues = $issue[$field['key']];
1163
                    }
1164
                    foreach ($field['optionValues'] as $ov)
1165
                    {
1166
                        if (in_array($ov['id'], $fieldValues))
1167
                        {
1168
                            $opv[] = $ov['name'];
1169
                        }
1170
                    }
1171
                    $val['value'] = implode(',', $opv);
1172
                }
1173
                else if ($field['type'] == 'File')
1174
                {
1175
                    $val['value'] = [];
1176
                    foreach ($issue[$field['key']] as $fid)
1177
                    {
1178
                        $file = File::find($fid);
1179
                        array_push($val['value'], $file->name);
1180
                    }
1181
                }
1182
                else if ($field['type'] == 'DatePicker' || $field['type'] == 'DateTimePicker')
1183
                {
1184
                    $val['value'] = $issue[$field['key']] ? date($field['type'] == 'DatePicker' ? 'Y/m/d' : 'Y/m/d H:i:s', $issue[$field['key']]) : $issue[$field['key']];
1185
                }
1186
                else
1187
                {
1188
                    $val['value'] = $issue[$field['key']];
1189
                }
1190
                //$val['type'] = $field['type']; 
1191
1192
                $snap_data[$field['key']] = $val;
1193
            }
1194
        }
1195
1196
        // special fields handle
1197
        if (in_array('type', $change_fields) || !isset($snap_data['type']))
1198
        {
1199
            $type = Type::find($issue['type']);
1200
            $snap_data['type'] = [ 'value' => isset($type->name) ? $type->name : '', 'name' => '类型' ];
1201
        }
1202
1203 View Code Duplication
        if (isset($issue['priority']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1204
        {
1205
            if ($issue['priority'])
1206
            { 
1207
                if (in_array('priority', $change_fields) || !isset($snap_data['priority']))
1208
                {
1209
                    $priority = Priority::Where('key', $issue['priority'])->orWhere('_id', $issue['priority'])->first();
1210
                    $snap_data['priority'] = [ 'value' => isset($priority->name) ? $priority->name : '', 'name' => '优先级' ];
1211
                }
1212
            }
1213
            else
1214
            {
1215
                $snap_data['priority'] = [ 'value' => '', 'name' => '优先级' ];
1216
            }
1217
        }
1218
1219 View Code Duplication
        if (isset($issue['state']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1220
        {
1221
            if ($issue['state'])
1222
            {
1223
                if (in_array('state', $change_fields) || !isset($snap_data['state']))
1224
                {
1225
                    $state = State::Where('key', $issue['state'])->orWhere('_id', $issue['state'])->first();
1226
                    $snap_data['state'] = [ 'value' => isset($state->name) ? $state->name : '', 'name' => '状态' ];
1227
                }
1228
            }
1229
            else
1230
            {
1231
                $snap_data['state'] = [ 'value' => '', 'name' => '状态' ];
1232
            }
1233
        }
1234
1235 View Code Duplication
        if (isset($issue['resolution']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1236
        {
1237
            if ($issue['resolution'])
1238
            {
1239
                if (in_array('resolution', $change_fields) || !isset($snap_data['resolution']))
1240
                {
1241
                    $resolution = Resolution::Where('key', $issue['resolution'])->orWhere('_id', $issue['resolution'])->first();
1242
                    $snap_data['resolution'] = [ 'value' => isset($resolution->name) ? $resolution->name : '', 'name' => '解决结果' ];
1243
                }
1244
            }
1245
            else
1246
            {
1247
                $snap_data['resolution'] = [ 'value' => '', 'name' => '解决结果' ];
1248
            }
1249
        }
1250
1251 View Code Duplication
        if (isset($issue['assignee']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1252
        {
1253
            if ($issue['assignee'])
1254
            {
1255
                if (in_array('assignee', $change_fields) || !isset($snap_data['assignee']))
1256
                {
1257
                    $snap_data['assignee'] = [ 'value' => $issue['assignee']['name'], 'name' => '经办人' ];
1258
                }
1259
            }
1260
            else
1261
            {
1262
                $snap_data['assignee'] = [ 'value' => '', 'name' => '经办人' ];
1263
            }
1264
        }
1265
        // labels
1266
        if (isset($issue['labels']))
1267
        {
1268
            if ($issue['labels'])
1269
            {
1270
                if (in_array('labels', $change_fields) || !isset($snap_data['labels']))
1271
                {
1272
                    $snap_data['labels'] = [ 'value' => implode(',', $issue['labels']), 'name' => '标签' ];
1273
                }
1274
            }
1275
            else
1276
            {
1277
                $snap_data['labels'] = [ 'value' => '', 'name' => '标签' ];
1278
            }
1279
        }
1280
1281
        // special fields handle
1282
        if (isset($issue['parent_id']))
1283
        {
1284
            if ($issue['parent_id'])
1285
            {
1286
                if (in_array('parent_id', $change_fields) || !isset($snap_data['parent_id']))
1287
                {
1288
                    $parent = DB::collection('issue_' . $project_key)->where('_id', $issue['parent_id'])->first(['no', 'title']);
1289
                    $snap_data['parent'] = [ 'value' => $parent['no'] . ' - ' . $parent['title'], 'name' => '父任务' ];
1290
                }
1291
            }
1292
            else
1293
            {
1294
                $snap_data['parent'] = [ 'value' => '', 'name' => '父任务' ];
1295
            }
1296
        }
1297
1298 View Code Duplication
        if (isset($issue['progress']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1299
        {
1300
            if ($issue['progress'] || $issue['progress'] === 0)
1301
            {
1302
                if (in_array('progress', $change_fields) || !isset($snap_data['progress']))
1303
                {
1304
                    $snap_data['progress'] = [ 'value' => $issue['progress'] . '%', 'name' => '进度' ];
1305
                }
1306
            }
1307
            else
1308
            {
1309
                $snap_data['progress'] = [ 'value' => '', 'name' => '进度' ];
1310
            }
1311
        }
1312
1313
        if (isset($issue['expect_start_time']))
1314
        {
1315
            if ($issue['expect_start_time'])
1316
            {
1317
                if (in_array('expect_start_time', $change_fields) || !isset($snap_data['expect_start_time']))
1318
                {
1319
                    $snap_data['expect_start_time'] = [ 'value' => date('Y/m/d', $issue['expect_start_time']), 'name' => '期望开始时间' ];
1320
                }
1321
            }
1322
            else
1323
            {
1324
                $snap_data['expect_start_time'] = [ 'value' => '', 'name' => '期望开始时间' ];
1325
            }
1326
        }
1327
1328
        if (isset($issue['expect_complete_time']))
1329
        {
1330
            if ($issue['expect_complete_time'])
1331
            {
1332
                if (in_array('expect_complete_time', $change_fields) || !isset($snap_data['expect_complete_time']))
1333
                {
1334
                    $snap_data['expect_complete_time'] = [ 'value' => date('Y/m/d', $issue['expect_complete_time']), 'name' => '期望完成时间' ];
1335
                }
1336
            }
1337
            else
1338
            {
1339
                $snap_data['expect_complete_time'] = [ 'value' => '', 'name' => '期望完成时间' ];
1340
            }
1341
        }
1342
1343
        if (!isset($snap_data['created_at']))
1344
        {
1345
            $snap_data['created_at'] = $issue['created_at'];
1346
        }
1347
1348
        if (!isset($snap_data['reporter']))
1349
        {
1350
            $snap_data['reporter'] = $issue['reporter'];
1351
        }
1352
1353
        $operated_at = isset($issue['updated_at']) ? $issue['updated_at'] : $issue['created_at'];
1354
        $operator = isset($issue['modifier']) ? $issue['modifier'] : $issue['reporter'];
1355
1356
        $snap_id = DB::collection('issue_his_' . $project_key)->insertGetId([ 'issue_id' => $issue['_id']->__toString(), 'operated_at' => $operated_at, 'operator' => $operator, 'data' => $snap_data ]);
1357
1358
        return $snap_id->__toString();
1359
    }
1360
1361
    /**
1362
     * check if issue exist.
1363
     *
1364
     * @param string $project_key
1365
     * @param string $issue_id
1366
     * @return bool
1367
     */
1368
    public static function isIssueExisted($project_key, $issue_id)
1369
    {
1370
        $isExisted = DB::collection('issue_' . $project_key)->where('_id', $issue_id)->exists();
1371
        return $isExisted;
1372
    }
1373
1374
    /**
1375
     * get all subtasks of the parent  
1376
     *
1377
     * @param string $project_key
1378
     * @param string $parent_no
1379
     * @return bool
1380
     */
1381
    public static function getChildrenByParentNo($project_key, $parent_no)
1382
    {
1383
        $parent = DB::collection('issue_' . $project_key)->where('no', $parent_no)->first();
1384
        if (!$parent) { return []; }
1385
1386
        $children = [];
1387
        $subtasks = DB::collection('issue_' . $project_key)->where('parent_id', $parent['_id']->__toString())->get(['no']);
1388
        foreach ($subtasks as $subtask)
1389
        {
1390
            $children[] = $subtask['no'];
1391
        }
1392
        return $children;
1393
    }
1394
1395
    /**
1396
     * get epic list.
1397
     *
1398
     * @param string $project_key
1399
     * @param array $fields
1400
     * @return collection
1401
     */
1402
    public static function getEpicList($project_key, $fields=[])
1403
    {
1404
        $epics = Epic::Where('project_key', $project_key)
1405
            ->orderBy('sn', 'asc')
1406
            ->get($fields)
1407
            ->toArray();
1408
1409
        return $epics;
1410
    }
1411
1412
    /**
1413
     * check if Epic has existed.
1414
     *
1415
     * @param string $project_key
1416
     * @param string $name
1417
     * @return bool
1418
     */
1419
    public static function isEpicExisted($project_key, $name)
1420
    {
1421
        $isExisted = Epic::Where('project_key', $project_key)
1422
            ->Where('name', $name)
1423
            ->exists();
1424
1425
        return $isExisted;
1426
    }
1427
1428
    /**
1429
     * get sprint list.
1430
     *
1431
     * @param string $project_key
1432
     * @return collection
1433
     */
1434
    public static function getSprintList($project_key, $fields=[])
1435
    {
1436
        $sprints = Sprint::Where('project_key', $project_key)
1437
            ->WhereIn('status', [ 'active', 'completed' ])
1438
            ->orderBy('no', 'desc')
1439
            ->get($fields)
1440
            ->toArray();
1441
1442
        foreach($sprints as $key => $sprint)
1443
        {
1444
            if (!isset($sprint['name']))
1445
            {
1446
                $sprints[$key]['name'] = 'Sprint ' . $sprint['no'];
1447
            }
1448
        }
1449
1450
        return $sprints;
1451
    }
1452
1453
    /**
1454
     * check if Label has existed.
1455
     *
1456
     * @param string $project_key
1457
     * @param string $name
1458
     * @return bool
1459
     */
1460
    public static function isLabelExisted($project_key, $name)
1461
    {
1462
        $isExisted = Labels::Where('project_key', $project_key)
1463
            ->Where('name', $name)
1464
            ->exists();
1465
1466
        return $isExisted;
1467
    }
1468
}
1469
1470