|
1
|
|
|
<?php |
|
2
|
|
|
namespace GV; |
|
3
|
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
|
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
6
|
|
|
die(); |
|
7
|
|
|
} |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A collection of \GV\Field objects. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Field_Collection extends Collection { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Returns all the objects in this collection as an an array. Here for docBlock purposes only. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 2.0.13.1 |
|
18
|
|
|
* |
|
19
|
|
|
* @return \GV\Field[] |
|
20
|
|
|
*/ |
|
21
|
106 |
|
public function all() { |
|
22
|
106 |
|
return parent::all(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Add a \GV\Field to this collection. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \GV\Field $field The field to add to the internal array. |
|
29
|
|
|
* |
|
30
|
|
|
* @api |
|
31
|
|
|
* @since 2.0 |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
178 |
|
public function add( $field ) { |
|
35
|
178 |
|
if ( ! $field instanceof Field ) { |
|
36
|
1 |
|
gravityview()->log->error( 'Field_Collections can only contain objects of type \GV\Field.' ); |
|
37
|
1 |
|
return; |
|
38
|
|
|
} |
|
39
|
178 |
|
parent::add( $field ); |
|
40
|
178 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get a \GV\Field from this list by UID. |
|
44
|
|
|
* |
|
45
|
|
|
* @param int $field_uid The UID of the field in the field to get. |
|
46
|
|
|
* |
|
47
|
|
|
* @api |
|
48
|
|
|
* @since 2.0 |
|
49
|
|
|
* |
|
50
|
|
|
* @return \GV\Field|null The \GV\Field with the $field_uid as the UID, or null if not found. |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function get( $field_uid ) { |
|
53
|
1 |
|
foreach ( $this->all() as $field ) { |
|
54
|
1 |
|
if ( $field->UID == $field_uid ) { |
|
55
|
1 |
|
return $field; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
1 |
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get a copy of this \GV\Field_Collection filtered by type. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $type The type of the field to get. |
|
65
|
|
|
* |
|
66
|
|
|
* @api |
|
67
|
|
|
* @since develop |
|
68
|
|
|
* |
|
69
|
|
|
* @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by type. |
|
70
|
|
|
*/ |
|
71
|
70 |
|
public function by_type( $type ) { |
|
72
|
70 |
|
$fields = new self(); |
|
73
|
|
|
|
|
74
|
70 |
|
$search = implode( '.*', array_map( 'preg_quote', explode( '*', $type ) ) ); |
|
75
|
|
|
|
|
76
|
70 |
|
foreach ( $this->all() as $field ) { |
|
77
|
70 |
|
if ( preg_match( "#^{$search}$#", $field->type ) ) { |
|
|
|
|
|
|
78
|
6 |
|
$fields->add( $field ); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
70 |
|
return $fields; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get a copy of this \GV\Field_Collection filtered by position. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $position The position to get the fields for. |
|
88
|
|
|
* Can be a wildcard * |
|
89
|
|
|
* |
|
90
|
|
|
* @api |
|
91
|
|
|
* @since |
|
92
|
|
|
* |
|
93
|
|
|
* @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by position. |
|
94
|
|
|
*/ |
|
95
|
47 |
|
public function by_position( $position ) { |
|
96
|
47 |
|
$fields = new self(); |
|
97
|
|
|
|
|
98
|
47 |
|
$search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) ); |
|
99
|
|
|
|
|
100
|
47 |
|
foreach ( $this->all() as $field ) { |
|
101
|
47 |
|
if ( preg_match( "#^{$search}$#", $field->position ) ) { |
|
102
|
45 |
|
$fields->add( $field ); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
47 |
|
return $fields; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get a copy of this \GV\Field_Collection filtered by visibility to current user context. |
|
110
|
|
|
* |
|
111
|
|
|
* @api |
|
112
|
|
|
* @since |
|
113
|
|
|
* |
|
114
|
|
|
* @param $view \GV\View The view! |
|
115
|
|
|
* |
|
116
|
|
|
* @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility. |
|
117
|
|
|
*/ |
|
118
|
73 |
|
public function by_visible( $view = null ) { |
|
119
|
73 |
|
$fields = new self(); |
|
120
|
|
|
|
|
121
|
|
|
/** @var \GV\Field $field */ |
|
122
|
73 |
|
foreach ( $this->all() as $field ) { |
|
123
|
70 |
|
if ( $field->is_visible( $view ) ) { |
|
124
|
70 |
|
$fields->add( $field ); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
73 |
|
return $fields; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Parse a configuration array into a Field_Collection. |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $configuration The configuration, structured like so: |
|
134
|
|
|
* |
|
135
|
|
|
* array( |
|
136
|
|
|
* |
|
137
|
|
|
* [other zones] |
|
138
|
|
|
* |
|
139
|
|
|
* 'directory_list-title' => array( |
|
140
|
|
|
* |
|
141
|
|
|
* [other fields] |
|
142
|
|
|
* |
|
143
|
|
|
* '5372653f25d44' => array( |
|
144
|
|
|
* @see \GV\Field::as_configuration() for structure |
|
145
|
|
|
* ) |
|
146
|
|
|
* |
|
147
|
|
|
* [other fields] |
|
148
|
|
|
* ) |
|
149
|
|
|
* |
|
150
|
|
|
* [other zones] |
|
151
|
|
|
* ) |
|
152
|
|
|
* |
|
153
|
|
|
* @return \GV\Field_Collection A collection of fields. |
|
154
|
|
|
*/ |
|
155
|
179 |
|
public static function from_configuration( $configuration ) { |
|
156
|
179 |
|
$fields = new self(); |
|
157
|
179 |
|
foreach ( $configuration as $position => $_fields ) { |
|
158
|
|
|
|
|
159
|
178 |
|
if ( empty( $_fields ) || ! is_array( $_fields ) ) { |
|
160
|
|
|
continue; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
178 |
|
foreach ( $_fields as $uid => $_configuration ) { |
|
164
|
178 |
|
$field = Field::from_configuration( $_configuration ); |
|
165
|
178 |
|
$field->UID = $uid; |
|
166
|
178 |
|
$field->position = $position; |
|
167
|
|
|
|
|
168
|
178 |
|
$fields->add( $field ); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
179 |
|
return $fields; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Return a configuration array for this field collection. |
|
176
|
|
|
* |
|
177
|
|
|
* @return array See \GV\Field_Collection::from_configuration() for structure. |
|
178
|
|
|
*/ |
|
179
|
53 |
|
public function as_configuration() { |
|
180
|
53 |
|
$configuration = array(); |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @var \GV\Field $field |
|
184
|
|
|
*/ |
|
185
|
53 |
|
foreach ( $this->all() as $field ) { |
|
186
|
52 |
|
if ( empty( $configuration[ $field->position ] ) ) { |
|
187
|
52 |
|
$configuration[ $field->position ] = array(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
52 |
|
$configuration[ $field->position ][ $field->UID ] = $field->as_configuration(); |
|
191
|
|
|
} |
|
192
|
53 |
|
return $configuration; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.