Provider::getLabelOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
    /**
39
     * get project principal.
40
     *
41
     * @param  string $project_key
42
     * @return array 
43
     */
44
    public static function getProjectPrincipal($project_key)
45
    {
46
        $project = Project::where('key', $project_key)->first()->toArray();
47
        return $project && isset($project['principal']) ? $project['principal'] : [];
48
    }
49
50
    /**
51
     * get the project default filters.
52
     *
53
     * @return array
54
     */
55
    public static function getDefaultIssueFilters()
56
    {
57
        return [
58
            [ 'id' => 'all', 'name' => '全部问题', 'query' => [] ],
59
            [ 'id' => 'unresolved', 'name' => '未解决的', 'query' => [ 'resolution' => 'Unresolved' ] ],
60
            [ 'id' => 'assigned_to_me', 'name' => '分配给我的', 'query' => [ 'assignee' => 'me', 'resolution' => 'Unresolved' ] ],
61
            [ 'id' => 'watched', 'name' => '我关注的', 'query' => [ 'watcher' => 'me' ] ],
62
            [ 'id' => 'reported', 'name' => '我报告的', 'query' => [ 'reporter' => 'me' ] ],
63
            [ 'id' => 'recent_created', 'name' => '最近增加的', 'query' => [ 'created_at' => '2w' ] ],
64
            [ 'id' => 'recent_updated', 'name' => '最近更新的', 'query' => [ 'updated_at' => '2w' ] ],
65
            [ 'id' => 'recent_resolved', 'name' => '最近解决的', 'query' => [ 'resolved_at' => '2w' ] ],
66
            [ 'id' => 'recent_closed', 'name' => '最近关闭的', 'query' => [ 'closed_at' => '2w' ] ],
67
        ];
68
    }
69
70
    /**
71
     * get the project filters.
72
     *
73
     * @param  string $project_key
74
     * @param  string $user_id
75
     * @return array
76
     */
77
    public static function getIssueFilters($project_key, $user_id)
78
    {
79
        // default issue filters
80
        $filters = self::getDefaultIssueFilters();
81
82
        $res = UserIssueFilters::where('project_key', $project_key)
83
            ->where('user', $user_id)
84
            ->first(); 
85
        if ($res) {
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
            $columns = isset($res->columns) ? $res->columns : [];
101
            return $columns;
102
        }
103
104
        return [
105
            [ 'key' => 'assignee', 'width' => '100' ],
106
            [ 'key' => 'priority', 'width' => '70' ],
107
            [ 'key' => 'state', 'width' => '100' ],
108
            [ 'key' => 'resolution', 'width' => '100' ],
109
        ];
110
    }
111
112
    /**
113
     * get the issue list columns.
114
     *
115
     * @param  string $project_key
116
     * @param  string $user_id
117
     * @return array
118
     */
119
    public static function getIssueDisplayColumns($project_key, $user_id)
120
    {
121
        // default issue filters
122
        $columns = self::getDefaultDisplayColumns($project_key);
123
        $res = UserIssueListColumns::where('project_key', $project_key)
124
            ->where('user', $user_id)
125
            ->first(); 
126
        if ($res) {
127
            $columns = isset($res->columns) ? $res->columns : [];
128
        }
129
        return $columns;
130
    }
131
132
    /**
133
     * get state list.
134
     *
135
     * @param  string $project_key
136
     * @param  array  $fields
137
     * @return collection 
138
     */
139
    public static function getStateList($project_key, $fields=[])
140
    {
141
        $states = State::Where('project_key', '$_sys_$')
142
            ->orWhere('project_key', $project_key)
143
            ->orderBy('project_key', 'asc')
144
            ->orderBy('sn', 'asc')
145
            ->get($fields)
146
            ->toArray();
147
148
        $stateProperty = StateProperty::Where('project_key', $project_key)->first();
149
        if ($stateProperty) {
150
            if ($sequence = $stateProperty->sequence) {
151
                $func = function ($v1, $v2) use ($sequence) {
152
                    $i1 = array_search($v1['_id'], $sequence);
153
                    $i1 = $i1 !== false ? $i1 : 998;
154
                    $i2 = array_search($v2['_id'], $sequence);
155
                    $i2 = $i2 !== false ? $i2 : 999;
156
                    return $i1 >= $i2 ? 1 : -1;
157
                };
158
                usort($states, $func);
159
            }
160
        }
161
162
        return $states;
163
    }
164
165
    /**
166
     * get state options.
167
     *
168
     * @param  string $project_key
169
     * @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...
170
     * @return collection
171
     */
172 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...
173
    {
174
        $states = self::getStateList($project_key);
175
176
        $options = [];
177
        foreach ($states as $state)
178
        {
179
            $tmp = [];
180
            $tmp['_id'] = isset($state['key']) && $state['key'] ? $state['key'] : $state['_id'];
181
            $tmp['name'] = isset($state['name']) ? trim($state['name']) : '';
182
            $tmp['category'] = isset($state['category']) ? $state['category'] : '';
183
            $options[] = $tmp;
184
        }
185
        return $options;
186
    }
187
188
    /**
189
     * get state options.
190
     *
191
     * @param  string $project_key
192
     * @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...
193
     * @return collection
194
     */
195
    public static function getLabelOptions($project_key)
196
    {
197
        $options = [];
198
199
        $labels = Labels::Where('project_key', $project_key)
200
            ->orderBy('_id', 'desc')
201
            ->get();
202
        foreach ($labels as $label)
203
        {
204
            $options[] = [ 'name' => $label->name, 'bgColor' => $label->bgColor ?: '' ];
205
        }
206
207
        return $options;
208
    }
