<?php
/*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
GalleryCoreApi::requireOnce('modules/imageareas/classes/ImageArea.class');
/*
* Functions to retrieve/add/update/remove ImageAreas
* @package ImageAreas
* @author Paul Hinze <paul.t.hinze@gmail.com>
* @version: $Revision$
*/
class ImageAreasHelper {
/**
* Given an image, returns the HTML necessary to generate the UI for it.
* @param img an ImageAreasImage
*/
function getImageAreasUI($img) {
$content = null;
$content .= "\n<!--module_imageareas-->\n";
// canvas offsets
$canvasOffSetTop = 20;
$canvasOffSetBottom = 0;
$canvasOffSetLeft = 0;
$canvasOffSetRight = 0;
$canvasHeight = $img->getHeight()+$canvasOffSetTop+$canvasOffSetBottom;
$canvasWidth = $img->getWidth()+$canvasOffSetLeft+$canvasOffSetRight;
$content .= '<div id="ia-canvas-id-'.$img->getUUID().'" '.
'class="ia-canvas ia-container-active" '.
'style="width: '.$canvasWidth.'px; height: '.$canvasHeight.'px;">'."\n";
$content .= '<div id="unique-id-'.$img->getUUID().'" '.
'class="ia-container ia-container-active" '.
'style="width: '.$img->getWidth().'px; height: '.$img->getHeight().'px; '.
'top:'.$canvasOffSetTop.'px; left:'.$canvasOffSetLeft.'px;">'."\n";
$content .= '<img src="'.$img->getSrc().'" width="'.$img->getWidth().'"'.
'height="'.$img->getHeight().'" alt="'.$img->getAlt().'" />'."\n";
$content .= '<span class="ia-scalefactor" title="'.$img->getScaleFactor().'"></span>';
list ($ret,
$areaArray) = ImageAreasHelper::
getImageAreas($img->
getItemId());
if ($ret) {
return array($ret,
null);
}
$area = null;
for ($i=0; $i<count($areaArray); $i++) {
$area = $areaArray[$i];
$areaBounds = ImageAreasHelper::boundsToVars($area->getAreaBounds());
$areaTitle = $area->getAreaTitle();
$areaContent = $area->getAreaContent();
$areaId = $area->getAreaId();
// This will kill the page if the content has '--' in it.
//$content .= "\n\n<!-- ******* ANNOTATION $i : $areaContent ********* -->\n";
$content .= <<<EOF
<div class="ia-area" style="left: $areaBounds[0]px; top: $areaBounds[1]px; width:
$areaBounds[2]px; height: $areaBounds[3]px;">
<div class="ia-note">
<span class="ia-note-title">$areaTitle</span>
<span class="ia-note-content">$areaContent</span>
<span class="ia-note-id" title="$areaId"></span>
</div>
<div class="ia-area-innerborder-left"></div>
<div class="ia-area-innerborder-right"></div>
<div class="ia-area-innerborder-top"></div>
<div class="ia-area-innerborder-bottom"></div>
</div>
EOF;
}
$content .= <<<EOF
<div class="ia-controlbar ia-controlbar-active">
<!--span class="ia-controlbar-logo"></span-->
<!--span class="ia-controlbar-credits"></span-->
<span class="ia-controlbar-del-inactive"></span>
<span class="ia-controlbar-edit-inactive"></span>
<span class="ia-controlbar-add-inactive"></span>
<span class="ia-controlbar-toggle-inactive"></span>
</div>
<form class="ia-editbar ia-editbar-inactive" name="ia_editbar" id="ia_editbar">
<div class="ia-editbar-fields">
<p>TITLE:</p>
<input type="input" class="ia-editbar-title" name="title" value="default" />
<input type="hidden" class="ia-editbar-entry_id" name="entry_ID" value="$areaId" />
<input type="hidden" class="ia-editbar-border-color" name="border_color" value="#FE0000" />
</div>
<div class="ia-editbar-fields">
<p>CONTENT:</p>
<textarea class="ia-editbar-content" name="content"></textarea>
</div>
<div class="ia-editbar-fields">
<span class="ia-editbar-ok"></span>
<span class="ia-editbar-cancel"></span>
</div>
</form>
</div>
EOF;
$content .= "\n</div><!--close ia-canvas-->\n";
$content .= "\n<!--module_imageareas-->\n";
return array (null,
$content);
}
function xmldecode($txt) {
return $txt;
}
/**
* Get all ImageAreas for a given itemId.
* @param itemId Gallery2 item
* @return array(GalleryStatus, array of ImageAreas)
*/
function getImageAreas($itemId) {
list ($ret,
$searchResults) = GalleryCoreApi::
getMapEntry("ImageAreaMap",
array('areaId',
'areaTitle',
'areaContent',
'areaBounds'),
array('itemId' =>
$itemId));
if ($ret) {
return array($ret,
null);
}
while ($result = $searchResults->nextResult()) {
$areas[] = new ImageArea($result[0], $itemId, $result[1], $result[2], $result[3]);
}
return array(null,
$areas);
}
function addArea($area) {
$storage =& $gallery->getStorage();
list ($ret,
$newAreaId) =
$storage->
getUniqueId();
if ($ret) {
return $ret;
}
$ret = GalleryCoreApi::addMapEntry('ImageAreaMap',
array('areaId' =>
$newAreaId,
'itemId' => $area->getItemId(),
'areaTitle' => $area->getAreaTitle(),
'areaContent' => $area->getAreaContent(),
'areaBounds' => $area->getAreaBounds()
));
if ($ret) {
return array($ret,
null);
}
return array(null,
$newAreaId);
}
function updateArea($area) {
$storage =& $gallery->getStorage();
return GalleryCoreApi::updateMapEntry('ImageAreaMap',
array('areaId' =>
$area->
getAreaId()),
array( 'areaTitle' =>
$area->
getAreaTitle(),
'areaContent' => $area->getAreaContent(),
'areaBounds' => $area->getAreaBounds()
));
}
function deleteArea($areaId) {
$storage =& $gallery->getStorage();
return GalleryCoreApi::removeMapEntry('ImageAreaMap',
array('areaId' =>
$areaId));
}
/**
* Converts string x1,y1,x2,y2 to array x1,y1,w,h.
* @param bounds a string of the format "x1,y1,x2,y2"
* @returns array(upperleftx, upperlefty, width, height)
*/
function boundsToVars($bounds) {
$ba =
split(',',
$bounds);
return array($ba[0],
$ba[1],
$ba[2] -
$ba[0],
$ba[3] -
$ba[1]);
}
}
?>