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 * This ItemAddOption allows the user to pick a watermark to apply to the image upon upload.
23 * @package Watermark
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class WatermarkOption extends ItemAddOption {
29
30    /**
31     * @see ItemAddOption::loadTemplate
32     */
33    function loadTemplate(&$template, &$form, $item) {
34	list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'watermark');
35	if ($ret) {
36	    return array($ret, null, null);
37	}
38
39	$WatermarkOption = array();
40	if (!$params['forceDefaultWatermark'] || empty($params['defaultWatermarkId'])) {
41	    GalleryCoreApi::requireOnce('modules/watermark/classes/WatermarkHelper.class');
42	    list ($ret, $watermarks) = WatermarkHelper::fetchWatermarks();
43	    if ($ret) {
44		return array($ret, null, null);
45	    }
46
47	    $currentId = null;
48	    foreach (array_keys($watermarks) as $id) {
49		if (!isset($currentId)) {
50		    $currentId = $id;
51		}
52		$watermarks[$id] = (array)$watermarks[$id];
53	    }
54
55	    $WatermarkOption['watermarks'] = $watermarks;
56	    $WatermarkOption['currentId'] = $currentId;
57	}
58
59	$template->setVariable('WatermarkOption', $WatermarkOption);
60	return array(null,
61		     'modules/watermark/templates/WatermarkOption.tpl',
62		     'modules_watermark');
63    }
64
65    /**
66     * @see ItemAddOption::isAppropriate
67     */
68    function isAppropriate() {
69	GalleryCoreApi::requireOnce('modules/watermark/classes/WatermarkHelper.class');
70
71	/* Only appropriate if we've got some watermarks to show */
72	list ($ret, $watermarks) = WatermarkHelper::fetchWatermarks();
73	if ($ret) {
74	    return array($ret, null);
75	}
76
77	return array(null, !empty($watermarks));
78    }
79
80    /**
81     * @see ItemAddOption::handleRequestAfterAdd
82     */
83    function handleRequestAfterAdd($form, $items) {
84	list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'watermark');
85	if ($ret) {
86	    return array($ret, null, null);
87	}
88	if ($params['forceDefaultWatermark'] && !empty($params['defaultWatermarkId'])) {
89	    $form['WatermarkOption']['watermarkId'] = $params['defaultWatermarkId'];
90	}
91
92	if (!empty($form['WatermarkOption']['watermarkId'])) {
93	    list ($ret, $watermark) = GalleryCoreApi::loadEntitiesById(
94		$form['WatermarkOption']['watermarkId'], 'WatermarkImage');
95	    if ($ret) {
96		if (!$ret->getErrorCode() & ERROR_MISSING_OBJECT) {
97		    return array($ret, null, null);
98		}
99		$ret = GalleryCoreApi::addEventLogEntry('WATERMARK ERROR',
100		    'Watermark doesn\'t exist', $form['WatermarkOption']['watermarkId']);
101		if ($ret) {
102		    return array($ret, null, null);
103		}
104
105		list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'watermark');
106		if ($ret) {
107		    return array($ret, null, null);
108		}
109		$message =
110		    $module->translate('Error: Missing watermark. Please see the Event Logs '
111				       . 'page in the Site Admin.');
112
113		return array(null, array($message), array());
114	    }
115
116	    foreach ($items as $i => $item) {
117		if (GalleryUtilities::isA($item, 'GalleryPhotoItem')) {
118		    $watermarkArray =
119			array(DERIVATIVE_TYPE_IMAGE_THUMBNAIL => $watermark->getApplyToThumbnail(),
120			      DERIVATIVE_TYPE_IMAGE_RESIZE => $watermark->getApplyToResizes(),
121			      DERIVATIVE_TYPE_IMAGE_PREFERRED => $watermark->getApplyToPreferred());
122		    $ret = WatermarkHelper::watermarkItem($watermark, $item,
123							  $watermark->getXPercentage(),
124							  $watermark->getYPercentage(),
125							  $watermarkArray);
126		    if ($ret) {
127			return array($ret, null, null);
128		    }
129		}
130	    }
131	}
132	return array(null, array(), array());
133    }
134}
135?>
136