209
210
    /**
211
     * get event list.
212
     *
213
     * @param  string $project_key
214
     * @param  array  $fields
215
     * @return collection 
216
     */
217
    public static function getEventList($project_key, $fields=[])
218
    {
219
        $events = Events::Where('project_key', '$_sys_$')
220
            ->orWhere('project_key', $project_key)
221
            ->orderBy('project_key', 'asc')
222
            ->orderBy('_id', 'asc')
223
            ->get($fields);
224
225
        return $events;
226
    }
227
228
    /**
229
     * get event options.
230
     *
231
     * @param  string $project_key
232
     * @return collection
233
     */
234
    public static function getEventOptions($project_key)
235
    {
236
        $events = self::getEventList($project_key); 
237
238
        $options = [];
239
        foreach ($events as $event)
240
        {
241
            if (!isset($event->apply) || $event->apply !== 'workflow') {
242
                continue;
243
            }
244
245
            $tmp = [];
246
            $tmp['_id'] = isset($event['key']) ? $event['key'] : $event['_id'];
247
            $tmp['name'] = isset($event['name']) ? trim($event['name']) : '';
248
            $options[] = $tmp;
249
        }
250
251
        return $options;
252
    }
253
254
    /**
255
     * get default priority.
256
     *
257
     * @param  string $project_key
258
     * @return string 
259
     */
260 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...
261
    {
262
        $priorityProperty = PriorityProperty::Where('project_key', $project_key)->first();
263
        if ($priorityProperty) {
264
            if ($defaultValue = $priorityProperty->defaultValue) {
265
                return $defaultValue;
266
            }
267
        }
268
269
        $priority = Priority::whereRaw([ 'project_key' => [ '$in' =>  [ '$_sys_$', $project_key ] ] ])
270
            ->Where('default', true)
271
            ->first();
272
273
        $default = $priority && isset($priority->key) && $priority->key ? $priority->key : $priority->id;
274
        return $default; 
275
    }
276
277
    /**
278
     * get priority list.
279
     *
280
     * @param  string $project_key
281
     * @param  array  $fields
282
     * @return collection
283
     */
284 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...
285
    {
286
        $priorities = Priority::Where('project_key', '$_sys_$')
287
            ->orWhere('project_key', $project_key)
288
            ->orderBy('project_key', 'asc')
289
            ->orderBy('sn', 'asc')
290
            ->get($fields)
291
            ->toArray();
292
293
        $priorityProperty = PriorityProperty::Where('project_key', $project_key)->first();
294
        if ($priorityProperty) {
295
            if ($sequence = $priorityProperty->sequence) {
296
                $func = function ($v1, $v2) use ($sequence) {
297
                    $i1 = array_search($v1['_id'], $sequence);
298
                    $i1 = $i1 !== false ? $i1 : 998;
299
                    $i2 = array_search($v2['_id'], $sequence);
300
                    $i2 = $i2 !== false ? $i2 : 999;
301
                    return $i1 >= $i2 ? 1 : -1;
302
                };
303
                usort($priorities, $func);
304
            }
305
306
            if ($defaultValue = $priorityProperty->defaultValue) {
307
                foreach($priorities as $key => $val)
308
                {
309
                    if ($val['_id'] == $defaultValue) {
310
                        $priorities[$key]['default'] = true;
311
                    }
312
                    else if (isset($val['default'])) {
313
                        unset($priorities[$key]['default']);
314
                    }
315
                }
316
            }
317
        }
318
319
        return $priorities;
320
    }
321
322
    /**
323
     * get priority options.
324
     *
325
     * @param  string $project_key
326
     * @return array
327
     */
328
    public static function getPriorityOptions($project_key)
329
    {
330
        $priorities = self::getPriorityList($project_key);
331
332
        $options = [];
333
        foreach ($priorities as $priority)
334
        {
335
            $tmp = [];
336
            $tmp['_id'] = isset($priority['key']) && $priority['key'] ? $priority['key'] : $priority['_id'];
337
            $tmp['name'] = isset($priority['name']) ? trim($priority['name']) : '';
338
            if (isset($priority['default'])) {
339
                $tmp['default'] = $priority['default'];
340
            }
341
            if (isset($priority['color'])) {
342
                $tmp['color'] = $priority['color'];
343
            }
344
            $options[] = $tmp;
345
        }
346
        return $options;
347
    }
348
349
    /**
350
     * get default resolution.
351
     *
352
     * @param  string $project_key
353
     * @return string 
354
     */
355 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...
356
    {
357
        $resolutionProperty = ResolutionProperty::Where('project_key', $project_key)->first();
358
        if ($resolutionProperty) {
359
            if ($defaultValue = $resolutionProperty->defaultValue) {
360
                return $defaultValue;
361
            }
362
        }
363
364
        $resolution = Resolution::whereRaw([ 'project_key' => [ '$in' =>  [ '$_sys_$', $project_key ] ] ])
365
            ->Where('default', true)
366
            ->first();
367
368
        $default = $resolution && isset($resolution->key) && $resolution->key ? $resolution->key : $resolution->id;
369
        return $default;
370
    }
371
372
    /**
373
     * get resolution list.
374
     *
375
     * @param  string $project_key
376
     * @param  array  $fields
377
     * @return collection
378
     */
