Skip to content
This repository
tree: 15f4fa171c
Fetching contributors…

Cannot retrieve contributors at this time

file 370 lines (334 sloc) 10.928 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

/**
* Behavior to add sortable peer methods
*
* @author François Zaninotto
* @author heltem <heltem@o2php.com>
* @author rozwell
* @package propel.generator.behavior.sortable
*/
class SortableBehaviorPeerBuilderModifier
{
    protected $behavior, $table, $builder, $objectClassname, $peerClassname;

    public function __construct($behavior)
    {
        $this->behavior = $behavior;
        $this->table = $behavior->getTable();
    }

    protected function getParameter($key)
    {
        return $this->behavior->getParameter($key);
    }

    protected function getColumnAttribute($name)
    {
        return strtolower($this->behavior->getColumnForParameter($name)->getName());
    }

        protected function getColumnConstant($name)
    {
        return strtoupper($this->behavior->getColumnForParameter($name)->getName());
    }

    protected function getColumnPhpName($name)
    {
        return $this->behavior->getColumnForParameter($name)->getPhpName();
    }

    protected function setBuilder($builder)
    {
        $this->builder = $builder;
        $this->objectClassname = $builder->getStubObjectBuilder()->getClassname();
        $this->peerClassname = $builder->getStubPeerBuilder()->getClassname();
        $this->queryClassname = $builder->getStubQueryBuilder()->getClassname();
    }

    public function staticAttributes($builder)
    {
        $tableName = $this->table->getName();
        $script = "
/**
* rank column
*/
const RANK_COL = '" . $tableName . '.' . $this->getColumnConstant('rank_column') . "';
";

        if ($this->behavior->useScope()) {
            $script .= "
/**
* Scope column for the set
*/
const SCOPE_COL = '" . $tableName . '.' . $this->getColumnConstant('scope_column') . "';
";
        }

        return $script;
    }

    /**
* Static methods
*
* @return string
*/
    public function staticMethods($builder)
    {
        $this->setBuilder($builder);
        $script = '';

        $this->addGetMaxRank($script);
        $this->addRetrieveByRank($script);
        $this->addReorder($script);
        $this->addDoSelectOrderByRank($script);
        if ($this->behavior->useScope()) {
            $this->addRetrieveList($script);
            $this->addCountList($script);
            $this->addDeleteList($script);
        }
        $this->addShiftRank($script);

        return $script;
    }

    protected function addGetMaxRank(&$script)
    {
        $useScope = $this->behavior->useScope();
        $script .= "
/**
* Get the highest rank
* ";
        if ($useScope) {
            $script .= "
* @param int \$scope Scope to determine which suite to consider";
        }
        $script .= "
* @param PropelPDO optional connection
*
* @return integer highest position
*/
public static function getMaxRank(" . ($useScope ? "\$scope = null, " : "") . "PropelPDO \$con = null)
{
if (\$con === null) {
\$con = Propel::getConnection({$this->peerClassname}::DATABASE_NAME);
}
// shift the objects with a position lower than the one of object
\$c = new Criteria();
\$c->addSelectColumn('MAX(' . {$this->peerClassname}::RANK_COL . ')');";
        if ($useScope) {
        $script .= "
\$c->add({$this->peerClassname}::SCOPE_COL, \$scope, Criteria::EQUAL);";
        }
        $script .= "
\$stmt = {$this->peerClassname}::doSelectStmt(\$c, \$con);

return \$stmt->fetchColumn();
}
";
    }

    protected function addRetrieveByRank(&$script)
    {
        $peerClassname = $this->peerClassname;
        $useScope = $this->behavior->useScope();
        $script .= "
/**
* Get an item from the list based on its rank
*
* @param integer \$rank rank";
        if ($useScope) {
            $script .= "
* @param int \$scope Scope to determine which suite to consider";
        }
        $script .= "
* @param PropelPDO \$con optional connection
*
* @return {$this->objectClassname}
*/
public static function retrieveByRank(\$rank, " . ($useScope ? "\$scope = null, " : "") . "PropelPDO \$con = null)
{
if (\$con === null) {
\$con = Propel::getConnection($peerClassname::DATABASE_NAME);
}

\$c = new Criteria;
\$c->add($peerClassname::RANK_COL, \$rank);";
        if ($useScope) {
            $script .= "
\$c->add($peerClassname::SCOPE_COL, \$scope, Criteria::EQUAL);";
        }
        $script .= "

return $peerClassname::doSelectOne(\$c, \$con);
}
";
    }

    protected function addReorder(&$script)
    {
        $peerClassname = $this->peerClassname;
        $columnGetter = 'get' . $this->behavior->getColumnForParameter('rank_column')->getPhpName();
        $columnSetter = 'set' . $this->behavior->getColumnForParameter('rank_column')->getPhpName();
        $script .= "
/**
* Reorder a set of sortable objects based on a list of id/position
* Beware that there is no check made on the positions passed
* So incoherent positions will result in an incoherent list
*
* @param array \$order id => rank pairs
* @param PropelPDO \$con optional connection
*
* @return boolean true if the reordering took place, false if a database problem prevented it
*/
public static function reorder(array \$order, PropelPDO \$con = null)
{
if (\$con === null) {
\$con = Propel::getConnection($peerClassname::DATABASE_NAME);
}

\$con->beginTransaction();
try {
\$ids = array_keys(\$order);
\$objects = $peerClassname::retrieveByPKs(\$ids);
foreach (\$objects as \$object) {
\$pk = \$object->getPrimaryKey();
if (\$object->$columnGetter() != \$order[\$pk]) {
\$object->$columnSetter(\$order[\$pk]);
\$object->save(\$con);
}
}
\$con->commit();

return true;
} catch (PropelException \$e) {
\$con->rollback();
throw \$e;
}
}
";
    }

