1 (edited by RTaulbee 2009-06-23 21:21)

Topic: Little PHP help please (PunBB related)

Don't shoot me, but I'm a SQL Server, ASP, ASP.Net guy, but like all good programmers, we can quickly adapt to other languages given time. 

So I'm trying to edit a bit of the PunBB "pun_pm" extension.  After several hours of tracing code back and reading on the forum, I've found that cache_hooks.php is where I want to make my edit.  I've also found the exact snippet I want to update, but I can't get it to work yet.

Idea: When a user gets a PM, show a large flashy image or something to actually get attention.  I've gone several weeks not noticing the New Message (1) hyperlink.  I'm not the only one either.  So basically I want to say "when New Message count > 0, show big flashy image, otherwise, business as usual"

Here's the code in cache_hooks.com

        $visit_elements[\'<!-- forum_visit -->\'] = preg_replace(\'#(<p id="visit-links" class="options">.*?)(</p>)#\', \'$1 <span><a href="\'.forum_link($forum_url[\'pun_pm_inbox\']).\'">\'.pun_pm_unread_messages().\'</a></span>$2\', $visit_elements[\'<!-- forum_visit -->\']);

Here's what I'm trying to do:

If (pun_pm_unread_messages() > 0)
{
        $visit_elements[\'<!-- forum_visit -->\'] = preg_replace(\'#(<p id="visit-links" class="options">.*?)(</p>)#\', \'$1 <span><a href="\'.forum_link($forum_url[\'pun_pm_inbox\']).\'"><img src="http://www.blah.com/mybigflashygif.gif"></a></span>$2\', $visit_elements[\'<!-- forum_visit -->\']);
}
else (same as original)
{
        $visit_elements[\'<!-- forum_visit -->\'] = preg_replace(\'#(<p id="visit-links" class="options">.*?)(</p>)#\', \'$1 <span><a href="\'.forum_link($forum_url[\'pun_pm_inbox\']).\'">\'.pun_pm_unread_messages().\'</a></span>$2\', $visit_elements[\'<!-- forum_visit -->\']);
}

Thanks for all help

Re: Little PHP help please (PunBB related)

Typo in "Here's the code in cache_hooks.com".

First, I'd check what kind of variable pun_pm_unread_messages() returns.

After reading 'functions.php' inside pun_pm's directory, the function returns a string formatted like so: "New Messages (X)".

So, you should use a regex check for that. Basically,

if(preg_match('/^'.str_replace(array('/','(%d)'), array('\/','\([0-9]*\)'), $lang_pun_pm['New link active']).'$/i', pun_pm_unread_messages()) > 0)
{
/* whatever you want in here */
}

Note: that could possibly not work with languages other than English.

As for the 'flashy image' you want, I'd use some JavaScript instead. Example:

if(confirm('You have one or more unread private messages. Would you like to read them?'))
    window.location = '/pun_pm/inbox/'; /* or whatever your relative URL to the inbox is */

All together:

if(preg_match('/^'.str_replace(array('/','(%d)'), array('\/','\([0-9]*\)'), $lang_pun_pm['New link active']).'$/i', pun_pm_unread_messages()) > 0)
{
$visit_elements['<!-- forum_visit -->'] .= "<script type=\"text/javascript\">if(confirm('You have one or more unread private messages. Would you like to read them?'))
    window.location = '/pun_pm/inbox/'; /* or whatever your relative URL to the inbox is */</script>";
}

FYI: do that change on Line 458 in 'extensions/pun_pm/manifest.xml'.

Let me know if that doesn't work.

Notes
The $visit_elements['<!-- forum_visit -->'] variable is probably not the best place to add the javascript, but it just seems to work for me. If it doesn't work for you, let met know and I'll find some other variable to put that in.

The alert message shows up as the page is loading. Meaning that the browser will stop loading until the user click Ok/Cancel. To fix this, you'd have to use jQuery or other JS framework that has an 'onload' event to run the alert in.

I just made this "mod" in 3-5 minutes so don't just say it doesn't work. It just needs some polishing.

- Garciat

Re: Little PHP help please (PunBB related)

Can't you just do this via CSS?

This problem resembles the one solved by CSS over at Flux:

http://fluxbb.org/forums/topic/2960/add … sergroups/

4 (edited by RTaulbee 2009-06-24 15:03)

Re: Little PHP help please (PunBB related)

@Garciat: Thanks so much for taking the time to work through it.  It helped me get on the right track

@Sirena: Perhaps.  Either way I would still have to code in an "if unread messages count > 0" statement which is where the real hang up was.  In other words "If unread message count > 0, use this style, otherwise this style" at least I think(?)

Here's my final solution.

I added a function to the pun_pm functions.php file to return an integer instead of a string.  I did this because I think it's cleaner and less clutter than parsing through a string in order to get a number.  So now I don't have to parse the "New Messages (1)" in order to just grab the integer value (which is all I care about for my if statement)

function pun_pm_unread_messages_integer()
{
    global $forum_db, $forum_user, $forum_config, $lang_pun_pm, $pun_pm_inbox_full;

    list($new_messages, $pun_pm_inbox_full) = pun_pm_read_cache();

    if ($new_messages === false)
    {
        pun_pm_deliver_messages();

        //How much delivered messages do we have?
        $query = array(
            'SELECT'    => 'count(id)',
            'FROM'        => 'pun_pm_messages',
            'WHERE'        => 'receiver_id = '.$forum_user['id'].' AND status = \'delivered\' AND deleted_by_receiver = 0'
        );

        $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);

        list($new_messages) = $forum_db->fetch_row($result);

        pun_pm_write_cache($forum_user['id'], $new_messages, $pun_pm_inbox_full);
    }

    return $new_messages;
}

Secondly, I modified the cache_hooks.php file to do the "if statement" and perform the swap.

        if(pun_pm_unread_messages_integer() > 0)
            {
                $visit_elements[\'<!-- forum_visit -->\'] = preg_replace(\'#(<p id="visit-links" class="options">.*?)(</p>)#\', \'$1 <span><a href="\'.forum_link($forum_url[\'pun_pm_inbox\']).\'"><img src="myimage.gif"></a></span>$2\', $visit_elements[\'<!-- forum_visit -->\']);            
            }
        else
            {
                $visit_elements[\'<!-- forum_visit -->\'] = preg_replace(\'#(<p id="visit-links" class="options">.*?)(</p>)#\', \'$1 <span><a href="\'.forum_link($forum_url[\'pun_pm_inbox\']).\'">\'.pun_pm_unread_messages().\'</a></span>$2\', $visit_elements[\'<!-- forum_visit -->\']);    
            }

I realize there are always many ways to solve a problem and this is just one.  I also realize that it's another database query that needs to happen, but performance isn't an issue.  This is for a small forum with maybe 15-20 active posters and only 3-4 online at a time.  It may not be the best or what you think is the best, but I understand it and like it.  Mission accomplished!

Thanks for the input again Garcia/Sirena.

Re: Little PHP help please (PunBB related)

RTaulbee wrote:

Secondly, I modified the cache_hooks.php file to do the "if statement" and perform the swap.

        if(pun_pm_unread_messages_integer() > 0)
            {
                $visit_elements['<!-- forum_visit -->'] = preg_replace('#(<p id="visit-links" class="options">.*?)(</p>)#', '$1 <span><a href="'.forum_link($forum_url['pun_pm_inbox']).'"><img src="myimage.gif"></a></span>$2', $visit_elements['<!-- forum_visit -->']);            
            }
        else
            {
                $visit_elements['<!-- forum_visit -->'] = preg_replace('#(<p id="visit-links" class="options">.*?)(</p>)#', '$1 <span><a href="'.forum_link($forum_url['pun_pm_inbox']).'">'.pun_pm_unread_messages().'</a></span>$2', $visit_elements['<!-- forum_visit -->']);    
            }

I realize there are always many ways to solve a problem and this is just one.  I also realize that it's another database query that needs to happen, but performance isn't an issue.  This is for a small forum with maybe 15-20 active posters and only 3-4 online at a time.  It may not be the best or what you think is the best, but I understand it and like it.  Mission accomplished!

Thanks for the input again Garcia/Sirena.

If you do this then when you then install a new extension or regenerate new cache files the cache_hooks.php gets overwritten with a new one and you will lose the code I think.

Best is to do is some other way.
What way I do not know.
Just a hint so you know.

Re: Little PHP help please (PunBB related)

I wondered if that would happen, thanks for the heads up!

Since it's just a snippet, I'll replace it when I install another extension.  I don't think it happens enough for me to go "back to the drawing board" on this.

Re: Little PHP help please (PunBB related)

You can also change the manifest.xml to hold that script and then generate new cache files. then you would not have to worry about that.

If something ends up in the cache file then is has to come from the manifest.xml.

Re: Little PHP help please (PunBB related)

Good point.  My "quest" to find the source originally took me to the manifest and I made the change there.  Then I realized, "that did nothing" and my quest continued.

I'll update the manifest to reflect the changes.  Thanks so much. smile

Re: Little PHP help please (PunBB related)

If you make any changes to the manifest, change the extension's version so you can "update" it in the ACP.

Re: Little PHP help please (PunBB related)

Actually the best way to add an image is to edit the 'New link active' entry in the language file.

Or you could create an extension and place your code in pun_pm_fn_unread_messages_end and pun_pm_hd_visit_elements_pre_change hooks.