379 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...
380
    {
381
        $resolutions = Resolution::Where('project_key', '$_sys_$')
382
            ->orWhere('project_key', $project_key)
383
            ->orderBy('project_key', 'asc')
384
            ->orderBy('sn', 'asc')
385
            ->get($fields)
386
            ->toArray();
387
388
        $resolutionProperty = ResolutionProperty::Where('project_key', $project_key)->first();
389
        if ($resolutionProperty) {
390
            if ($sequence = $resolutionProperty->sequence) {
391
                $func = function ($v1, $v2) use ($sequence) {
392
                    $i1 = array_search($v1['_id'], $sequence);
393
                    $i1 = $i1 !== false ? $i1 : 998;
394
                    $i2 = array_search($v2['_id'], $sequence);
395
                    $i2 = $i2 !== false ? $i2 : 999;
396
                    return $i1 >= $i2 ? 1 : -1;
397
                };
398
                usort($resolutions, $func);
399
            }
400
401
            if ($defaultValue = $resolutionProperty->defaultValue) {
402
                foreach($resolutions as $key => $val)
403
                {
404
                    if ($val['_id'] == $defaultValue) {
405
                        $resolutions[$key]['default'] = true;
406
                    }
407
                    else if (isset($val['default'])) {
408
                        unset($resolutions[$key]['default']);
409
                    }
410
                }
411
            }
412
        }
413
414
        return $resolutions;
415
    }
416
417
    /**
418
     * get resolution options.
419
     *
420
     * @param  string $project_key
421
     * @return array 
422
     */
423 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...
424
    {
425
        $resolutions = self::getResolutionList($project_key);
426
427
        $options = [];
428
        foreach ($resolutions as $resolution)
429
        {
430
            $tmp = [];
431
            $tmp['_id'] = isset($resolution['key']) && $resolution['key'] ? $resolution['key'] : $resolution['_id'];
432
            $tmp['name'] = isset($resolution['name']) ? trim($resolution['name']) : '';
433
            if (isset($resolution['default'])) {
434
                $tmp['default'] = $resolution['default'];
435
            }
436
            $options[] = $tmp;
437
        }
438
        return $options;
439
    }
440
441
    /**
442
     * get screen list.
443
     *
444
     * @param  string $project_key
445
     * @param  array  $fields
446
     * @return collection
447
     */
448
    public static function getScreenList($project_key, $fields=[])
449
    {
450
        $screens = Screen::Where('project_key', '$_sys_$')
451
            ->orWhere('project_key', $project_key)
452
            ->orderBy('project_key', 'asc')
453
            ->orderBy('_id', 'asc')
454
            ->get($fields);
455
456
        return $screens;
457
    }
458
459
    /**
460
     * get field list.
461
     *
462
     * @param  string $project_key
463
     * @param  array  $fields
464
     * @return collection
465
     */
466
    public static function getFieldList($project_key, $fields=[])
467
    {
468
        $fields = Field::Where('project_key', '$_sys_$')
469
            ->orWhere('project_key', $project_key)
470
            ->orderBy('project_key', 'asc')
471
            ->orderBy('_id', 'asc')
472
            ->get($fields);
473
474
        return $fields;
475
    }
476
477
    /**
478
     * get workflow list.
479
     *
480
     * @param  string $project_key
481
     * @param  array  $fields
482
     * @return collection
483
     */
484
    public static function getWorkflowList($project_key, $fields=[])
485
    {
486
        $workflows = Definition::Where('project_key', '$_sys_$')
487
            ->orWhere('project_key', $project_key)
488
            ->orderBy('project_key', 'asc')
489
            ->orderBy('_id', 'asc')
490
            ->get($fields);
491
492
        return $workflows;
493
    }
494
495
496
    /**
497
     * get type list.
498
     *
499
     * @param  string $project_key
500
     * @param  array  $fields
501
     * @return collection
502
     */
503
    public static function getTypeList($project_key, $fields=[])
504
    {
505
        $types = Type::Where('project_key', $project_key)
506
            ->orderBy('sn', 'asc')
507
            ->get($fields);
508
509
        return $types;
510
    }
511
512
    /**
513
     * get role list.
514
     *
515
     * @param  string $project_key
516
     * @param  array  $fields
517
     * @return collection
518
     */
519
    public static function getRoleList($project_key, $fields=[])
520
    {
521
        $roles = Role::Where('project_key', '$_sys_$')
522
            ->orWhere('project_key', $project_key)
523
            ->orderBy('project_key', 'asc')
524
            ->orderBy('_id', 'asc')
525
            ->get($fields);
526
527
        return $roles;
528
    }
529
530
    /**
531
     * get user list.
532
     *
533
     * @param  string $project_key
534
     * @return array
535
     */
536
    public static function getUserList($project_key)
537
    {
538
        $user_group_ids = UserGroupProject::Where('project_key', $project_key)
539
            ->Where('link_count', '>', 0)
540
            ->get(['ug_id', 'type']);
541
542
        $user_ids = [];
543
        $group_ids = [];
544
        foreach ($user_group_ids as $value)
545
        {
546
            if (isset($value->type) && $value->type === 'group') {
547
                $group_ids[] = $value->ug_id;
548
            }
549
            else
550
            {
551
                $user_ids[] = $value->ug_id;
552
            }
553
        }
554
555
        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...
556
            $groups = Group::find($group_ids);
557
            foreach($groups as $group)
558
            {
559
                $user_ids = array_merge($user_ids, isset($group->users) && $group->users ? $group->users : []);
560
            }
561
        }
562
        $user_ids = array_unique($user_ids);
563
564
        $user_list = [];
565
        $users = EloquentUser::find($user_ids);
566 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...
567
        {
568
            if (isset($user->invalid_flag) && $user->invalid_flag === 1) {
569
                continue;
570
            }
571
            $user_list[] = ['id' => $user->id, 'name' => $user->first_name, 'email' => $user->email ];
572
        }
