<?php
/*
 * Copyright (c) 2025, Tribal Limited
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Zenario, Tribal Limited nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL TRIBAL LTD BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
if (!defined('NOT_ACCESSED_DIRECTLY')) exit('This file may not be directly accessed');

class zenario_common_features__organizer__email_templates extends zenario_common_features {
	
	public function preFillOrganizerPanel($path, &$panel, $refinerName, $refinerId, $mode) {
		//...your PHP code...//
	}
	
	public function fillOrganizerPanel($path, &$panel, $refinerName, $refinerId, $mode) {
		
		if (ze::setting('debug_override_enable')) {
			$sendToDebugAddressOrDontSentAtAll = ze::setting('send_to_debug_address_or_dont_send_at_all');
			$panel['notice']['show'] = true;
			
			if ($sendToDebugAddressOrDontSentAtAll == 'send_to_debug_email_address') {
				$panel['notice']['message'] =
					ze\admin::phrase('Email debug mode is enabled, all emails will be sent to [[email]].',
						['email' => ze::setting('debug_override_email_address')]);
			} elseif ($sendToDebugAddressOrDontSentAtAll == 'dont_send_at_all') {
				$panel['notice']['message'] =
					ze\admin::phrase('Email debug mode is enabled, emails will not be sent at all.');
			}
		}
		
		if ($refinerName == 'email_templates_using_image') {
			$mrg = ze\row::get('files', ['filename'], $refinerId);
			$panel['title'] = ze\admin::phrase('Email templates using the image "[[filename]]"', $mrg);
			$panel['no_items_message'] = ze\admin::phrase('There are no email templates using the image "[[filename]]"', $mrg);
			
			// Hide collection buttons
			$panel['collection_buttons']['create_template']['hidden'] = 
			$panel['collection_buttons']['test']['hidden'] = true;
		}
		
		$panelItemKeys = array_keys($panel['items']);
		
		if ($panelItemKeys) {
			$sql = "
				SELECT t.code, COUNT(etsl.id) AS sent_email_count
				FROM " . DB_PREFIX . "email_template_sending_log AS etsl
				LEFT JOIN " . DB_PREFIX . "email_templates AS t
					ON etsl.email_template_id = t.id
				WHERE t.code IN (" . ze\escape::in($panelItemKeys) . ")
				GROUP BY t.code";
		
			$result = ze\sql::fetchAssocs($sql);
		
			$emailTemplateSentEmailCountArray = [];
		
			if ($result) {
				foreach ($result as $templateSentEmailRow) {
					$emailTemplateSentEmailCountArray[$templateSentEmailRow['code']] = $templateSentEmailRow['sent_email_count'];
				}
			}
		}
		
		
		foreach ($panel['items'] as $K=>$item){
			$body_extract = strip_tags($panel['items'][$K]['body_extract']);
			
			$body_extract_length = 250;
			if (strlen($body_extract) > $body_extract_length) {
				if ($length = strpos($body_extract, ' ', $body_extract_length)) {
					$body_extract_length = $length;
				}
			}
			
			$body_extract_snippet = substr($body_extract, 0, $body_extract_length);
			if (strlen($body_extract) > $body_extract_length) {
				$body_extract_snippet .= '...';
			}
			$panel['items'][$K]['body_extract'] = $body_extract_snippet;
			
			$details = self::checkTemplateIsProtectedAndGetCreatedDetails($K);
			if ($details['protected']) {
				$panel['items'][$K]['protected'] = true;
			}
			
			if (!empty($emailTemplateSentEmailCountArray[$K])) {
				$panel['items'][$K]['sent_email_count'] = $emailTemplateSentEmailCountArray[$K];
			} else {
				$panel['items'][$K]['sent_email_count'] = 0;
			}
		}
	}
	
	public function handleOrganizerPanelAJAX($path, $ids, $ids2, $refinerName, $refinerId) {
		if ((ze::post('action') == 'delete_template') && ze\priv::check('_PRIV_MANAGE_EMAIL_TEMPLATE')) {
			foreach (explode(',',$ids) as $code) {
				$sql = "
					DELETE FROM "
							. DB_PREFIX.  "email_templates
					WHERE 
						code = '". ze\escape::sql($code) . "'";
				ze\sql::update($sql);
				
				ze\contentAdm::removeItemFromPluginSettings('email_template', 0, $code);
			}
		}
	}
	
	public function organizerPanelDownload($path, $ids, $refinerName, $refinerId) {
		//...your PHP code...//
	}
}