GravityView  2.17
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  * @param $view \GV\View The view!
92  *
93  * @return \GV\Field_Collection A filtered collection of \GV\Fields, filtered by visibility.
94  */
95  public function by_visible( $view = null ) {
96  $fields = new self();
97 
98  /** @type \GV\Field $field */
99  foreach ( $this->all() as $field ) {
100  if ( $field->is_visible( $view ) ) {
101  $fields->add( $field );
102  }
103  }
104  return $fields;
105  }
106 
107  /**
108  * Parse a configuration array into a Field_Collection.
109  *
110  * @param array $configuration The configuration, structured like so:
111  *
112  * array(
113  *
114  * [other zones]
115  *
116  * 'directory_list-title' => array(
117  *
118  * [other fields]
119  *
120  * '5372653f25d44' => array(
121  * @see \GV\Field::as_configuration() for structure
122  * )
123  *
124  * [other fields]
125  * )
126  *
127  * [other zones]
128  * )
129  *
130  * @return \GV\Field_Collection A collection of fields.
131  */
132  public static function from_configuration( $configuration ) {
133  $fields = new self();
134  foreach ( $configuration as $position => $_fields ) {
135 
136  if ( empty( $_fields ) || ! is_array( $_fields ) ) {
137  continue;
138  }
139 
140  foreach ( $_fields as $uid => $_configuration ) {
141  $field = Field::from_configuration( $_configuration );
142  $field->UID = $uid;
143  $field->position = $position;
144 
145  $fields->add( $field );
146  }
147  }
148  return $fields;
149  }
150 
151  /**
152  * Return a configuration array for this field collection.
153  *
154  * @return array See \GV\Field_Collection::from_configuration() for structure.
155  */
156  public function as_configuration() {
157  $configuration = array();
158 
159  /**
160  * @var \GV\Field $field
161  */
162  foreach ( $this->all() as $field ) {
163  if ( empty( $configuration[ $field->position ] ) ) {
164  $configuration[ $field->position ] = array();
165  }
166 
167  $configuration[ $field->position ][ $field->UID ] = $field->as_configuration();
168  }
169  return $configuration;
170  }
171 }
If this file is called directly, abort.
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.
by_visible( $view=null)
Get a copy of this filtered by visibility to current user context.
gravityview()
The main GravityView wrapper function.
static from_configuration( $configuration)
Parse a configuration array into a Field_Collection.