1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 Bharat Mediratta
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21/**
22 * @package Rating
23 * @author  Don Seiler <don@seiler.us>
24 * @version $Revision: 17580 $
25 */
26class RatingCallbacks {
27
28    function callback($params, &$smarty, $callback, $userId) {
29	global $gallery;
30
31	switch ($callback) {
32	case 'LoadRating':
33	    $itemId = $params['itemId'];
34	    list ($ret, $item) = GalleryCoreApi::loadEntitiesById($itemId, 'GalleryItem');
35	    if ($ret) {
36		return $ret;
37	    }
38
39	    list ($ret, $hasPermission) =
40		GalleryCoreApi::hasItemPermission($item->getId(), 'core.view');
41	    if ($ret) {
42		return $ret;
43	    }
44	    if (!$hasPermission) {
45		/* Avoid information disclosure, act as if the item didn't exist. */
46		return GalleryCoreApi::error(ERROR_MISSING_OBJECT);
47	    }
48
49	    if (GalleryUtilities::isA($item, 'GalleryAlbumItem')) {
50		$albumId = $itemId;
51		list ($ret, $allowAlbumRating) =
52		    GalleryCoreApi::getPluginParameter('module', 'rating', 'allowAlbumRating');
53		if ($ret) {
54		    return $ret;
55		}
56		if (!$allowAlbumRating) {
57		    return null;
58		}
59	    } else {
60		$albumId = $item->getParentId();
61	    }
62
63	    list ($ret, $enabled) =
64		GalleryCoreApi::getPluginParameter('module', 'rating', 'enabled', $albumId);
65	    if ($ret) {
66		return $ret;
67	    }
68	    if (!$enabled) {
69		return null;
70	    }
71
72	    list ($ret, $permission) =
73		GalleryCoreApi::hasItemPermission($itemId, 'rating.view', $userId);
74	    if ($ret) {
75		return $ret;
76	    }
77	    if (!$permission) {
78		return null;
79	    }
80
81	    GalleryCoreApi::requireOnce('modules/rating/classes/RatingHelper.class');
82	    list ($ret, $RatingData) = RatingHelper::fetchRatings(array($itemId), $userId);
83	    if ($ret) {
84		return $ret;
85	    }
86
87	    list ($ret, $permission) =
88		GalleryCoreApi::hasItemPermission($itemId, 'rating.add', $userId);
89	    if ($ret) {
90		return $ret;
91	    }
92	    $RatingData[$itemId]['canRate'] = ($permission ? true : false);
93
94	    $session =& $gallery->getSession();
95	    $authToken = $session->isPersistent() ? $session->getAuthToken() : '';
96	    $RatingSummary = array('ratingValues' => array(1, 2, 3, 4, 5),
97				   'firstCall' => true, 'authToken' => $authToken);
98
99	    $block =& $smarty->_tpl_vars['block'];
100	    $block['rating']['RatingData'] = $RatingData[$itemId];
101	    $block['rating']['RatingSummary'] = $RatingSummary;
102
103	    return null;
104	}
105
106	return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
107    }
108}
109?>
110