<?php
//SECTION - Class dacdb_plus
if (!class_exists('dacdb_plus')) {
    /**
     * Class dacdb_plus
     *
     * Description: This class defines a plugin called 'dacdb_plus' with various methods and functions.
     */
    class dacdb_plus
    {
        function dacdb_plus_register()
        {
            register_widget('DACDB_Plus_Widget');
        }
        function __construct()
        {
            //Register Plugin
            add_action('widgets_init', array($this, 'dacdb_plus_register'));
            //Add Core Functionality Shortcode
            add_shortcode('dacdb_plus', array($this, 'dacdb_plus_function'));
            // Add JavaScript Files
            add_action('admin_enqueue_scripts', array($this, 'dacdb_plus_admin_js')); // Admin scripts
            add_action('wp_enqueue_scripts', array($this, 'enqueue_resources')); // Front-end scripts and styles
        }
        public function enqueue_resources() {
            // Enqueue front-end scripts
            wp_enqueue_script('dacdb-plus-js', MMPDB_PLUGIN_URL. 'js/dacdb_plus.js', array('jquery'), MMPDB_PLUGIN_VERSION, true);
            wp_enqueue_script('pagination', MMPDB_PLUGIN_URL . 'js/pagination.js', array('jquery'), MMPDB_PLUGIN_VERSION, true);
            wp_enqueue_script('moment2', MMPDB_PLUGIN_URL . 'js/moment.min.js', array('jquery'), MMPDB_PLUGIN_VERSION, true);
            wp_enqueue_script('fullcalendar', MMPDB_PLUGIN_URL . 'js/index.global.js', array('jquery'), MMPDB_PLUGIN_VERSION, true);
            
            // Conditionally enqueue FontAwesome
            global $post;
            if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'dacdb_plus')) {
                if (!wp_style_is('font-awesome', 'enqueued') && !wp_style_is('dacdb-fontawesome', 'enqueued')) {
                    wp_enqueue_style('dacdb-fontawesome', 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.2/css/all.css', array(), '6.4.2', 'all');
                }
            }
            
            // Add UserWay script with data-account attribute
            if (isset($this->options['userway_data_account']) && !empty($this->options['userway_data_account'])) {
                add_action('wp_head', array($this, 'add_userway_script_to_head'));
            }
        }
        
        public function add_userway_script_to_head() {
            if (isset($this->options['userway_data_account']) && !empty($this->options['userway_data_account'])) {
                printf(
                    '<script src="https://cdn.userway.org/widget.js" data-account="%s"></script>',
                    esc_attr($this->options['userway_data_account'])
                );
            }
        }
        
        public static function activate()
        {
            // Do nothing plug in does not use Wordpress classes or functions
        }
        public static function deactivate()
        {
            // Do nothing plug in does not use Wordpress classes or functions
        }
        public function dacdb_plus_admin_js() {
            // Use the defined constant to point to the correct directory
            wp_enqueue_script('dacdb-admin-js', MMPDB_PLUGIN_URL . 'js/admin.js', array('jquery'), MMPDB_PLUGIN_VERSION, true);
        }
        public function dacdb_plus_js()
        {
            wp_enqueue_script('dacdb-plus-js', plugins_url('js/dacdb_plus.js', __FILE__), '', MMPDB_PLUGIN_VERSION);
        }
        public function pagination()
        {
            wp_enqueue_script('pagination', plugins_url('js/pagination.js', __FILE__), '', MMPDB_PLUGIN_VERSION);
        }
        public function moment2()
        {
            wp_enqueue_script('moment2', plugins_url('js/moment.min.js', __FILE__), '', MMPDB_PLUGIN_VERSION);
        }
        public function fullcalendar()
        {
            wp_enqueue_script('fullcalendar', plugins_url('js/index.global.js', __FILE__), '', MMPDB_PLUGIN_VERSION);
        }

        // Method to conditionally enqueue FontAwesome based on shortcode presence
        public function maybe_enqueue_fontawesome()
        {
            global $post;
            if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'dacdb_plus')) {
                add_action('wp_enqueue_scripts', array($this, 'enqueue_fontawesome'));
            }
        }
        public function enqueue_fontawesome()
        {
            if (!wp_style_is('font-awesome', 'enqueued') && !wp_style_is('dacdb-fontawesome', 'enqueued')) {
                wp_enqueue_style('dacdb-fontawesome', 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.2/css/all.css', array(), '6.4.2', 'all');
            }
        }
        //ANCHOR - Process Shortcode
        public function dacdb_plus_function($atts)
        {
            global $public_path, $fDebug, $data_path, $file_path, $options;
            //NOTE - Handle Shortcode Attributes
            extract(
                shortcode_atts(
                    array(
                        'show' => '',
                        'storyid' => '',
                        'catid' => '',
                        'catids' => '',
                        'separator' => '',
                        'display' => '',            // removed
                        'pagetitle' => '',          // added
                        'org' => '',                // depreciated
                        'orgyear' => '',
                        'description' => '',
                        'imgwidth' => '',
                        'folder' => '',             // for documents
                        'sort' => '',               // where clubs meet
                        // committee                // not used
                        'cid' => '',
                        'row' => '',
                        'style' => '',              // New
                        // documents                // not used
                        'icon' => 1,
                        'size' => 1,
                        'date' => 1,
                        'club_id' => '',            // Display club calendar on district website
                        'mailto' => '',
                        'show_headline' => 1,       // added
                        'headline' => '',           // added
                        'story_output_style' => 0,  // added
                        'story_summary' => 0,       // added
                        'story_sep' => 0,           // added
                        'story_image' => 0,         // added
                        'story_open' => 0,          // added
                        'story_share' => 1,         // added
                        'story_social' => 0,        // added
                        'story_limit' => 0,          // added
                        'theme' => ''               // RI Theme Logo Color
                    ),
                    $atts,
                    'dacdb_plus'
                )
            );

            $pagedata = "";     //NOTE -  output for short code need to be returned.  so this variable accumulates the output.  Each short code does their own
            //NOTE -  get the settings here - update widgets at bottom if this changes!!!
            $options = get_mmp_options();
            setDataPath();   //NOTE - ShortCode DACdb or iMembersDB paths

            if ($data_path == '') {
                $pagedata .= "=================================================================<BR>";
                $pagedata .=  "Missing System selection under Plugin Settings!<BR>";
                $pagedata .=  "data_path=" . $data_path;
                $pagedata .=  "=================================================================<BR>";
                $pagedata .=  "<BR>";
            }

            //REVIEW - $subscription = file_get_contents($data_path.'wp_subscription.cfm?AccountID='.$options['dacdb_district'].'&ClubID='.$options['dacdb_club']);
            $xml = simplexml_load_file($data_path . 'wp_subscription2.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club']);  // mrl changed it back to wp_subscription.cfm
            $int = (int)$xml->status;
            if ( $int != 1 ) {
                $pagedata .= "=================================================================<BR>";
                $pagedata .=  "&nbsp; &nbsp;  Missing subscription record. System=" . $options['dacdb_system'] . ", AccountID=" . $options['dacdb_district'] . ", ClubID=" . $options['dacdb_club'] . "<BR>";
                $pagedata .=  "&nbsp; &nbsp;  Please contact: support@memberminderpro.com<BR>";
                $pagedata .=  "data_path=" . $data_path . "<br>";
                $pagedata .=  "subscription=" . $xml->status . "<br>";
                $pagedata .=  "ip_server=" . $_SERVER['SERVER_ADDR'] . "<br>";
                $pagedata .=  "php version=" . phpversion() . "<br>";
                if ( \filter_var( \ini_get('allow_url_fopen'), \FILTER_VALIDATE_BOOLEAN ) ) {
                    $pagedata .=  "allow_url_fopen is configured properly in php.ini". "<br>";
                }
                else {
                    $pagedata .=  "allow_url_fopen is NOT configured properly in php.ini". "<br>";
                }
                if ($fDebug) {
                    $pagedata .=  "&nbsp; &nbsp;  url=" . $data_path . 'wp_subscription2.cfm?AccountId=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . "<BR>";
                }
                $pagedata .=  "=================================================================<BR>";
                $pagedata .=  "<BR>";
            }
           

            
            if ($fDebug) { 
                $districtOrAccount = $options['dacdb_system'] == 'dacdb' ? 'District' : 'Account';
                $pagedata .= '
                <div class="debug"><div class="info-msg"><i class="fa fa-info-circle"></i> <strong>Debug mode <em>ON</em></strong> </div>
                    <h5>' . dacdb_debug_shortcode($GLOBALS['post'], $atts) . '</h5>
                    <pre>
                        <dl>
                            <dt>Plugin Version</dt><dd>' . MMPDB_PLUGIN_VERSION  . '</dd>
                            <dt>' . $districtOrAccount . ' ID</dt><dd>' . $options['dacdb_district'] . '</dd>
                            <dt>Club ID</dt><dd>' . $options['dacdb_club'] . '</dd>
                            <dt>System</dt><dd>' . (isset($options['dacdb_system']) ? $options['dacdb_system'] : 'Not set') . '</dd>
                            <dt>CatID</dt><dd>' . (isset($GLOBALS['catid']) && $GLOBALS['catid'] !== '' ? $GLOBALS['catid'] : 'Not set') . '</dd>
                            <dt>data_path</dt><dd>' . (isset($data_path) ? $data_path : 'Not set') . '</dd>
                            <dt>file_path</dt><dd>' . (isset($file_path) ? $file_path : 'Not set') . '</dd>
                            <dt>public_path</dt><dd>' . (isset($GLOBALS['public_path']) && $GLOBALS['public_path'] !=='' ? $GLOBALS['public_path'] : 'Not set') . '</dd>
                            <dt>subscription</dt><dd>' . (isset($xml->status) ? $xml->status : 'Not set') . '</dd>
                            <dt>ip_server</dt><dd>' . (isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'Not set') . '</dd>
                            <dt>php version</dt><dd>' . (null!==phpversion() ? phpversion() : 'Not set') . '</dd>
                        </dl>
                    </pre>';
                //print("<div><strong>XML Data:</strong><pre>" . print_r($xml, true) . "</pre></div>");
                //print("<div><strong>GLOBALS:</strong><pre>".var_dump($GLOBALS)."</pre></div>");
                //print("<div class=\"info-msg\"><strong>Options: </strong><pre>" . var_dump($options) . "</pre></div>");
                //print("<div class=\"info-msg\"><strong>Show:</strong> <pre>" . var_dump($show) . "</pre></div>");
            }
            try {
                //NOTE - wcm master, club_meetings and meeting_info deprecated
                $show = strtolower(str_replace(' ', '', $show));
                switch ($show) {
                    case 'areas':
                        $pagedata .=  dacdb_areas($atts);
                        break;
                    case 'birthdays':
                        $pagedata .=  dacdb_birthdays($atts);
                        break;
                    case 'calendar':
                        $pagedata .= dacdb_calendar($atts);
                        break;
                    case 'cog':
                        $pagedata .=  dacdb_cog($atts);
                        break;
                    case 'club_meetings':
                        $pagedata .=  dacdb_club_meetings($atts);
                        break;
                    case 'club_presidents':
                        $pagedata .=  dacdb_club_presidents($atts);
                        break;
                    case 'clubleaders':
                        $pagedata .=  dacdb_clubleaders($atts);
                        break;
                    case 'committee':
                        $pagedata .=  dacdb_committee($atts);
                        break;
                    case 'committee_leaders':
                        $pagedata .=  dacdb_committee_leaders($atts);
                        break;
                    case 'committees':
                        $pagedata .=  dacdb_committees($atts);
                        break;
                    case 'events':
                    case 'd_events':
                        // If the case is 'd_events', set type to 'district'
                        if ($show === 'd_events') {
                            $atts['type'] = 'district';
                        }
                        $pagedata .=  dacdb_events($atts);
                        break;
                    case 'd_leaders':
                        $pagedata .=  dacdb_d_leaders($atts);
                        break;
                    case 'dcc':
                        $pagedata .=  dacdb_dcc($atts);
                        break;
                    case 'documents':
                        $pagedata .=  dacdb_documents($atts);
                        break;
                    case 'leaders':
                        $pagedata .=  dacdb_leaders($atts);
                        break;
                    case 'map':
                        $pagedata .=  dacdb_map($atts);
                        break;
                    case 'meeting_info':
                        $pagedata .=  dacdb_meeting_info($atts);
                        break;
                    case 'members':
                        $pagedata .=  dacdb_members($atts);
                        break;
                    case 'newsletter':
                    case 'newsletters':
                        $pagedata .=  dacdb_newsletter($atts);
                        break;
                    case 'sponsors':
                        $pagedata .=  dacdb_sponsors($atts);
                        break;
                    case 'speakers':
                        $pagedata .=  dacdb_speakers($atts);
                        break;
                    case 'stories':
                        $pagedata .= dacdb_stories($atts);
                        break;
                    case 'wcm':
                        $pagedata .=  dacdb_wcm($atts);
                        break;
                    case 'joinforms':
                        $pagedata .=  dacdb_NewMemberFormList($atts);
                        break;
                    case 'ri-theme':
                        $pagedata .=  dacdb_riThemeLogo($atts);
                        break;
                    default:
                        throw new InvalidShortcodeException("Invalid shortcode 'show' value encountered: <em>{$show}</em>");
                        //NOTE - wcm master, club_meetings and meeting_info deprecated
                }
            } catch (InvalidShortcodeException $e) {
                // Handle the exception, such as logging the error or displaying a user-friendly message
                $pagedata .= "<div class=\"error-msg\"><i class=\"fa fa-times-circle\"></i>{$e->getMessage()}</div>";
            }

            return $pagedata;
        }
    }
}

//!SECTION - Class dacdb_plus