field() public method

If you are getting a field for output in a theme, most of the time you will want to use display() instead. This function will return arrays for relationship and file fields.
Since: 2.0
public field ( string | array $name, boolean $single = null, boolean $raw = false ) : mixed | null
$name string | array The field name, or an associative array of parameters
$single boolean (optional) For tableless fields, to return the whole array or the just the first item, or an associative array of parameters
$raw boolean (optional) Whether to return the raw value, or to run through the field type's display method, or an associative array of parameters
return mixed | null Value returned depends on the field type, null if the field doesn't exist, false if no value returned for tableless fields
Exemplo n.º 1
0
 /**
  * Get an offset if already set or traverse Pod and then set plus reutrn.
  *
  * @since 0.1.0
  *
  * @param mixed $offset Offset to get
  *
  * @return mixed
  */
 public function offsetGet($offset)
 {
     if ($this->offsetExists($offset)) {
         return parent::offsetGet($offset);
     } else {
         if ('id' == $offset || 'ID' == $offset) {
             $_value = $this->pod->id();
         } else {
             $_value = $this->pod->field($offset);
         }
         if ($_value) {
             parent::offsetSet($offset, $_value);
             return $_value;
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Export a pod item by depth level
  *
  * @param Pods $pod Pods object
  * @param array $fields Fields to export
  * @param int $depth Depth limit
  * @param boolean $flatten Whether to flatten arrays for display
  * @param int $current_depth Current depth level
  *
  * @return array Data array
  *
  * @since 2.3
  */
 private function export_pod_item_level($pod, $fields, $depth, $flatten = false, $current_depth = 1)
 {
     $tableless_field_types = PodsForm::tableless_field_types();
     $simple_tableless_objects = PodsForm::simple_tableless_objects();
     $object_fields = (array) pods_var_raw('object_fields', $pod->pod_data, array(), null, true);
     $export_fields = array();
     foreach ($fields as $k => $field) {
         if (!is_array($field)) {
             $field = array('id' => 0, 'name' => $field);
         }
         if (isset($pod->fields[$field['name']])) {
             $field = $pod->fields[$field['name']];
             $field['lookup_name'] = $field['name'];
             if (in_array($field['type'], $tableless_field_types) && !in_array(pods_var('pick_object', $field), $simple_tableless_objects)) {
                 if ('pick' == $field['type']) {
                     if (empty($field['table_info'])) {
                         $field['table_info'] = $this->get_table_info(pods_var_raw('pick_object', $field), pods_var_raw('pick_val', $field), null, null, $field);
                     }
                     if (!empty($field['table_info'])) {
                         $field['lookup_name'] .= '.' . $field['table_info']['field_id'];
                     }
                 } elseif (in_array($field['type'], PodsForm::file_field_types())) {
                     $field['lookup_name'] .= '.guid';
                 }
             }
             $export_fields[$field['name']] = $field;
         } elseif (isset($object_fields[$field['name']])) {
             $field = $object_fields[$field['name']];
             $field['lookup_name'] = $field['name'];
             $export_fields[$field['name']] = $field;
         } elseif ($field['name'] == $pod->pod_data['field_id']) {
             $field['type'] = 'number';
             $field['lookup_name'] = $field['name'];
             $export_fields[$field['name']] = $field;
         }
     }
     $data = array();
     foreach ($export_fields as $field) {
         // Return IDs (or guid for files) if only one level deep
         if (1 == $depth) {
             $data[$field['name']] = $pod->field(array('name' => $field['lookup_name'], 'output' => 'arrays'));
         } elseif ((-1 == $depth || $current_depth < $depth) && 'pick' == $field['type'] && !in_array(pods_var('pick_object', $field), $simple_tableless_objects)) {
             $related_data = array();
             $related_ids = $pod->field(array('name' => $field['name'], 'output' => 'ids'));
             if (!empty($related_ids)) {
                 $related_ids = (array) $related_ids;
                 $pick_object = pods_var_raw('pick_object', $field);
                 $related_pod = pods(pods_var_raw('pick_val', $field), null, false);
                 // If this isn't a Pod, return data exactly as Pods does normally
                 if (empty($related_pod) || 'pod' != $pick_object && $pick_object != $related_pod->pod_data['type'] || $related_pod->pod == $pod->pod) {
                     $related_data = $pod->field(array('name' => $field['name'], 'output' => 'arrays'));
                 } else {
                     $related_object_fields = (array) pods_var_raw('object_fields', $related_pod->pod_data, array(), null, true);
                     $related_fields = array_merge($related_pod->fields, $related_object_fields);
                     foreach ($related_ids as $related_id) {
                         if ($related_pod->fetch($related_id)) {
                             $related_item = $this->export_pod_item_level($related_pod, $related_fields, $depth, $flatten, $current_depth + 1);
                             $related_data[$related_id] = $this->do_hook('export_pod_item_level', $related_item, $related_pod->pod, $related_pod->id(), $related_pod, $related_fields, $depth, $flatten, $current_depth + 1);
                         }
                     }
                     if ($flatten && !empty($related_data)) {
                         $related_data = pods_serial_comma(array_values($related_data), array('and' => '', 'field_index' => $related_pod->pod_data['field_index']));
                     }
                 }
             }
             $data[$field['name']] = $related_data;
         } else {
             $data[$field['name']] = $pod->field(array('name' => $field['name'], 'output' => 'arrays'));
         }
         if ($flatten && is_array($data[$field['name']])) {
             $data[$field['name']] = pods_serial_comma($data[$field['name']], array('field' => $field['name'], 'fields' => $export_fields, 'and' => ''));
         }
     }
     $data['id'] = (int) $pod->id();
     return $data;
 }
 public function restricted($action = 'edit', $row = null)
 {
     $restricted = false;
     $restrict = array();
     if (isset($this->restrict[$action])) {
         $restrict = (array) $this->restrict[$action];
     }
     // @todo Build 'edit', 'duplicate', 'delete' action support for 'where' which runs another find() query
     /*if ( !in_array( $action, array( 'manage', 'reorder' ) ) ) {
                 $where = pods_var_raw( $action, $this->where, null, null, true );
     
                 if ( !empty( $where ) ) {
                     $restricted = true;
     
                     $old_where = $this->where[ $action ];
     
                     $id = $this->row[ $this->sql[ 'field_id' ] ];
     
                     if ( is_array( $where ) ) {
                         if ( 'OR' == pods_var( 'relation', $where ) )
                             $where = array( $where );
     
                         $where[] = "`t`.`" . $this->sql[ 'field_id' ] . "` = " . (int) $id;
                     }
                     else
                         $where = "( {$where} ) AND `t`.`" . $this->sql[ 'field_id' ] . "` = " . (int) $id;
     
                     $this->where[ $action ] = $where;
     
                     $data = false;
     
                     //$data = $this->get_data();
     
                     $this->where[ $action ] = $old_where;
     
                     if ( empty( $data ) )
                         $restricted = true;
                 }
             }*/
     $author_restrict = false;
     if (!empty($this->restrict['author_restrict']) && $restrict === $this->restrict['author_restrict']) {
         $restricted = false;
         $author_restrict = true;
         if (is_object($this->pod)) {
             $restricted = true;
             if ('settings' == $this->pod->pod_data['type'] && 'add' == $action) {
                 $action = 'edit';
             }
             if (pods_is_admin(array('pods', 'pods_content'))) {
                 $restricted = false;
             } elseif ('manage' == $action) {
                 if (!in_array('edit', $this->actions_disabled) && current_user_can('pods_edit_' . $this->pod->pod) && current_user_can('pods_edit_others_' . $this->pod->pod)) {
                     $restricted = false;
                 } elseif (!in_array('delete', $this->actions_disabled) && current_user_can('pods_delete_' . $this->pod->pod) && current_user_can('pods_delete_others_' . $this->pod->pod)) {
                     $restricted = false;
                 } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) {
                     $restricted = false;
                 }
             } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) {
                 $restricted = false;
             }
         }
         /* @todo determine proper logic for non-pods capabilities
                     else {
                         $restricted = true;
         
                         if ( pods_is_admin( array( 'pods', 'pods_content' ) ) )
                             $restricted = false;
                         elseif ( current_user_can( 'pods_' . $action . '_others_' . $_tbd ) )
                             $restricted = false;
                     }*/
     }
     if ($restricted && !empty($restrict)) {
         $relation = strtoupper(trim(pods_var('relation', $restrict, 'AND', null, true)));
         if ('AND' != $relation) {
             $relation = 'OR';
         }
         $okay = true;
         foreach ($restrict as $field => $match) {
             if ('relation' == $field) {
                 continue;
             }
             if (is_array($match)) {
                 $match_okay = true;
                 $match_relation = strtoupper(trim(pods_var('relation', $match, 'OR', null, true)));
                 if ('AND' != $match_relation) {
                     $match_relation = 'OR';
                 }
                 foreach ($match as $the_field => $the_match) {
                     if ('relation' == $the_field) {
                         continue;
                     }
                     $value = null;
                     if (is_object($this->pod)) {
                         $value = $this->pod->field($the_match, true);
                     } else {
                         if (empty($row)) {
                             $row = $this->row;
                         }
                         if (isset($row[$the_match])) {
                             if (is_array($row[$the_match])) {
                                 if (false !== strpos($the_match, '.')) {
                                     $the_matches = explode('.', $the_match);
                                     $value = $row[$the_match];
                                     foreach ($the_matches as $m) {
                                         if (is_array($value) && isset($value[$m])) {
                                             $value = $value[$m];
                                         } else {
                                             $value = null;
                                             break;
                                         }
                                     }
                                 }
                             } else {
                                 $value = $row[$the_match];
                             }
                         }
                     }
                     if (is_array($value)) {
                         if (!in_array($the_match, $value)) {
                             $match_okay = false;
                         } elseif ('OR' == $match_relation) {
                             $match_okay = true;
                             break;
                         }
                     } elseif ($value == $the_match) {
                         $match_okay = false;
                     } elseif ('OR' == $match_relation) {
                         $match_okay = true;
                         break;
                     }
                 }
                 if (!$match_okay) {
                     $okay = false;
                 }
                 if ('OR' == $relation) {
                     $okay = true;
                     break;
                 }
             } else {
                 $value = null;
                 if (is_object($this->pod)) {
                     $value = $this->pod->field($match, true);
                 } else {
                     if (empty($row)) {
                         $row = $this->row;
                     }
                     if (isset($row[$match])) {
                         if (is_array($row[$match])) {
                             if (false !== strpos($match, '.')) {
                                 $matches = explode('.', $match);
                                 $value = $row[$match];
                                 foreach ($matches as $m) {
                                     if (is_array($value) && isset($value[$m])) {
                                         $value = $value[$m];
                                     } else {
                                         $value = null;
                                         break;
                                     }
                                 }
                             }
                         } else {
                             $value = $row[$match];
                         }
                     }
                 }
                 if (is_array($value)) {
                     if (!in_array($match, $value)) {
                         $okay = false;
                     } elseif ('OR' == $relation) {
                         $okay = true;
                         break;
                     }
                 } elseif ($value != $match) {
                     $okay = false;
                 } elseif ('OR' == $relation) {
                     $okay = true;
                     break;
                 }
             }
         }
         if (!empty($author_restrict)) {
             if (is_object($this->pod) && 'manage' == $action) {
                 if (!in_array('edit', $this->actions_disabled) && !current_user_can('pods_edit_' . $this->pod->pod) && !in_array('delete', $this->actions_disabled) && !current_user_can('pods_delete_' . $this->pod->pod)) {
                     $okay = false;
                 }
             }
             if (is_object($this->pod) && !current_user_can('pods_' . $action . '_' . $this->pod->pod)) {
                 $okay = false;
             }
             /* @todo determine proper logic for non-pods capabilities
                elseif ( !current_user_can( 'pods_' . $action . '_' . $_tbd ) )
                    $okay = false;*/
             if (!$okay && !empty($row)) {
                 foreach ($this->restrict['author_restrict'] as $key => $val) {
                     $author_restricted = $this->get_field($key);
                     if (!empty($author_restricted)) {
                         if (!is_array($author_restricted)) {
                             $author_restricted = (array) $author_restricted;
                         }
                         if (is_array($val)) {
                             foreach ($val as $v) {
                                 if (in_array($v, $author_restricted)) {
                                     $okay = true;
                                 }
                             }
                         } elseif (in_array($val, $author_restricted)) {
                             $okay = true;
                         }
                     }
                 }
             }
         }
         if ($okay) {
             $restricted = false;
         }
     }
     if (isset($this->actions_custom[$action]) && is_array($this->actions_custom[$action]) && isset($this->actions_custom[$action]['restrict_callback']) && is_callable($this->actions_custom[$action]['restrict_callback'])) {
         $restricted = call_user_func($this->actions_custom[$action]['restrict_callback'], $restricted, $restrict, $action, $row, $this);
     }
     $restricted = $this->do_hook('restricted_' . $action, $restricted, $restrict, $action, $row);
     return $restricted;
 }
Exemplo n.º 4
0
 public function restricted($action = 'edit', $row = null)
 {
     $restricted = false;
     $restrict = array();
     if (isset($this->restrict[$action])) {
         $restrict = (array) $this->restrict[$action];
     }
     $author_restrict = false;
     if (!empty($this->restrict['author_restrict']) && $restrict == $this->restrict['author_restrict']) {
         $author_restrict = true;
         if (is_object($this->pod)) {
             $restricted = true;
             if (pods_is_admin(array('pods', 'pods_content'))) {
                 $restricted = false;
             } elseif ('manage' == $action) {
                 if (!in_array('edit', $this->actions_disabled) && current_user_can('pods_edit_' . $this->pod->pod) && current_user_can('pods_edit_others_' . $this->pod->pod)) {
                     $restricted = false;
                 } elseif (!in_array('delete', $this->actions_disabled) && current_user_can('pods_delete_' . $this->pod->pod) && current_user_can('pods_delete_others_' . $this->pod->pod)) {
                     $restricted = false;
                 } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) {
                     $restricted = false;
                 }
             } elseif (current_user_can('pods_' . $action . '_' . $this->pod->pod) && current_user_can('pods_' . $action . '_others_' . $this->pod->pod)) {
                 $restricted = false;
             }
         }
         /* @todo determine proper logic for non-pods capabilities
                     else {
                         $restricted = true;
         
                         if ( pods_is_admin( array( 'pods', 'pods_content' ) ) )
                             $restricted = false;
                         elseif ( current_user_can( 'pods_' . $action . '_others_' . $_tbd ) )
                             $restricted = false;
                     }*/
     }
     if ($restricted && !empty($restrict)) {
         $relation = strtoupper(trim(pods_var('relation', $restrict, 'AND', null, true)));
         if ('AND' != $relation) {
             $relation = 'OR';
         }
         $okay = true;
         foreach ($restrict as $field => $match) {
             if ('relation' == $field) {
                 continue;
             }
             if (is_array($match)) {
                 $match_okay = true;
                 $match_relation = strtoupper(trim(pods_var('relation', $match, 'OR', null, true)));
                 if ('AND' != $match_relation) {
                     $match_relation = 'OR';
                 }
                 foreach ($match as $the_field => $the_match) {
                     if ('relation' == $the_field) {
                         continue;
                     }
                     $value = null;
                     if (is_object($this->pod)) {
                         $value = $this->pod->field($the_match, true);
                     } else {
                         if (empty($row)) {
                             $row = $this->row;
                         }
                         if (isset($row[$the_match])) {
                             if (is_array($row[$the_match])) {
                                 if (false !== strpos($the_match, '.')) {
                                     $the_matches = explode('.', $the_match);
                                     $value = $row[$the_match];
                                     foreach ($the_matches as $m) {
                                         if (is_array($value) && isset($value[$m])) {
                                             $value = $value[$m];
                                         } else {
                                             $value = null;
                                             break;
                                         }
                                     }
                                 }
                             } else {
                                 $value = $row[$the_match];
                             }
                         }
                     }
                     if (is_array($value)) {
                         if (!in_array($the_match, $value)) {
                             $match_okay = false;
                         } elseif ('OR' == $match_relation) {
                             $match_okay = true;
                             break;
                         }
                     } elseif ($value == $the_match) {
                         $match_okay = false;
                     } elseif ('OR' == $match_relation) {
                         $match_okay = true;
                         break;
                     }
                 }
                 if (!$match_okay) {
                     $okay = false;
                 }
                 if ('OR' == $relation) {
                     $okay = true;
                     break;
                 }
             } else {
                 $value = null;
                 if (is_object($this->pod)) {
                     $value = $this->pod->field($match, true);
                 } else {
                     if (empty($row)) {
                         $row = $this->row;
                     }
                     if (isset($row[$match])) {
                         if (is_array($row[$match])) {
                             if (false !== strpos($match, '.')) {
                                 $matches = explode('.', $match);
                                 $value = $row[$match];
                                 foreach ($matches as $m) {
                                     if (is_array($value) && isset($value[$m])) {
                                         $value = $value[$m];
                                     } else {
                                         $value = null;
                                         break;
                                     }
                                 }
                             }
                         } else {
                             $value = $row[$match];
                         }
                     }
                 }
                 if (is_array($value)) {
                     if (!in_array($match, $value)) {
                         $okay = false;
                     } elseif ('OR' == $relation) {
                         $okay = true;
                         break;
                     }
                 } elseif ($value != $match) {
                     $okay = false;
                 } elseif ('OR' == $relation) {
                     $okay = true;
                     break;
                 }
             }
         }
         if (!empty($author_restrict)) {
             if (is_object($this->pod) && 'manage' == $action) {
                 if (!in_array('edit', $this->actions_disabled) && !current_user_can('pods_edit_' . $this->pod->pod) && !in_array('delete', $this->actions_disabled) && !current_user_can('pods_delete_' . $this->pod->pod)) {
                     $okay = false;
                 }
             }
             if (is_object($this->pod) && !current_user_can('pods_' . $action . '_' . $this->pod->pod)) {
                 $okay = false;
             }
             /* @todo determine proper logic for non-pods capabilities
                elseif ( !current_user_can( 'pods_' . $action . '_' . $_tbd ) )
                    $okay = false;*/
             if (!$okay && !empty($row)) {
                 foreach ($this->restrict['author_restrict'] as $key => $val) {
                     $author_restricted = $this->get_field($key);
                     if (!empty($author_restricted)) {
                         if (!is_array($author_restricted)) {
                             $author_restricted = (array) $author_restricted;
                         }
                         if (is_array($val)) {
                             foreach ($val as $v) {
                                 if (in_array($v, $author_restricted)) {
                                     $okay = true;
                                 }
                             }
                         } elseif (in_array($val, $author_restricted)) {
                             $okay = true;
                         }
                     }
                 }
             }
         }
         if ($okay) {
             $restricted = false;
         }
     }
     $restricted = $this->do_hook('restricted_' . $action, $restricted, $restrict, $action, $row);
     return $restricted;
 }