Seeking info: replacing a currently used tag

Discuss PivotX 2.0.x extensions here, and view extensions that are available for download.

Seeking info: replacing a currently used tag

Postby Stingray » Tue May 10, 2011 8:06 pm

I'm looking to replace the [[ comments ]] tag in an extension so that I can release my useragent info hack as an ordinary extension (I can provide a link as soon as my site is back up: server move and other transitions).

However, looking at the associated file and looking in the fancybox extension for a bit of guidance, I'm curious if doing the following (simply stated) is what I'm supposed to do:
Code: Select all
<?php
// - Extension: [TBD]
// - Version: 0.1
// - Author: Stingray
// - Email: stingray@afn.org
// - Site: http://geoffrey.famwagner.com/
// - Description: Add useragent information to your commenters' comments
// - Date: 2011-05-00
// - Identifier: [TBD]
// - Required PivotX version: 2.2.5

// Reregister 'comments', overriding the old 'comments'
$PIVOTX['template']->unregister_block('comments');
$PIVOTX['template']->register_block('comments', 'smarty_uacomments');

function smarty_uacomments($params, $format, &$smarty) {
    [php code - mostly copied from module_smarty.php (function smarty_comments) with ua enhancements]
}


I'm mostly curious if I'm doing the unregistering/registering part correctly and if defining the new function AFTER the registering is what I'm supposed to do.

I've got some ideas for configuration of this snippet in the PivotX backend, too, but I want to get this part up and running, first.

Also, as soon as my site(s) return, I'll be getting to work on this, right away.

Thanks for the help!
Stingray
 
Posts: 107
Joined: Wed Oct 28, 2009 2:40 pm

Re: Seeking info: replacing a currently used tag

Postby Harm10 » Tue May 10, 2011 10:31 pm

As far as I can conclude I think your approach is the right one. You can also look at the Hello world, Test and Warning extensions which are no real extensions but purely samples. Make sure you put your own extension in the extension folder and name the file correctly (snippet or hook). You are aware that you also can define your own template tag via an extension? Because I'm not entirely sure why you want to reregister comments...... ;)
Quality is in the detail of things............

Want to change or update your PivotX site? Mail or PM me!
I can also convert your site to a Wordpress site!
Harm10
Developer
 
Posts: 2011
Joined: Wed Jun 17, 2009 9:37 am
Location: Somewhere in The Netherlands (aka Holland)

Re: Seeking info: replacing a currently used tag

Postby Stingray » Wed May 11, 2011 2:13 am

Harm10 wrote:As far as I can conclude I think your approach is the right one. You can also look at the Hello world, Test and Warning extensions which are no real extensions but purely samples. Make sure you put your own extension in the extension folder and name the file correctly (snippet or hook). You are aware that you also can define your own template tag via an extension? Because I'm not entirely sure why you want to reregister comments...... ;)


Thanks for the info!

I've already written two extensions (snippets), each of which add a new template tag, so I've got a general grasp of creating a new tag. For this new one, I want to enhance (as I see it) an existing tag. It will do exactly the same thing as [[ comments ]], but I will have added a few more %tags% (what are these called, btw? parameters?) to be replaced by other information. While templates will need to be edited anyway to use this extension, keeping the tag name the same made more sense to me.
Stingray
 
Posts: 107
Joined: Wed Oct 28, 2009 2:40 pm

Re: Seeking info: replacing a currently used tag

Postby Harm10 » Wed May 11, 2011 5:28 am

The documentation calls them parameters: http://book.pivotx.net/index.php?page=app-b#anchor-comments.
Maybe your changes to comments (and the other template tags) would interest others as well?
If so, you could submit them to development to incorporate them into a new release?
For comments you have to be aware that you always check whether this tag does not change in future........ but I am sure you are aware of that! ;)
Quality is in the detail of things............

Want to change or update your PivotX site? Mail or PM me!
I can also convert your site to a Wordpress site!
Harm10
Developer
 
Posts: 2011
Joined: Wed Jun 17, 2009 9:37 am
Location: Somewhere in The Netherlands (aka Holland)

Re: Seeking info: replacing a currently used tag

Postby hansfn » Wed May 11, 2011 7:22 am

Stingray, it's much better if you submit your minimal change to smarty_comments (so it supports display of useragent) to me - hansfn@pivotx.net. If you are really quick it might get included in the release coming very, very soon. Modifying a core template tag (or creating a replacement tag) will become a maintenance problem.

PS! It could be considered a bug, that we only added storing of useragent info for comments and didn't add displaying.
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Seeking info: replacing a currently used tag

Postby Stingray » Wed May 11, 2011 7:19 pm

That's a possibility. What I have done, the results of which you've seen hans, is display an image corresponding to the commenter's OS and browser. It's a routine I'm constantly updating (well, pretty often anyway, more often than stable releases of PivotX have come out, that is) as well as the images. I'm really wishing the server issues my host is experiencing were over so I could show again what I have done. Though, I did keep a rudimentary version of this as a hack for the old PivotLog, and you can see some of the work (crude by comparison to what I have written now) on those forums.

Given what has been said so far, I'm coming around; I would create a new tag, not replace [[ comments ]].

