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\Tag; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent; |
15
|
|
|
use Tinyissue\Model\Project; |
16
|
|
|
use Tinyissue\Model\Tag; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* CrudTrait is trait class containing the methods for adding/editing/deleting the Tag model. |
20
|
|
|
* |
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @method Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and') |
24
|
|
|
*/ |
25
|
|
|
trait CrudTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Create a new tag. |
29
|
|
|
* |
30
|
|
|
* @param array $input |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
1 |
|
*/ |
34
|
|
|
public function createTag(array $input) |
35
|
1 |
|
{ |
36
|
|
|
return $this->fill($this->prepareTagToSave($input))->save(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Update the tags in the database. |
41
|
|
|
* |
42
|
|
|
* @param array $attributes |
43
|
|
|
* |
44
|
|
|
* @return bool|int |
45
|
|
|
*/ |
46
|
|
|
public function update(array $attributes = []) |
47
|
|
|
{ |
48
|
|
|
return parent::update($this->prepareTagToSave($attributes)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new tag if valid or return existing one. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @param null|Tag $parent |
56
|
|
|
* |
57
|
|
|
* @return bool|$this |
58
|
|
|
*/ |
59
|
|
|
public function validOrCreate($name, Tag $parent = null) |
60
|
|
|
{ |
61
|
|
|
$group = $parent === null ? true : false; |
62
|
|
|
$tag = $this->where('name', '=', $name)->first(); |
63
|
|
|
if ($tag && $tag->group != $group) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$tag) { |
68
|
|
|
$tag = new Tag(); |
69
|
|
|
$tag->name = $name; |
70
|
|
|
$tag->group = $group; |
71
|
|
|
if (!is_null($parent)) { |
72
|
|
|
$tag->parent_id = $parent->id; |
73
|
|
|
$tag->setRelation('parent', $parent); |
74
|
|
|
} |
75
|
|
|
$tag->save(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $tag; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete tag. |
83
|
|
|
* |
84
|
|
|
* @return bool|null |
85
|
|
|
* |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
public function delete() |
89
|
|
|
{ |
90
|
|
|
// Remove kanban tags |
91
|
|
|
\DB::table('projects_kanban_tags')->where('tag_id', '=', $this->id)->delete(); |
|
|
|
|
92
|
|
|
|
93
|
|
|
// Remove relation to issues |
94
|
|
|
\DB::table('projects_issues_tags')->where('tag_id', '=', $this->id)->delete(); |
95
|
|
|
|
96
|
|
|
return parent::delete(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Prepare tag details to save. |
101
|
|
|
* |
102
|
|
|
* @param array $input |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
protected function prepareTagToSave(array $input) |
107
|
|
|
{ |
108
|
|
|
$input['group'] = 0; |
109
|
1 |
|
|
110
|
|
|
return $input; |
111
|
1 |
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.