<?php
// +---------------------------------------------+
// |     Copyright ?2003 ?2005 weenCompany |
// |     http://www.weentech.com               |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


define('MOD_ACCESS', true);
define('CUSTOM_MODULE', true);
define('IN_ADMIN', true);
define('IN_WEENCOMPANY', true);

$rootpath = "./../";

include($rootpath . 'includes/core.php');

PrintHeader('设计模块', 1);

// get the value of $action
$action = isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : '');



// ########################### CREATE CUSTOM MODULE ############################

if($action == 'createcustommodule')
{
  $name           = $_POST['name'];
  $displayname    = $_POST['displayname'];
  $module         = $_POST['module'];
  $includefile    = $_POST['includefile'];

  $DB->query("INSERT INTO " . TABLE_PREFIX . "custommodules (name, displayname, module, includefile, settings)
              VALUES ('$name', '$displayname', '$module', '$includefile', 17)");

  $custommoduleid = $DB->insert_id();

  // install default usergroup settings
  $usergroups = $DB->query("SELECT usergroupid, custommoduleviewids, custommoduleadminids FROM " . TABLE_PREFIX . "usergroups");

  while($usergroup = $DB->fetch_array($usergroups))
  {
    // Administrators
    if($usergroup['usergroupid'] == 1)
    {
      $custommoduleviewids  = strlen($usergroup['custommoduleviewids'])  ? $usergroup['custommoduleviewids']  . ',' . $custommoduleid : $custommoduleid;
      $custommoduleadminids = strlen($usergroup['custommoduleadminids']) ? $usergroup['custommoduleadminids'] . ',' . $custommoduleid : $custommoduleid;

      $DB->query("UPDATE " . TABLE_PREFIX . "usergroups SET custommoduleviewids  = '$custommoduleviewids',
                                                            custommoduleadminids = '$custommoduleadminids'
                                                      WHERE usergroupid          = '" . $usergroup['usergroupid'] . "'");
    }
    else
    {
      $custommoduleviewids = strlen($usergroup['custommoduleviewids'])  ? $usergroup['custommoduleviewids'] . ',' . $custommoduleid : $custommoduleid;

      $DB->query("UPDATE " . TABLE_PREFIX . "usergroups SET custommoduleviewids  = '$custommoduleviewids'
                                                      WHERE usergroupid          = '" . $usergroup['usergroupid'] . "'");
    }
  }

  echo '<BODY onLoad="parent.leftFrame.location.reload(true)">';
  PrintRedirect('custommodules.php?custommoduleid=' . $custommoduleid, 1);
}



// ########################### UPDATE CUSTOM MODULE ############################

if($action == 'updatecustommodule')
{
  $name           = $_POST['name'];
  $displayname    = $_POST['displayname'];
  $module         = $_POST['module'];
  $includefile    = $_POST['includefile'];
  $custommoduleid = $_POST['custommoduleid'];

  $DB->query("UPDATE " . TABLE_PREFIX . "custommodules SET name           = '$name',
                                                           displayname    = '$displayname',
                                                           module         = '$module',
                                                           includefile    = '$includefile'
                                                     WHERE custommoduleid = '$custommoduleid'");

  echo '<BODY onLoad="parent.leftFrame.location.reload(true)">';
  PrintRedirect('custommodules.php?custommoduleid=' . $custommoduleid, 1);
}



// ####################### CREATE/EDIT CUSTOM MODULE FORM ######################

echo '<form method="post" action="custommodules.php">';

if(isset($_GET['custommoduleid']))
{
  $custommoduleid = $_GET['custommoduleid'];
  
  // Check the user has permission to do this
  if(!@in_array($custommoduleid, $userinfo['custommoduleadminids']))
  {
    PrintErrors("系统设置，无法操作!", '操作失败');
    PrintFooter();
    return;
  }

  $custommodule = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "custommodules WHERE custommoduleid = '$custommoduleid'");

  echo '<input type="hidden" name="action" value="updatecustommodule" />
        <input type="hidden" name="custommoduleid" value="' . $custommoduleid . '" />';

  PrintSection('设置自定义模块 - ' . $custommodule['name']);
}
else
{
  echo '<input type="hidden" name="action" value="createcustommodule" />';

  $custommodule = array('name'        => '',
                        'displayname' => '',
                        'module'      => '',
                        'includefile' => '');

  $selected = '';

  PrintSection('制作自定义模块');
}




echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
      <tr>
        <td class="tdrow1" colspan="2">模块后台名称:</td>
      </tr>
      <tr>
        <td class="tdrow2" width="60%">自定义模块后台名称:</td>
        <td class="tdrow3"><input type="text" name="name" value="'.CleanFormValue($custommodule['name']).'" size="50" />
      </tr>
      <tr>
        <td class="tdrow1" colspan="2">模块前台名称:</td>
      </tr>
      <tr>
        <td class="tdrow2" width="60%">自定义模块前台名称:</td>
        <td class="tdrow3"><input type="text" name="displayname" value="'.CleanFormValue($custommodule['displayname']).'" size="50" />
      </tr>
      <tr>
        <td class="tdrow1" colspan="2">包含文件(Include)</td>
      </tr>
      <tr>
        <td class="tdrow2" width="60%">自定模块需要包含(Include)的文件:<br>包含其它的php, js等类型的文件, 以实现特殊的功能. 要求相对路径, 如: includes/myfile.php</td>
        <td class="tdrow3"><input size="50" type="text" name="includefile" value="'.CleanFormValue($custommodule['includefile']).'" /></td>
      </tr>
      <tr>
        <td class="tdrow1" colspan="2">模块内容';
		  
		  if($enablewysiwyg){
			echo '&nbsp&nbsp<a href="javascript:;" onclick="javascript: ween_toggleEditor(\'customdescription\', \'tinytogglecustom\', \'tinytoggleimgcustom\');return false;" id="tinytogglecustom" title="Hide TinyMCE"><img src="styles/advanced/images/disabletiny.gif" border="0" align="absmiddle" id="tinytoggleimgcustom"></a>';
		  }
	echo '</td></tr>
      <tr>
        <td class="tdrow2" colspan="2"><textarea name="module" style="width: 100%;" rows="30" class="mceEditor" id="customdescription">' . $custommodule['module'] . '</textarea></td>
      </tr>
      <tr>
        <td class="tdrow1" bgcolor="#FCFCFC" colspan="2" align="center">
        <input type="submit" value="' . iif(isset($_GET['custommoduleid']), '更新模块', '创建模块') . '" />
        </td>
      </tr>
      </table>
      </form>';
EndSection();


PrintFooter();

?>