Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
uldisrudzitis committed Mar 2, 2020
2 parents 5cfd649 + ec43b31 commit d6af4a6
Show file tree
Hide file tree
Showing 46 changed files with 826 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "_themes"]
path = _themes
url = [email protected]:berta-cms/themes.git
4 changes: 2 additions & 2 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ RewriteRule ^ - [R=404,L]
# If you have folders or files inside berta's folder you wish to access directly,
# then modify and uncomment the following line
# RewriteCond %{REQUEST_URI} ^/(my_folder_name1|my_folder_name1|my_file.html).*$ [OR]
RewriteCond %{REQUEST_URI} ^/(_plugin_shop|engine|INSTALL|storage|_templates).*$ [OR]
RewriteCond %{REQUEST_URI} ^/(_plugin_shop|engine|INSTALL|storage|_templates|_themes).*$ [OR]
RewriteCond %{REQUEST_URI} ^/(index\.php|sitemap\.xml\.php|robots\.txt|crossdomain\.xml|favicon\.ico).*$

RewriteRule .* - [L]
Expand All @@ -75,7 +75,7 @@ RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

# the content is fed to index.php, which handles it
RewriteRule ^.*$ index.php?__rewrite=1 [L]
RewriteRule ^.*$ index.php?__rewrite=1 [QSA]

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /

Expand Down
20 changes: 20 additions & 0 deletions _api_app/app/Events/SectionUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Events;

class SectionUpdated extends Event
{
public $siteName;
public $sectionName;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct($siteName, $sectionName)
{
$this->siteName = $siteName;
$this->sectionName = $sectionName;
}
}
7 changes: 6 additions & 1 deletion _api_app/app/Http/Controllers/StateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Configuration\SiteSettingsConfigService;
use App\Configuration\SiteTemplatesConfigService;
use App\Shared\Helpers;
use App\Sites\ThemesDataService;
use App\Sites\Sections\Entries\SectionEntriesDataService;
use App\Sites\Sections\SiteSectionsDataService;
use App\Sites\Sections\Tags\SectionTagsDataService;
Expand All @@ -30,6 +31,8 @@ public function get($site='')
'siteSettingsUpload' => route('site_settings_upload'),
'siteTemplateSettings' => route('site_template_settings'),
'siteTemplateSettingsUpload' => route('site_template_settings_upload'),
'siteThemePreview' => route('site_theme_preview'),
'siteThemeApply' => route('site_theme_apply'),
'localeSettings' => route('locale_settings'),
'siteSections' => route('site_sections'),
'siteSectionsReset' => route('site_sections_reset'),
Expand Down Expand Up @@ -95,12 +98,14 @@ public function getMeta()
{
include realpath(config('app.old_berta_root') . '/engine/inc.version.php');
$user = new UserModel();
$themesDS = new ThemesDataService();
$meta = [
'version' => $options['version'],
'forgotPasswordUrl' => $user->forgot_password_url,
'loginUrl' => $user->profile_url ? $user->profile_url : route('login'),
'authenticateUrl' => route('authenticate'),
'isBertaHosting' => $user->profile_url != false
'isBertaHosting' => $user->profile_url != false,
'themes' => $themesDS->getThemes()
];

return Helpers::api_response('', $meta);
Expand Down
4 changes: 3 additions & 1 deletion _api_app/app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
$app->put('sites', 'SitesController@order');
$app->delete('sites', 'SitesController@delete');

$app->post('sites/theme-preview', ['as' => 'site_theme_preview', 'uses' => 'SitesController@themePreview']);
$app->put('sites/theme-apply', ['as' => 'site_theme_apply', 'uses' => 'SitesController@themeApply']);

$app->get('sites/render-menu[/{site}]', 'SitesController@renderMenu');

$app->patch('sites/settings', ['as' => 'site_settings', 'uses' => 'Settings\SiteSettingsController@update']);
Expand Down Expand Up @@ -94,4 +97,3 @@
if (app()->environment('local', 'stage')) {
require __DIR__ . '/../Dev/testRoutes.php';
}

31 changes: 31 additions & 0 deletions _api_app/app/Listeners/UnsetSectionDemoStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Listeners;

use App\Events\SectionUpdated;
use App\Sites\Sections\SiteSectionsDataService;

class UnsetSectionDemoStatus
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param ExampleEvent $event
* @return void
*/
public function handle(SectionUpdated $event)
{
$siteSectionsDS = new SiteSectionsDataService($event->siteName);
$siteSectionsDS->unsetDemoStatus($event->sectionName);
}
}
4 changes: 2 additions & 2 deletions _api_app/app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
'App\Events\SectionUpdated' => [
'App\Listeners\UnsetSectionDemoStatus',
],
];
}
60 changes: 39 additions & 21 deletions _api_app/app/Shared/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@ class Storage

