Skip to content

Commit

Permalink
updater for themes
Browse files Browse the repository at this point in the history
  • Loading branch information
Praesidiarius committed Apr 1, 2021
1 parent 47411a1 commit e5e8013
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"laminas/laminas-session": "^2.9.1",
"laminas/laminas-paginator": "^2.8",
"laminas/laminas-mail": "^2.10",
"oneplace/oneplace-templates": "^1.0.0",
"oneplace/oneplace-user": "^1.0.23"
},
"autoload": {
Expand Down
13 changes: 13 additions & 0 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@
],
],
],
'theme-update' => [
'type' => Segment::class,
'options' => [
'route' => '/application/updatetheme[/:theme]',
'constraints' => [
'theme' => '[a-zA-Z0-9]+',
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'updatetheme',
],
],
],
'form-updatetabsort' => [
'type' => Literal::class,
'options' => [
Expand Down
1 change: 1 addition & 0 deletions module/Application/data/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ INSERT INTO `settings` (`settings_key`, `settings_value`) VALUES
INSERT INTO `permission` (`permission_key`, `module`, `label`, `nav_label`, `nav_href`, `show_in_menu`, `needs_globaladmin`) VALUES
('index', 'Application\\Controller\\IndexController', 'Home', 'Home', '/', 0, 0),
('update', 'Application\\Controller\\IndexController', 'Core Update', '', '', 0, 0),
('updatetheme', 'Application\\Controller\\IndexController', 'Theme Update', '', '', 0, 0),
('addtheme', 'Application\\Controller\\UploadController', 'Upload Theme', '', '/application/addtheme', 0, 1),
('themes', 'Application\\Controller\\IndexController', 'Theme Selection', '', '/application/themes', 0, 0),
('filepond', 'Application\\Controller\\UploadController', 'Upload Featured Image', '', '', 0, 0),
Expand Down
114 changes: 114 additions & 0 deletions module/Application/src/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,4 +640,118 @@ public function updatetabsortAction()

return false;
}

public function updatethemeAction()
{
$this->setThemeBasedLayout('core');

$sTheme = $this->params()->fromRoute('theme', 'default');
$sThemeFile = \Application\Module::getModuleDir().'../../public/themes/'.$sTheme.'/theme.json';
$oThemeInfo = false;
if(file_exists($sThemeFile)) {
$oThemeInfo = json_decode(file_get_contents($sThemeFile));
}

$opts = [
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: PHP'
]
]
];
$oContext = stream_context_create($opts);

$sNewInfo = file_get_contents('https://api.github.com/repos/OnePlc/oneplace-theme-'.$sTheme.'/releases/latest', false, $oContext);
$oNewInfo = json_decode($sNewInfo);

$sChangeLog = file_get_contents('https://github.com/OnePlc/oneplace-theme-'.$sTheme.'/raw/'.$oNewInfo->tag_name.'/CHANGELOG.md', false, $oContext);
$oRequest = $this->getRequest();

if(!$oRequest->isPost()) {
return new ViewModel([
'sTheme' => $sTheme,
'oThemeInfo' => $oThemeInfo,
'oNewInfo' => $oNewInfo,
'sChangeLog' => $sChangeLog,
]);
}

