<?php
// file: public/class-cemetery-sexton-tools.php
/**
 * Conversion Tools for Cemetery Sexton plugin.
 *
 * @link       https://orbicular.media
 * @since      1.0.0
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 */

/**
 * Conversion Tools for Cemetery Sexton plugin.
 *
 * Converts text to pretty format.
 * Converts grave location to pretty format.
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 * @author     Bryan Meeks <bryan@orbicular.media>
 */
class Cemetery_Sexton_tools {

    // Format date (from sql to readable format)
    public function tools_dateFormat($olddate){
        if($olddate != ''){
            return date_format($olddate, 'm/d/Y');
        }else{
            return '';
        }
    }
    public function tools_dateFormatPretty($olddate){
        if($olddate != ''){
            return date_format($olddate, 'M d, Y');
        }else{
            return '';
        }
    }
    
    //************************************************************
    // DisplayLocation(): Displays the proper lot location information
    // Riverside Cemetery website and database
    // by: Bryan Meeks
    //************************************************************
    public function tools_displayLocation($block, $section, $rowno, $spaceno, $lotno, $compass, $mausoleum, $cryptno, $niche) {
        // Trim variables to ensure there are no spaces
        $block = trim((string)$block);
        $section = trim((string)$section);
        $rowno = trim((string)$rowno);
        $spaceno = trim((string)$spaceno);
        $lotno = trim((string)$lotno);
        $compass = trim((string)$compass);
        $mausoleum = trim((string)$mausoleum);
        $cryptno = trim((string)$cryptno);
        $niche = trim((string)$niche);
        $noexist = '';
        
        // Break down each component and change as necessary
        if (strlen($block)<> 0) {
            switch($block) {
                case '1':
                    $block = 'Block 1, ';
                    break;
                case '2':
                    $block = 'Block 2, ';
                    break;
                case '3':
                    $block = 'Block 3, ';
                    break;
                case '4':
                    $block = 'Block 4, ';
                    break;
                case '5':
                    $block = 'Block 5, ';
                    break;
                default:
                    $block = $block.', ';
                    break;
            }
        }else {
            $noexist .= 'no';
        }
        
        if (strlen($section) <> 0) {
            $section = 'Section '.$section.', ';
        }else {
            $noexist .= 'no';
        }
        
        if (strlen($rowno) <> 0) {
            switch($rowno) {
                case 'fence':
                    $rowno = 'Fence Row, ';
                    break;
                default:
                    $rowno = 'Row '.$rowno.', ';
                    break;
            }
        }
        
        if (strlen($spaceno) <> 0) {
            $spaceno = 'Space '.$spaceno.', ';
        }
        
        if (strlen($lotno) <> 0) {
            $lotno = 'Lot '.$lotno.', ';
        }
        
        if (strlen($compass) <> 0) {
            switch($compass) {
                case 'N':
                    $compass = 'North, ';
                    break;
                case 'S':
                    $compass = 'South, ';
                    break;
                case 'E':
                    $compass = 'East, ';
                    break;
                case 'W':
                    $compass = 'West, ';
                    break;
                case 'NE':
                    $compass = 'North Ease, ';
                    break;
                case 'SE':
                    $compass = 'South East, ';
                    break;
                case 'NW':
                    $compass = 'North West, ';
                    break;
                case 'SW':
                    $compass = 'South West, ';
                    break;
            }
        }
        
        if (strlen($mausoleum) <> 0) {
            $mausoleum = $mausoleum.', ';
        }else {
            $noexist .= 'no';
        }
        
        if (strlen($cryptno) <> 0) {
            $cryptno = 'Crypt '.$cryptno.', ';
        }
        
        if (strlen($niche) <> 0) {
            $niche = 'Niche '.$niche.', ';
        }
        
        // Plot location to display
        $disloc = $block.$mausoleum.$section.$rowno.$spaceno.$lotno.$compass.$cryptno.$niche;
        $display = substr($disloc,0,strlen($disloc)-2);
        //echo $noexist;
        
        // Check to make sure the lot is correct or unknown and output lot information
        if ($noexist == 'nonono') {
            $finaldisplay = 'Location is Unknown';
        }else {
            $finaldisplay = $display;
        }
        
        return $finaldisplay;
    }   
}