|
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
|
|
|
/** |
|
29
|
|
|
* Add condition to current query. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $fieldName Entity field name |
|
32
|
|
|
* @param string $fieldValue Value |
|
33
|
|
|
* @return self Chaining |
|
34
|
|
|
*/ |
|
35
|
|
|
public function where($fieldName, $fieldValue = null) |
|
36
|
|
|
{ |
|
37
|
|
|
// Try to find entity additional field |
|
38
|
|
|
if (property_exists(static::$identifier, $fieldName)) { |
|
39
|
|
|
// Store additional field filter value |
|
40
|
|
|
$this->fieldFilter[$fieldName] = $fieldValue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get collection of entity identifiers filtered by navigation identifiers. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
|
50
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function findByNavigationIDs($entityIDs) |
|
53
|
|
|
{ |
|
54
|
|
|
return (new MaterialNavigation($entityIDs))->idsByRelationID(static::$navigationIDs); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get collection of entity identifiers filtered by additional field and its value. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $additionalFields Collection of additional field identifiers => values |
|
61
|
|
|
* @param array $entityIDs Additional collection of entity identifiers for filtering |
|
62
|
|
|
* @return array Collection of material identifiers by navigation identifiers |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function findByAdditionalFields($additionalFields, $entityIDs = array()) |
|
65
|
|
|
{ |
|
66
|
|
|
// Iterate all additional fields needed for filter entity |
|
67
|
|
|
foreach ($additionalFields as $fieldID => $fieldValue) { |
|
68
|
|
|
// Get collection of entity identifiers passing already found identifiers |
|
69
|
|
|
$entityIDs = (new MaterialField($entityIDs))->idsByRelationID($fieldID, $fieldValue); |
|
70
|
|
|
|
|
71
|
|
|
// Stop execution if we have no entities found at this step |
|
72
|
|
|
if (!sizeof($entityIDs)) { |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $entityIDs; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Perform SamsonCMS query and get entities collection. |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed[] Collection of found entities |
|
84
|
|
|
*/ |
|
85
|
|
|
public function find() |
|
86
|
|
|
{ |
|
87
|
|
|
// TODO: Find and describe approach with maximum generic performance |
|
88
|
|
|
$entityIDs = $this->findByNavigationIDs(); |
|
|
|
|
|
|
89
|
|
|
$entityIDs = $this->findByAdditionalFields($this->fieldFilter, $entityIDs); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
return (new Material(static::$identifier))->byIDs($entityIDs, 'exec'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks for function calls that miss required arguments.