1<?php
2// Copyright (C) 2016 Combodo SARL
3//
4//   This file is part of iTop.
5//
6//   iTop is free software; you can redistribute it and/or modify
7//   it under the terms of the GNU Affero General Public License as published by
8//   the Free Software Foundation, either version 3 of the License, or
9//   (at your option) any later version.
10//
11//   iTop is distributed in the hope that it will be useful,
12//   but WITHOUT ANY WARRANTY; without even the implied warranty of
13//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14//   GNU Affero General Public License for more details.
15//
16//   You should have received a copy of the GNU Affero General Public License
17//   along with iTop. If not, see <http://www.gnu.org/licenses/>
18
19
20/**
21 * Base class to hold the value managed by CustomFieldsHandler
22 *
23 * @copyright   Copyright (C) 2016 Combodo SARL
24 * @license     http://opensource.org/licenses/AGPL-3.0
25 */
26
27class ormCustomFieldsValue
28{
29	protected $oHostObject;
30	protected $sAttCode;
31	protected $aCurrentValues;
32
33	/**
34	 * @param DBObject $oHostObject
35	 * @param $sAttCode
36	 */
37	public function __construct(DBObject $oHostObject, $sAttCode, $aCurrentValues = null)
38	{
39		$this->oHostObject = $oHostObject;
40		$this->sAttCode = $sAttCode;
41		$this->aCurrentValues = $aCurrentValues;
42	}
43
44	public function GetValues()
45	{
46		return $this->aCurrentValues;
47	}
48
49	/**
50	 * Wrapper used when the only thing you have is the value...
51	 * @return \Combodo\iTop\Form\Form
52	 */
53	public function GetForm()
54	{
55		$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
56		return $oAttDef->GetForm($this->oHostObject);
57	}
58
59	public function GetAsHTML($bLocalize = true)
60	{
61		$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
62		$oHandler = $oAttDef->GetHandler($this->GetValues());
63		return $oHandler->GetAsHTML($this->aCurrentValues, $bLocalize);
64	}
65
66	public function GetAsXML($bLocalize = true)
67	{
68		$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
69		$oHandler = $oAttDef->GetHandler($this->GetValues());
70		return $oHandler->GetAsXML($this->aCurrentValues, $bLocalize);
71	}
72
73	public function GetAsCSV($sSeparator = ',', $sTextQualifier = '"', $bLocalize = true)
74	{
75		$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
76		$oHandler = $oAttDef->GetHandler($this->GetValues());
77		return $oHandler->GetAsCSV($this->aCurrentValues, $sSeparator, $sTextQualifier, $bLocalize);
78	}
79
80	/**
81	 * Get various representations of the value, for insertion into a template (e.g. in Notifications)
82	 * @param $value mixed The current value of the field
83	 * @param $sVerb string The verb specifying the representation of the value
84	 * @param $bLocalize bool Whether or not to localize the value
85	 */
86	public function GetForTemplate($sVerb, $bLocalize = true)
87	{
88		$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
89		$oHandler = $oAttDef->GetHandler($this->GetValues());
90		return $oHandler->GetForTemplate($this->aCurrentValues, $sVerb, $bLocalize);
91	}
92
93	/**
94	 * @param ormCustomFieldsValue $fellow
95	 * @return bool
96	 */
97	public function Equals(ormCustomFieldsValue $oReference)
98	{
99		$oAttDef = MetaModel::GetAttributeDef(get_class($this->oHostObject), $this->sAttCode);
100		$oHandler = $oAttDef->GetHandler($this->GetValues());
101		return $oHandler->CompareValues($this->aCurrentValues, $oReference->aCurrentValues);
102	}
103}
104