1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 13:47:31 +02:00

New feature: Create a new theme from an existing one. See: e107_admin/theme.php?mode=convert

This commit is contained in:
Cameron
2017-01-31 07:52:19 -08:00
parent 6531a68e00
commit 363a5e4c4c
4 changed files with 313 additions and 82 deletions

View File

@@ -193,16 +193,16 @@ if($mode == 'download' && !empty($_GET['src']))
echo $mes->render('default', 'debug'); echo $mes->render('default', 'debug');
} }
elseif(vartrue($_POST['selectadmin'])) elseif(!empty($_POST['selectadmin']))
{ {
$mode = "admin"; $mode = "admin";
} }
if(vartrue($_POST['upload'])) if(!empty($_POST['upload']))
{ {
$mode = "upload"; $mode = "upload";
} }
elseif(vartrue($_POST['selectmain']) || varset($_POST['setUploadTheme'])) elseif(!empty($_POST['selectmain']) || isset($_POST['setUploadTheme']))
{ {
$mode = "main"; $mode = "main";
} }
@@ -248,7 +248,7 @@ function theme_adminmenu()
$var['upload']['text'] = TPVLAN_38; $var['upload']['text'] = TPVLAN_38;
$var['upload']['link'] = e_SELF."?mode=upload"; $var['upload']['link'] = e_SELF."?mode=upload";
$var['convert']['text'] = TPVLAN_63; $var['convert']['text'] = ADLAN_CL_6;
$var['convert']['link'] = e_SELF."?mode=convert"; $var['convert']['link'] = e_SELF."?mode=convert";
// $selected = (e_QUERY) ? e_QUERY : "main"; // $selected = (e_QUERY) ? e_QUERY : "main";
@@ -264,7 +264,28 @@ class theme_builder
function __construct() function __construct()
{ {
$this->themeName = $_GET['newtheme'];
$ns = e107::getRender();
$tp = e107::getParser();
e107::getMessage()->addDebug("Disable debug to save generated files. ");
$this->themeName = $tp->filter($_GET['newtheme'],'w');
if(!empty($_GET['src']))
{
$this->themeSrc = $tp->filter($_GET['src'],'w');
$this->copyTheme();
/* $src = $tp->filter($_GET['src'],'w');
$name = $tp->filter($_GET['f']);
$title = $tp->filter($_GET['t']);
$this->copyTheme($src,$name,$title);*/
}
if(vartrue($_GET['step']) == 3) if(vartrue($_GET['step']) == 3)
{ {
@@ -278,9 +299,22 @@ class theme_builder
} }
else else
{ {
$this->step1(); $ret = $this->step1();
$ret2 = $this->copyThemeForm();
$tabs = array(
0 => array('caption'=>$ret['caption'], 'text'=>$ret['text']),
1 => array('caption'=>$ret2['caption'], 'text'=>$ret2['text']),
);
$ns->tablerender(ADLAN_140.SEP."Tools",e107::getForm()->tabs($tabs));
} }
} }
function step1() function step1()
@@ -315,6 +349,7 @@ class theme_builder
<col class='col-label' /> <col class='col-label' />
<col class='col-control' /> <col class='col-control' />
</colgroup> </colgroup>
<tbody>
<tr> <tr>
<td> ".TPVLAN_68."</td> <td> ".TPVLAN_68."</td>
<td>".$frm->select("newtheme",$newDir)."</td> <td>".$frm->select("newtheme",$newDir)."</td>
@@ -328,7 +363,7 @@ class theme_builder
</tr>"; </tr>";
*/ */
$text .= " $text .= "</tbody>
</table> </table>
<div class='buttons-bar center'> <div class='buttons-bar center'>
".$frm->admin_button('step', 2,'other',LAN_GO)." ".$frm->admin_button('step', 2,'other',LAN_GO)."
@@ -336,6 +371,8 @@ class theme_builder
$text .= $frm->close(); $text .= $frm->close();
return array('caption'=>TPVLAN_88, 'text'=>$mes->render() . $text);
$ns->tablerender(TPVLAN_26.SEP.TPVLAN_88.SEP. TPVLAN_CONV_1, $mes->render() . $text); $ns->tablerender(TPVLAN_26.SEP.TPVLAN_88.SEP. TPVLAN_CONV_1, $mes->render() . $text);
} }
@@ -351,11 +388,13 @@ class theme_builder
$data = array( $data = array(
'main' => array('name','lang','version','date', 'compatibility'), 'main' => array('name','lang','version','date', 'compatibility'),
'author' => array('name','url'), 'author' => array('name','url'),
'summary' => array('summary'), 'summary' => array('summary'),
'description' => array('description'), 'description' => array('description'),
'keywords' => array('one','two'), 'keywords' => array('one','two'),
'category' => array('category'), 'category' => array('category'),
'livedemo' => array('livedemo'),
'copyright' => array('copyright'), 'copyright' => array('copyright'),
'stylesheets' => array('stylesheets') 'stylesheets' => array('stylesheets')
// 'adminLinks' => array('url','description','icon','iconSmall','primary'), // 'adminLinks' => array('url','description','icon','iconSmall','primary'),
@@ -363,7 +402,46 @@ class theme_builder
); );
$legacyFile = e_THEME.$this->themeName."/theme.php"; $legacyFile = e_THEME.$this->themeName."/theme.php";
if(file_exists($legacyFile))
$newThemeXML = e_THEME.$this->themeName."/theme.xml";
if(file_exists($newThemeXML))
{
$info = e107::getTheme()->getThemeInfo($this->themeName);
e107::getDebug()->log($info);
if($this->themeSrc) // New theme copied from another
{
$defaults = array(
"main-name" => ucfirst($this->themeName),
'category-category' => vartrue($info['category']),
);
}
else
{
$defaults = array(
"main-name" => vartrue($info['name']),
"main-date" => vartrue($info['date']),
"main-version" => vartrue($info['version']),
"author-name" => vartrue($info['author']),
"author-url" => vartrue($info['website']),
"description-description" => vartrue($info['description']),
"summary-summary" => vartrue($info['summary']),
'category-category' => vartrue($info['category']),
// "custompages" => vartrue($leg['CUSTOMPAGES']),
);
}
if(!empty($info['keywords']['word']))
{
$defaults['keywords-one'] = $info['keywords']['word'][0];
$defaults['keywords-two'] = $info['keywords']['word'][1];
}
}
elseif(file_exists($legacyFile))
{ {
$legacyData = file_get_contents($legacyFile); $legacyData = file_get_contents($legacyFile);
@@ -398,7 +476,7 @@ class theme_builder
$mes->addInfo("Loading theme.php file"); $mes->addInfo("Loading theme.php file");
} }
$text = $frm->open('newtheme-step3','post', e_SELF.'?mode=convert&newtheme='.$this->themeName.'&step=3'); $text = $frm->open('newtheme-step3','post', e_SELF.'?mode=convert&src='.$this->themeSrc.'&newtheme='.$this->themeName.'&step=3');
$text .= "<table class='table adminlist'>"; $text .= "<table class='table adminlist'>";
foreach($data as $key=>$val) foreach($data as $key=>$val)
{ {
@@ -421,14 +499,16 @@ class theme_builder
$text .= "</table>"; $text .= "</table>";
$text .= " $text .= "
<div class='buttons-bar center'>" <div class='buttons-bar center'>"
.$frm->hidden('newtheme', $this->themeName) .$frm->hidden('newtheme', $this->themeName);
.$frm->hidden('xml[custompages]', trim(vartrue($leg['CUSTOMPAGES']))) $text .= $frm->hidden('xml[custompages]', trim(vartrue($leg['CUSTOMPAGES'])))
.$frm->admin_button('step', 3,'other',LAN_GENERATE)." .$frm->admin_button('step', 3,'other',LAN_GENERATE)."
</div>"; </div>";
$text .= $frm->close(); $text .= $frm->close();
$ns->tablerender(TPVLAN_26.SEP.TPVLAN_88.SEP. TPVLAN_CONV_2, $mes->render() . $text); // return array('caption'=>TPVLAN_88.SEP. TPVLAN_CONV_2, 'text'=>$mes->render() . $text);
$ns->tablerender(TPVLAN_26.SEP.ADLAN_CL_6.SEP. TPVLAN_CONV_2, $mes->render() . $text);
} }
@@ -439,56 +519,28 @@ class theme_builder
// print_a($_POST); // print_a($_POST);
if($_POST['xml']) if(!empty($_POST['xml']))
{ {
$xmlText = $this->createXml($_POST['xml']); $xmlText = $this->createXml($_POST['xml']);
$ns->tablerender("theme.xml", $mes->render(). "<pre>".$xmlText."</pre>");
} }
$ns->tablerender("theme.xml", $mes->render(). "<pre>".$xmlText."</pre>");
$legacyFile = e_THEME.$this->themeName."/theme.php"; $legacyFile = e_THEME.$this->themeName."/theme.php";
if(file_exists($legacyFile)) if(file_exists($legacyFile) && empty($this->themeSrc))
{ {
$legacyData = file_get_contents($legacyFile); $legacyData = file_get_contents($legacyFile);
$legacyData = e107::getTheme()->upgradeThemeCode($legacyData);
$output = nl2br(htmlentities($legacyData));
$ns->tablerender("theme.php (updated)", $output);
} }
$legacyData = $this->cleanUp($legacyData);
$output = nl2br(htmlentities($legacyData));
// $legacyData = str_replace("\n\n\n","\n",$legacyData);
$ns->tablerender("theme.php (updated)", $output);
} }
function cleanUp($text)
{
$search = array();
$replace = array();
$search[0] = '$HEADER ';
$replace[0] = '$HEADER["default"] ';
$search[1] = '$FOOTER ';
$replace[1] = '$FOOTER["default"] ';
// Early 0.6 and 0.7 Themes
$search[2] = '$CUSTOMHEADER ';
$replace[2] = '$HEADER["custom"] ';
$search[3] = '$CUSTOMFOOTER ';
$replace[3] = '$FOOTER["custom"] ';
//TODO Handle v1.x style themes. eg. $CUSTOMHEADER['something'];
$text = str_replace($_SESSION['themebulder-remove'],"",$text);
$text = str_replace($search, $replace, $text);
return $text;
}
function createXml($data) function createXml($data)
@@ -503,7 +555,7 @@ class theme_builder
$newArray[$key] = $val; $newArray[$key] = $val;
} }
if(vartrue($newArray['CUSTOMPAGES'])) if(!empty($newArray['CUSTOMPAGES']))
{ {
$newArray['CUSTOMPAGES'] = trim($newArray['CUSTOMPAGES']); $newArray['CUSTOMPAGES'] = trim($newArray['CUSTOMPAGES']);
$LAYOUTS = "\n<layout name='custom' title='Custom'>\n"; $LAYOUTS = "\n<layout name='custom' title='Custom'>\n";
@@ -515,14 +567,23 @@ class theme_builder
$LAYOUTS = ""; $LAYOUTS = "";
} }
if(vartrue($newArray['STYLESHEETS_STYLESHEETS'])) if(!empty($newArray['STYLESHEETS_STYLESHEETS']))
{ {
$STYLESHEETS = "\n\t<stylesheets>\n"; $STYLESHEETS = '';
foreach($newArray['STYLESHEETS_STYLESHEETS'] as $val) foreach($newArray['STYLESHEETS_STYLESHEETS'] as $val)
{ {
if(empty($val['file']))
{
continue;
}
$STYLESHEETS .= "\t\t<css file=\"".$val['file']."\" name=\"".$val['name']."\" />\n"; $STYLESHEETS .= "\t\t<css file=\"".$val['file']."\" name=\"".$val['name']."\" />\n";
} }
$STYLESHEETS .= "\t</stylesheets>";
if(!empty($STYLESHEETS))
{
$STYLESHEETS = "\n\t<stylesheets>\n".$STYLESHEETS."\t</stylesheets>";
}
unset($newArray['STYLESHEETS_STYLESHEETS']); unset($newArray['STYLESHEETS_STYLESHEETS']);
} }
@@ -538,15 +599,15 @@ class theme_builder
$template = <<<TEMPLATE $template = <<<TEMPLATE
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<e107Theme name="{MAIN_NAME}" lan="{MAIN_LANG}" version="{MAIN_VERSION}" date="{MAIN_DATE}" compatibility="{MAIN_COMPATIBILITY}" > <e107Theme name="{MAIN_NAME}" lan="{MAIN_LANG}" version="{MAIN_VERSION}" date="{MAIN_DATE}" compatibility="{MAIN_COMPATIBILITY}" livedemo="{LIVEDEMO_LIVEDEMO}">
<author name="{AUTHOR_NAME}" url="{AUTHOR_URL}" /> <author name="{AUTHOR_NAME}" url="{AUTHOR_URL}" />
<summary lan="">{SUMMARY_SUMMARY}</summary> <summary lan="">{SUMMARY_SUMMARY}</summary>
<description lan="">{DESCRIPTION_DESCRIPTION}</description> <description lan="">{DESCRIPTION_DESCRIPTION}</description>
<category>{CATEGORY_CATEGORY}</category>
<keywords> <keywords>
<word>{KEYWORDS_ONE}</word> <word>{KEYWORDS_ONE}</word>
<word>{KEYWORDS_TWO}</word> <word>{KEYWORDS_TWO}</word>
</keywords> </keywords>
<category>{CATEGORY_CATEGORY}</category>
<copyright>{COPYRIGHT_COPYRIGHT}</copyright> <copyright>{COPYRIGHT_COPYRIGHT}</copyright>
<screenshots> <screenshots>
<image>preview.jpg</image> <image>preview.jpg</image>
@@ -606,7 +667,7 @@ TEMPLATE;
case 'main-name': case 'main-name':
$help = TPVLAN_CONV_3; $help = TPVLAN_CONV_3;
$required = true; $required = true;
$pattern = "[A-Za-z ]*"; $pattern = "[A-Za-z 0-9]*";
break; break;
case 'main-lang': case 'main-lang':
@@ -619,6 +680,7 @@ TEMPLATE;
case 'main-date': case 'main-date':
$help = TPVLAN_CONV_6; $help = TPVLAN_CONV_6;
$required = true; $required = true;
$default = (empty($default)) ? time() : strtotime($default);
break; break;
case 'main-version': case 'main-version':
@@ -648,6 +710,13 @@ TEMPLATE;
// $pattern = "https?://.+"; // $pattern = "https?://.+";
break; break;
case 'livedemo-livedemo':
$required = false;
$help = TPVLAN_CONV_16;
$placeholder= "http://demo-of-my-theme.com";
// $pattern = "https?://.+";
break;
//case 'main-installRequired': //case 'main-installRequired':
// return "Installation required: ".$frm->radio_switch($name,'',LAN_YES, LAN_NO); // return "Installation required: ".$frm->radio_switch($name,'',LAN_YES, LAN_NO);
//break; //break;
@@ -701,7 +770,7 @@ TEMPLATE;
$fl = e107::getFile(); $fl = e107::getFile();
$fl->setMode('full'); $fl->setMode('full');
$stylesheets = $fl->get_files(e_THEME.$this->themeName."/", "\.css", $reject, 1); $stylesheets = $fl->get_files(e_THEME.$this->themeName."/", "\.css", null, 1);
foreach($stylesheets as $key=>$path) foreach($stylesheets as $key=>$path)
{ {
$file = str_replace(e_THEME.$this->themeName."/",'',$path); $file = str_replace(e_THEME.$this->themeName."/",'',$path);
@@ -723,11 +792,11 @@ TEMPLATE;
case 'date': case 'date':
$text = $frm->datepicker($name, time(), 'format=yyyy-mm-dd'.$req.'&size=block-level'); $text = $frm->datepicker($name, $default, 'format=yyyy-mm-dd'.$req.'&size=block-level');
break; break;
case 'description': case 'description':
$text = $frm->textarea($name,$default, 3, 100, $req,'&size=block-level'); // pattern not supported. $text = $frm->textarea($name,$default, 3, 100, $req.'&size=block-level'); // pattern not supported.
break; break;
@@ -740,7 +809,7 @@ TEMPLATE;
sort($allowedCategories); sort($allowedCategories);
$text = $frm->select($name, $allowedCategories,'','useValues=1&required=1', true); $text = $frm->select($name, $allowedCategories,$default,'useValues=1&required=1', true);
break; break;
@@ -754,6 +823,99 @@ TEMPLATE;
return $text; return $text;
} }
function copyThemeForm()
{
$frm = e107::getForm();
$list = e107::getTheme()->clearCache()->getThemeList(); // (e_THEME);
$folders = array_keys($list);
$text = $frm->open('copytheme','get','theme.php?mode=convert');
$text .= "<table class='table adminform'>
<colgroup>
<col class='col-label' />
<col class='col-control' />
</colgroup>
<tr>
<td>Create a new theme based on</td>
<td>".$frm->select("src",$folders,'',array('useValues'=>1))."</td>
</tr>
<tr>
<td>New Theme Folder</td>
<td>".$frm->text("newtheme",'',25, array('pattern'=>'[a-z_0-9]*', 'required'=>1))."</td>
</tr>
";
/*
$text .= "
<tr>
<td>Create Files</td>
<td>".$frm->checkbox('createFiles',1,1)."</td>
</tr>";
*/
$text .= "
</table>
<div class='buttons-bar center'>
".$frm->admin_button('step', 2,'success', LAN_CREATE)."
</div>";
$text .= $frm->close();
// $text = "Create a new theme based on ".e->select('copytheme',$list);
return array('caption'=>"Create New", 'text'=>$text);
}
private function copyTheme()
{
if(empty($this->themeSrc) || empty($this->themeName) || is_dir(e_THEME.$this->themeName))
{
return false;
}
if(e107::getFile()->copy(e_THEME.$this->themeSrc, e_THEME.$this->themeName))
{
$newfiles = scandir(e_THEME.$this->themeName);
foreach($newfiles as $file)
{
if(is_dir(e_THEME.$this->themeName.'/'.$file) || $file === '.' || $file === '..')
{
continue;
}
if(strpos($file,"admin_") === 0)
{
unlink(e_THEME.$this->themeName.'/'.$file);
}
}
}
}
} }

