<?php
/**
 * @copyright (C) 2016 - 2025 Holger Brandt IT Solutions
 * @license GNU/GPL, see license.txt
 * WP Mailster is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * WP Mailster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WP Mailster; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * or see http://www.gnu.org/licenses/.
 */


if ( ! defined( 'ABSPATH' ) ) {
    exit; // Not the droids
}

$log = MstFactory::getLogger();
$model = MstFactory::getThreadsModel();
$totalpages = $model->getTotal() / get_option('posts_per_page', 12);
if( isset($_REQUEST['mailpage']) ) {
	$page = intval($_REQUEST['mailpage']);
} else {
	$page = 1;
}
if( isset($_REQUEST['lid']) ) {
	$listId = sanitize_text_field($_REQUEST['lid']);
    $listId = intval($listId);
} else {
	$listId = "";
}
if( isset($_REQUEST['thread_order']) ) {
	$threadOrder = sanitize_text_field($_REQUEST['thread_order']);
} else {
	$threadOrder = "";
}
if( isset($_REQUEST['pageclass_sfx']) ) {
	$pageclass_sfx = sanitize_text_field($_REQUEST['pageclass_sfx']);
} else {
	$pageclass_sfx = "";
}
if( isset($_REQUEST['title']) ) {
	$menuTitle = sanitize_text_field($_REQUEST['title']);
} else {
	$menuTitle = "";
}

if( isset($_REQUEST['show_mailing_list_name_in_list']) ) {
	$showMailingListNameInList = sanitize_text_field($_REQUEST['show_mailing_list_name_in_list']);
} else {
	$showMailingListNameInList = "";
}
if( isset($_REQUEST['show_mailing_list_name_in_title']) ) {
	$showMailingListNameInTitle = sanitize_text_field($_REQUEST['show_mailing_list_name_in_title']);
} else {
	$showMailingListNameInTitle = "";
}

if( isset($_REQUEST['show_page_heading']) ) {
	$menuParamShowHeading = sanitize_text_field($_REQUEST['show_page_heading']);
} else {
	$menuParamShowHeading = "";
}
if( isset($_REQUEST['page_heading']) ) {
	$menuParamPageHeading = sanitize_text_field($_REQUEST['page_heading']);
} else {
	$menuParamPageHeading = "";
}

$defaultPageHeading = $menuParamShowHeading ? $menuParamPageHeading : '';

$log->debug('Threads view showMailingListNameInList: '.$showMailingListNameInList);

$mailingListUtils = MstFactory::getMailingListUtils();
if($listId>0){
	$mList = $mailingListUtils->getMailingList($listId);
	$list = $mList;
	if($showMailingListNameInTitle){
		$mListName = $mList->name;
		$title = $mListName;
	}else{
		$title = $defaultPageHeading;
	}
}else{
	if($showMailingListNameInTitle){
		$allMailingLists = __('- All Mailing Lists -', 'wp-mailster');
		$title = $allMailingLists;
	}else{
		$title = $defaultPageHeading;
	}
}
$threads = $model->getData($listId, false, $threadOrder, true, $page);
if( isset($_REQUEST['filter_search']) ) {
	$filterSearch = sanitize_text_field($_REQUEST['filter_search']);
} else {
	$filterSearch = "";
}

$filter = ($filterSearch != '' ? '&filter_search='.urlencode($filterSearch) : '');

$currUrl = '?subpage=mailsterthreads&amp;lid=' . $listId . $filter;
$mstImgPath =  plugins_url( '/asset/images/', dirname(__FILE__) );
$dateUtils = MstFactory::getDateUtils();
$threadUtils = MstFactory::getThreadUtils();
$page_sfx_attr = esc_attr($pageclass_sfx);
$formatStr = __('%Y-%M-%D', 'wp-mailster');
?>