573
574
        return $user_list;
575
    }
576
577
    /**
578
     * get assigned user list.
579
     *
580
     * @param  string $project_key
581
     * @return array
582
     */
583
    public static function getAssignedUsers($project_key)
584
    {
585
        $user_ids = Acl::getUserIdsByPermission('assigned_issue', $project_key);
586
587
        $user_list = [];
588
        $users = EloquentUser::find($user_ids); 
589 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...
590
        {
591
            if (isset($user->invalid_flag) && $user->invalid_flag === 1) {
592
                continue;
593
            }
594
            $user_list[] = [ 'id' => $user->id, 'name' => $user->first_name, 'email' => $user->email ];
595
        }
596
        return $user_list;
597
    }
598
599
    /**
600
     * get version list.
601
     *
602
     * @param  string $project_key
603
     * @param  array  $fields
604
     * @return collection
605
     */
606
    public static function getVersionList($project_key, $fields=[])
607
    {
608
        $versions = Version::where([ 'project_key' => $project_key ])
609
            ->orderBy('status', 'desc')
610
            ->orderBy('released_time', 'desc')
611
            ->orderBy('end_time', 'desc')
612
            ->orderBy('created_at', 'desc')
613
            ->get($fields);
614
615
        return $versions;
616
    }
617
618
    /**
619
     * get module list.
620
     *
621
     * @param  string $project_key
622
     * @param  array  $fields
623
     * @return collection
624
     */
625
    public static function getModuleList($project_key, $fields=[])
626
    {
627
        $modules = Module::where([ 'project_key' => $project_key ])
628
            ->orderBy('sn', 'asc')
629
            ->get($fields);
630
631
        return $modules;
632
    }
633
634
    /**
635
     * check if type has existed.
636
     *
637
     * @param  string $project_key
638
     * @param  string $name 
639
     * @return bool
640
     */
641
    public static function isTypeExisted($project_key, $name)
642
    {
643
        $isExisted = Type::Where('project_key', $project_key)
644
            ->Where('name', $name)
645
            ->exists();
646
647
        return $isExisted;
648
    }
649
650
    /**
651
     * check if type abb has existed.
652
     *
653
     * @param  string $project_key
654
     * @param  string $abb 
655
     * @return bool
656
     */
657
    public static function isTypeAbbExisted($project_key, $abb)
658
    {
659
        $isExisted = Type::Where('project_key', $project_key)
660
            ->Where('abb', $abb)
661
            ->exists();
662
663
        return $isExisted;
664
    }
665
666
    /**
667
     * check if state has existed.
668
     *
669
     * @param  string $project_key
670
     * @param  string $name 
671
     * @return bool
672
     */
673
    public static function isStateExisted($project_key, $name)
674
    {
675
        $isExisted = State::Where('project_key', '$_sys_$')
676
            ->orWhere('project_key', $project_key)
677
            ->Where('name', $name)
678
            ->exists();
679
680
        return $isExisted;
681
    }
682
683
    /**
684
     * check if priority has existed.
685
     *
686
     * @param  string $project_key
687
     * @param  string $name
688
     * @return bool
689
     */
690
    public static function isPriorityExisted($project_key, $name)
691
    {
692
        $isExisted = Priority::Where('project_key', '$_sys_$')
693
            ->orWhere('project_key', $project_key)
694
            ->Where('name', $name)
695
            ->exists();
696
697
        return $isExisted;
698
    }
699
700
    /**
701
     * check if resolution has existed.
702
     *
703
     * @param  string $project_key
704
     * @param  string $name
705
     * @return bool
706
     */
707
    public static function isResolutionExisted($project_key, $name)
708
    {
709
        $isExisted = Resolution::Where('project_key', '$_sys_$')
710
            ->orWhere('project_key', $project_key)
711
            ->Where('name', $name)
712
            ->exists();
713
714
        return $isExisted;
715
    }
716
717
    /**
718
     * check if field has existed.
719
     *
720
     * @param  string $project_key
721
     * @param  string $key
722
     * @return bool
723
     */
724
    public static function isFieldKeyExisted($project_key, $key)
725
    {
726
        $fields = Field::Where('project_key', '$_sys_$')
727
            ->orWhere('project_key', $project_key)
728
            ->get();
729
        foreach ($fields as $field)
730
        {
731
            if ($field->key === $key || ($field->type === 'MutiUser' && $field->key . '_ids' === $key) || ($field->type === 'TimeTracking' && $field->key . '_m' === $key)) {
732
                return true;
733
            }
734
        }
735
736
        return false;
737
    }
738
739
    /**
740
     * check if Event has existed.
741
     *
742
     * @param  string $project_key
743
     * @param  string $name
744
     * @return bool
745
     */
746
    public static function isEventExisted($project_key, $name)
747
    {
748
        $isExisted = Events::Where('project_key', '$_sys_$')
749
            ->orWhere('project_key', $project_key)
750
            ->Where('name', $name)
751
            ->exists();
752
753
        return $isExisted;
754
    }
755
756
    /**
757
     * get issue type schema 
758
     *
759
     * @param  string $project_key
760
     * @return array 
761
     */
762
    public static function getTypeListExt($project_key, $options)
