1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilDclSelectionFieldRepresentation
6 *
7 * @author  Theodor Truffer <tt@studer-raimann.ch>
8 */
9abstract class ilDclSelectionFieldRepresentation extends ilDclBaseFieldRepresentation
10{
11
12    // those should be overwritten by subclasses
13    const PROP_SELECTION_TYPE = '';
14    const PROP_SELECTION_OPTIONS = '';
15
16
17    /**
18     * @param ilObjDataCollection $dcl
19     * @param string              $mode
20     *
21     * @return ilPropertyFormGUI
22     */
23    protected function buildFieldCreationInput(ilObjDataCollection $dcl, $mode = 'create')
24    {
25        $opt = parent::buildFieldCreationInput($dcl, $mode);
26
27        $selection_options = $this->buildOptionsInput();
28        $opt->addSubItem($selection_options);
29
30        $selection_type = new ilRadioGroupInputGUI($this->lng->txt('dcl_selection_type'), 'prop_' . static::PROP_SELECTION_TYPE);
31        $selection_type->setRequired(true);
32
33        $option_1 = new ilRadioOption(
34            $this->lng->txt('dcl_' . ilDclSelectionFieldModel::SELECTION_TYPE_SINGLE),
35            ilDclSelectionFieldModel::SELECTION_TYPE_SINGLE
36        );
37        $selection_type->addOption($option_1);
38
39        $option_2 = new ilRadioOption(
40            $this->lng->txt('dcl_' . ilDclSelectionFieldModel::SELECTION_TYPE_MULTI),
41            ilDclSelectionFieldModel::SELECTION_TYPE_MULTI
42        );
43        $selection_type->addOption($option_2);
44
45        $option_3 = new ilRadioOption(
46            $this->lng->txt('dcl_' . ilDclSelectionFieldModel::SELECTION_TYPE_COMBOBOX),
47            ilDclSelectionFieldModel::SELECTION_TYPE_COMBOBOX
48        );
49        $selection_type->addOption($option_3);
50
51        $opt->addSubItem($selection_type);
52
53        return $opt;
54    }
55
56
57    /**
58     * @param ilPropertyFormGUI $form
59     * @param int               $record_id
60     *
61     * @return ilMultiSelectInputGUI|ilRadioGroupInputGUI|ilSelectInputGUI
62     */
63    public function getInputField(ilPropertyFormGUI $form, $record_id = 0)
64    {
65        /** @var ilDclSelectionOption[] $options */
66        $options = ilDclSelectionOption::getAllForField($this->getField()->getId());
67        switch ($this->getField()->getProperty(static::PROP_SELECTION_TYPE)) {
68            case ilDclSelectionFieldModel::SELECTION_TYPE_SINGLE:
69                $input = new ilRadioGroupInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
70                foreach ($options as $opt) {
71                    $input->addOption(new ilRadioOption($opt->getValue(), $opt->getOptId()));
72                }
73                $input->setValue(array_keys($options)[0]);
74                break;
75            case ilDclSelectionFieldModel::SELECTION_TYPE_MULTI:
76                //				global $DIC;
77                //				$DIC->ui()->mainTemplate()->addOnLoadCode('$("#field_' . $this->getField()->getId() . '").removeClass("input")');
78                $input = new ilMultiSelectInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
79
80                $input->setHeight(100);
81                $input->setHeightUnit('%; max-height: 150px');
82                $input->setWidth(100);
83                $input->setWidthUnit('%');
84
85                $array = array();
86                foreach ($options as $opt) {
87                    $array[$opt->getOptId()] = $opt->getValue();
88                }
89                $input->setOptions($array);
90                break;
91            case ilDclSelectionFieldModel::SELECTION_TYPE_COMBOBOX:
92                $input = new ilSelectInputGUI($this->getField()->getTitle(), 'field_' . $this->getField()->getId());
93                $array = array();
94                foreach ($options as $opt) {
95                    $array[$opt->getOptId()] = $opt->getValue();
96                }
97                $input->setOptions(array("" => $this->lng->txt('dcl_please_select')) + $array);
98                break;
99        }
100        $this->setupInputField($input, $this->getField());
101
102        return $input;
103    }
104
105
106    /**
107     * @param ilTable2GUI $table
108     *
109     * @return null
110     */
111    public function addFilterInputFieldToTable(ilTable2GUI $table)
112    {
113        $input = $table->addFilterItemByMetaType("filter_" . $this->getField()->getId(), ilTable2GUI::FILTER_SELECT, false, $this->getField()->getId());
114
115        $options = ilDclSelectionOption::getAllForField($this->getField()->getId());
116        $array = array('' => $this->lng->txt('dcl_any'));
117        foreach ($options as $opt) {
118            $array[$opt->getOptId()] = $opt->getValue();
119        }
120
121        $input->setOptions($array);
122
123        $this->setupFilterInputField($input);
124
125        return $this->getFilterInputFieldValue($input);
126    }
127
128
129    /**
130     * @return mixed
131     */
132    abstract protected function buildOptionsInput();
133}
134