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 samsonframework\orm\QueryInterface; |
13
|
|
|
|
14
|
|
|
/*** |
15
|
|
|
* Gallery additional field for material. |
16
|
|
|
* This class enables getting all information about additional fields gallery for specific material. |
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 database record its material and field identifiers. |
29
|
|
|
* |
30
|
|
|
* @param QueryInterface $query Database query interface |
31
|
|
|
* @param integer $materialId material identifier |
32
|
|
|
* @param integer $fieldId field identifier |
33
|
|
|
*/ |
34
|
|
|
public function __construct(QueryInterface $query, $materialId, $fieldId) |
35
|
|
|
{ |
36
|
|
|
/** @var object $materialField additional field value database record*/ |
37
|
|
|
$materialField = null; |
38
|
|
|
|
39
|
|
|
//set query interface |
40
|
|
|
$this->query = $query; |
41
|
|
|
|
42
|
|
|
// Checking params by type |
43
|
|
|
if (is_int($materialId) && is_int($fieldId)) { |
44
|
|
|
//Find additional field value database record by its material and field identifiers. |
45
|
|
|
if (MaterialField::byFieldIDAndMaterialID($query, $materialId, $fieldId, $materialField)) { |
46
|
|
|
//Getting first record |
47
|
|
|
$materialField = array_shift($materialField); |
48
|
|
|
//Set materialFieldId |
49
|
|
|
$this->materialFieldId = $materialField->id; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check on empty gallery. If materialFieldId = 0 and quantity images not more 1 then material not has images. |
56
|
|
|
* |
57
|
|
|
* @return boolean |
58
|
|
|
**/ |
59
|
|
|
public function hasImages() |
60
|
|
|
{ |
61
|
|
View Code Duplication |
if (isset($this->materialFieldId)) { |
|
|
|
|
62
|
|
|
// Getting quantity images, if quantity more 0 then material has images |
63
|
|
|
if ($this->query |
|
|
|
|
64
|
|
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
65
|
|
|
->cond(Field::F_DELETION, 1) |
66
|
|
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId) |
67
|
|
|
->count() > 0) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get collection of images for material by gallery additional field selector. If none is passed |
76
|
|
|
* all images from gallery table would be returned empty array. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getImages() |
81
|
|
|
{ |
82
|
|
|
/** @var $images[] Get material images for this gallery */ |
83
|
|
|
$images = array(); |
84
|
|
|
|
85
|
|
View Code Duplication |
if ($this->hasImages()) { |
|
|
|
|
86
|
|
|
//Get All images for materialFieldId |
87
|
|
|
$images = $this->query |
|
|
|
|
88
|
|
|
->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY) |
89
|
|
|
->cond(Field::F_DELETION, 1) |
90
|
|
|
->where(MaterialField::F_PRIMARY, $this->materialFieldId) |
91
|
|
|
->exec(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $images; |
95
|
|
|
} |
96
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.