<?php

include("../includes/site_config.php");
include('../session_handler.inc');
include('../error.php');
include('../functions.php');
include('../languages/english_admin.php');
include_once __INCLUDE_CLASS_PATH."/class.ItemManager.php";
$MTemplateManager = new MTemplateManager();
include_once __INCLUDE_CLASS_PATH."/class.Language.php";
$Language = new Language();
include_once __INCLUDE_CLASS_PATH."/class.Pager.php";
$pager = new Pager(1,20);
/*
if ($Sess_UserType != "A") {
    exit;
}*/
$mailtemplate =& MTemplateManager::getInstance();
$lang = & Language::getInstance();
$pager->SetUrl($CONST_LINK_ROOT."/admin/admin_mailtemplates.php");



$Name = form_get('Name');

$LANG_ID = form_get('LANG_ID');

$Type = form_get('Type');

$Body = form_get('Body');

$act = form_get('act');




if($Name){
$m_template = $mailtemplate->Get($Name);
}


if($act == 'save') {

    restrict_demo();

    $res = $m_template->Save($Body,$Type,$LANG_ID);

    if ($res===null ) {

        error_page(join("<br>",$m_template->error),GENERAL_USER_ERROR);

    } else {

         echo "<script> location.href='$CONST_LINK_ROOT/admin/admin_mailtemplates.php'; </script>";
//header("Location: $CONST_LINK_ROOT/admin/admin_mailtemplates.php");

        exit;

    }

}






# retrieve the template
$area = 'member';

$aMtemplates = $mailtemplate->GetList($pager);
?>
<?php include("include/header.php"); ?>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;Email Templates</div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="400" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
	 <tr>

      <td align="right">

      <td>
	<? //include("../pager.php"); ?>
	<table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">
    <tr  align="left">
      <td class="admin-head2">
        <?=ADM_MAILTEMPLATES_DESCRIPTION?>
      </td>
      <td class="admin-head2">
        <?=ADM_MAILTEMPLATES_TYPE?>
      </td>
      <td class="admin-head2">&nbsp;
        <?=BUTTON_EDIT?>
      </td>
     </tr>
	 <tr><td>&nbsp;</td></tr>
        <?php
        foreach ($aMtemplates as $row){

        ?>

    <tr class="normal" >
      <td>
        <?=$row->comments?>
      </td>
      <td>
        <?=$row->type?>
      </td>
      <td>
      <? foreach($lang->GetActiveList() as $lang_row){?>
        <!--<a href="<?=$_SERVER['PHP_SELF']?>?Name=<?=$row->name?>&LANG_ID=<?=$lang_row->LangID?>">
            [<?=$lang_row->LangID?>]
        </a>-->
		<a href="<?=$_SERVER['PHP_SELF']?>?Name=<?=$row->name?>&LANG_ID=<?=$lang_row->LangID?>">
            <?=$lang_row->LangID?>
        </a>
        <? } ?>
      </td>
    </tr>
    <?php } ?>
</table>
<br>
<br>
<br>
</td></tr></table>
<table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">

        <tr>

      <td align="right">

      </td>

    </tr>

    <tr>

    <td class="admin-head">

      <?= ADM_MAILTEMPLATES_EDIT_SECTION_NAME?>

    </td>

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

      <td><table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">

        <form method="post" action="<?php echo $CONST_LINK_ROOT?>/admin/admin_mailtemplates_edit.php<?php print("?PHPSESSID=".session_id()); ?>" enctype="multipart/form-data">

          <input type="hidden" name="act" value="save">

          <input type="hidden" name="Name" value="<?=$Name?>">

          <input type="hidden" name="LANG_ID" value="<?=$LANG_ID?>">

          <tr>

            <td align="center">

    	    <table width="100%"  border="0" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>">

                <tr>

                  <td colspan="2" class="normal">&nbsp;</td>

                </tr>

                <tr class="normal">

                  <td>

                    <?=ADM_MAILTEMPLATES_DESCRIPTION?>

                  </td>

                  <td height="25"> <?= $m_template->comments?></td>

                </tr>

                <tr class="normal">

                  <td>

                    <?=ADM_MAILTEMPLATES_VARIABLES?>

                  </td>
<? if($Name){?>
                  <td height="25"> <?= join(", ",$m_template->config)?></td>
<? }?>
                </tr>

                <tr class="normal">

                  <td>

                    <?=ADM_MAILTEMPLATES_TYPE?>

                  </td>

                  <td height="25">

                  <select name="Type" class="input">

                    <option value="text/html" <? if ($m_template->type == "text/html"){?>selected<?}?>>Text/Html

                    <option value="text/plain" <? if ($m_template->type == "text/plain"){?>selected<?}?>>Text/Plain

                  </select>

                  </td>

                </tr>

                <tr class="normal">

                  <td>

                    <?=ADM_MAILTEMPLATES_MESSAGE?>

                  </td>

                  <td> <textarea style="width=500 px;" name="Body" cols="80" rows="15" wrap="soft" class="inputl"><?=$m_template->value[$LANG_ID]?></textarea></td>

                </tr>

                <tr>

                  <td colspan="2" align="center" class="normal"> <input name="submit" type="submit" class="normal" value="<?=GENERAL_SAVE?>">

                    <input name="normal" type="button" class="normal" onclick="window.location = '<?=$CONST_LINK_ROOT?>/admin/admin_mailtemplates.php'" value="<?=GENERAL_CANCEL?>">

                  </td>

                </tr>

              </table></td>

          </tr>

        </form>

      </table></td>

    </tr>

  </table></div><div class="admin-page-bottom"></div></div>
	  </td>
    </tr>
</table>
<?php mysql_close( $link ); ?>
<?php include("include/footer.php"); ?>