diff --git a/assets/components/bannery/js/mgr/widgets/banners.grid.js b/assets/components/bannery/js/mgr/widgets/banners.grid.js index 0ecb79c..e414ca0 100644 --- a/assets/components/bannery/js/mgr/widgets/banners.grid.js +++ b/assets/components/bannery/js/mgr/widgets/banners.grid.js @@ -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} } }); @@ -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: ' ' + _('bannery.ads.update') diff --git a/core/components/bannery/processors/mgr/ads/getlist.class.php b/core/components/bannery/processors/mgr/ads/getlist.class.php index 6ec833a..4d14678 100644 --- a/core/components/bannery/processors/mgr/ads/getlist.class.php +++ b/core/components/bannery/processors/mgr/ads/getlist.class.php @@ -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')) { diff --git a/core/components/bannery/processors/mgr/ads/sort.class.php b/core/components/bannery/processors/mgr/ads/sort.class.php new file mode 100644 index 0000000..5508245 --- /dev/null +++ b/core/components/bannery/processors/mgr/ads/sort.class.php @@ -0,0 +1,57 @@ +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'; \ No newline at end of file