• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Using the Global variables from CMS
PostPosted: Wed Mar 22, 2006 4:18 pm 
Offline
Forum Members
Forum Members

Joined: Thu May 12, 2005 8:50 pm
Posts: 50
Location: Port Townsend, WA
I have a question, but first a little help for anyone who needs this type of thing:

I've learned to use the variables defined by cmsms in my user tags rather than using my own select queries which is really efficient. For those who don't know what they are, here's a little user tag I use to quickly pull them out and identify them:

user tag name: show_variables
Use the tag in a template like this: {show_variables}

Here's the code for the tag:

Code:
global $gCms;

$pageinfo = &$gCms->variables['pageinfo'];
$page_title = $pageinfo->content_title;
$template = $pageinfo->template_id;
$menu = $pageinfo->content_menutext;

$alias = $gCms->variables['page'];
$content_id = $gCms->variables['content_id'];
$curhierarchy = $gCms->variables['position'];
$page_name = $gCms->variables['page_name'];

echo "menu text: " . $menu."<br>";
echo "template: " . $template."<br>";
echo "page title: " . $page_title."<br>";
echo "alias: " . $alias."<br>";
echo "hierarchy: " . $curhierarchy."<br>";
echo "id: " . $content_id."<br>";
echo "page name: " . $page_name."<br>";


Okay, now my question...are there other variables that i could be using but don't know about? Specifically, it would be nice to know the variable for parent_id and parent title or parent menu text. Now I am using my own select statement to get that when i need it, by first getting the parent id:

Code:
$query = "SELECT parent_id FROM cms_content WHERE content_id = '$content_id'";
$result = mysql_query($query,$dblink);
$content = mysql_fetch_assoc($result);
                  
$parent_id = $content['parent_id'];


Then getting the title, menu text, whatever else I need with another query:

Code:
$query = "SELECT menu_text FROM cms_content WHERE content_id = '$parent_id'";
$result = mysql_query($query,$dblink);
$content = mysql_fetch_assoc($result);
                  
$menu_text = $content['menu_text'];


Obviously this isn't very efficient. I know breadcrumbs already does this, but I have poured through that code and with my (rudimentary) php/mysql skills can't figure it out.

Any help is appreciated.


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Wed Mar 22, 2006 5:32 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 5954
Location: Fernie British Columbia, Canada
I wrote this little user defined tag to see all of the variables that smarty knows about..... it may be helpfull to alot of people:

Code:
// get a list of all of the smarty assigned variables
// user defined tag named "get_template_vars"
global $gCms;
$tpl_vars = $gCms->smarty->get_template_vars();
print_r( $tpl_vars );


Then place your {get_template_vars} tag somewhere in your content for debugging

_________________
Follow me on twitter
For quality help follow these instructions:
a) Think about the problem for an hour
b) research the problem for an hour
c) spend 1/2 an hour explaining it and providing as much information as you can muster
(too much information is okay, not enough information may get your question ignored).
--
if you can't bother explaining your problem well, why should we bother helping with it.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Wed Mar 22, 2006 5:40 pm 
Offline
Administrator
Administrator
User avatar

Joined: Fri Jun 11, 2004 6:58 pm
Posts: 3334
Location: Fairless Hills, Pa USA
Ok, time for an intro to hierarchy manager, which is new in 0.12.

Basically, hierarchy manager helps us keep the structure of the content in between page requests.  It's a cache of sorts, but it's also more of a structure to make sure we're not querying the database more than necessary.

We store all of the content in nodes in a tree.  The content has no properties loaded, so it doesn't use a lot of memory.  However, if a parameter does get loaded, it only has to be loaded once, even if it's used again on the same page.

Ok, so what does this mean?  Basically, you can utilize the hierarchy manager to look up stuff for you without writing sql queries.  Since it's all the same interface, you don't have to worry about slowing down your site too much.

So, in a plugin, you can do:
Code:
global $gCms;
$hm =& $gCms->GetHierarchyManager();


This will return the same global instance.  The & is very important.  Anyway...  then you can grab a node from it.

Code:
$node =& $hm->getNodeById($some_id);


or:

Code:
$node =& $hm->getNodeByAlias($some_page_alias);


or even:

Code:
$node =& $hm->getNodeByHierarchy('1.3.1');


