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 with all/top/right/bottom/left in a property form.
26*
27* @author Alex Killing <alex.killing@gmx.de>
28* @version $Id$
29* @ingroup	ServicesForm
30*/
31class ilTRBLNumericStyleValueInputGUI 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        $this->dirs = array("all", "top", "bottom", "left", "right");
56    }
57
58    /**
59    * Set All Value.
60    *
61    * @param	string	$a_allvalue	All Value
62    */
63    public function setAllValue($a_allvalue)
64    {
65        $this->allvalue = $a_allvalue;
66    }
67
68    /**
69    * Get All Value.
70    *
71    * @return	string	All Value
72    */
73    public function getAllValue()
74    {
75        return $this->allvalue;
76    }
77
78    /**
79    * Set Top Value.
80    *
81    * @param	string	$a_topvalue	Top Value
82    */
83    public function setTopValue($a_topvalue)
84    {
85        $this->topvalue = $a_topvalue;
86    }
87
88    /**
89    * Get Top Value.
90    *
91    * @return	string	Top Value
92    */
93    public function getTopValue()
94    {
95        return $this->topvalue;
96    }
97
98    /**
99    * Set Bottom Value.
100    *
101    * @param	string	$a_bottomvalue	Bottom Value
102    */
103    public function setBottomValue($a_bottomvalue)
104    {
105        $this->bottomvalue = $a_bottomvalue;
106    }
107
108    /**
109    * Get Bottom Value.
110    *
111    * @return	string	Bottom Value
112    */
113    public function getBottomValue()
114    {
115        return $this->bottomvalue;
116    }
117
118    /**
119    * Set Left Value.
120    *
121    * @param	string	$a_leftvalue	Left Value
122    */
123    public function setLeftValue($a_leftvalue)
124    {
125        $this->leftvalue = $a_leftvalue;
126    }
127
128    /**
129    * Get Left Value.
130    *
131    * @return	string	Left Value
132    */
133    public function getLeftValue()
134    {
135        return $this->leftvalue;
136    }
137
138    /**
139    * Set Right Value.
140    *
141    * @param	string	$a_rightvalue	Right Value
142    */
143    public function setRightValue($a_rightvalue)
144    {
145        $this->rightvalue = $a_rightvalue;
146    }
147
148    /**
149    * Get Right Value.
150    *
151    * @return	string	Right Value
152    */
153    public function getRightValue()
154    {
155        return $this->rightvalue;
156    }
157
158    /**
159    * Set Allow Percentage.
160    *
161    * @param	boolean	$a_allowpercentage	Allow Percentage
162    */
163    public function setAllowPercentage($a_allowpercentage)
164    {
165        $this->allowpercentage = $a_allowpercentage;
166    }
167
168    /**
169    * Get Allow Percentage.
170    *
171    * @return	boolean	Allow Percentage
172    */
173    public function getAllowPercentage()
174    {
175        return $this->allowpercentage;
176    }
177
178    /**
179    * Check input, strip slashes etc. set alert, if input is not ok.
180    *
181    * @return	boolean		Input ok, true/false
182    */
183    public function checkInput()
184    {
185        $lng = $this->lng;
186
187        foreach ($this->dirs as $dir) {
188            $num_value = $_POST[$this->getPostVar()][$dir]["num_value"] =
189                trim(ilUtil::stripSlashes($_POST[$this->getPostVar()][$dir]["num_value"]));
190            $num_unit = $_POST[$this->getPostVar()][$dir]["num_unit"] =
191                trim(ilUtil::stripSlashes($_POST[$this->getPostVar()][$dir]["num_unit"]));
192
193            /*
194            if ($this->getRequired() && trim($num_value) == "")
195            {
196                $this->setAlert($lng->txt("msg_input_is_required"));
197
198                return false;
199            }*/
200
201            if (!is_numeric($num_value) && $num_value != "") {
202                $this->setAlert($lng->txt("sty_msg_input_must_be_numeric"));
203                return false;
204            }
205
206            if (trim($num_value) != "") {
207                switch ($dir) {
208                    case "all": $this->setAllValue($num_value . $num_unit); break;
209                    case "top": $this->setTopValue($num_value . $num_unit); break;
210                    case "bottom": $this->setBottomValue($num_value . $num_unit); break;
211                    case "left": $this->setLeftValue($num_value . $num_unit); break;
212                    case "right": $this->setRightValue($num_value . $num_unit); break;
213                }
214            }
215        }
216
217        return true;
218    }
219
220    /**
221    * Insert property html
222    */
223    public function insert(&$a_tpl)
224    {
225        $lng = $this->lng;
226
227        $layout_tpl = new ilTemplate("tpl.prop_trbl_layout.html", true, true, "Services/Style/Content");
228
229        foreach ($this->dirs as $dir) {
230            $tpl = new ilTemplate("tpl.prop_trbl_style_numeric.html", true, true, "Services/Style/Content");
231            $unit_options = ilObjStyleSheet::_getStyleParameterNumericUnits(!$this->getAllowPercentage());
232
233            switch ($dir) {
234                case "all": $value = strtolower(trim($this->getAllValue())); break;
235                case "top": $value = strtolower(trim($this->getTopValue())); break;
236                case "bottom": $value = strtolower(trim($this->getBottomValue())); break;
237                case "left": $value = strtolower(trim($this->getLeftValue())); break;
238                case "right": $value = strtolower(trim($this->getRightValue())); break;
239            }
240
241            $current_unit = "";
242            foreach ($unit_options as $u) {
243                if (substr($value, strlen($value) - strlen($u)) == $u) {
244                    $current_unit = $u;
245                }
246            }
247            $disp_val = substr($value, 0, strlen($value) - strlen($current_unit));
248            if ($current_unit == "") {
249                $current_unit = "px";
250            }
251
252            foreach ($unit_options as $option) {
253                $tpl->setCurrentBlock("unit_option");
254                $tpl->setVariable("VAL_UNIT", $option);
255                $tpl->setVariable("TXT_UNIT", $option);
256                if ($current_unit == $option) {
257                    $tpl->setVariable("UNIT_SELECTED", 'selected="selected"');
258                }
259                $tpl->parseCurrentBlock();
260            }
261
262            $tpl->setVariable("POSTVAR", $this->getPostVar());
263            $tpl->setVariable("VAL_NUM", $disp_val);
264            $tpl->setVariable("TXT_DIR", $lng->txt("sty_$dir"));
265            $tpl->setVariable("DIR", $dir);
266
267            $layout_tpl->setVariable(strtoupper($dir), $tpl->get());
268        }
269
270        $a_tpl->setCurrentBlock("prop_generic");
271        $a_tpl->setVariable("PROP_GENERIC", $layout_tpl->get());
272        $a_tpl->parseCurrentBlock();
273    }
274
275    /**
276    * Set value by array
277    *
278    * @param	array	$a_values	value array
279    */
280    public function setValueByArray($a_values)
281    {
282        $ilUser = $this->user;
283
284        $this->setAllValue($a_values[$this->getPostVar()]["all"]["num_value"] .
285            $a_values[$this->getPostVar()]["all"]["num_unit"]);
286        $this->setBottomValue($a_values[$this->getPostVar()]["bottom"]["num_value"] .
287            $a_values[$this->getPostVar()]["bottom"]["num_unit"]);
288        $this->setTopValue($a_values[$this->getPostVar()]["top"]["num_value"] .
289            $a_values[$this->getPostVar()]["top"]["num_unit"]);
290        $this->setLeftValue($a_values[$this->getPostVar()]["left"]["num_value"] .
291            $a_values[$this->getPostVar()]["left"]["num_unit"]);
292        $this->setRightValue($a_values[$this->getPostVar()]["right"]["num_value"] .
293            $a_values[$this->getPostVar()]["right"]["num_unit"]);
294    }
295}
296