/**
  * When landing on the homepage, if there is a shortlist for the current
  * user, redirect to the correct URL. Otherwise, 404.
  * */
 public function index($request)
 {
     if ($shortlist = $this->getSessionShortList()) {
         return $this->redirect(Config::inst()->get('ShortList', 'URLSegment') . $shortlist->URL);
     } else {
         $shortlist = $this->getSessionShortList();
         if (!$shortlist || !$shortlist->exists()) {
             $shortlist = new ShortList();
             $shortlist->write();
         }
     }
     // render with empty template.
     return $this->renderWith(array('Page', 'ShortList_empty'));
 }
 /**
  * Add item to shortlist
  *
  * @param shortlist shortlist to perform action on.
  * @param ID id of the object to add.
  * @param type classname of the item to remove.
  * @param session session id.
  *
  * */
 public function performAction(ShortList $shortlist = null, $ID = false, $type = null, $session = false)
 {
     if (!$ID || is_null($type) || !$session) {
         return false;
     }
     if (!$shortlist || !$shortlist->exists()) {
         $shortlist = new ShortList();
         $shortlist->SessionID = $session;
         $shortlist->write();
     }
     // check whether the itme is already in the list
     // before attempting to add it.
     $existing = $shortlist->ShortListItems()->filter(array('ItemID' => $ID, 'ItemType' => $type));
     // Item already exists, we're not going to add it again.
     if ($existing->count() == 1) {
         return $shortlist;
     }
     $shortlistItem = new ShortListItem();
     $shortlistItem->ShortListID = $shortlist->ID;
     $shortlistItem->ItemID = $ID;
     $shortlistItem->ItemType = $type;
     $shortlist->ShortListItems()->add($shortlistItem);
     $shortlist->write();
     return $shortlist;
 }
 public function testRemoveItemFromShortList()
 {
     $testpage = $this->objFromFixture('Page', 'page1');
     // create the shortlist
     $shortlist = new ShortList();
     $shortlist->write();
     $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
     // create the item.
     $item = new ShortListItem();
     $item->ItemType = $testpage->ClassName;
     $item->ItemID = $testpage->ID;
     $item->write();
     // add to the shortlist.
     $shortlist->ShortListItems()->add($item);
     $shortlist->write();
     // do we have an item added?
     $this->assertNotEquals($shortlist->ShortListItems()->Count(), 0);
     // what about the reverse side?
     $this->assertNotEquals($item->ShortList()->ID, 0);
     $shortlist->ShortListItems()->remove($item);
     // do we have an item removed?
     $this->assertEquals($shortlist->ShortListItems()->Count(), 0);
 }