$this->layout('layout/json');
$oNewTheme = file_get_contents('https://github.com/OnePlc/oneplace-theme-'.$sTheme.'/archive/refs/tags/'.$oNewInfo->tag_name.'.zip', false, $oContext);
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/themes/'.$sTheme.'-update.zip',$oNewTheme);
# unzip theme
$zip = new \ZipArchive();
$x = $zip->open($_SERVER['DOCUMENT_ROOT'].'/themes/'.$sTheme.'-update.zip');
if ($x === true) {
$zip->extractTo($_SERVER['DOCUMENT_ROOT'].'/themes/'); // change this to the correct site path
$zip->close();

unlink($_SERVER['DOCUMENT_ROOT'].'/themes/'.$sTheme.'-update.zip');
IndexController::deleteDir($_SERVER['DOCUMENT_ROOT'].'/themes/oneplace-theme-'.$sTheme.'-'.$oNewInfo->tag_name.'/assets');
IndexController::recurseCopy($_SERVER['DOCUMENT_ROOT'].'/themes/'.$sTheme.'/assets', $_SERVER['DOCUMENT_ROOT'].'/themes/oneplace-theme-'.$sTheme.'-'.$oNewInfo->tag_name.'/assets');
IndexController::deleteDir($_SERVER['DOCUMENT_ROOT'].'/themes/'.$sTheme);
rename($_SERVER['DOCUMENT_ROOT'].'/themes/oneplace-theme-'.$sTheme.'-'.$oNewInfo->tag_name,$_SERVER['DOCUMENT_ROOT'].'/themes/'.$sTheme);

$this->setThemeBasedLayout('core');
return new ViewModel([
'sTheme' => $sTheme,
'oThemeInfo' => $oThemeInfo,
'oNewInfo' => $oNewInfo,
'sChangeLog' => $sChangeLog,
]);
}
}

private static function recurseCopy($src,$dst, $childFolder='') {

$dir = opendir($src);
mkdir($dst);
if ($childFolder!='') {
mkdir($dst.'/'.$childFolder);

while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
IndexController::recurseCopy($src . '/' . $file,$dst.'/'.$childFolder . '/' . $file);
}
else {
copy($src . '/' . $file, $dst.'/'.$childFolder . '/' . $file);
}
}
}
}else{
// return $cc;
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
IndexController::recurseCopy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
}

closedir($dir);
}

private static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
}
2 changes: 1 addition & 1 deletion module/Application/view/application/index/themes.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use Application\Controller\CoreController; ?>
<li class="list-group-item">
<?=ucfirst($sTheme)?>
<?=(CoreController::$oSession->oUser->getTheme() != $sTheme)
? '<a href="/user/settheme/'.$sTheme.'">'.$this->translate('Activate').'</a>' : ''?>
? '<a href="/user/settheme/'.$sTheme.'">'.$this->translate('Activate').'</a>' : '<a href="/application/updatetheme/'.$sTheme.'">'.$this->translate('Check for Update').'</a>'?>
</li>
<?php } ?>
</ul>
Expand Down
69 changes: 69 additions & 0 deletions module/Application/view/application/index/updatetheme.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<form action="" method="POST">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h2>Update Theme <?=ucfirst($sTheme)?></h2>
</div>
<div class="card-header">
<div class="row">
<div class="col-md-2">
<?=$this->translate('Current Version')?>
</div>
<div class="col-md-10">
<?=$oThemeInfo->version?>
</div>
</div>
</div>
<div class="card-header">
<div class="row">
<div class="col-md-2">
<?=$this->translate('New Version on github.com')?>
</div>
<div class="col-md-10">
<?=$oNewInfo->tag_name?>
</div>
</div>
</div>
<div class="card-header">
<div class="row">
<div class="col-md-2">
<?=$this->translate('Changes')?>
</div>
<div class="col-md-10">
<?php
$sNewChanges = explode('## ['.$oThemeInfo->version.'] -',$sChangeLog)[0];
$sNewChanges = explode('## ['.$oNewInfo->tag_name.'] -',$sNewChanges)[1];
$sNewChanges = str_replace([
'### Added',
'### Changed',
'### Fixed',
'### Removed',
'- '
],[
'<br/><b>Added</b>',
'<br/><b>Changed</b>',
'<br/><b>Fixed</b>',
'<br/><b>Removed</b>',
'<br/>- '
],$sNewChanges);
echo 'Release Date: '.$sNewChanges;
?>
</div>
</div>
</div>
<div class="card-footer">
<?php if($oThemeInfo->version != $oNewInfo->tag_name) { ?>
<button type="submit" class="btn btn-success btn-lg">
<?=$this->translate('Update '.ucfirst($sTheme))?>
</button>
<?php } else { ?>
<div class="alert alert-success p-2">
You already have the newest version of this theme
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</form>

0 comments on commit e5e8013

Please sign in to comment.