1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * This class acts as Model for all system styles settings related settings
7 * such as activated or default system styles etc, be it in database or inifile.
8 * Do not use this class to get the current system style, use ilStyleDefinition insteaed.
9 *
10 * Semantics of terms style, sub style, skin, template --> see ilStyleDefinition
11 *
12 * @author Alex Killing <alex.killing@gmx.de>
13 * @author Timon Amstutz <timon.amstutz@ilub.unibe.ch>
14 *
15 * @version $Id$
16 * @ingroup ServicesStyle
17 *
18 */
19class ilSystemStyleSettings
20{
21    /**
22     * lookup if a style is activated
23     *
24     * @param $a_skin
25     * @param $a_style
26     * @return bool
27     */
28    public static function _lookupActivatedStyle($a_skin, $a_style)
29    {
30        global $DIC;
31
32        $q = "SELECT count(*) cnt FROM settings_deactivated_s" .
33            " WHERE skin = " . $DIC->database()->quote($a_skin, "text") .
34            " AND style = " . $DIC->database()->quote($a_style, "text") . " ";
35
36        $cnt_set = $DIC->database()->query($q);
37        $cnt_rec = $DIC->database()->fetchAssoc($cnt_set);
38
39        if ($cnt_rec["cnt"] > 0) {
40            return false;
41        } else {
42            return true;
43        }
44    }
45
46    /**
47     * deactivate system style
48     *
49     * @param $a_skin
50     * @param $a_style
51     */
52    public static function _deactivateStyle($a_skin, $a_style)
53    {
54        global $DIC;
55
56        ilSystemStyleSettings::_activateStyle($a_skin, $a_style);
57        $q = "INSERT into settings_deactivated_s" .
58            " (skin, style) VALUES " .
59            " (" . $DIC->database()->quote($a_skin, "text") . "," .
60            " " . $DIC->database()->quote($a_style, "text") . ")";
61
62        $DIC->database()->manipulate($q);
63    }
64
65    /**
66     * activate system style
67     *
68     * @param $a_skin
69     * @param $a_style
70     */
71    public static function _activateStyle($a_skin, $a_style)
72    {
73        global $DIC;
74
75        $q = "DELETE FROM settings_deactivated_s" .
76            " WHERE skin = " . $DIC->database()->quote($a_skin, "text") .
77            " AND style = " . $DIC->database()->quote($a_style, "text");
78
79        $DIC->database()->manipulate($q);
80    }
81
82    /**
83     * Get all system sub styles category assignments. This is used to check wheter a system sub style is to be used
84     * in a particular category.
85     *
86     * @param string $a_skin_id skin id
87     * @param string $a_style_id style id
88     * @return array ('substyle' => substyle_id, 'ref id' => cat_ref_id)
89     */
90    public static function getSystemStyleCategoryAssignments($a_skin_id, $a_style_id)
91    {
92        global $DIC;
93
94        $assignments = [];
95        $set = $DIC->database()->query(
96            "SELECT substyle, category_ref_id FROM syst_style_cat " .
97                " WHERE skin_id = " . $DIC->database()->quote($a_skin_id, "text") .
98                " AND style_id = " . $DIC->database()->quote($a_style_id, "text")
99        );
100        while (($rec = $DIC->database()->fetchAssoc($set))) {
101            $assignments[] = [
102                    "substyle" => $rec["substyle"],
103                    "ref_id" => $rec["category_ref_id"]
104            ];
105        }
106        return $assignments;
107    }
108
109    /**
110     * Get all system category assignments of exactly one substyle. This is used to check wheter a system sub style is to be used
111     * in a particular category.
112     *
113     * @param $a_skin_id
114     * @param $a_style_id
115     * @param $a_sub_style_id
116     * @return array
117     */
118    public static function getSubStyleCategoryAssignments($a_skin_id, $a_style_id, $a_sub_style_id)
119    {
120        global $DIC;
121
122        $assignmnts = [];
123
124        $set = $DIC->database()->query(
125            "SELECT substyle, category_ref_id FROM syst_style_cat " .
126                " WHERE skin_id = " . $DIC->database()->quote($a_skin_id, "text") .
127                " AND substyle = " . $DIC->database()->quote($a_sub_style_id, "text") .
128                " AND style_id = " . $DIC->database()->quote($a_style_id, "text")
129        );
130        while (($rec = $DIC->database()->fetchAssoc($set))) {
131            $assignmnts[] = [
132                    "substyle" => $rec["substyle"],
133                    "ref_id" => $rec["category_ref_id"]
134            ];
135        }
136        return $assignmnts;
137    }
138
139    /**
140     * Sets a substyle category assignment.
141     *
142     * @param $a_skin_id
143     * @param $a_style_id
144     * @param $a_substyle
145     * @param $a_ref_id
146     * @throws ilSystemStyleException
147     */
148    public static function writeSystemStyleCategoryAssignment(
149        $a_skin_id,
150        $a_style_id,
151        $a_substyle,
152        $a_ref_id
153    ) {
154        global $DIC;
155
156        $assignments = self::getSubStyleCategoryAssignments($a_skin_id, $a_style_id, $a_substyle);
157
158        foreach ($assignments as $assignment) {
159            if ($assignment["ref_id"] == $a_ref_id) {
160                throw new ilSystemStyleException(ilSystemStyleException::SUBSTYLE_ASSIGNMENT_EXISTS, $a_substyle . ": " . $a_ref_id);
161            }
162        }
163        $DIC->database()->manipulate("INSERT INTO syst_style_cat " .
164                "(skin_id, style_id, substyle, category_ref_id) VALUES (" .
165                $DIC->database()->quote($a_skin_id, "text") . "," .
166                $DIC->database()->quote($a_style_id, "text") . "," .
167                $DIC->database()->quote($a_substyle, "text") . "," .
168                $DIC->database()->quote($a_ref_id, "integer") .
169                ")");
170    }
171
172    /**
173     * Deletes all sub style category assignment of a system style. This is used if a system style is deleted
174     * completely
175     *
176     * @param $a_skin_id
177     * @param $a_style_id
178     * @param $a_substyle
179     * @param $a_ref_id
180     */
181    public static function deleteSystemStyleCategoryAssignment(
182        $a_skin_id,
183        $a_style_id,
184        $a_substyle,
185        $a_ref_id
186    ) {
187        global $DIC;
188
189        $DIC->database()->manipulate("DELETE FROM syst_style_cat WHERE " .
190                " skin_id = " . $DIC->database()->quote($a_skin_id, "text") .
191                " AND style_id = " . $DIC->database()->quote($a_style_id, "text") .
192                " AND substyle = " . $DIC->database()->quote($a_substyle, "text") .
193                " AND category_ref_id = " . $DIC->database()->quote($a_ref_id, "integer"));
194    }
195
196    /**
197     * Delets a sub styles category assignment.
198     *
199     * @param $a_skin_id
200     * @param $a_style_id
201     * @param $a_substyle
202     */
203    public static function deleteSubStyleCategoryAssignments($a_skin_id, $a_style_id, $a_substyle)
204    {
205        global $DIC;
206
207        $DIC->database()->manipulate("DELETE FROM syst_style_cat WHERE " .
208                " skin_id = " . $DIC->database()->quote($a_skin_id, "text") .
209                " AND style_id = " . $DIC->database()->quote($a_style_id, "text") .
210                " AND substyle = " . $DIC->database()->quote($a_substyle, "text"));
211    }
212
213    /**
214     * Sets a users preferred system skin/style by using the user object.
215     *
216     * @param $skin_id
217     * @param $style_id
218     */
219    public static function setCurrentUserPrefStyle($skin_id, $style_id)
220    {
221        global $DIC;
222
223        $DIC->user()->setPref("skin", $skin_id);
224        $DIC->user()->setPref("style", $style_id);
225        $DIC->user()->update();
226    }
227
228    /**
229     * Gets a users preferred skin by using the user object.
230     *
231     * @return bool
232     */
233    public static function getCurrentUserPrefSkin()
234    {
235        global $DIC;
236
237        return $DIC->user()->getPref("skin");
238    }
239
240    /**
241     * Gets a users preferred style by using the user object.
242     *
243     * @return bool
244     */
245    public static function getCurrentUserPrefStyle()
246    {
247        global $DIC;
248
249        return $DIC->user()->getPref("style");
250    }
251
252    /**
253     * Sets the default style of the system
254     *
255     * @param $skin_id
256     * @param $style_id
257     */
258    public static function setCurrentDefaultStyle($skin_id, $style_id)
259    {
260        global $DIC;
261
262        $DIC->clientIni()->setVariable("layout", "skin", $skin_id);
263        $DIC->clientIni()->setVariable("layout", "style", $style_id);
264        $DIC->clientIni()->write();
265        self::_activateStyle($skin_id, $style_id);
266    }
267
268    public static function resetDefaultToDelos()
269    {
270        $system_style_conf = new ilSystemStyleConfig();
271
272        self::setCurrentDefaultStyle($system_style_conf->getDefaultSkinId(), $system_style_conf->getDefaultSkinId());
273    }
274
275    /**
276     * Gets default Skin of the System
277     *
278     * @return string
279     */
280    public static function getCurrentDefaultSkin()
281    {
282        global $DIC;
283
284        $skin_id = $DIC->clientIni()->readVariable("layout", "skin");
285
286        if (!ilStyleDefinition::skinExists($skin_id)) {
287            self::resetDefaultToDelos();
288            $skin_id = $DIC->clientIni()->readVariable("layout", "skin");
289        }
290        return $skin_id;
291    }
292
293    /**
294     * Gets default style of the system
295     *
296     * @return mixed
297     * @throws ilSystemStyleException
298     */
299    public static function getCurrentDefaultStyle()
300    {
301        global $DIC;
302        $skin_id = $DIC->clientIni()->readVariable("layout", "skin");
303        $style_id = $DIC->clientIni()->readVariable("layout", "style");
304
305        if (!ilStyleDefinition::styleExistsForSkinId($skin_id, $style_id)) {
306            self::resetDefaultToDelos();
307            $style_id = $DIC->clientIni()->readVariable("layout", "style");
308        }
309        return $style_id;
310    }
311}
312