Jump to content
MuchDev

How can this be optimized? Redirecting item page.

Recommended Posts

Hey all, so I'm not really sure how I should frame this question, but mostly I was hoping that by posting a script I use on a live site I may be able to get some ideas on how I should better optimize a pretty clunky operation. 

This template here is for items that sit on pages. The site is laid out where the items are children of their parent page, and the parent page is the view for all the children pages. When you click on an item, the item is displayed at a larger resolution in a lightbox. Because this is an art site I wanted to have the ability for users to have the feeling that they were browsing through an actual gallery, so the focus never leaves the page that they were on. The purpose of this template is one part so that I could build links directly to items from my site search.

So the problem with this page is a couple fold. The first problem is that this page is heavy and can take a second or two just to find the proper page to redirect to and insert a #anchor to open the proper lightbox.  The second is that after learning a ton over the last year or two since I built it I don't think that it helps anything from an seo standpoint.

Well I think I am going to rebuild this, and if you think you have any pearls of wisdom about art websites and their content I'd love to hear it. Also if you spot anything in this code that looks stupid I'd like to hear that too :)

sample page structure to illustrate what this whacky thing does
This script gathers all the children and figures out what page the item it is on by getting a max count from the page or the site default. 

(parent page)
|
|__item
|__item
|__item
|__section
      |__item
      |__item 
		
<?php
//artwork template only used to redirect back to parent template.

//find what page the item is on when using pagination
$maxItems = $pages->get(4945)->maxItems;

$pagebucket = new PageArray();

//get parents and flip em to find base page
$parents = $page->parents->reverse();

//now iterate all pages in reverse to find the base page
foreach($parents as $parent){
	if($parent->parent->template == 'base' || $parent->parent->template == 'homebase' || $parent->parent->template == 'pastExhibitions'){
		$basepage = $parent;
		break;
	}
}

//check to see if section has a max item amount set
if($basepage->maxItems>0) $maxItems = $basepage->maxItems;

//now add all children to the pagebucket

foreach($basepage->children as $child){
	
	$pagebucket->append($child);
	if($child->numChildren){
		foreach($child->children  as $grandChild){
			$pagebucket->append($grandChild);
			if($grandChild->numChildren){
				foreach($grandChild->children as $greatGrandChild){
					$pagebucket->append($greatGrandChild);
				}				
			}
		}
	} 
}

//debug
//foreach($pagebucket as $thispage) echo $thispage->title."<br>";

$itemPosition = $pagebucket->getItemKey($page) + 1;
$redirectPath = $basepage->url;

echo $itemPosition;

////now figure out what page its on
$pageNumber = ceil($itemPosition/$maxItems);

if($pageNumber>1){
$redirectPath .= "page{$pageNumber}\#artwork_{$page->id}";
}else{
$redirectPath .= "#artwork_{$page->id}";
}
//$redirectPath .= $pagebucket->getItemKey($page);
//NOW DO FUNKY REDIRECT ---PROFFIT!
$session->redirect($redirectPath);
?> 

Share this post


Link to post
Share on other sites

Before any tips I would like to understand why do you have these "section"-parents, when it seems that the user actually browses a merged list of all the artwork under the "base"-parent? Either way, you definitely have too much code and database queries just to resolve a position. I'm sure I will be able to help you if you could tell me a little bit more about the ideas behind the structure you have chosen.

  • Like 1

Share this post


Link to post
Share on other sites

Before any tips I would like to understand why do you have these "section"-parents, when it seems that the user actually browses a merged list of all the artwork under the "base"-parent? Either way, you definitely have too much code and database queries just to resolve a position. I'm sure I will be able to help you if you could tell me a little bit more about the ideas behind the structure you have chosen.

I wouldn't mind explaining at all :) I should have taken some time and gotten a bit more information to post at first. 

Items on this site are all artworks. The way that I have set up will allow the editors to build sections a couple different ways. The first way a section is built is by nesting.  When using the Artist or Section template you can create items underneath it that are either by the artist that the items are under or they are part of the section that they are under. You can then create another section underneath these which allow for a structure as shown below. 

You can also organize items for the artist template similarly using a section under an artist. 

So my reasons for doing this is mostly due to the fact that when I originally built this site I was brand new to processwire and I couldn't figure out a seamless way of creating a system that allowed people visiting the site the ability to browse through the items for a section. My solution was to put all items on the page and then use a lightbox to allow users to browse the items on the page with the lightbox. This is a system which I would like to do away with, but I have yet to figure out a good way to implement browsing through all of the items that are in the section in the order that they appear, and also provide visual reference for where the user is at while browsing. 

post-2490-0-00286000-1444052110_thumb.jp

post-2490-0-08588300-1444053339_thumb.jp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...