<?php

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
abstract class PluginSlideshowSlide extends BaseSlideshowSlide
{
  public function getSlideTitle()
  {
    return $this->getSlide()->getTitle();
  }
  
  
  // ====================
  // = Sortable Methods =
  // ====================
  /**
   * Sets a sortable object to the first position
   *
   * @return void
   * @author Travis Black
   */
  public function moveToFirst()
  {
    $this->moveToPosition(1);
  }


  /**
   * Sets a sortable object to the last position
   *
   * @return void
   * @author Travis Black
   */
  public function moveToLast()
  {
    $this->moveToPosition($this->getFinalPosition());
  }

  /**
   * Demotes a sortable object to a lower position
   *
   * @return void
   * @author Travis Black
   */
  public function demote()
  {  
    $position = $this->position;
    
    if ($this->position < $this->getFinalPosition())
    {
      $this->moveToPosition($position + 1);
    }
  }


  /**
   * Promotes a sortable object to a lower position
   *
   * @return void
   * @author Travis Black
   */
  public function promote()
  {    
    $position = $this->position;
    
    if ($this->position > 1)
    {
      $this->moveToPosition($position - 1);
    }
  }
  /**
   * Get the final position of a model
   *
   * @return $position
   * @author Travis Black
   */
  public function getFinalPosition()
  {
    $q = Doctrine_Query::create()
                       ->select('position')
                       ->from('SlideshowSlide st')
                       ->orderBy('position desc')
                       ->addWhere('st.slideshow_id = ?', $this['slideshow_id']);
    
    $last = $q->fetchOne();

    return $last ? $last->position : 0;
  }

/**
   * Set the position value automatically when a new SlideshowSlide object is created
   *
   * @param Doctrine_Event $event
   * @return void
   * @author Travis Black
   */
  public function preInsert($event)
  {
    $this->position = $this->getFinalPosition()+1;
  }


  /**
   * When a SlideshowSlide object is deleted, promote all SlideshowSlides positioned lower than itself
   *
   * @param string $Doctrine_Event 
   * @return void
   * @author Travis Black
   */  
  public function postDelete($event)
  {
    $q = Doctrine_Query::create()
             ->update('SlideshowSlide')
             ->set('position', 'position - ?', '1')
             ->where('position > ' . $this->position)
             ->addWhere('slideshow_id = ?', $this['slideshow_id']);

    $q->execute();
  }  

  /**
   * Moves a sortable object to a designate position
   *
   * @param string $newPosition
   * @return void
   * @author Travis Black
   */
  public function moveToPosition($newPosition)
  {
    $position = $this->position;

    // Position is required to be unique. Blanks it out before it moves others up/down.
    $this->setPosition(null);
    $this->save();

    if ($position > $newPosition)
    {
      $q = Doctrine_Query::create()
                         ->update('SlideshowSlide')
                         ->set('position', 'position + 1')
                         ->where('position < ?', $position)
                         ->andWhere('position >= ?', $newPosition)
                         ->andWhere('slideshow_id = ?', $this['slideshow_id'])
                         ->orderBy('position DESC');
                
    }
    elseif ($position < $newPosition)
    {
      $q = Doctrine_Query::create()
                         ->update('SlideshowSlide')
                         ->set('position', 'position - 1')
                         ->where('position > ?', $position)
                         ->andWhere('slideshow_id = ?', $this['slideshow_id'])
                         ->andWhere('position <= ?', $newPosition);
    }
    $q->execute();
    $this->setPosition($newPosition);
    $this->save();
  }
}