LightboxGallery.php
author Dan
Wed, 27 May 2009 09:37:06 -0400
changeset 3 b52509cf73af
parent 2 e7eb457efdff
child 4 c2458c3b0365
permissions -rw-r--r--
Fixed maxwidth being required
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     1
<?php
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     2
/**!info**
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     3
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     4
  "Plugin Name"  : "Lightbox Gallery",
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     5
  "Plugin URI"   : "http://enanocms.org/plugin/lightboxgallery",
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     6
  "Description"  : "Adds a <lightboxgallery> tag that lets you have a gallery triggered on click of a thumbnail or something. Documentation at provided URL.",
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     7
  "Author"       : "Dan Fuhry",
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     8
  "Version"      : "0.1",
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
     9
  "Author URI"   : "http://enanocms.org/"
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    10
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    11
**!*/
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    12
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    13
// Hook into wikitext render flow
2
e7eb457efdff Changed hook to render_wikiformat_posttemplates to allow templating galleries
Dan
parents: 1
diff changeset
    14
$plugins->attachHook('render_wikiformat_posttemplates', 'lbgallery_process_tags($text);');
1
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
    15
$plugins->attachHook('html_attribute_whitelist', '$whitelist["lightboxgallery"] = array("maxwidth"); $whitelist["trigger"] = array(); $whitelist["randomimage"] = array("/");');
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    16
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    17
function lbgallery_process_tags(&$text)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    18
{
2
e7eb457efdff Changed hook to render_wikiformat_posttemplates to allow templating galleries
Dan
parents: 1
diff changeset
    19
  // if there are no galleries in this blob, just get out here. also pulls all the matches we need.
3
b52509cf73af Fixed maxwidth being required
Dan
parents: 2
diff changeset
    20
  if ( !preg_match_all('#<lightboxgallery(?: maxwidth="?([0-9]+)"?)?>(.+?)</lightboxgallery>#s', $text, $matches) )
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    21
    return true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    22
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    23
  lbgallery_add_headers();
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    24
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    25
  foreach ( $matches[0] as $i => $match )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    26
  {
2
e7eb457efdff Changed hook to render_wikiformat_posttemplates to allow templating galleries
Dan
parents: 1
diff changeset
    27
    // actual parser loop is pretty simple.
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    28
    $gallery = lbgallery_build_gallery($matches[2][$i], $matches[1][$i]);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    29
    $text = str_replace($match, $gallery, $text);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    30
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    31
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    32
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    33
function lbgallery_add_headers()
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    34
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    35
  global $db, $session, $paths, $template, $plugins; // Common objects
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    36
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    37
  $template->add_header('<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/jquery.js"></script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    38
  $template->add_header('<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/jquery-ui.js"></script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    39
  $template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/lightboxgallery/jquery.lightbox-0.5.pack.js"></script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    40
  $template->add_header('<link rel="stylesheet" type="text/css" href="' . scriptPath . '/plugins/lightboxgallery/jquery.lightbox-0.5.css" />');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    41
  $template->add_header('<script type="text/javascript">
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    42
      var loaded_components = loaded_components || {};
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    43
      loaded_components["jquery.js"] = true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    44
      loaded_components["jquery-ui.js"] = true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    45
      if ( window.pref_disable_js_fx )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    46
      {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    47
        jQuery.fx.off = true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    48
      }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    49
      function lbgallery_construct(selector)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    50
      {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    51
        var settings = {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    52
          // Configuration related to images
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    53
          imageLoading:			\'' . cdnPath . '/images/loading-big.gif\',		// (string) Path and the name of the loading icon
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    54
          imageBtnPrev:			\'' . scriptPath . '/plugins/lightboxgallery/images/lightbox-btn-prev.gif\',			// (string) Path and the name of the prev button image
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    55
          imageBtnNext:			\'' . scriptPath . '/plugins/lightboxgallery/images/lightbox-btn-next.gif\',			// (string) Path and the name of the next button image
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    56
          imageBtnClose:		\'' . scriptPath . '/plugins/lightboxgallery/images/lightbox-btn-close.gif\',		// (string) Path and the name of the close btn
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    57
          imageBlank:				\'' . cdnPath . '/images/spacer.gif\',			// (string) Path and the name of a blank image (one pixel)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    58
        };
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    59
        jQuery(selector).lightBox(settings);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    60
      }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    61
    </script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    62
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    63
2
e7eb457efdff Changed hook to render_wikiformat_posttemplates to allow templating galleries
Dan
parents: 1
diff changeset
    64
// The actual function to build the HTML behind a gallery.
e7eb457efdff Changed hook to render_wikiformat_posttemplates to allow templating galleries
Dan
parents: 1
diff changeset
    65
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    66
function lbgallery_build_gallery($gallerytag, $width)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    67
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    68
  // parse out any text sections
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    69
  $text = preg_replace('#^.*<trigger>(.+?)</trigger>.*$#s', '$1', $gallerytag);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    70
  if ( $text == $gallerytag )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    71
    $text = '';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    72
  $gallerytag = preg_replace('#<trigger>(.+?)</trigger>#s', '', $gallerytag);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    73
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    74
  $images = explode("\n", $gallerytag);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    75
  if ( empty($images) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    76
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    77
    return '<div class="error-box-mini">' . $lang->get('lboxgal_err_no_images') . '</div>';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    78
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    79
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    80
  $id = 'lbgal' . md5(microtime() . mt_rand());
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    81
  $inner = '';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    82
  $width = intval($width);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    83
  if ( empty($width) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    84
    $width = 640;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    85
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    86
  $imagelist = array();
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    87
  foreach ( $images as $line )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    88
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    89
    $line = trim($line);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    90
    if ( empty($line) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    91
      continue;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    92
    
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    93
    list($image) = explode('|', $line);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    94
    $image = sanitize_page_id(trim($image));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    95
    if ( ($alt = strstr($line, '|')) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    96
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    97
      $alt = trim(substr($alt, 1));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    98
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    99
    else
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   100
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   101
      $alt = str_replace('_', ' ', dirtify_page_id($image));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   102
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   103
    $imagelist[] = array($image, $alt);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   104
    $tag = '<a class="' . $id . '" href="' . makeUrlNS('Special', "DownloadFile/$image", "preview&width=$width&height=9999", true) . '" title="' . trim(htmlspecialchars(RenderMan::render($alt))) . '">';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   105
    if ( !isset($firstimageid) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   106
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   107
      $firstimagetag = $tag;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   108
      $firstimageid = $image;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   109
      $firstimagealt = $alt;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   110
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   111
    else
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   112
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   113
      $inner .= $tag . '.</a>';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   114
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   115
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   116
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   117
  if ( $text )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   118
  {
1
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   119
    $trigger = trim($text);
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   120
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   121
  else
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   122
  {
1
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   123
    $trigger = '<a><randomimage /></a>';
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   124
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   125
  
1
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   126
  $trigger = str_replace('<a>', $firstimagetag, $trigger);
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   127
  
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   128
  list($image, $alt) = $imagelist[ array_rand($imagelist) ];
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   129
  $randomimage = '<img alt="' . htmlspecialchars($alt) . '" src="' . makeUrlNS('Special', "DownloadFile/$image", "preview", true) . '" />';
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   130
  $trigger = str_replace(array('<randomimage>', '<randomimage/>', '<randomimage />'), $randomimage, $trigger);
d2584c5a075c Added <randomimage /> tag in trigger
Dan
parents: 0
diff changeset
   131
  
0
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   132
  return "$trigger<nowiki>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   133
    <div style=\"display: none;\">$inner</div>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   134
    <script type=\"text/javascript\">
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   135
      addOnloadHook(function()
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   136
        {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   137
          lbgallery_construct('a.$id');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   138
        });
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   139
    </script></nowiki>";
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   140
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   141
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   142
/**!language**
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   143
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   144
The following text up to the closing comment tag is JSON language data.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   145
It is not PHP code but your editor or IDE may highlight it as such. This
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   146
data is imported when the plugin is loaded for the first time; it provides
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   147
the strings displayed by this plugin's interface.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   148
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   149
You should copy and paste this block when you create your own plugins so
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   150
that these comments and the basic structure of the language data is
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   151
preserved. All language data is in the same format as the Enano core
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   152
language files in the /language/* directories. See the Enano Localization
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   153
Guide and Enano API Documentation for further information on the format of
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   154
language files.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   155
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   156
The exception in plugin language file format is that multiple languages
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   157
may be specified in the language block. This should be done by way of making
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   158
the top-level elements each a JSON language object, with elements named
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   159
according to the ISO-639-1 language they are representing. The path should be:
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   160
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   161
  root => language ID => categories array, ( strings object => category \
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   162
  objects => strings )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   163
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   164
All text leading up to first curly brace is stripped by the parser; using
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   165
a code tag makes jEdit and other editors do automatic indentation and
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   166
syntax highlighting on the language data. The use of the code tag is not
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   167
necessary; it is only included as a tool for development.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   168
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   169
<code>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   170
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   171
  eng: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   172
    categories: [ 'meta', 'lboxgal' ],
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   173
    strings: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   174
      meta: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   175
        lboxgal: 'Lightbox gallery plugin'
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   176
      },
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   177
      lboxgal: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   178
        msg_docs: 'See <a href="http://enanocms.org/plugin/lightboxgallery">lightboxgallery on enanocms.org</a> for usage information.',
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   179
        err_no_images: 'No images specified in gallery. %this.lboxgal_msg_docs%',
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   180
      }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   181
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   182
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   183
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   184
</code>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   185
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   186
**!*/