1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Tiki_Editable_Value
9{
10	private $inner;
11	private $layout = 'inline';
12	private $fieldFetchUrl;
13	private $objectStoreUrl;
14
15	function __construct($html, array $parameters)
16	{
17		$this->inner = $html;
18
19		if (! empty($parameters['layout']) && in_array($parameters['layout'], ['inline', 'block'])) {
20			$this->layout = $parameters['layout'];
21		}
22
23		if (empty($parameters['field_fetch_url'])) {
24			throw new Exception(tr('Internal error: mandatory parameter field_fetch_url is missing'));
25		}
26
27		if (empty($parameters['object_store_url'])) {
28			throw new Exception(tr('Internal error: mandatory parameter object_store_url is missing'));
29		}
30
31		$this->fieldFetchUrl = $parameters['field_fetch_url'];
32		$this->objectStoreUrl = $parameters['object_store_url'];
33	}
34
35	function __toString()
36	{
37		global $prefs;
38
39		if ($prefs['ajax_inline_edit'] != 'y') {
40			return $this->inner;
41		}
42
43		$tag = ($this->layout == 'block') ? 'div' : 'span';
44		$fieldFetch = htmlspecialchars($this->fieldFetchUrl);
45		$objectStore = htmlspecialchars($this->objectStoreUrl);
46
47		$value = $this->inner;
48		if (trim(strip_tags($value)) == '') {
49			// When the value is empty, make sure it becomes visible/clickable
50			$value .= '&nbsp;';
51		}
52		$smarty = TikiLib::lib('smarty');
53		$smarty->loadPlugin('smarty_function_icon');
54		return "<$tag class=\"editable-inline\" data-field-fetch-url=\"$fieldFetch\" data-object-store-url=\"$objectStore\">$value " . smarty_function_icon(['name' => 'edit', 'iclass' => 'ml-2'], $smarty->getEmptyInternalTemplate()) . "</$tag>";
55	}
56}
57