Skip to content

Commit

Permalink
adding ordering to the cms categoires, includes helper method for the…
Browse files Browse the repository at this point in the history
… links and modding the admin_reorder method to work with both mptt and sequenced models
  • Loading branch information
dogmatic69 committed Feb 11, 2010
1 parent 2cf52a4 commit 7270971
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 9 deletions.
81 changes: 75 additions & 6 deletions app_controller.php
Expand Up @@ -152,9 +152,10 @@ function __getClassName() {
* reorder records.
*
* uses named paramiters can use the following:
* - up: moves the record up.
* - down: moves the record down.
* - position: sets the position for the record.
* - position: sets the position for the record for sequenced models
* - possition needs the new possition of the record
*
* - direction: for MPTT and only needs the record id.
*
* @param int $id the id of the record to move.
* @return does a redirect to the referer.
Expand All @@ -169,14 +170,82 @@ function admin_reorder($id = null) {

$this->$model->id = $id;

if (!isset($this->params['named']['possition'])) {
$this->Session->setFlash(__('A problem occured moving the record.', true));
if (!isset($this->params['named']['possition']) && isset($this->$model->actsAs['Libs.Sequence'])) {
$this->Session->setFlash(__('A problem occured moving the ordered record.', true));
$this->redirect($this->referer());
}

if (!isset($this->params['named']['possition']) && isset($this->$model->actsAs['Tree'])) {
$this->Session->setFlash(__('A problem occured moving the MPTT record.', true));
$this->redirect($this->referer());
}

if (isset($this->params['named']['possition'])) {
$this->_orderedMove();
}

if (isset($this->params['named']['direction'])) {
$this->_treeMove();
}

$this->redirect($this->referer());
}

/**
* Moving MPTT records
*
* This is used for moving mptt records and is called by admin_reorder.
*/
function _treeMove(){
$model = $this->modelNames[0];
$check = $this->{$model}->find(
'first',
array(
'fields' => array($model.'.id'),
'conditions' => array($model.'.id' => $this->$model->id),
'recursive' => -1
)
);

if (empty($check)) {
$this->Session->setFlash(__('Nothing found to move', true));
return false;
}

switch($this->params['named']['direction']){
case 'up':
$this->Session->setFlash(__('The record was moved up', true));
if (!$this->{$model}->moveUp($this->{$model}->id, abs(1))) {
$this->Session->setFlash(__('Unable to move the record up', true));
}
break;

case 'down':
$this->Session->setFlash(__('The record was moved down', true));
if (!$this->{$model}->moveDown($this->{$model}->id, abs(1))) {
$this->Session->setFlash(__('Unable to move the record down', true));
}
break;

default:
$this->Session->setFlash(__('Error occured reordering the records', true));
break;
} // switch
return true;
}

/**
* Moving records that actas sequenced
*
* This is used for moving sequenced records and is called by admin_reorder.
*/
function _orderedMove(){
$model = $this->modelNames[0];

if (isset($this->$model->actsAs['Libs.Sequence']['order_field']) && !empty($this->$model->actsAs['Libs.Sequence']['order_field'])) {
$this->data[$model][$this->$model->actsAs['Libs.Sequence']['order_field']] = $this->params['named']['possition'];
}

else{
$this->data[$model]['ordering'] = $this->params['named']['possition'];
}
Expand All @@ -185,7 +254,7 @@ function admin_reorder($id = null) {
$this->Session->setFlash(__('The record could not be moved', true));
}

$this->redirect($this->referer());
return true;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions app_helper.php
Expand Up @@ -214,6 +214,56 @@ function ordering($id = null, $currentPossition = null, $model = null) {
return $out;
}

function treeOrdering($data = null){
if (!$data) {
$this->errors[] = 'There is no data to build reorder links';
return false;
}


$out = $this->Html->link(
$this->Html->image(
$this->Image->getRelativePath('actions', 'arrow-up'),
array(
'alt' => __('Up', true),
'title' => __('Move up', true),
'width' => '16px',
'class' => 'arrow-up'
)
),
array(
'action' => 'reorder',
'direction' => 'up',
$data['id']
),
array(
'escape' => false,
)
);

$out .= $this->Html->link(
$this->Html->image(
$this->Image->getRelativePath('actions', 'arrow-down'),
array(
'alt' => __('Down', true),
'title' => __('Move down', true),
'width' => '16px',
'class' => 'arrow-down'
)
),
array(
'action' => 'reorder',
'direction' => 'down',
$data['id']
),
array(
'escape' => false,
)
);

return $out;
}

function paginationCounter($pagintion) {
if (empty($pagintion)) {
$this->errors[] = 'You did not pass the pagination object.';
Expand Down
4 changes: 1 addition & 3 deletions infinitas/cms/views/categories/admin_index.ctp
Expand Up @@ -100,9 +100,7 @@
<?php echo $this->Time->niceShort( $category['Category']['modified'] ); ?>
</td>
<td>
<?php
// @todo -c Implement .add up and down for mptt
?>
<?php echo $this->Cms->treeOrdering( $category['Category'] ); ?>&nbsp;
</td>
<td>
<?php
Expand Down

0 comments on commit 7270971

Please sign in to comment.