Test Failed
Push — master ( 83b526...bd639c )
by Stefan
23:38
created

OptionDisplay   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 514
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 72
eloc 251
c 2
b 1
f 0
dl 0
loc 514
rs 2.64

How to fix   Complexity   

Complex Class

Complex classes like OptionDisplay often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OptionDisplay, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * *****************************************************************************
5
 * Contributions to this work were made on behalf of the GÉANT project, a 
6
 * project that has received funding from the European Union’s Framework 
7
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
8
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
9
 * 691567 (GN4-1) and No. 731122 (GN4-2).
10
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
11
 * of the copyright in all material which was developed by a member of the GÉANT
12
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
13
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
14
 * UK as a branch of GÉANT Vereniging.
15
 * 
16
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
17
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
18
 *
19
 * License: see the web/copyright.inc.php file in the file structure or
20
 *          <base_url>/copyright.php after deploying the software
21
 */
22
23
namespace web\lib\admin;
24
25
use Exception;
26
27
/**
28
 * We need to display previously set options in various forms. This class covers
29
 * the ways to do that; the generated page content can then be parsed with 
30
 * OptionParser.
31
 * 
32
 * @author Stefan Winter <[email protected]>
33
 */
34
class OptionDisplay extends \core\common\Entity
35
{
36
37
    /**
38
     * stores all the options we are caring about
39
     * 
40
     * @var array
41
     */
42
    private $listOfOptions;
43
44
    /**
45
     * on which level are we operating?
46
     * 
47
     * @var string
48
     */
49
    private $level;
50
51
    /**
52
     * a counter storing how many locations are to be displayed
53
     * 
54
     * @var integer
55
     */
56
    private $allLocationCount;
57
58
    /**
59
     * When "fresh" options are displayed (HTML select/option fields, optionally
60
     * with language, and of varying data types) we want to give each option
61
     * the same prominence and iterate over all options in the list. This
62
     * variable keeps track how many option HTML code we've already sent, so
63
     * that we can iterate correctly.
64
     * 
65
     * Only used inside noPrefillText variant of the optiontext() call
66
     * 
67
     * @var integer
68
     */
69
    private $optionIterator;
70
71
    private $htmlDatatypeTexts;
72
    
73
    private $enumPrettyPrints;
74
    /**
75
     * Which attributes are we talking about?
76
     * @param array  $options the options of interest
77
     * @param string $level   the level on which these options were defined by the user (not applicable for XHR UI, then it is NULL)
78
     */
79
    public function __construct(array $options, string $level = NULL)
80
    {
81
        $this->listOfOptions = $options;
82
        $this->level = $level;
83
        $this->allLocationCount = 0;
84
85
        $this->enumPrettyPrints = [
86
            "ask" => _("Ask User"),
87
            "ask-preagreed" => _("Ask User; T&C Pre-Agreed"),
88
            "always" => _("Always"),
89
            "always-preagreed" => _("Always; T&C Pre-Agreed"),
90
        ];
91
        $openRoamingTail = "";
92
        foreach ($this->enumPrettyPrints as $optionName => $optionDisplay) {
93
            $openRoamingTail .= "<option value='$optionName'>$optionDisplay</option>";
94
        }
95
        
96
        $this->htmlDatatypeTexts = [
97
            \core\Options::TYPECODE_FILE => ["html" => "input type='file'", "tail" => ' size=\'10\''],
98
            \core\Options::TYPECODE_BOOLEAN => ["html" => "input type='checkbox' checked", "tail" => ''],
99
            \core\Options::TYPECODE_INTEGER => ["html" => "input type='number'", "tail" => ''],
100
            \core\Options::TYPECODE_STRING => ["html" => "input type='string'", "tail" => ''],
101
            \core\Options::TYPECODE_ENUM_OPENROAMING => ["html" => "select", "tail" => ">$openRoamingTail</select"],
102
            \core\Options::TYPECODE_TEXT => ["html" => "textarea cols='30' rows='3'", "tail" => '></textarea'],
103
        ];
104
    }
105
106
    /**
107
     * creates a table with all the set options prefilled. Only displays options
108
     * of the category indicated.
109
     * @param string $attributePrefix category of option to display
110
     * @param string $fed             the federation we are in
111
     * @return string HTML code <table>
112
     */
113
    public function prefilledOptionTable(string $attributePrefix, $fed)
114
    {
115
        $retval = "<table id='expandable_$attributePrefix" . "_options'>";
116
117
        $prepopulate = [];
118
        foreach ($this->listOfOptions as $existingAttribute) {
119
            if ($existingAttribute['level'] == $this->level) {
120
                $prepopulate[] = $existingAttribute;
121
            }
122
        }
123
        if (is_array($prepopulate) && ( count($prepopulate) > 0 || $attributePrefix == "device-specific" || $attributePrefix == "eap-specific" )) { // editing... fill with values
124
            $retval .= $this->addOptionEdit($attributePrefix, $prepopulate);
125
        } else {
126
            $retval .= $this->addOptionNew($attributePrefix, $fed);
127
        }
128
        $retval .= "</table>";
129
        return $retval;
130
    }
131
132
    /**
133
     * Displays options for a given option class, in Edit mode.
134
     * 
135
     * @param string $class       the class of options that is to be displayed
136
     * @param array  $prepopulate should an empty set of fillable options be displayed, or do we have existing data to prefill with
137
     * @return string
138
     */
139
    private function addOptionEdit(string $class, array $prepopulate = [])
140
    { // no GET class ? we've been called directly:
141
        // this can mean either a new object (list all options with empty values)
142
        // or that an object is to be edited. In that case, $prepopulated has to
143
        // contain the array of existing variables
144
        // we expect the variable $class to contain the class of options
145
        $retval = "";
146
        $optioninfo = \core\Options::instance();
147
        $blackListOnPrefill = "user:fedadmin|managedsp:vlan|managedsp:operatorname";
148
        if (\config\Master::FUNCTIONALITY_LOCATIONS['CONFASSISTANT_SILVERBULLET'] == "LOCAL" && \config\Master::FUNCTIONALITY_LOCATIONS['CONFASSISTANT_RADIUS'] != "LOCAL") {
149
            $blackListOnPrefill .= "|fed:silverbullet";
150
        }
151
        foreach ($prepopulate as $option) {
152
            if (preg_match("/^$class:/", $option['name']) && !preg_match("/($blackListOnPrefill)/", $option['name'])) {
153
                $optiontypearray = $optioninfo->optionType($option['name']);
154
                $loggerInstance = new \core\common\Logging();
155
                $loggerInstance->debug(5, "About to execute optiontext with PREFILL!\n");
156
                $retval .= $this->optiontext([$option['name']], ($optiontypearray["type"] == "file" ? 'ROWID-' . $option['level'] . '-' . $option['row_id'] : $option['value']), $option['lang']);
157
            }
158
        }
159
        return $retval;
160
    }
161
162
    /**
163
     * Find which options to expose to UI and which to hide.
164
     * Not all options defined in the database are (always) displayed. Some have
165
     * custom UI not matching the usual dropdown display, some depend on context
166
     * (e.g. OpenRoaming or not, depending on whether the fed operator wants it
167
     * 
168
     * @param string $class the type of options requested
169
     * @param string $fed   the federation TLD, to determine fed ops preference context
170
     * @return array the list of options to display
171
     */
172
    public static function enumerateOptionsToDisplay($class, $fed)
173
    {
174
        $optioninfo = \core\Options::instance();
175
176
        $list = $optioninfo->availableOptions($class);
177
178
        // use federation context to delete more options, if the feds don't like
179
        // a particular one
180
        $fedInstance = new \core\Federation($fed);
181
        switch ($class) {
182
            case "general":
183
                unset($list[array_search("general:geo_coordinates", $list)]);
184
                break;
185
            case "user":
186
                unset($list[array_search("user:fedadmin", $list)]);
187
                break;
188
            case "managedsp":
189
                unset($list[array_search("managedsp:vlan", $list)]);
190
                unset($list[array_search("managedsp:operatorname", $list)]);
191
                break;
192
            case "fed":
193
                //normally, we have nothing to hide on that level
194
                // if we are a Managed IdP exclusive deployment, do not display or allow
195
                // to change the "Enable Managed IdP" boolean - it is simply always there
196
                if (\config\Master::FUNCTIONALITY_LOCATIONS['CONFASSISTANT_SILVERBULLET'] == "LOCAL" && \config\Master::FUNCTIONALITY_LOCATIONS['CONFASSISTANT_RADIUS'] != "LOCAL") {
197
                    unset($list[array_search("fed:silverbullet", $list)]);
198
                }
199
                break;
200
            case "media":
201
                if ($fedInstance->getAttributes("fed:openroaming") == []) {
202
                    // no openroaming here
203
                    unset($list[array_search("media:openroaming", $list)]);
204
                }
205
                break;
206
            default:
207
                break;
208
        }
209
210
        return $list;
211
    }
212
213
    /**
214
     * Displays options for a given option class, in New mode.
215
     * 
216
     * @param string $class           the class of options that is to be displayed
217
     * @param string $fed             the federation we are in
218
     * @return string
219
     */
220
    private function addOptionNew(string $class, $fed)
221
    {
222
        $retval = "";
223
224
        $list2 = array_values(OptionDisplay::enumerateOptionsToDisplay($class, $fed));
225
226
        // add as many options as there are different option types
227
        $numberOfOptions = count($list2);
228
        for ($this->optionIterator = 0; $this->optionIterator < $numberOfOptions; $this->optionIterator++) {
229
            $retval .= $this->optiontext($list2);
230
        }
231
        return $retval;
232
    }
233
234
    /**
235
     * produce code for a option-specific tooltip
236
     * @param int     $rowid     the number (once during page build) of the option 
237
     *                           that should get the tooltip
238
     * @param string  $input     the option name. Tooltip for it will be displayed
239
     *                           if we have one available.
240
     * @param boolean $isVisible should the tooltip be visible with the option,
241
     *                           or are they both currently hidden?
242
     * @return string
243
     */
244
    private function tooltip($rowid, $input, $isVisible)
245
    {
246
        \core\common\Entity::intoThePotatoes();
247
        $descriptions = [];
248
        if (count(\config\ConfAssistant::CONSORTIUM['ssid']) > 0) {
249
            $descriptions["media:SSID"] = sprintf(_("This attribute can be set if you want to configure an additional SSID besides the default SSIDs for %s. It is almost always a bad idea not to use the default SSIDs. The only exception is if you have premises with an overlap of the radio signal with another %s hotspot. Typical misconceptions about additional SSIDs include: I want to have a local SSID for my own users. It is much better to use the default SSID and separate user groups with VLANs. That approach has two advantages: 1) your users will configure %s properly because it is their everyday SSID; 2) if you use a custom name and advertise this one as extra secure, your users might at some point roam to another place which happens to have the same SSID name. They might then be misled to believe that they are connecting to an extra secure network while they are not."), \config\ConfAssistant::CONSORTIUM['display_name'], \config\ConfAssistant::CONSORTIUM['display_name'], \config\ConfAssistant::CONSORTIUM['display_name']);
250
        }
251
        $descriptions["media:force_proxy"] = sprintf(_("The format of this option is: IPv4|IPv6|hostname:port . Forcing your users through a content filter of your own is a significant invasion of user self-determination. It also has technical issues. Please thoroughly read the discussion at %s before specifying a proxy with this option. This feature is currently experimental and only has an effect in Apple installers."), "https://github.com/GEANT/CAT/issues/96");
252
        $descriptions["managedsp:realmforvlan"] = sprintf(_("If you are also using %s, then your own realm is automatically tagged with the VLAN you choose, there is no need to add it here manually."), \core\ProfileSilverbullet::PRODUCTNAME);
253
        $descriptions["media:openroaming"] = sprintf(_("By opting in to OpenRoaming, you agree to be bound by the %s."), "eduroam Ecosystem Broker OpenRoaming Identity Provider Policy") .
254
                " " .
255
                sprintf(_("Note that your requirement to inform users about the OpenRoaming End User Terms and Conditions is fulfilled when directing your end users to the %s download portal for installer download. Any other means of providing the installers needs to present this information via its own channel."), \config\Master::APPEARANCE['productname']) .
256
                " " .
257
                _("You are also aware that for best technical interoperability, you need to add a DNS entry into the DNS zone of your RADIUS realm.") .
258
                " " .
259
                _("Read the instructions in the wiki.");
260
        \core\common\Entity::outOfThePotatoes();
261
        if (!isset($descriptions[$input])) {
262
            return "";
263
        }
264
        return "<span class='tooltip' id='S$rowid-tooltip-$input' style='display:" . ($isVisible ? "block" : "none") . "' onclick='alert(\"" . $descriptions[$input] . "\")'><img src='../resources/images/icons/question-mark-icon.png" . "'></span>";
265
    }
266
267
    /**
268
     * 
269
     * @param int   $rowid the number (once during page build) of the option 
270
     *                     that should get the tooltip
271
     * @param array $list  elements of the drop-down list
272
     * @return array HTML code and which option is active
273
     * @throws \Exception
274
     */
275
    private function selectElement($rowid, $list)
276
    {
277
        $jsmagic = "onchange='
278
                               if (/#ML#/.test(document.getElementById(\"option-S" . $rowid . "-select\").value)) {
279
                                   document.getElementById(\"S$rowid-input-langselect\").style.display = \"block\";
280
                                   } else {
281
                                   document.getElementById(\"S$rowid-input-langselect\").style.display = \"none\";
282
                                   }";
283
        foreach (array_keys($this->htmlDatatypeTexts) as $key) {
284
            $jsmagic .= "if (/#" . $key . "#/.test(document.getElementById(\"option-S" . $rowid . "-select\").value)) {
285
                                  document.getElementById(\"S$rowid-input-file\").style.display = \"" . ($key == \core\Options::TYPECODE_FILE ? "block" : "none") . "\";
286
                                  document.getElementById(\"S$rowid-input-text\").style.display = \"" . ($key == \core\Options::TYPECODE_TEXT ? "block" : "none") . "\";
287
                                  document.getElementById(\"S$rowid-input-string\").style.display = \"" . ($key == \core\Options::TYPECODE_STRING ? "block" : "none") . "\";
288
                                  document.getElementById(\"S$rowid-input-enum_openroaming\").style.display = \"" . ($key == \core\Options::TYPECODE_ENUM_OPENROAMING ? "block" : "none") . "\";
289
                                  document.getElementById(\"S$rowid-input-boolean\").style.display = \"" . ($key == \core\Options::TYPECODE_BOOLEAN ? "block" : "none") . "\";
290
                                  document.getElementById(\"S$rowid-input-integer\").style.display = \"" . ($key == \core\Options::TYPECODE_INTEGER ? "block" : "none") . "\";
291
                             }
292
                             ";
293
            // hide all tooltips (each is a <span>, and there are no other <span>s)
294
            $jsmagic .= <<< FOO
295
                    var ourtooltips = document.querySelectorAll(&#34;[id^=&#39;S$rowid-tooltip-&#39;]&#34;);
296
                    for (var i=0; i<ourtooltips.length; i++) {
297
                      ourtooltips[i].style.display = "none";
298
                    }
299
                    var optionnamefull = document.getElementById("option-S$rowid-select").value;
300
                    var firstdelimiter = optionnamefull.indexOf("#");
301
                    var optionname = optionnamefull.substring(0,firstdelimiter);
302
                    var tooltipifany = document.getElementById("S$rowid-tooltip-"+optionname);
303
                    if (tooltipifany != null) {
304
                      tooltipifany.style.display = "block";
305
                    }
306
FOO;
307
        }
308
        $jsmagic .= "'";
309
310
        $optioninfo = \core\Options::instance();
311
        $retval = "<span style='display:flex';>";
312
        $iterator = 0;
313
        $tooltips = "";
314
        $uiElements = new UIElements();
315
        $activelisttype = [];
316
        switch (count($list)) {
317
            case 1: // if there is only one option available, don't introduce an artificial drop-down for it
318
                $value = array_shift($list);
319
                $listtype = $optioninfo->optionType($value);
320
                $retval .= $uiElements->displayName($value);
321
                $retval .= "<input type='hidden' name='option[S$rowid]' value='$value#" . $listtype["type"] . "#" . $listtype["flag"] . "#'/>";
322
                $activelisttype = $listtype;
323
                $tooltips = $this->tooltip($rowid, $value, TRUE);
324
                break;
325
            default:
326
                $retval .= "<select id='option-S$rowid-select' name='option[S$rowid]' $jsmagic>";
327
                foreach ($list as $value) {
328
                    $listtype = $optioninfo->optionType($value);
329
                    $retval .= "<option id='option-S$rowid-v-$value' value='$value#" . $listtype["type"] . "#" . $listtype["flag"] . "#' ";
330
                    if ($iterator == $this->optionIterator) {
331
                        $retval .= "selected='selected'";
332
                        $activelisttype = $listtype;
333
                        $tooltips .= $this->tooltip($rowid, $value, TRUE);
334
                    } else {
335
                        $tooltips .= $this->tooltip($rowid, $value, FALSE);
336
                    }
337
                    $retval .= ">" . $uiElements->displayName($value) . "</option>";
338
                    $iterator++;
339
                }
340
341
                if (count($activelisttype) == 0) {
342
                    throw new \Exception("We should have found the active list type by now!");
343
                }
344
                $retval .= "</select>";
345
        }
346
        $retval .= $tooltips;
347
        $retval .= "</span>";
348
349
        return ["TEXT" => $retval, "ACTIVE" => $activelisttype];
350
    }
351
352
    /**
353
     * HTML code to display the language selector
354
     * 
355
     * @param int     $rowid       the number (once during page build) of the option 
356
     *                             that should get the tooltip
357
     * @param boolean $makeVisible is the language selector to be made visible?
358
     * @return string
359
     */
360
    private function selectLanguage($rowid, $makeVisible)
361
    {
362
        \core\common\Entity::intoThePotatoes();
363
        $retval = "<select style='display:" . ($makeVisible ? "block" : "none") . "' name='value[S$rowid-lang]' id='S" . $rowid . "-input-langselect'>
364
            <option value='' name='select_language' selected>" . _("select language") . "</option>
365
            <option value='C' name='all_languages'>" . _("default/other languages") . "</option>";
366
        foreach (\config\Master::LANGUAGES as $langindex => $possibleLang) {
367
            $thislang = $possibleLang['display'];
368
            $retval .= "<option value='$langindex' name='$langindex'>$thislang</option>";
369
        }
370
        $retval .= "</select>";
371
        \core\common\Entity::outOfThePotatoes();
372
        return $retval;
373
    }
374
375
    /**
376
     * HTML code for a given option. Marks the matching datatype as visible, all other datatypes hidden
377
     * @param int   $rowid      the number (once during page build) of the option 
378
     *                          that should get the tooltip
379
     * @param array $activetype the active datatype that is to be visible
380
     * @return string
381
     */
382
    private function inputFields($rowid, $activetype)
383
    {
384
        $retval = "";
385
        foreach ($this->htmlDatatypeTexts as $key => $type) {
386
            $retval .= "<" . $type['html'] . " style='display:" . ($activetype['type'] == $key ? "block" : "none") . "' name='value[S$rowid-$key]' id='S" . $rowid . "-input-" . $key . "'" . $type['tail'] . ">";
387
        }
388
        return $retval;
389
    }
390
391
    /**
392
     * HTML code to display a "fresh" option (including type selector and JavaScript to show/hide relevant input fields)
393
     * @param int   $rowid the HTML field base name of the option to be displayed
394
     * @param array $list  the list of option names to include in the type selector
395
     * @return string HTML code
396
     * @throws Exception
397
     */
398
    private function noPrefillText(int $rowid, array $list)
399
    {
400
        // first column: the <select> element with the names of options and their field-toggling JS magic
401
        $selectorInfo = $this->selectElement($rowid, $list);
402
        $retval = "<td>" . $selectorInfo["TEXT"] . "</td>";
403
        // second column: the <select> element for language selection - only visible if the active option is multi-lang
404
        $retval .= "<td>" . $this->selectLanguage($rowid, $selectorInfo['ACTIVE']['flag'] == "ML") . "</td>";
405
        // third column: the actual input fields; the data type of the active option is visible, all others hidden
406
        $retval .= "<td>" . $this->inputFields($rowid, $selectorInfo['ACTIVE']) . "</td>";
407
        return $retval;
408
    }
409
410
    /**
411
     * generates HTML code that displays an already set option.
412
     * 
413
     * @param int    $rowid       the HTML field base name of the option to be displayed
414
     * @param string $optionName  the name of the option to display
415
     * @param string $optionValue the value of the option to display
416
     * @param mixed  $optionLang  the language of the option to display
417
     * @return string HTML code
418
     * @throws Exception
419
     */
420
    private function prefillText(int $rowid, string $optionName, string $optionValue, $optionLang)
421
    {
422
        \core\common\Entity::intoThePotatoes();
423
        $retval = "";
424
        $optioninfo = \core\Options::instance();
425
        $loggerInstance = new \core\common\Logging();
426
        $loggerInstance->debug(5, "Executed with PREFILL $optionValue!\n");
427
        $retval .= "<td>";
428
        $uiElements = new UIElements();
429
        $listtype = $optioninfo->optionType($optionName);
430
        $retval .= "<span style='display:flex;'>" . $uiElements->displayName($optionName);
431
        $retval .= $this->tooltip($rowid, $optionName, TRUE) . "</span>";
432
        $retval .= "<input type='hidden' id='option-S$rowid-select' name='option[S$rowid]' value='$optionName#" . $listtype["type"] . "#" . $listtype["flag"] . "#' ></td>";
433
434
        // language tag if any
435
        $retval .= "<td>";
436
        if ($listtype["flag"] == "ML") {
437
438
            $language = "(" . strtoupper($optionLang) . ")";
439
            if ($optionLang == 'C') {
440
                $language = _("(default/other languages)");
441
            }
442
            $retval .= $language;
443
            $retval .= "<input type='hidden' name='value[S$rowid-lang]' id='S" . $rowid . "-input-langselect' value='" . $optionLang . "' style='display:block'>";
444
        }
445
        $retval .= "</td>";
446
// attribute content
447
        $retval .= "<td>";
448
        $displayedVariant = "";
449
        switch ($listtype["type"]) {
450
            case \core\Options::TYPECODE_COORDINATES:
451
                $this->allLocationCount = $this->allLocationCount + 1;
452
                // display of the locations varies by map provider
453
                $classname = "\web\lib\admin\Map" . \config\ConfAssistant::MAPPROVIDER['PROVIDER'];
454
                $link = $classname::optionListDisplayCode($optionValue, $this->allLocationCount);
455
                $retval .= "<input readonly style='display:none' type='text' name='value[S$rowid-" . \core\Options::TYPECODE_TEXT . "]' id='S$rowid-input-text' value='$optionValue'>$link";
456
                break;
457
            case \core\Options::TYPECODE_FILE:
458
                $retval .= "<input readonly type='text' name='value[S$rowid-" . \core\Options::TYPECODE_STRING . "]' id='S" . $rowid . "-input-string' style='display:none' value='" . urlencode($optionValue) . "'>";
459
                $uiElements = new UIElements();
460
                switch ($optionName) {
461
                    case "eap:ca_file":
462
                    // fall-through intentional: display both types the same way
463
                    case "fed:minted_ca_file":
464
                        $retval .= $uiElements->previewCAinHTML($optionValue);
465
                        break;
466
                    case "general:logo_file":
467
                    // fall-through intentional: display both types the same way
468
                    case "fed:logo_file":
469
                        $retval .= $uiElements->previewImageinHTML($optionValue);
470
                        break;
471
                    case "support:info_file":
472
                        $retval .= $uiElements->previewInfoFileinHTML($optionValue);
473
                        break;
474
                    default:
475
                        $retval .= _("file content");
476
                }
477
                break;
478
            case \core\Options::TYPECODE_ENUM_OPENROAMING: // is a string after all
479
                $displayedVariant = $this->enumPrettyPrints[$optionValue];
480
                $retval .= "<strong>$displayedVariant</strong><input type='hidden' name='value[S$rowid-" . $listtype['type'] . "]' id='S" . $rowid . "-input-" . $listtype["type"] . "' value=\"" . htmlspecialchars($optionValue) . "\" style='display:block'>";
481
                break;
482
            case \core\Options::TYPECODE_STRING:
483
            // fall-thorugh is intentional; mostly identical HTML code for the three types
484
            case \core\Options::TYPECODE_INTEGER:
485
            // fall-thorugh is intentional; mostly identical HTML code for the three types
486
            case \core\Options::TYPECODE_TEXT:
487
                $displayedVariant = $optionValue; // for all three types, value tag and actual display are identical
488
                $retval .= "<strong>$displayedVariant</strong><input type='hidden' name='value[S$rowid-" . $listtype['type'] . "]' id='S" . $rowid . "-input-" . $listtype["type"] . "' value=\"" . htmlspecialchars($optionValue) . "\" style='display:block'>";
489
                break;
490
            case \core\Options::TYPECODE_BOOLEAN:
491
                $displayedVariant = ($optionValue == "on" ? _("on") : _("off"));
492
                $retval .= "<strong>$displayedVariant</strong><input type='hidden' name='value[S$rowid-" . $listtype['type'] . "]' id='S" . $rowid . "-input-" . $listtype["type"] . "' value=\"" . htmlspecialchars($optionValue) . "\" style='display:block'>";
493
                break;
494
            default:
495
                // this should never happen!
496
                throw new Exception("Internal Error: unknown attribute type $listtype!");
497
        }
498
        $retval .= "</td>";
499
        \core\common\Entity::outOfThePotatoes();
500
        return $retval;
501
    }
502
503
    /**
504
     * Displays a container for options. Either with prefilled data or empty; if
505
     * empty then has HTML <input> tags with clever javaScript to allow selection
506
     * of different option names and types
507
     * @param array  $list         options which should be displayed; can be only exactly one if existing option, or multiple if new option type
508
     * @param string $prefillValue for an existing option, it's value to be displayed
509
     * @param string $prefillLang  for an existing option, the language of the value to be displayed
510
     * @return string HTML code <tr>
511
     * @throws Exception
512
     */
513
    public function optiontext(array $list, string $prefillValue = NULL, string $prefillLang = NULL)
514
    {
515
        $rowid = mt_rand();
516
517
        $retval = "<tr id='option-S$rowid' style='vertical-align:top'>";
518
519
        $item = "MULTIPLE";
520
        if ($prefillValue === NULL) {
521
            $retval .= $this->noPrefillText($rowid, $list);
522
        }
523
524
        if ($prefillValue !== NULL) {
525
            // prefill is always only called with a list with exactly one element.
526
            // if we see anything else here, get excited.
527
            if (count($list) != 1) {
528
                throw new Exception("Optiontext prefilled display only can work with exactly one option!");
529
            }
530
            $item = array_pop($list);
531
            $retval .= $this->prefillText($rowid, $item, $prefillValue, $prefillLang);
532
        }
533
        $retval .= "
534
535
       <td>
536
          <button type='button' class='delete' onclick='";
537
        if ($prefillValue !== NULL && $item == "general:geo_coordinates") {
538
            $funcname = "Map" . \config\ConfAssistant::MAPPROVIDER['PROVIDER'] . 'DeleteCoord';
539
            $retval .= 'if (typeof ' . $funcname . ' === "function") { ' . $funcname . '(' . $this->allLocationCount . '); } ';
540
        }
541
        $retval .= 'deleteOption("option-S' . $rowid . '")';
542
        $retval .= "'>-</button>
543
       </td>
544
    </tr>";
545
        return $retval;
546
    }
547
}
548