<?php
/**
 * Copyright © 2012 Alcatel-Lucent.
 *
 * See the NOTICE file distributed with this work for additional information regarding copyright ownership. Licensed to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
 */

/**
 * User: David
 * Date: 8/22/12
 * Time: 6:53 PM
 *
 */

require_once APPLICATION_PATH . '/models/LogConfig.class.php';

$logDepthSelections = array(
    '25' => '25',
    '50' => '50',
    '100' => '100',
    '250' => '250'
);

$depthTip = $this->translate("Choose the number of lines to retrieve from the server");

?>
<div class="container">
    <h1><? echo $this->translate("Logs")?></h1>
    <form class="well well-small form-horizontal" action="/logging/index" method="POST">
        <fieldset>
            <label class="control-label" for="log_depth"><strong><? echo $this->translate("Depth")?>: </strong></label>
            <div class="controls">
                <select id="log_depth" name="log_depth" onchange="submitDepth()" rel="tooltip" title="<?=$depthTip?>" data-placement="right">
                    <?php
                    foreach ($logDepthSelections as $depth) {
                        echo '<option';
                        if ($depth == $this->logDepth) {
                            echo " selected";
                        }
                        echo ">" . $depth . "</option>\n";
                    }
                    ?>
                </select>
            </div>
        </fieldset>
    </form>

    <div class="accordion" id="accordion0">
    <?php
    if ($this->logCollection) {
        $i = 0;
        foreach ($this->logCollection as $ip => $sourceArray) {
            $ipSanitized = preg_replace("|\\.|", "_", $ip);?>
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="accordion0" href="#<? echo $ipSanitized;?>">
                        <strong><? echo $ip;?></strong></a>
                </div>
                <div id="<? echo $ipSanitized;?>" class="accordion-body collapse" style="height:<? if($i >= 1){
                echo"0px";
                }else{
                    echo "auto";
                }?>">
                    <div class="accordion-inner">
                        <div class="accordion" id="accordion<? echo $ipSanitized;?>">
                            <?
                            foreach ($sourceArray as $source => $lines) { $ipSource = $ipSanitized . "-" . $source;?>
                                <div class="accordion-group">
                                    <div class="accordion-heading">
                                        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion<? echo $ipSanitized;?>" href="#collapse<? echo $ipSanitized . $source;?>">
                                            <?php $sourceName = !empty(LogConfig::$logSources[$source]) ? LogConfig::$logSources[$source] : $source; ?>
                                            <strong><? echo $sourceName; ?></strong>
                                        </a>
                                    </div>
                                    <div id="collapse<? echo $ipSanitized . $source;?>" class="accordion-body collapse" style="height: 0px; ">
                                        <div class="accordion-inner limit-height">
                                            <? foreach ($lines as $line) {
                                                echo htmlspecialchars($line), "<br/>\n";
                                            }?>
                                        </div>
                                    </div>
                                </div>

                            <?}?>
                        </div>
                    </div>
                </div>
            </div>
        <? $i++;
        }
    } else {?>
        <div class="well" style="background-color: rgb(240, 180, 180); text-align: center;"><strong><? echo $this->translate("No logs to display")?></strong></div>
    <?}?>
    </div>
    <script type="text/javascript">
        function submitDepth() {
            $('#log_depth').closest('form').submit();
        }
    </script>
</div> <!-- /container -->