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: $plugin_is_filter = 5 | FEATURE_PLUGIN;
42: $plugin_description = gettext("Multiple <em>Theme</em> layouts");
43: $plugin_author = "Malte Müller (acrylian), Stephen Billard (sbillard)";
44:
45: $option_interface = 'multipleLayoutOptions';
46:
47: zp_register_filter('load_theme_script', 'getLayout');
48: zp_register_filter('remove_object', 'deleteLayoutSelection');
49: zp_register_filter('copy_object', 'copyLayoutSelection');
50: if (getOption('multiple_layouts_albums')) {
51: zp_register_filter('edit_album_utilities', 'layoutSelector_album');
52: zp_register_filter('save_album_utilities_data', 'saveZenphotoLayoutSelection');
53: }
54: if (getOption('multiple_layouts_images')) {
55: zp_register_filter('edit_image_utilities', 'layoutSelector');
56: zp_register_filter('save_image_utilities_data', 'saveZenphotoLayoutSelection');
57: }
58: if (extensionEnabled('zenpage')) {
59: if (getOption('multiple_layouts_pages')) {
60: zp_register_filter('publish_page_utilities', 'layoutSelector');
61: zp_register_filter('new_page', 'saveLayoutSelection');
62: zp_register_filter('update_page', 'saveLayoutSelection');
63: }
64: if (getOption('multiple_layouts_news')) {
65: zp_register_filter('publish_article_utilities', 'layoutSelector');
66: zp_register_filter('new_article', 'saveLayoutSelection');
67: zp_register_filter('update_article', 'saveLayoutSelection');
68: }
69: if (getOption('multiple_layouts_news_categories')) {
70: zp_register_filter('publish_category_utilities', 'layoutSelector');
71: zp_register_filter('new_category', 'saveLayoutSelection');
72: zp_register_filter('update_category', 'saveLayoutSelection');
73: }
74: }
75:
76: 77: 78: 79:
80: class multipleLayoutOptions {
81:
82: function __construct() {
83: setOptionDefault('multiple_layouts_images', 0);
84: setOptionDefault('multiple_layouts_albums', 0);
85: setOptionDefault('multiple_layouts_pages', 1);
86: setOptionDefault('multiple_layouts_news', 1);
87: setOptionDefault('multiple_layouts_news_categories', 1);
88: }
89:
90: function getOptionsSupported() {
91: $checkboxes = array(gettext('Albums') => 'multiple_layouts_albums', gettext('Images') => 'multiple_layouts_images');
92: if (extensionEnabled('zenpage')) {
93: $checkboxes = array_merge($checkboxes, array(gettext('Pages') => 'multiple_layouts_pages', gettext('News') => 'multiple_layouts_news', gettext('News categories') => 'multiple_layouts_news_categories'));
94: }
95: $options = array(gettext('Enable multiple layouts for') => array('key' => 'multiple_layouts_allowed', 'type' => OPTION_TYPE_CHECKBOX_ARRAY,
96: 'checkboxes' => $checkboxes,
97: 'desc' => '')
98: );
99: return $options;
100: }
101:
102: function handleOption($option, $currentValue) {
103:
104: }
105:
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115:
116: function getSelectedLayout($obj, $type) {
117: if ($obj && $obj->exists) {
118: $assignedlayout = query_single_row("SELECT * FROM " . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND `type` = "' . $type . '"');
119: if (!$assignedlayout || empty($assignedlayout['data'])) {
120: $assignedlayout = checkParentLayouts($obj, $type);
121: }
122: return $assignedlayout;
123: }
124: return false;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: function checkParentLayouts($obj, $type) {
136: $parents = array();
137: switch ($type) {
138: case 'multiple_layouts_images':
139: $type = 'multiple_layouts_albums_images';
140: $obj = $obj->getAlbum();
141: array_unshift($parents, $obj);
142: case 'multiple_layouts_albums':
143: case 'multiple_layouts_albums_images':
144: while (!is_null($obj = $obj->getParent())) {
145: array_unshift($parents, $obj);
146: }
147: if (count($parents) > 0) {
148: $parents = array_reverse($parents);
149: foreach ($parents as $parentobj) {
150: $parentlayouts = query_single_row('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `aux`=' . $parentobj->getID() . ' AND `type` = "' . $type . '"');
151: if ($parentlayouts && $parentlayouts['data']) {
152: return $parentlayouts;
153: }
154: }
155: }
156: break;
157: case 'multiple_layouts_pages':
158: case 'multiple_layouts_news_categories':
159: $parents = $obj->getParents();
160: if (count($parents) > 0) {
161: $parents = array_reverse($parents);
162: foreach ($parents as $parent) {
163: if ($type === 'multiple_layouts_pages') {
164: $parentobj = new ZenpagePage($parent);
165: } else {
166: $parentobj = new ZenpageCategory($parent);
167: }
168: $parentlayouts = query_single_row('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `aux`=' . $parentobj->getID() . ' AND `type` = "' . $type . '"');
169: if ($parentlayouts && $parentlayouts['data']) {
170: return $parentlayouts;
171: }
172: }
173: }
174: break;
175: }
176: return false;
177: }
178:
179: 180: 181: 182: 183: 184:
185: function checkLayoutUseForImages($obj) {
186: $albumimagelayout = query_single_row("SELECT id, `data` FROM " . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND `type` = "multiple_layouts_albums_images"');
187: if ($albumimagelayout) {
188: return $albumimagelayout;
189: } else {
190: $parents = array();
191: while (!is_null($obj = $obj->getParent())) {
192: array_unshift($parents, $obj);
193: }
194: if (count($parents) > 0) {
195: $parents = array_reverse($parents);
196: foreach ($parents as $parent) {
197: $parentimagelayouts = query_full_array('SELECT id, `data` FROM ' . prefix('plugin_storage') . ' WHERE `aux`=' . $parent->getID() . ' AND `type` = "multiple_layouts_albums_images"');
198: if ($parentimagelayouts && $parentimagelayouts['data']) {
199: return $parentimagelayouts;
200: }
201: }
202: }
203: }
204: return false;
205: }
206:
207: 208: 209: 210: 211: 212:
213: function layoutSelector($html, $obj, $prefix = '') {
214: $type = 'multiple_layouts_' . $obj->table;
215: if (getOption($type)) {
216: $html .= getLayoutSelector($obj, $type, '<hr /><p>' . gettext('Select layout:') . '</p>', $prefix);
217: }
218: return $html;
219: }
220:
221: 222: 223: 224: 225: 226:
227: function layoutSelector_album($html, $obj, $prefix) {
228: if (getOption('multiple_layouts_albums')) {
229: $albumhtml = getLayoutSelector($obj, 'multiple_layouts_albums', '<hr /><p>' . gettext('Select album layout:') . '</p>', $prefix);
230: $imagehtml = getLayoutSelector($obj, 'multiple_layouts_albums_images', '<p>' . gettext('Select default album image layout:') . '</p>', $prefix, true);
231: if (!$obj->isDynamic() && strpos($imagehtml, '<p class="no_extra">') === false) {
232: $imagehtml .= '<br /><input type="checkbox" id="layout_selector_resetimagelayouts" name="layout_selector_resetimagelayouts" /><label for="layout_selector_resetimagelayouts">' . gettext('Reset individual image layouts') . '</label>';
233: }
234: return $html . $albumhtml . $imagehtml;
235: }
236: return $html;
237: }
238:
239: 240: 241: 242: 243: 244: 245: 246:
247: function getLayoutSelector($obj, $type, $text, $prefix = '', $secondary = false) {
248: global $_zp_gallery;
249: $selectdefault = '';
250: $selected = '';
251: $files = array();
252: $list = array();
253: $getlayout = '';
254: $table = $obj->table;
255: $path = SERVERPATH . '/' . THEMEFOLDER . '/' . $_zp_gallery->getCurrentTheme() . '/';
256: $defaultlayout = '';
257: $defaulttext = gettext('default');
258: switch ($table) {
259: case 'albums':
260: if ($secondary) {
261: $filesmask = 'image';
262: } else {
263: $filesmask = 'album';
264: };
265: $child = $obj->getParentID();
266: $defaulttext = gettext('inherited');
267: break;
268: case 'images':
269: $filesmask = 'image';
270: $album = $obj->album;
271: $child = $album->getID();
272: $defaulttext = gettext('album default');
273: break;
274: case 'pages':
275: $filesmask = 'pages';
276: $child = $obj->getParentID();
277: $defaulttext = gettext('inherited');
278: break;
279: case 'news':
280: $child = false;
281: $categories = $obj->getCategories();
282: if ($categories) {
283: foreach ($categories as $cat) {
284: $cat = new ZenpageCategory($cat['titlelink']);
285: $getlayout = getSelectedLayout($cat, 'multiple_layouts_news_categories');
286: if ($getlayout && $getlayout['data']) {
287: $defaulttext = gettext('inherited');
288: $defaultlayout = gettext('from category');
289: break;
290: }
291: }
292: }
293: $filesmask = 'news';
294: break;
295: case 'news_categories':
296: $child = $obj->getParentID();
297: $defaulttext = gettext('inherited');
298: $filesmask = 'news';
299: break;
300: }
301: $curdir = getcwd();
302: chdir($path);
303: $files = safe_glob($filesmask . '*.php');
304: chdir($curdir);
305:
306: if ($child) {
307: $defaultlayout = checkParentLayouts($obj, $type);
308: $defaultlayout = $defaultlayout['data'];
309: }
310: if ($defaultlayout) {
311: $defaultlayout = stripSuffix($defaultlayout);
312: } else {
313: $defaultlayout = $filesmask;
314: }
315:
316: if ($obj->transient) {
317: $getlayout = false;
318: } else {
319: $getlayout = query_single_row("SELECT * FROM " . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND `type` = "' . $type . '"');
320: }
321: if (!$child && ($key = array_search($filesmask . '.php', $files)) !== false) {
322: unset($files[$key]);
323: }
324: foreach ($files as $file) {
325: $file = filesystemToInternal($file);
326: $list[stripSuffix($file)] = $file;
327: }
328: ksort($list);
329:
330: $html = $text;
331: if (count($files) != 0) {
332: $html .= '<select id="' . $type . $prefix . '" name="' . $prefix . $type . '">' . "\n";
333: if (is_array($getlayout)) {
334: $selectedlayout = $getlayout['data'];
335: } else {
336: $selectedlayout = '';
337: }
338: $html .= '<option value=""' . ($selectedlayout == '' ? ' selected="selected"' : '') . ' style="background-color:LightGray" >*' . $defaulttext . '* (' . $defaultlayout . ')</option>' . "\n";
339: foreach ($list as $display => $file) {
340: $html .= '<option value="' . html_encode($file) . '"' . ($selectedlayout == $file ? ' selected="selected"' : '') . '>' . $display . '</option>' . "\n";
341: }
342: $html .= '</select>' . "\n";
343: } else {
344: $html = '<p class="no_extra">' . sprintf(gettext('No extra <em>%s</em> theme pages available'), $filesmask) . '</p>' . "\n";
345: }
346: return $html;
347: }
348:
349: 350: 351: 352: 353: 354: 355:
356: function getLayout($path) {
357: global $_zp_gallery, $_zp_gallery_page, $_zp_current_image, $_zp_current_album, $_zp_current_zenpage_page, $_zp_current_zenpage_news, $_zp_current_category, $_zp_current_search;
358: if ($path) {
359: $themepath = THEMEFOLDER . '/' . $_zp_gallery->getCurrentTheme() . '/';
360: $getlayout = false;
361: switch ($_zp_gallery_page) {
362: case 'album.php':
363: if (getOption('multiple_layouts_albums')) {
364: $getlayout = getSelectedLayout($_zp_current_album, 'multiple_layouts_albums');
365: }
366: break;
367: case 'image.php':
368: if (getOption('multiple_layouts_images')) {
369: $currentalbumname = $_zp_current_album->name;
370: if (in_context(ZP_SEARCH_LINKED) && !in_context(ZP_ALBUM_LINKED)) {
371: if (!$album = $_zp_current_search->getDynamicAlbum()) {
372: $album = $_zp_current_album;
373: }
374: } else {
375: $getlayout = getSelectedLayout($_zp_current_image, 'multiple_layouts_images');
376: $album = $_zp_current_album;
377: }
378: if ($album && !$getlayout) {
379: $getlayout = checkLayoutUseForImages($album);
380: }
381: }
382: break;
383: case 'pages.php':
384: if (getOption('multiple_layouts_pages')) {
385: $getlayout = getSelectedLayout($_zp_current_zenpage_page, 'multiple_layouts_pages');
386: }
387: break;
388: case 'news.php':
389: if (getOption('multiple_layouts_news_categories') && in_context(ZP_ZENPAGE_NEWS_CATEGORY)) {
390: $getlayout = getSelectedLayout($_zp_current_category, 'multiple_layouts_news_categories');
391: } elseif (getOption('multiple_layouts_news') && in_context(ZP_ZENPAGE_SINGLE)) {
392: $getlayout = getSelectedLayout($_zp_current_zenpage_news, 'multiple_layouts_news');
393: }
394: break;
395: }
396: if ($getlayout && $getlayout['data'] && file_exists(internalToFilesystem(SERVERPATH . '/' . $themepath . $getlayout['data']))) {
397: return $themepath . $getlayout['data'];
398: }
399: }
400: return $path;
401: }
402:
403: 404: 405: 406: 407: 408: 409: 410:
411: function saveLayoutSelection($message, $obj) {
412: $selectedlayout = '';
413: $type = 'multiple_layouts_' . $obj->table;
414: if (isset($_POST[$type])) {
415: $selectedlayout = sanitize($_POST[$type]);
416: $table = $obj->table;
417: $exists = query_single_row("SELECT * FROM " . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND `type` = "' . $type . '"');
418: if ($selectedlayout) {
419: if ($exists) {
420: $query = query('UPDATE ' . prefix('plugin_storage') . ' SET `aux`=' . $obj->getID() . ', `data`=' . db_quote($selectedlayout) . ' WHERE `id`=' . $exists['id']);
421: } else {
422: $query = query('INSERT INTO ' . prefix('plugin_storage') . ' (type,aux,data) VALUES (' . db_quote($type) . ', ' . $obj->getID() . ', ' . db_quote($selectedlayout) . ')');
423: }
424: } else {
425: if ($exists) {
426: $query = query('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `id`=' . $exists['id']);
427: } else {
428: $query = true;
429: }
430: }
431: if (!$query) {
432: $message .= '<p class="errorbox">' . sprintf(gettext('Query failure: %s'), db_error()) . '</p>';
433: }
434: }
435: return $message;
436: }
437:
438: 439: 440: 441: 442: 443:
444: function saveZenphotoLayoutSelection($obj, $prefix) {
445: $cssIDappend = '';
446: $selectedlayout = '';
447: $titlelink = '';
448: $table = $obj->table;
449: $type = 'multiple_layouts_' . $table;
450: if (isset($_POST[$prefix . $type])) {
451: $selectedlayout = sanitize($_POST[$prefix . $type]);
452: $exists = query_single_row("SELECT * FROM " . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND `type` = "' . $type . '"');
453: if ($selectedlayout) {
454: if ($exists) {
455: $query = query('UPDATE ' . prefix('plugin_storage') . ' SET `aux`=' . $obj->getID() . ', `data`=' . db_quote($selectedlayout) . ' WHERE `id`=' . $exists['id']);
456: } else {
457: $query = query('INSERT INTO ' . prefix('plugin_storage') . ' (type,aux,data) VALUES ("' . $type . '", ' . $obj->getID() . ', ' . db_quote($selectedlayout) . ')');
458: }
459: } else {
460: if ($exists) {
461: $query = query('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `id`=' . $exists['id']);
462: } else {
463: $query = true;
464: }
465: }
466: if ($table == 'albums') {
467: if (isset($_POST['layout_selector_resetimagelayouts'])) {
468: $result = query_full_array('SELECT `id` FROM ' . prefix('images') . ' WHERE `albumid`=' . $obj->getID());
469: if ($result) {
470: $imagelist = '';
471: foreach ($result as $row) {
472: $imagelist .= '`aux`=' . $row['id'] . ' OR ';
473: }
474: $query = query($sql = 'DELETE FROM ' . prefix('plugin_storage') . ' WHERE `type`="multiple_layouts_images" AND (' . substr($imagelist, 0, -4) . ')', false);
475: }
476: }
477: $exists = query_single_row("SELECT * FROM " . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND `type` = "multiple_layouts_albums_images"');
478: $selectedlayout = isset($_POST[$prefix . 'multiple_layouts_albums_images']) ? sanitize($_POST[$prefix . 'multiple_layouts_albums_images']) : NULL;
479: if ($selectedlayout) {
480: if ($exists) {
481: $query = query('UPDATE ' . prefix('plugin_storage') . ' SET `aux`=' . $obj->getID() . ', `data`=' . db_quote($selectedlayout) . ' WHERE `id`=' . $exists['id']);
482: } else {
483: $query = query('INSERT INTO ' . prefix('plugin_storage') . ' (type,aux,data) VALUES ("multiple_layouts_albums_images", ' . $obj->getID() . ', ' . db_quote($selectedlayout) . ')');
484: }
485: } else {
486: if ($exists) {
487: $query = query('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `id`=' . $exists['id']);
488: } else {
489: $query = true;
490: }
491: }
492: }
493: }
494: return $obj;
495: }
496:
497: 498: 499: 500: 501: 502: 503:
504: function deleteLayoutSelection($allow, $obj) {
505: $type = 'multiple_layouts_' . $obj->table;
506: if (getOption($type)) {
507: $query = query('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND type = "' . $type . '"', false);
508: if (isAlbumClass($obj)) {
509: $result = query_single_row('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND type = "multiple_layouts_albums_images"', false);
510: }
511: }
512: return $allow;
513: }
514:
515: 516: 517: 518: 519: 520:
521: function copyLayoutSelection($newid, $obj) {
522: $type = 'multiple_layouts_' . $obj->table;
523: if (getOption($type)) {
524: $result = query_single_row('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND type = "' . $type . '"', false);
525: if ($result) {
526: $query = query('INSERT INTO ' . prefix('plugin_storage') . ' (type,aux,data) VALUES ("' . $result['type'] . '", ' . $newid . ', ' . db_quote($result['data']) . ')');
527: }
528: if (isAlbumClass($obj)) {
529: $result = query_single_row('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `aux` = ' . $obj->getID() . ' AND type = "multiple_layouts_albums_images"', false);
530: if ($result) {
531: $query = query('INSERT INTO ' . prefix('plugin_storage') . ' (type,aux,data) VALUES ("multiple_layouts_albums_images", ' . $newid . ', ' . db_quote($result['data']) . ')');
532: }
533: }
534: }
535: return $newid;
536: }
537:
538: ?>