<?php
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id: wikiplugin_annotation.php 58201 2016-04-06 14:46:22Z jonnybradley $

function wikiplugin_annotation_info()
{
	return array(
		'name' => tra('Image Annotation'),
		'documentation' => 'PluginAnnotation',
		'description' => tra('Annotate an image'),
		'prefs' => array('wikiplugin_annotation'),
		'body' => tra('Autogenerated content. Leave blank initially.'),
		'filter' => 'striptags',
		'iconname' => 'edit',
		'introduced' => 2,
		'tags' => array( 'basic' ),
		'format' => 'html',
		'params' => array(
			'src' => array(
				'required' => true,
				'name' => tra('Location'),
				'description' => tra('Absolute URL to the image, relative path from Tiki site root or an image from the file gallery <code>display1</code>.'),
				'filter' => 'url',
				'default' => '',
				'since' => '3.0',
			),
			'width' => array(
				'required' => true,
				'name' => tra('Width'),
				'description' => tra('Image width in pixels.'),
				'filter' => 'digits',
				'default' => '',
				'since' => '3.0',
			),
			'height' => array(
				'required' => true,
				'name' => tra('Height'),
				'description' => tra('Image height in pixels.'),
				'filter' => 'digits',
				'default' => '',
				'since' => '3.0',
			),
			'align' => array(
				'required' => false,
				'name' => tra('Alignment'),
				'description' => tra('Image alignment.'),
				'filter' => 'alpha',
				'advanced' => true,
				'default' => 'left',
				'since' => '2.0',
				'options' => array(
					array('text' => '', 'value' => ''), 
					array('text' => tra('Left'), 'value' => 'left'), 
					array('text' => tra('Right'), 'value' => 'right'), 
					array('text' => tra('Center'), 'value' => 'center'), 
				),
			),
			'class' => array(
				'required' => false,
				'name' => tra('CSS Class'),
				'description' => tra('Class of the containing DIV element.'),
				'filter' => 'text',
				'default' => '',
				'since' => '15.0',
				'advanced' => true,
			),
			'showlist' => array(
				'required' => false,
				'name' => tra('Show List'),
				'description' => tra('Show the list of annotations below the image.') . ' ' . tra('(y/n)'),
				'filter' => 'alpha',
				'default' => 'y',
				'since' => '15.0',
				'advanced' => true,
				'options' => array(
					array('text' => '', 'value' => ''),
					array('text' => tra('Yes'), 'value' => 'y'),
					array('text' => tra('No'), 'value' => 'n'),
				),
			),
			'showlink' => array(
				'required' => false,
				'name' => tra('Show Link'),
				'description' => tra('Show the link below the label in the popups.') . ' ' . tra('(y/n)'),
				'filter' => 'alpha',
				'default' => 'n',
				'since' => '15.0',
				'advanced' => true,
				'options' => array(
					array('text' => '', 'value' => ''),
					array('text' => tra('Yes'), 'value' => 'y'),
					array('text' => tra('No'), 'value' => 'n'),
				),
			),
		)
	);
}

function wikiplugin_annotation($data, $params)
{
	global $page, $tiki_p_edit;
	$headerlib = TikiLib::lib('header');

	$defaults = array();
	$plugininfo = wikiplugin_annotation_info();
	foreach ($plugininfo['params'] as $key => $param) {
		$defaults["$key"] = $param['default'];
	}
	$params = array_merge($defaults, $params);

	$annotations = array();
	foreach ( explode("\n", $data) as $line ) {
		$line = trim($line);
		if ( empty( $line ) )
			continue;

		if ( preg_match("/^\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*,\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)(.*)\[(.*)\]$/", $line, $parts) ) {
			$parts = array_map('trim', $parts);
			list( $full, $x1, $y1, $x2, $y2, $label, $target ) = $parts;

			$annotations[] = array(
				'x1' => $x1,
				'y1' => $y1,
				'x2' => $x2,
				'y2' => $y2,
				'value' => $label,
				'target' => $target,
			);
		}
	}

	$annotations = json_encode($annotations);

	$headerlib->add_jsfile('lib/jquery_tiki/wikiplugin-annotation.js');

	static $uid = 0;
	$uid++;
	$cid = 'container-annotation-' . $uid;

	$labelSave = tra('Save changes to annotations');
	$message = tra('Image annotations changed.');
	
	if ( $tiki_p_edit == 'y' ) {
		$editableStr = tra('Editable');

		$form = <<<FORM
<form method="post" action="tiki-wikiplugin_edit.php" class="form save-annotations">
	<div style="display:none">
		<input type="hidden" name="page" value="$page"/>
		<input type="hidden" name="type" value="annotation"/>
		<input type="hidden" name="index" value="$uid"/>
		<input type="hidden" name="message" value="$message"/>
		<textarea id="$cid-content" name="content"></textarea>
	</div>
	<div class="form-group">
		<input type="submit" class="btn btn-default btn-sm" value="$labelSave"/>
		<label>
			<input type="checkbox" id="$cid-editable">
			{$editableStr}
		</label>
	</div>
</form>
FORM;
	}
	else
		$form = '';

	// inititalise the annotations
	$showlink = $params['showlink'] === 'y' ? 'true' : 'false';

	$headerlib->add_jq_onready('$("#' . $cid . '").imageAnnotation(' . $annotations . ', ' . $showlink . ');');

	$smarty = TikiLib::lib('smarty');
	$smarty->loadPlugin('smarty_function_icon');
	$close = smarty_function_icon(['name' => 'close'], $smarty);
	$delete = smarty_function_icon(['name' => 'trash'], $smarty);

	$labelStr = tra('Label');
	$linkStr = tra('Link');
	$saveStr = tra('Save');
	$closeStr = tra('Close');
	$removeStr = tra('Remove');

	if ($tiki_p_edit == 'y') {
		$editor_form = <<<EDITORFORM
		<div class="editor panel">
			<div class="panel-body">
				<form method="post" action="#" class="form-horizontal">
					<div class="form-group">
						<label style="width:100%">
							<span class="col-sm-3">$labelStr</span>
							<span class="col-sm-9"><textarea name="label" class="form-control"></textarea></span>
						</label>
					</div>
					<div class="form-group">
						<label style="width:100%">
							<span class="col-sm-3">$linkStr</span>
							<span class="col-sm-9"><input type="text" name="link" class="form-control"></span>
						</label>
					</div>
					<div class="form-group">
						<div class="col-sm-9 col-sm-offset-3">
							<input type="submit" class="btn btn-default btn-sm" value="$saveStr">
							<div class="pull-right">
								<a class="btn btn-default btn-sm minimize" href="#" title="$closeStr">$close</a>
								<a class="btn btn-default btn-sm delete" href="#" title="$removeStr">$delete</a>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
EDITORFORM;
	} else {
		$editor_form = '';
	}

	if ($params['showlist'] === 'y') {
		$list_div = '<div class="list-box"><div>';
	} else {
		$list_div = '';
	}

	return <<<ANNOTATION
<div class="wp-annotation {$params['class']}">
	<div id="$cid" style="background:url({$params['src']}); width:{$params['width']}px; height:{$params['height']}px;">
{$editor_form}
	</div>
	{$list_div}
	{$form}
</div>
ANNOTATION;
}
