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 drag'n'drop sorting to banners #27

Open
wants to merge 2 commits into
base: master
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
42 changes: 38 additions & 4 deletions assets/components/bannery/js/mgr/widgets/banners.grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ Bannery.grid.Ads = function(config) {
return '';
}
}
,ddGroup: 'dd'
,enableDragDrop: true
,listeners: {
rowDblClick: function(grid, rowIndex, e) {
rowDblClick: function (grid, rowIndex, e) {
var row = grid.store.getAt(rowIndex);
this.updateAd(grid, e, row);
}
},
render: {fn: this._initDD, scope: this}
}
});

Expand All @@ -85,8 +88,39 @@ Bannery.grid.Ads = function(config) {
Bannery.grid.Ads.superclass.constructor.call(this,config);
};
Ext.extend(Bannery.grid.Ads,MODx.grid.Grid,{
getMenu: function(grid,idx) {
var icon = 'x-menu-item-icon icon icon-';
_initDD: function(grid) {
new Ext.dd.DropTarget(grid.el, {
ddGroup : 'dd',
copy:false,
notifyDrop : function(dd, e, data) {
var store = grid.store.data.items;
var target = store[dd.getDragData(e).rowIndex].data;
var source = store[data.rowIndex].data;
var position = grid.store.baseParams.position;
if (position === undefined || position == "") { return; }
if ((target.parent == source.parent) && (target.id != source.id)) {
dd.el.mask(_('loading'),'x-mask-loading');
MODx.Ajax.request({
url: Bannery.config.connectorUrl
,params: {
action: 'mgr/ads/sort'
,source: source.id
,target: target.id
,position: position
}
,listeners: {
success: {fn:function(r) { dd.el.unmask(); grid.refresh();},scope:grid}
,failure: {fn:function(r) { dd.el.unmask();},scope:grid}
}
});
}
}
});
}
,getMenu: function(grid,idx) {
var icon = MODx.modx23
? 'x-menu-item-icon icon icon-'
: 'x-menu-item-icon fa fa-';
var row = grid.store.data.items[idx]
var m = [{
text: '<i class="' + icon + 'edit"></i> ' + _('bannery.ads.update')
Expand Down
20 changes: 7 additions & 13 deletions core/components/bannery/processors/mgr/ads/getlist.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ function prepareQueryBeforeCount(xPDOQuery $c) {
// Filter by position
if ($position = $this->getProperty('position')) {
$mode = $this->getProperty('mode','include');

$q = $this->modx->newQuery('byAdPosition');
$q->select('ad');
$q->where(array('position' => $position));
if ($q->prepare() && $q->stmt->execute()) {
$ads = array_unique($q->stmt->fetchAll(PDO::FETCH_COLUMN));
$c->innerJoin('byAdPosition', 'byAdPosition', array('`byAdPosition`.`ad` = `byAd`.`id`'));
if ($mode == 'exclude') {
$c->where(array("byAdPosition.position:!="=>$position));
}
if (!empty($ads)) {
if ($mode == 'exclude') {
$c->where(array('id:NOT IN' => $ads));
}
else {
$c->where(array('id:IN' => $ads));
}
else {
$c->where(array("byAdPosition.position"=>$position));
}

$c->sortby("byAdPosition.idx,id", "ASC");
}
// Filter by search query
if ($query = $this->getProperty('query')) {
Expand Down
57 changes: 57 additions & 0 deletions core/components/bannery/processors/mgr/ads/sort.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
class AdSortProcessor extends modObjectProcessor {
public $classKey = 'byAdPosition';
public $languageTopics = array('bannery:default');
public $objectType = 'bannery.ad';
private $pos;
public function process() {
/* @var string $pos */
$this->pos = $this->getProperty('position');
/* @var byAd $source */
$source = $this->modx->getObject($this->classKey, array('ad'=>$this->getProperty('source'), 'position'=>$this->pos));
/* @var byAd $target */
$target = $this->modx->getObject($this->classKey, array('ad'=>$this->getProperty('target'), 'position'=>$this->pos));
if (empty($source) || empty($target)) {
return $this->modx->error->failure();
}
$table = $this->modx->getTableName($this->classKey);
if ($source->get('idx') < $target->get('idx')) {
$this->modx->exec("UPDATE {$table}
SET idx = idx - 1 WHERE
position = {$this->pos}
AND idx <= {$target->get('idx')}
AND idx > {$source->get('idx')}
AND idx > 0
");
} else {
$this->modx->exec("UPDATE {$table}
SET idx = idx + 1 WHERE
position = {$this->pos}
AND idx >= {$target->get('idx')}
AND idx < {$source->get('idx')}
");
}
$newRank = $target->get('idx');
$source->set('idx',$newRank);
$source->save();
if (!$this->modx->getCount($this->classKey, array('position' => $this->pos))) {
$this->setIndex();
}
return $this->modx->error->success();
}
public function setIndex() {
$q = $this->modx->newQuery($this->classKey, array('position' => $this->pos));
$q->select('id');
$q->sortby('idx ASC, id', 'ASC');
if ($q->prepare() && $q->stmt->execute()) {
$ids = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
$sql = '';
$table = $this->modx->getTableName($this->classKey);
foreach ($ids as $k => $id) {
$sql .= "UPDATE {$table} SET `idx` = '{$k}' WHERE `id` = '{$id}';";
}
$this->modx->exec($sql);
}
}
}
return 'AdSortProcessor';