Skip to content

Commit

Permalink
Merge pull request #43 from AlexStack/alex_dev
Browse files Browse the repository at this point in the history
Basic tags for a page & Upgrade CMS online via browser
  • Loading branch information
AlexStack authored Oct 18, 2019
2 parents f8ea703 + ae03cf0 commit c33b5c3
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Http/Controllers/LaravelCmsFileAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function index()
return $this->repo->extractFile(request()->extract_file);
} elseif (false !== strpos(request()->install_package, '/temp/')) {
return $this->repo->installPackage(request()->install_package);
} elseif (request()->new_version && request()->old_version) {
return $this->repo->upgradeCmsViaBrowser(request()->new_version, request()->old_version);
}

$data = $this->repo->index();
Expand Down
4 changes: 3 additions & 1 deletion src/Repositories/LaravelCmsDashboardAdminRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function index()
if (isset($packages['packages'])) {
foreach ($packages['packages'] as $p) {
if ('alexstack/laravel-cms' == strtolower($p['name'])) {
$data['cms_version'] = $p['version'];
if (version_compare($p['version'], $data['cms_version']) > 0) {
$data['cms_version'] = $p['version'];
}
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/Repositories/LaravelCmsFileAdminRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,65 @@ public function installPlugin($package_file)
return response()->json($result);
}

public function upgradeCmsViaBrowser($cms_version, $old_version)
{
$need_composer = true;
$cms_source_dir = storage_path('app/laravel-cms/backups/cms-source-code/git-clone-cms-'.$cms_version.'-'.date('Y-m-d-His'));
$cms_backup_dir = storage_path('app/laravel-cms/backups/cms-source-code/vendor-laravel-cms-'.$old_version.'-'.date('Y-m-d-His'));
$cms_vendor_dir = base_path('vendor/alexstack/laravel-cms');

if (false !== strpos(ini_get('disable_functions'), 'exec')) {
$result['success'] = false;
$result['error_message'] = 'Your server do not support upgrade the CMS online via browser, please upgrade the CMS via composer command';

return response()->json($result);
}

exec('git clone --branch '.$cms_version.' --depth 1 https://github.com/AlexStack/Laravel-CMS.git '.$cms_source_dir);

if (file_exists($cms_source_dir.'/composer.json')) {
$old_composer = json_decode(file_get_contents($cms_vendor_dir.'/composer.json'), true);
$new_composer = json_decode(file_get_contents($cms_source_dir.'/composer.json'), true);
if (isset($new_composer['require']) && $new_composer['require'] == $old_composer['require']) {
$need_composer = false;
}
}

if ($need_composer) {
$result['success'] = false;
$result['error_message'] = 'This new version can NOT upgrade via browser, needs to upgrade via composer command!';

return response()->json($result);
}

$rs = @rename($cms_vendor_dir, $cms_backup_dir);
if ($rs) {
$rs = @rename($cms_source_dir, $cms_vendor_dir);
if ($rs) {
exec('git --git-dir='.$cms_vendor_dir.'/.git --work-tree='.$cms_vendor_dir.' remote add composer https://github.com/AlexStack/Laravel-CMS.git');

$rs = LaravelCmsSetting::updateOrCreate(
['param_name' => 'cms_version', 'category' => 'system'],
['param_name' => 'cms_version', 'category' => 'system', 'param_value'=>$cms_version, 'enabled'=>1]
);

exec('php '.base_path('artisan').' laravelcms --action=upgrade --silent=yes');

$result['success'] = true;
$result['error_message'] = 'Upgrade CMS successful!';

return response()->json($result);
}
}

@rename($cms_source_dir, $cms_source_dir.'-pending-del');

$result['success'] = false;
$result['error_message'] = 'Upgrade CMS failed!';

return response()->json($result);
}

public function moveCmsFiles($source_files, $target_dir, $backup_dir, $ignore_files=[])
{
if (! file_exists($backup_dir)) {
Expand Down
26 changes: 26 additions & 0 deletions src/Repositories/LaravelCmsPageAdminRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function store($form_data)
//$this->helper->debug($form_data, 'no_exit');

$form_data['slug'] = $this->getSlug($form_data);

// tags string to json
$form_data['tags'] = $this->commaStrToJson($form_data['tags']);

// DB::enableQueryLog();

// $rs = LaravelCmsPage::create($form_data); // create() not working ???
Expand Down Expand Up @@ -97,6 +101,9 @@ public function update($form_data, $id)
//$this->debug($all_file_data, 'exit');
$this->handleUpload($form_data, $all_file_data);

// tags string to json
$form_data['tags'] = $this->commaStrToJson($form_data['tags']);

unset($form_data['_method'], $form_data['_token']);

$page->update($form_data);
Expand Down Expand Up @@ -129,6 +136,11 @@ public function edit($id)

$data['plugins'] = $this->extraPageTabs('edit', $id, $data['page']);

// tags
if ($tags_array = json_decode($data['page']->tags, true)) {
$data['page']->tags = implode(' , ', $tags_array);
}

//$this->helper->debug($data['plugins']->toArray(), 'no_exit22');

return $data;
Expand Down Expand Up @@ -372,4 +384,18 @@ public function templateFileOption()

return $option_ary;
}

public function commaStrToJson($str)
{
if ('' != trim($str)) {
$str = str_replace(['', ';', '|'], ',', $str);
// trim every elements
$tags_array = array_map('trim', explode(',', $str));
// remove empty, duplicate and keys
$tags_array = array_values(array_unique(array_filter($tags_array)));
$str = json_encode($tags_array, JSON_UNESCAPED_UNICODE);
}

return $str;
}
}
30 changes: 22 additions & 8 deletions src/Repositories/LaravelCmsPageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function show($id)

if (($reserved_slugs['search'] ?? 'Search-CMS.html') == $slug) {
return $this->search($slug);
} elseif (($reserved_slugs['tag'] ?? 'List-Tag.html') == $slug) {
return $this->search($slug, 'tag');
} elseif (($reserved_slugs['sitemap'] ?? 'sitemap.txt') == $slug) {
return $this->sitemap('txt');
} elseif (($reserved_slugs['redirect'] ?? 'redirect-link') == $slug) {
Expand Down Expand Up @@ -59,7 +61,11 @@ public function show($id)
$data['page']->parent_flat_ary = [];
}

//$this->helper->debug($data['page']->parent->toArray(), 'no_exit');
if ('' != trim($data['page']->tags)) {
$data['page']->tags_ary = json_decode($data['page']->tags, true);
} else {
$data['page']->tags_ary = [];
}

$data['file_data'] = json_decode($data['page']->file_data);
if (null == $data['file_data']) {
Expand Down Expand Up @@ -178,17 +184,25 @@ public function goExternalLink()
return redirect($s, 301, ['X-Robots-Tag' => 'noindex, nofollow']);
}

public function search($slug)
public function search($slug, $search_type = 'content')
{
$keyword = request()->keyword;
if ($keyword) {
$data['search_results'] = LaravelCmsPage::when($keyword, function ($query, $keyword) {
return $query->where('title', 'like', '%'.trim($keyword).'%')
if ('content' == $search_type) {
$data['search_results'] = LaravelCmsPage::when($keyword, function ($query, $keyword) {
return $query->where('title', 'like', '%'.trim($keyword).'%')
->orWhere('main_content', 'like', '%'.trim($keyword).'%')
->orWhere('sub_content', 'like', '%'.trim($keyword).'%');
})
->orderBy('id', 'desc')
->paginate($this->helper->s('template.number_per_search') ?? 6);
})
->orderBy('id', 'desc')
->paginate($this->helper->s('template.number_per_search') ?? 6);
} elseif ('tag' == $search_type) {
$data['search_results'] = LaravelCmsPage::when($keyword, function ($query, $keyword) {
return $query->where('tags', 'like', '%"'.trim($keyword).'"%');
})
->orderBy('id', 'desc')
->paginate($this->helper->s('template.number_per_search') ?? 6);
}
} else {
$data['search_results'] = [];
}
Expand All @@ -199,7 +213,7 @@ public function search($slug)
// simulate a page collection
$data['page'] = collect([]);
$data['page']->template_file = 'page-search-result';
$data['page']->title = $this->helper->t('search').' '.$keyword;
$data['page']->title = ('tag' == $search_type) ? $this->helper->t('tag').' '.$keyword : $this->helper->t('search').' '.$keyword;
$data['page']->meta_title = $data['page']->title.' ('.$this->helper->t('page_number', ['number'=>$_GET['page'] ?? 1]).')';
$data['page']->slug = $slug;
$data['page']->id = $slug;
Expand Down
1 change: 0 additions & 1 deletion src/config/laravel-cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
'slug_suffix' => '.html',
'slug_separate' => '-',
'allow_json_response' => 0,
'cms_version' => '1.0.1',
'cms_admin' => [
'super_admin' => [1],
'web_admin' => [1],
Expand Down
10 changes: 9 additions & 1 deletion src/resources/views/backend/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@
.done(function(data) {
// console.log( "second success");
// console.log(data['tag_name']);
$('span.latest_version').html('' + data['tag_name']);
var latest_version = data['tag_name'];
if ( '{{$cms_version}}' !== data['tag_name'] ){
latest_version = '<a href="./files?new_version=' + data['tag_name'] + '&old_version={{$cms_version}}" target="_blank" class="text-danger update-cms" onclick="return updateCms();">' + data['tag_name'] + '</a>';
}
$('span.latest_version').html(latest_version);
})
.fail(function() {
console.log( "cmsGitHubTags error" );
Expand Down Expand Up @@ -193,6 +197,10 @@
//console.log( "get majorInfo complete" );
});
}
function updateCms() {
return confirm("Are you sure to update the CMS online via browser? It may take few minutes, please keep the browser open until it complete.");
}
</script>

@endsection
9 changes: 9 additions & 0 deletions src/resources/views/frontend/includes/tags.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="row mb-4 nav-tags">
<div class="col-md text-right">
<span class="text-secondary text-tags">{{$helper->t('tags')}} :</span>
@foreach( $page->tags_ary as $tag )
<a href="{{route("LaravelCmsPages.show",$helper->s('system.reserved_slugs.tag'), false)}}?keyword={{$tag }}"
class="btn btn-sm btn-light ml-1">{{$tag }}</a>
@endforeach
</div>
</div>
2 changes: 2 additions & 0 deletions src/resources/views/frontend/page-detail-default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class="btn btn-primary" target="_blank" rel="nofollow">' . trim($external_link_a
<div class="text-center mb-4 extra_content extra_content_3">{!! $page->extra_content_3 !!}</div>
@endif

@include($helper->bladePath('includes.tags'))

@include($helper->bladePath('includes.breadcrumb'))


Expand Down

0 comments on commit c33b5c3

Please sign in to comment.