Hello everyone!

I use an ordenable behavior to sort pages in my site. I created a field
called 'index' in my pages table, and it's working fine, except that when I
use the action MoveDown, it keeps moving the index down even limitlessly. 

For example, if there are 5 pages, and I press moveDown in my "page 1" with
"index 1" 10 times, then my pages will be like:
(index - title)
1 - page 2
2 - page 3
3 - page 4
4 - page 5
10 - page 1

It should stop when it's 

5 - page 1

Here's the behavior code:

[code]
<?php
class OrdenableBehavior extends ModelBehavior {
        
        static $__settings = array();
  
        var $toWrite = array();

        function setup(&$model, $settings = array()) {
                $defaults = array(
                        'field' => 'index',
                );
    
                if (array_key_exists('field',$settings))
                {
                        self::$__settings['field'] = $settings['field'];
                }
                else
                {
                        self::$__settings['field'] = $defaults['field'];
                }
        }
        
        function beforeSave(&$model)
        {
                if (! isset($model->data[$model->name]['id']))
                {
                        $lastPage = $this->_getLastContent($model);
                        
                        if($lastPage)
                        {
                                
$model->data[$model->name][self::$__settings['field']] =
$lastPage[$model->name][self::$__settings['field']] + 1;
                        }
                        else
                        {
                                
$model->data[$model->name][self::$__settings['field']] = 1;
                        }
                }
                
                return true;
        }
        
        function moveUp(&$model,$content)
        {
                if ( $content[$model->name][self::$__settings['field']] > 1)
                {

                        $searchKey = 
$content[$model->name][self::$__settings['field']] - 1;
                        echo $searchKey;
                        $upContent = $model->find('first',array(
                                'conditions'=> array(
                                        self::$__settings['field'] => $searchKey
                                )
                        ));
                        
                        if ($upContent)
                        {
                                
$upContent[$model->name][self::$__settings['field']] =
$content[$model->name][self::$__settings['field']];
                                $model->save($upContent);
                                $model->create(); 
                        }
                        
                        $content[$model->name][self::$__settings['field']] = 
$searchKey;
                        $model->save($content,array('callbacks'=>false));
                }
        }
        
        function moveDown(&$model,$content)
        {
                
                if ( $content[$model->name][self::$__settings['field']] !=
$this->_getLastContent($model))
                {
                        $searchKey = 
$content[$model->name][self::$__settings['field']] + 1;
                        
                        $downContent = $model->find('first',array(
                                'conditions'=> array(
                                        self::$__settings['field'] => $searchKey
                                )
                        ));
                        
                        if ($downContent )
                        {
                                $downContent 
[$model->name][self::$__settings['field']] =
$content[$model->name][self::$__settings['field']];
                                $model->save($downContent 
,array('callbacks'=>false));
                                $model->create(); 
                        }
                        
                        $content[$model->name][self::$__settings['field']] = 
$searchKey;
                        $model->save($content,array('callbacks'=>false));
                }               
        }
        
        function _getLastContent(&$model)
        {
                return $model->find('first',array(
                        
'order'=>array($model->name.'.'.self::$__settings['field'] => 'DESC')
                ));
        }
}
?>
[/code]

-- 
View this message in context: 
http://www.nabble.com/bug-in-ordenable-behavior%3A-moveDown-exceeds-number-of-order_display-tp23905710p23905710.html
Sent from the CakePHP mailing list archive at Nabble.com.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to