/
Manager.php
124 lines (111 loc) · 2.93 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Concrete\Core\Search\Field;
use Concrete\Core\Attribute\SetManagerInterface;
class Manager implements ManagerInterface
{
/**
* The groups of fields.
*
* @var GroupInterface[]
*/
protected $groups = [];
/**
* {@inheritdoc}
*
* @see ManagerInterface::getGroups()
*/
public function getGroups()
{
return $this->groups;
}
/**
* Add a group of fields.
*
* @param string $name the group name
* @param FieldInterface[] $fields
*/
public function addGroup($name, $fields = [])
{
$group = new Group();
$group->setName($name);
$group->setFields($fields);
$this->addGroupObject($group);
}
/**
* {@inheritdoc}
*
* @see ManagerInterface::getGroupByName()
*/
public function getGroupByName($name)
{
foreach ($this->groups as $group) {
if ($group->getName() == $name) {
return $group;
}
}
}
/**
* Add a field group.
*
* @param GroupInterface $group
*/
public function addGroupObject(GroupInterface $group)
{
$this->groups[] = $group;
}
/**
* {@inheritdoc}
*
* @see ManagerInterface::getFieldByKey()
*/
public function getFieldByKey($key)
{
foreach ($this->groups as $group) {
foreach ($group->getFields() as $field) {
if ($field->getKey() == $key) {
return $field;
}
}
}
}
/**
* {@inheritdoc}
*
* @see ManagerInterface::getFieldsFromRequest()
*/
public function getFieldsFromRequest(array $request)
{
$keys = isset($request['field']) ? $request['field'] : null;
$fields = [];
if (is_array($keys)) {
foreach ($this->groups as $group) {
foreach ($group->getFields() as $field) {
if (in_array($field->getKey(), $keys)) {
$field->loadDataFromRequest($request);
$fields[] = $field;
}
}
}
}
return $fields;
}
protected function populateAttributeGroups(SetManagerInterface $setManager)
{
$attributeSets = $setManager->getAttributeSets();
$unassigned = $setManager->getUnassignedAttributeKeys();
$attributes = [];
foreach($attributeSets as $set) {
foreach($set->getAttributeKeys() as $key) {
$field = new AttributeKeyField($key);
$attributes[] = $field;
}
$this->addGroup($set->getAttributeSetDisplayName(), $attributes);
}
$attributes = [];
foreach($unassigned as $key) {
$field = new AttributeKeyField($key);
$attributes[] = $field;
}
$this->addGroup(t('Other Attributes'), $attributes);
}
}