source: extensions/piwishack/class.inc.php @ 27153

Last change on this file since 27153 was 23724, checked in by gil, 11 years ago
File size: 24.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  var $config;
26  var $list;
27 
28  /**************************************************************************
29   *  function: PiwiShack
30   **************************************************************************
31   *  Rôle:
32   *    Constructeur; charge la config et initialise les handlers
33   *************************************************************************/
34  function PiwiShack($plugin_name, $plugin_path)
35  {
36    // Args
37    $this->plugin_name = $plugin_name;
38    $this->plugin_path = $plugin_path;
39
40    $this->load_config();
41
42    // handler
43    $this->initialize_event_handler();
44
45  }
46
47  /**************************************************************************
48   *  function: initialize_event_handler
49   **************************************************************************
50   *  Rôle:
51   *    Ajout des handlers nécessaires aux pages index et picture
52   *    si les droits l'autorisent.   
53   *************************************************************************/
54  function initialize_event_handler()
55  {
56 
57    if ((($this->config['URLShackRight'] == "admin") && (is_admin()))
58     || (($this->config['URLShackRight'] == "member") && (!is_a_guest()))
59     ||  ($this->config['URLShackRight'] == "all"))
60    {
61      if (script_basename() != $this->plugin_name.'_controller')
62      {
63        add_event_handler('loc_end_index', array(&$this, 'handler_index'));
64        add_event_handler('picture_pictures_data', array(&$this, 'handler_picture'));
65      }
66    }
67  }
68
69  /**************************************************************************
70   *  function: loading_lang
71   **************************************************************************
72   *  Rôle:
73   *    Chargement des données de langue
74   *************************************************************************/
75  function loading_lang()
76  {
77    load_language('plugin.lang', $this->plugin_path);
78  }
79
80  /**************************************************************************
81   *  function: get_link_icon
82   **************************************************************************
83   *  in:
84   *  out:
85   *    code de l'action   
86   **************************************************************************
87   *  Rôle:
88   *    Retourne le code de l'action d'ouverture piwishack
89   *************************************************************************/
90  function get_link_icon($main_link)
91  {
92    global $page;
93
94    if (empty($page['items']))
95    {
96        return;
97    }
98
99    $this->loading_lang();
100    $link_title = l10n('open_window_piwishack');
101    $link_url = '<img id="urlshackicon" title="'.$link_title.'" onclick="urlshack_switch();" src="'.get_root_url().'plugins/piwishack/icon/controller_open.png" class="button" alt="URLShack links popup">';
102
103    return $link_url;
104  }
105
106  /**************************************************************************
107   *  function: handler_index
108   **************************************************************************
109   *  in:
110   *  out:
111   *    Code source   
112   **************************************************************************
113   *  Rôle:
114   *    Handler dans le cas des pages d'index : ajoute le code javascript,
115   *     le code html du popup et l'icone d'ouverture piwishack.
116   *************************************************************************/
117  function handler_index()
118  {
119    global $template;
120
121    $link_url = $this->get_link_icon(duplicate_index_url());
122    if (!empty($link_url))
123    {
124      $mystyle = '<link rel="stylesheet" type="text/css" href="' . get_root_url() . 'plugins/piwishack/style.css">';
125      $template->block_html_head(null, $mystyle);
126
127      // get button javascript code and add to page
128      $code = $this->get_js_button_code();
129      $template->concat('PLUGIN_INDEX_CONTENT_AFTER', $code);
130   
131      // Add piwishack button in the list of action icons
132      $template->concat('PLUGIN_INDEX_ACTIONS', '<li>'.$link_url.'</li>');
133   
134      // get popup html code and add to page
135      $popup = $this->get_html_popup_code("index");
136      $template->concat('PLUGIN_INDEX_CONTENT_AFTER', $popup);
137    }
138  }
139
140  /**************************************************************************
141   *  function: handler_picture
142   **************************************************************************
143   *  in:
144   *  out:
145   *    Code source   
146   **************************************************************************
147   *  Rôle:
148   *    Handler dans le cas des pages images : ajoute le code javascript,
149   *     le code html du popup et l'icone d'ouverture piwishack.   
150   *************************************************************************/
151  function handler_picture($pictures)
152  {
153    global $template;
154
155    $link_url = $this->get_link_icon(duplicate_index_url());
156    if (!empty($link_url))
157    {
158      $mystyle = '<link rel="stylesheet" type="text/css" href="' . get_root_url() . 'plugins/piwishack/style.css">';
159      $template->block_html_head(null, $mystyle);
160
161      // get button javascript code and add to page
162      $code = $this->get_js_button_code();
163      $template->concat('PLUGIN_PICTURE_AFTER', $code);
164   
165      // Add piwishack button in the list of action icons
166      $template->concat('PLUGIN_PICTURE_ACTIONS', $link_url);
167   
168      // get popup html code and add to page
169      $popup = $this->get_html_popup_code("picture");
170      $template->concat('PLUGIN_PICTURE_AFTER', $popup);
171    }
172    return $pictures;   
173  }
174
175  /**************************************************************************
176   *  function: get_js_button_code
177   **************************************************************************
178   *  in:
179   *  out:
180   *    Code source   
181   **************************************************************************
182   *  Rôle:
183   *    Genere le code javascript de la page
184   *************************************************************************/
185  function get_js_button_code()
186  {
187    $this->loading_lang();
188    $link_open = l10n('open_window_piwishack');
189    $link_close = l10n('close_window_piwishack');
190
191    $icon_open = get_root_url() . 'plugins/piwishack/icon/controller_open.png';
192    $icon_close = get_root_url() . 'plugins/piwishack/icon/controller_close.png'; 
193    $code = '
194<script type="text/javascript">
195  function urlshack_switch() {
196    if (document.getElementById("urlshackpopup").style.visibility != "visible")
197      urlshack_open();
198    else
199      urlshack_close();
200  }
201  function urlshack_open() {
202    document.getElementById("urlshackpopup").style.visibility = "visible";
203    document.getElementById("urlshackicon").src = "' . $icon_close . '";
204    document.getElementById("urlshackicon").title = "' . $link_close . '";
205    return false;
206  }   
207  function urlshack_close() {
208    document.getElementById("urlshackpopup").style.visibility = "hidden";
209    document.getElementById("urlshackicon").src = "' . $icon_open . '";
210    document.getElementById("urlshackicon").title = "' . $link_open . '";
211    return false;
212  }
213  function urlshack_html() {
214    document.getElementById("bbcode").style.display = "none";
215    document.getElementById("custom").style.display = "none";
216    document.getElementById("html").style.display = "block";
217    document.getElementById("tab-bbcode").setAttribute("class", "tab");
218    document.getElementById("tab-custom").setAttribute("class", "tab");
219    document.getElementById("tab-html").setAttribute("class", "tabselected");
220    return false;
221  }   
222  function urlshack_bbcode() {
223    document.getElementById("html").style.display = "none";
224    document.getElementById("custom").style.display = "none";
225    document.getElementById("bbcode").style.display = "block";
226    document.getElementById("tab-bbcode").setAttribute("class", "tabselected");
227    document.getElementById("tab-custom").setAttribute("class", "tab");
228    document.getElementById("tab-html").setAttribute("class", "tab");
229    return false;
230  }   
231  function urlshack_custom() {
232    document.getElementById("html").style.display = "none";
233    document.getElementById("bbcode").style.display = "none";
234    document.getElementById("custom").style.display = "block";
235    document.getElementById("tab-bbcode").setAttribute("class", "tab");
236    document.getElementById("tab-custom").setAttribute("class", "tabselected");
237    document.getElementById("tab-html").setAttribute("class", "tab");
238    return false;
239  }   
240   
241</script>
242        ';
243    return $code;
244  }
245
246  /**************************************************************************
247   *  function: get_html_popup_code
248   **************************************************************************
249   *  in:
250   *    $pagetype (index ou picture)   
251   *  out:
252   *    Code source   
253   **************************************************************************
254   *  Rôle:
255   *    Genere le code html complet du popup piwishack
256   *************************************************************************/
257  function get_html_popup_code($pagetype)
258  {
259    $prefix = 0;
260
261    $icon_close = get_root_url() . 'plugins/piwishack/icon/controller_close.png';
262    $icon_open = get_root_url() . 'plugins/piwishack/icon/controller_open.png';
263    $this->get_data();
264    $listdisp = $this->config['Display'];
265    asort($listdisp);
266    $namedisp = array_keys($listdisp); 
267
268    $this->loading_lang();
269
270    $popup  = "<div id='urlshackpopup' class='piwishack' >\n";
271    $popup .= "<div class='header'><img src='" .$icon_open . "'> &nbsp; Piwishack</div>\n";
272    $popup .= "<div class='area'>\n";
273    $popup .= "<h1>" . l10n('Partage_me') . "</h1>\n";
274    $popup .= "<form>\n";
275    $popup .= "<span onclick='urlshack_html();' id='tab-html' class='" . ($this->config['DefaultHtmlBBcode'] == 'html' ? "tabselected" : "tab") . "'>\n";
276    $popup .= l10n('sheet_html') . "</span>\n";
277    $popup .= "<span onclick='urlshack_bbcode();' id='tab-bbcode' class='" . ($this->config['DefaultHtmlBBcode'] == 'bbcode' ? "tabselected" : "tab") . "'>\n";
278    $popup .= l10n('sheet_bbcode') . "</span>\n";
279    $popup .= "<span onclick='urlshack_custom();' id='tab-custom' class='" . ($this->config['DefaultHtmlBBcode'] == 'custom' ? "tabselected" : "tab") . "'>\n";
280    $popup .= l10n('sheet_custom') . "</span>\n";
281    $popup .= "</form>\n";
282
283    // A block is inserted for each format, bbcode and html
284    //  but displayed only for the selected (radio) format
285    foreach (array("html", "bbcode") as $format)
286    {
287      $popup.= "<div id='" . $format . "' class='" . ($this->config['DefaultHtmlBBcode'] == $format ? "sheetselected" : "sheet") . "'>\n";
288      foreach ($namedisp as $i) {
289        if ($listdisp[$i] != 0) {
290          // Compute code length
291          $current_code = $this->get_codes($i, $format, $prefix);
292          $nb_cols = 100;
293          $nb_rows = min(1 + (strlen($current_code) / $nb_cols), 5);
294
295          $popup .= "<form>\n";
296          $popup .= "<h2>". l10n('post_code_' .$i) . " : </h2>";
297          $popup .= '<textarea name="' . $i . '" cols="' . $nb_cols . '" rows="' . $nb_rows .'" onclick="select()">';
298          if (($this->config['ImageNameBeforeCode'] === "all")
299           || (($this->config['ImageNameBeforeCode'] === "list") && ($pagetype === "index")))
300          {
301             $prefix = 1;
302          }
303          $popup .= $current_code;
304          $popup .= '</textarea>';
305          $popup .= "<img src='plugins/piwishack/icon/$i.png' alt='$i' width='200' />";
306          $popup .= "</form>\n";
307        }
308      }
309      $popup .= "</div>\n";
310    }
311
312    $nb = count($this->config['ExtendedDisplay']);
313    $popup .= "<div id='custom' class='" . ($this->config['DefaultHtmlBBcode'] == "custom" ? "sheetselected" : "sheet") . "'>\n";
314    for ($i = 0; $i < $nb; $i++) {
315      if ($this->config['ExtendedDisplay'][$i] != "") {
316        // Compute code length
317        $current_code = $this->get_list_pattern_result($this->config['ExtendedDisplay'][$i]);
318        $nb_cols = 125;
319        $nb_rows = min(1 + (strlen($current_code) / $nb_cols), 5);
320
321        $popup .= "<form>\n";
322        $popup .= "<h2>" . l10n('custom_code') . ($i+1) . " - " . $this->config['ExtendedLabel'][$i] . " </h2>";
323        $popup .= '<textarea class="commentContent" name="ExtendedDisplay' . $i . '" cols="' . $nb_cols . '" rows="' . $nb_rows .'" onclick="select()">';
324        $popup .= $current_code;
325        $popup .= '</textarea>';
326        $popup .= "</form>\n";
327      }
328    }
329    $popup .= "</div>\n";
330 
331    $popup .= "</div>\n";
332    $popup .= "<div class='footer'>";
333    $popup .= '<button onclick="urlshack_close();"><img src="' .$icon_close . '"><span class="txtbutton">' . l10n('close') . '</span><img src="' .$icon_close . '"></button>';
334    $popup .= "</div>\n";
335    $popup .= "</div>\n";
336    return $popup;
337  }
338
339  /**************************************************************************
340   *  function: get_data
341   **************************************************************************
342   *  in:
343   *  out:
344   **************************************************************************
345   *  Rôle:
346   *    Remplit le tableau d'images en fonction du contexte
347   *************************************************************************/
348  function get_data()
349  {
350    global $page;
351    $this->list = array();
352    $S = '';
353
354    switch (script_basename())
355    {
356      case 'index':
357      {
358        global $pictures;
359        if (isset($pictures))
360        {
361          $this->list = $pictures;
362        }
363        break;
364      }
365      case 'picture':
366      {
367        global $picture;
368        if (isset($picture['current']))
369        {
370          $this->list[] = $picture['current'];
371        }
372        break;
373      }
374    }
375
376    if (empty($this->list) and !empty($page['items']))
377    {
378          $rank_of = array_flip($page['items']);
379      $query = '
380          SELECT *
381      FROM '.IMAGES_TABLE.'
382      WHERE id IN ('.implode(',', $page['items']).')
383          ;';
384
385      $result = pwg_query($query);
386
387      while ($row = pwg_db_fetch_assoc($result))
388      {
389        $row['rank'] = $rank_of[ $row['id'] ];
390        array_push($this->list, $row);
391      }
392
393      usort($this->list, 'rank_compare');
394    }
395  }
396
397  /**************************************************************************
398   *  function: get_codes
399   **************************************************************************
400   *  in:
401   *    $type: type du code
402   *    $format: html ou bbcode
403   *    $prefix : indique si le nom doit précéder le code     
404   *  out:
405   *    Code du type
406   **************************************************************************
407   *  Rôle:
408   *    Retourne le code (pattern) correspondant au type fourni
409   *    en le faisant précéder éventuellement du nom de l'image
410   *************************************************************************/
411  function get_codes($type, $format, $prefix)
412  {
413    $pattern = '';
414   
415    $target = $this->config['WebLinkOpenOnOtherWindow'] == "other" ? " target=_blank" : "";
416     
417    if ($type === 'Thumbnail') {
418      if ($format === 'html')   { $pattern = "<img src=\"##t\" />"; }
419      if ($format === 'bbcode') { $pattern = "[img]##t[/img]"; }
420    }
421    else if ($type === 'ThumbnailToImage') {
422      if ($format === 'html')   { $pattern = "<a href=\"##i\"". $target . "><img src=\"##t\" /></a>"; }
423      if ($format === 'bbcode') { $pattern = "[url=##i][img]##t[/img][/url]"; }
424    }
425    else if ($type === 'ThumbnailToGallery') {
426      if ($format === 'html')   { $pattern = "<a href=\"##g\"". $target . "><img src=\"##t\" /></a>"; }
427      if ($format === 'bbcode') { $pattern = "[url=##g][img]##t[/img][/url]"; }
428    }
429    else if ($type === 'Image') {
430      if ($format === 'html')   { $pattern = "<img src=\"##t\" />"; }
431      if ($format === 'bbcode') { $pattern = "[img]##i[/img]"; }
432    }
433    else if ($type === 'ImageToGallery') {
434      if ($format === 'html')   { $pattern = "<a href=\"##g\"". $target . "><img src=\"##i\" /></a>"; }
435      if ($format === 'bbcode') { $pattern = "[url=##g][img]##i[/img][/url]"; }
436    }
437    else if ($type === 'NameToImage') {
438      if ($format === 'html')   { $pattern = "<a href=\"##i\"". $target . ">##n</a>"; }
439      if ($format === 'bbcode') { $pattern = "[url=##i]##n[/url]"; }
440    }
441    else if ($type === 'NameToGallery') {
442      if ($format === 'html')   { $pattern = "<a href=\"##g\"". $target . ">##n</a>"; }
443      if ($format === 'bbcode') { $pattern = "[url=##g]##n[/url]"; }
444    }
445    if ($prefix === 1) {
446      // TO_DO : n'ajouter que si ##n n'est pas déjà dans la chaine ?
447      $pattern = "##n: " . $pattern;
448    }
449    return $this->get_list_pattern_result($pattern);
450  }
451
452  /**************************************************************************
453   *  function: get_list_pattern_result
454   **************************************************************************
455   *  in:
456   *    $pattern
457   *  out:
458   *    Le code à afficher
459   **************************************************************************
460   *  Rôle:
461   *    Retourne la chaine à afficher pour le pattern fourni et pour
462   *       l'ensemble des images du contexte courant
463   *************************************************************************/
464  function get_list_pattern_result($pattern)
465  {
466    global $page, $lang_info, $conf;
467    // force $conf['derivative_url_style'] to 2 (script) to make sure
468    // exported HTML/BBCode will use i.php?/upload and not _data/i/upload
469    // because you don't know when the cache will be flushed
470    $previous_derivative_url_style = $conf['derivative_url_style'];
471    $conf['derivative_url_style'] = 2;
472    $S = '';
473    if (!empty($this->list))
474    {
475      set_make_full_url();
476      foreach ($this->list as $row)
477      {
478         $S .= $this->get_pattern_result($row, $pattern) . "\n";
479      }
480      unset_make_full_url();
481    }
482   
483    // restore configuration setting
484    $conf['derivative_url_style'] = $previous_derivative_url_style;
485    return $S;
486  }
487
488  /**************************************************************************
489   *  function: get_pattern_result
490   **************************************************************************
491   *  in:
492   *    $pattern
493   *  out:
494   *    La chaine expansée
495   **************************************************************************
496   *  Rôle:
497   *    transformer le pattern fourni en résolvant les variables
498   *      ##i : image URL
499   *      ##t : thumbnail URL
500   *      ##g : galerie URL
501   *      ##n : nom de l'image
502   *************************************************************************/
503  function get_pattern_result($row, $pattern)
504  {
505    global $page, $lang_info;
506    include_once(PHPWG_ROOT_PATH.'/include/functions_picture.inc.php');
507
508    // nom image
509    $name = (!empty($row['name']) ? $row['name'] : $row['file']);
510
511    // lien image miniature
512    $tbn = DerivativeImage::thumb_url($row);
513
514    // lien image grand format
515    $img = DerivativeImage::url(IMG_MEDIUM, $row);
516
517    // lien image dans sa galerie
518    $gal = duplicate_picture_url( array( 'image_id' => $row['id'], 'image_file' => $row['file'],));
519
520    $applic = str_replace('##i', $img, $pattern);
521    $applic = str_replace('##t', $tbn, $applic);
522    $applic = str_replace('##g', $gal, $applic);
523    $applic = str_replace('##n', $name, $applic);
524
525    return $applic;
526  }
527   
528  /**************************************************************************
529   *  function: load_config
530   **************************************************************************
531   *  Rôle:
532   *    Chargement de la configuration du plugin
533   *    Definition des valeurs par défaut   
534   *************************************************************************/
535  function load_config()
536  {
537    $x = @file_get_contents( dirname(__FILE__).'/data.dat' );
538    if ($x!==false)
539    {
540      $c = unserialize($x);
541      $this->config = $c;
542    }
543
544    if ( !isset($this->config))
545    {
546      // Default configuration
547      $this->config['URLShackRight'] = 'admin';    // admin, members, all
548      $this->config['DefaultHtmlBBcode'] = 'bbcode'; // bbcode, html, custom
549      $this->config['WebLinkOpenOnOtherWindow'] = 'local';    // local, other
550      $this->config['ImageNameBeforeCode'] = 'list';    // no, list, all
551     
552      $this->config['Display']['Thumbnail'] = 1;
553      $this->config['Display']['ThumbnailToGallery'] = 2;
554      $this->config['Display']['ThumbnailToImage'] = 3;
555      $this->config['Display']['Image'] = 4;
556      $this->config['Display']['ImageToGallery'] = 5;
557      $this->config['Display']['NameToImage'] = 0;
558      $this->config['Display']['NameToGallery'] = 0;
559     
560      $this->config['ExtendedDisplay'][0] = "Thumbnail in Textile format";
561      $this->config['ExtendedLabel'][0] = "!##t(##n)!";
562
563      $this->config['ExtendedDisplay'][1] = "{{##i|##n}}";
564      $this->config['ExtendedLabel'][1] = "Image in Creole format";
565
566      $this->config['ExtendedDisplay'][2] = '<h6 style="text-align: center;"><a href="##i" target="_blank"><img class="aligncenter" style="width: 100%; height: 100%;" src="##t" alt="##t" /></a></h6>';
567      $this->config['ExtendedLabel'][2] = "Personnalised HTML format example";
568
569      $this->config['ExtendedDisplay'][3] = "##n : ##i";
570      $this->config['ExtendedLabel'][3] = "Image name + image URL";
571
572      $this->config['ExtendedDisplay'][4] = ""; 
573      $this->config['ExtendedLabel'][4] = ""; 
574      $this->config['ExtendedDisplay'][5] = ""; 
575      $this->config['ExtendedLabel'][5] = ""; 
576      $this->config['ExtendedDisplay'][6] = ""; 
577      $this->config['ExtendedLabel'][6] = ""; 
578      $this->save_config();
579    }
580  }
581
582  /**************************************************************************
583   *  function: save_config
584   **************************************************************************
585   *  Rôle:
586   *    Sauvegarde de la configuration du plugin
587   *************************************************************************/
588  function save_config()
589  {
590    $file = fopen( dirname(__FILE__).'/data.dat', 'w' );
591    fwrite($file, serialize($this->config) );
592    fclose( $file );
593  }
594
595  /**************************************************************************
596   *  function: plugin_admin_menu
597   **************************************************************************
598   *  in: menu
599   *  out: menu
600   **************************************************************************
601   *  Rôle:
602   *    Ajout du lien d'administration du plugin
603   *************************************************************************/
604  function plugin_admin_menu($menu)
605  {
606    array_push($menu,
607      array(
608      'NAME' => 'PiwiShack',
609      'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/admin/piwishack_admin.php')
610      )
611    );
612    return $menu;
613  }
614   
615}
616
617?>
Note: See TracBrowser for help on using the repository browser.