GravityView  2.1.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-collection-field.php
Go to the documentation of this file.
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  */
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  public function all() {
22  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  public function add( $field ) {
35  if ( ! $field instanceof Field ) {
36  gravityview()->log->error( 'Field_Collections can only contain objects of type \GV\Field.' );
37  return;
38  }
39  parent::add( $field );
40  }
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  public function get( $field_uid ) {
53  foreach ( $this->all() as $field ) {
54  if ( $field->UID == $field_uid ) {
55  return $field;
56  }
57  }
58  return null;
59  }
60 
61  /**
62  * Get a copy of this \GV\Field_Collection filtered by position.
63  *
64  * @param string $position The position to get the fields for.
65  * Can be a wildcard *
66  *
67  * @api
68  * @since
69  *
70  * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by position.
71  */
72  public function by_position( $position ) {
73  $fields = new self();
74 
75  $search = implode( '.*', array_map( 'preg_quote', explode( '*', $position ) ) );
76 
77  foreach ( $this->all() as $field ) {
78  if ( preg_match( "#^{$search}$#", $field->position ) ) {
79  $fields->add( $field );
80  }
81  }
82  return $fields;
83  }
84 
85  /**
86  * Get a copy of this \GV\Field_Collection filtered by visibility to current user context.
87  *
88  * @api
89  * @since
90  *
91  * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility.
92  */
93  public function by_visible() {
94  $fields = new self();
95 
96  /** @var \GV\Field $field */
97  foreach ( $this->all() as $field ) {
98  if ( $field->is_visible() ) {
99  $fields->add( $field );
100  }
101  }
102  return $fields;
103  }
104 
105  /**
106  * Parse a configuration array into a Field_Collection.
107  *
108  * @param array $configuration The configuration, structured like so:
109  *
110  * array(
111  *
112  * [other zones]
113  *
114  * 'directory_list-title' => array(
115  *
116  * [other fields]
117  *
118  * '5372653f25d44' => array(
119  * @see \GV\Field::as_configuration() for structure
120  * )
121  *
122  * [other fields]
123  * )
124  *
125  * [other zones]
126  * )
127  *
128  * @return \GV\Field_Collection A collection of fields.
129  */
130  public static function from_configuration( $configuration ) {
131  $fields = new self();
132  foreach ( $configuration as $position => $_fields ) {
133 
134  if ( empty( $_fields ) || ! is_array( $_fields ) ) {
135  continue;
136  }
137 
138  foreach ( $_fields as $uid => $_configuration ) {
139  $field = Field::from_configuration( $_configuration );
140  $field->UID = $uid;
141  $field->position = $position;
142 
143  $fields->add( $field );
144  }
145  }
146  return $fields;
147  }
148 
149  /**
150  * Return a configuration array for this field collection.
151  *
152  * @return array See \GV\Field_Collection::from_configuration() for structure.
153  */
154  public function as_configuration() {
155  $configuration = array();
156 
157  /**
158  * @var \GV\Field $field
159  */
160  foreach ( $this->all() as $field ) {
161  if ( empty( $configuration[ $field->position ] ) ) {
162  $configuration[ $field->position ] = array();
163  }
164 
165  $configuration[ $field->position ][ $field->UID ] = $field->as_configuration();
166  }
167  return $configuration;
168  }
169 }
all()
Get all the settings.
If this file is called directly, abort.
gravityview()
Definition: _stubs.php:26
If this file is called directly, abort.
If this file is called directly, abort.
all()
Returns all the objects in this collection as an an array.
add( $field)
Add a to this collection.
by_position( $position)
Get a copy of this filtered by position.
static from_configuration( $configuration)
Parse a configuration array into a Field_Collection.
$field
Definition: gquiz_grade.php:11