protected $SITE = '';
protected $XML_MAIN_ROOT;
protected $XML_STORAGE_ROOT;
protected $XML_SITES_ROOT;
protected $XML_PREVIEW_ROOT;
protected $THEMES_ROOT;
public $MEDIA_ROOT;
public $MEDIA_URL;
protected static $DEFAULT_VALUES = [];

private $MEDIA_FOLDER = 'media';
public $MEDIA_FOLDER = 'media';
private $PREVIEW_FOLDER = 'preview';

public function __construct($site = '')
public function __construct($site = '', $isPreview = false)
{
$this->SITE = $site;
$this->XML_MAIN_ROOT = realpath(config('app.old_berta_root') . '/storage');
$this->XML_STORAGE_ROOT = $this->XML_MAIN_ROOT;
$this->XML_SITES_ROOT = $this->XML_MAIN_ROOT . '/-sites';
$this->THEMES_ROOT = realpath(config('app.old_berta_root') . '/_themes');
$preview_folder = $isPreview ? $this->PREVIEW_FOLDER . '/' : '';

if (!empty($site) and $site !== '0') {
$this->MEDIA_ROOT = $this->XML_SITES_ROOT . '/' . $site . '/' . $this->MEDIA_FOLDER;
$this->MEDIA_URL = '/storage/-sites/' . $site . '/' . $this->MEDIA_FOLDER;
$this->XML_STORAGE_ROOT = $this->XML_SITES_ROOT . '/' . $site;
$this->XML_PREVIEW_ROOT = $this->XML_STORAGE_ROOT . '/' . $this->PREVIEW_FOLDER;
$this->MEDIA_ROOT = $this->XML_SITES_ROOT . '/' . $site . '/' . $preview_folder . $this->MEDIA_FOLDER;
$this->MEDIA_URL = '/storage/-sites/' . $site . '/' . $preview_folder . $this->MEDIA_FOLDER;
} else {
$this->MEDIA_ROOT = $this->XML_MAIN_ROOT . '/' . $this->MEDIA_FOLDER;
$this->MEDIA_URL = '/storage/' . $this->MEDIA_FOLDER;
$this->XML_PREVIEW_ROOT = $this->XML_MAIN_ROOT . '/' . $this->PREVIEW_FOLDER;
$this->MEDIA_ROOT = $this->XML_MAIN_ROOT . '/' . $preview_folder . $this->MEDIA_FOLDER;
$this->MEDIA_URL = '/storage/' . $preview_folder . $this->MEDIA_FOLDER;
}
}

