source: extensions/piwishack/include/class.inc.php @ 16639

Last change on this file since 16639 was 16639, checked in by Gotcha, 12 years ago

Compatibility with Piwigo v2.4
bug:2690

File size: 18.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PiwiShack - a Piwigo Plugin                                           |
4// | Copyright (C) 2009 MOREAU Julien - gotcha@piwigo.org                  |
5// +-----------------------------------------------------------------------+
6// | This program is free software; you can redistribute it and/or modify  |
7// | it under the terms of the GNU General Public License as published by  |
8// | the Free Software Foundation                                          |
9// |                                                                       |
10// | This program is distributed in the hope that it will be useful, but   |
11// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13// | General Public License for more details.                              |
14// |                                                                       |
15// | You should have received a copy of the GNU General Public License     |
16// | along with this program; if not, write to the Free Software           |
17// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18// | USA.                                                                  |
19// +-----------------------------------------------------------------------+
20
21class PiwiShack
22{
23  var $plugin_name, $plugin_path;
24  var $opened, $to_close;
25 
26  function PiwiShack($plugin_name, $plugin_path)
27  {
28    // Args
29    $this->plugin_name = $plugin_name;
30    $this->plugin_path = $plugin_path;
31    // handler
32    $this->initialize_event_handler();
33  }
34
35  function initialize_event_handler()
36  {
37    if (script_basename() != $this->plugin_name.'_controller')
38    {
39      add_event_handler('loc_end_page_header', array(&$this, 'loc_end_page_header'));
40      add_event_handler('loc_end_index', array(&$this, 'loc_end_index'));
41      add_event_handler('picture_pictures_data', array(&$this, 'picture_pictures_data'));
42    }
43
44    if (isset($_GET[$this->plugin_name]))
45    {
46      add_event_handler('loc_begin_page_header', array(&$this, 'loc_begin_page_header'));
47      pwg_set_session_var($this->plugin_name, ($_GET[$this->plugin_name] === 'open'));
48    }
49    $this->opened = pwg_get_session_var($this->plugin_name, false);
50    $this->to_close = (isset($_GET[$this->plugin_name]) and ($_GET[$this->plugin_name] === 'close'));
51  }
52 
53  function get_thumb_post_on_a_website()
54  {
55    global $page, $lang_info;
56
57    $list = array();
58 
59    $S = '';
60
61    switch (script_basename())
62    {
63      case 'index':
64      {
65        global $pictures;
66
67        if (isset($pictures))
68        {
69          $list = $pictures;
70        }
71        break;
72      }
73      case 'picture':
74      {
75        global $picture;
76
77        if (isset($picture['current']))
78        {
79          $list[] = $picture['current'];
80        }
81        break;
82      }
83    }
84
85    if (empty($list) and !empty($page['items']))
86    {
87          $rank_of = array_flip($page['items']);
88      $query = '
89          SELECT *
90      FROM '.IMAGES_TABLE.'
91      WHERE id IN ('.implode(',', $page['items']).')
92          ;';
93
94      $result = pwg_query($query);
95
96      while ($row = mysql_fetch_assoc($result))
97      {
98        $row['rank'] = $rank_of[ $row['id'] ];
99        array_push($list, $row);
100      }
101
102      usort($list, 'rank_compare');
103  }
104    if (!empty($list))
105    {
106
107      set_make_full_url();
108
109      foreach ($list as $row)
110      {
111           /*
112           Affichage de la miniature sur des sites. Cliquable.
113           EXEMPLE
114           <a href="{$ROOT_WAY}{$current.U_IMG}" target=_blank><img src="{$ROOT_WAY}{$current.THUMB_SRC|@replace:'./':''}" /></a>
115           */
116            $S .= '<a href=\"'.
117          duplicate_picture_url(
118            array(
119              'image_id' => $row['id'],
120              'image_file' => $row['file'],
121              )).'\" target=_blank><img src=\"'.
122                  str_replace('/./', '/', DerivativeImage::thumb_url($row)).'\" /></a>';
123            $S .= '\n\n';
124      }
125
126      unset_make_full_url();
127    }
128
129    return $S;
130  }
131 
132  function get_thumb_post_on_a_forum()
133  {
134    global $page, $lang_info;
135
136    $list = array();
137 
138    $S = '';
139
140    switch (script_basename())
141    {
142      case 'index':
143      {
144        global $pictures;
145
146        if (isset($pictures))
147        {
148          $list = $pictures;
149        }
150        break;
151      }
152      case 'picture':
153      {
154        global $picture;
155
156        if (isset($picture['current']))
157        {
158          $list[] = $picture['current'];
159        }
160        break;
161      }
162    }
163
164    if (empty($list) and !empty($page['items']))
165    {
166          $rank_of = array_flip($page['items']);
167      $query = '
168          SELECT *
169      FROM '.IMAGES_TABLE.'
170      WHERE id IN ('.implode(',', $page['items']).')
171          ;';
172
173      $result = pwg_query($query);
174
175      while ($row = mysql_fetch_assoc($result))
176      {
177        $row['rank'] = $rank_of[ $row['id'] ];
178        array_push($list, $row);
179      }
180
181      usort($list, 'rank_compare');
182  }
183    if (!empty($list))
184    {
185
186      set_make_full_url();
187
188      foreach ($list as $row)
189      {
190           /*
191           Affichage de la miniature sur des forums. Cliquable.
192           EXEMPLE
193           [url={$ROOT_WAY}{$current.U_IMG}][img]{$ROOT_WAY}{$current.THUMB_SRC|@replace:'./':''}[/img][/url]
194           */
195            $S .= '[url='.
196          duplicate_picture_url(
197            array(
198              'image_id' => $row['id'],
199              'image_file' => $row['file'],
200              )).'][img]'.
201                  str_replace('/./', '/', DerivativeImage::thumb_url($row)).'[/img][/url]';
202            $S .= '\n\n';
203      }
204
205      unset_make_full_url();
206    }
207
208    return $S;
209  }
210 
211  function get_view_post_on_a_website()
212  {
213    global $page, $lang_info;
214
215    $list = array();
216 
217    $S = '';
218
219    switch (script_basename())
220    {
221      case 'index':
222      {
223        global $pictures;
224
225        if (isset($pictures))
226        {
227          $list = $pictures;
228        }
229        break;
230      }
231      case 'picture':
232      {
233        global $picture;
234
235        if (isset($picture['current']))
236        {
237          $list[] = $picture['current'];
238        }
239        break;
240      }
241    }
242
243    if (empty($list) and !empty($page['items']))
244    {
245          $rank_of = array_flip($page['items']);
246      $query = '
247          SELECT *
248      FROM '.IMAGES_TABLE.'
249      WHERE id IN ('.implode(',', $page['items']).')
250          ;';
251
252      $result = pwg_query($query);
253
254      while ($row = mysql_fetch_assoc($result))
255      {
256        $row['rank'] = $rank_of[ $row['id'] ];
257        array_push($list, $row);
258      }
259
260      usort($list, 'rank_compare');
261  }
262    if (!empty($list))
263    {
264          include_once(PHPWG_ROOT_PATH.'/include/functions_picture.inc.php');
265      set_make_full_url();
266
267      foreach ($list as $row)
268      {
269           /*
270           Affichage de la vue normale sur des sites. Non cliquable.
271           EXEMPLE
272           <a href="{$ROOT_WAY}{$current.U_IMG}" target=_blank><img src="{$ROOT_WAY}{$SRC_IMG|@replace:'./':''}" /></a>
273           */
274           $src_image = new SrcImage($row);
275           if (strpos(  $image_url = DerivativeImage::url(IMG_MEDIUM, $src_image), get_absolute_root_url()  )  === false) {
276                 $abs_root_img= get_absolute_root_url(). str_replace('./', '', $image_url = DerivativeImage::url(IMG_MEDIUM, $src_image)) ;
277           } else {
278           $abs_root_img= DerivativeImage::url(IMG_MEDIUM, $src_image) ;
279           }       
280            $S .= '<a href=\"'.
281          duplicate_picture_url(
282            array(
283              'image_id' => $row['id'],
284              'image_file' => $row['file'],
285              )).'\" target=_blank><img src=\"'.$abs_root_img.'\" /></a>';
286                $S .= '\n\n';
287      }
288
289      unset_make_full_url();
290    }
291
292    return $S;
293  }
294 
295  function get_view_post_on_a_forum()
296  {
297    global $page, $lang_info;
298
299    $list = array();
300 
301    $S = '';
302
303    switch (script_basename())
304    {
305      case 'index':
306      {
307        global $pictures;
308
309        if (isset($pictures))
310        {
311          $list = $pictures;
312        }
313        break;
314      }
315      case 'picture':
316      {
317        global $picture;
318
319        if (isset($picture['current']))
320        {
321          $list[] = $picture['current'];
322        }
323        break;
324      }
325    }
326
327    if (empty($list) and !empty($page['items']))
328    {
329          $rank_of = array_flip($page['items']);
330      $query = '
331          SELECT *
332      FROM '.IMAGES_TABLE.'
333      WHERE id IN ('.implode(',', $page['items']).')
334          ;';
335
336      $result = pwg_query($query);
337
338      while ($row = mysql_fetch_assoc($result))
339      {
340        $row['rank'] = $rank_of[ $row['id'] ];
341        array_push($list, $row);
342      }
343
344      usort($list, 'rank_compare');
345  }
346      if (!empty($list))
347    {
348          include_once(PHPWG_ROOT_PATH.'/include/functions_picture.inc.php');
349      set_make_full_url();
350
351      foreach ($list as $row)
352      {
353           /*
354           Affichage de la vue normale sur des forums.
355           EXEMPLE
356           [url={$ROOT_WAY}{$current.U_IMG}][img]{$ROOT_WAY}{$SRC_IMG|@replace:'./':''}[/img][/url]
357           */
358           $src_image = new SrcImage($row);
359           if (strpos(  $image_url = DerivativeImage::url(IMG_MEDIUM, $src_image), get_absolute_root_url()  )  === false) {
360                 $abs_root_img= get_absolute_root_url(). str_replace('./', '', $image_url = DerivativeImage::url(IMG_MEDIUM, $src_image)) ;
361           } else {
362       $abs_root_img= DerivativeImage::url(IMG_MEDIUM, $src_image) ;
363           }
364            $S .= '[url='.
365          duplicate_picture_url(
366            array(
367              'image_id' => $row['id'],
368              'image_file' => $row['file'],
369              )).'][img]'.$abs_root_img.'[/img][/url]';
370            $S .= '\n\n';
371      }
372
373      unset_make_full_url();
374    }
375
376    return $S;
377  }
378 
379  function get_view_direct_link()
380  {
381    global $page, $lang_info;
382
383    $list = array();
384 
385    $S = '';
386
387    switch (script_basename())
388    {
389      case 'index':
390      {
391        global $pictures;
392
393        if (isset($pictures))
394        {
395          $list = $pictures;
396        }
397        break;
398      }
399      case 'picture':
400      {
401        global $picture;
402
403        if (isset($picture['current']))
404        {
405          $list[] = $picture['current'];
406        }
407        break;
408      }
409    }
410
411    if (empty($list) and !empty($page['items']))
412    {
413          $rank_of = array_flip($page['items']);
414      $query = '
415          SELECT *
416      FROM '.IMAGES_TABLE.'
417      WHERE id IN ('.implode(',', $page['items']).')
418          ;';
419
420      $result = pwg_query($query);
421
422      while ($row = mysql_fetch_assoc($result))
423      {
424        $row['rank'] = $rank_of[ $row['id'] ];
425        array_push($list, $row);
426      }
427
428      usort($list, 'rank_compare');
429  }
430    if (!empty($list))
431    {
432          include_once(PHPWG_ROOT_PATH.'/include/functions_picture.inc.php');
433      set_make_full_url();
434         
435          $page_name = script_basename();
436        foreach ($list as $row)
437        {
438             /*
439             Affichage du chemin en clair du lien vers la vue normale.
440             EXEMPLE
441             {$ROOT_WAY}{$SRC_IMG|@replace:'./':''}
442             */
443                  $src_image = new SrcImage($row);
444              $S .= $image_url = DerivativeImage::url(IMG_MEDIUM, $src_image);
445                  $S .= '\n\n';
446        }
447
448      unset_make_full_url();
449      }
450
451    return $S;
452  }
453 
454  function get_p_perso_writer_01()
455  {
456    global $page, $lang_info;
457
458    $list = array();
459 
460    $S = '';
461
462    switch (script_basename())
463    {
464      case 'index':
465      {
466        global $pictures;
467
468        if (isset($pictures))
469        {
470          $list = $pictures;
471        }
472        break;
473      }
474      case 'picture':
475      {
476        global $picture;
477
478        if (isset($picture['current']))
479        {
480          $list[] = $picture['current'];
481        }
482        break;
483      }
484    }
485
486    if (empty($list) and !empty($page['items']))
487    {
488          $rank_of = array_flip($page['items']);
489      $query = '
490          SELECT *
491      FROM '.IMAGES_TABLE.'
492      WHERE id IN ('.implode(',', $page['items']).')
493          ;';
494
495      $result = pwg_query($query);
496
497      while ($row = mysql_fetch_assoc($result))
498      {
499        $row['rank'] = $rank_of[ $row['id'] ];
500        array_push($list, $row);
501      }
502
503      usort($list, 'rank_compare');
504  }
505    if (!empty($list))
506    {
507          include_once(PHPWG_ROOT_PATH.'/include/functions_picture.inc.php');
508      set_make_full_url();
509
510      foreach ($list as $row)
511      {
512           /*
513           Affichage Personnalisé, 01.
514           EXEMPLE
515           <h6 style="text-align: center;"><a href="{$ROOT_WAY}{$current.U_IMG}" target="_blank"><img class="aligncenter" style="width: 100%; height: 100%;" src="{$ROOT_WAY}{$SRC_IMG|@replace:'./':''}" alt="{$ALT_IMG}" /></a><em><a href="{$ROOT_WAY}{$U_UP}"; target="_blank">Galerie complète à visiter</a></em></h6>
516           */
517           
518            $src_image = new SrcImage($row);
519                $S .= '<h6 style=\"text-align: center;\"><a href=\"'. // Arguments à transmettre
520          duplicate_picture_url( // Lien Piwigo vers la page de la vue normale
521            array(
522              'image_id' => $row['id'],
523              'image_file' => $row['file'],
524              )).
525                  '\" target=\"_blank\"><img class=\"aligncenter\" style=\"width: 100%; height: 100%;\" src=\"'. // Arguments à transmettre
526                  get_absolute_root_url().str_replace('./', '', $image_url = DerivativeImage::url(IMG_MEDIUM, $src_image)). // Liens direct vers la vue normale
527                  '\" alt=\"'. // Arguments à transmettre
528                  $row['file']. // Noms du fichier
529                  '\" /></a><em><a href=\"'. // Arguments à transmettre
530                  duplicate_index_url(). // Catégorie parente
531                  '\"; target=\"_blank\"><br />Galerie complète à visiter</a></em></h6>'; // Arguments à transmettre
532            $S .= '\n\n';
533      }
534
535      unset_make_full_url();
536    }
537
538    return $S;
539  }
540 
541  function get_p_perso_writer_02()
542  {
543    global $page, $lang_info;
544
545    $list = array();
546 
547    $S = '';
548
549    switch (script_basename())
550    {
551      case 'index':
552      {
553        global $pictures;
554
555        if (isset($pictures))
556        {
557          $list = $pictures;
558        }
559        break;
560      }
561      case 'picture':
562      {
563        global $picture;
564
565        if (isset($picture['current']))
566        {
567          $list[] = $picture['current'];
568        }
569        break;
570      }
571    }
572
573    if (empty($list) and !empty($page['items']))
574    {
575          $rank_of = array_flip($page['items']);
576      $query = '
577          SELECT *
578      FROM '.IMAGES_TABLE.'
579      WHERE id IN ('.implode(',', $page['items']).')
580          ;';
581
582      $result = pwg_query($query);
583
584      while ($row = mysql_fetch_assoc($result))
585      {
586        $row['rank'] = $rank_of[ $row['id'] ];
587        array_push($list, $row);
588      }
589
590      usort($list, 'rank_compare');
591  }
592
593    if (!empty($list))
594    {
595          include_once(PHPWG_ROOT_PATH.'/include/functions_url.inc.php');
596      set_make_full_url();
597
598      foreach ($list as $row)
599      {
600       //'[(('.$picture['current']['thumbnail'].'))|'.$picture['current']['url'].'|'.$lang_info['code'].']';
601        $S  .= '*** '.(!empty($row['name']) ? $row['name'] : $row['file']).' ***\n';
602        $S  .= '[(('.
603          str_replace('/./', '/', DerivativeImage::thumb_url($row)).'))|'.
604          duplicate_picture_url(
605            array(
606              'image_id' => $row['id'],
607              'image_file' => $row['file'],
608              )).'|'.
609          $lang_info['code'].']';
610            $S .= '\n\n';
611      }
612
613      unset_make_full_url();
614
615    }
616
617    return $S;
618  }
619 
620  // Fin de la définition des champs
621
622  function loc_begin_page_header()
623  {
624    global $template;
625
626    $redirect_url = ( script_basename()=='picture'
627        ? duplicate_picture_url(array(), array($this->plugin_name))
628          : duplicate_index_url(array(), array($this->plugin_name))
629      );
630
631     $template->assign(
632        array(
633          'page_refresh' => array(
634                'TIME' => 1,
635                'U_REFRESH' => $redirect_url
636              )
637          ));
638  }
639
640  function loc_end_page_header()
641  {
642    global $template;
643
644    if ($this->opened or $this->to_close)
645    {
646      $plugin_root_url = get_root_url().'plugins/'.$this->plugin_name.'/';
647
648      $js = '
649  <script type="text/javascript">
650    var theController = window.open("", "'.$this->plugin_name.'_controller", "alwaysRaised=yes,dependent=yes,toolbar=no,height=807,width=1220,menubar=no,resizable=yes,scrollbars=yes,status=no");';
651
652    if ($this->to_close)
653    {
654      $js .= '
655    theController.close();';
656    }
657    else
658    {
659      $js .= '
660    if (theController.location.toString()=="about:blank" || !theController.location.toString().match(/^(https?.*\/)'.$this->plugin_name.'_controller\.php(\?.+)?$/))
661    {
662      theController.location = "'.$plugin_root_url.$this->plugin_name.'_controller.php";
663    }
664    else
665    {
666      theController.focus();
667    }
668    theController.document.getElementsByName("thumb_Post_on_a_website")[0].value = "'.$this->get_thumb_post_on_a_website().'";
669    theController.document.getElementsByName("thumb_Post_on_a_forum")[0].value = "'.$this->get_thumb_post_on_a_forum().'";
670        theController.document.getElementsByName("view_Post_on_a_website")[0].value = "'.$this->get_view_post_on_a_website().'";
671        theController.document.getElementsByName("view_Post_on_a_forum")[0].value = "'.$this->get_view_post_on_a_forum().'";
672        theController.document.getElementsByName("view_Direct_link")[0].value = "'.$this->get_view_direct_link().'";
673    theController.document.getElementsByName("p_Perso_writer_01")[0].value = "'.$this->get_p_perso_writer_01().'";
674    theController.document.getElementsByName("p_Perso_writer_02")[0].value = "'.$this->get_p_perso_writer_02().'";';
675    }
676      $js .= '
677  </script>';
678
679      $template->append('head_elements', $js);
680    }
681  }
682
683  function get_link_icon($main_link)
684  {
685    global $page;
686
687    if (empty($page['items']))
688    {
689        return;
690    }
691
692    $this->loading_lang();
693
694    if ($this->opened)
695    {
696      $suffix_url = 'close';
697      $link_title = l10n('close_window_piwishack');
698    }
699    else
700    {
701      $suffix_url = 'open';
702      $link_title = l10n('open_window_piwishack');
703    }
704
705    $link_url = add_url_params($main_link, array($this->plugin_name => $suffix_url));
706    if (!empty($link_url))
707    {
708      $link_url = 
709        '<a href="'.$link_url.'" title="'.$link_title.'" rel="nofollow"><img src="'.get_root_url().'plugins/'.$this->plugin_name.'/icon/controller_'.$suffix_url.'.png" class="button" alt="PiwiShack controller"></a>';
710    }
711
712    return $link_url;
713  }
714
715  function loc_end_index()
716  {
717    global $template;
718
719    $link_url = $this->get_link_icon(duplicate_index_url());
720    if (!empty($link_url))
721    {
722      $template->concat(
723        'PLUGIN_INDEX_ACTIONS',
724        '<li>'.$link_url.'</li>');
725    }
726  }
727
728  function picture_pictures_data($pictures)
729  {
730    global $template;
731
732    $link_url = $this->get_link_icon(duplicate_picture_url());
733    if (!empty($link_url))
734    {
735      $template->concat(
736        'PLUGIN_PICTURE_ACTIONS',
737        $link_url);
738    }
739
740    return $pictures;
741  }
742
743  function loading_lang()
744  {
745    load_language('plugin.lang', $this->plugin_path);
746  }
747}
748
749?>
Note: See TracBrowser for help on using the repository browser.