MadHatter Posted January 21, 2015 Share Posted January 21, 2015 Hi, I've thrown together a quick class that extends PageArray to allow me to append unique pages to a PageArray (so if the appended PageArray contains pages which already in the PageArray then they are ignored). I've put the class inside _init.php but I just want to check what the best place to set this class would be as I'm not sure this is the right place to save it. Thanks, MadHatter Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted January 21, 2015 Share Posted January 21, 2015 (edited) Just checked the cheetsheet to see if you can do the job with in-built tools. Why not use unique method after appending a page to a PageArray? And on your question. If you are extending PageArray class, I think you better do it via a module. If you're just adding a function or a class you can put it in _functions.inc file (or any name you like) and include it either in _init.php (I assume you prepemding each template with this file like in delayed output aproach described here), or include it on per template basis in template file. Edited January 21, 2015 by Ivan Gretsky Link to comment Share on other sites More sharing options...
MadHatter Posted January 21, 2015 Author Share Posted January 21, 2015 Yes, that would make more sense. It would still be nice to know where to put extended classes though. EDIT: Just tried the unique() function, it works but doesn't let me sort the results in the way I'd like. I need to do an initial search to get a PageArray and then add the results of the second PageArray to the first, ignoring duplicates. unique() seemed to be returning the results in the same order each time, which is not desirable. I've extended the PageArray like so: class UniquePageArray extends PageArray { public function appendUnique($pageArray){ foreach($pageArray as $page){ if(!$this->has($page)){ $this->append($page); } } } } This has the advantage of having the appended Pages appear at the end of the PageArray. Link to comment Share on other sites More sharing options...
Soma Posted January 21, 2015 Share Posted January 21, 2015 Well, PageArray is already unique. Considering this example, you'll end up with 1 entry in PageArray: $pa = new PageArray(); $pa->add($pages->get("/about/")); $pa->add($pages->get("/about/")); echo $pa; 1 Link to comment Share on other sites More sharing options...
kongondo Posted January 21, 2015 Share Posted January 21, 2015 Maybe you need to read this....http://processwire.com/api/arrays/page/. There is a sort function there if that is at all useful. 1 Link to comment Share on other sites More sharing options...
MadHatter Posted January 21, 2015 Author Share Posted January 21, 2015 @Soma is it still unique if you append one PageArray to the other? I've just tried reverting back to the append() function but it's still showing my results in the wrong order (i.e randomizing all the results instead of showing me my exact results before the extended search results). If I use the appendUnique() function then I get them in the order I need them. @kongondo The sort function only helps for sorting by a field, what I'm trying to do is find results where the title of the page matches the query string, randomzing it, then searching the summary and content for the same string, randominzing that, then adding that to the end of the first result. There are more searches to take into account (due to some ridiculous client demands) so I need to append the results of each one to the end of the PageArray and check each is unique. My extended Class seems to work fine, my question was really about where to place the code for the Class. Link to comment Share on other sites More sharing options...
Soma Posted January 21, 2015 Share Posted January 21, 2015 Doesn't matter if you add a page or a page array to a page array it will always contain only unique pages. 1 Link to comment Share on other sites More sharing options...
Soma Posted January 21, 2015 Share Posted January 21, 2015 $pa = new PageArray(); $pa->add($pages->find("template=basic-page")); // 1001 at the first pos $pa->append($pages->get("/about/")); // 1001 at the last position now (unique) echo $pa; 1 Link to comment Share on other sites More sharing options...
MadHatter Posted January 21, 2015 Author Share Posted January 21, 2015 The issue with this is that the results from the first search will get replaced when the second PageArray is appended if there is an overlap in the results. For example, one of my searches returns back 1 result intially, then the second search returns back another 4. The single result form the first search is in the second search so it gets moved in the final PageArray due to being replaced. With my extended class there is no removing of pages so it maintains the correct ordering I require. Link to comment Share on other sites More sharing options...
renobird Posted January 21, 2015 Share Posted January 21, 2015 Could be slow if you have a lot of pages, but for a reasonable number this might work. foreach($pageArray2 as $p){ if ($pageArray1->has($p)) continue; $pageArray1->append($p); } *untested and written in the browser. 1 Link to comment Share on other sites More sharing options...
renobird Posted January 21, 2015 Share Posted January 21, 2015 Ah, I see your class already does the same thing. So yeah, either way to get what you need. Seems easy enough to do in the template. 1 Link to comment Share on other sites More sharing options...
Soma Posted January 21, 2015 Share Posted January 21, 2015 I see. Yes that's somewhat different to what WireArray does, but you're way is fine then. You can add your own method to PageArray using hook then instead of a new class. wire()->addHook("WireArray::addUnique", null, function($event){ $item = $event->arguments(0); if($item instanceof WireArray){ foreach($item as $p){ if(!$event->object->has($p)) $event->object->add($p); } } else { if(!$event->object->has($item)) $event->object->add($p); } }); $pa = new PageArray(); $pa->add($pages->find("template=basic-page")); $pa->addUnique($pages->get("/about/")->children); $content .= $pa; 3 Link to comment Share on other sites More sharing options...
MadHatter Posted January 21, 2015 Author Share Posted January 21, 2015 Cool, hooks to the rescue! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now