<?php
//Force title for Add Record from VIN or camera field.
add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
$forms = array( 942,2101,937,6944,8712,8755,11018,2285,2267,6932,1469,2418,6976,7463,6929,2206,7451,2095 );
if ( in_array($form_data['id'], $forms) ){
$vin = get_post_meta($post_id, 'wpcf-vin', true);
$camera = get_post_meta($post_id, 'wpcf-camera-name', true);
$post_title=$vin. ' ' .$camera ;
$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}
// add userid to cred
add_filter( 'wpt_field_options', 'add_some_options', 10, 3);
function add_some_options( $options, $title, $type )
{
switch( $title )
{
case 'user id':
$args = array(
'orderby' => 'display_name',
'fields' => array( 'ID', 'display_name')
);
foreach( get_users($args) as $user ) {
$options[] = array(
'#value' => $user->ID,
'#title' => $user->display_name,
);
}
break;
}
return $options;
}
// show pending posts in view
function ts_filter_query( $view_args, $view_settings, $view_id ) {
if ( in_array( $view_id, array( 11008 ) ) ) { // list of View IDs to apply this to
$view_args['post_status'] = array( 'publish', 'pending' );
}
return $view_args;
}
add_filter( 'wpv_filter_query', 'ts_filter_query', 101, 3 );
// Add select field values for TMNA Locations
add_shortcode('get_dynamic_select_options', 'get_dynamic_select_options_fn');
function get_dynamic_select_options_fn($atts) {
$field = $atts['field'];
$default_text = $atts['default_text'];
$default_value = $atts['default_value'];
$data = do_shortcode('[wpv-control-postmeta type="select" field="'.$field.'" url_param="wpv-'.$field.'"]');
$data_arr = explode('value="', $data);
if(sizeof($data_arr) >= 2)
{
for ($i=1; $i < sizeof($data_arr) ; $i++) {
$temp_arr = explode('">', $data_arr[$i]);
if(!empty($temp_arr[1])) {
$final_data[] = $temp_arr[0];
}
}
if(!empty($final_data))
{
ob_start();
echo '<select class="form-control wpt-form-select form-select select" output="bootstrap" preset_value="" urlparam="" preview="" previewsize="" select_label="" edit_label="" value_escape="" make_readonly="" placeholder="" select_text="" data-wpt-type="select" name="'.$field.'">';
echo '<option value="'.$default_value.'" selected="selected">'.$default_text.'</option>';
foreach ($final_data as $final_data_item) {
echo '<option value="'.$final_data_item.'" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-name="'.$field.'">'.$final_data_item.'</option>';
}
echo '</select>';
return ob_get_clean();
}
}
}
// Dynamically populate TMNA Location data for vehicle creation
add_filter( 'wpt_field_options', 'func_to_dynamically_populate_vehicle_locations', 10, 3);
function func_to_dynamically_populate_vehicle_locations( $options, $title, $type ){
switch( $title ){
case 'Vehicle Location':
$options = array();
// add first empty value
$options[] = array('#value' => '', '#title' => '---');
// get all tmna-location post items
$args = array( 'post_type' => 'tmna-location', 'posts_per_page' => -1, 'post_status' => 'publish','orderby' => 'title' );
$results = get_posts( $args );
if ( $results ) {
foreach ( $results as $post ) {
$options[] = array('#value' => $post->ID, '#title' => $post->post_title);
}
}
break;
}
return $options;
}
// Create relationship beteen vehicle and location
add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
if ($form_data['id']==6944) {
if(!empty($_POST['wpcf-vehicle-location'])) {
toolset_connect_posts( 'tmna-location-vehicle', $_POST['wpcf-vehicle-location'], $post_id );
}
}
}
// Add date filtering for views
add_shortcode('ts_day_week', function($atts){
$atts = shortcode_atts( array(
'day' => 'Monday',
), $atts);
$res = strtotime('last ' . $atts['day']);
if(date('l') == $atts['day']){
$res = strtotime('this ' . $atts['day']);
}
return $res;
});
?>