Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next/prev/up buttons to the resource panel #15464

Open
wants to merge 5 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions core/src/Revolution/modResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -1584,4 +1584,38 @@ public function clearCache($context = '')
$this->xpdo->invokeEvent('OnResourceCacheUpdate', ['id' => $this->get('id')]);
}
}

/**
* Returns array with all neighborhood resources
*
* @return array $arr Array with neighborhood from left and right
*/
public function getNeighborhood()
{
$arr = [];

$q = $this->xpdo->newQuery(modResource::class, ['parent' => $this->parent]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like this approach. On large sites, this basically means that everytime you open resource to edit you'll fetch all resources under the same parent, which could be hundreds or even thousands of records in some cases, just to get 2 specific IDs.

Wouldn't be better to query directly for the resource under and above the current one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was idea for modx docs, not for modx CMS) my mistake

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's one way to get the prev/next without having to get all resources: add a where that matches the sortby.

For the next:

$qNext->where([
     'id:!=' => $this->get('id'),
     'AND:menuindex:<=' => $this->get('menuindex'),
]);
$qNext->limit(1);
$qNext->sortby('menuindex', 'ASC');

For the previous:

$qPrevious->where([
     'id:!=' => $this->get('id'),
     'AND:menuindex:>=' => $this->get('menuindex'),
]);
$qPrevious->limit(1);
$qPrevious->sortby('menuindex', 'ASC');

The "equals or larger/smaller" makes sure that items with the same menuindex still appear.

It means two queries, but will be much more optimized than loading all siblings, especially on large sites.

$q->sortby('menuindex', 'ASC');
$q->select('id');
if ($q->prepare() && $q->stmt->execute()) {
$ids = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
$current = array_search($this->id, $ids);

$right = $left = [];
foreach ($ids as $k => $v) {
if ($k > $current) {
$right[] = $v;
} elseif ($k < $current) {
$left[] = $v;
}
}

$arr = [
'left' => array_reverse($left),
'right' => $right,
];
}

return $arr;
}
}
45 changes: 45 additions & 0 deletions manager/assets/modext/sections/resource/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Ext.extend(MODx.page.UpdateResource,MODx.Component,{
,scope: this
});

btns.push(this.getAdditionalButtons(config));

