DaoMoveHelper.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Dmitry E. Demidov                               *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: DaoMoveHelper.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class DaoMoveHelper extends StaticFactory
00017     {
00018         private static $nullValue   = 0;
00019         private static $property    = 'position';
00020         
00021         /* void */ public static function setNullValue($nullValue)
00022         {
00023             self::$nullValue = $nullValue;
00024         }
00025         
00026         /* void */ public static function setProperty($property)
00027         {
00028             self::$property = $property;
00029         }
00030         
00031         /* void */ public static function up(
00032             DAOConnected $object,
00033             LogicalObject $exp = null
00034         )
00035         {
00036             $getMethod = 'get'.ucfirst(self::$property);
00037             
00038             Assert::isTrue(
00039                 method_exists($object, $getMethod)
00040             );
00041             
00042             $oldPosition = $object->$getMethod();
00043             
00044             $dao = $object->dao();
00045             
00046             $query =
00047                 ObjectQuery::create()->
00048                 sort(self::$property)->
00049                 desc()->
00050                 setLimit(1);
00051             
00052             if ($exp)
00053                 $query->addLogic($exp);
00054             
00055             $query->addLogic(
00056                 Expression::lt(
00057                     self::$property,
00058                     $oldPosition
00059                 )
00060             );
00061             
00062             try {
00063                 $upperObject = $dao->get($query);
00064                 
00065                 DaoUtils::setNullValue(self::$nullValue);
00066                 DaoUtils::swap($upperObject, $object, self::$property);
00067                 
00068             } catch (ObjectNotFoundException $e) {
00069                 // no need to move up top object
00070             }
00071         }
00072         
00073         /* void */ public static function down(
00074             DAOConnected $object,
00075             LogicalObject $exp = null
00076         )
00077         {
00078             $getMethod = 'get'.ucfirst(self::$property);
00079             
00080             Assert::isTrue(
00081                 method_exists($object, $getMethod)
00082             );
00083             
00084             $oldPosition = $object->$getMethod();
00085             
00086             $dao = $object->dao();
00087             
00088             $query =
00089                 ObjectQuery::create()->
00090                 addLogic(
00091                     Expression::gt(
00092                         self::$property,
00093                         $oldPosition
00094                     )
00095                 )->
00096                 sort(self::$property)->
00097                 asc()->
00098                 setLimit(1);
00099             
00100             if ($exp)
00101                 $query->addLogic($exp);
00102             
00103             try {
00104                 $lowerObject = $dao->get($query);
00105                 
00106                 DaoUtils::setNullValue(self::$nullValue);
00107                 DaoUtils::swap($lowerObject, $object, self::$property);
00108                 
00109             } catch (ObjectNotFoundException $e) {
00110                 // no need to move down bottom object
00111             }
00112         }
00113     }
00114 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4