<?php

/**
 * Returns the services content
 *
 * This function will return the specified services
 * content. If the shortcode dataType parameter is
 * 'quote', the content will be the services quote
 * rather than the main content. This mean a customer
 * can remove the quotes by deleting the shortcode.
 *
 * @function get_services()
 *
 * @uses $iris_db::get_results(), wpautop()
 *
 * @param (string) $title the services title, $dataType the type of services content
 *
 * @return (string) the services post content
 * 
 * @since 1.0.0 
 *
 */
function get_services( $title, $dataType ) {

    global $iris_db;

    $services_array = $iris_db->get_results( "SELECT * FROM services WHERE service_name='$title'" );

    foreach ( $services_array as $services_item ) {

        if ( $dataType == 'quote' ) {

            return '<blockquote>' . $services_item->service_quote . '</blockquote>';
        } else if ( $dataType == 'content' ) {

            return wpautop( $services_item->service_data );
        }
    }
}


/**
 * returns the services list
 *
 * This function will return the services list.
 * The list is made up of child pages to the
 * current page.
 *
 * @function list_services()
 *
 * @uses wp_list_pages()
 *
 * @param N/A
 *
 * @return (string) a list of services pages
 *
 * @update 1.5.6
 *
 * @deprecated 1.1.0 This function is very similar to add_index_page() so will been removed.
 *
 */
function list_services() {
    $args = array(
        'post_type' => 'page',
        'posts_per_page' => -1,
        'post_parent' => get_the_ID(),
        'order' => 'ASC',
        'orderby' => 'title'
    );


    $parent = new WP_Query( $args );

    if ( $parent->have_posts() ):
        $servicelist = '<ul id="services-list">';
    while ( $parent->have_posts() ): $parent->the_post();
    global $iris_db;
    $service_from_db = $iris_db->get_row( "SELECT service_name FROM services WHERE old_title='" . get_the_title() . "'" );
    if ( $service_from_db->service_name != '' ) {
        $servicelist .= '<li class="page_item page-item-' . get_the_ID() . '"><a href="' . get_the_permalink() . '">' . $service_from_db->service_name . '</a></li>';
    } else {
        $servicelist .= '<li class="page_item page-item-' . get_the_ID() . '"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
    }
    endwhile;
    $servicelist .= '</ul>';
    endif;
    wp_reset_postdata();
    return $servicelist;
}


/**
 * Obfuscates mailto: links in the mail content
 *
 * This function will replace plain text
 * mailto: links within the WoprdPress
 * content to obfuscated inline JavaScript.
 *
 * @function add_services()
 *
 * @uses get_services(), list_services()
 *
 * @param (array) $service[ (string) service_name, (string) service_data_type ]
 *
 * @return (string) either the services content or the list of services
 * 
 * @since 1.0.0 
 *
 */
function add_services( $service ) {

    $service = shortcode_atts( array( 'service_name' => '', 'services_data_type' => 'content' ), $service, 'services' );

    if ( $service[ 'service_name' ] != '' || $service[ 'service_name' ] != NULL ) { //echo $service['service_name'];
        global $iris_db;
        $service_from_db = $iris_db->get_row( "SELECT service_name FROM services WHERE old_title='" . $service[ 'service_name' ] . "'" );
        if ( $service_from_db->service_name != '' ) {

            return get_services( $service_from_db->service_name, $service[ 'services_data_type' ] );
        } else {
            return get_services( $service[ 'service_name' ], $service[ 'services_data_type' ] );
        }

    }
    return list_services();
}
add_shortcode( 'services', 'add_services' );

/**
 * Calls the plugin template file services so that the appropriate title is called from Generic DB
 *
 * Calls the plugin template file services 
 * so that the appropriate title is 
 * called from Generic DB
 *
 * @function services_template()
 *
 * @return - template is returned
 * 
 * @since 1.5.6
 */
function services_template() {
    global $wp;
    $plugindir = dirname( __FILE__ );
    $uriSegments = explode( "/", parse_url( $_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH ) );
    if ( $uriSegments[ 1 ] == 'services' && $uriSegments[ 2 ] != '' ) {
        $templatefilename = 'services.php';

        $return_template = $plugindir . '/template/' . $templatefilename;

        do_theme_redirect( $return_template );
    }
}

add_action( "template_redirect", 'services_template' );