|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: VITALYIEGOROV |
|
5
|
|
|
* Date: 08.12.15 |
|
6
|
|
|
* Time: 23:11 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsoncms\api\query; |
|
9
|
|
|
|
|
10
|
|
|
use samson\activerecord\dbQuery; |
|
11
|
|
|
use samsonframework\orm\Query; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Material with additional fields query. |
|
15
|
|
|
* @package samsoncms\api |
|
16
|
|
|
*/ |
|
17
|
|
|
class Generic |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string Entity identifier */ |
|
20
|
|
|
protected static $identifier; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string Entity navigation identifiers */ |
|
23
|
|
|
protected static $navigationIDs; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array Collection of entity field filter */ |
|
26
|
|
|
protected $fieldFilter; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array Collection of matching entity identifiers */ |
|
29
|
|
|
protected $entityIDs; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Add condition to current query. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $fieldName Entity field name |
|
35
|
|
|
* @param string $fieldValue Value |
|
36
|
|
|
* @param string $relation Relation between field name and its value |
|
37
|
|
|
* @return self Chaining |
|
38
|
|
|
*/ |
|
39
|
|
|
public function where($fieldName, $fieldValue = null, $relation = '=') |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
// Try to find entity additional field |
|
42
|
|
|
if (property_exists(static::$identifier, $fieldName)) { |
|
43
|
|
|
// Store additional field filter value |
|
44
|
|
|
$this->fieldFilter[$fieldName] = $fieldValue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get collection of entity identifiers filtered by navigation identifiers. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
|
54
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function findByNavigationIDs($entityIDs) |
|
57
|
|
|
{ |
|
58
|
|
|
return (new MaterialNavigation($entityIDs))->idsByRelationID(static::$navigationIDs); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get collection of entity identifiers filtered by additional field and its value. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $additionalFields Collection of additional field identifiers => values |
|
65
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
|
66
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
|
69
|
|
|
{ |
|
70
|
|
|
// Iterate all additional fields needed for filter entity |
|
71
|
|
|
foreach ($additionalFields as $fieldID => $fieldValue) { |
|
72
|
|
|
// Get collection of entity identifiers passing already found identifiers |
|
73
|
|
|
$entityIDs = (new MaterialField($entityIDs))->idsByRelationID($fieldID, $fieldValue); |
|
74
|
|
|
|
|
75
|
|
|
// Stop execution if we have no entities found at this step |
|
76
|
|
|
if (!sizeof($entityIDs)) { |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $entityIDs; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** @return array Collection of material identifiers by navigation identifiers */ |
|
85
|
|
|
protected function findByAdditionalField() |
|
86
|
|
|
{ |
|
87
|
|
|
$return = (new MaterialField($idsByNavigation)) |
|
|
|
|
|
|
88
|
|
|
->byRelationID($this->fieldFilter[]); |
|
89
|
|
|
return (new MaterialNavigation())->idsByRelationID(static::$navigationIDs); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Perform SamsonCMS query and get entities collection. |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed[] Collection of found entities |
|
96
|
|
|
*/ |
|
97
|
|
|
public function find() |
|
98
|
|
|
{ |
|
99
|
|
|
$return = array(); |
|
100
|
|
|
/** @var array $idsByNavigation First step - filter by navigation */ |
|
101
|
|
|
if (sizeof($idsByNavigation = $this->findByNavigationIDs())) { |
|
|
|
|
|
|
102
|
|
|
// Second step filter by additional field value |
|
103
|
|
|
if (sizeof($this->fieldFilter)) { |
|
104
|
|
|
$return = (new MaterialField($idsByNavigation)) |
|
|
|
|
|
|
105
|
|
|
->byRelationID($this->fieldFilter[]); |
|
106
|
|
|
} else { // Just return entities filtered by navigation |
|
107
|
|
|
return (new Material($idsByNavigation, static::$identifier))->byIDs($idsByNavigation, 'exec'); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $return; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.