    protected function addDoSelectOrderByRank(&$script)
    {
        $peerClassname = $this->peerClassname;
        $script .= "
/**
* Return an array of sortable objects ordered by position
*
* @param Criteria \$criteria optional criteria object
* @param string \$order sorting order, to be chosen between Criteria::ASC (default) and Criteria::DESC
* @param PropelPDO \$con optional connection
*
* @return array list of sortable objects
*/
public static function doSelectOrderByRank(Criteria \$criteria = null, \$order = Criteria::ASC, PropelPDO \$con = null)
{
if (\$con === null) {
\$con = Propel::getConnection($peerClassname::DATABASE_NAME);
}

if (\$criteria === null) {
\$criteria = new Criteria();
} elseif (\$criteria instanceof Criteria) {
\$criteria = clone \$criteria;
}

\$criteria->clearOrderByColumns();

if (\$order == Criteria::ASC) {
\$criteria->addAscendingOrderByColumn($peerClassname::RANK_COL);
} else {
\$criteria->addDescendingOrderByColumn($peerClassname::RANK_COL);
}

return $peerClassname::doSelect(\$criteria, \$con);
}
";
    }

    protected function addRetrieveList(&$script)
    {
        $peerClassname = $this->peerClassname;
        $script .= "
/**
* Return an array of sortable objects in the given scope ordered by position
*
* @param int \$scope the scope of the list
* @param string \$order sorting order, to be chosen between Criteria::ASC (default) and Criteria::DESC
* @param PropelPDO \$con optional connection
*
* @return array list of sortable objects
*/
public static function retrieveList(\$scope, \$order = Criteria::ASC, PropelPDO \$con = null)
{
\$c = new Criteria();
\$c->add($peerClassname::SCOPE_COL, \$scope);

return $peerClassname::doSelectOrderByRank(\$c, \$order, \$con);
}
";
    }

    protected function addCountList(&$script)
    {
        $peerClassname = $this->peerClassname;
        $script .= "
/**
* Return the number of sortable objects in the given scope
*
* @param int \$scope the scope of the list
* @param PropelPDO \$con optional connection
*
* @return array list of sortable objects
*/
public static function countList(\$scope, PropelPDO \$con = null)
{
\$c = new Criteria();
\$c->add($peerClassname::SCOPE_COL, \$scope);

return $peerClassname::doCount(\$c, \$con);
}
";
    }

    protected function addDeleteList(&$script)
    {
        $peerClassname = $this->peerClassname;
        $script .= "
/**
* Deletes the sortable objects in the given scope
*
* @param int \$scope the scope of the list
* @param PropelPDO \$con optional connection
*
* @return int number of deleted objects
*/
public static function deleteList(\$scope, PropelPDO \$con = null)
{
\$c = new Criteria();
\$c->add($peerClassname::SCOPE_COL, \$scope);

return $peerClassname::doDelete(\$c, \$con);
}
";
    }
    protected function addShiftRank(&$script)
    {
        $useScope = $this->behavior->useScope();
        $peerClassname = $this->peerClassname;
        $script .= "
/**
* Adds \$delta to all Rank values that are >= \$first and <= \$last.
* '\$delta' can also be negative.
*
* @param int \$delta Value to be shifted by, can be negative
* @param int \$first First node to be shifted
* @param int \$last Last node to be shifted";
        if ($useScope) {
            $script .= "
* @param int \$scope Scope to use for the shift";
        }
        $script .= "
* @param PropelPDO \$con Connection to use.
*/
public static function shiftRank(\$delta, \$first = null, \$last = null, " . ($useScope ? "\$scope = null, " : "") . "PropelPDO \$con = null)
{
if (\$con === null) {
\$con = Propel::getConnection($peerClassname::DATABASE_NAME, Propel::CONNECTION_WRITE);
}

\$whereCriteria = {$this->queryClassname}::create();
if (null !== \$first) {
\$whereCriteria->add($peerClassname::RANK_COL, \$first, Criteria::GREATER_EQUAL);
}
if (null !== \$last) {
\$whereCriteria->addAnd($peerClassname::RANK_COL, \$last, Criteria::LESS_EQUAL);
}";
        if ($useScope) {
            $script .= "
\$whereCriteria->add($peerClassname::SCOPE_COL, \$scope, Criteria::EQUAL);";
        }
        $script .= "

\$valuesCriteria = new Criteria($peerClassname::DATABASE_NAME);
\$valuesCriteria->add($peerClassname::RANK_COL, array('raw' => $peerClassname::RANK_COL . ' + ?', 'value' => \$delta), Criteria::CUSTOM_EQUAL);

{$this->builder->getPeerBuilder()->getBasePeerClassname()}::doUpdate(\$whereCriteria, \$valuesCriteria, \$con);
$peerClassname::clearInstancePool();
}
";
    }
}
Something went wrong with that request. Please try again.