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 samson\cms\CMSGallery; |
11
|
|
|
use samsoncms\api\MaterialField; |
12
|
|
|
use samsoncms\api\Material; |
13
|
|
|
use samsonframework\orm\QueryInterface; |
14
|
|
|
|
15
|
|
|
/*** |
16
|
|
|
* Gallery additional field for material. |
17
|
|
|
* This class enables getting all information about additional fields gallery for specific material. |
18
|
|
|
* @author [email protected] |
19
|
|
|
*/ |
20
|
|
|
class Gallery |
21
|
|
|
{ |
22
|
|
|
/** @var integer materialFieldId Table materialField identifier */ |
23
|
|
|
protected $materialFieldId = null; |
24
|
|
|
|
25
|
|
|
/** @var QueryInterface Database query interface */ |
26
|
|
|
protected $query; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor Gallery. |
30
|
|
|
* This constructor finds identifier additional field gallery from database record its material and field identifiers. |
31
|
|
|
* |
32
|
|
|
* @param QueryInterface $query Database query interface |
33
|
|
|
* @param integer $materialId material identifier |
34
|
|
|
* @param integer $fieldId field identifier |
35
|
|
|
*/ |
36
|
|
|
public function __construct(QueryInterface $query, $materialId, $fieldId) |
37
|
|
|
{ |
38
|
|
|
/** @var object $materialField additional field value database record*/ |
39
|
|
|
$materialField = null; |
|
|
|
|
40
|
|
|
|
41
|
|
|
//set query interface |
42
|
|
|
$this->query = $query; |
43
|
|
|
|
44
|
|
|
//Find additional field value database record by its material and field identifiers. |
45
|
|
|
$materialField = $this->query->entity(MaterialField::ENTITY) |
46
|
|
|
->where(Material::F_PRIMARY, $materialId) |
47
|
|
|
->where(Field::F_PRIMARY, $fieldId) |
48
|
|
|
->where(Material::F_DELETION, 1) |
49
|
|
|
->first(); |
50
|
|
|
|
51
|
|
|
if ($materialField) { |
52
|
|
|
//Set materialFieldId |
53
|
|
|
$this->materialFieldId = $materialField->id; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images. |
60
|
|
|
* |
61
|
|
|
* @return boolean |
62
|
|
|
**/ |
63
|
|
|
public function hasImages() |
64
|
|
|
{ |
65
|
|
|
/**@var $hasImages */ |
66
|
|
|
$hasImages = false; |
67
|
|
|
|
68
|
|
View Code Duplication |
if (isset($this->materialFieldId)) { |
|
|
|
|
69
|
|
|
// Getting quantity images, if quantity more 0 then material has images |
70
|
|
|
if ($this->query |
|
|
|
|
71
|
|
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
72
|
|
|
->where(Field::F_DELETION, 1) |
73
|
|
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId) |
74
|
|
|
->count() > 0) { |
75
|
|
|
$hasImages = true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $hasImages; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Getting quantity images in additional field gallery |
84
|
|
|
* |
85
|
|
|
* @return integer $count |
86
|
|
|
*/ |
87
|
|
|
public function getCount() |
88
|
|
|
{ |
89
|
|
|
/**@var integer $count quantity images in additional field gallery*/ |
90
|
|
|
$count = 0; |
91
|
|
|
|
92
|
|
View Code Duplication |
if ($this->hasImages()) { |
|
|
|
|
93
|
|
|
// Getting quantity images for gallery |
94
|
|
|
$count = $this->query |
|
|
|
|
95
|
|
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
96
|
|
|
->where(Field::F_DELETION, 1) |
97
|
|
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId) |
98
|
|
|
->count(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $count; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get collection of images for material by gallery additional field selector. If none is passed |
106
|
|
|
* all images from gallery table would be returned empty array. |
107
|
|
|
* |
108
|
|
|
* @param integer $currentPage current page with images. Min value = 1 |
109
|
|
|
* @param integer $countView quantity view by page |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getImages($currentPage = null, $countView = 20) |
113
|
|
|
{ |
114
|
|
|
/** @var $images[] Get material images for this gallery */ |
115
|
|
|
$images = array(); |
116
|
|
|
|
117
|
|
|
/** @var QueryInterface $query Database query interface*/ |
118
|
|
|
$query = null; |
|
|
|
|
119
|
|
|
|
120
|
|
|
if ($this->hasImages()) { |
121
|
|
|
// Select all images in DB by materialFieldId |
122
|
|
|
$query = $this->query |
123
|
|
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
124
|
|
|
->where(Field::F_DELETION, 1) |
125
|
|
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId); |
126
|
|
|
|
127
|
|
|
if (isset($currentPage) && $currentPage > 0) { |
128
|
|
|
//Set limit for query |
129
|
|
|
$query->limit(--$currentPage * $countView, $countView); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Execute query |
133
|
|
|
$images = $query->exec(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $images; |
137
|
|
|
} |
138
|
|
|
} |
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.