1<?php
2
3/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
4/**
5* This class represents a tag list property in a property form.
6*
7* @author Guido Vollbach <gvollbach@databay.de>
8* @version $Id$
9* @ingroup	ServicesForm
10*/
11class ilTagInputGUI extends ilSubEnabledFormPropertyGUI
12{
13    /**
14     * @var ilTemplate
15     */
16    protected $tpl;
17
18    protected $options = array();
19    protected $max_tags = 0;
20    protected $max_chars = 0;
21    protected $allow_duplicates = false;
22    protected $js_self_init = true;
23
24    protected $type_ahead = false;
25    protected $type_ahead_ignore_case = true;
26    protected $type_ahead_list = array();
27    protected $type_ahead_min_length = 2;
28    protected $type_ahead_limit = 30;
29    protected $type_ahead_highlight = true;
30
31    /**
32     * @param int $max_tags
33     */
34    public function setMaxTags($max_tags)
35    {
36        $this->max_tags = $max_tags;
37    }
38
39    /**
40     * @param int $max_chars
41     */
42    public function setMaxChars($max_chars)
43    {
44        $this->max_chars = $max_chars;
45    }
46
47    /**
48     * @param boolean $allow_duplicates
49     */
50    public function setAllowDuplicates($allow_duplicates)
51    {
52        $this->allow_duplicates = $allow_duplicates;
53    }
54
55    /**
56     * @param boolean $js_self_init
57     */
58    public function setJsSelfInit($js_self_init)
59    {
60        $this->js_self_init = $js_self_init;
61    }
62
63    /**
64     * @param boolean $type_ahead
65     */
66    public function setTypeAhead($type_ahead)
67    {
68        $this->type_ahead = $type_ahead;
69    }
70
71    /**
72     * @param boolean $type_ahead_ignore_case
73     */
74    public function setTypeAheadIgnoreCase($type_ahead_ignore_case)
75    {
76        $this->type_ahead_ignore_case = $type_ahead_ignore_case;
77    }
78
79    /**
80     * @param int $min_length
81     */
82    public function setTypeAheadMinLength($min_length)
83    {
84        $this->type_ahead_min_length = $min_length;
85    }
86
87    /**
88     * @param int $limit
89     */
90    public function setTypeAheadLimit($limit)
91    {
92        $this->type_ahead_limit = $limit;
93    }
94
95    /**
96     * @param boolean $highlight
97     */
98    public function setTypeAheadHighlight($highlight)
99    {
100        $this->type_ahead_highlight = $highlight;
101    }
102
103    /**
104     * @param array $type_ahead_list
105     */
106    public function setTypeAheadList($type_ahead_list)
107    {
108        $this->type_ahead_list = $type_ahead_list;
109    }
110
111    /**
112     * Set Options.
113     *
114     * @param	array	$a_options	Options.
115     */
116    public function setOptions($a_options)
117    {
118        $this->options = $a_options;
119    }
120
121    /**
122     * Get Options.
123     *
124     * @return	array	Options. Array
125     */
126    public function getOptions()
127    {
128        return $this->options ? $this->options : array();
129    }
130
131    /**
132    * Constructor
133    *
134    * @param	string	$a_title	Title
135    * @param	string	$a_postvar	Post Variable
136    */
137    public function __construct($a_title = "", $a_postvar = "")
138    {
139        global $DIC;
140
141        $this->tpl = $DIC["tpl"];
142        $this->lng = $DIC->language();
143        parent::__construct($a_title, $a_postvar);
144        $this->setType("tag_input");
145        $tpl = $DIC["tpl"];
146        $tpl->addJavaScript('./Services/Form/js/bootstrap-tagsinput_2015_25_03.js');
147        $tpl->addJavaScript('./Services/Form/js/typeahead_0.11.1.js');
148        $tpl->addCss('./Services/Form/css/bootstrap-tagsinput_2015_25_03.css');
149    }
150
151    /**
152    * Set value by array
153    *
154    * @param	array	$a_values	value array
155    */
156    public function setValueByArray($a_values)
157    {
158        $this->setOptions($a_values[$this->getPostVar()]);
159        foreach ($this->getSubItems() as $item) {
160            $item->setValueByArray($a_values);
161        }
162    }
163
164    /**
165    * Check input, strip slashes etc. set alert, if input is not ok.
166    *
167    * @return	boolean		Input ok, true/false
168    */
169    public function checkInput()
170    {
171        $lng = $this->lng;
172
173        $valid = true;
174        if (array_key_exists($this->getPostVar(), $_POST)) {
175            foreach ($_POST[$this->getPostVar()] as $idx => $value) {
176                $_POST[$this->getPostVar()][$idx] = ilUtil::stripSlashes($value);
177            }
178            $_POST[$this->getPostVar()] = array_unique($_POST[$this->getPostVar()]);
179
180            if ($this->getRequired() && !trim(implode("", $_POST[$this->getPostVar()]))) {
181                $valid = false;
182            }
183        } elseif ($this->getRequired()) {
184            $valid = false;
185        }
186        if (!$valid) {
187            $this->setAlert($lng->txt("msg_input_is_required"));
188            return false;
189        }
190        return $this->checkSubItemsInput();
191    }
192
193    /**
194     * @param string    $a_mode
195     * @return string
196     */
197    public function render($a_mode = "")
198    {
199        if ($this->type_ahead) {
200            $tpl = new ilTemplate("tpl.prop_tag_typeahead.html", true, true, "Services/Form");
201            $tpl->setVariable("MIN_LENGTH", $this->type_ahead_min_length);
202            $tpl->setVariable("LIMIT", $this->type_ahead_limit);
203            $tpl->setVariable("HIGHLIGHT", $this->type_ahead_highlight);
204            if ($this->type_ahead_ignore_case) {
205                $tpl->setVariable("CASE", 'i');
206            }
207            $tpl->setVariable("TERMS", json_encode($this->type_ahead_list));
208        } else {
209            $tpl = new ilTemplate("tpl.prop_tag.html", true, true, "Services/Form");
210        }
211
212        $tpl->setVariable("MAXTAGS", $this->max_tags);
213        $tpl->setVariable("MAXCHARS", $this->max_chars);
214        $tpl->setVariable("ALLOW_DUPLICATES", $this->allow_duplicates);
215
216        foreach ($this->getOptions() as $option_value => $option_text) {
217            $tpl->setCurrentBlock("prop_select_option");
218            $tpl->setVariable("VAL_SELECT_OPTION", ilUtil::prepareFormOutput($option_text));
219            $tpl->setVariable("TXT_SELECT_OPTION", $option_text);
220            $tpl->parseCurrentBlock();
221        }
222
223        $tpl->setVariable("ID", $this->getFieldId());
224
225        $tpl->setVariable("POST_VAR", $this->getPostVar() . "[]");
226
227        if ($this->js_self_init) {
228            $tpl->setCurrentBlock("initialize_on_page_load");
229            $tpl->parseCurrentBlock();
230        }
231        $tpl->setVariable("ID", $this->getFieldId());
232        return $tpl->get();
233    }
234
235    /**
236     * @param $a_tpl
237     */
238    public function insert($a_tpl)
239    {
240        $a_tpl->setCurrentBlock("prop_generic");
241        $a_tpl->setVariable("PROP_GENERIC", $this->render());
242        $a_tpl->parseCurrentBlock();
243    }
244}
245