<!-- wblogbook/index.phtml -->

<script type="text/javascript" src="<?php echo $this->baseUrl , '/scripts/jquery-ui.datepicker-', $this->language, '.js';?>"></script>
<script type="text/javascript">
$(function() {
    $("#tabs").tabs();
});
</script>

<script type="text/javascript">
$(function() {
    $('#date_begin').datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true
    });
    $('#date_end').datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true
    });
    $('#date_begin').datepicker('setDate', '-1m' );
    $('#date_begin').datepicker('option', {dateFormat: $.datepicker.ISO_8601} );
});
</script>

<script type="text/javascript">
<!--
	// для показа версии для печати
	function triggerPrintable()	{
		if ( document.getElementById("printable_by_date").value = "0" )	{
			document.getElementById("printable_by_date").value = "1";
		} else {
			document.getElementById("printable_by_date").value = "0";
		}

		if ( document.getElementById("printable_by_id").value = "0" )	{
			document.getElementById("printable_by_id").value = "1";
		} else {
			document.getElementById("printable_by_id").value = "0";
		}
		if ( document.getElementById("printable_by_text").value = "0" )	{
			document.getElementById("printable_by_text").value = "1";
		} else {
			document.getElementById("printable_by_text").value = "0";
		}

		return;
	}
//-->
</script>

<script type="text/javascript">
<!--
	// для сортировок
	function triggerSortOrder()	{
		if ( document.getElementById("sortorder_by_date").value = "DESC" )	{
			document.getElementById("sortorder_by_date").value = "ASC";
		} else {
			document.getElementById("sortorder_by_date").value = "DESC";
		}

		if ( document.getElementById("sortorder_by_id").value = "DESC" )	{
			document.getElementById("sortorder_by_id").value = "ASC";
		} else {
			document.getElementById("sortorder_by_id").value = "DESC";
		}
		return;
	}
//-->
</script>

<h1><?php echo $this->escape($this->title); ?></h1>

<div style="width: 200px; margin-top: 30px; margin-bottom: 20px;">
<form method="POST" action="<?php echo $this->baseUrl ?>/wblogbook/add">
	<input type="submit" name="name1" value="<?php print $this->translate->_("+ Add New Record"); ?>" class="ui-button ui-state-default ui-corner-all">
</form>
</div>

<div class="filter-form" style="width: 600px; text-align: left; margin-top: 40px; margin-bottom: 50px;">
<div id="tabs">
<ul>
	<li><a href="#tabs1"><?php print $this->translate->_("Filter by date"); ?></a></li>
    <li><a href="#tabs2"><?php print $this->translate->_("Filter by Id"); ?></a></li>
    <li><a href="#tabs3"><?php print $this->translate->_("Text Search"); ?></a></li>
</ul>

<!-- filter by date -->
<div id="tabs1">
<form name="filter_by_date" id="filter_by_date" method="POST"  action="<?php echo $this->baseUrl ?>/wblogbook/filterbydate">
	<?php print $this->translate->_("Date range : from"); ?>
	<?php echo $this->formText('date_begin', $this->escape($this->date_begin), array('size' => 10));?>
	&nbsp;&nbsp;<?php print $this->translate->_("&nbsp; to"); ?>
	<?php echo $this->formText('date_end', $this->escape($this->date_end), array('size' => 10)) ?>
   	&nbsp;&nbsp;&nbsp;
	<?php echo $this->formHidden('printable_by_date', '0', null); ?>
	<?php echo $this->formHidden('sortorder_by_date', 'DESC', null); ?>
	<br>
    <?php
        echo $this->formSubmit('filter1', $this->translate->_('Apply filter'),
            array('style' => 'margin-top: 15px; margin-bottom: 20px;',
            'class' => "ui-button ui-state-default ui-corner-all") );
    ?>
</form>
</div>

<!-- filter by id -->
<div id="tabs2">
<form name="filter_by_id" id="filter_by_id" method="POST" action="<?php echo $this->baseUrl ?>/wblogbook/filterbyid/">
	<?php print $this->translate->_("Id range : from"); ?>
	<?php echo $this->formText('id_begin', $this->escape($this->id_begin), array('size' => 5, 'maxlength' => 15)); ?>
	&nbsp;
	<?php print $this->translate->_("&nbsp; to"); ?>
	<?php echo $this->formText('id_end', $this->escape($this->id_end), array('size' => 5, 'maxlength' => 15)); ?>
	&nbsp;
	<?php echo $this->formHidden('printable_by_id', '0', null); ?>
	<?php echo $this->formHidden('sortorder_by_id', 'DESC', null); ?>
	<br>
    <?php
        echo $this->formSubmit('filter2', $this->translate->_('Apply filter'),
            array('style' => 'margin-top: 15px; margin-bottom: 20px;',
                'class' => "ui-button ui-state-default ui-corner-all") );
    ?>
</form>
</div>

