Announcement

  •  » Extensions
  •  » How to make sure a particular image information never gets cached?

#1 2016-07-12 23:50:17

hvergelmir
Member
2016-07-12
4

How to make sure a particular image information never gets cached?

Hi,

I'd like to add to each 'picture' page another item for the image information. This item should be computed the time the user requests the page and shown like the "Visits" or "Dimensions" item.

Since the information gets computed on-the-fly, I guess I need to re-parse the template and flush it or do something else along these lines. This is where I am stuck.

Since I don't want to explain the details of how this information is computed, for this question we can assume that I want to display the current date and time whenever a page is requested.

How would I achieve that? Currently I got it to work to the point where the current date and time are assigned the template inside a `loc_end_picture` event handler. My plugin roughly looks like this:

Code:

add_event_handler('loc_end_picture', 'myplugin_loc_end_picture');

function myplugin_loc_end_picture()
{
  global $template;
  $template->set_prefilter('picture', 'myplugin_picture_prefilter');
  $date = new DateTime();
  $template->assign('INFO_CURRENT_DATETIME', $date->format('Y-m-d H:i:sP'));
}

function myplugin_picture_prefilter($content)
{
  $search =  '{if $display_info.visits}';
  $replace = "\t" . '<div id="CurrentDateTime" class="imageInfo"><dt>{\'myplugin_datetime\'|@translate}</dt><dd>$INFO_CURRENT_DATETIME</dd></div>' . "\n\t";
  return str_replace($search, $replace.$search, $content);
}

Quite frankly I'm not even sure I picked the correct event handler for the purpose. However, I need something that triggers whenever a 'picture' is requested. And then - sidestepping the cache - display the latest information instead of some cached version of it.

Thanks for reading.


Piwigo version: 2.8.2
PHP version: PHP 5.5.9-1ubuntu4.17
MySQL version: 5.5.49-MariaDB

Offline

 

#2 2016-07-13 17:29:56

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13789

Re: How to make sure a particular image information never gets cached?

Hi hvergelmir,

That's nearly good :-)

In the $replace, you should add {} around $INFO_CURRENT_DATETIME

Maybe you should also add $smarty as second argument to prefilter function:

Code:

function myplugin_picture_prefilter($content, $smarty)

Offline

 

#3 2016-07-13 17:30:52

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13789

Re: How to make sure a particular image information never gets cached?

... and don't worry about cache. The template is cached, not the values sent to the template.

Offline

 

#4 2016-07-13 23:32:44

hvergelmir
Member
2016-07-12
4

Re: How to make sure a particular image information never gets cached?

Thanks for the response and your valuable suggestions.

I'll implement your suggestions and get back with a corrected snippet, in case others want or need something similar.

Offline

 

#5 2016-07-14 00:06:28

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13789

Re: How to make sure a particular image information never gets cached?

Offline

 

#6 2016-07-14 00:27:56

hvergelmir
Member
2016-07-12
4

Re: How to make sure a particular image information never gets cached?

Merci beaucoup, Pierrick!

The two changes you recommended (curly brackets around the smarty variable and adding the $smarty argument) fixed the whole thing for me.

Code:

add_event_handler('loc_end_picture', 'myplugin_loc_end_picture');

function myplugin_loc_end_picture()
{
  global $template;
  $template->set_prefilter('picture', 'myplugin_picture_prefilter');
  $date = new DateTime();
  $template->assign('INFO_CURRENT_DATETIME', $date->format('Y-m-d H:i:sP'));
}

function myplugin_picture_prefilter($content, $smarty)
{
  $search =  '{if $display_info.visits}';
  $replace = "\t" . '<div id="CurrentDateTime" class="imageInfo"><dt>{\'myplugin_datetime\'|@translate}</dt><dd>{$INFO_CURRENT_DATETIME}</dd></div>' . "\n\t";
  return str_replace($search, $replace.$search, $content);
}

The only other thing I did in the place where the above reads

Code:

$template->assign('INFO_CURRENT_DATETIME', $date->format('Y-m-d H:i:sP'));

was to assign a string resource, such that I could assemble a translatable template as a string. So for a variable 'myplugin_date' I would use:

Code:

$template->assign('INFO_CURRENT_DATETIME', $template->smarty->fetch('string:{'myplugin_date'|translate}'.$date->format('Y-m-d H:i:sP')));

The above is a contrived example to simplify the question. In reality I am showing an age in years, months, days, hours and minutes - skipping any items that may not exist (e.g. 0 years) and taking care to translate singular, plural, dual and the special case for value zero.

Last edited by hvergelmir (2016-07-14 00:28:14)

Offline

 

#7 2016-07-14 00:31:54

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13789

Re: How to make sure a particular image information never gets cached?

hvergelmir wrote:

In reality I am showing an age in years, months, days, hours and minutes - skipping any items that may not exist (e.g. 0 years) and taking care to translate singular, plural, dual and the special case for value zero.

A bit like [Github] Piwigo-birthdate file include/functions.inc.php@L4

Offline

 

#8 2016-07-14 00:39:43

hvergelmir
Member
2016-07-12
4

Re: How to make sure a particular image information never gets cached?

plg wrote:

hvergelmir wrote:

In reality I am showing an age in years, months, days, hours and minutes - skipping any items that may not exist (e.g. 0 years) and taking care to translate singular, plural, dual and the special case for value zero.

A bit like [Github] Piwigo-birthdate file include/functions.inc.php@L4

Pretty much, yeah.

However, I am using PHP's DateTime (as I don't have to support older 5.x flavors of PHP and DateTime::diff gives a nice representation and all of it is timezone-aware.

Here's my respective code ... you feed it a date/time (with or without timezone) that can be parsed by DateTime:

Code:

function pluralize_time_unit($numval, $singular, $plural, $dual = NULL, $zero = NULL)
{
        if($numval >= 0)
        {
                switch($numval)
                {
                case 0:
                        return is_null($zero) ? false : sprintf('%d %s', $numval, $zero);
                case 1:
                        return sprintf('%d %s', $numval, $singular);
                case 2:
                        return sprintf('%d %s', $numval, is_null($dual) ? $plural : $dual);
                default:
                        return sprintf('%d %s', $numval, $plural);
                }
        }
        return false;
}

function time_since_birth($strBirthDate='now')
{
        $retval = '';
        $birth = new DateTime($strBirthDate);
        $since = $birth->diff(new DateTime());
        $sincearr = array(
                array($since->y, "{'ma_year'|translate}",   "{'ma_years'|translate}"),
                array($since->m, "{'ma_month'|translate}",  "{'ma_months'|translate}"),
                array($since->d, "{'ma_day'|translate}",    "{'ma_days'|translate}"),
                array($since->h, "{'ma_hour'|translate}",   "{'ma_hours'|translate}"),
                array($since->i, "{'ma_minute'|translate}", "{'ma_minutes'|translate}", ),
        );
        for($i = 0; $i < sizeof($sincearr); $i++)
        {
                $tmpstr = pluralize_time_unit($sincearr[$i][0], $sincearr[$i][1], $sincearr[$i][2], isset($sincearr[$i][3]) ? $sincearr[$i][3] : NULL, isset($sincearr[$i][4]) ? $sincearr[$i][4] : NULL);
                if(false != $tmpstr)
                {
                        $retval .= ((strlen($retval)) ? ', ' : '') . $tmpstr;
                }
        }
        return $retval;
}

Language handling is a bit crude at the moment, but it is prepared to deal not just with singular and plural, but also with dual used by some languages and special cases for zero (here reusing plural form at the moment).

Offline

 
  •  » Extensions
  •  » How to make sure a particular image information never gets cached?

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact