<?php
/**
 *
 * This file is part of HESK - PHP Help Desk Software.
 *
 * (c) Copyright Klemen Stirn. All rights reserved.
 * https://www.hesk.com
 *
 * For the full copyright and license agreement information visit
 * https://www.hesk.com/eula.php
 *
 */
define('IN_SCRIPT',1);
define('HESK_PATH','../');

/* Get all the required files and functions */
require(HESK_PATH . 'hesk_settings.inc.php');
require(HESK_PATH . 'inc/common.inc.php');
require(HESK_PATH . 'inc/admin_functions.inc.php');
require(HESK_PATH . 'inc/reporting_functions.inc.php');
hesk_load_database_functions();

hesk_session_start();
hesk_dbConnect();
hesk_isLoggedIn();

// Check permissions for this feature
hesk_checkPermission('can_export');

// Just a delete file action?
$delete = hesk_GET('delete');
if (strlen($delete) && preg_match('/^hesk_export_[0-9_\-]+$/', $delete))
{
    hesk_unlink(HESK_PATH.$hesk_settings['cache_dir'].'/'.$delete.'.zip');
    hesk_process_messages($hesklang['fd'], hesk_verifyGoto(), 'SUCCESS');
}

// Load custom fields
require_once(HESK_PATH . 'inc/custom_fields.inc.php');

// Load priorities
require_once(HESK_PATH . 'inc/priorities.inc.php');

// Load statuses
require_once(HESK_PATH . 'inc/statuses.inc.php');

// Set default values
define('CALENDAR',1);
define('MAIN_PAGE',1);
define('LOAD_TABS',1);

$selected = array(
	'w'    => array(0=>'',1=>''),
	'time' => array(1=>'',2=>'',3=>'',4=>'',5=>'',6=>'',7=>'',8=>'',9=>'',10=>'',11=>'',12=>''),
);
$is_all_time = 0;

// Default this month to date
$hesk_settings['datepicker'] = array();

$df = new DateTime("first day of this month");
$date_from = $df->format('Y-m-d');
$hesk_settings['datepicker']['#datefrom']['timestamp'] = $df->getTimestamp();

$dt = new DateTime();
$date_to = $dt->format('Y-m-d');
$hesk_settings['datepicker']['#dateto']['timestamp'] = $dt->getTimestamp();

$input_datefrom = hesk_translate_date_string(date($hesk_settings['format_datepicker_php'], strtotime('last month')));
$input_dateto = hesk_translate_date_string(date($hesk_settings['format_datepicker_php']));

