/** * * @param Zupal_Domain_Abstract $pItem */ public function move($pItem, $pMode, $pRank, $pData = NULL) { $id = $pItem->identity(); if (!$pItem->isSaved()) { throw new Exception(__METHOD__ . ': attempt to move unsaved ' . get_class($pItem)); } if (is_null($pData)) { $pData = $this->findAll($pRank); } $found = FALSE; $out = array(); switch ($pMode) { case self::MOVE_TOP: $out[] = $pItem; foreach ($pData as $item) { if ($pItem->identity() != $item->identity()) { $out[] = $item; } } break; case self::MOVE_UP: foreach ($pData as $item) { if ($found) { $out[] = $item; } elseif ($pItem->identity() == $item->identity()) { if (count($out)) { $prev = array_pop($out); $out[] = $item; $out[] = $prev; } else { $out[] = $item; } $found = TRUE; } else { $out[] = $item; } } break; case self::MOVE_DOWN: $just_saved = FALSE; foreach ($pData as $item) { if ($found) { $out[] = $item; if ($just_saved) { $out[] = $pItem; $just_saved = FALSE; } } elseif ($pItem->identity() == $item->identity()) { $found = TRUE; $just_saved = TRUE; } } break; case self::MOVE_BOTTOM: foreach ($pData as $item) { if ($pItem->identity() != $item->identity()) { $out[] = $item; } } $out[] = $pItem; break; default: throw new Exception(__METHOD__ . ': attempt to sort with unknown mode ' . $pMode); } foreach ($out as $k => $item) { $item->{$pRank} = $k; $item->save(); } }