Topic: [Snippet] UltimateParent  (Read 20525 times)

Pages: [1] 2   Go Down

#1: 3-Nov-2005, 11:52 AM

Coding Team

sottwell
Posts: 10,505

WWW
This snippet returns the ultimate parent of the current document.  It is useful for setting the starting root folder for menu snippets to create submenus.

Code:
// UltimateParent - MODx snippet
// recursively searches the document tree
// and determines the "ultimate" parent
// (the one whose parent is 0)
// of the given document
//
// created 02 Nov 2005 by sottwell@sottwell.com
// released to the public domain
//
// Usage:
// [!Menusnippet?folderid =`[[UltimateParent]]`!]
// [!...!] form must be used so the inner snippet
// is parsed properly.
//
$id = $modx->documentIdentifier;
$pid = $modx->getParent($id,1,"id");
if($pid['id'] == 0) { return $id; }
while ($pid['id'] != 0) {
    $id = $pid['id'];
    $pid = $modx->getParent($id,1,"id");
}
return $id;
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#2: 4-Nov-2005, 09:59 AM

Administrator

zi
MODx Special Forces /
Posts: 3,555

May Peace Be On You

WWW
Thanks for sharing nice snippet Susan !

Best regards,

zi

#3: 8-Nov-2005, 07:24 AM

Coding Team

jaredc
Posts: 300

I have long held the belief that when the page object is built, it should have some of this information built in. For example:

$modx->genes

could be an array of information about the pages leading back to the root. Each element in the array would be an array containing basic information such as: page id, show in menu, published, pagetitle (perhaps menutitle), etc. It may seem like collecting all this for every page from the current page back to the root would be overkill, but really, I think it would save processing. Most of the menu snippets I've written or seen perform this task independently. Therefore, if you have several types of navigation (say tabs, section menu, breadcrumbs), then a lot of the same information is being retrieved multiple times.

Susans snippet here about finding the top level section for styling is yet another use for this kind of information. The "ultimate parent" would simply be the last one in the array (or first depending on how the genes array was built). So instead of performing multiple db queries, a page object could be quickly referenced. Then a menu could be built, a stylesheet could be associated, a language could be selected... whatever.

I don't know. It wouldn't work for every situation of course, but it seems like it might have enough merit to consider.
Standard Disclaimer
I could be totally wrong.

#4: 8-Nov-2005, 07:59 AM

Foundation

rthrash
Posts: 11,348

WWW
That's a very cool idea Jared! It's very little overhead to store a bit of extra information related to each page, but would save a lot of processing time potentially. Very very cool... I like it!

MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#5: 16-Nov-2005, 03:35 AM

cryptomancer
Posts: 27

I have long held the belief that when the page object is built, it should have some of this information built in. For example:

$modx->genes

could be an array of information about the pages leading back to the root. Each element in the array would be an array containing basic information such as: page id, show in menu, published, pagetitle (perhaps menutitle), etc. It may seem like collecting all this for every page from the current page back to the root would be overkill, but really, I think it would save processing.

I would just make extra 2 fields in pages table 'crumb' and 'url_path', and write "breadcrumbs" and "friendly URL path" into them at time of page editing.

#6: 16-Nov-2005, 07:37 AM

Foundation

rthrash
Posts: 11,348

WWW
Yes indeed. Next to no perceptible overhead in DB size for a lot of improvement in performance for things that need access to a page's genealogy.
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#7: 16-Nov-2005, 11:21 AM

Foundation

OpenGeek
MODx Co-Founder
Posts: 6,938

damn accurate caricatures...

WWW
+1 on this from me, though I think the best option would be to allow this to be specified per page instance, or be automatically calculated if not specified (per page override).  This geneology information could also be used to improve the friendly alias path implementation, allowing such sites to be built with full portability (e.g. using relative paths).
Jason Coward
MODx Co-Founder
xPDO Founder
CTO @ Collabpad
work productively.
work intelligently.
work together.
Light is just a vibration of a note too. Everything is. You've got to keep that in mind.
  Frank Zappa

#8: 16-Nov-2005, 11:27 AM

Foundation

rthrash
Posts: 11,348

WWW
Jason, want to take a stab at implementing this?
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#9: 16-Nov-2005, 12:32 PM

Foundation

OpenGeek
MODx Co-Founder
Posts: 6,938

damn accurate caricatures...

WWW
Sure thing, I'll put it on my list...  Grin
Jason Coward
MODx Co-Founder
xPDO Founder
CTO @ Collabpad
work productively.
work intelligently.
work together.
Light is just a vibration of a note too. Everything is. You've got to keep that in mind.
  Frank Zappa

#10: 17-Nov-2005, 06:55 AM

Coding Team

jaredc
Posts: 300

...This geneology information could also be used to improve the friendly alias path implementation...
Yeah, I think that's what I'm getting at. This information, if available consistently by default, could be used for many things. Mostly navigation related, but not necessarily.
Standard Disclaimer
I could be totally wrong.

#11: 19-Nov-2005, 09:47 AM

Emeritus
Djamoer
Posts: 1,495

No one can limit a man other than the man himself.

WWW
I vote for yes too....

So will this be implemented in the next release or it will wait until the major release of TP 4?

I can imagine implementing a breadcrumb, menu, subdomain implementation, language internaionalization, and a lot more, just by having this implementation by default. Is it do able during OnInit event? Fetching the data from database, and store it as an array, make it global/visible, to be accessed externally by snippets/plugins?

I'm looking forward to it guys. Keep up the good work, and thanks to OpenGeek for taking it as the next project. All the best...

Sincerely,
Wendy Novianto

#12: 19-Nov-2005, 02:12 PM

Foundation

rthrash
Posts: 11,348

