<?php
namespace Copa\CustomFields;
use Copa;
/**
* Class EventFields
*
* @package Copa\EventFields
*
*/
class EventFields {
private static $prefix = Copa\PREFIX;
/**
* Initiate our hooks.
*/
public static function init() {
// Split seaialized String and saves meta for the object.
add_filter( 'cmb2_override_meta_save', array( MetaSplit::instance(), 'meta_save' ), 100, 3 );
add_action( 'cmb2_init', array( __CLASS__, '_register_events_formfields_metabox' ) );
add_filter( 'cmb2_render_date_select', array( __CLASS__, 'cmb2_render_date_select_field_callback' ), 10, 5 );
add_filter( 'cmb2_sanitize_date_select', array(
__CLASS__,
'cmb2_sanitize_date_select_field_callback',
), 10, 4 );
}
/**
* events field
* Hook in and add a metabox to select templates
*/
public function _register_events_formfields_metabox() {
$metabox = new_cmb2_box( array(
'id' => self::$prefix . 'eventform',
'title' => __( 'Termine', 'copa' ),
'object_types' => array( 'events_posts', ),
'classes' => 'event-form',
'show_names' => true,
'cmb_styles' => false,
'fields' => array(
array(
'name' => __( 'Ragulary Event', 'copa' ),
'id' => self::$prefix . 'date_regular',
'type' => 'checkbox',
'attributes' => array(
'class' => 'checkbox',
),
),
array(
'name' => __( 'Email *', 'copa' ),
'id' => self::$prefix . 'email',
'type' => 'text_email',
'attributes' => array(
'required' => 'required',
'class' => 'form-control',
),
),
array(
'name' => __( 'Phone', 'copa' ),
'id' => self::$prefix . 'phone',
'type' => 'text',
'attributes' => array(
'class' => 'form-control',
),
),
array(
'name' => __( 'Website', 'copa' ),
'id' => self::$prefix . 'text_url',
'type' => 'text_url',
'protocols' => array( 'http', 'https' ),
'attributes' => array(
'class' => 'form-control',
),
),
array(
'name' => __( 'Date from', 'copa' ),
'id' => self::$prefix . 'date_start',
'type' => 'date_select',
),
array(
'name' => __( 'Date to', 'copa' ),
'id' => self::$prefix . 'date_end',
'type' => 'date_select',
'default' => 'save_timestamp',
),
array(
'name' => 'Google Maps *',
'id' => self::$prefix . 'location',
'type' => 'pw_map',
'split_values' => true, // Save latitude and longitude as two separate fields
'attributes' => array(
'placeholder' => __( 'Please enter your address', 'copa' ),
'required' => 'required',
),
),
),
) );
if ( !is_admin() ) {
$metabox->add_field( array(
'name' => __( 'Titel *', 'copa' ),
'id' => self::$prefix . 'title',
'type' => 'text',
'attributes' => array(
'autocomplete' => 'off',
'required' => 'required',
),
), 1 );
$metabox->add_field( array(
'name' => __( 'Text *', 'copa' ),
'id' => self::$prefix . 'content',
'type' => 'textarea',
'options' => array(
'textarea_rows' => 12,
),
'attributes' => array(
'maxlength' => '900',
'cols' => '70',
'required' => 'required',
),
), 2 );
$metabox->add_field( array(
'id' => 'form_type',
'type' => 'hidden',
'attributes' => array(
'value' => 'event',
),
), 8 );
}
}
/**
* Returns options markup for a month select field.
*
* @param mixed $value Selected/saved month
*
* @return string html string containing all month options
*/
public function cmb2_get_month_options( $value ) {
$month_list = array(
'01' => __( 'January', 'copa' ),
'02' => __( 'February', 'copa' ),
'03' => __( 'March', 'copa' ),
'04' => __( 'April', 'copa' ),
'05' => __( 'May', 'copa' ),
'06' => __( 'June', 'copa' ),
'07' => __( 'July', 'copa' ),
'08' => __( 'August', 'copa' ),
'09' => __( 'September', 'copa' ),
'10' => __( 'October', 'copa' ),
'11' => __( 'November', 'copa' ),
'12' => __( 'December', 'copa' ),
);
$month_options = '';
$month_options .= '<option disabled selected>' . __( 'Month', 'copa' ) . '</option>';
foreach ( $month_list as $key => $month ) {
$month_options .= '<option value="' . $key . '" ' . selected( $value, $key, false ) . '>' . $month . '</option>';
}
return $month_options;
}
/**
* Returns options markup for a day select field.
*
* @param mixed $value Selected/saved day
*
* @return string html string containing all day options
*/
public function cmb2_get_day_options( $value ) {
$day_list = range( 1, 31 );
$day_options = '';
$day_options .= '<option disabled selected>' . __( 'Day', 'copa' ) . '</option>';
foreach ( $day_list as $day ) {
$day_options .= '<option value="' . $day . '" ' . selected( $value, $day, false ) . '>' . $day . '</option>';
}
return $day_options;
}
/**
* Returns options markup for a year select field.
*
* @param mixed $value Selected/saved year
*
* @return string html string containing all year options
*/
public function cmb2_get_year_options( $value ) {
$curyear = date( 'Y' );
$year_list = range( $curyear, $curyear + 5 );
$year_options = '';
$year_options .= '<option disabled selected>' . __( 'Year', 'copa' ) . '</option>';
foreach ( $year_list as $year ) {
$year_options .= '<option value="' . $year . '" ' . selected( $value, $year, false ) . '>' . $year . '</option>';
}
return $year_options;
}
/**
* Render Date Field.
* @see https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types#example-4-multiple-inputs-one-field-lets-create-an-address-field
*
* @param $field
* @param $value
* @param $object_id
* @param $object_type
* @param $field_type
*/
public function cmb2_render_date_select_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
// make sure we specify each part of the value we need.
$value = wp_parse_args( $value, array(
'day' => '',
'month' => '',
'year' => '',
'datetimestamp' => '',
) );
?>
<div class="alignleft">
<?php echo $field_type->select( array(
'name' => $field_type->_name( '[day]' ),
'id' => $field_type->_id( '_day' ),
'options' => self::cmb2_get_day_options( $value['day'] ),
'desc' => '',
'class' => 'form-control',
) );
?>
</div>
<div class="alignleft">
<?php echo $field_type->select( array(
'name' => $field_type->_name( '[month]' ),
'id' => $field_type->_id( '_month' ),
'options' => self::cmb2_get_month_options( $value['month'] ),
'desc' => '',
'class' => 'form-control',
) );
?>
</div>
<div class="alignleft">
<?php echo $field_type->select( array(
'name' => $field_type->_name( '[year]' ),
'id' => $field_type->_id( '_year' ),
'options' => self::cmb2_get_year_options( $value['year'] ),
'desc' => '',
'class' => 'form-control',
) );
?>
</div>
<?php
$day = isset( $value['day'] ) ? $value['day'] : '';
$month = isset( $value['month'] ) ? $value['month'] : '';
$year = isset( $value['year'] ) ? $value['year'] : '';
?>
<div class="alignleft hidden">
<?php echo $field_type->input( array(
'name' => $field_type->_name( '[datetimestamp]' ),
'id' => $field_type->_id( '_datetimestamp' ),
'value' => strtotime( $day . '-' . $month . '-' . $year ),
'type' => 'hidden',
) ); ?>
</div>
<div class="alignleft">
<input type="hidden">
</div>
<br class="clear">
<?php
echo $field_type->_desc( true );
}
/**
* Save the datetimestap if end date not exist.
*
* @param $override_value
* @param $value
* @param $object_id
* @param $field_args
*
* @return mixed
*/
public function cmb2_sanitize_date_select_field_callback( $override_value, $value, $object_id, $field_args ) {
if ( empty( $field_args['kradblatt_date_regular'] ) && ( isset( $value['datetimestamp'] ) && ( isset( $value['day'] ) && isset( $value['month'] ) && isset( $value['year'] ) ) ) ) {
$datetimestamp = strtotime( $value['datetimestamp'] );
update_post_meta( $object_id, 'kradblatt_date_end_datetimestamp', $datetimestamp );
}
return $value;
}
}