<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005-2006 ARMIA INC                                    |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts EasyCreate 1.1                 |
// +----------------------------------------------------------------------+
// | Authors: sudheesh<sudheesh.s@armia.com>              		          |
// |          									                          |
// +----------------------------------------------------------------------+
$curTab = 'template_manager';
include "../includes/session.php";
include "../includes/config.php";
include "../includes/function.php";
include "includes/adminheader.php";
if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
} 
if ($_POST["delact"] == "delete") {

    /* check the template already in use.*/
    /*check whether the templateid exist in tbl_tempsite_mast*/

    $qry = "select * from tbl_tempsite_mast where ntemplate_id='" . addslashes($_POST["delid"]) . "'";
    $res1 = mysql_query($qry);
    if (mysql_num_rows($res1) > 0) {
        $message = "Template could not be deleted since it is in use!." ;
    } else {
        /*check whether the templateid exist in tbl_site_mast*/
        $qry1 = "select * from tbl_site_mast where ntemplate_id='" . addslashes($_POST["delid"]) . "'";
        $res2 = mysql_query($qry1);
        if (mysql_num_rows($res2) > 0) {
            $message = "Template could not be deleted since it is in use!" ;
        } else {
            // remove template folder
            if (is_dir("../" . $_SESSION["session_template_dir"] . "/" . $_POST["delid"])) {
                remove_dir("../" . $_SESSION["session_template_dir"] . "/" . $_POST["delid"]);
            }
            $sql = "delete from tbl_template_mast  where ntemplate_mast='" . addslashes($_POST["delid"]) . "'" ;
            mysql_query($sql);
            $message = "Template deleted successfully!" ;
        }
    }
} 
include "includes/adminheader.php";

?>
 
<form name="frmTemplate" method=post>
    <input name="postback" type="hidden" id="postback">
    <input name="delact" type="hidden" id="delact">
    <input name="delid" type="hidden" id="delid">

    <?php
