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 background position in a property form.
26*
27* @author Alex Killing <alex.killing@gmx.de>
28* @version $Id$
29* @ingroup	ServicesForm
30*/
31class ilBackgroundPositionInputGUI extends ilFormPropertyGUI
32{
33    /**
34     * @var ilObjUser
35     */
36    protected $user;
37
38    protected $value;
39
40    /**
41    * Constructor
42    *
43    * @param	string	$a_title	Title
44    * @param	string	$a_postvar	Post Variable
45    */
46    public function __construct($a_title = "", $a_postvar = "")
47    {
48        global $DIC;
49
50        $this->lng = $DIC->language();
51        $this->user = $DIC->user();
52        parent::__construct($a_title, $a_postvar);
53        $this->setType("border_width");
54        $this->dirs = array("horizontal", "vertical");
55    }
56
57    /**
58    * Set Horizontal Value.
59    *
60    * @param	string	$a_horizontalvalue	Horizontal Value
61    */
62    public function setHorizontalValue($a_horizontalvalue)
63    {
64        $this->horizontalvalue = $a_horizontalvalue;
65    }
66
67    /**
68    * Get Horizontal Value.
69    *
70    * @return	string	Horizontal Value
71    */
72    public function getHorizontalValue()
73    {
74        return $this->horizontalvalue;
75    }
76
77    /**
78    * Set Vertical Value.
79    *
80    * @param	string	$a_verticalvalue	Vertical Value
81    */
82    public function setVerticalValue($a_verticalvalue)
83    {
84        $this->verticalvalue = $a_verticalvalue;
85    }
86
87    /**
88    * Get Vertical Value.
89    *
90    * @return	string	Vertical Value
91    */
92    public function getVerticalValue()
93    {
94        return $this->verticalvalue;
95    }
96
97    /**
98    * Get value
99    */
100    public function getValue()
101    {
102        if ($this->getHorizontalValue() != "") {
103            if ($this->getVerticalValue() != "") {
104                return $this->getHorizontalValue() . " " . $this->getVerticalValue();
105            } else {
106                return $this->getHorizontalValue();
107            }
108        } else {
109            if ($this->getVerticalValue() != "") {
110                return "left " . $this->getVerticalValue();
111            }
112        }
113        return "";
114    }
115
116    /**
117    * Set value
118    */
119    public function setValue($a_val)
120    {
121        $a_val = trim($a_val);
122        $a_val_arr = explode(" ", $a_val);
123        $hor = trim($a_val_arr[0]);
124        $ver = trim($a_val_arr[1]);
125        if ($hor == "center" && $ver == "") {
126            $ver = "center";
127        }
128        $this->setHorizontalValue($hor);
129        $this->setVerticalValue($ver);
130    }
131
132    /**
133    * Check input, strip slashes etc. set alert, if input is not ok.
134    *
135    * @return	boolean		Input ok, true/false
136    */
137    public function checkInput()
138    {
139        $lng = $this->lng;
140
141        foreach ($this->dirs as $dir) {
142            $type = $_POST[$this->getPostVar()][$dir]["type"] =
143                ilUtil::stripSlashes($_POST[$this->getPostVar()][$dir]["type"]);
144            $num_value = $_POST[$this->getPostVar()][$dir]["num_value"] =
145                trim(ilUtil::stripSlashes($_POST[$this->getPostVar()][$dir]["num_value"]));
146            $num_unit = $_POST[$this->getPostVar()][$dir]["num_unit"] =
147                trim(ilUtil::stripSlashes($_POST[$this->getPostVar()][$dir]["num_unit"]));
148            $pre_value = $_POST[$this->getPostVar()][$dir]["pre_calue"] =
149                ilUtil::stripSlashes($_POST[$this->getPostVar()][$dir]["pre_value"]);
150
151            /*
152            if ($this->getRequired() && trim($num_value) == "")
153            {
154                $this->setAlert($lng->txt("msg_input_is_required"));
155
156                return false;
157            }*/
158
159            if (!is_numeric($num_value) && trim($num_value) != "") {
160                $this->setAlert($lng->txt("sty_msg_input_must_be_numeric"));
161                return false;
162            }
163
164            $value = "";
165            if ($type == "numeric") {
166                if ($num_value != "") {
167                    $value = $num_value . $num_unit;
168                }
169            } else {
170                $value = $pre_value;
171            }
172
173            if (trim($value) != "") {
174                switch ($dir) {
175                    case "horizontal": $this->setHorizontalValue($value); break;
176                    case "vertical": $this->setVerticalValue($value); break;
177                }
178            }
179        }
180
181        return true;
182    }
183
184    /**
185    * Insert property html
186    */
187    public function insert(&$a_tpl)
188    {
189        $lng = $this->lng;
190
191        $layout_tpl = new ilTemplate("tpl.prop_hv_layout.html", true, true, "Services/Style/Content");
192
193        foreach ($this->dirs as $dir) {
194            $tpl = new ilTemplate("tpl.prop_background_position.html", true, true, "Services/Style/Content");
195            $unit_options = ilObjStyleSheet::_getStyleParameterNumericUnits();
196            $pre_options = ilObjStyleSheet::_getStyleParameterValues("background-position");
197            $pre_options = $pre_options[$dir];
198            switch ($dir) {
199                case "horizontal": $value = strtolower(trim($this->getHorizontalValue())); break;
200                case "vertical": $value = strtolower(trim($this->getVerticalValue())); break;
201            }
202
203            if (in_array($value, $pre_options)) {
204                $current_type = "pre";
205                $tpl->setVariable("PREDEFINED_SELECTED", 'checked="checked"');
206            } else {
207                $current_type = "unit";
208                $tpl->setVariable("NUMERIC_SELECTED", 'checked="checked"');
209
210                $current_unit = "";
211                foreach ($unit_options as $u) {
212                    if (substr($value, strlen($value) - strlen($u)) == $u) {
213                        $current_unit = $u;
214                    }
215                }
216                $disp_val = substr($value, 0, strlen($value) - strlen($current_unit));
217                if ($current_unit == "") {
218                    $current_unit = "px";
219                }
220                $tpl->setVariable("VAL_NUM", $disp_val);
221            }
222
223            foreach ($unit_options as $option) {
224                $tpl->setCurrentBlock("unit_option");
225                $tpl->setVariable("VAL_UNIT", $option);
226                $tpl->setVariable("TXT_UNIT", $option);
227                if ($current_type == "unit" && $current_unit == $option) {
228                    $tpl->setVariable("UNIT_SELECTED", 'selected="selected"');
229                }
230                $tpl->parseCurrentBlock();
231            }
232
233            foreach ($pre_options as $option) {
234                $tpl->setCurrentBlock("pre_option");
235                $tpl->setVariable("VAL_PRE", $option);
236                $tpl->setVariable("TXT_PRE", $option);
237                if ($current_type == "pre" && $value == $option) {
238                    $tpl->setVariable("PRE_SELECTED", 'selected="selected"');
239                }
240                $tpl->parseCurrentBlock();
241            }
242
243            $tpl->setVariable("POSTVAR", $this->getPostVar());
244            $tpl->setVariable("TXT_DIR", $lng->txt("sty_$dir"));
245            $tpl->setVariable("DIR", $dir);
246
247            $layout_tpl->setVariable(strtoupper($dir), $tpl->get());
248        }
249        $layout_tpl->setVariable("COLSPAN", "2");
250
251        $a_tpl->setCurrentBlock("prop_generic");
252        $a_tpl->setVariable("PROP_GENERIC", $layout_tpl->get());
253        $a_tpl->parseCurrentBlock();
254    }
255
256    /**
257    * Set value by array
258    *
259    * @param	array	$a_values	value array
260    */
261    public function setValueByArray($a_values)
262    {
263        $ilUser = $this->user;
264
265        if ($a_values[$this->getPostVar()]["horizontal"]["type"] == "predefined") {
266            $this->setHorizontalValue($a_values[$this->getPostVar()]["horizontal"]["pre_value"]);
267        } else {
268            $this->setHorizontalValue($a_values[$this->getPostVar()]["horizontal"]["num_value"] .
269                $a_values[$this->getPostVar()]["horizontal"]["num_unit"]);
270        }
271        if ($a_values[$this->getPostVar()]["vertical"]["type"] == "predefined") {
272            $this->setVerticalValue($a_values[$this->getPostVar()]["vertical"]["pre_value"]);
273        } else {
274            $this->setVerticalValue($a_values[$this->getPostVar()]["vertical"]["num_value"] .
275                $a_values[$this->getPostVar()]["vertical"]["num_unit"]);
276        }
277    }
278}
279