GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-date.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-date.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
8 /**
9  * Add custom options for date fields
10  */
12 
13  var $name = 'date';
14 
15  var $_gf_field_class_name = 'GF_Field_Date';
16 
17  var $is_searchable = true;
18 
19  var $search_operators = array( 'less_than', 'greater_than', 'is', 'isnot' );
20 
21  var $group = 'advanced';
22 
23  var $icon = 'dashicons-calendar-alt';
24 
25  public function __construct() {
26 
27  $this->label = esc_html__( 'Date', 'gk-gravityview' );
28 
29  add_filter( 'gravityview/merge_tags/modifiers/value', array( $this, 'apply_format_date_modifiers' ), 10, 6 );
30 
31  parent::__construct();
32  }
33 
34  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
35 
36  if( 'edit' === $context ) {
37  return $field_options;
38  }
39 
40  $this->add_field_support('date_display', $field_options );
41 
42  return $field_options;
43  }
44 
45  /**
46  * Allow Date fields to take advantage of the GV date modifiers
47  *
48  * @since 2.0
49  * @uses GravityView_Merge_Tags::format_date
50  *
51  * @param string $return The current merge tag value to be filtered.
52  * @param string $raw_value The raw value submitted for this field. May be CSV or JSON-encoded.
53  * @param string $value The original merge tag value, passed from Gravity Forms
54  * @param string $merge_tag If the merge tag being executed is an individual field merge tag (i.e. {Name:3}), this variable will contain the field's ID. If not, this variable will contain the name of the merge tag (i.e. all_fields).
55  * @param string $modifier The string containing any modifiers for this merge tag. For example, "maxwords:10" would be the modifiers for the following merge tag: `{Text:2:maxwords:10}`.
56  * @param GF_Field $field The current field.
57  *
58  * @return string If Date field, run it through GravityView_Merge_Tags::format_date; otherwise, return the original value
59  */
60  public function apply_format_date_modifiers( $return, $raw_value = '', $value = '', $merge_tag = '', $modifier = '', $field = null ) {
61 
62  if ( 'date' === $field->type ) {
63  $return = GravityView_Merge_Tags::format_date( $raw_value, $modifier );
64  }
65 
66  return $return;
67  }
68 
69  /**
70  * Get the default date format for a field based on the field ID and the time format setting
71  *
72  * @since 1.16.4
73 
74  * @param string $date_format The Gravity Forms date format for the field. Default: "mdy"
75  * @param int $field_id The ID of the field. Used to figure out full date/day/month/year
76  *
77  * @return string PHP date format for the date
78  */
79  static public function date_display( $value = '', $date_format = 'mdy', $field_id = 0 ) {
80 
81  // Let Gravity Forms figure out, based on the date format, what day/month/year values are.
82  $parsed_date = GFCommon::parse_date( $value, $date_format );
83 
84  // Are we displaying an input or the whole field?
85  $field_input_id = gravityview_get_input_id_from_id( $field_id );
86 
87  $date_field_output = '';
88  switch( $field_input_id ) {
89  case 1:
90  $date_field_output = \GV\Utils::get( $parsed_date, 'month' );
91  break;
92  case 2:
93  $date_field_output = \GV\Utils::get( $parsed_date, 'day' );
94  break;
95  case 3:
96  $date_field_output = \GV\Utils::get( $parsed_date, 'year' );
97  break;
98  }
99 
100  /**
101  * @filter `gravityview_date_format` Whether to override the Gravity Forms date format with a PHP date format
102  * @see https://codex.wordpress.org/Formatting_Date_and_Time
103  * @param null|string Date Format (default: $field->dateFormat)
104  */
105  $full_date_format = apply_filters( 'gravityview_date_format', $date_format );
106 
107  $full_date = GFCommon::date_display( $value, $full_date_format );
108 
109  // If the field output is empty, use the full date.
110  // Note: The output might be empty because $parsed_date didn't parse correctly.
111  return ( '' === $date_field_output ) ? $full_date : $date_field_output;
112  }
113 
114 }
115 
Modify field settings by extending this class.
apply_format_date_modifiers( $return, $raw_value='', $value='', $merge_tag='', $modifier='', $field=null)
Allow Date fields to take advantage of the GV date modifiers.
add_field_support( $key, &$field_options)
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
gravityview_get_input_id_from_id( $field_id='')
Very commonly needed: get the # of the input based on a full field ID.
Add custom options for date fields.
static format_date( $date_created='', $property='')
Format Merge Tags using GVCommon::format_date()
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
static date_display( $value='', $date_format='mdy', $field_id=0)
Get the default date format for a field based on the field ID and the time format setting...
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22