Skip to content

Commit

Permalink
Merge pull request #368 from rainlab/wip/halcyon-db-datasource
Browse files Browse the repository at this point in the history
Support new CMS DB layer
  • Loading branch information
daftspunk authored Jun 1, 2019
2 parents af38de9 + 60cf59f commit 7092b73
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 198 deletions.
17 changes: 15 additions & 2 deletions assets/js/pages-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
$(document).on('submenu.oc.treeview', 'form.layout[data-content-id=pages]', this.proxy(this.onSidebarSubmenuItemClick))

// The Delete Object button click
$(document).on('click', '#pages-side-panel form button[data-control=delete-object], #pages-side-panel form button[data-control=delete-template]',
$(document).on('click', '#pages-side-panel form button[data-control=delete-object], #pages-side-panel form button[data-control=delete-template]',
this.proxy(this.onDeleteObject))

// A new tab is added to the editor
Expand Down Expand Up @@ -204,6 +204,10 @@
var $form = $(event.currentTarget),
$tabPane = $form.closest('.tab-pane')

// Update the visibilities of the commit & reset buttons
$('[data-control=commit-button]', $form).toggleClass('hide', !data.canCommit)
$('[data-control=reset-button]', $form).toggleClass('hide', !data.canReset)

if (data.objectPath !== undefined) {
$('input[name=objectPath]', $form).val(data.objectPath)
$('input[name=objectMtime]', $form).val(data.objectMtime)
Expand All @@ -228,11 +232,18 @@
$('[data-control=filelist]', this.$sidePanel).fileList('markActive', tabId)

var objectType = $('input[name=objectType]', $form).val()
if (objectType.length > 0 && context.handler == 'onSave')
if (objectType.length > 0 &&
(context.handler == 'onSave' || context.handler == 'onCommit' || context.handler == 'onReset')
)
this.updateObjectList(objectType)

if (context.handler == 'onSave' && (!data['X_OCTOBER_ERROR_FIELDS'] && !data['X_OCTOBER_ERROR_MESSAGE']))
$form.trigger('unchange.oc.changeMonitor')

// Reload the form if the server has requested it
if (data.forceReload) {
this.reloadForm($form)
}
}

PagesPage.prototype.onBeforeSaveContent = function(e, data) {
Expand Down Expand Up @@ -504,6 +515,8 @@

$form.on('changed.oc.changeMonitor', function() {
$panel.trigger('modified.oc.tab')
$panel.find('[data-control=commit-button]').addClass('hide');
$panel.find('[data-control=reset-button]').addClass('hide');
self.updateModifiedCounter()
})

Expand Down
164 changes: 29 additions & 135 deletions classes/Menu.php
Original file line number Diff line number Diff line change
@@ -1,118 +1,64 @@
<?php namespace RainLab\Pages\Classes;

use Url;
use File;
use Lang;
use Yaml;
use Event;
use Config;
use Request;
use Validator;
use RainLab\Pages\Classes\MenuItem;
use RainLab\Pages\Classes\MenuItemReference;
use Cms\Classes\Theme;
use Cms\Classes\CmsObject;
use Cms\Classes\Controller as CmsController;
use October\Rain\Support\Str;
use October\Rain\Router\Helper as RouterHelper;
use ApplicationException;
use ValidationException;
use SystemException;
use DirectoryIterator;
use Exception;
use Cms\Classes\Meta;
use October\Rain\Support\Str;

/**
* Represents a front-end menu.
*
* @package rainlab\pages
* @author Alexey Bobkov, Samuel Georges
*/
class Menu extends CmsObject
class Menu extends Meta
{
/**
* @var string The container name associated with the model, eg: pages.
*/
protected $dirName = 'meta/menus';

/**
* @var array Cache store used by parseContent method.
*/
protected $contentDataCache;

/**
* @var array Allowable file extensions.
*/
protected $allowedExtensions = ['yaml'];

/**
* @var string Default file extension.
*/
protected $defaultExtension = 'yaml';

/**
* @var array The attributes that are mass assignable.
*/
protected $fillable = [
'content',
'code',
'name',
'itemData'
'itemData',
];

/**
* @var array List of attribute names which are not considered "settings".
*/
protected $purgeable = [
'code',
'name',
'itemData'
];

/**
* Triggered before the menu is saved.
* @return void
* @var array The rules to be applied to the data.
*/
public function beforeSave()
{
$this->content = $this->renderContent();
}
public $rules = [
'code' => 'required|regex:/^[0-9a-z\-\_]+$/i',
];

/**
* Validate custom attributes.
* @return void
* @var array The array of custom error messages.
*/
public function beforeValidate()
{
if (!strlen($this->code)) {
throw new ValidationException([
'code' => Lang::get('rainlab.pages::lang.menu.code_required')
]);
}

if (!preg_match('/^[0-9a-z\-\_]+$/i', $this->code)) {
throw new ValidationException([
'code' => Lang::get('rainlab.pages::lang.menu.invalid_code')
]);
}
}
public $customMessages = [
'required' => 'rainlab.pages::lang.menu.code_required',
'regex' => 'rainlab.pages::lang.menu.invalid_code',
];

/**
* Returns the menu code.
* @return string
*/
public function getCodeAttribute()
{
if (isset($this->attributes['code'])) {
return $this->attributes['code'];
}

$place = strrpos($this->fileName, '.');

if ($place !== false) {
return substr($this->fileName, 0, $place);
}

return null;
return $this->getBaseFileName();
}

/**
Expand All @@ -124,60 +70,39 @@ public function setCodeAttribute($code)
{
$code = trim($code);

$this->attributes['code'] = $code;

if (strlen($code)) {
$this->fileName = $code.'.yaml';
$this->attributes = array_merge($this->attributes, ['code' => $code]);
}

return $this;
}

/**
* Returns a default value for name attribute.
* @return string
*/
public function getNameAttribute()
{
if (array_key_exists('name', $this->attributes)) {
return $this->attributes['name'];
}

return $this->attributes['name'] = array_get($this->parseContent(), 'name');
}

/**
* Returns a default value for items attribute.
* Items are objects of the \RainLab\Pages\Classes\MenuItem class.
* @return array
*/
public function getItemsAttribute()
{
if (array_key_exists('items', $this->attributes)) {
return $this->attributes['items'];
}

if ($items = array_get($this->parseContent(), 'items')) {
$itemObjects = MenuItem::initFromArray($items);
}
else {
$itemObjects = [];
$items = [];
if (!empty($this->attributes['items'])) {
$items = MenuItem::initFromArray($this->attributes['items']);
}

return $this->attributes['items'] = $itemObjects;
return $items;

This comment has been minimized.

Copy link
@acasar

acasar Jun 10, 2019

Contributor

@daftspunk something strange is happening here. In my project $this->attributes['items'] is always empty and all menus are empty as well after updating to this revision. Before this commit parseContent() was used, which populated items correctly.

cc @LukeTowers

This comment has been minimized.

Copy link
@LukeTowers

LukeTowers Jun 10, 2019

Contributor

@acasar I believe this was fixed in octobercms/october#4362

}

/**
* Returns a default value for itemData attribute.
* @return array
* Store the itemData in the items attribute
*
* @param array $data
* @return void
*/
public function getItemDataAttribute()
public function setItemDataAttribute($data)
{
if (array_key_exists('itemData', $this->attributes)) {
return $this->attributes['itemData'];
}

return $this->attributes['itemData'] = array_get($this->parseContent(), 'items');
$this->items = $data;
return $this;
}

/**
Expand All @@ -186,44 +111,13 @@ public function getItemDataAttribute()
*/
protected function parseContent()
{
if ($this->contentDataCache !== null) {
return $this->contentDataCache;
}

$parsedData = Yaml::parse($this->content);

if (!is_array($parsedData)) {
return null;
}
$parsedData = parent::parseContent();

if (!array_key_exists('name', $parsedData)) {
throw new SystemException(sprintf('The content of the %s file is invalid: the name element is not found.', $fileName));
throw new SystemException(sprintf('The content of the %s file is invalid: the name element is not found.', $this->fileName));
}

return $this->contentDataCache = $parsedData;
}

/**
* Compile the content for this CMS object, used by the theme logger.
* @return string
*/
public function toCompiled()
{
return $this->renderContent();
}

/**
* Renders the menu data as a content string in YAML format.
* @return string
*/
protected function renderContent()
{
$contentData = [
'name' => $this->name,
'items' => $this->itemData ? $this->itemData : []
];

return Yaml::render($contentData);
return $parsedData;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MenuItem
public $viewBag = [];

/**
* Initializes a menu item from a data array.
* Initializes a menu item from a data array.
* @param array $items Specifies the menu item data.
* @return Returns an array of the MenuItem objects.
*/
Expand Down
56 changes: 15 additions & 41 deletions classes/PageList.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<?php namespace RainLab\Pages\Classes;

use Yaml;
use Lang;
use File;
use ApplicationException;
use Cms\Classes\Meta;
use RainLab\Pages\Classes\Page;
use SystemException;
use DirectoryIterator;

/**
* The page list class reads and manages the static page hierarchy.
Expand Down Expand Up @@ -146,31 +141,6 @@ public function getPageSubTree($page)
return $subTree;
}

/**
* Updates the page hierarchy structure in the theme's meta/static-pages.yaml file.
* @param array $structure A nested associative array representing the page structure
*/
public function updateStructure($structure)
{
$originalData = $this->getPagesConfig();
$originalData['static-pages'] = $structure;

$yamlData = Yaml::render($originalData);

$filePath = $this->getConfigFilePath();
$dirPath = dirname($filePath);

if (!file_exists($dirPath) || !is_dir($dirPath)) {
if (!File::makeDirectory($dirPath, 0777, true, true)) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_creating_directory', ['name' => $dirPath]));
}
}

if (@File::put($filePath, $yamlData) === false) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_saving', ['name' => $filePath]));
}
}

/**
* Appends page to the page hierarchy.
* The page can be added to the end of the hierarchy or as a subpage to any existing page.
Expand Down Expand Up @@ -242,26 +212,30 @@ protected function getPagesConfig()
return self::$configCache;
}

$filePath = $this->getConfigFilePath();
$config = Meta::loadCached($this->theme, 'static-pages.yaml');

if (!file_exists($filePath)) {
return self::$configCache = ['static-pages' => []];
if (!$config) {
$config = new Meta();
$config->fileName = 'static-pages.yaml';
$config['static-pages'] = [];
$config->save();
}

$config = Yaml::parse(File::get($filePath));
if (!array_key_exists('static-pages', $config)) {
throw new SystemException('The content of the theme meta/static-pages.yaml file is invalid: the "static-pages" root element is not found.');
if (!isset($config->attributes['static-pages'])) {
$config['static-pages'] = [];
}

return self::$configCache = $config;
}

/**
* Returns an absolute path to the meta/static-pages.yaml file.
* @return string
* Updates the page hierarchy structure in the theme's meta/static-pages.yaml file.
* @param array $structure A nested associative array representing the page structure
*/
protected function getConfigFilePath()
public function updateStructure($structure)
{
return $this->theme->getPath().'/meta/static-pages.yaml';
$config = $this->getPagesConfig();
$config['static-pages'] = $structure;
$config->save();
}
}
Loading

0 comments on commit 7092b73

Please sign in to comment.