As a short preview, and I plan to refine this more once/if I turn it into an extension, within the smarty_comments I've put this:
Code: Select all
                    // get translate ua function
                    include_once($Paths['pivot_path']."includes/useragent.inc.php");
                    // make images for useragents
                    if ($temp_row['useragent']) {
                        $uainfo = translateua($temp_row['useragent']);
                    } else {
                        $uainfo = array();
                        $uainfo['os']['link'] = "";
                        $uainfo['os']['image'] = "<img src=\"pivotx/pics/useragent/unknown.png\" border=\"0\" alt=\"Unknown/Unspecified OS\" title=\"Unknown/Unspecified OS\" />";
                        $uainfo['os']['text'] = "UNKNOWN";
                        $uainfo['browser']['link'] = "";
                        $uainfo['browser']['image'] = "<img src=\"pivotx/pics/useragent/unknown.png\" border=\"0\" alt=\"Unknown/Unspecified Browser\" title=\"Unknown/Unspecified Browser\" />";
                        $uainfo['browser']['text'] = "UNKNOWN";
                        $uainfo['browser']['typeimg'] = "";
         $uainfo['browser']['typetext'] = "";
                    }

The configuration I had in mind was to give the user a choice to use a local copy of useragents.inc.php and the useragent images or resources I would keep on one of my sites (I've got the bandwidth for it). There are pros and cons to either choice.

Parameters are handled thus:
Code: Select all
                    $this_tag= str_replace("%gravatar%", $gravatar, $this_tag);
                    $this_tag= str_replace("%editlink%", $editlink, $this_tag);
                    // My additions
                    $this_tag= str_replace("%oslink%", $uainfo['os']['link'], $this_tag);
                    $this_tag= str_replace("%os%", $uainfo['os']['image'], $this_tag);
                    $this_tag= str_replace("%ostext%", $uainfo['os']['text'], $this_tag);
                    $this_tag= str_replace("%browserlink%", $uainfo['browser']['link'], $this_tag);
                    $this_tag= str_replace("%browser%", $uainfo['browser']['image'], $this_tag);
                    $this_tag= str_replace("%browsertext%", $uainfo['browser']['text'], $this_tag);
                    $this_tag= str_replace("%gecko-webkit%", $uainfo['browser']['typeimg'], $this_tag);
                    $this_tag= str_replace("%gecko-webkit-text%", $uainfo['browser']['typetext'], $this_tag);


Now, if you want to include this as part of the next build of PivotX, by all means, please do. I can send you the useragents.inc.php file (which, again, I was planning on refining for the intended extension), and I would be more than pleased to keep that updated, as well as the images. If we do go this route, we should probably figure out a way to make it easy for people to update these files apart from the rest of PivotX, perhaps even giving the option to use local or remote copies.
Stingray
 
Posts: 107
Joined: Wed Oct 28, 2009 2:40 pm

Re: Seeking info: replacing a currently used tag

Postby hansfn » Wed May 11, 2011 9:53 pm

OK, after looking at your code I agree that making it an extension might be better. In addition the list of user agents probably needs to be updated often as you say. However, I have some really good news - we have been good developers and there is a hook that you can use to alter the output of the comments tag. Unfortunately, we have also been bad developers - there is a bug so the hook doesn't work. I have fixed the bug in revision 3676 and it will be included in PivotX 2.2.6. After applying the fix, you can write your extension as below:

Code: Select all
$this->addHook(
    'comment_after_parse',
    'callback',
    'addUseragentInfo'
);

function addUseragentInfo(&$commentinfo) {
    global $PIVOTX;

    $format = $commentinfo['format'];
    $comment = $commentinfo['comment'];

    // Do the work - minimal example ...
    $output = str_replace("%useragent%", $comment['useragent'], $format);

    $commentinfo['format'] = $output;
    $commentinfo['comment'] = $comment;

}


I have left out the meta data since you know how to write that. Remember to set "Required PivotX version" to 2.2.6 when you publish it. (You can send the extension directly to Harm too.)

PS! I have tested and my code above actually works (after applying the hook fix).
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Seeking info: replacing a currently used tag

Postby Stingray » Wed May 11, 2011 10:59 pm

That's awesome! Thanks, hans!

Bugs in your code don't make you bad developers, but human. Ignoring the bugs makes you bad developers. Acknowledging and working to fix the bugs makes you great developers.

So, this would be a hook extension, then (filename hook_something.php)? I can still have a hook extension be configurable in the PivotX backend, right?

I can't believe how much the prospect of working on this excites me.
Stingray
 
Posts: 107
Joined: Wed Oct 28, 2009 2:40 pm

Re: Seeking info: replacing a currently used tag

Postby hansfn » Wed May 11, 2011 11:24 pm

Look for example at the Piwik extension for some inspiration - it has a hook and an admin/backend part for configuration.
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Seeking info: replacing a currently used tag

Postby Stingray » Mon May 30, 2011 8:37 pm

My sites are back, will be upgrading to 2.2.6 very soon, and get to work on the extension this weekend. I'm gonna need a little bit of a break from all this before working on the extension, but I will get on it.
Stingray
 
Posts: 107
Joined: Wed Oct 28, 2009 2:40 pm

Next

Return to 2.x Extensions

Who is online

Users browsing this forum: No registered users and 3 guests

cron