Welcome to Paster, Anonymous Friend!
Added by anonymous on 2008-07-28 14:57:30 Download/View
  1. public static function shift_field(Resource_Model $resource, $direction = 'up')
  2.         {
  3.                 // Make sure we have a valid resource (and only one!)
  4.                 if(count($resource) === 1)
  5.                 {
  6.                         // What direction are we moving it?
  7.                         list($direction, $sort) = (strtolower($direction) === 'up') ? array('<','DESC') : array('>','ASC');
  8.                        
  9.                         // Get resource information
  10.                         $resource->current();
  11.                        
  12.                         // Get the field we are going to swap with
  13.                         $field_for_swap = Resource_Model::factory()
  14.                                                                         ->where('sort_order '.$direction.$field->sort_order)
  15.                                                                         ->where('resource_id', $field->resource_id)
  16.                                                                         ->orderby('sort_order', $sort)
  17.                                                                         ->limit(1)
  18.                                                                         ->get()
  19.                                                                         ->current();
  20.                        
  21.                         // If we found a field to swap with, then let's update them
  22.                         if (count($field_for_swap === 1))
  23.                         {
  24.                                 // Swap the values
  25.                                 list($resource->sort_order, $field_for_swap->sort_order) = array($field_for_swap->sort_order, $resource->sort_order);
  26.                                
  27.                                 // Save the changes!
  28.                                 $resource->save();
  29.                                 $field_for_swap->save();
  30.                         }
  31.                 }
  32.         }
  33.