763
    {
764
        $typeOptions = [];
765
        $types = self::getTypeList($project_key);
766
        foreach ($types as $key => $type)
767
        {
768
            $schema = self::_repairSchema($project_key, $type->id, $type->screen && $type->screen->schema ? $type->screen->schema : [], $options);
769
770
            $tmp = [ 'id' => $type->id, 'name' => $type->name, 'abb' => $type->abb, 'disabled' => $type->disabled && true, 'type' => $type->type == 'subtask' ? 'subtask' : 'standard', 'schema' => $schema ];
771
            if ($type->default) {
772
                $tmp['default'] = true;
773
            }
774
            $typeOptions[] = $tmp;
775
        }
776
        return $typeOptions;
777
    }
778
779
    /**
780
     * get issue type schema
781
     *
782
     * @param  string $project_key
783
     * @return array
784
     */
785
    private static function _repairSchema($project_key, $issue_type, $schema, $options)
786
    {
787
        $new_schema = [];
788
        foreach ($schema as $key => $val)
789
        {
790 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...
791
                if ($val['applyToTypes'] && !in_array($issue_type, explode(',', $val['applyToTypes'] ?: ''))) {
792
                    continue;
793
                }
794
                unset($val['applyToTypes']);
795
            }
796
797
            if ($val['type'] == 'SingleVersion' || $val['type'] == 'MultiVersion') {
798
                if (!isset($options['version'])) {
799
                    $options['version'] = self::getVersionList($project_key);
800
                }
801
                $val['optionValues'] = self::pluckFields($options['version'], ['_id', 'name']);
802
            }
803
            else if ($val['type'] == 'SingleUser' || $val['type'] == 'MultiUser') {
804
                $val['optionValues'] = self::pluckFields($options['user'], ['id', 'name', 'email']);
805 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...
806
                {
807
                    $val['optionValues'][$k]['name'] = $v['name'] . '(' . $v['email'] . ')';
808
                    unset($val['optionValues'][$k]['email']);
809
                }
810
            }
811
            else if ($val['key'] == 'assignee') {
812
                $val['optionValues'] = self::pluckFields($options['assignee'], ['id', 'name', 'email']);
813 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...
814
                {
815
                    $val['optionValues'][$k]['name'] = $v['name'] . '(' . $v['email'] . ')';
816
                    unset($val['optionValues'][$k]['email']);
817
                }
818
            }
819
            else if ($val['key'] == 'labels') {
820
                $couple_labels = [];
821 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...
822
                {
823
                    $couple_labels[] = [ 'id' => $label['name'], 'name' => $label['name'] ];
824
                }
825
                $val['optionValues'] = $couple_labels;
826
            }
827
            else if (array_key_exists($val['key'], $options)) {
828
                $val['optionValues'] = self::pluckFields($options[$val['key']], ['_id', 'name']);
829 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...
830
                {
831
                    if (isset($val2['default']) && $val2['default']) {
832
                        $val['defaultValue'] = $val2['_id'];
833
                        break;
834
                    }
835
                }
836
            }
837
            if (isset($val['_id'])) {
838
                unset($val['_id']);
839
            }
840
            $new_schema[] = $val;
841
        }
842
        return $new_schema;
843
    }
844
845
    /**
846
     * filter the fields.
847
     *
848
     * @param  array $srcData
849
     * @param  array $fields
850
     * @return array 
851
     */
852
    public static function pluckFields($srcData, $fields)
853
    {
854
        $destData = [];
855
        foreach ($srcData as $val)
856
        {
857
            $tmp = [];
858
            foreach ($fields as $field)
859
            {
860
                if ($field === '_id') {
861
                    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...
862
                        $tmp['id'] = $val[$field]->__toString();
863
                    } else {
864
                        $tmp['id'] = isset($val[$field]) ? $val[$field] : '';
865
                    }
866
                } else {
867
                    $tmp[$field] = isset($val[$field]) ? $val[$field] : '';
868
                }
869
            }
870
            $destData[] = $tmp;
871
        } 
872
        return $destData;
873
    }
874
875
    /**
876
     * get module principal.
877
     *
878
     * @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...
879
     * @param  string $mid
880
     * @return array 
881
     */
882
    public static function getModuleById($mid)
883
    {
884
        $module = Module::find($mid)->toArray();
885
        return $module ?: [];
886
    }
887
888
    /**
889
     * get workflow by type_id
890
     *
891
     * @param  string $type_id
892
     * @return array
893
     */
894
    public static function getWorkflowByType($type_id)
895
    {
896
        $type = Type::find($type_id);
897
        return $type->workflow;
898
    }
899
900
    /**
901
     * get schema by type_id
902
     *
903
     * @param  string $type_id
904
     * @return array
905
     */
906
    public static function getSchemaByType($type_id)
907
    {
908
        $type = Type::find($type_id);
909
        if (!$type) {
910
            return [];
911
        }
912
913
        $screen = $type->screen;
914
        $project_key = $type->project_key;
915
916
        return self::getScreenSchema($project_key, $type_id, $screen);
917
    }
918
919
    /**
920
     * get schema by screen_id
921
     *
922
     * @param  string $project_key
923
     * @param  string $type
924
     * @param  string $screen_id
925
     * @return array
926
     */
927
    public static function getSchemaByScreenId($project_key, $type, $screen_id)
928
    {
929
        $screen = Screen::find($screen_id);
930
        if (!$screen) {
931
            return [];
932
        }
933
        return self::getScreenSchema($project_key, $type, $screen);
934
    }
935
936
    /**
937
     * get screen schema
938
     *
939
     * @param  string $type_id
940
     * @return array
941
     */
942
    public static function getScreenSchema($project_key, $type_id, $screen)