Expand Down Expand Up @@ -62,10 +72,10 @@ protected function asList($val)
{
if (is_array($val)) {
if (array_values($val) !== $val) {
return array(0 => $val);
return [0 => $val];
}
} else {
return array(0 => $val);
return [0 => $val];
}

return $val;
Expand Down Expand Up @@ -163,7 +173,8 @@ protected function getUniqueFileName($dir, $fileName)
preg_match('/(.+)_([0-9])+$/', $name, $nrFileParts);

if (!$nrFileParts) {
$fileName = $name . '_1.' . $extension;;
$fileName = $name . '_1.' . $extension;
;
if (!file_exists($dir . '/' . $fileName)) {
return $fileName;
}
Expand All @@ -172,15 +183,19 @@ protected function getUniqueFileName($dir, $fileName)

$dirContent = [];
foreach (scandir($dir) as $file) {
if (!is_file($dir . '/' . $file)) { continue; }
if (!is_file($dir . '/' . $file)) {
continue;
}
$dirContent[] = $file;
}

$maxFileIdx = intval($nrFileParts[2]);

foreach ($dirContent as $file) {
preg_match('/' .$name . '_([0-9]).' . $extension . '/', $file, $nrParts);
if (!$nrParts) { continue; }
preg_match('/' . $name . '_([0-9]).' . $extension . '/', $file, $nrParts);
if (!$nrParts) {
continue;
}
$idx = intval($nrParts[1]);
if ($idx > $maxFileIdx) {
$maxFileIdx = $idx;
Expand Down Expand Up @@ -255,7 +270,6 @@ protected function array2xmlFile($arr, $xml_file, $root)
protected function xmlFile2array($xml_file)
{
if (file_exists($xml_file)) {

$fp = fopen($xml_file, 'r');
if (flock($fp, LOCK_SH)) {
$xml_str = file_get_contents($xml_file);
Expand All @@ -275,20 +289,24 @@ protected function xmlFile2array($xml_file)
return $this->xml2array($xml->documentElement);
}

return array();
return [];
}

/**
*/
protected function copyFolder($src, $dst)
{
if (!is_dir($src)) {
return;
}

$dir = opendir($src);
@mkdir($dst, 0777, true);

while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
if ($file != '-sites') {
if (!in_array($file, ['-sites', $this->PREVIEW_FOLDER])) {
self::copyFolder($src . '/' . $file, $dst . '/' . $file);
}
} else {
Expand All @@ -308,7 +326,7 @@ protected function delFolder($dir)
return;
}

$files = array_diff(scandir($dir), array('.', '..'));
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file") && !is_link($dir)) ? $this->delFolder("$dir/$file") : unlink("$dir/$file");
}
Expand All @@ -334,7 +352,7 @@ protected function removeXMLInvalidChars($string)
* @param string $arr Array to convert to XML
* @return mixed Node to append to XML document
*/
protected function array2xml($xml, $node_name, $arr = array())
protected function array2xml($xml, $node_name, $arr = [])
{
$node = $xml->createElement($node_name);

Expand Down Expand Up @@ -420,7 +438,7 @@ private function isValidTagName($tag)
*/
private function xml2array($node)
{
$output = array();
$output = [];

switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
Expand All @@ -443,7 +461,7 @@ private function xml2array($node)

// assume more nodes of same kind are coming
if (!isset($output[$tag_name])) {
$output[$tag_name] = array();
$output[$tag_name] = [];
}

$output[$tag_name][] = $subtree;
Expand Down Expand Up @@ -471,15 +489,15 @@ private function xml2array($node)

// loop through the attributes and collect them
if ($node->attributes->length) {
$attrs = array();
$attrs = [];

foreach ($node->attributes as $attrName => $attrNode) {
$attrs[$attrName] = (string)$attrNode->value;
}

// if its an leaf node, store the value in @value instead of directly storing it.
if (!is_array($output)) {
$output = array('@value' => $output);
$output = ['@value' => $output];
}

$output['@attributes'] = $attrs;
Expand Down
39 changes: 32 additions & 7 deletions _api_app/app/Sites/Sections/Entries/SectionEntriesDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Shared\Storage;
use App\Shared\ImageHelpers;
use App\Events\SectionUpdated;
use App\Sites\Settings\SiteSettingsDataService;
use App\Sites\TemplateSettings\SiteTemplateSettingsDataService;
use App\Sites\Sections\SiteSectionsDataService;
Expand Down Expand Up @@ -181,10 +182,10 @@ class SectionEntriesDataService extends Storage
private $XML_ROOT;
private $XML_FILE;

public function __construct($site = '', $sectionName = '', $sectionTitle = '')
public function __construct($site = '', $sectionName = '', $sectionTitle = '', $xml_root = null, $isPreview=false)
{
parent::__construct($site);
$this->XML_ROOT = $this->getSiteXmlRoot($site);
parent::__construct($site, $isPreview);
$this->XML_ROOT = $xml_root ? $xml_root : $this->getSiteXmlRoot($site);
$this->SECTION_NAME = $sectionName;
$this->SECTION_TITLE = $sectionTitle;
$this->XML_FILE = $this->XML_ROOT . '/blog.' . $sectionName . '.xml';
Expand Down Expand Up @@ -272,11 +273,13 @@ public function getByTag($tag = null, $isEditMode = false)

$siteSectionsDataService = new SiteSectionsDataService($this->SITE);
$sections = $siteSectionsDataService->get();
$section_order = array_search($this->SECTION_NAME, array_column($sections, 'name'));
$section = $sections[$section_order];
$sectionType = isset($section['@attributes']['type']) ? $section['@attributes']['type'] : 'default';
if (!empty($sections)) {
$section_order = array_search($this->SECTION_NAME, array_column($sections, 'name'));
$section = $sections[$section_order];
$sectionType = isset($section['@attributes']['type']) ? $section['@attributes']['type'] : 'default';
}

$isResponsive = $sectionType == 'portfolio' || $isResponsiveTemplate;
$isResponsive = (isset($sectionType) && $sectionType == 'portfolio') || $isResponsiveTemplate;

// if messy template and auto responsive is ON and environment is `site`
// reorder entries based on XY position
Expand Down Expand Up @@ -377,6 +380,7 @@ public function saveValueByPath($path, $value)
);

$this->array2xmlFile($entries, $this->XML_FILE, $this->ROOT_ELEMENT);
event(new SectionUpdated($this->SITE, $this->SECTION_NAME));

$ret['entry'] = $entries[self::$ROOT_LIST_ELEMENT][$index];

Expand Down Expand Up @@ -486,6 +490,7 @@ public function order($entry_id, $value)

array_splice($entries['entry'], $entry_new_order, 0, $entry_to_move);
$this->array2xmlFile($entries, $this->XML_FILE, $this->ROOT_ELEMENT);
event(new SectionUpdated($this->SITE, $this->SECTION_NAME));

$order = array_column($entries['entry'], 'id');

Expand Down Expand Up @@ -857,6 +862,7 @@ public function galleryOrder($section_name, $entry_id, $new_files)
$entry['mediaCacheData']['file'] = $new_files ? $reordered : [];

$this->array2xmlFile($entries, $this->XML_FILE, $this->ROOT_ELEMENT);
event(new SectionUpdated($this->SITE, $this->SECTION_NAME));

return [
'site' => $this->SITE,
Expand Down Expand Up @@ -1083,4 +1089,23 @@ private function deleteMedia($folder, $file = '')
closedir($handle);
}
}

/**
* Copy all section entries gallery files to destination folder
* @param string $dst_root destination folder
*/
public function copyMediaFiles($dst_root)
{
$entries = $this->get();
$entries = $entries[self::$ROOT_LIST_ELEMENT];

foreach ($entries as $entry) {
if (isset($entry['mediafolder'])) {
$this->copyFolder(
$this->XML_ROOT . '/' . $this->MEDIA_FOLDER . '/' . $entry['mediafolder'],
$dst_root . '/' . $this->MEDIA_FOLDER . '/' . $entry['mediafolder']
);
}
}
}
}
Loading

0 comments on commit d6af4a6

Please sign in to comment.