<div class="componentheading<?php echo $page_sfx_attr; ?>"><h2><?php echo esc_html($title); ?></h2></div>
<div class="mailster_thread_backlink<?php echo $page_sfx_attr; ?>"><a href="<?php echo get_permalink( get_the_ID() ); ?>"><?php _e( 'Go Back', 'wpsmt-mailster'); ?></a></div>
<form id="adminForm" name="adminForm" method="post" action="<?php echo esc_url($currUrl); ?>">
	<div class="mailster_threads contentpane<?php echo $page_sfx_attr; ?>">
		<div class="input-group searchbox<?php echo $page_sfx_attr; ?> mailster_searchbox">
			<input id="filter_search" name="filter_search" type="text" class="form-control inputbox" placeholder="" value="<?php echo esc_attr($filterSearch); ?>">
			<span class="input-group-btn">
                <input id="filter_search_btn" class="btn btn-default" type="submit" value="<?php _e( 'Search', 'wp-mailster'); ?>"/>
            </span>
		</div>
		<div class="mailster_threads_list_container<?php echo $page_sfx_attr; ?>">
			<table class="mailster_threads_list<?php echo $page_sfx_attr; ?> mailster_std_table table">
				<thead>
				<tr>
					<th class="mailster_threads_list_header_num<?php echo $page_sfx_attr; ?>"><?php _e( 'Num', 'wp-mailster'); ?></th>
					<th class="mailster_threads_list_header_attachments<?php echo $page_sfx_attr; ?>"><?php _e( 'Attachments', 'wp-mailster'); ?></th>
					<th class="mailster_threads_list_header_subject<?php echo $page_sfx_attr; ?>"><?php _e( 'Subject', 'wp-mailster'); ?></th>
					<?php if($showMailingListNameInList): ?><th class="mailster_threads_list_header_mailing_list<?php echo $page_sfx_attr; ?>"><?php _e( 'Mailing list', 'wp-mailster'); ?></th><?php endif; ?>
					<th class="mailster_threads_list_header_date<?php echo $page_sfx_attr; ?>"><?php _e( 'Date', 'wp-mailster');?></th>
				</tr>
				</thead>
				<tbody>
				<?php
				for($i=0;$i<count($threads);$i++){
					$thread = &$threads[$i];
					$backlink = base64_encode($currUrl);
					$detaillink = '?subpage=mailsterthread&id=' . intval($thread->thread_id) . '&bl=' . $backlink;
					$receiveTimestamp = $dateUtils->formatDateAsConfigured($thread->receive_timestamp);
					$hasAttachs = $threadUtils->hasThreadAttachments($thread->thread_id);
                    $attachImg = '<span class="mailster_has_attach" title="' .__( 'Has attachments', 'wp-mailster' ) .'">📎</span>';
					$subject = (is_null($thread->subject) || strlen($thread->subject) == 0) ? '<em>'.__( '(No Subject)', 'wp-mailster' ).'</em>' : $thread->subject;
					?>
					<tr>
						<td class="mailster_threads_list_num<?php echo $page_sfx_attr; ?>"><?php echo ($i+1); ?></td>
						<td class="mailster_threads_list_attachments<?php echo $page_sfx_attr; ?>"><?php echo $hasAttachs ? $attachImg : ' - '; ?></td>
						<td class="mailster_threads_list_subject<?php echo $page_sfx_attr; ?>">
							<a href="<?php echo esc_url($detaillink); ?>"><?php echo esc_html($subject); ?></a>
						</td>
						<?php if($showMailingListNameInTitle): ?><td class="mailster_threads_list_mailing_list<?php echo $page_sfx_attr; ?>"><?php echo esc_html($thread->list_name); ?></td><?php endif; ?>
						<td class="mailster_threads_list_date<?php echo $page_sfx_attr; ?>"><?php echo $receiveTimestamp; ?></td>
					</tr>
					<?php
				}
				?>
				</tbody>
			</table>
			<?php
			$prevpage = $page -1;
			if($prevpage>0) {
				?>
				<a href="<?php echo esc_url($currUrl . "&amp;mailpage=" . $prevpage); ?>"><?php _e("Previous", 'wp-mailster'); ?></a>
			<?php }
			$nextpage = $page+1;
			if($nextpage <= $totalpages) {
				?>
				<a href="<?php echo esc_url($currUrl . "&amp;mailpage=" . $nextpage); ?>"><?php _e("Next", 'wp-mailster'); ?></a>
			<?php } ?>
		</div>
	</div>
</form>