1<?php
2
3/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
6include_once("./Services/Taxonomy/exceptions/class.ilTaxonomyException.php");
7
8/**
9 * Input GUI class for taxonomy assignments
10 *
11 * @author Alex Killing <alex.killing@gmx.de>
12 * @version $Id$
13 *
14 * @ingroup	ServicesTaxonomy
15 */
16class ilTaxAssignInputGUI extends ilSelectInputGUI
17{
18    /**
19     * Constructor
20     *
21     * @param	string	$a_title	Title
22     * @param	string	$a_postvar	Post Variable
23     */
24    public function __construct(
25        $a_taxonomy_id,
26        $a_multi = true,
27        $a_title = "",
28        $a_postvar = "",
29        $a_include_please_select = true
30    ) {
31        global $DIC;
32
33        $this->lng = $DIC->language();
34        $lng = $DIC->language();
35
36        $lng->loadLanguageModule("tax");
37        $this->setMulti($a_multi);
38        $this->include_please_select = $a_include_please_select;
39
40        if ($a_title == "") {
41            $a_title = $lng->txt("tax_taxonomy");
42        }
43
44        if ($a_postvar == "") {
45            $a_postvar = "tax_node_assign";
46        }
47
48        parent::__construct($a_title, $a_postvar);
49        $this->setType("tax_assign");
50
51        if ((int) $a_taxonomy_id == 0) {
52            throw new ilTaxonomyExceptions("No taxonomy ID passed to ilTaxAssignInputGUI.");
53        }
54
55        $this->setTaxonomyId((int) $a_taxonomy_id);
56    }
57
58    /**
59     * Set taxonomy id
60     *
61     * @param int $a_val taxonomy id
62     */
63    public function setTaxonomyId($a_val)
64    {
65        $this->taxononmy_id = $a_val;
66    }
67
68    /**
69     * Get taxonomy id
70     *
71     * @return int taxonomy id
72     */
73    public function getTaxonomyId()
74    {
75        return $this->taxononmy_id;
76    }
77
78    /**
79     * Set Options.
80     *
81     * @param	array	$a_options	Options. Array ("value" => "option_text")
82     */
83    public function setOptions($a_options)
84    {
85        throw new ilTaxonomyExceptions("setOptions: Not supported for ilTaxAssignInputGUI.");
86    }
87
88    /**
89     * Get Options.
90     *
91     * @return	array	Options. Array ("value" => "option_text")
92     */
93    public function getOptions()
94    {
95        $lng = $this->lng;
96
97        if ($this->include_please_select) {
98            $options = array("" => $lng->txt("please_select"));
99        }
100
101        include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
102        $tax_tree = new ilTaxonomyTree($this->getTaxonomyId());
103
104        $nodes = $tax_tree->getSubtree($tax_tree->getNodeData($tax_tree->readRootId()));
105        foreach ($nodes as $n) {
106            if ($n["type"] == "taxn") {
107                $options[$n["child"]] = str_repeat("&nbsp;", ($n["depth"] - 2) * 2) . $n["title"];
108            }
109        }
110
111        return $options;
112    }
113
114    /**
115     * Save input
116     *
117     * @param
118     * @return
119     */
120    public function saveInput($a_component_id, $a_obj_id, $a_item_type, $a_item_id)
121    {
122        include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
123        $tax_node_ass = new ilTaxNodeAssignment($a_component_id, $a_obj_id, $a_item_type, $this->getTaxonomyId());
124        //$tax_node_ass->deleteAssignmentsOfItem($a_item_id);
125
126        $post = $_POST[$this->getPostVar()];
127        if (!$this->getMulti()) {
128            $post = array($post);
129        } elseif (!is_array($post)) {
130            // BH: when multi values are ENABLED and $form->checkInput is NOT called
131            // there is no post parameter available WHEN the selection is left empty
132            // - fixed mantis #22186 - the followup issue
133            $post = array();
134        }
135
136        $current_ass = $tax_node_ass->getAssignmentsOfItem($a_item_id);
137        $exising = array();
138        foreach ($current_ass as $ca) {
139            if (!in_array($ca["node_id"], $post)) {
140                $tax_node_ass->deleteAssignment($ca["node_id"], $a_item_id);
141            } else {
142                $exising[] = $ca["node_id"];
143            }
144        }
145
146        foreach ($post as $p) {
147            if (!in_array($p, $exising)) {
148                $tax_node_ass->addAssignment(ilUtil::stripSlashes($p), $a_item_id);
149            }
150        }
151    }
152
153    /**
154     * Set current values
155     *
156     * @param
157     * @return
158     */
159    public function setCurrentValues($a_component_id, $a_obj_id, $a_item_type, $a_item_id)
160    {
161        include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
162        $tax_node_ass = new ilTaxNodeAssignment($a_component_id, $a_obj_id, $a_item_type, $this->getTaxonomyId());
163        $ass = $tax_node_ass->getAssignmentsOfItem($a_item_id);
164
165        $nodes = array();
166        foreach ($ass as $a) {
167            $nodes[] = $a["node_id"];
168        }
169        if ($this->getMulti()) {
170            $this->setValue($nodes);
171        } else {
172            $this->setValue($nodes[0]);
173        }
174    }
175}
176