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
|
111 |
|
public function all() { |
22
|
111 |
|
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
|
183 |
|
public function add( $field ) { |
35
|
183 |
|
if ( ! $field instanceof Field ) { |
36
|
1 |
|
gravityview()->log->error( 'Field_Collections can only contain objects of type \GV\Field.' ); |
37
|
1 |
|
return; |
38
|
|
|
} |
39
|
183 |
|
parent::add( $field ); |
40
|
183 |
|
} |
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
|
2 |
|
public function get( $field_uid ) { |
53
|
2 |
|
foreach ( $this->all() as $field ) { |
54
|
2 |
|
if ( $field->UID == $field_uid ) { |
55
|
2 |
|
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
|
75 |
|
public function by_type( $type ) { |
72
|
75 |
|
$fields = new self(); |
73
|
|
|
|
74
|
75 |
|
$search = implode( '.*', array_map( 'preg_quote', explode( '*', $type ) ) ); |
75
|
|
|
|
76
|
75 |
|
foreach ( $this->all() as $field ) { |
77
|
75 |
|
if ( preg_match( "#^{$search}$#", $field->type ) ) { |
|
|
|
|
78
|
9 |
|
$fields->add( $field ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
75 |
|
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
|
49 |
|
public function by_position( $position ) { |
96
|
49 |
|
$fields = new self(); |
97
|
|
|
|
98
|
49 |
|
$search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) ); |
99
|
|
|
|
100
|
49 |
|
foreach ( $this->all() as $field ) { |
101
|
49 |
|
if ( preg_match( "#^{$search}$#", $field->position ) ) { |
102
|
47 |
|
$fields->add( $field ); |
103
|
|
|
} |
104
|
|
|
} |
105
|
49 |
|
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
|
75 |
|
public function by_visible( $view = null ) { |
119
|
75 |
|
$fields = new self(); |
120
|
|
|
|
121
|
|
|
/** @var \GV\Field $field */ |
122
|
75 |
|
foreach ( $this->all() as $field ) { |
123
|
72 |
|
if ( $field->is_visible( $view ) ) { |
124
|
72 |
|
$fields->add( $field ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
75 |
|
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
|
184 |
|
public static function from_configuration( $configuration ) { |
156
|
184 |
|
$fields = new self(); |
157
|
184 |
|
foreach ( $configuration as $position => $_fields ) { |
158
|
|
|
|
159
|
183 |
|
if ( empty( $_fields ) || ! is_array( $_fields ) ) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
|
163
|
183 |
|
foreach ( $_fields as $uid => $_configuration ) { |
164
|
183 |
|
$field = Field::from_configuration( $_configuration ); |
165
|
183 |
|
$field->UID = $uid; |
166
|
183 |
|
$field->position = $position; |
167
|
|
|
|
168
|
183 |
|
$fields->add( $field ); |
169
|
|
|
} |
170
|
|
|
} |
171
|
184 |
|
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
|
55 |
|
public function as_configuration() { |
180
|
55 |
|
$configuration = array(); |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @var \GV\Field $field |
184
|
|
|
*/ |
185
|
55 |
|
foreach ( $this->all() as $field ) { |
186
|
54 |
|
if ( empty( $configuration[ $field->position ] ) ) { |
187
|
54 |
|
$configuration[ $field->position ] = array(); |
188
|
|
|
} |
189
|
|
|
|
190
|
54 |
|
$configuration[ $field->position ][ $field->UID ] = $field->as_configuration(); |
191
|
|
|
} |
192
|
55 |
|
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@property
annotation 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.