1: <?php
2:
3: 4: 5:
6: class favorites extends AlbumBase {
7:
8: var $imageSortDirection;
9: var $albumSortDirection;
10: var $imageSortType;
11: var $albumSortType;
12: var $list = array('');
13: var $owner;
14: var $instance = '';
15:
16: function __construct($user) {
17: $this->table = 'albums';
18: $this->name = $user;
19: $this->owner = $user;
20: $this->setTitle(get_language_string(getOption('favorites_title')));
21: $this->setDesc(get_language_string(getOption('favorites_desc')));
22: $this->imageSortDirection = getOption('favorites_image_sort_direction');
23: $this->albumSortDirection = getOption('favorites_album_sort_direction');
24: $this->imageSortType = getOption('favorites_image_sort_type');
25: $this->albumSortType = getOption('favorites_album_sort_type');
26: $list = query_full_array('SELECT `aux` FROM ' . prefix('plugin_storage') . ' WHERE `type`="favorites" AND `aux` REGEXP ' . db_quote('[[:<:]]' . $user . '[[:>:]]'));
27: foreach ($list as $aux) {
28: $instance = getSerializedArray($aux['aux']);
29: if (isset($instance[1])) {
30: $this->list[$instance[1]] = $instance[1];
31: }
32: }
33: }
34:
35: protected function getInstance() {
36: if ($this->instance) {
37: return serialize(array($this->owner, $this->instance));
38: } else {
39: return $this->owner;
40: }
41: }
42:
43: function getList() {
44: return $this->list;
45: }
46:
47: function getOwner() {
48: return $this->owner;
49: }
50:
51: function addImage($img) {
52: $folder = $img->imagefolder;
53: $filename = $img->filename;
54: $sql = 'INSERT INTO ' . prefix('plugin_storage') . ' (`type`, `aux`,`data`) VALUES ("favorites",' . db_quote($this->getInstance()) . ',' . db_quote(serialize(array('type' => 'images', 'id' => $folder . '/' . $filename))) . ')';
55: query($sql);
56: zp_apply_filter('favoritesHandler_action', 'add', $img, $this->name);
57: }
58:
59: function removeImage($img) {
60: $folder = $img->imagefolder;
61: $filename = $img->filename;
62: $sql = 'DELETE FROM ' . prefix('plugin_storage') . ' WHERE `type`="favorites" AND `aux`=' . db_quote($this->getInstance()) . ' AND `data`=' . db_quote(serialize(array('type' => 'images', 'id' => $folder . '/' . $filename)));
63: query($sql);
64: zp_apply_filter('favoritesHandler_action', 'remove', $img, $this->name);
65: }
66:
67: function addAlbum($alb) {
68: $folder = $alb->name;
69: $sql = 'INSERT INTO ' . prefix('plugin_storage') . ' (`type`, `aux`,`data`) VALUES ("favorites",' . db_quote($this->getInstance()) . ',' . db_quote(serialize(array('type' => 'albums', 'id' => $folder))) . ')';
70: query($sql);
71: zp_apply_filter('favoritesHandler_action', 'add', $alb, $this->name);
72: }
73:
74: function removeAlbum($alb) {
75: $folder = $alb->name;
76: $sql = 'DELETE FROM ' . prefix('plugin_storage') . ' WHERE `type`="favorites" AND `aux`=' . db_quote($this->getInstance()) . ' AND `data`=' . db_quote(serialize(array('type' => 'albums', 'id' => $folder)));
77: query($sql);
78: zp_apply_filter('favoritesHandler_action', 'remove', $alb, $this->name);
79: }
80:
81: static function getWatchers($obj) {
82: switch ($obj->table) {
83: case 'images':
84: $folder = $obj->imagefolder;
85: $filename = $obj->filename;
86: $sql = 'SELECT DISTINCT `aux` FROM ' . prefix('plugin_storage') . ' WHERE `data`=' . db_quote(serialize(array('type' => 'images', 'id' => $folder . '/' . $filename)));
87: break;
88: case 'albums':
89: $folder = $obj->name;
90: $sql = 'SELECT DISTINCT `aux` FROM ' . prefix('plugin_storage') . ' WHERE `data`=' . db_quote(serialize(array('type' => 'albums', 'id' => $folder)));
91: break;
92: }
93: $watchers = array();
94: $result = query_full_array($sql);
95: if ($result) {
96: foreach ($result as $watch) {
97: $watchers[] = $watch['aux'];
98: }
99: }
100: return $watchers;
101: }
102:
103: static function showWatchers($html, $obj, $prefix) {
104: $watchers = self::getWatchers($obj);
105: if (!empty($watchers)) {
106: natcasesort($watchers);
107: ?>
108: <tr>
109: <td>
110: <?php echo gettext('Users watching:'); ?>
111: </td>
112: <td>
113: <ul class="userlist">
114: <?php
115: foreach ($watchers as $watchee) {
116: ?>
117: <li>
118: <?php echo html_encode($watchee); ?>
119: </li>
120: <?php
121: }
122: ?>
123: </ul>
124: </td>
125: </tr>
126: <?php
127: }
128:
129: return $html;
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139: 140: 141:
142: function getAlbums($page = 0, $sorttype = null, $sortdirection = null, $care = true, $mine = NULL) {
143: global $_zp_gallery;
144: if ($mine || is_null($this->subalbums) || $care && $sorttype . $sortdirection !== $this->lastsubalbumsort) {
145: $results = array();
146: $result = query('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `type`="favorites" AND `aux`=' . db_quote($this->getInstance()) . ' AND `data` LIKE "%s:4:\"type\";s:6:\"albums\";%"');
147: if ($result) {
148: while ($row = db_fetch_assoc($result)) {
149: $data = getSerializedArray($row['data']);
150: $albumobj = newAlbum($data['id'], true, true);
151: if ($albumobj->exists) {
152: $results[$data['id']] = $albumobj->getData();
153: } else {
154: query("DELETE FROM " . prefix('plugin_storage') . ' WHERE `id`=' . $row['id']);
155: }
156: }
157: db_free_result($result);
158: if (is_null($sorttype)) {
159: $sorttype = $this->getSortType('album');
160: }
161: if (is_null($sortdirection)) {
162: if ($this->getSortDirection('album')) {
163: $sortdirection = 'DESC';
164: } else {
165: $sortdirection = '';
166: }
167: }
168: $sortkey = $this->getAlbumSortKey($sorttype);
169: if (($sortkey == '`sort_order`') || ($sortkey == 'RAND()')) {
170: $order = false;
171: } else {
172: if (!is_null($sortdirection)) {
173: $order = strtoupper($sortdirection) == 'DESC';
174: } else {
175: $order = $obj->getSortDirection('album');
176: }
177: }
178: $results = sortByKey($results, $sortkey, $order);
179: $this->subalbums = array_keys($results);
180: $this->lastsubalbumsort = $sorttype . $sortdirection;
181: }
182: }
183: return parent::getAlbums($page);
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
200: function getImages($page = 0, $firstPageCount = 0, $sorttype = null, $sortdirection = null, $care = true, $mine = NULL) {
201: if ($mine || is_null($this->images) || $care && $sorttype . $sortdirection !== $this->lastimagesort) {
202: $this->images = NULL;
203: $images = array();
204: $result = query('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `type`="favorites" AND `aux`=' . db_quote($this->getInstance()) . ' AND `data` LIKE "%s:4:\"type\";s:6:\"images\";%"');
205: if ($result) {
206: while ($row = db_fetch_assoc($result)) {
207: $id = $row['id'];
208: $data = getSerializedArray($row['data']);
209: $imageObj = newImage(NULL, array('folder' => dirname($data['id']), 'filename' => basename($data['id'])), true);
210: if ($imageObj->exists) {
211: $images[] = array_merge(array('folder' => dirname($data['id']), 'filename' => basename($data['id'])), $imageObj->getData());
212: } else {
213: query("DELETE FROM " . prefix('plugin_storage') . ' WHERE `id`=' . $row['id']);
214: }
215: }
216: db_free_result($result);
217: if (is_null($sorttype)) {
218: $sorttype = $this->getSortType();
219: }
220: $sortkey = str_replace('` ', ' ', $this->getImageSortKey($sorttype));
221: if (($sortkey == 'sort_order') || ($sortkey == 'RAND()')) {
222:
223: $order = false;
224: } else {
225: if (!is_null($sortdirection)) {
226: $order = strtoupper($sortdirection) == 'DESC';
227: } else {
228: $order = $this->getSortDirection('image');
229: }
230: }
231: $images = sortByKey($images, $sortkey, $order);
232: $this->images = array();
233: foreach ($images as $data) {
234: $this->images[] = array('folder' => $data['folder'], 'filename' => $data['filename']);
235: }
236: $this->lastimagesort = $sorttype . $sortdirection;
237: }
238: }
239: return parent::getImages($page, $firstPageCount);
240: }
241:
242: static function loadScript($script, $request) {
243: global $_zp_current_admin_obj, $_zp_gallery_page, $_myFavorites, $_zp_current_album, $_zp_conf_vars;
244: if ($_myFavorites && isset($_REQUEST['instance'])) {
245: $_myFavorites->instance = sanitize(rtrim($_REQUEST['instance'], '/'));
246: if ($_myFavorites->instance)
247: $_myFavorites->setTitle($_myFavorites->getTitle() . '[' . $_myFavorites->instance . ']');
248: }
249: if ($_zp_gallery_page == "favorites.php") {
250: if (zp_loggedin()) {
251: $_zp_current_album = $_myFavorites;
252: add_context(ZP_ALBUM);
253: prepareAlbumPage();
254: $_zp_gallery_page = 'favorites.php';
255: } else {
256: $script = false;
257: }
258: }
259: return $script;
260: }
261:
262: static function pageCount($count, $gallery_page, $page) {
263: global $_firstPageImages, $_oneImagePage;
264: if (stripSuffix($gallery_page) == 'favorites') {
265: $albums_per_page = max(1, getOption('albums_per_page'));
266: $pageCount = (int) ceil(getNumAlbums() / $albums_per_page);
267: $imageCount = getNumImages();
268: if ($_oneImagePage) {
269: if ($_oneImagePage === true) {
270: $imageCount = min(1, $imageCount);
271: } else {
272: $imageCount = 0;
273: }
274: }
275: $images_per_page = max(1, getOption('images_per_page'));
276: $count = ($pageCount + (int) ceil(($imageCount - $_firstPageImages) / $images_per_page));
277: if ($count < $page && isset($_POST['addToFavorites']) && !$_POST['addToFavorites']) {
278:
279: global $_zp_page;
280: header('location: ' . FULLWEBPATH . '/' . $this->getLink($_zp_page - 1));
281: exitZP();
282: }
283: }
284: return $count;
285: }
286:
287: static function toolbox($zf) {
288: printFavoritesURL(gettext('Favorites'), '<li>', '</li><li>', '</li>');
289: return $zf;
290: }
291:
292: function getLink($page = NULL, $instance = NULL) {
293: $link = _FAVORITES_ . '/';
294: $link_no = 'index.php?p=favorites';
295: if (is_null($instance))
296: $instance = $this->instance;
297: if ($instance) {
298: $instance = rtrim($instance, '/');
299: $link .= $instance . '/';
300: $link_no .= '&instance=' . $instance;
301: }
302: if ($page > 1) {
303: $link .= $page . '/';
304: $link_no .= '&page=' . $page;
305: }
306: return zp_apply_filter('getLink', rewrite_path($link, $link_no), 'favorites.php', $page);
307: }
308:
309: static function ad_removeButton($obj, $id, $v, $add, $instance, $multi) {
310: global $_myFavorites;
311: $table = $obj->table;
312: if ($v) {
313: $tag = '_add';
314: } else {
315: $tag = '_remove';
316: }
317: if ($instance && $multi) {
318: $add .= '[' . $instance . ']';
319: }
320: ?>
321: <form name="<?php echo $table . $obj->getID(); ?>Favorites_<?php echo $instance . $tag; ?>" class = "<?php echo $table; ?>Favorites<?php echo $tag; ?>" action = "<?php echo html_encode(getRequestURI()); ?>" method = "post" accept-charset = "UTF-8">
322: <input type = "hidden" name = "addToFavorites" value = "<?php echo $v; ?>" />
323: <input type = "hidden" name = "type" value = "<?php echo html_encode($table); ?>" />
324: <input type = "hidden" name = "id" value = "<?php echo html_encode($id); ?>" />
325: <input type = "submit" class = "button buttons" value = "<?php echo $add; ?>" title = "<?php echo $add; ?>"/>
326: <?php
327: if ($v) {
328: if ($multi) {
329: ?>
330: <span class="tagSuggestContainer">
331: <input type="text" name="instance" class="favorite_instance" value="" />
332: </span>
333: <?php
334: }
335: } else {
336: ?>
337: <input type="hidden" name="instance" value="<?php echo $_myFavorites->instance; ?>" />
338: <?php
339: }
340: ?>
341: </form>
342: <?php
343: }
344:
345: }
346: