LightboxGallery.php
author Dan
Tue, 26 May 2009 01:50:16 -0400
changeset 0 1978f8d29bea
child 1 d2584c5a075c
permissions -rw-r--r--
Initial revision.
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
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    14
$plugins->attachHook('render_wikiformat_pre', 'lbgallery_process_tags($text);');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    15
$plugins->attachHook('html_attribute_whitelist', '$whitelist["lightboxgallery"] = array("maxwidth"); $whitelist["trigger"] = array();');
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
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    19
  if ( !preg_match_all('#<lightboxgallery(?: maxwidth="?([0-9]+)"?)>(.+?)</lightboxgallery>#s', $text, $matches) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    20
    return true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    21
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    22
  lbgallery_add_headers();
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    23
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    24
  foreach ( $matches[0] as $i => $match )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    25
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    26
    $gallery = lbgallery_build_gallery($matches[2][$i], $matches[1][$i]);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    27
    $text = str_replace($match, $gallery, $text);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    28
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    29
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    30
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    31
function lbgallery_add_headers()
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    32
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    33
  global $db, $session, $paths, $template, $plugins; // Common objects
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    34
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    35
  $template->add_header('<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/jquery.js"></script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    36
  $template->add_header('<script type="text/javascript" src="' . cdnPath . '/includes/clientside/static/jquery-ui.js"></script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    37
  $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
    38
  $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
    39
  $template->add_header('<script type="text/javascript">
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    40
      var loaded_components = loaded_components || {};
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    41
      loaded_components["jquery.js"] = true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    42
      loaded_components["jquery-ui.js"] = true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    43
      if ( window.pref_disable_js_fx )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    44
      {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    45
        jQuery.fx.off = true;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    46
      }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    47
      function lbgallery_construct(selector)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    48
      {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    49
        var settings = {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    50
          // Configuration related to images
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    51
          imageLoading:			\'' . cdnPath . '/images/loading-big.gif\',		// (string) Path and the name of the loading icon
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    52
          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
    53
          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
    54
          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
    55
          imageBlank:				\'' . cdnPath . '/images/spacer.gif\',			// (string) Path and the name of a blank image (one pixel)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    56
        };
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    57
        jQuery(selector).lightBox(settings);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    58
      }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    59
    </script>');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    60
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    61
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    62
function lbgallery_build_gallery($gallerytag, $width)
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    63
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    64
  // parse out any text sections
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    65
  $text = preg_replace('#^.*<trigger>(.+?)</trigger>.*$#s', '$1', $gallerytag);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    66
  if ( $text == $gallerytag )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    67
    $text = '';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    68
  $gallerytag = preg_replace('#<trigger>(.+?)</trigger>#s', '', $gallerytag);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    69
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    70
  $images = explode("\n", $gallerytag);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    71
  if ( empty($images) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    72
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    73
    return '<div class="error-box-mini">' . $lang->get('lboxgal_err_no_images') . '</div>';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    74
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    75
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    76
  $id = 'lbgal' . md5(microtime() . mt_rand());
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    77
  $inner = '';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    78
  $width = intval($width);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    79
  if ( empty($width) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    80
    $width = 640;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    81
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    82
  $imagelist = array();
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    83
  foreach ( $images as $line )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    84
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    85
    $line = trim($line);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    86
    if ( empty($line) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    87
      continue;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    88
    
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    89
    list($image) = explode('|', $line);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    90
    $image = sanitize_page_id(trim($image));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    91
    if ( ($alt = strstr($line, '|')) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    92
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    93
      $alt = trim(substr($alt, 1));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    94
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    95
    else
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    96
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    97
      $alt = str_replace('_', ' ', dirtify_page_id($image));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    98
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
    99
    $imagelist[] = array($image, $alt);
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   100
    $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
   101
    if ( !isset($firstimageid) )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   102
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   103
      $firstimagetag = $tag;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   104
      $firstimageid = $image;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   105
      $firstimagealt = $alt;
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   106
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   107
    else
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   108
    {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   109
      $inner .= $tag . '.</a>';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   110
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   111
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   112
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   113
  if ( $text )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   114
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   115
    $trigger = str_replace('<a>', $firstimagetag, trim($text));
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   116
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   117
  else
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   118
  {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   119
    list($image, $alt) = $imagelist[ array_rand($imagelist) ];
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   120
    $trigger = $firstimagetag . '<img alt="' . htmlspecialchars($alt) . '" src="' . makeUrlNS('Special', "DownloadFile/$image", "preview", true) . '" />' . '</a>';
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   121
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   122
  
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   123
  return "$trigger<nowiki>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   124
    <div style=\"display: none;\">$inner</div>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   125
    <script type=\"text/javascript\">
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   126
      addOnloadHook(function()
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   127
        {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   128
          lbgallery_construct('a.$id');
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   129
        });
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   130
    </script></nowiki>";
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   131
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   132
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   133
/**!language**
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   134
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   135
The following text up to the closing comment tag is JSON language data.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   136
It is not PHP code but your editor or IDE may highlight it as such. This
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   137
data is imported when the plugin is loaded for the first time; it provides
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   138
the strings displayed by this plugin's interface.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   139
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   140
You should copy and paste this block when you create your own plugins so
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   141
that these comments and the basic structure of the language data is
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   142
preserved. All language data is in the same format as the Enano core
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   143
language files in the /language/* directories. See the Enano Localization
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   144
Guide and Enano API Documentation for further information on the format of
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   145
language files.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   146
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   147
The exception in plugin language file format is that multiple languages
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   148
may be specified in the language block. This should be done by way of making
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   149
the top-level elements each a JSON language object, with elements named
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   150
according to the ISO-639-1 language they are representing. The path should be:
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   151
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   152
  root => language ID => categories array, ( strings object => category \
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   153
  objects => strings )
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   154
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   155
All text leading up to first curly brace is stripped by the parser; using
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   156
a code tag makes jEdit and other editors do automatic indentation and
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   157
syntax highlighting on the language data. The use of the code tag is not
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   158
necessary; it is only included as a tool for development.
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   159
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   160
<code>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   161
{
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   162
  eng: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   163
    categories: [ 'meta', 'lboxgal' ],
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   164
    strings: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   165
      meta: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   166
        lboxgal: 'Lightbox gallery plugin'
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   167
      },
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   168
      lboxgal: {
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   169
        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
   170
        err_no_images: 'No images specified in gallery. %this.lboxgal_msg_docs%',
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   171
      }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   172
    }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   173
  }
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   174
}
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   175
</code>
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   176
1978f8d29bea Initial revision.
Dan
parents:
diff changeset
   177
**!*/