943
    {
944
        $new_schema = [];
945
        $versions = null;
946
        $users = null;
947
        foreach ($screen->schema ?: [] as $key => $val)
948
        {
949 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...
950
                if ($val['applyToTypes'] && !in_array($type_id, explode(',', $val['applyToTypes'] ?: ''))) {
951
                    continue;
952
                }
953
                unset($val['applyToTypes']);
954
            }
955
956
            if ($val['key'] == 'assignee') {
957
                $users = self::getAssignedUsers($project_key);
958 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...
959
                {
960
                    $users[$key]['name'] = $user['name'] . '(' . $user['email'] . ')';
961
                } 
962
                $val['optionValues'] = self::pluckFields($users, ['id', 'name']);
963
            }
964
            else if ($val['key'] == 'resolution') {
965
                $resolutions = self::getResolutionOptions($project_key);
966
                $val['optionValues'] = self::pluckFields($resolutions, ['_id', 'name']);
967 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...
968
                {
969
                    if (isset($val2['default']) && $val2['default']) {
970
                        $val['defaultValue'] = $val2['_id'];
971
                        break;
972
                    }
973
                }
974
            }
975
            else if ($val['key'] == 'priority') {
976
                $priorities = self::getPriorityOptions($project_key);
977
                $val['optionValues'] = self::pluckFields($priorities, ['_id', 'name']);
978 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...
979
                {
980
                    if (isset($val2['default']) && $val2['default']) {
981
                        $val['defaultValue'] = $val2['_id'];
982
                        break;
983
                    }
984
                }
985
            }
986
            else if ($val['key'] == 'module') {
987
                $modules = self::getModuleList($project_key);
988
                $val['optionValues'] = self::pluckFields($modules, ['_id', 'name']);
989
            }
990
            else if ($val['key'] == 'epic') {
991
                $epics = self::getEpicList($project_key);
992
                $val['optionValues'] = self::pluckFields($epics, ['_id', 'name', 'bgColor']);
993
            }
994
            else if ($val['key'] == 'labels') {
995
                $labels = self::getLabelOptions($project_key);
996
                $couple_labels = [];
997 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...
998
                {
999
                    $couple_labels[] = [ 'id' => $label['name'], 'name' => $label['name'] ];
1000
                }
1001
                $val['optionValues'] = $couple_labels;
1002
            }
1003
            else if ($val['type'] == 'SingleVersion' || $val['type'] == 'MultiVersion') {
1004
                $versions === null && $versions = self::getVersionList($project_key);
1005
                $val['optionValues'] = self::pluckFields($versions, ['_id', 'name']);
0 ignored issues
show
Bug introduced by
It seems like $versions defined by null on line 945 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...
1006
            }
1007
            else if ($val['type'] == 'SingleUser' || $val['type'] == 'MultiUser') {
1008
                $users === null && $users = self::getUserList($project_key);
1009 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...
1010
                {
1011
                    $users[$key]['name'] = $user['name'] . '(' . $user['email'] . ')';
1012
                }
1013
                $val['optionValues'] = self::pluckFields($users, ['id', 'name']);
0 ignored issues
show
Bug introduced by
It seems like $users defined by null on line 946 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...
1014
            }
1015
1016
            if (isset($val['_id'])) {
1017
                unset($val['_id']);
1018
            }
1019
1020
            $new_schema[] = $val;
1021
        }
1022
1023
        return $new_schema;
1024
    }
1025
1026
    /**
1027
     * snap issue data to history
1028
     *
1029
     * @param  string $project_key
1030
     * @param  string $issue_id
1031
     * @param  array  $schema
1032
     * @param  array  $change_fields
1033
     * @return \Illuminate\Http\Response
1034
     */
1035
    public static function snap2His($project_key, $issue_id, $schema = [], $change_fields=[])
1036
    {
1037
        //获取问题数据
1038
        $issue = DB::collection('issue_' . $project_key)->where('_id', $issue_id)->first();
1039
1040
        $latest_ver_issue = DB::collection('issue_his_' . $project_key)->where('issue_id', $issue_id)->orderBy('_id', 'desc')->first();
1041
        if ($latest_ver_issue) {
1042
            $snap_data = $latest_ver_issue['data'];
1043
        }
1044
        else
1045
        {
1046
            $snap_data = [];
1047
        }
1048
1049
        // fetch the schema data
1050
        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...
1051
            $schema = [];
1052
            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...
1053
                $out_schema_fields = [ 'type', 'state', 'resolution', 'priority', 'assignee', 'labels', 'parent_id', 'progress', 'expect_start_time', 'expect_complete_time' ];
1054
                if (array_diff($change_fields, $out_schema_fields)) {
1055
                    $schema = self::getSchemaByType($issue['type']);
1056
                }
1057
            }
1058
            else
1059
            {
1060
                $schema = self::getSchemaByType($issue['type']);
1061
            }
1062
        }
1063
1064
        foreach ($schema as $field)
1065
        {
1066
            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...
1067
                continue;
1068
            }
