1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25: 
 26: $plugin_description = gettext("Provides a means allowing users to select the image size to view.");
 27: $plugin_author = "Stephen Billard (sbillard)";
 28: 
 29: $option_interface = 'viewer_size_image_options';
 30: 
 31:  32:  33:  34: 
 35: class viewer_size_image_options {
 36: 
 37:     function __construct() {
 38:         if (OFFSET_PATH == 2) {
 39:             $default = getOption('image_size');
 40:             setOptionDefault('viewer_size_image_sizes', '$s=' . ($default - 200) . '; $s=' . ($default - 100) . '; $s=' . ($default) . '; $s=' . ($default + 100) . '; $s=' . ($default + 200) . ';');
 41:             setOptionDefault('viewer_size_image_default', '$s=' . $default);
 42:             setOptionDefault('viewer_size_image_radio', 2);
 43:             if (class_exists('cacheManager')) {
 44:                 cacheManager::deleteThemeCacheSizes('viewer_size_image');
 45:                 cacheManager::addThemeCacheSize('viewer_size_image', $default - 200, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 46:                 cacheManager::addThemeCacheSize('viewer_size_image', $default - 100, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 47:                 cacheManager::addThemeCacheSize('viewer_size_image', $default, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 48:                 cacheManager::addThemeCacheSize('viewer_size_image', $default + 100, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 49:                 cacheManager::addThemeCacheSize('viewer_size_image', $default + 200, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 50:             }
 51:         }
 52:     }
 53: 
 54:     function getOptionsSupported() {
 55:         return array(gettext('Image sizes allowed') => array('key'                   => 'viewer_size_image_sizes', 'type'                => OPTION_TYPE_TEXTAREA,
 56:                                         'multilingual' => false,
 57:                                         'desc'               => gettext('List of sizes from which the viewer may select.<br />The form is "$s=<size>" or "$h=<height>,$w=<width>"....<br />See printCustomSizedImage() for details')),
 58:                         gettext('Selector')                      => array('key'          => 'viewer_size_image_radio', 'type'        => OPTION_TYPE_RADIO,
 59:                                         'buttons'    => array(gettext('Radio buttons') => 2, gettext('Drop-down') => 1),
 60:                                         'desc'       => gettext('Choose the kind of selector to be presented the viewer.')),
 61:                         gettext('Default size')              => array('key'  => 'viewer_size_image_default', 'type' => OPTION_TYPE_TEXTBOX,
 62:                                         'desc' => gettext('The initial size for the image. Format is a single instance of the sizes list.'))
 63:         );
 64:     }
 65: 
 66:     function handleOption($option, $currentValue) {
 67: 
 68:     }
 69: 
 70: }
 71: 
 72: if (!OFFSET_PATH) {
 73:     $saved = @$_COOKIE['viewer_size_image_saved']; 
 74:     if (empty($saved)) {
 75:         $postdefault = trim(getOption('viewer_size_image_default'));
 76:     } else {
 77:         $_POST['viewer_size_image_selection'] = true; 
 78:         $postdefault = $saved;
 79:     }
 80: }
 81: 
 82:  83:  84:  85:  86:  87:  88: 
 89: function printUserSizeSelector($text = '', $default = NULL, $usersizes = NULL) {
 90:     $size = $width = $height = NULL;
 91:     getViewerImageSize($default, $size, $width, $height);
 92:     if (!empty($size)) {
 93:         $current = $size;
 94:     } else {
 95:         $current = $width . 'x' . $height;
 96:     }
 97:     $sizes = array();
 98:     if (empty($text))
 99:         $text = gettext('Select image size');
100:     if (is_null($usersizes)) {
101:         $inputs = explode(';', trim(getOption('viewer_size_image_sizes')));
102:         if (!empty($inputs)) {
103:             foreach ($inputs as $size) {
104:                 if (!empty($size)) {
105:                     $size = str_replace(',', ';', $size) . ';';
106:                     $s = $w = $h = NULL;
107:                     if (false === eval($size)) {
108:                         trigger_error(gettext('There is a format error in your <em>viewer_size_image_sizes</em> option string.'), E_USER_NOTICE);
109:                     }
110:                     if (!empty($s)) {
111:                         $key = $s;
112:                     } else {
113:                         $key = $w . 'x' . $h;
114:                     }
115:                     $sizes[$key] = array('$s' => $s, '$h' => $h, '$w' => $w);
116:                 }
117:             }
118:         }
119:     } else {
120:         foreach ($usersizes as $key => $size) {
121:             if (!empty($size)) {
122:                 $size = str_replace(',', ';', $size) . ';';
123:                 $s = $w = $h = NULL;
124:                 if (false === eval($size)) {
125:                     trigger_error(gettext('There is a format error in your $usersizes string.'), E_USER_NOTICE);
126:                 }
127:                 if (!empty($s)) {
128:                     $key = $s;
129:                 } else {
130:                     $key = $w . 'x' . $h;
131:                 }
132:                 $sizes[$key] = array('$s' => $s, '$h' => $h, '$w' => $w);
133:             }
134:         }
135:     }
136:     if (($cookiepath = WEBPATH) == '')
137:         $cookiepath = '/';
138:     ?>
139:     <script type="text/javascript">
140:         
141:     <?php
142:     $selector = getOption('viewer_size_image_radio') == 1;
143:     if ($selector) {
144:         ?>
145:             function switchselection() {
146:                 var selection = $("#viewer_size_image_selection").val();
147:                 var items = selection.split(':');
148:                 $('#image img').attr('width', items[1]);
149:                 $('#image img').attr('height', items[2]);
150:                 $('#image img').attr('src', items[3]);
151:                 document.cookie = 'viewer_size_image_saved=' + items[0] + '; expires=<?php echo date('Y-m-d H:i:s', time() + COOKIE_PESISTENCE); ?>; path=<?php echo $cookiepath ?>';
152:             }
153:         <?php
154:     } else { 
155:         ?>
156:             function switchimage(obj) {
157:                 var url = $(obj).attr('url');
158:                 var w = $(obj).attr('im_w');
159:                 var h = $(obj).attr('im_h');
160:                 $('#image img').attr('width', w);
161:                 $('#image img').attr('height', h);
162:                 $('#image img').attr('src', url);
163:                 document.cookie = 'viewer_size_image_saved=' + $(obj).attr('value') + '; expires=<?php echo date('Y-m-d H:i:s', time() + COOKIE_PESISTENCE); ?>; path=<?php echo $cookiepath ?>';
164:             }
165:         <?php
166:     }
167:     ?>
168:         
169:     </script>
170:     <div>
171:         <?php
172:         echo $text;
173:         if ($selector) {
174:             ?>
175:             <select id="viewer_size_image_selection" name="viewer_size_image_selection" onchange="switchselection();" >
176:                 <?php
177:             }
178:             foreach ($sizes as $key => $size) {
179:                 if (empty($size['$s'])) {
180:                     $display = sprintf(gettext('%1$s x %2$s px'), $size['$w'], $size['$h']);
181:                     $url = getCustomImageURL(null, $size['$w'], $size['$h'], null, null, null, null, false);
182:                     $value = '$h=' . $size['$h'] . ',$w=' . $size['$w'];
183:                     $dims = array($size['$w'], $size['$h']);
184:                 } else {
185:                     $dims = getSizeCustomImage($size['$s']);
186:                     $display = sprintf(gettext('%s px'), $size['$s']);
187:                     $url = getCustomImageURL($size['$s'], null, null, null, null, null, null, false);
188:                     $value = '$s=' . $size['$s'];
189:                 }
190:                 if ($selector) {
191:                     $selected = '';
192:                     if ($key == $current) {
193:                         $selected = ' selected="selected"';
194:                     }
195:                     ?>
196:                     <option id="s<?php echo $key; ?>" value="<?php echo $value . ':' . implode(':', $dims) . ':' . $url; ?>"<?php echo $selected; ?> />
197:                     <?php echo $display; ?>
198:                     </option>
199:                     <?php
200:                 } else {
201:                     $checked = "";
202:                     if ($key == $current) {
203:                         $checked = ' checked="checked"';
204:                     }
205:                     ?>
206:                     <input type="radio" name="viewer_size_image_selection" id="s<?php echo $key; ?>" url="<?php echo $url; ?>"
207:                                  im_w="<?php echo $dims[0]; ?>" im_h="<?php echo $dims[1]; ?>"
208:                                  value="<?php echo $value; ?>"<?php echo $checked; ?> onclick="switchimage(this);" />
209:                     <label for="s<?php echo $key; ?>"> <?php echo $display; ?></label>
210:                     <?php
211:                 }
212:             }
213:             if ($selector) {
214:                 ?>
215:             </select>
216:             <?php
217:         }
218:         ?>
219:     </div>
220:     <?php
221: }
222: 
223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 
238: function getViewerImageSize($default, &$size, &$width, &$height) {
239:     global $postdefault;
240:     if (isset($_POST['viewer_size_image_selection']) || empty($default)) {
241:         $msg = gettext('There is a format error in user size selection');
242:         $validate = $postdefault;
243:     } else {
244:         $msg = gettext('There is a format error in your $default parameter');
245:         $validate = $default;
246:     }
247:     $size = $width = $height = NULL;
248:     preg_match_all('/(\$[shw])[\s]*=[\s]*([0-9]+)/', $validate, $matches);
249:     if ($matches) {
250:         foreach ($matches[0] as $key => $str) {
251:             switch ($matches[1][$key]) {
252:                 case '$s':
253:                     $size = $matches[2][$key];
254:                     break;
255:                 case '$w':
256:                     $width = $matches[2][$key];
257:                     break;
258:                 case '$h':
259:                     $height = $matches[2][$key];
260:                     break;
261:             }
262:         }
263: 
264:         if (!empty($size)) {
265:             $width = $height = NULL;
266:         } else {
267:             $size = NULL;
268:         }
269:     }
270:     if (empty($size) && empty($width) && empty($height)) {
271:         trigger_error($msg, E_USER_NOTICE);
272:     }
273: }
274: 
275: 276: 277: 278: 279: 280: 281: 282: 
283: function printUserSizeImage($alt, $default = NULL, $class = NULL, $id = NULL) {
284:     $size = $width = $height = NULL;
285:     getViewerImageSize($default, $size, $width, $height);
286:     if (empty($size)) {
287:         printCustomSizedImageMaxSpace($alt, $width, $height, $class, $id);
288:     } else {
289:         printCustomSizedImage($alt, $size, $width, $height, NULL, NULL, NULL, NULL, $class, $id, false);
290:     }
291: }
292: ?>
293: