<?php
/**
 * DACdb Plus Document Display Function
 *
 * Generates a formatted display of documents and folders from DACdb storage.
 * Supports hierarchical folder navigation, file downloads, and customizable display options.
 *
 * @package    DACdbPlus
 * @subpackage Display
 * @author     Member Minder Pro, LLC
 * @link       https://github.com/memberminderpro/dacdbplus
 * @copyright  2023 Member Minder Pro, LLC
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Displays DACdb documents in a formatted table with optional features
 *
 * @param array $atts {
 *     Optional. Array of display attributes.
 *     
 *     @type string $icon    Whether to show file/folder icons (0|1). Default '0'
 *     @type string $size    Whether to show file sizes (0|1). Default '0'
 *     @type string $date    Whether to show modification dates (0|1). Default '0'
 *     @type string $folder  Initial folder to display. Default empty
 *     @type string $folder2 Secondary folder level to display. Default empty
 * }
 * @return string HTML markup for document display
 */
function dacdb_documents($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    $documents = '';
    $account_id = isset($options['dacdb_district']) ? $options['dacdb_district'] : '';
    $club_id = isset($options['dacdb_club']) ? $options['dacdb_club'] : '';
    $icon = isset($atts['icon']) ? $atts['icon'] : '0';
    $size = isset($atts['size']) ? $atts['size'] : '0';
    $date = isset($atts['date']) ? $atts['date'] : '0';
    $folder = isset($_GET['folder']) ? $_GET['folder'] : (isset($atts['folder']) ? $atts['folder'] : '');
    $folder2 = isset($_GET['folder2']) ? isset($_GET['folder2']) : (isset($atts['folder2']) ? isset($atts['folder2']) : '');
    $level = isset($folder) ? 1 : 0;
    $sort = "";
    $current_url = "";

    if ($fDebug) {
        $documents .= "=================================================================<BR>";
        $documents .= "function dacdb_documents called<br>";
        $documents .= "AccountID=" . (isset($options['dacdb_district']) ? $options['dacdb_district'] : '') . "<br>";
        $documents .= "ClubID=" . (isset($options['dacdb_club']) ? $options['dacdb_club'] : '') . "<br>";
        $documents .= "folder=" . (isset($folder) ? $folder : '') . "<br>";
        $documents .= "icon=" . (isset($icon) ? $icon : '') . "<br>";
        $documents .= "size=" . (isset($size) ? $size : '') . "<br>";
        $documents .= "date=" . (isset($date) ? $date : '') . "<br>";
        $documents .= "current_url=" . (isset($current_url) ? $current_url : '') . "<br>";
        $documents .= "path=" . (isset($file_path) ? $file_path : '') . "<br>";
        $documents .= "url=" . (isset($data_path) ? $data_path : '') . 'documents.php?AccountID=' . (isset($options['dacdb_district']) ? $options['dacdb_district'] : '') . '&ClubID=' . (isset($options['dacdb_club']) ? $options['dacdb_club'] : '') . '&folder=' . (isset($folder) ? $folder : '') . '&folder2=' . (isset($folder2) ? $folder2 : '') . '&sort=' . (isset($sort) ? $sort : '') . "<br>";
        $documents .= "=================================================================<BR>";
    }


    $xml = simplexml_load_file((isset($data_path) ? $data_path : '') . 'documents.php?AccountID=' . (isset($options['dacdb_district']) ? $options['dacdb_district'] : '') . '&ClubID=' . (isset($options['dacdb_club']) ? $options['dacdb_club'] : '') . '&Folder=' . (isset($folder) ? $folder : '') . '&Folder2=' . (isset($folder2) ? $folder2 : '') . '&sort=' . (isset($sort) ? $sort : '') . '&R=' . rand());
    if ($xml->count() == 0) {
        $documents .= 'There are no documents to display at this time.';
        return $documents;
    }


    $counter = 1;
    //
    // Breadcrumbs
    //
    $documents .= '<div class="breadcrumbs"> ';
    if ($level == 1) {
        if ($folder && $folder2 ==  '') {
            $documents .= '' . $folder;
        } elseif ($folder2) {
            $documents .= '
            <a href="' . $current_url . '?folder=' . $folder . '">' . $folder . '</a> > ' . $folder2 . '';
        }
    } else {
        if ($folder && $folder2 ==  '') {
            $documents .= '<a href="' . $file_path . '">Documents</a> > ' . $folder;
        } elseif ($folder2) {
            $documents .= '
            <a href="' . $file_path . '&">Documents</a> >
            <a href="' . $file_path . '&folder=' . $folder . '">' . $folder . '</a> > ' . $folder2 . '';
        }
    }
    $documents .= '</ul></div>';

    //
    // Process all the documents returned
    //

    $icon_header = ($icon == 1) ? '<th width="36">&nbsp;</th>' : '';
    $size_header = ($size == 1) ? '<th data-sort="int">Size</th>' : '';
    $date_header = ($date == 1) ? '<th data-sort="int">Modified</th>' : '';

    $documents .= '<div class="row">
        <div class="dac-col-md-12">
        <table class="table">
            <thead>
            <tr>
            ' . $icon_header . '
            <th data-sort="string">File Name</th>
            ' . $size_header . '
            ' . $date_header . '
            </tr>
            </thead>
            <tbody>';

    if (!empty($xml->document)) {
        foreach ($xml->document as $document) {
            $name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $document->name);

            if ($document->type == 0) {
                if ($folder && $folder2) {
                    // do nothing
                } else {
                    $documents .= '<tr>';
                    if ($icon == 1) {
                        if ($folder) {
                            $documents .= '<td><img src="' . plugins_url('img/folder.png', __FILE__) . '" width="32" style="max-width: 32px !important; width: 32px !important;"></td><td><a href="' . $current_url . '?folder=' . $folder . '&folder2=' . $document->name . '">' . $name . '</a>' . $current_url . '</td>';
                        } else {
                            $documents .= '<td><img src="' . plugins_url('img/folder.png', __FILE__) . '" width="32" style="max-width: 32px !important; width: 32px !important;"></td><td><a href="' . $current_url . '?folder=' . $name . '">' . $name . '</a></td>';
                        }
                    } else {
                        if ($folder) {
                            $documents .= '<td><a href="' . $current_url . '?folder=' . $folder . '&folder2=' . $document->name . '">' . trim($name) . '</a></td>';
                        } else {
                            $documents .= '<td><a href="' . $current_url . '?folder=' . $name . '">' . trim($name) . '</a></td>';
                        }
                    }

                    if ($size == 1) {
                        $documents .= '<td>-</td>';
                    }

                    if ($date == 1) {
                        $documents .= '<td>-</td>';
                    }
                    $documents .= '</tr>';
                    $counter = $counter + 1;
                }
            } else {
                $extension = substr(strrchr($document->name, '.'), 1);
                if ($extension == '') {
                    $extension = '__blank';
                }
                if ($icon == 1) {
                    $documents .= '<tr><td><img src="' . plugins_url('img/16px/' . $extension . '.png', __FILE__) . '" width="32" style="max-width: 32px !important; width: 32px !important;"></td>';
                } else {
                    $documents .= '<tr>';
                }
                // $documents .= '<td><a href="'.$file_path.'.$options['dacdb_district'].'/Downloads/'.$options['dacdb_club'].'/'.$document->name.'" target="__blank">'.$document->name.'</a></td>';

                if ($extension == 'URL') {

                    // $docName = str_replace(" ", "%20",$document->name);

                    if ($folder && $folder2 ==  '') {
                        $getURL = file_get_contents($file_path . $options['dacdb_district'] . '/Downloads/' . rawurlencode($options['dacdb_club']) . '/' . rawurlencode($folder) . '/' . rawurlencode($document->name));
                    } elseif ($folder && $folder2) {
                        $getURL = file_get_contents($file_path . $options['dacdb_district'] . '/Downloads/' . rawurlencode($options['dacdb_club']) . '/' . rawurlencode($folder) . '/' . rawurlencode($folder2) . '/' . rawurlencode($document->name));
                    } else {
                        $getURL = file_get_contents($file_path . $options['dacdb_district'] . '/Downloads/' . rawurlencode($options['dacdb_club']) . '/' . rawurlencode($document->name));
                    }
                    if (strpos($getURL, "http://") === false && strpos($getURL, "https://") === false) {
                        $getURL = 'https://' . $getURL;
                    }
                } else {
                    if ($folder && $folder2 ==  '') {
                        $getURL = $file_path . $options['dacdb_district'] . '/Downloads/' . $options['dacdb_club'] . '/' . $folder . '/' . $document->name;
                    } elseif ($folder && $folder2) {
                        $getURL = $file_path . $options['dacdb_district'] . '/Downloads/' . $options['dacdb_club'] . '/' . $folder . '/' . $folder2 . '/' . $document->name;
                    } else {
                        $getURL = $file_path . $options['dacdb_district'] . '/Downloads/' . $options['dacdb_club'] . '/' . trim($document->name);
                    }
                }

                $documents .= '<td><a href="' . $getURL . '" target="__blank">' . $document->name . '</a></td>';
                if ($size == 1) {
                    $documents .= '<td>' . $document->size . '</td>';
                }
                if ($date == 1) {
                    $documents .= '<td>' . $document->modded . '</td>';
                }
                $documents .= '</tr>';
                $counter = $counter + 1;
            }
        }
    } else {
        $documents .= '<tr><td colspan="4"><strong>There are no documents available at this time.<strong></td></tr>';
    }
    $documents .= '</tbody></table></div></div>';
    return $documents;
}