So far I'm tracking the "current" item in a paginated DataObjectSet manually, I'm wondering if there's a better way to do this. I.e. access the iterator for a DataObjectSet attached to a Page.
Example
simple gallery:
Page with has_many Images
page 1
- image 1
- image 2
- image 3
- image 4
page 2
- image 5
- image 6
- image 7
- image 8
page 3
- image 9
- image 10
- image 11
- image 12
Each page shows the "current" image, plus paginated thumbnails. Each thumbnail updates "current" to the selected thumbnail. I.e. page/showImage/7
I can paginate fine, showing thumbnails, using the standard pagination examples. To ensure the current page is not reset when I click the thumbnail on page 2 or 3 I need to pass the start (paginator) param, generated from my controller. I.e. page/showImage/7?start=5
controller:
function CurrentPageLink() {
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
return $_GET['start'];
}
template:
$Link(showImage)/$ID?start=$CurrentPageLink
To keep track of "current" I have a variable in my controller, which updates the ID when a new ID is passed in, then gets the object via DataObject::get(). Is there a better way to do this? Ideally I'd like iterator access "built-in". I.e. to get the "current" Image:
Page:
function CurrentImage() {
return $this->Images()->Current();
}
instead of:
Controller
function CurrentImage() {
if($this->currentImage) {
return $this->currentImage;
} else {
$this->currentImage = DataObject::get_one("Image", "GalleryPageID = $this->ID"); //get first image attached to page
return $this->currentImage;
}
}
Am I missing something here? The reason I ask, is that accessing iteration via the DataObjectSet opens up Next(), Prev(), etc. objects. If I try to access $this->Images()->Current() I simply get [User Error] Uncaught Exception: Object->__call(): the method 'current' does not exist on 'ComponentSet'. Even though ComponentSet is a subclass of DataObjectSet, which has a current() method, it's not available. Because it does not have an iterator attached? If someone could help explain it'd be great.
tia