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: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: $plugin_is_filter = 5 | FEATURE_PLUGIN;
47: $plugin_description = gettext('Support for <em>favorites</em> handling.');
48: $plugin_author = "Stephen Billard (sbillard)";
49:
50: $option_interface = 'favoritesOptions';
51:
52: require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/favoritesHandler/favoritesClass.php');
53:
54: class favoritesOptions {
55:
56: function __construct() {
57: if (OFFSET_PATH == 2) {
58: setOptionDefault('favorites_multi', 0);
59: setOptionDefault('favorites_link', '_PAGE_/favorites');
60: gettext($str = 'My favorites');
61: setOptionDefault('favorites_title', getAllTranslations($str));
62: setOptionDefault('favorites_linktext', getAllTranslations($str));
63: gettext($str = 'The albums and images selected as favorites.');
64: setOptionDefault('favorites_desc', getAllTranslations($str));
65: gettext($str = 'Add favorite');
66: setOptionDefault('favorites_add_button', getAllTranslations($str));
67: gettext($str = 'Remove favorite');
68: setOptionDefault('favorites_remove_button', getAllTranslations($str));
69: setOptionDefault('favorites_album_sort_type', 'title');
70: setOptionDefault('favorites_image_sort_type', 'title');
71: setOptionDefault('favorites_album_sort_direction', '');
72: setOptionDefault('favorites_image_sort_direction', '');
73: }
74: }
75:
76: function getOptionsSupported() {
77: global $_zp_gallery;
78: $themename = $_zp_gallery->getCurrentTheme();
79: $curdir = getcwd();
80: $root = SERVERPATH . '/' . THEMEFOLDER . '/' . $themename . '/';
81: chdir($root);
82: $filelist = safe_glob('*.php');
83: $list = array();
84: foreach ($filelist as $file) {
85: $file = filesystemToInternal($file);
86: $list[$file] = str_replace('.php', '', $file);
87: }
88: $list = array_diff($list, standardScripts());
89:
90: $options = array(gettext('Link text') => array('key' => 'favorites_linktext', 'type' => OPTION_TYPE_TEXTBOX,
91: 'multilingual' => true,
92: 'order' => 2,
93: 'desc' => gettext('The text for the link to the favorites page.')),
94: gettext('Multiple sets') => array('key' => 'favorites_multi', 'type' => OPTION_TYPE_CHECKBOX,
95: 'order' => 6,
96: 'desc' => gettext('If enabled a user may have multiple (named) favorites.')),
97: gettext('Add button') => array('key' => 'favorites_add_button', 'type' => OPTION_TYPE_TEXTBOX,
98: 'multilingual' => true,
99: 'order' => 6,
100: 'desc' => gettext('Default text for the <em>add to favorites</em> button.')),
101: gettext('Remove button') => array('key' => 'favorites_remove_button', 'type' => OPTION_TYPE_TEXTBOX,
102: 'multilingual' => true,
103: 'order' => 7,
104: 'desc' => gettext('Default text for the <em>remove from favorites</em> button.')),
105: gettext('Title') => array('key' => 'favorites_title', 'type' => OPTION_TYPE_TEXTBOX,
106: 'multilingual' => true,
107: 'order' => 3,
108: 'desc' => gettext('The favorites page title text.')),
109: gettext('Description') => array('key' => 'favorites_desc', 'type' => OPTION_TYPE_TEXTAREA,
110: 'multilingual' => true,
111: 'order' => 5,
112: 'desc' => gettext('The favorites page description text.')),
113: gettext('Sort albums by') => array('key' => 'favorites_albumsort', 'type' => OPTION_TYPE_CUSTOM,
114: 'order' => 9,
115: 'desc' => ''),
116: gettext('Sort images by') => array('key' => 'favorites_imagesort', 'type' => OPTION_TYPE_CUSTOM,
117: 'order' => 10,
118: 'desc' => '')
119: );
120: if (!MOD_REWRITE) {
121: $options['note'] = array(
122: 'key' => 'favorites_note',
123: 'type' => OPTION_TYPE_NOTE,
124: 'order' => 0,
125: 'desc' => gettext('<p class="notebox">Favorites requires the <code>mod_rewrite</code> option be enabled.</p>')
126: );
127: }
128:
129: return $options;
130: }
131:
132: function handleOption($option, $currentValue) {
133: $sort = array(gettext('Filename') => 'filename',
134: gettext('Custom') => 'custom',
135: gettext('Date') => 'date',
136: gettext('Title') => 'title',
137: gettext('ID') => 'id',
138: gettext('Filemtime') => 'mtime',
139: gettext('Owner') => 'owner',
140: gettext('Published') => 'show'
141: );
142:
143: switch ($option) {
144: case 'favorites_albumsort':
145: ?>
146: <span class="nowrap">
147: <select id="albumsortselect" name="subalbumsortby" onchange="update_direction(this, 'album_direction_div', 'album_custom_div');">
148: <?php
149: $cvt = $type = strtolower(getOption('favorites_album_sort_type'));
150: if ($type && !in_array($type, $sort)) {
151: $cv = array('custom');
152: } else {
153: $cv = array($type);
154: }
155: generateListFromArray($cv, $sort, false, true);
156: ?>
157: </select>
158: <?php
159: if (($type == 'random') || ($type == '')) {
160: $dsp = 'none';
161: } else {
162: $dsp = 'inline';
163: }
164: ?>
165: <label id="album_direction_div" style="display:<?php echo $dsp; ?>;white-space:nowrap;">
166: <?php echo gettext("Descending"); ?>
167: <input type="checkbox" name="album_sortdirection" value="1"
168: <?php
169: if (getOption('favorites_album_sort_direction')) {
170: echo "CHECKED";
171: };
172: ?> />
173: </label>
174: </span>
175: <?php
176: break;
177: case 'favorites_imagesort':
178: ?>
179: <span class="nowrap">
180: <select id="imagesortselect" name="sortby" onchange="update_direction(this, 'image_direction_div', 'image_custom_div')">
181: <?php
182: $cvt = $type = strtolower(getOption('favorites_image_sort_type'));
183: if ($type && !in_array($type, $sort)) {
184: $cv = array('custom');
185: } else {
186: $cv = array($type);
187: }
188: generateListFromArray($cv, $sort, false, true);
189: ?>
190: </select>
191: <?php
192: if (($type == 'random') || ($type == '')) {
193: $dsp = 'none';
194: } else {
195: $dsp = 'inline';
196: }
197: ?>
198: <label id="image_direction_div" style="display:<?php echo $dsp; ?>;white-space:nowrap;">
199: <?php echo gettext("Descending"); ?>
200: <input type="checkbox" name="image_sortdirection" value="1"
201: <?php
202: if (getOption('favorites_image_sort_direction')) {
203: echo ' checked="checked"';
204: }
205: ?> />
206: </label>
207: </span>
208: <?php
209: break;
210: }
211: }
212:
213: function handleOptionSave($theme, $album) {
214: $sorttype = strtolower(sanitize($_POST['sortby'], 3));
215: if ($sorttype == 'custom') {
216: $sorttype = unquote(strtolower(sanitize($_POST['customimagesort'], 3)));
217: }
218: setOption('favorites_image_sort_type', $sorttype);
219: if (($sorttype == 'manual') || ($sorttype == 'random')) {
220: setOption('favorites_image_sort_direction', 0);
221: } else {
222: if (empty($sorttype)) {
223: $direction = 0;
224: } else {
225: $direction = isset($_POST['image_sortdirection']);
226: }
227: setOption('favorites_image_sort_direction', $direction ? 'DESC' : '');
228: }
229: $sorttype = strtolower(sanitize($_POST['subalbumsortby'], 3));
230: if ($sorttype == 'custom')
231: $sorttype = strtolower(sanitize($_POST['customalbumsort'], 3));
232: setOption('favorites_album_sort_type', $sorttype);
233: if (($sorttype == 'manual') || ($sorttype == 'random')) {
234: $direction = 0;
235: } else {
236: $direction = isset($_POST['album_sortdirection']);
237: }
238: setOption('favorites_album_sort_direction', $direction ? 'DESC' : '');
239: return false;
240: }
241:
242: }
243:
244: $_zp_conf_vars['special_pages']['favorites'] = array('define' => '_FAVORITES_', 'rewrite' => getOption('favorites_link'),
245: 'option' => 'favorites_link', 'default' => '_PAGE_/favorites');
246: $_zp_conf_vars['special_pages'][] = array('definition' => '%FAVORITES%', 'rewrite' => '_FAVORITES_');
247: $_zp_conf_vars['special_pages'][] = array('define' => false, 'rewrite' => '^%FAVORITES%/(.+)/([0-9]+)/?$',
248: 'rule' => '%REWRITE% index.php?p=favorites&instance=$1&page=$2 [L,QSA]');
249: $_zp_conf_vars['special_pages'][] = array('define' => false, 'rewrite' => '^%FAVORITES%/([0-9]+)/?$',
250: 'rule' => '%REWRITE% index.php?p=favorites&page=$1 [L,QSA]');
251: $_zp_conf_vars['special_pages'][] = array('define' => false, 'rewrite' => '^%FAVORITES%/(.+)/?$',
252: 'rule' => '%REWRITE% index.php?p=favorites&instance=$1 [L,QSA]');
253: $_zp_conf_vars['special_pages'][] = array('define' => false, 'rewrite' => '^%FAVORITES%/*$',
254: 'rule' => '%REWRITE% index.php?p=favorites [L,QSA]');
255:
256: if (OFFSET_PATH) {
257: zp_register_filter('edit_album_custom_data', 'favorites::showWatchers');
258: zp_register_filter('edit_image_custom_data', 'favorites::showWatchers');
259: } else {
260: zp_register_filter('load_theme_script', 'favorites::loadScript');
261: zp_register_filter('checkPageValidity', 'favorites::pageCount');
262: zp_register_filter('admin_toolbox_global', 'favorites::toolbox', 21);
263: if (zp_loggedin()) {
264: if (isset($_POST['addToFavorites'])) {
265: $___Favorites = new favorites($_zp_current_admin_obj->getUser());
266: if (isset($_POST['instance']) && $_POST['instance']) {
267: $___Favorites->instance = trim(sanitize($_POST['instance']));
268: unset($_POST['instance']);
269: }
270: $id = sanitize($_POST['id']);
271: switch ($_POST['type']) {
272: case 'images':
273: $img = newImage(NULL, array('folder' => dirname($id), 'filename' => basename($id)));
274: if ($_POST['addToFavorites']) {
275: if ($img->loaded) {
276: $___Favorites->addImage($img);
277: }
278: } else {
279: $___Favorites->removeImage($img);
280: }
281: break;
282: case 'albums':
283: $alb = newAlbum($id);
284: if ($_POST['addToFavorites']) {
285: if ($alb->loaded) {
286: $___Favorites->addAlbum($alb);
287: }
288: } else {
289: $___Favorites->removeAlbum($alb);
290: }
291: break;
292: }
293: unset($___Favorites);
294: if (isset($_instance)) {
295: unset($_instance);
296: }
297: }
298: $_myFavorites = new favorites($_zp_current_admin_obj->getUser());
299:
300: function printAddToFavorites($obj, $add = NULL, $remove = NULL) {
301: global $_myFavorites, $_zp_current_admin_obj, $_zp_gallery_page, $_myFavorites_button_count;
302: if (!zp_loggedin() || $_myFavorites->getOwner() != $_zp_current_admin_obj->getUser() || !is_object($obj) || !$obj->exists) {
303: return;
304: }
305:
306: $v = 1;
307: if (is_null($add)) {
308: $add = get_language_string(getOption('favorites_add_button'));
309: }
310: if (is_null($remove)) {
311: $remove = get_language_string(getOption('favorites_remove_button'));
312: } else {
313: $add = $remove;
314: }
315: $table = $obj->table;
316: $target = array('type' => $table);
317: if ($_zp_gallery_page == 'favorites.php') {
318:
319: $multi = false;
320: $list = array($_myFavorites->instance);
321: } else {
322: if ($multi = getOption('favorites_multi')) {
323: $list = $_myFavorites->list;
324: } else {
325: $list = array('');
326: }
327: if (extensionEnabled('tag_suggest') && !$_myFavorites_button_count) {
328: $_myFavorites_button_count++;
329: $favList = array_slice($list, 1);
330: ?>
331: <script type="text/javascript">
332:
333: var _favList = ['<?php echo implode("','", $favList); ?>'];
334: $(function() {
335: $('.favorite_instance').tagSuggest({tags: _favList})
336: });
337:
338: </script>
339: <?php
340: }
341: }
342: $seen = array_flip($list);
343: switch ($table) {
344: case 'images':
345: $id = $obj->imagefolder . '/' . $obj->filename;
346: foreach ($list as $instance) {
347: $_myFavorites->instance = $instance;
348: $images = $_myFavorites->getImages(0);
349: $seen[$instance] = false;
350: foreach ($images as $image) {
351: if ($image['folder'] == $obj->imagefolder && $image['filename'] == $obj->filename) {
352: $seen[$instance] = true;
353: favorites::ad_removeButton($obj, $id, 0, $remove, $instance, $multi);
354: break;
355: }
356: }
357: }
358: if ($multi || in_array(false, $seen))
359: favorites::ad_removeButton($obj, $id, 1, $add, NULL, $multi);
360: break;
361: case 'albums':
362: $id = $obj->name;
363: foreach ($list as $instance) {
364: $_myFavorites->instance = $instance;
365: $albums = $_myFavorites->getAlbums(0);
366: $seen[$instance] = false;
367: foreach ($albums as $album) {
368: if ($album == $id) {
369: $seen[$instance] = true;
370: favorites::ad_removeButton($obj, $id, 0, $remove, $instance, $multi);
371: break;
372: }
373: }
374: }
375: if ($multi || in_array(false, $seen))
376: favorites::ad_removeButton($obj, $id, 1, $add, NULL, $multi);
377: break;
378: default:
379:
380: return;
381: }
382: }
383:
384: function getFavoritesURL() {
385: global $_myFavorites;
386: return $_myFavorites->getLink();
387: }
388:
389: 390: 391: 392: 393: 394:
395: function printFavoritesURL($text = NULL, $before = NULL, $between = NULL, $after = NULL) {
396: global $_myFavorites;
397: if (zp_loggedin()) {
398: if (is_null($text)) {
399: $text = get_language_string(getOption('favorites_linktext'));
400: }
401: $list = $_myFavorites->getList();
402: $betwixt = NULL;
403: echo $before;
404: foreach ($_myFavorites->getList()as $instance) {
405: $link = $_myFavorites->getLink(NULL, $instance);
406: $display = $text;
407: if ($instance) {
408: $display .= '[' . $instance . ']';
409: }
410: echo $betwixt;
411: $betwixt = $between;
412: ?>
413: <a href="<?php echo $link; ?>" class="favorite_link"><?php echo html_encode($display); ?> </a>
414: <?php
415: }
416: echo $after;
417: }
418: }
419:
420: }
421: }
422: ?>