You are here

final public function PartyDefaultDataSet::shiftEntity in Party 8.2

Same name and namespace in other branches
  1. 7 includes/party.data.inc \PartyDefaultDataSet::shiftEntity()

Shift a particular entity in the order

Parameters

int $delta: The delta of the item we're shifting

string $method: The method of shift we want:

  • 'up' Shifts the entity up by $target
  • 'down' Shifts the entity down by $target
  • 'insert' Moves the entity to that position, shifting everything else
  • 'swap' Swaps the entity with the entity in $target

int $target: If method is 'up' or 'down', shift the element by this amount. If method is 'swap', swap the entity with the entity at this delta.

Return value

$this

File

includes/party.data.inc, line 790
Provides the default class for managing party - Attached entity relationships.

Class

PartyDefaultDataSet
Class PartyDefaultDataSet

Code

public final function shiftEntity($delta, $method, $target = 1) {
  if (isset($this->entities[$delta])) {
    $entity = $this->entities[$delta];
    switch ($method) {
      case 'up':

        // Calculate our new position
        $newDelta = min(0, $delta - $target);
        unset($this->entities[$delta]);
        array_splice($this->entities, $newDelta, 0, array(
          $entity,
        ));
        break;
      case 'down':

        // Calculate our new position, including removal of the moved entity
        $newDelta = $delta + $target - 1;
        unset($this->entities[$delta]);
        array_splice($this->entities, $newDelta, 0, array(
          $entity,
        ));
        break;
      case 'insert':
        array_splice($this->entities, $target, 0, array(
          $entity,
        ));
        break;
      case 'swap':

        // Make sure we have the other entity if it exists
        if (isset($this->entities[$target])) {
          $otherEntity = $this->entities[$target];
        }

        // Set our new position for this entity
        $this->entities[$target] = $entity;

        // If we had another entity, put it back in place
        if (isset($otherEntity)) {
          $this->entities[$delta] = $otherEntity;
        }

        // Make sure our deltas are numeric and sequencial
        $this->entities = array_values($this->entities);
        break;
    }
  }
  return $this;
}