1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2007 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24/**
25* This class represents a numeric style property in a property form.
26*
27* @author Alex Killing <alex.killing@gmx.de>
28* @version $Id$
29* @ingroup	ServicesForm
30*/
31class ilNumericStyleValueInputGUI extends ilFormPropertyGUI
32{
33    /**
34     * @var ilObjUser
35     */
36    protected $user;
37
38    protected $value;
39    protected $allowpercentage = true;
40
41    /**
42    * Constructor
43    *
44    * @param	string	$a_title	Title
45    * @param	string	$a_postvar	Post Variable
46    */
47    public function __construct($a_title = "", $a_postvar = "")
48    {
49        global $DIC;
50
51        $this->lng = $DIC->language();
52        $this->user = $DIC->user();
53        parent::__construct($a_title, $a_postvar);
54        $this->setType("style_numeric");
55    }
56
57    /**
58    * Set Value.
59    *
60    * @param	string	$a_value	Value
61    */
62    public function setValue($a_value)
63    {
64        $this->value = $a_value;
65    }
66
67    /**
68    * Get Value.
69    *
70    * @return	string	Value
71    */
72    public function getValue()
73    {
74        return $this->value;
75    }
76
77    /**
78    * Set Allow Percentage.
79    *
80    * @param	boolean	$a_allowpercentage	Allow Percentage
81    */
82    public function setAllowPercentage($a_allowpercentage)
83    {
84        $this->allowpercentage = $a_allowpercentage;
85    }
86
87    /**
88    * Get Allow Percentage.
89    *
90    * @return	boolean	Allow Percentage
91    */
92    public function getAllowPercentage()
93    {
94        return $this->allowpercentage;
95    }
96
97    /**
98    * Check input, strip slashes etc. set alert, if input is not ok.
99    *
100    * @return	boolean		Input ok, true/false
101    */
102    public function checkInput()
103    {
104        $lng = $this->lng;
105
106        $num_value = $_POST[$this->getPostVar()]["num_value"] =
107            trim(ilUtil::stripSlashes($_POST[$this->getPostVar()]["num_value"]));
108        $num_unit = $_POST[$this->getPostVar()]["num_unit"] =
109            trim(ilUtil::stripSlashes($_POST[$this->getPostVar()]["num_unit"]));
110
111        if ($this->getRequired() && trim($num_value) == "") {
112            $this->setAlert($lng->txt("msg_input_is_required"));
113
114            return false;
115        }
116
117        if (!is_numeric($num_value) && $num_value != "") {
118            $this->setAlert($lng->txt("sty_msg_input_must_be_numeric"));
119
120            return false;
121        }
122
123        if (trim($num_value) != "") {
124            $this->setValue($num_value . $num_unit);
125        }
126
127        return true;
128    }
129
130    /**
131    * Insert property html
132    */
133    public function insert(&$a_tpl)
134    {
135        $tpl = new ilTemplate("tpl.prop_style_numeric.html", true, true, "Services/Style/Content");
136
137        $tpl->setVariable("POSTVAR", $this->getPostVar());
138
139        $unit_options = ilObjStyleSheet::_getStyleParameterNumericUnits(!$this->getAllowPercentage());
140
141        $value = strtolower(trim($this->getValue()));
142
143        $current_unit = "";
144        foreach ($unit_options as $u) {
145            if (substr($value, strlen($value) - strlen($u)) == $u) {
146                $current_unit = $u;
147            }
148        }
149        $tpl->setVariable(
150            "VAL_NUM",
151            substr($value, 0, strlen($value) - strlen($current_unit))
152        );
153        if ($current_unit == "") {
154            $current_unit = "px";
155        }
156
157        foreach ($unit_options as $option) {
158            $tpl->setCurrentBlock("unit_option");
159            $tpl->setVariable("VAL_UNIT", $option);
160            $tpl->setVariable("TXT_UNIT", $option);
161            if ($current_unit == $option) {
162                $tpl->setVariable("UNIT_SELECTED", 'selected="selected"');
163            }
164            $tpl->parseCurrentBlock();
165        }
166
167        $a_tpl->setCurrentBlock("prop_generic");
168        $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
169        $a_tpl->parseCurrentBlock();
170    }
171
172    /**
173    * Set value by array
174    *
175    * @param	array	$a_values	value array
176    */
177    public function setValueByArray($a_values)
178    {
179        $ilUser = $this->user;
180
181        $this->setValue($a_values[$this->getPostVar()]["num_value"] .
182            $a_values[$this->getPostVar()]["num_unit"]);
183    }
184}
185