|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Rafidion Michael |
|
5
|
|
|
* Date: 10/12/2014 |
|
6
|
|
|
* Time: 12:33 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Mykees\TagBundle\Traits; |
|
10
|
|
|
|
|
11
|
|
|
use Mykees\TagBundle\Util\Urlizer; |
|
12
|
|
|
use Mykees\TagBundle\Entity\Tag; |
|
13
|
|
|
use Mykees\TagBundle\Interfaces\Taggable; |
|
14
|
|
|
use Doctrine\ORM\Query\Expr; |
|
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
17
|
|
|
|
|
18
|
|
|
trait ManagerObjectTrait { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Insert one Tag in model referer |
|
22
|
|
|
* @param Tag $tag |
|
23
|
|
|
* @param Taggable $model |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function addTag(Tag $tag, Taggable $model) |
|
26
|
|
|
{ |
|
27
|
|
|
$model->getTags()->add($tag); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Insert several Tags in model referer |
|
32
|
|
|
* @param array $tags |
|
33
|
|
|
* @param Taggable $model |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function addTags(array $tags, Taggable $model) |
|
36
|
|
|
{ |
|
37
|
|
|
foreach($tags as $tag) |
|
38
|
|
|
{ |
|
39
|
|
|
if(!empty($tag) && $tag instanceof Tag) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->addTag($tag,$model); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Remove and insert tags in model referer |
|
48
|
|
|
* @param array $tags |
|
49
|
|
|
* @param Taggable $model |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function refreshTags(array $tags, Taggable $model) |
|
52
|
|
|
{ |
|
53
|
|
|
$model->getTags()->clear(); |
|
54
|
|
|
$this->addTags($tags,$model); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get Tags and Relations |
|
59
|
|
|
* @param Taggable $model |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getTagRelation(Taggable $model) |
|
62
|
|
|
{ |
|
63
|
|
|
$query = $this->queryRelation($model); |
|
64
|
|
|
|
|
65
|
|
|
return $this->refreshTags($query,$model); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function queryTag() |
|
69
|
|
|
{ |
|
70
|
|
|
$query = $this->em->createQueryBuilder() |
|
|
|
|
|
|
71
|
|
|
->select('t') |
|
72
|
|
|
->from($this->tag,'t') |
|
|
|
|
|
|
73
|
|
|
; |
|
74
|
|
|
|
|
75
|
|
|
return $query; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function slugify(array $datas) |
|
79
|
|
|
{ |
|
80
|
|
|
$datas = array_unique($datas); |
|
81
|
|
|
foreach($datas as $k=>$d) |
|
82
|
|
|
{ |
|
83
|
|
|
$datas[$k] = Urlizer::urlize($d); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $datas; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
protected function addNameConstraint(QueryBuilder $query,$names) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if($names) |
|
92
|
|
|
{ |
|
93
|
|
|
if(is_array($names)) |
|
94
|
|
|
{ |
|
95
|
|
|
$names = $this->slugify($names); |
|
96
|
|
|
$query->where($query->expr()->in('t.slug', $names)); |
|
97
|
|
|
}else{ |
|
98
|
|
|
$query->andWhere('t.slug = :slug') |
|
99
|
|
|
->setParameter('slug',Urlizer::urlize($names)) |
|
100
|
|
|
; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return $query; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
protected function addModelTypeConstraint(QueryBuilder $query,$modelType) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
if($modelType) |
|
109
|
|
|
{ |
|
110
|
|
|
if(is_array($modelType)) |
|
111
|
|
|
{ |
|
112
|
|
|
$modelType = $this->slugify($modelType); |
|
113
|
|
|
$query->andWhere($query->expr()->in('tr.model', $modelType)); |
|
114
|
|
|
}else{ |
|
115
|
|
|
$query->andWhere('tr.model = :model') |
|
116
|
|
|
->setParameter('model',$modelType) |
|
117
|
|
|
; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
return $query; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function queryRelation(Taggable $model) |
|
124
|
|
|
{ |
|
125
|
|
|
$query = $this->em->createQueryBuilder() |
|
126
|
|
|
->select('t') |
|
127
|
|
|
->from($this->tag,'t') |
|
128
|
|
|
->innerJoin('t.tagRelation','tg',Expr\Join::WITH, 'tg.model = :model AND tg.modelId = :modelId') |
|
129
|
|
|
->addSelect('tg') |
|
130
|
|
|
->setParameter('model',$model->getModel()) |
|
131
|
|
|
->setParameter('modelId',$model->getModelId()) |
|
132
|
|
|
->getQuery() |
|
133
|
|
|
->getResult() |
|
134
|
|
|
; |
|
135
|
|
|
|
|
136
|
|
|
return $query; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function findRelationByTagName(ArrayCollection $names, Taggable $model) |
|
140
|
|
|
{ |
|
141
|
|
|
$slugified = []; |
|
142
|
|
|
foreach($names as $name) |
|
143
|
|
|
{ |
|
144
|
|
|
$slugified[] = $name->getSlug(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$query = $this->em->createQueryBuilder() |
|
148
|
|
|
->select('t') |
|
149
|
|
|
->from($this->tag,'t') |
|
150
|
|
|
->innerJoin('t.tagRelation','tg',Expr\Join::WITH, 'tg.model = :model AND tg.modelId = :modelId') |
|
151
|
|
|
->addSelect('tg') |
|
152
|
|
|
->where($this->em->createQueryBuilder()->expr()->in('t.slug', $slugified)) |
|
153
|
|
|
->setParameter('model',$model->getModel()) |
|
154
|
|
|
->setParameter('modelId',$model->getModelId()) |
|
155
|
|
|
->getQuery() |
|
156
|
|
|
->getResult() |
|
157
|
|
|
; |
|
158
|
|
|
|
|
159
|
|
|
return $query; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: