General UDT Question re: accessing module output etc

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
User avatar
wakewatcher
Forum Members
Forum Members
Posts: 149
Joined: Fri Dec 28, 2007 12:33 am

General UDT Question re: accessing module output etc

Post by wakewatcher »

I've got a general product support question but am showing a specific example to illustrate what I'm trying to learn.

When a UDT is invoked I want it to be able to invoke a module and retrieve its output data.

For example when a user gets to a certain page I want to log that into a database table. I was able to get my UDT to access and write into my table but I couldn't figure out how determine which user was on the page. I figured since the user had already logged in then his user id must be available somewhere. I found reference to putting $smarty = cmsms()->GetSmarty(); in a UDT but couldn't find what I was looking for (although it might be in there.) So I brute forced it. In my page content I did this:

Code: Select all

{FrontEndUsers form="silent"}
{page_timestamp id=$userid}
Then in my page_timestamp UDT I had the line:

Code: Select all

$id = $params['id']) ? $params['id'];  
to get the id. As mentioned that seems a bit clunky but works.

However I'm now playing around with events and this approach doesn't (seem to) work with events. Again a specific example but a general lack of understanding...

I've installed the JM_forum module and want on an "OnNewTopic" event to invoke a UDT that provides me information of the last posts so that I can do stuff with them. (e.g. poor man's email notification.) So for example from a page (or GCB) I can get that information by doing this.

Code: Select all

 {JM_Forum action="last_posts" assign="junk"}
<pre>
{if $itemcount > 0}
    {foreach from=$items item=entry}  
    {$entry->forum_name|cms_html_entity_decode}
      {$entry->subject|cms_html_entity_decode}
      {$entry->body|trim|strip_tags|summarize:40}
      {$entry->last_poster_time|rfc_date}
      {$entry->url}#msg{$entry->post_id}
    {/foreach}
{/if}
</pre>
So I guess my question boils down to how does one 'call' a module from within a UDT and then assign its output to php variables? (I think I understand smarty assignment the other way. e.g. pulling in php variables and then accessing them for displaying. Something like $smarty->assign('name', 'george smith'); {$name} but other than pulling passed parameters from the $params array I'm a bit stuck. -Thx-
Last edited by wakewatcher on Mon Feb 25, 2013 11:05 pm, edited 2 times in total.
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: General UDT Question about accessing module output etc.

Post by rotezecke »

smarty in UDT:

Code: Select all

$smarty = cmsms()->GetSmarty();
$smarty_data = "{CGSmartImage src='uploads/images/photos/wind/wind_turbine_paddock.jpg' filter_resize='w,300,0' filter_roundedcorners='10'}"; //
echo $smarty->display('string:'.$smarty_data);
module output in UDT:

Code: Select all

$feusers = cms_utils::get_module('FrontEndUsers');
$uid = $feusers->LoggedInId();
echo $uid;
User avatar
wakewatcher
Forum Members
Forum Members
Posts: 149
Joined: Fri Dec 28, 2007 12:33 am

Re: General UDT Question about accessing module output etc.

Post by wakewatcher »

My delay in the thanks was me using your great tips and first digging in and figuring the rest out. (And additional thanks to the smarty website.) This is what I ended up with. There may be a more elegant way but this gets me the page into a variable which then, at the very least, I can parse to pull out the stuff I want.

Code: Select all

$smarty = cmsms()->GetSmarty(); 
$smarty_data = "{JM_Forum action='last_posts'}";
$smarty->setCaching(true);
// set a separate cache_id for each unique URL
$cache_id = md5($_SERVER['REQUEST_URI']);
// capture the output
$output = $smarty->fetch('string:'.$smarty_data, $cache_id);
Thanks again!
User avatar
wakewatcher
Forum Members
Forum Members
Posts: 149
Joined: Fri Dec 28, 2007 12:33 am

Re: General UDT Question re: accessing module output etc

Post by wakewatcher »

:( I spoke a bit too soon. While it works in a page as a udt it doesn't work when invoked by a triggered event. So positioning this as a general event handler question but using a specific example for illustration; Using the JM_Forum Module I've set up an "OnNewTopic" event triggered which invokes my "store_last" udt coded as:

Code: Select all

$smarty = cmsms()->GetSmarty();
$smarty_data = "{JM_Forum action='last_posts'}"; 
$smarty->setCaching(true);
$cache_id = md5($_SERVER['REQUEST_URI']);
$output = $smarty->fetch('string:'.$smarty_data, $cache_id);
/*
Store $output in database
*/
I guess my general question is 1) is this a case (problem) of a UDT being processed differently when invoked from a page rather than an event or 2) is this a problem because my event is using functionality (morphed recursion?) of the module that triggered the event? That is JM_Forum is the module that triggers the "OnNewTopic" event and my event handler then uses (or tries to use) a rendition of JM_Forum to get information about the last posts in this case. -Thx-
User avatar
wakewatcher
Forum Members
Forum Members
Posts: 149
Joined: Fri Dec 28, 2007 12:33 am

Re: General UDT Question re: accessing module output etc

Post by wakewatcher »

Problem doesn't seem to be item 2 above as it hangs the page no matter which event I pin it to. E.g. I invoked it with the FEU OnLogin event and it just hangs and times out. Hmmm.....
User avatar
wakewatcher
Forum Members
Forum Members
Posts: 149
Joined: Fri Dec 28, 2007 12:33 am

Re: General UDT Question re: accessing module output etc

Post by wakewatcher »

I'd like to claim inspiration but I think more of desperation I just simplified and this seems to work as an event handler udt.

Code: Select all

$smarty_data = "{JM_Forum action='last_posts'}";
$output = $smarty->fetch('string:'.$smarty_data);
/*
Massage and store $output in database
*/
I read in the documentation that:
As of CMSMS version 1.11 it is invalid to overwrite the $smarty object that is passed in to the UDT with the global smarty object.
In example in a User Defined Tag this code is invalid and will cause errors:
$smarty = cmsms()->GetSmarty()
so I pulled that line and it still didn't work so I simplified it further as shown. I have no idea what will happen under load by turning off caching but for now it works.
Post Reply

Return to “CMSMS Core”