//========================================================
   
   
    $sql="select tm.ntemplate_mast,tm.vthumpnail,tm.ncat_id,Date_Format(tm.ddate,'%m/%d/%Y') as date ,
   tm.vtype,tc.vcat_name,T.theme_image_thumb,T.theme_id from tbl_template_mast tm left join tbl_template_category tc on tm.ncat_id=tc.ncat_id LEFT JOIN tbl_template_themes AS T ON T.temp_id=tm.ntemplate_mast where 1=1 AND theme_default=1 ". $qryopt." order by ntemplate_mast DESC ";
   
    $totalrows = mysql_num_rows(mysql_query($sql));
     $rs = mysql_query($sql) or die(mysql_error());

    $linkArray = array( TEMP_MANAGER     =>'admin/templatemanager.php',
            MANAGE_TEMP  =>'admin/templatelisting.php');

    ?>
    <script language="javascript" type="text/javascript" src="../js/jquery.js"></script>
    <script language="javascript" src="../js/modal.popup.js"></script>
    <script language="javascript" src="../js/validations.js"></script>
    <div class="admin-pnl">
        <?php echo getBreadCrumb($linkArray); ?>
        <h2>Manage Templates</h2>
        <table width="100%"  border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td  valign="top" align=center>
                    <div class=errormessage><?php echo $message?>&nbsp;</div>
                    <table width="100%"  border="0" cellspacing="0" cellpadding="0">

                        <tr>
                            <td class="">
                                <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="">
                                    <tr class=>
                                        <td width="100%">

                                            <div class="admin-search-pnl">
                                                <label>Search</label>
                                                <select name="cmbSearchType" class="selectbox" style="width: 120px;">
                                                    <option value="category"
                                                    <?php  if($cmbSearchType == "category" || $cmbSearchType == "") {
                                                                echo("selected");
                                                            } ?>>Category</option>

                                                    <option value="date"  <?php
                                                            if($cmbSearchType == "date" ) {
                                                                echo("selected");
                                                            } ?>>Date</option>
                                                </select>

                                                &nbsp;
                                                <input style="width:300px;" type="text" name="txtSearch" size="20" maxlength="50" value="<?php  echo(htmlentities($txtSearch)); ?>"
                                                       onKeyPress="if(window.event.keyCode == '13'){ return false; }"
                                                       class="textbox">

                                                <a href="javascript:clickSearch();" class="btn05">Search</a>
                                                <div class="clear"></div>
                                            </div>
                                        </td>
                                    </tr>
                                </table>


                                <table width="100%"  border="0" cellpadding="5" cellspacing="0" class="admin-table-list">
                                    <tr>
                                        <th width="20%" valign="top">Category</th>
                                        <th width="30%" >Thumbnail</th>
                                        <!--<td width="12%"  >Type</td>-->
                                        <th width="10%"  >Date</th>
                                        <th width="18%"  >Preview</th>
                                        <th width="10%"  >Delete</th>
                                    </tr>

                                    <?php

                                    //loop and display the limited records being browsed
                                    $counter=1;
                                    if($totalrows > 0 ){
                                    while ($arr = mysql_fetch_array($rs)) {
                                        // echo "<pre>";
                                        //print_r($arr);

                                        $templateLink="<a class='modal' name='600' id='viewtemplate.php?prtype=index&tid=".$arr['theme_id']."&id=".$arr['ntemplate_mast']."&type=redirect&'  href='javascript:void(0);' title='Click to get preview'>Preview</a>";

                                        //echo "../".$_SESSION["session_template_dir"]."/".$arr["ntemplate_mast"]."/".$arr["vthumpnail"]."<br>";
                                        $userlink = "sitemanager.php?begin=0&num=1&numBegin=1&cmbSearchType=username&txtSearch=".$arr["vuser_login"];
                                        $thumbimage="<img src='../".$_SESSION["session_template_dir"]."/".$arr["ntemplate_mast"]."/".$arr["theme_image_thumb"]."'>";
                                        echo "<tr   class=background>
                                                        <td style='word-break:break-all;'
                                                         align='left' >&nbsp;".htmlentities(stripslashes($arr["vcat_name"]))."</a></td>";

                                        echo "<td  align='left'  style='word-break:break-all;'>
                                                        &nbsp;" .$thumbimage. "</td>";
                                        // echo "<td align='left' style='word-break:break-all;'>".htmlentities($arr["vtype"])." </td>";
                                        echo "<td align='center'>&nbsp;" .htmlentities(stripslashes($arr["date"])). "</td>";
                                        echo '<td  align="center">'.$templateLink.'</td>';
                                        // echo "<td >&nbsp;<a class=subtext  target='_blank' href='templatepriview.php?prtype=index&id=".stripslashes($arr["ntemplate_mast"])."&type=".$arr["vtype"]."'>Indexpage</a>";
                                        //echo "<br><a class=subtext  target='_blank' href='templatepriview.php?prtype=sub&id=".stripslashes($arr["ntemplate_mast"])."&type=".$arr["vtype"]."'>Subpage</a></td>";

                                        $tempEditLink = '<a href="edittemplate.php?tempid='.$arr['ntemplate_mast'].'">Edit</a> | ';
                                        echo "<td >".$tempEditLink."&nbsp;<a href=javascript:deleteTemplate('". stripslashes($arr["ntemplate_mast"]) ."','".$begin."','".$num."','".$numBegin."') class=subtext>Delete</a></td>";
                                        echo "</tr>";
                                        $counter++;
                                        }  } else { ?>
                                    <tr><td colspan="8" style="text-align: center;"><b>No Data Found!</b></td></tr>
                                <?php }
                                ?>
                                       

                                </table>

                                 <?php if($totalrows >$pageCount){ ?>
                                <div class="admin-table-btm">
                                    <div class="total-list lft">
                                    <?php echo("Listing $navigate[1] of $totalrows results.");?>
                                    </div>
                                    <div class="list-pagin ryt">
                                    <?php echo($navigate[2]); ?>
                                    </div>
                                <div class="clear"></div>
                                </div>
                            <?php } ?>

                            </td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                        </tr>
                    </table>

                </td>
            </tr>
        </table>

</form>
</div>


<?php
include "includes/adminfooter.php";
?>