1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: myslyvyi |
5
|
|
|
* Date: 05.01.2016 |
6
|
|
|
* Time: 15:25 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\api; |
9
|
|
|
|
10
|
|
|
use samsonframework\orm\QueryInterface; |
11
|
|
|
|
12
|
|
|
/*** |
13
|
|
|
* Gallery additional field for material. |
14
|
|
|
* This class enables getting all information about additional fields gallery for specific material. |
15
|
|
|
* @author [email protected] |
16
|
|
|
* @deprecated Use \samsoncms\api\generator\GalleryCollection instead |
17
|
|
|
*/ |
18
|
|
|
class Gallery |
19
|
|
|
{ |
20
|
|
|
/** @var integer materialFieldId Table materialField identifier */ |
21
|
|
|
protected $materialFieldId = null; |
22
|
|
|
|
23
|
|
|
/** @var QueryInterface Database query interface */ |
24
|
|
|
protected $query; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor Gallery. |
28
|
|
|
* This constructor finds identifier additional field gallery from |
29
|
|
|
* database record its material and field identifiers. |
30
|
|
|
* |
31
|
|
|
* @param QueryInterface $query Database query interface |
32
|
|
|
* @param integer $materialId material identifier |
33
|
|
|
* @param integer $fieldId field identifier |
34
|
|
|
*/ |
35
|
3 |
|
public function __construct(QueryInterface $query, $materialId, $fieldId) |
36
|
|
|
{ |
37
|
|
|
/** @var object $materialField additional field value database record*/ |
38
|
3 |
|
$materialField = null; |
|
|
|
|
39
|
|
|
|
40
|
|
|
//set query interface |
41
|
3 |
|
$this->query = $query; |
42
|
|
|
|
43
|
|
|
//Find additional field value database record by its material and field identifiers. |
44
|
3 |
|
$materialField = $this->query->entity(MaterialField::ENTITY) |
45
|
3 |
|
->where(Material::F_PRIMARY, $materialId) |
46
|
3 |
|
->where(Field::F_PRIMARY, $fieldId) |
47
|
3 |
|
->where(Material::F_DELETION, 1) |
48
|
3 |
|
->first(); |
49
|
|
|
|
50
|
3 |
|
if ($materialField) { |
51
|
|
|
//Set materialFieldId |
52
|
3 |
|
$this->materialFieldId = $materialField->id; |
53
|
3 |
|
} |
54
|
|
|
|
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Getting quantity images in additional field gallery |
59
|
|
|
* |
60
|
|
|
* @return integer $count |
61
|
|
|
*/ |
62
|
1 |
|
public function getCount() |
63
|
|
|
{ |
64
|
|
|
/**@var integer $count quantity images in additional field gallery */ |
65
|
1 |
|
$count = 0; |
66
|
|
|
|
67
|
1 |
View Code Duplication |
if ($this->hasImages()) { |
|
|
|
|
68
|
|
|
// Getting quantity images for gallery |
69
|
1 |
|
$count = $this->query |
70
|
1 |
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
71
|
1 |
|
->where(Field::F_DELETION, 1) |
72
|
1 |
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId) |
73
|
1 |
|
->count(); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
1 |
|
return $count; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images. |
81
|
|
|
* |
82
|
|
|
* @return boolean |
83
|
|
|
**/ |
84
|
3 |
|
public function hasImages() |
85
|
|
|
{ |
86
|
|
|
/**@var $hasImages */ |
87
|
3 |
|
$hasImages = false; |
88
|
|
|
|
89
|
3 |
View Code Duplication |
if (isset($this->materialFieldId)) { |
|
|
|
|
90
|
|
|
// Getting quantity images, if quantity more 0 then material has images |
91
|
3 |
|
if ($this->query |
92
|
3 |
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
93
|
3 |
|
->where(Field::F_DELETION, 1) |
94
|
3 |
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId) |
95
|
3 |
|
->count() > 0) { |
96
|
3 |
|
$hasImages = true; |
97
|
3 |
|
} |
98
|
3 |
|
} |
99
|
|
|
|
100
|
3 |
|
return $hasImages; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get collection of images for material by gallery additional field selector. If none is passed |
105
|
|
|
* all images from gallery table would be returned empty array. |
106
|
|
|
* |
107
|
|
|
* @param integer $currentPage current page with images. Min value = 1 |
108
|
|
|
* @param integer $countView quantity view by page |
109
|
|
|
* |
110
|
|
|
*@return array |
111
|
|
|
* @deprecated Use find() |
112
|
|
|
*/ |
113
|
2 |
|
public function getImages($currentPage = null, $countView = 20) |
114
|
|
|
{ |
115
|
2 |
|
return $this->find($currentPage, $countView); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Perform SamsonCMS query and get entity gallery images. |
120
|
|
|
* |
121
|
|
|
* @param int $page Page number |
122
|
|
|
* @param int $size Page size |
123
|
|
|
* |
124
|
|
|
* @return GalleryField[] Collection of entity gallery images |
125
|
|
|
*/ |
126
|
2 |
|
public function find($page = null, $size = null) |
127
|
|
|
{ |
128
|
|
|
/** @var GalleryField[] $images Get material images for this gallery */ |
129
|
2 |
|
$images = array(); |
130
|
|
|
|
131
|
|
|
/** @var QueryInterface $query Database query interface*/ |
132
|
2 |
|
$query = null; |
|
|
|
|
133
|
|
|
|
134
|
2 |
|
if ($this->hasImages()) { |
135
|
|
|
// Select all images in DB by materialFieldId |
136
|
2 |
|
$query = $this->query |
137
|
2 |
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
138
|
2 |
|
->where(Field::F_DELETION, 1) |
139
|
2 |
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId); |
140
|
|
|
|
141
|
|
|
// Add paging |
142
|
2 |
|
if (isset($page) && $page > 0) { |
143
|
|
|
//Set limit for query |
144
|
1 |
|
$query->limit(--$page * $size, $size); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
// Execute query |
148
|
2 |
|
$images = $query->exec(); |
149
|
2 |
|
} |
150
|
|
|
|
151
|
2 |
|
return $images; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.