1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Traits\Project; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent; |
15
|
|
|
use Tinyissue\Model\Project; |
16
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SortTrait is trait class containing the methods for sorting database queries of the Project model. |
20
|
|
|
* |
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @property static $this |
24
|
|
|
*/ |
25
|
|
|
trait SortTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Sort by updated_at column. |
29
|
|
|
* |
30
|
|
|
* @param HasMany $query |
31
|
|
|
* @param string $order |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function sortByUpdated(HasMany $query, $order = 'asc') |
36
|
|
|
{ |
37
|
|
|
$query->orderBy('updated_at', $order); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sort by issues tag group |
42
|
|
|
* Note: this sort will return the collection. |
43
|
|
|
* |
44
|
|
|
* @param HasMany $query |
45
|
|
|
* @param string $tagGroup |
46
|
|
|
* @param string $order |
47
|
|
|
* |
48
|
|
|
* @return Eloquent\Collection |
49
|
|
|
*/ |
50
|
|
|
public function sortByTag(HasMany $query, $tagGroup, $order = 'asc') |
51
|
|
|
{ |
52
|
|
|
// If tag group is string prefixed with tag: |
53
|
|
|
if (!is_numeric($tagGroup)) { |
54
|
|
|
$tagGroup = substr($tagGroup, strlen('tag:')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$results = $query->get() |
58
|
|
|
->sort(function (Project\Issue $issue1, Project\Issue $issue2) use ($tagGroup, $order) { |
59
|
|
|
$tag1 = $issue1->tags->where('parent.id', $tagGroup, false)->first(); |
60
|
|
|
$tag2 = $issue2->tags->where('parent.id', $tagGroup, false)->first(); |
61
|
|
|
$tag1 = $tag1 ? $tag1->name : ''; |
62
|
|
|
$tag2 = $tag2 ? $tag2->name : ''; |
63
|
|
|
|
64
|
|
|
if ($order === 'asc') { |
65
|
|
|
return strcmp($tag1, $tag2); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return strcmp($tag2, $tag1); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
return $results; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|