/* Date */
if (!empty($_GET['w']))
{
    $df = hesk_datepicker_get_date( hesk_GET('datefrom') );
    if ($df === false) {
        try {
            $df = new DateTime( hesk_GET('datefrom') );
            $date_from = $df->format('Y-m-d');
            $input_datefrom = hesk_translate_date_string($df->format($hesk_settings['format_datepicker_php']));
        } catch(Exception $e) {
            $date_from = date('Y-m-d', strtotime('last month') );
        }
    } else {
        $date_from = $df->format('Y-m-d');
        $input_datefrom = hesk_translate_date_string($df->format($hesk_settings['format_datepicker_php']));
    }

    $dt = hesk_datepicker_get_date( hesk_GET('dateto') );
    if ($dt === false) {
        try {
            $dt = new DateTime( hesk_GET('dateto') );
            $date_to = $dt->format('Y-m-d');
            $input_dateto = hesk_translate_date_string($dt->format($hesk_settings['format_datepicker_php']));
        } catch(Exception $e) {
            $date_to = date('Y-m-d');
        }
    } else {
        $date_to = $dt->format('Y-m-d');
        $input_dateto = hesk_translate_date_string($dt->format($hesk_settings['format_datepicker_php']));
    }

    if ($date_from > $date_to)
    {
        $tmp = $date_from;
        $tmp2 = $input_datefrom;

        $date_from = $date_to;
        $input_datefrom = $input_dateto;

        $date_to = $tmp;
        $input_dateto = $tmp2;

        $note_buffer = $hesklang['datetofrom'];

        $df2 = $df;
        $df = $dt;
        $dt = $df2;
        unset($df2);
    }

    if ($date_to > date('Y-m-d'))
    {
        $dt = new DateTime();
        $date_to = $dt->format('Y-m-d');
        $input_dateto = hesk_translate_date_string(date($hesk_settings['format_datepicker_php']));
    }

    if ($df instanceof DateTime) {
        $hesk_settings['datepicker']['#datefrom']['timestamp'] = $df->getTimestamp();
    } elseif (isset($hesk_settings['datepicker']['#datefrom']['timestamp'])) {
        unset($hesk_settings['datepicker']['#datefrom']['timestamp']);
    }
    if ($dt instanceof DateTime) {
        $hesk_settings['datepicker']['#dateto']['timestamp'] = $dt->getTimestamp();
    } elseif (isset($hesk_settings['datepicker']['#dateto']['timestamp'])) {
        unset($hesk_settings['datepicker']['#dateto']['timestamp']);
    }
	$selected['w'][1]='checked="checked"';
    $selected['time'][3]='selected="selected"';
}
else
{
	$selected['w'][0]='checked="checked"';
	$_GET['time'] = intval( hesk_GET('time', 3) );

    switch ($_GET['time'])
    {
    	case 1:
			/* Today */
			$date_from = date('Y-m-d');
			$date_to = $date_from;
			$selected['time'][1]='selected="selected"';
            $is_all_time = 1;
        break;

    	case 2:
			/* Yesterday */
			$date_from = date('Y-m-d',mktime(0, 0, 0, date("m"), date("d")-1, date("Y")));
			$date_to = $date_from;
			$selected['time'][2]='selected="selected"';
            $is_all_time = 1;
        break;

    	case 4:
			/* Last month */
			$date_from = date('Y-m-d',mktime(0, 0, 0, date("m")-1, 1, date("Y")));
			$date_to = date('Y-m-d',mktime(0, 0, 0, date("m"), 0, date("Y")));
			$selected['time'][4]='selected="selected"';
        break;

    	case 5:
			/* Last 30 days */
			$date_from = date('Y-m-d',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
			$date_to = date('Y-m-d');
			$selected['time'][5]='selected="selected"';
        break;

    	case 6:
			/* This week */
			list($date_from,$date_to)=dateweek(0);
            $date_to = date('Y-m-d');
			$selected['time'][6]='selected="selected"';
        break;

    	case 7:
			/* Last week */
			list($date_from,$date_to)=dateweek(-1);
			$selected['time'][7]='selected="selected"';
        break;

    	case 8:
			/* This business week */
			list($date_from,$date_to)=dateweek(0,1);
            $date_to = date('Y-m-d');
			$selected['time'][8]='selected="selected"';
        break;

    	case 9:
			/* Last business week */
			list($date_from,$date_to)=dateweek(-1,1);
			$selected['time'][9]='selected="selected"';
        break;

    	case 10:
			/* This year */
			$date_from = date('Y').'-01-01';
			$date_to = date('Y-m-d');
			$selected['time'][10]='selected="selected"';
        break;

    	case 11:
			/* Last year */
			$date_from = date('Y')-1 . '-01-01';
			$date_to = date('Y')-1 . '-12-31';
			$selected['time'][11]='selected="selected"';
        break;

    	case 12:
			/* All time */
			$date_from = hesk_getOldestDate();
			$date_to = date('Y-m-d');
			$selected['time'][12]='selected="selected"';
            $is_all_time = 1;
        break;

        default:
        	$_GET['time'] = 3;
			$selected['time'][3]='selected="selected"';
    }

}

unset($tmp);

// Start SQL statement for selecting tickets
$sql = "SELECT `tickets`.*, `requester`.`name` AS `name`
    FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` AS `tickets`
    LEFT JOIN `".hesk_dbEscape($hesk_settings['db_pfix'])."ticket_to_collaborator` AS `w` ON (`tickets`.`id` = `w`.`ticket_id` AND `w`.`user_id` = ".intval($_SESSION['id']).")
    LEFT JOIN `".hesk_dbEscape($hesk_settings['db_pfix'])."ticket_to_customer` AS `ticket_to_customer`
        ON `tickets`.`id` = `ticket_to_customer`.`ticket_id`
        AND `ticket_to_customer`.`customer_type` = 'REQUESTER'
    LEFT JOIN `".hesk_dbEscape($hesk_settings['db_pfix'])."customers` AS `requester`
        ON `ticket_to_customer`.`customer_id` = `requester`.`id` 
    WHERE ";

// Some default settings
$archive = array(1=>0,2=>0);
$s_my = array(1=>1,2=>1);
$s_ot = array(1=>1,2=>1);
$s_un = array(1=>1,2=>1);

// --> TICKET CATEGORY
$category = intval( hesk_GET('category', 0) );

// Make sure user has access to this category
if ($category && hesk_okCategory($category, 0) )
{
	$sql .= " `category`='{$category}' ";
}
// No category selected, show only allowed categories
else
{
	$sql .= hesk_myCategories();
}

// Show only tagged tickets?
if ( ! empty($_GET['archive']) )
{
	$archive[1]=1;
	$sql .= " AND `archive`='1' ";
}

// Ticket owner preferences
$fid = 1;
require(HESK_PATH . 'inc/assignment_search.inc.php');

// --> TICKET STATUS
$status = $hesk_settings['statuses'];

foreach ($status as $k => $v)
{
	if (empty($_GET['s'.$k]))
    {
    	unset($status[$k]);
    }
}

// How many statuses are we pulling out of the database?
$tmp = count($status);

// Do we need to search by status?
if ( $tmp < count($hesk_settings['statuses']) )
{
	// If no statuses selected, show all
	if ($tmp == 0)
	{
		$status = $hesk_settings['statuses'];
	}
	else
	{
		// Add to the SQL
		$sql .= " AND `status` IN ('" . implode("','", array_keys($status) ) . "') ";
	}
}

// --> TICKET PRIORITY
$possible_priority = hesk_possible_priorities();
$priority = $possible_priority;

foreach ($priority as $k => $v)
{
	if (empty($_GET['p'.$k]))
    {
    	unset($priority[$k]);
    }
}

// How many priorities are we pulling out of the database?
$tmp = count($priority);

// Create the SQL based on the number of priorities we need
if ($tmp == 0 || $tmp == 4)
{
	// Nothing or all selected, no need to modify the SQL code
    $priority = $possible_priority;
}
else
{
	// A custom selection of priorities
	$sql .= " AND `priority` IN ('" . implode("','", array_keys($priority) ) . "') ";
}

// Prepare variables used in search and forms
require_once(HESK_PATH . 'inc/prepare_ticket_export.inc.php');

////////////////////////////////////////////////////////////////////////////////

// Can view tickets that are unassigned or assigned to others?
$can_view_ass_others = hesk_checkPermission('can_view_ass_others',0);
$can_view_unassigned = hesk_checkPermission('can_view_unassigned',0);

// Category options
$category_options = '';
$my_cat = array();
$res2 = hesk_dbQuery("SELECT `id`, `name` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."categories` WHERE " . hesk_myCategories('id') . " ORDER BY `cat_order` ASC");
while ($row=hesk_dbFetchAssoc($res2))
{
	$my_cat[$row['id']] = hesk_msgToPlain($row['name'], 1, 0);
	$row['name'] = (hesk_mb_strlen($row['name']) > 50) ? hesk_mb_substr($row['name'],0,50) . '...' : $row['name'];
	$cat_selected = ($row['id'] == $category) ? 'selected="selected"' : '';
	$category_options .= '<option value="'.$row['id'].'" '.$cat_selected.'>'.$row['name'].'</option>';
}

// Generate export file
if (isset($_GET['w'])) {
    if (defined('HESK_DEMO')) {
        hesk_process_messages($hesklang['ddemo'], 'export.php', 'NOTICE');
    } else {
        require_once(HESK_PATH . 'inc/export_functions.inc.php');
        list($success_msg, $tickets_exported) = hesk_export_to_XML($sql, false, $history, $replies);
    }
}

/* Print header */
require_once(HESK_PATH . 'inc/header.inc.php');

/* Print main manage users page */
require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');

/* This will handle error, success and notice messages */
hesk_handle_messages();

// If an export was generated, show the link to download
if (isset($success_msg))
{
	if ($tickets_exported > 0)
	{
		hesk_show_success($success_msg);
	}
	else
	{
		hesk_show_notice($hesklang['n2ex']);
	}
}
?>
<div class="main__content reports">
    <h2>
        <?php echo $hesklang['export']; ?>
        <div class="tooltype right out-close">
            <svg class="icon icon-info">
                <use xlink:href="<?php echo HESK_PATH; ?>img/sprite.svg#icon-info"></use>
            </svg>
            <div class="tooltype__content">
                <div class="tooltype__wrapper">
                    <?php echo $hesklang['export_intro']; ?>
                </div>
            </div>
        </div>
    </h2>
    <form name="showt" action="export.php" method="get" aria-label="<?php echo $hesklang['export']; ?>">
        <div class="reports__range pl0">
            <h4><?php echo $hesklang['dtrg']; ?></h4>
            <div class="reports__range_form form">
                <div class="radio-list">
                    <div class="radio-custom">
                        <input type="radio" name="w" value="0" id="w0" <?php echo $selected['w'][0]; ?>>
                        <label for="w0" aria-label="<?php echo $hesklang['dtrg']; ?>">&nbsp;</label>
                        <div class="dropdown-select center out-close">
                            <select name="time" onclick="document.getElementById('w0').checked = true" onchange="document.getElementById('w0').checked = true" style="margin-top:5px;margin-bottom:5px;">
                                <option value="1" <?php echo $selected['time'][1]; ?>><?php echo $hesklang['r1']; ?> (<?php echo $hesklang['d'.date('w')]; ?>)</option>
                                <option value="2" <?php echo $selected['time'][2]; ?>><?php echo $hesklang['r2']; ?> (<?php echo $hesklang['d'.date('w',mktime(0, 0, 0, date('m'), date('d')-1, date('Y')))]; ?>)</option>
                                <option value="3" <?php echo $selected['time'][3]; ?>><?php echo $hesklang['r3']; ?> (<?php echo $hesklang['m'.date('n')]; ?>)</option>
                                <option value="4" <?php echo $selected['time'][4]; ?>><?php echo $hesklang['r4']; ?> (<?php echo $hesklang['m'.date('n',mktime(0, 0, 0, date('m')-1, 1, date('Y')))]; ?>)</option>
                                <option value="5" <?php echo $selected['time'][5]; ?>><?php echo $hesklang['r5']; ?></option>
                                <option value="6" <?php echo $selected['time'][6]; ?>><?php echo $hesklang['r6']; ?></option>
                                <option value="7" <?php echo $selected['time'][7]; ?>><?php echo $hesklang['r7']; ?></option>
                                <option value="8" <?php echo $selected['time'][8]; ?>><?php echo $hesklang['r8']; ?></option>
                                <option value="9" <?php echo $selected['time'][9]; ?>><?php echo $hesklang['r9']; ?></option>
                                <option value="10" <?php echo $selected['time'][10]; ?>><?php echo $hesklang['r10']; ?> (<?php echo date('Y'); ?>)</option>
                                <option value="11" <?php echo $selected['time'][11]; ?>><?php echo $hesklang['r11']; ?> (<?php echo date('Y',mktime(0, 0, 0, date('m'), date('d'), date('Y')-1)); ?>)</option>
                                <option value="12" <?php echo $selected['time'][12]; ?>><?php echo $hesklang['r12']; ?></option>
                            </select>
                        </div>
                    </div>
                    <div class="radio-custom">
                        <input type="radio" name="w" value="1" id="w1" <?php echo $selected['w'][1]; ?>>
                        <label for="w1" aria-label="<?php echo $hesklang['cdr']; ?>">&nbsp;</label>
                        <?php echo $hesklang['from']; ?>
                        <section class="param calendar" style="margin-left: 10px; margin-right: 10px">
                            <div class="calendar--button">
                                <button type="button" onclick="document.getElementById('w1').checked = true" aria-label="<?php echo $hesklang['date'] . ' ' . $hesklang['from']; ?>">
                                    <svg class="icon icon-calendar">
                                        <use xlink:href="<?php echo HESK_PATH; ?>img/sprite.svg#icon-calendar"></use>
                                    </svg>
                                </button>
                                <input name="datefrom"
                                       id="datefrom"
                                    <?php if ($input_datefrom) {echo 'value="'.$input_datefrom.'"';} ?>
                                       type="text" class="datepicker" aria-label="<?php echo $hesklang['date'] . ' ' . $hesklang['from']; ?>">
                            </div>
                            <div class="calendar--value" <?php echo ($input_datefrom ? 'style="display: block"' : ''); ?>>
                                <span><?php echo $input_datefrom; ?></span>
                                <i class="close">
                                    <svg class="icon icon-close">
                                        <use xlink:href="<?php echo HESK_PATH; ?>img/sprite.svg#icon-close"></use>
                                    </svg>
                                </i>
                            </div>
                        </section>
                        <?php echo $hesklang['to']; ?>
                        <section class="param calendar" style="margin-left: 10px;">
                            <div class="calendar--button">
                                <button type="button" onclick="document.getElementById('w1').checked = true" aria-label="<?php echo $hesklang['date'] . ' ' . $hesklang['to']; ?>">
                                    <svg class="icon icon-calendar">
                                        <use xlink:href="<?php echo HESK_PATH; ?>img/sprite.svg#icon-calendar"></use>
                                    </svg>
                                </button>
                                <input name="dateto"
                                       id="dateto"
                                    <?php if ($input_dateto) {echo 'value="'.$input_dateto.'"';} ?>
                                       type="text" class="datepicker" aria-label="<?php echo $hesklang['date'] . ' ' . $hesklang['to']; ?>">
                            </div>
                            <div class="calendar--value" <?php echo ($input_dateto ? 'style="display: block"' : ''); ?>>
                                <span><?php echo $input_dateto; ?></span>
                                <i class="close">
                                    <svg class="icon icon-close">
                                        <use xlink:href="<?php echo HESK_PATH; ?>img/sprite.svg#icon-close"></use>
                                    </svg>
                                </i>
                            </div>
                        </section>
                    </div>
                </div>
            </div>
        </div>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['status']; ?></h3>
            <?php
            hesk_get_status_checkboxes($status);
            ?>
        </section>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['priority']; ?></h3>
            <?php
                hesk_get_priority_checkboxes($priority);
            ?>
        </section>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['assigned_to']; ?></h3>
            <div class="checkbox-custom">
                <input type="checkbox" name="s_my" id="s_my" value="1" <?php if ($s_my[1]) echo 'checked'; ?>>
                <label for="s_my"><?php echo $hesklang['s_my']; ?></label>
            </div>
            <?php
            if ($can_view_unassigned)
            {
                ?>
                <div class="checkbox-custom">
                    <input type="checkbox" name="s_un" id="s_un" value="1" <?php if ($s_un[1]) echo 'checked'; ?>>
                    <label for="s_un"><?php echo $hesklang['s_un']; ?></label>
                </div>
                <?php
            }

            if ($can_view_ass_others)
            {
                ?>
                <div class="checkbox-custom">
                    <input type="checkbox" name="s_ot" id="s_ot" value="1" <?php if ($s_ot[1]) echo 'checked'; ?>>
                    <label for="s_ot"><?php echo $hesklang['s_ot']; ?></label>
                </div>
                <?php
            }
            ?>
            <div class="checkbox-custom">
                <input type="checkbox" name="archive" id="archive" value="1" <?php if ($archive[1]) echo 'checked'; ?>>
                <label for="archive"><?php echo $hesklang['disp_only_archived']; ?></label>
            </div>
        </section>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['sort_by']; ?></h3>
            <div class="radio-list">
                <div class="radio-custom">
                    <input type="radio" name="sort" id="sort_priority" value="priority" <?php if ($sort == 'priority') {echo 'checked';} ?>>
                    <label for="sort_priority"><?php echo $hesklang['priority']; ?></label>
                </div>
                <div class="radio-custom">
                    <input type="radio" name="sort" id="sort_lastchange" value="lastchange" <?php if ($sort == 'lastchange') {echo 'checked';} ?>>
                    <label for="sort_lastchange"><?php echo $hesklang['last_update']; ?></label>
                </div>
                <div class="radio-custom">
                    <input type="radio" name="sort" id="sort_name" value="name" <?php if ($sort == 'name') {echo 'checked';} ?>>
                    <label for="sort_name"><?php echo $hesklang['name']; ?></label>
                </div>
                <div class="radio-custom">
                    <input type="radio" name="sort" id="sort_subject" value="subject" <?php if ($sort == 'subject') {echo 'checked';} ?>>
                    <label for="sort_subject"><?php echo $hesklang['subject']; ?></label>
                </div>
                <div class="radio-custom">
                    <input type="radio" name="sort" id="sort_status" value="status" <?php if ($sort == 'status') {echo 'checked';} ?>>
                    <label for="sort_status"><?php echo $hesklang['status']; ?></label>
                </div>
                <div class="radio-custom">
                    <input type="radio" name="sort" id="sort_id" value="id" <?php if ($sort == 'id') {echo 'checked';} ?>>
                    <label for="sort_id"><?php echo $hesklang['sequentially']; ?></label>
                </div>
            </div>
        </section>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['category']; ?></h3>
            <div class="dropdown-select center out-close">
                <select name="category">
                    <option value="0" ><?php echo $hesklang['any_cat']; ?></option>
                    <?php echo $category_options; ?>
                </select>
            </div>
        </section>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['order']; ?></h3>
            <div class="radio-list">
                <div class="radio-custom">
                    <input type="radio" name="asc" id="asc_1" value="1" <?php if ($asc) {echo 'checked';} ?>>
                    <label for="asc_1"><?php echo $hesklang['ascending']; ?></label>
                </div>
                <div class="radio-custom">
                    <input type="radio" name="asc" id="asc_0" value="0" <?php if (!$asc) {echo 'checked';} ?>>
                    <label for="asc_0"><?php echo $hesklang['descending']; ?></label>
                </div>
            </div>
        </section>
        <section class="reports__checkbox">
            <h3><?php echo $hesklang['opt']; ?></h3>
            <div class="checkbox-custom">
                <input type="checkbox" name="history" id="history" value="1" <?php if ($history) echo 'checked'; ?>>
                <label for="history"><?php echo $hesklang['ex_history']; ?></label>
            </div>
            <div class="checkbox-custom">
                <input type="checkbox" name="replies" id="replies" value="1" <?php if ($replies) echo 'checked'; ?>>
                <label for="replies"><?php echo $hesklang['ex_replies']; ?></label>
            </div>
        </section>
        <div class="reports__export">
            <input type="hidden" name="cot" value="1">
            <button class="btn btn-full" ripple="ripple" data-action="reports-export"><?php echo $hesklang['export_btn']; ?></button>
        </div>
    </form>
</div>

<?php
require_once(HESK_PATH . 'inc/footer.inc.php');
exit();
?>
