epl = EPL_base::get_instance();
$this->ecm = $this->epl->load_model( 'epl-common-model' );
$this->rm = $this->epl->load_model( 'epl-recurrence-model' );
$this->opt = $this->ecm->get_epl_options();
}
/**
* short descr
*
* long description
*
* @since 1.0.0
* @param int $var
* @return string
*/
function render_fields( $args ) {
global $fields;
extract( $args );
$defaults = array(
'_view' => 0,
'_type' => 'ind',
'_rows' => 1,
'_content' => ''
);
$meta = wp_parse_args( $meta, $defaults );
$_table = $section; //$fields[$section];
if ( empty( $_table ) || empty( $fields_to_display ) )
return null;
//make the values of the arrays into keys
$fields_to_display = array_flip( $fields_to_display );
//return only the array keys that match our $fields_to_display array
$fields_to_display = array_intersect_key( ( array ) $_table, $fields_to_display );
//if we want to see the form in a table row format
if ( $meta['_type'] == 'row' ) {
//For fields that are added via a filter for the first time and there is data for an event, there will not be any
//data for the new fields. We grab the keys of the master field (usually the first one), so that we can assign
//the keys to the new field. Otherwise, if there are already rows of data, only one row will be returned
$master_keys = '';
$r = array();
//The number of rows to display. This is determined by how many rows of data there are
for ( $i = 0; $i < $meta['_rows']; $i++ ) {
$_r = '';
$tmp_key = $this->make_unique_id( 6 );
//cycle through the fields that need to be displayed (from the config array)
foreach ( $fields_to_display as $field_name => $field_attr ) {
//$field_attr['key'] = '';
//if there is a value associated, meaning it is being edited
if ( isset( $meta['value'] ) ) {
//prepare the value to be passed to the next function
if ( isset( $field_attr['parent_keys'] ) )
$master_keys = (isset( $meta['value'][$field_name] )) ? $meta['value'][$field_name] : '';
$field_attr['value'] = (isset( $meta['value'][$field_name] )) ? $meta['value'][$field_name] : '';
$k = '';
//if the value is an array (from dynamically created fields)
if ( isset( $meta['value'][$field_name] ) && is_array( $meta['value'][$field_name] ) ) {
$k = array_keys( $meta['value'][$field_name] ); //will be used for checking dinamically added row data
$field_attr['value'] = $meta['value'][$field_name];
$field_attr['key'] = $k[$i]; //the selected row, for select, radio, checkbox
}
elseif ( $master_keys != '' ) {
//this will be used for newly added fields that will be stores as rows, like dates, times....
$k = array_keys( ( array ) $master_keys );
$field_attr['value'] = $this->remove_array_vals( $master_keys );
$field_attr['key'] = $k[$i];
}
}
//$field_attr['content'] = $meta['_content'];
$field_attr['tmp_key'] = $tmp_key;
$_r .= $this->create_element( $field_attr, $meta['_view'] );
}
if ( isset( $field_attr['key'] ) && $field_attr['key'] == '' )
$_k = $field_attr['tmp_key'];
else
$_k = isset( $field_attr['key'] ) ? $field_attr['key'] : '';
$r[$_k] = $_r;
}
}
else {
foreach ( $fields_to_display as $key => $field ) {
if ( isset( $meta['value'] ) ) {
$field['value'] = (isset( $meta['value'][$key] )) ? $meta['value'][$key] : '';
}
$field['content'] = $meta['_content'];
$_r[$key] = $this->create_element( $field, $meta['_view'] );
}
$r = $_r;
}
return $r;
}
/**
* Create a form field
*
* @param array $args (input_type,name, value, id, options, opt_key,opt_value, multiple, label, description, class, wrapper, size,readonly, multiple, size, $style)
*
* @return form field
*/
function create_element( $args = array(), $response_view = 0 ) {
if ( empty( $args ) )
return null;
$response_veiws = array( '', 'common/form-table-row', 'common/form-table-cell' ); //views used for returning the fields
//echo "
" . print_r($args, true). "
";
$defaults = array(
'input_type' => '',
'input_name' => '',
'return' => 1,
'name' => '',
'key' => '',
'value' => null,
'default_value' => null,
'default_checked' => 0,
'id' => '',
'options' => '',
'empty_row' => false,
'opt_key' => '',
'opt_value' => '',
'label' => '',
'description' => '',
'class' => '',
'rel' => '',
'wrapper' => '',
'size' => '',
'readonly' => '',
'required' => 0,
'validation' => '',
'multiple' => '',
'style' => '',
'content' => '',
'display_inline' => false,
'overview' => 0,
'tmp_key' => ''
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
if ( $return == 0 )
return null;
$value = stripslashes_deep( ($value == '' && !is_null( $default_value )) ? $default_value : $value );
//all the values come in as strings. For cbox, radio and selects,
//the options loop key is numeric. I am doing a cast comparison so
//string 0 != int 0, lots of issues.
//NEW issue: phone number is converted to int if it comes in 5557778888
$value = (is_numeric( $value ) && $value < 1000) ? ( int ) $value : $value;
$data = array(
'label' => '',
'description' => '',
'response_view' => $response_view,
'input_type' => $input_type,
'overview' => ''
);
$name = ($input_name != '') ? $input_name : $name;
//Doing this for the very first key of the a new record.
//Since we want to keep track of keys for registration purposes,
//leaving [] will make the key assignment automatic, creating problems when deleting or adding.moving records.
$name = (($input_type == 'text' || $input_type == 'hidden') && $key === '') ? str_replace( "[]", "[{$tmp_key}]", $name ) : $name;
//if a text field has already been saved with a key, assign the key to the name
if ( $key !== '' && ($input_type == 'text' || $input_type == 'hidden') ) {
$name = str_replace( "[", "[" . $key, $name );
$value = $value[$key];
}
if ( !is_numeric( $value ) )
$value = stripslashes_deep( $value );
if ( $readonly != '' ) {
$readonly = 'readonly="readonly"';
if ( $input_type == 'checkbox' ) {
//$readonly .= ' disabled="disabled"';
$style .= "visibility:hidden;";
}
if ( $input_type == 'radio' ) {
$readonly = ' disabled="disabled"';
$style .= "visibility:hidden;";
}
}
if ( $size != '' )
$size = "size='$size'";
if ( $multiple != '' ) {
$multiple = 'multiple="multiple"';
$size = ($size != '' ? "size='$size'" : "size='5'"); //default size needed for wordpress
$style .= " height:auto !important;"; //override wp height
}
if ( $required != 0 ) {
$required = '*';
$class .= ' required';
}
else
$required = '';
if ( $validation != '' ) {
$class .= ' required ' . $validation;
}
if ( $label != '' || $response_view == 2 )
$data['label'] = "";
if ( $description != '' || $response_view == 2 )
$data['description'] = "$description";
$data['field'] = '';
switch ( $input_type )
{
case 'section':
$response_view = 6;
$data['field'] = "