1069
1070
            if (isset($issue[$field['key']])) {
1071
                $val = [];
1072
                $val['name'] = $field['name'];
1073
                
1074
                if ($field['type'] === 'SingleUser' || $field['type'] === 'MultiUser') {
1075
                    if ($field['type'] === 'SingleUser') {
1076
                        $val['value'] = $issue[$field['key']] ? $issue[$field['key']]['name'] : $issue[$field['key']];
1077
                    }
1078
                    else
1079
                    {
1080
                        $tmp_users = [];
1081
                        if ($issue[$field['key']]) {
1082
                            foreach ($issue[$field['key']] as $tmp_user)
1083
                            {
1084
                                $tmp_users[] = $tmp_user['name'];
1085
                            }
1086
                        }
1087
                        $val['value'] = implode(',', $tmp_users);
1088
                    }
1089
                }
1090
                else if (isset($field['optionValues']) && $field['optionValues']) {
1091
                    $opv = [];
1092
                    
1093
                    if (!is_array($issue[$field['key']])) {
1094
                        $fieldValues = explode(',', $issue[$field['key']]);
1095
                    }
1096
                    else
1097
                    {
1098
                        $fieldValues = $issue[$field['key']];
1099
                    }
1100
                    foreach ($field['optionValues'] as $ov)
1101
                    {
1102
                        if (in_array($ov['id'], $fieldValues)) {
1103
                            $opv[] = $ov['name'];
1104
                        }
1105
                    }
1106
                    $val['value'] = implode(',', $opv);
1107
                }
1108
                else if ($field['type'] == 'File') {
1109
                    $val['value'] = [];
1110
                    foreach ($issue[$field['key']] as $fid)
1111
                    {
1112
                        $file = File::find($fid);
1113
                        array_push($val['value'], $file->name);
1114
                    }
1115
                }
1116
                else if ($field['type'] == 'DatePicker' || $field['type'] == 'DateTimePicker') {
1117
                    $val['value'] = $issue[$field['key']] ? date($field['type'] == 'DatePicker' ? 'Y/m/d' : 'Y/m/d H:i:s', $issue[$field['key']]) : $issue[$field['key']];
1118
                }
1119
                else
1120
                {
1121
                    $val['value'] = $issue[$field['key']];
1122
                }
1123
                //$val['type'] = $field['type']; 
1124
1125
                $snap_data[$field['key']] = $val;
1126
            }
1127
        }
1128
1129
        // special fields handle
1130
        if (in_array('type', $change_fields) || !isset($snap_data['type'])) {
1131
            $type = Type::find($issue['type']);
1132
            $snap_data['type'] = [ 'value' => isset($type->name) ? $type->name : '', 'name' => '类型' ];
1133
        }
1134
1135 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...
1136
            if ($issue['priority']) { 
1137
                if (in_array('priority', $change_fields) || !isset($snap_data['priority'])) {
1138
                    $priority = Priority::Where('key', $issue['priority'])->orWhere('_id', $issue['priority'])->first();
1139
                    $snap_data['priority'] = [ 'value' => isset($priority->name) ? $priority->name : '', 'name' => '优先级' ];
1140
                }
1141
            }
1142
            else
1143
            {
1144
                $snap_data['priority'] = [ 'value' => '', 'name' => '优先级' ];
1145
            }
1146
        }
1147
1148 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...
1149
            if ($issue['state']) {
1150
                if (in_array('state', $change_fields) || !isset($snap_data['state'])) {
1151
                    $state = State::Where('key', $issue['state'])->orWhere('_id', $issue['state'])->first();
1152
                    $snap_data['state'] = [ 'value' => isset($state->name) ? $state->name : '', 'name' => '状态' ];
1153
                }
1154
            }
1155
            else
1156
            {
1157
                $snap_data['state'] = [ 'value' => '', 'name' => '状态' ];
1158
            }
1159
        }
1160
1161 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...
1162
            if ($issue['resolution']) {
1163
                if (in_array('resolution', $change_fields) || !isset($snap_data['resolution'])) {
1164
                    $resolution = Resolution::Where('key', $issue['resolution'])->orWhere('_id', $issue['resolution'])->first();
1165
                    $snap_data['resolution'] = [ 'value' => isset($resolution->name) ? $resolution->name : '', 'name' => '解决结果' ];
1166
                }
1167
            }
1168
            else
1169
            {
1170
                $snap_data['resolution'] = [ 'value' => '', 'name' => '解决结果' ];
1171
            }
1172
        }
1173
1174 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...
1175
            if ($issue['assignee']) {
1176
                if (in_array('assignee', $change_fields) || !isset($snap_data['assignee'])) {
1177
                    $snap_data['assignee'] = [ 'value' => $issue['assignee']['name'], 'name' => '经办人' ];
1178
                }
1179
            }
1180
            else
1181
            {
1182
                $snap_data['assignee'] = [ 'value' => '', 'name' => '经办人' ];
1183
            }
1184
        }
1185
        // labels
1186
        if (isset($issue['labels'])) {
1187
            if ($issue['labels']) {
1188
                if (in_array('labels', $change_fields) || !isset($snap_data['labels'])) {
1189
                    $snap_data['labels'] = [ 'value' => implode(',', $issue['labels']), 'name' => '标签' ];
1190
                }
1191
            }
1192
            else
1193
            {
1194
                $snap_data['labels'] = [ 'value' => '', 'name' => '标签' ];
1195
            }
1196
        }
1197
1198
        // special fields handle
1199
        if (isset($issue['parent_id'])) {
1200
            if ($issue['parent_id']) {
1201
                if (in_array('parent_id', $change_fields) || !isset($snap_data['parent_id'])) {
1202
                    $parent = DB::collection('issue_' . $project_key)->where('_id', $issue['parent_id'])->first(['no', 'title']);
1203
                    $snap_data['parent'] = [ 'value' => $parent['no'] . ' - ' . $parent['title'], 'name' => '父任务' ];
1204
                }
1205
            }
1206
            else
1207
            {
1208
                $snap_data['parent'] = [ 'value' => '', 'name' => '父任务' ];
1209
            }
1210
        }
1211
1212 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...
1213
            if ($issue['progress'] || $issue['progress'] === 0) {
1214
                if (in_array('progress', $change_fields) || !isset($snap_data['progress'])) {
1215
                    $snap_data['progress'] = [ 'value' => $issue['progress'] . '%', 'name' => '进度' ];
1216
                }
1217
            }
