/**
  * 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;
 }
 /**
  * Ensure we populate these fields before a save.
  */
 public function onBeforeWrite()
 {
     // Run other beforewrites first.
     parent::onBeforeWrite();
     if (!$this->isBrowser()) {
         return false;
     }
     // If this is the first save...
     if (!$this->ID) {
         // Ensure the session exists before querying it.
         if (!Session::request_contains_session_id()) {
             Session::start();
         }
         // Store the sesion and has information in the database.
         $this->SessionID = SecurityToken::getSecurityID();
         if (is_null($this->SessionID)) {
             return false;
         }
         $gen = new RandomGenerator();
         $uniqueurl = substr($gen->randomToken(), 0, 32);
         while (ShortList::get()->filter('URL', $uniqueurl)->count() > 0) {
             $uniqueurl = substr($gen->randomToken(), 0, 32);
         }
         $this->URL = $uniqueurl;
         $this->UserAgent = Controller::curr()->getRequest()->getHeader('User-Agent');
     }
 }
 /**
  * Get a paginated list of the shortlist items.
  *
  * @return mixed the paginated list of items, or false if the list cannot be found.
  * */
 public function paginatedItems()
 {
     if (!$this->owner->getRequest()->param('URL') || !ShortList::isBrowser()) {
         return false;
     }
     $items = false;
     $list = DataObject::get_one('ShortList', $filter = array('URL' => $this->owner->getRequest()->param('URL')));
     if ($list) {
         $items = $list->ShortListItems();
     }
     $this->owner->list = new PaginatedList($items, $this->owner->getRequest());
     $this->owner->list->setPageLength(Config::inst()->get('ShortList', 'PaginationCount'));
     $this->owner->list->setPaginationGetVar('page');
     if ($this->owner->currentPage) {
         $this->owner->list->setCurrentPage($this->owner->currentPage);
     }
     return $this->owner->list;
 }
 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);
 }