btns.push({
process: 'Resource/Update'
,text: _('save') + ' <i class="icon icon-check"></i>'
Expand All @@ -242,5 +244,48 @@ Ext.extend(MODx.page.UpdateResource,MODx.Component,{

return btns;
}

,getAdditionalButtons: function (config) {
return [{
text: '<i class="icon icon-arrow-left"></i>',
handler: this.prevPage,
disabled: !config['prev_page'],
scope: this,
tooltip: _('prev_page'),
keys: [{key: 37, alt: true, scope: this, fn: this.prevPage}]
}, {
text: '<i class="icon icon-arrow-up"></i>',
handler: this.upPage,
scope: this,
disabled: !config['up_page'],
tooltip: _('up_page'),
keys: [{key: 38, alt: true, scope: this, fn: this.upPage}]
}, {
text: '<i class="icon icon-arrow-right"></i>',
handler: this.nextPage,
disabled: !config['next_page'],
scope: this,
tooltip: _('next_page'),
keys: [{key: 39, alt: true, scope: this, fn: this.nextPage}]
}];
}

,prevPage: function () {
if (this.config['prev_page'] > 0) {
MODx.loadPage('resource/update', 'id=' + this.config['prev_page'])
}
}

,nextPage: function () {
if (this.config['next_page'] > 0) {
MODx.loadPage('resource/update', 'id=' + this.config['next_page'])
}
}

,upPage: function () {
if (this.config['up_page'] > 0) {
MODx.loadPage('resource/update', 'id=' + this.config['up_page'])
}
}
});
Ext.reg('modx-page-resource-update',MODx.page.UpdateResource);
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function loadCustomCssJs()
$this->addJavascript($mgrUrl . 'assets/modext/widgets/resource/modx.panel.resource.static.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/update.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/static/update.js');
$neighborhood = $this->resource->getNeighborhood();
$data = [
'xtype' => 'modx-page-static-update',
'resource' => $this->resource->get('id'),
Expand All @@ -44,6 +45,13 @@ public function loadCustomCssJs()
'canCreate' => (int)$this->modx->hasPermission('new_document'),
'canDelete' => (int)$this->modx->hasPermission('delete_document'),
'show_tvs' => (int)!empty($this->tvCounts),
'next_page' => !empty($neighborhood['right'][0])
? $neighborhood['right'][0]
: 0,
'prev_page' => !empty($neighborhood['left'][0])
? $neighborhood['left'][0]
: 0,
'up_page' => $this->resource->parent
];
$this->addHtml('<script>
MODx.config.publish_document = "' . $this->canPublish . '";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function loadCustomCssJs()
$this->addJavascript($mgrUrl . 'assets/modext/widgets/resource/modx.panel.resource.symlink.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/update.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/symlink/update.js');
$neighborhood = $this->resource->getNeighborhood();
$data = [
'xtype' => 'modx-page-symlink-update',
'resource' => $this->resource->get('id'),
Expand All @@ -44,6 +45,13 @@ public function loadCustomCssJs()
'canDuplicate' => (int)$this->canDuplicate,
'canDelete' => (int)$this->canDelete,
'show_tvs' => (int)!empty($this->tvCounts),
'next_page' => !empty($neighborhood['right'][0])
? $neighborhood['right'][0]
: 0,
'prev_page' => !empty($neighborhood['left'][0])
? $neighborhood['left'][0]
: 0,
'up_page' => $this->resource->parent
];
$this->addHtml('<script>
MODx.config.publish_document = "' . $this->canPublish . '";
Expand Down
8 changes: 8 additions & 0 deletions manager/controllers/default/resource/update.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function loadCustomCssJs()
$this->addJavascript($mgrUrl . 'assets/modext/widgets/resource/modx.panel.resource.tv.js');
$this->addJavascript($mgrUrl . 'assets/modext/widgets/resource/modx.panel.resource.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/update.js');
$neighborhood = $this->resource->getNeighborhood();
$data = [
'xtype' => 'modx-page-resource-update',
'resource' => $this->resource->get('id'),
Expand All @@ -61,6 +62,13 @@ public function loadCustomCssJs()
'canDelete' => (int)$this->canDelete,
'show_tvs' => (int)!empty($this->tvCounts),
'mode' => 'update',
'next_page' => !empty($neighborhood['right'][0])
? $neighborhood['right'][0]
: 0,
'prev_page' => !empty($neighborhood['left'][0])
? $neighborhood['left'][0]
: 0,
'up_page' => $this->resource->parent
];
$this->addHtml('<script>
MODx.config.publish_document = "' . $this->canPublish . '";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function loadCustomCssJs()
$this->addJavascript($mgrUrl . 'assets/modext/widgets/resource/modx.panel.resource.weblink.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/update.js');
$this->addJavascript($mgrUrl . 'assets/modext/sections/resource/weblink/update.js');
$neighborhood = $this->resource->getNeighborhood();
$data = [
'xtype' => 'modx-page-weblink-update',
'resource' => $this->resource->get('id'),
Expand All @@ -44,6 +45,13 @@ public function loadCustomCssJs()
'canDuplicate' => (int)$this->canDuplicate,
'canDelete' => (int)$this->canDelete,
'show_tvs' => (int)!empty($this->tvCounts),
'next_page' => !empty($neighborhood['right'][0])
? $neighborhood['right'][0]
: 0,
'prev_page' => !empty($neighborhood['left'][0])
? $neighborhood['left'][0]
: 0,
'up_page' => $this->resource->parent
];
$this->addHtml('<script>
MODx.config.publish_document = "' . $this->canPublish . '";
Expand Down