<!-- full text search -->
<div id="tabs3">
<form name="filter_by_text" id="search_text" method="POST" action="<?php echo $this->baseUrl ?>/wblogbook/searchtext/">
	<?php print $this->translate->_("Text"); ?>
	<?php echo $this->formText('id_text', $this->escape($this->id_text), array('size' => 50, 'maxlength' => 250)); ?>
	&nbsp;
	<?php echo $this->formHidden('printable_by_text', '0', null); ?>
	<?php echo $this->formHidden('sortorder_by_text', 'ASC', null); ?>
    <?php
        echo $this->formSubmit('filter3', $this->translate->_('Search'),
            array('style' => 'margin-top: 15px; margin-bottom: 20px;',
                'class' => "ui-button ui-state-default ui-corner-all") );
    ?>
</form>
</div>

<img src="<?php echo $this->baseUrl; ?>/images/printer1.png"
       alt="<?php print $this->translate->_("View a printable version"); ?>"
       title="<?php print $this->translate->_("View a printable version"); ?>" align="absmiddle" border="0" height="16" hspace="1" vspace="1" width="16">
<input type="checkbox" name="printable" value="printable" onchange="triggerPrintable()">
<?php print $this->translate->_("View a printable version"); ?>
<br>
<img src="<?php echo $this->baseUrl; ?>/images/1x1.png"
       alt="<?php print $this->translate->_("To sort in ascending/descending order"); ?>"
       title="<?php print $this->translate->_("To sort in ascending/descending order"); ?>" align="absmiddle" border="0" height="16" hspace="1" vspace="1" width="16">
<input type="checkbox" name="sortorder" value="sortorder" onchange="triggerSortOrder()">
<?php print $this->translate->_("Sort in ascending order"); ?>

</div> <!-- END id="tabs" -->
</div> <!-- END class="filter-form"" -->

<?php if ($this->result): ?>

<table id="box-table-wblogbook" border="0">
<thead>
<tr>
     <th scope="col" width="15%"> <?php print $this->translate->_("Date"); ?> </th>
     <th scope="col"> <?php print $this->translate->_("Text"); ?> </th>
     <th scope="col" width="5%"> <?php print $this->translate->_("Type"); ?> </th>

	<th scope="col" width="5%"> <?php print $this->translate->_("Last modified"); ?> </th>
     <th scope="col" width="3%"> Id </th>
</tr>
</thead>
<tbody>
<?php foreach($this->result as $line) : ?>

<tr>
	<td valign="top">
		<?php
			$str = $line['logdatecreate'];
			if ($line['logisdel'] != 0) { $str = '<s>' . $str . '</s>'; }; // record deleted
			echo $str;
		?>
	</td>
	<td>
		<?php
			$str = $line['logtxt'];

			$pattern = "/(BACULA_JOBID=)([0-9]*)/i";
            $replacement = "<a href=\"" . $this->baseUrl . "/job/detail/jobid/\\2\">Bacula JobId \\2</a>";
            $str = preg_replace($pattern, $replacement, $str);

            $pattern = "/(LOGBOOK_ID=)([0-9]*)/i";
            $replacement = "<a href=\"" . $this->baseUrl . "/wblogbook/filterbyid/id_begin/\\2\">Logbook Id \\2</a>";
            $str = preg_replace($pattern, $replacement, $str);

			// each OS have different ASCII chars for linebreak:
			// windows = \r\n
			// unix = \n
			// mac = \r
			$str = str_replace(array("\r\n", "\r", "\n"), "<br>", $str);
			//$str = nl2br($str);

			if ($line['logisdel'] != 0) { $str = '<s>' . $str . '</s>'; }; // record deleted

			echo $str;

			echo '<br><br>';

			echo '<form method="post" action="', $this->baseUrl, '/wblogbook/modify">',
			    $this->formHidden('logid', $line['logid'], null),
			    $this->formSubmit('modify', $this->translate->_('Modify Entry') ),
			    '</form><br>';
		?>
	</td>

	<?php
		if ($line['logisdel'] != 0) {
			// record deleted
			echo '<td valign="top">', $this->translate->_("Deleted"), '</td>';
		} else {

			switch ($line['typeid'])
			{
				case '20' : $class = 'class="ok"'; break;
				case '30' : $class = 'class="warn"'; break;
				case '255': $class = 'class="err"';  break;
				default: $class = '';
			}
			echo '<td ', $class, ' valign="top">', $this->translate->_($line['typedesc']), '</td>';
		}
	?>

	<td valign="top" align="center">
		<?php
		if ( isset($line['logdatelast']) )	{
				echo $line['logdatelast'];
			} else {
				echo '-';
			}
		?>
	</td>

	<td valign="top" align="center">
		<?php echo $line['logid']; ?>
	</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<?php else: ?>
<div class="ui-widget" style="float: left;">
	<div class="ui-state-highlight ui-corner-all" style="padding: 0pt 0.7em; margin-top: 20px;">
		<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span>
		<strong><?php print $this->translate->_("No records found."); ?></strong></p>
	</div>
</div>
<div style="clear: both;"><!--  --></div>
<?php endif; ?>