View File

@@ -880,7 +880,43 @@ class e_file
} }
/**
* Copy a file, or copy the contents of a folder.
* @param string $source Source path
* @param string $dest Destination path
* @param int $perm
* @return bool Returns true on success, false on error
*/
function copy($source, $dest, $perm = 0755)
{
// Simple copy for a file
if(is_file($source))
{
return copy($source, $dest);
}
// Make destination directory
if(!is_dir($dest))
{
mkdir($dest, $perm);
}
// Directory - so copy it.
$dir = scandir($source);
foreach($dir as $folder)
{
// Skip pointers
if($folder === '.' || $folder == '..')
{
continue;
}
$this->copy("$source/$folder", "$dest/$folder", $perm);
}
return true;
}
/** /**

View File

@@ -56,12 +56,44 @@ class e_theme
public function clearCache() public function clearCache()
{ {
e107::getCache()->clear(self::CACHETAG, true); e107::getCache()->clear(self::CACHETAG, true);
return $this;
}
public function upgradeThemeCode($text)
{
$search = array();
$replace = array();
$search[0] = '$HEADER ';
$replace[0] = '$HEADER["default"] ';
$search[1] = '$FOOTER ';
$replace[1] = '$FOOTER["default"] ';
// Early 0.6 and 0.7 Themes
$search[2] = '$CUSTOMHEADER ';
$replace[2] = '$HEADER["custom"] ';
$search[3] = '$CUSTOMFOOTER ';
$replace[3] = '$FOOTER["custom"] ';
//TODO Handle v1.x style themes. eg. $CUSTOMHEADER['something'];
$text = str_replace($_SESSION['themebulder-remove'],"",$text);
$text = str_replace($search, $replace, $text);
return $text;
} }
/** /**
* Get a list of all themes in theme folder and its data. * Get a list of all themes in theme folder and its data.
* @param bool|false xml|false * @param bool|false xml|false

View File

@@ -111,6 +111,7 @@ define("TPVLAN_CONV_12", "A full description of the theme (Must be written in En
define("TPVLAN_CONV_13", "What category of theme is this?"); define("TPVLAN_CONV_13", "What category of theme is this?");
define("TPVLAN_CONV_14", "Enable this stylesheet as a selectable option in the Theme Manager."); define("TPVLAN_CONV_14", "Enable this stylesheet as a selectable option in the Theme Manager.");
define("TPVLAN_CONV_15", "Give this stylesheet a name"); define("TPVLAN_CONV_15", "Give this stylesheet a name");
define("TPVLAN_CONV_16", "URL to a live-demo of this theme.");
//marketplace //marketplace
define("TPVLAN_81","Downloading..."); define("TPVLAN_81","Downloading...");