Ok, so now you have a node object.  Keep in mind that this isn't content.  It's a thin wrapper around it.  But it does keep all kinds of details about the structure.  So, for instance, to get the parent's title...

Code:
if ($node != null)
{
    //Grab the parent node
    $parentnode =& $node->getParentNode();
   
    //Make sure something came back
    if ($parentnode != null)
    {
        //Get the actual content object
        $content =& $node->getContent();
        if ($content != null) //Just in case
        {
            //Display it's title
            echo $content->Title();
        }
    }
}


Or to find out if it has any children...
Code:
if ($node != null)
{
    if ($node->hasChildren())
    {
        //Do something...
    }
}


Anyway, you should get the idea.  This is a very powerful tool, but of course it's undocumented and well under the hood, so it's not easy to realize that it's there.  Enjoy!

_________________
http://about.me/tedkulp


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Thu Mar 23, 2006 5:35 pm 
Offline
Forum Members
Forum Members

Joined: Thu May 12, 2005 8:50 pm
Posts: 50
Location: Port Townsend, WA
Thanks so much Ted and Calguy, I'll still have some questions as I get into this more, but it looks like this is exactly the kind of thing I've been looking for!

However, this line is causing a problem:

Code:
echo $content->Title();


I've used Ted's code in several different ways in a user tag called {parent_title} (used in the template in the content area) and after experiencing blank pages commented each section of the code out until I arrived at that line causing a problem. If I leave everything as Ted wrote it, I get blank pages. IF I comment that line out the page works (but obviously then the tag does nothing).

Here's the full tag with everything in it:

Code:
global $gCms;
$content_id = $gCms->variables['content_id'];
$hm =& $gCms->GetHierarchyManager();

$node =& $hm->getNodeById($content_id);

if ($node != null)
{
    if ($node->hasChildren())
    {
        echo "This page has children<br />";
    }
    else
    {
        echo "This page doesn't have children<br />";
    }
    //Grab the parent node
    $parentnode =& $node->getParentNode();
  
    //Make sure something came back
    if ($parentnode != null)
    {
        //Get the actual content object
        $content =& $node->getContent();
        if ($content != null) //Just in case
        {
            //Display it's title
            echo $content->Title();
        }
    }
}


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Fri Mar 24, 2006 12:13 am 
Offline
Administrator
Administrator
User avatar

Joined: Fri Jun 11, 2004 6:58 pm
Posts: 3334
Location: Fairless Hills, Pa USA
Sorry, that's me being dumb.  Try:
Code:
echo $content->Name();


Can you tell that I didn't test the code?  :)

_________________
http://about.me/tedkulp


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Fri Mar 24, 2006 5:19 pm 
Offline
Forum Members
Forum Members

Joined: Thu Mar 16, 2006 3:17 am
Posts: 32
Hi,  Thanks for all this work.

I tried out the code with the new Name() function in there and everything worked, except that it just displayed the page name of the current page.  I was sort of hoping for the top level parent.

So I've be fiddling with it and have got something that will show you the title of the parent.  Or the title of the current page if there is no parent.  I don't know if we could use recursion to get up to the top level parent.  (Is that allowed??!)  Anyways, here's the function.

Code:
// This is only going to step up the tree one level

global $gCms;
$content_id = $gCms->variables['content_id'];
$hm =& $gCms->GetHierarchyManager();

$node =& $hm->getNodeById($content_id);

if ($node != null)
{

    //Grab the parent node
    $parentnode =& $node->getParentNode();
    $ncontent =& $node->getContent();
    $pnode = $ncontent->ParentID();

    //Make sure something came back
    if ($pnode != -1)
    {
        //Get the actual content object
        $content =& $parentnode->getContent();
    }
    else {
        //Ok no parent lets get the title of this node
        $content = $ncontent;
     }

     if ($content != null) //Just in case
        {
            //Display it's title
            echo $content->Name();
        }  
}


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Tue Apr 11, 2006 1:26 pm 
Ted wrote:
Ok, time for an intro to hierarchy manager, which is new in 0.12.
...
Anyway, you should get the idea.  This is a very powerful tool, but of course it's undocumented and well under the hood, so it's not easy to realize that it's there.  Enjoy!