1218
            else
1219
            {
1220
                $snap_data['progress'] = [ 'value' => '', 'name' => '进度' ];
1221
            }
1222
        }
1223
1224
        if (isset($issue['expect_start_time'])) {
1225
            if ($issue['expect_start_time']) {
1226
                if (in_array('expect_start_time', $change_fields) || !isset($snap_data['expect_start_time'])) {
1227
                    $snap_data['expect_start_time'] = [ 'value' => date('Y/m/d', $issue['expect_start_time']), 'name' => '期望开始时间' ];
1228
                }
1229
            }
1230
            else
1231
            {
1232
                $snap_data['expect_start_time'] = [ 'value' => '', 'name' => '期望开始时间' ];
1233
            }
1234
        }
1235
1236
        if (isset($issue['expect_complete_time'])) {
1237
            if ($issue['expect_complete_time']) {
1238
                if (in_array('expect_complete_time', $change_fields) || !isset($snap_data['expect_complete_time'])) {
1239
                    $snap_data['expect_complete_time'] = [ 'value' => date('Y/m/d', $issue['expect_complete_time']), 'name' => '期望完成时间' ];
1240
                }
1241
            }
1242
            else
1243
            {
1244
                $snap_data['expect_complete_time'] = [ 'value' => '', 'name' => '期望完成时间' ];
1245
            }
1246
        }
1247
1248
        if (!isset($snap_data['created_at'])) {
1249
            $snap_data['created_at'] = $issue['created_at'];
1250
        }
1251
1252
        if (!isset($snap_data['reporter'])) {
1253
            $snap_data['reporter'] = $issue['reporter'];
1254
        }
1255
1256
        $operated_at = isset($issue['updated_at']) ? $issue['updated_at'] : $issue['created_at'];
1257
        $operator = isset($issue['modifier']) ? $issue['modifier'] : $issue['reporter'];
1258
1259
        $snap_id = DB::collection('issue_his_' . $project_key)->insertGetId([ 'issue_id' => $issue['_id']->__toString(), 'operated_at' => $operated_at, 'operator' => $operator, 'data' => $snap_data ]);
1260
1261
        return $snap_id->__toString();
1262
    }
1263
1264
    /**
1265
     * check if issue exist.
1266
     *
1267
     * @param  string $project_key
1268
     * @param  string $issue_id
1269
     * @return bool
1270
     */
1271
    public static function isIssueExisted($project_key, $issue_id)
1272
    {
1273
        $isExisted = DB::collection('issue_' . $project_key)->where('_id', $issue_id)->exists();
1274
        return $isExisted;
1275
    }
1276
1277
    /**
1278
     * get all subtasks of the parent  
1279
     *
1280
     * @param  string $project_key
1281
     * @param  string $parent_no
1282
     * @return bool
1283
     */
1284
    public static function getChildrenByParentNo($project_key, $parent_no)
1285
    {
1286
        $parent = DB::collection('issue_' . $project_key)->where('no', $parent_no)->first();
1287
        if (!$parent) { return []; 
1288
        }
1289
1290
        $children = [];
1291
        $subtasks = DB::collection('issue_' . $project_key)->where('parent_id', $parent['_id']->__toString())->get(['no']);
1292
        foreach ($subtasks as $subtask)
1293
        {
1294
            $children[] = $subtask['no'];
1295
        }
1296
        return $children;
1297
    }
1298
1299
    /**
1300
     * get epic list.
1301
     *
1302
     * @param  string $project_key
1303
     * @param  array  $fields
1304
     * @return collection
1305
     */
1306
    public static function getEpicList($project_key, $fields=[])
1307
    {
1308
        $epics = Epic::Where('project_key', $project_key)
1309
            ->orderBy('sn', 'asc')
1310
            ->get($fields)
1311
            ->toArray();
1312
1313
        return $epics;
1314
    }
1315
1316
    /**
1317
     * check if Epic has existed.
1318
     *
1319
     * @param  string $project_key
1320
     * @param  string $name
1321
     * @return bool
1322
     */
1323
    public static function isEpicExisted($project_key, $name)
1324
    {
1325
        $isExisted = Epic::Where('project_key', $project_key)
1326
            ->Where('name', $name)
1327
            ->exists();
1328
1329
        return $isExisted;
1330
    }
1331
1332
    /**
1333
     * get sprint list.
1334
     *
1335
     * @param  string $project_key
1336
     * @return collection
1337
     */
1338
    public static function getSprintList($project_key, $fields=[])
1339
    {
1340
        $sprints = Sprint::Where('project_key', $project_key)
1341
            ->WhereIn('status', [ 'active', 'completed' ])
1342
            ->orderBy('no', 'desc')
1343
            ->get($fields)
1344
            ->toArray();
1345
1346
        foreach($sprints as $key => $sprint)
1347
        {
1348
            if (!isset($sprint['name'])) {
1349
                $sprints[$key]['name'] = 'Sprint ' . $sprint['no'];
1350
            }
1351
        }
1352
1353
        return $sprints;
1354
    }
1355
1356
    /**
1357
     * check if Label has existed.
1358
     *
1359
     * @param  string $project_key
1360
     * @param  string $name
1361
     * @return bool
1362
     */
1363
    public static function isLabelExisted($project_key, $name)
1364
    {
1365
        $isExisted = Labels::Where('project_key', $project_key)
1366
            ->Where('name', $name)
1367
            ->exists();
1368
1369
        return $isExisted;
1370
    }
1371
}
1372
1373