source: extensions/modus/themeconf.inc.php @ 27412

Last change on this file since 27412 was 27412, checked in by rvelices, 10 years ago

modus js optims

File size: 16.2 KB
Line 
1<?php
2/*
3Theme Name: modus
4Version: 2.6.b
5Description: Responsive, horizontal menu, retina aware, no lost space.
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=728
7Author: rvelices
8Author URI: http://www.modusoptimus.com
9*/
10$themeconf = array(
11        'name'  => 'modus',
12        'parent' => 'default',
13);
14
15define('MODUS_STR_RECENT', "\xe2\x9c\xbd"); //HEAVY TEARDROP-SPOKED ASTERISK
16define('MODUS_STR_RECENT_CHILD', "\xe2\x9c\xbb"); //TEARDROP-SPOKED ASTERISK
17
18if (isset($conf['modus_theme']) && !is_array($conf['modus_theme']))
19{
20        $conf['modus_theme'] = unserialize($conf['modus_theme']);
21}
22
23if (!empty($_GET['skin']) && !preg_match('/[^a-zA-Z0-9_-]/', $_GET['skin']))
24        $conf['modus_theme']['skin'] = $_GET['skin'];
25
26$this->assign('MODUS_CSS_VERSION', crc32(implode(',', array(
27                'a'.@$conf['modus_theme']['skin'],
28                @$conf['modus_theme']['album_thumb_size'],
29                ImageStdParams::get_by_type(IMG_SQUARE)->max_width(),
30        ))));
31
32if (isset($_COOKIE['caps']))
33{
34        setcookie('caps',false,0,cookie_path());
35        pwg_set_session_var('caps', explode('x', $_COOKIE['caps']) );
36        /*file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'tmp/modus.log', implode("\t", array(
37                date("Y-m-d H:i:s"), $_COOKIE['caps'], $_SERVER['HTTP_USER_AGENT']
38                ))."\n", FILE_APPEND);*/
39}
40
41if ('mobile'==get_device())
42        $conf['tag_letters_column_number'] = 1;
43elseif ('tablet'==get_device())
44        $conf['tag_letters_column_number'] = min($conf['tag_letters_column_number'],3);
45
46$this->smarty->registerFilter('pre', 'rv_menubar');
47function rv_menubar($source)
48{
49        global $lang, $conf;
50
51        $source = str_replace('<div id="imageHeaderBar">', '<div class=titrePage id=imageHeaderBar>', $source );
52        $source = str_replace('<div id=imageHeaderBar>',   '<div class=titrePage id=imageHeaderBar>', $source );
53
54        if (!isset($lang['modus_theme']))
55                load_language('theme.lang', dirname(__FILE__).'/');
56
57        // picture page actionButtons wrap for mobile
58        if (strpos($source, '<div id="imageToolBar">')!==false || strpos($source, '<div id=imageToolBar>')!==false){
59                if ( !($pos=strpos($source,'<div class="actionButtons">') ) )
60                        $pos = strpos($source,'<div class=actionButtons>');
61                if ($pos !== false)
62                {
63                        $source = substr_replace($source, '<div class=actionButtonsWrapper><a id=imageActionsSwitch class=pwg-button><span class="pwg-icon pwg-icon-ellipsis"></span></a>{combine_script version=1 id=\'modus.async\' path="themes/`$themeconf.id`/js/modus.async.js" load=\'async\'}', $pos, 0);
64                        $pos = strpos($source,'caddie', $pos+1);
65                        $pos = strpos($source,'</div>', $pos+1);
66                        $source = substr_replace($source, '</div>', $pos, 0);
67                }
68        }
69
70        if ( ($pos=strpos($source, '<ul class="categoryActions">'))!==false || ($pos=strpos($source, '<ul class=categoryActions>'))!==false){
71                if ( ($pos2=strpos($source, '</ul>', $pos))!==false
72                        && (substr_count($source, '<li>', $pos, $pos2-$pos) > 2) )
73                        $source = substr_replace($source, '<a id=albumActionsSwitcher class=pwg-button><span class="pwg-icon pwg-icon-ellipsis"></span></a>{combine_script version=1 id=\'modus.async\' path="themes/`$themeconf.id`/js/modus.async.js" load=\'async\'}', $pos, 0);
74        }
75
76        $re = preg_quote('<img title="{$cat.icon_ts.TITLE}" src="', '/')
77                        .'[^>]+'
78                        .preg_quote('/recent{if $cat.icon_ts.IS_CHILD_DATE}_by_child{/if}.png"', '/')
79                        .'[^>]+'
80                        .preg_quote('alt="(!)">', '/');
81        $source = preg_replace('/'.$re.'/',
82                '<span class=albSymbol title="{$cat.icon_ts.TITLE}">{if $cat.icon_ts.IS_CHILD_DATE}'.MODUS_STR_RECENT_CHILD.'{else}'.MODUS_STR_RECENT.'{/if}</span>',
83                $source);
84
85        $re = preg_quote('<img title="{$thumbnail.icon_ts.TITLE}" src="', '/')
86                .'[^>]+'
87                .preg_quote('/recent.png" alt="(!)">', '/');
88        $source = preg_replace('/'.$re.'/',
89                '<span class=albSymbol title="{$thumbnail.icon_ts.TITLE}">'.MODUS_STR_RECENT.'</span>',
90                $source);
91
92        return $source;
93}
94
95
96if (!defined('IN_ADMIN') && defined('RVCDN') )
97{
98        $this->smarty->registerFilter('pre', 'rv_cdn_prefilter' );
99        add_event_handler('combined_script', 'rv_cdn_combined_script', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
100}
101
102function rv_cdn_prefilter($source, &$smarty)
103{
104        $source = str_replace('src="{$ROOT_URL}{$themeconf.icon_dir}/', 'src="'.RVCDN_ROOT_URL.'{$themeconf.icon_dir}/', $source);
105        $source = str_replace('url({$'.'ROOT_URL}', 'url('.RVCDN_ROOT_URL, $source);
106        return $source;
107}
108function rv_cdn_combined_script($url, $script)
109{
110        if (!$script->is_remote() /*&& strpos($script->path,'thumb.arrange')===false*/)
111                $url = RVCDN_ROOT_URL.$script->path;
112        return $url;
113}
114
115if (defined('RVPT_JQUERY_SRC'))
116add_event_handler('loc_begin_page_header', 'modus_loc_begin_page_header');
117function modus_loc_begin_page_header()
118{
119        global $template;
120        $all = $template->scriptLoader->get_all();
121        if ( ($jq = @$all['jquery']) )
122        {
123                $jq->set_path(RVPT_JQUERY_SRC);
124        }
125}
126
127add_event_handler('combinable_preparse', 'modus_combinable_preparse');
128function modus_combinable_preparse($template)
129{
130        global $conf;
131        include_once(dirname(__FILE__).'/functions.inc.php');
132  $template->smarty->unregisterPlugin('modifier', 'cssGradient');
133        $template->smarty->registerPlugin('modifier', 'cssGradient', 'modus_css_gradient');
134
135        include( dirname(__FILE__).'/skins/'.$conf['modus_theme']['skin'].'.inc.php' );
136
137        $template->assign( array(
138                'skin' => $skin,
139                'MODUS_ALBUM_THUMB_SIZE' => intval(@$conf['modus_theme']['album_thumb_size']),
140                'SQUARE_WIDTH' => ImageStdParams::get_by_type(IMG_SQUARE)->max_width(),
141                ));
142}
143
144
145$this->smarty->registerPlugin('function', 'cssResolution', 'modus_css_resolution');
146function modus_css_resolution($params)
147{
148        $base = @$params['base'];
149        $min = @$params['min'];
150        $max = @$params['max'];
151
152        $rules = array();
153        if (!empty($base))
154                $rules[] = $base;
155        foreach(array('min','max') as $type)
156        {
157                if (!empty($$type))
158                        $rules[] = '(-webkit-'.$type.'-device-pixel-ratio:'.$$type.')';
159        }
160        $res = implode(' and ', $rules);
161
162        $rules = array();
163        if (!empty($base))
164                $rules[] = $base;
165        foreach(array('min','max') as $type)
166        {
167                if (!empty($$type))
168                        $rules[] = '('.$type.'-resolution:'.round(96*$$type,1).'dpi)';
169        }
170        $res .= ','.implode(' and ', $rules);
171
172        return $res;
173}
174
175$this->smarty->registerPlugin('function', 'modus_thumbs', 'modus_thumbs');
176function modus_thumbs($x, $smarty)
177{
178        global $template, $page, $conf;
179        if (!isset($_GET['rvts'])) echo '<div id=pop style=display:none;z-index:2;border:0;position:absolute></div>
180';
181
182        $default_params = $smarty->getTemplateVars('derivative_params');
183        $row_height = $default_params->max_height();
184        $device = get_device();
185        $container_margin = 5;
186
187        if ('mobile'==$device)
188        {
189                $horizontal_margin = floor(0.01*$row_height);
190                $container_margin = 0;
191        }
192        elseif ('tablet'==$device)
193                $horizontal_margin = floor(0.015*$row_height);
194        else
195                $horizontal_margin = floor(0.02*$row_height);
196        $vertical_margin = $horizontal_margin+1;
197
198        $candidates = array($default_params);
199        foreach( ImageStdParams::get_defined_type_map() as $params)
200        {
201                if ($params->max_height() > $row_height && $params->sizing->max_crop == $default_params->sizing->max_crop )
202                {
203                        $candidates[] = $params;
204                        if (count($candidates)==3)
205                                break;
206                }
207        }
208
209        $do_pop = 'desktop' == $device;
210
211        $new_icon = " <span class=albSymbol title=\"".l10n('posted on %s')."\">".MODUS_STR_RECENT.'</span>';
212
213        foreach($smarty->getTemplateVars('thumbnails') as $item)
214        {
215                $src_image = $item['src_image'];
216                $new = !empty($item['icon_ts']) ? sprintf($new_icon, format_date($item['date_available'])) : '';
217
218                $idx=0;
219                do {
220                        $cparams = $candidates[$idx];
221                        $c = new DerivativeImage($cparams, $src_image);
222                        $csize = $c->get_size();
223                        $idx++;
224                }
225                while($csize[1]<$row_height-2 && $idx<count($candidates));
226
227                if ($do_pop && $idx<count($candidates))
228                {
229                        $pop = new DerivativeImage($candidates[$idx], $src_image);
230                        $popsize = $pop->get_size();
231                }
232                else
233                {
234                        $pop = $c;
235                        $popsize = $csize;
236                }
237
238                $a_style = '';
239                if ($csize[1] < $row_height)
240                        $a_style=' style="top:'.floor(($row_height-$csize[1])/2).'px"';
241                elseif ($csize[1] > $row_height)
242                {
243                        $csize = $c->get_scaled_size(9999, $row_height);
244                }
245?>
246<li style=width:<?=$csize[0]?>px;height:<?=$row_height?>px><a href="<?=$item['URL']?>"<?=$a_style?>><img src="<?=$c->get_url()?>" width=<?=$csize[0]?> height=<?=$csize[1]?> alt="<?=$item['TN_ALT']?>" data-pop='{"w":<?=$popsize[0]?>,"h":<?=$popsize[1]?>,"url":"<?=$pop->get_url()?>"}'></a><b class=popDesc><b><?=$item['NAME']?></b><?=$new?><br><?=$item['DESCRIPTION']?></b></li>
247<?php
248        }
249
250        $template->block_html_style(null,
251'#thumbnails{text-align:justify;overflow:hidden;margin-left:'.($container_margin-$horizontal_margin).'px;margin-right:'.$container_margin.'px}
252#thumbnails>li{float:left;overflow:hidden;position:relative;margin-bottom:'.$vertical_margin.'px;margin-left:'.$horizontal_margin.'px}#thumbnails>li>a{position:absolute;border:0}');
253        $template->block_footer_script(null, 'rvgtProcessor=new RVGThumbs({hMargin:'.$horizontal_margin.',rowHeight:'.$row_height.'});');
254
255        $my_base_name = basename(dirname(__FILE__));
256        // not async to avoid visible flickering reflow
257        $template->scriptLoader->add('modus.arange', 1, array('jquery'), 'themes/'.$my_base_name."/js/thumb.arrange.min.js", 0);
258        if ($do_pop)
259                $template->scriptLoader->add('modus.pop', 2, array('jquery'), 'themes/'.$my_base_name."/js/thumb.pop.js", 0);
260}
261
262add_event_handler('loc_end_index', 'modus_on_end_index');
263function modus_on_end_index()
264{
265        global $template;
266        if (!pwg_get_session_var('caps'))
267                $template->block_footer_script(null, 'try{document.cookie="caps="+(window.devicePixelRatio?window.devicePixelRatio:1)+"x"+document.documentElement.clientWidth+"x"+document.documentElement.clientHeight+";path='.cookie_path().'"}catch(er){document.cookie="caps=1x1x1x"+err.message;}');
268
269        $req = null;
270        $all = $template->scriptLoader->get_all();
271        if (isset($all['modus.thumb.pop']) || 'desktop' != get_device())
272                return;
273        foreach($all as $script)
274        {
275                if($script->load_mode==2 && !$script->is_remote() && count($script->precedents)==0)
276                {
277                        $req = $script->id;
278                        break;
279                }
280        }
281        if($req!=null)
282        {
283                $my_base_name = basename(dirname(__FILE__));
284    $template->scriptLoader->add('modus.pop', 2, array($req), 'themes/'.$my_base_name."/js/thumb.pop.js", 0);
285        }
286}
287
288add_event_handler('get_index_derivative_params', 'modus_get_index_photo_derivative_params', EVENT_HANDLER_PRIORITY_NEUTRAL+1 );
289function modus_get_index_photo_derivative_params($default)
290{
291        global $conf;
292        if (isset($conf['modus_theme']) && pwg_get_session_var('index_deriv')===null)
293        {
294                $type = $conf['modus_theme']['index_photo_deriv'];
295                if ( $caps=pwg_get_session_var('caps') )
296                {
297                        if ( ($caps[0]>=2 && $caps[1]>=768) /*Ipad3 always has clientWidth 768 independently of orientation*/
298                                || $caps[0]>=3
299                                )
300                                $type = $conf['modus_theme']['index_photo_deriv_hdpi'];
301                }
302                $new = @ImageStdParams::get_by_type($type);
303                if ($new) return $new;
304        }
305        return $default;
306}
307
308add_event_handler('loc_end_index_category_thumbnails', 'modus_index_category_thumbnails' );
309function modus_index_category_thumbnails($items)
310{
311        global $page, $template, $conf;
312
313        if ('categories'!=$page['section'] || !($wh=@$conf['modus_theme']['album_thumb_size']) )
314                return $items;;
315
316        $template->assign('album_thumb_size', $wh);
317
318        $def_params = ImageStdParams::get_custom($wh, $wh, 1, $wh, $wh);
319        foreach( ImageStdParams::get_defined_type_map() as $params)
320        {
321                if ($params->max_height() == $wh)
322                        $alt_params = $params;
323        }
324
325        foreach($items as &$item)
326        {
327                $src_image = $item['representative']['src_image'];
328                $src_size = $src_image->get_size();
329
330                $deriv = null;
331                if (isset($alt_params) && $src_size[0]>=$src_size[1])
332                {
333                        $dsize = $alt_params->compute_final_size($src_size);
334                        if ($dsize[0]>=$wh && $dsize[1]>=$wh)
335                        {
336                                $deriv = new DerivativeImage($alt_params, $src_image);
337                                $rect = new ImageRect($dsize);
338                                $rect->crop_h( $dsize[0]-$wh, $item['representative']['coi'] );
339                                $rect->crop_v( $dsize[1]-$wh, $item['representative']['coi'] );
340                                $l = - $rect->l;
341                                $t = - $rect->t;
342                        }
343                }
344
345                if (!isset($deriv))
346                {
347                        $deriv = new DerivativeImage($def_params, $src_image);
348                        $dsize = $deriv->get_size();
349                        $l = intval($wh-$dsize[0])/2;
350                        $t = intval($wh-$dsize[1])/2;
351                }
352                $item['modus_deriv'] = $deriv;
353
354                if (!empty($item['icon_ts']))
355                        $item['icon_ts']['TITLE'] = time_since($item['max_date_last'], 'month');
356
357                        $styles = array();
358                if ($l<-1 || $l>1)
359                {
360                        //$styles[] = 'left:'.$l.'px';
361                        $styles[] = 'left:'.(100*$l/$wh).'%';
362                }
363                if ($t<-1 || $t>1)
364                        $styles[] = 'top:'.$t.'px';
365                if (count($styles))
366                        $styles = ' style='.implode(';', $styles);
367                else
368                        $styles='';
369                $item['MODUS_STYLE'] = $styles;
370        }
371
372        return $items;
373}
374
375add_event_handler('loc_begin_picture', 'modus_loc_begin_picture');
376function modus_loc_begin_picture()
377{
378        global $conf, $template;
379        if ( isset($_GET['slideshow']) )
380        {
381                $conf['picture_menu'] = false;
382                return;
383        }
384
385        if ( isset($_GET['map']) )
386                return;
387        $template->append('head_elements', '<script>if(document.documentElement.offsetWidth>1270)document.documentElement.className=\'wide\'</script>');
388}
389
390add_event_handler('loc_end_picture', 'modus_loc_end_picture');
391function modus_loc_end_picture()
392{
393        global $template, $picture;
394}
395
396add_event_handler('render_element_content', 'modus_picture_content', EVENT_HANDLER_PRIORITY_NEUTRAL-1, 2 );
397function modus_picture_content($content, $element_info)
398{
399        global $conf, $picture, $template;
400
401        if ( !empty($content) )
402        {// someone hooked us - so we skip;
403                return $content;
404        }
405
406        $unique_derivatives = array();
407        $show_original = isset($element_info['element_url']);
408        $added = array();
409        foreach($element_info['derivatives'] as $type => $derivative)
410        {
411                if ($type==IMG_SQUARE || $type==IMG_THUMB)
412                        continue;
413                if (!array_key_exists($type, ImageStdParams::get_defined_type_map()))
414                        continue;
415                $url = $derivative->get_url();
416                if (isset($added[$url]))
417                        continue;
418                $added[$url] = 1;
419                $show_original &= !($derivative->same_as_source());
420                $unique_derivatives[$type]= $derivative;
421        }
422
423        if (isset($_COOKIE['picture_deriv'])) // ignore persistence
424                setcookie('picture_deriv', false, 0, cookie_path() );
425
426        $selected_derivative = null;
427        if (isset($_COOKIE['phavsz']))
428        {
429                $available_size = explode('x', $_COOKIE['phavsz']);
430                if (empty($available_size[2]))
431                        $available_size[2] = 1;
432
433                foreach($unique_derivatives as $derivative)
434                {
435                        $size = $derivative->get_size();
436                        if (!$size)
437                                break;
438
439                        if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1])
440                        {
441                                $selected_derivative = $derivative;
442                        }
443                        else
444                        {
445                                if ($available_size[2]>1 || !$selected_derivative)
446                                        $selected_derivative = $derivative;
447                                break;
448                        }
449                }
450
451                if ($available_size[2]>1 && $selected_derivative)
452                {
453                        $ratio_w = $size[0] / $available_size[0];
454                        $ratio_h = $size[1] / $available_size[1];
455                        if ($ratio_w>1 || $ratio_h>1)
456                        {
457                                if ($ratio_w > $ratio_h)
458                                        $display_size = array( $available_size[0]/$available_size[2], floor($size[1] / $ratio_w / $available_size[2]) );
459                                else
460                                        $display_size = array( floor($size[0] / $ratio_h / $available_size[2]), $available_size[1]/$available_size[2] );
461                        }
462                        else
463                                $display_size = array( round($size[0]/$available_size[2]), round($size[1]/$available_size[2]) );
464                        $template->assign( array(
465                                        'rvas_display_size' => $display_size,
466                                        'rvas_natural_size' => $size,
467                                ));
468                }
469
470                if (isset($picture['next'])
471                        and $picture['next']['src_image']->is_original())
472                {
473                        $next_best = null;
474                        foreach( $picture['next']['derivatives'] as $derivative)
475                        {
476                                $size = $derivative->get_size();
477                                if (!$size)
478                                        break;
479                                if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1])
480                                        $next_best = $derivative;
481                                else
482                                {
483                                        if ($available_size[2]>1 || !$next_best)
484                                                 $next_best = $derivative;
485                                        break;
486                                }
487                        }
488
489                        if (isset($next_best))
490                                $template->assign('U_PREFETCH', $derivative->get_url() );
491                }
492
493        }
494
495        $as_pending = false;
496        if (!$selected_derivative)
497        {
498                $as_pending = true;
499                $selected_derivative = $element_info['derivatives'][ pwg_get_session_var('picture_deriv',$conf['derivative_default_size']) ];
500        }
501
502
503
504        if ($show_original)
505        {
506                $template->assign( 'U_ORIGINAL', $element_info['element_url'] );
507        }
508
509        $template->append('current', array(
510                        'selected_derivative' => $selected_derivative,
511                        'unique_derivatives' => $unique_derivatives,
512                ), true);
513
514
515        $template->set_filenames(
516                array('default_content'=>'picture_content_asize.tpl')
517                );
518
519        $template->assign( array(
520                        'ALT_IMG' => $element_info['file'],
521                        'COOKIE_PATH' => cookie_path(),
522                        'RVAS_PENDING' => $as_pending,
523                        )
524                );
525        return $template->parse( 'default_content', true);
526}
527
528?>
Note: See TracBrowser for help on using the repository browser.