Even if the documentation is minimal, I think it would be very useful to have the ContentHierarchyManager class appear in the API documentation. That would allow one to find easily which methods it provides.


Top
  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Mon Apr 24, 2006 2:08 am 
Offline
Forum Members
Forum Members
User avatar

Joined: Wed Apr 19, 2006 3:08 am
Posts: 146
Location: Rochester, NY
jzip wrote:
I think it would be very useful to have the ContentHierarchyManager class appear in the API documentation.


Indeed! I know you're working on 0.13. But, could you please update the API docs to 0.12 for those of us trying to develop new code?

Thanks,
Tim

_________________
spBackup - PHP-based web site backup tool for files and MySQL databases


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Fri Jan 25, 2008 3:56 pm 
Offline
Forum Members
Forum Members

Joined: Wed Mar 07, 2007 3:46 pm
Posts: 27
Location: London, England
Quote:
// This is only going to step up the tree one level


How would I get it to show the highest level? I have content on e.g. level 2.1.3 and 3.2.2 but I would like to show the highest level title (2 and 3 in this case)

thanks

Mark


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Fri Jan 25, 2008 4:21 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 5954
Location: Fernie British Columbia, Canada
The CGSimpleSmarty module (what a bad name, who thought of that?) has smarty functions to
allow you to:

a) get the page alias of the parent of the current page (or any page)
   
Code:
{$cgsimple->get_parent_alias()}


b) get the page alias of the root parent of the current page (or any page)
   
Code:
{$cgsimple->get_root_alias()}


c) get the page title of any page
   
Code:
{$cgsimple->get_page_title($alias)}


d) get the content from any content block in any page
   
Code:
{$cgsimple->get_page_content($alias)}


e) detect if the current page (or any page) has children
   
Code:
{$cgsimple->has_children()}


Just thought I'd let you know.... it's been tested, and it works well.

_________________
Follow me on twitter
For quality help follow these instructions:
a) Think about the problem for an hour
b) research the problem for an hour
c) spend 1/2 an hour explaining it and providing as much information as you can muster
(too much information is okay, not enough information may get your question ignored).
--
if you can't bother explaining your problem well, why should we bother helping with it.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Fri Jan 25, 2008 7:24 pm 
Offline
Forum Members
Forum Members

Joined: Wed Mar 07, 2007 3:46 pm
Posts: 27
Location: London, England
Thanks, that works fine but it doesn't show the root title, the closest is the root alias


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Fri Jan 25, 2008 7:27 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 5954
Location: Fernie British Columbia, Canada
{$cgsimple->get_root_alias('','root_alias')}{$cgsimple->get_page_title($root_alias)}

_________________
Follow me on twitter
For quality help follow these instructions:
a) Think about the problem for an hour
b) research the problem for an hour
c) spend 1/2 an hour explaining it and providing as much information as you can muster
(too much information is okay, not enough information may get your question ignored).
--
if you can't bother explaining your problem well, why should we bother helping with it.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Sat Jan 26, 2008 11:43 am 
Offline
Forum Members
Forum Members

Joined: Wed Mar 07, 2007 3:46 pm
Posts: 27
Location: London, England
great thank you.

so just to help me understand is that saying: "first get the root alias then, from that root alias, get the page title"? And it's done this way because it can't get the page title directly.

thanks again


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Sat Jan 26, 2008 2:54 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 5954
Location: Fernie British Columbia, Canada
I could have written a function to get the root page title directly, but this is more flexible and re-useable.

_________________
Follow me on twitter
For quality help follow these instructions:
a) Think about the problem for an hour
b) research the problem for an hour
c) spend 1/2 an hour explaining it and providing as much information as you can muster
(too much information is okay, not enough information may get your question ignored).
--
if you can't bother explaining your problem well, why should we bother helping with it.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Using the Global variables from CMS
PostPosted: Mon Nov 23, 2009 3:46 pm 
Offline
Forum Members
Forum Members

Joined: Wed Nov 18, 2009 4:06 pm
Posts: 17
To pick up an old thread - is there a way to get a page URL based on the Alias? The page's URL can be constructed using the site's URL and the alias, but if the page in question is an external link, for example, that's no good...:/

_________________
USB Duplicator Software


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: uniqu3


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Arvixe - A CMSMS Partner