WWW
Travis IM'd me while I was away with some information regarding a possible way of to do this efficiently:

Quote
Hey.  I just saw the posts on the Ultimate Parent and $modx->genes
Just thought I'd mention that there may be a "nicer" way to do this that wouldn't cause any overhead when using it, only when saving a page. First, this article:  http://www.codeproject.com/database/modhierarchies.asp
The second is to simply store a lineage field with the records:
1-2-3-5, for instance
So sid 1 would have a lineage of 1
Sid 2 that is a child of 1 would have 1-2
Sid 3 would have 1-2-3
Sid would have 1-2-3-5
When adding a new page, you simply ask the parent page for its lineage and add yourself to the end
Pulling 1-2% would give you sid 2 (the parent) plus all children
Sorting by lineage gives you the entire tree in proper order
Etc
Facilitates pulling the entire tree, in tree form, with a single query
Not the most efficient method, but for something like you all are talking about, might be worth considering for its simplicity when inserting and when working with it...  Makes it very trivial to find any parent or any child
Just a thought; I'm sure you all will probably find a better method.

Let the wheels start turning. Smiley
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#13: 13-Mar-2006, 07:09 AM

Support Subscriber

edge
Posts: 206

WWW
Is there a way of including the parent folder itself as the first list item when using ultimate parent snippet. Ideally I need to have the parent folder for each sub-section listed

Many thanks

#14: 13-Mar-2006, 07:46 AM

Coding Team

sottwell
Posts: 10,505

WWW
Is there a way of including the parent folder itself as the first list item when using ultimate parent snippet. Ideally I need to have the parent folder for each sub-section listed

You could use this snippet's return value in another snippet to get the parent of the document whose id is returned by this one (use $modx->runSnippet in your second snippet).

Code:
$grandparent = $modx->getParent($modx->runSnippet('GetUltimateParent'));

I'm not sure what you want to do, though.  If you want a list of submenu folders, with only the current document's submenus showing, you can use a regular DropMenu and use the here class and css to show/hide the submenus.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#15: 13-Mar-2006, 07:55 AM

Support Subscriber

edge
Posts: 206

WWW
Hi Susan

The only problem with dropmenu is that it will not show the folder (the actual start id page) which in this case I need as its represents the home page for that section. I could have made the first page within that folder the sections home page, but I have another top level menu and that lists each sections folder so that would not have worked

Many thanks for your help

#16: 13-Mar-2006, 07:56 AM

Coding Team

sottwell
Posts: 10,505

WWW
Added an option to stop the search at a certain level.

Code:
// arguments:
// &id      - the id of the document whose parent you want to find
// &top     - the top of the search
// examples:
// [[GetUltimateParent?id=`45`&top=`6`]]
// will find the first parent of document #45 under document #6
// if id == 0 or top == id, will return id.
// you can use this as the startDoc for DropMenu to create specific submenus.

$top = isset($top)?$top:0;
$id = isset($id)?$id:$modx->documentIdentifier;
if($id==$top || $id==0) { return $id; }
$pid = $modx->getParent($id,1,'id');
if($pid == $top) { /* echo $id */ return $id; }
while ($pid != $top) {
    $id = $pid;
    $pid = $modx->getParent($id,1,'id');
    if($pid == $top) { /* echo $id */ return $id; }
}
return 0; // if all else fails
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#17: 13-Mar-2006, 08:19 AM

Support Subscriber

edge
Posts: 206

WWW
Hi Susan,

Maybe I am being dense today..

I am using ultimateParent so that I can use a single template in my site. The site has six sections (Folders) and a number of section pages in each folder. I have a sub-menu which lists only the pages for the section I am in (using ultimateparent snippet as my start ID in drop menu) which all works fine.

I then need to display the folder for each section I am in as this is that sections home page. I cannot hard code any start ID information. Modx needs to display 1. the folder menutitle based on the page I am viewing . 2. Separately Dropmenu will display a list of all pages in that section using UltimateParent (Works great)

Does that make sense ?
« Last Edit: 13-Mar-2006, 08:23 AM by edge »

#18: 13-Mar-2006, 08:30 AM

Coding Team

sottwell
Posts: 10,505

WWW
Ah, I get it!  You need to display the folder's page content as the subsection's homepage!  I was totally focused on the menu itself!  If a weblink won't work because you need the id to be a variable, make a document that uses a snippet or a TV to redirect to the proper folder/document (such as the parent of the ultimate parent as I suggested below). 
« Last Edit: 13-Mar-2006, 08:43 AM by sottwell »
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#19: 13-Mar-2006, 08:50 AM

Support Subscriber

edge
Posts: 206

WWW
Hi

Its OK I have got it solved with following code

Code:
<a href="[~[[UltimateParent]]~]">[!getdoc? &id=`[[UltimateParent]] &field=`menutitle`!]</a>

#20: 14-Mar-2006, 05:11 PM

Marketing & Design Team

davidm
MODx evangelist
Posts: 7,073

The best way to predict the future is to invent it

WWW
Waow that's a pretty nifty trick, thanks for sharing !
.: nodeo.net : Pour un web libre, moderne et ouvert ! :: david-molliere.net : Suivez en "live" mes expérimentations et billets sur les CMS et autres applications web :.

*** Forums modxcms.fr Participez à l'élaboration du site MODx francophone ! ***

! Nouveau !  En live, ne manquez pas les news de modxcms.fr sur Twitter   ! Nouveau !

MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accès pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.67 - PHP 5.2.8 | Debian 4.0 (Etch)

Réalisations sous MODx : | pargade-notaires.fr | soleil.info | gican.asso.fr | michelez-notaires.com | amadom.gerondicap.com | jocelyne-violet.net
Pages: [1] 2   Go Up
0 Members and 1 Guest are viewing this topic.