Link.php 47.2 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2010 Glowbase GmbH <support@glowbase.com>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

namespace qfq;

//use qfq;

require_once(__DIR__ . '/Define.php');
30
31
require_once(__DIR__ . '/../store/Store.php');
require_once(__DIR__ . '/../store/Sip.php');
32
require_once(__DIR__ . '/../exceptions/UserReportExtension.php');
33
require_once(__DIR__ . '/../helper/KeyValueStringParser.php');
34
35
36
37
38
39
40
41
42
43
44
45

/*
 * u:url
 * m:mailto
 * p:page
 * 
 * t:text
 * a:AltText
 * o:ToolTip
 * 
 * r:render   
 * g:target
46
 * c:class  [n|i|e|<class>]
47
48
 * q:question  <text>
 * e:encryption 0|1
49
 * b:bootstrap button
50
51
52
53
54
55
56
57
58
59
60
 * 
 * E:edit
 * N:new
 * D:delete
 * H:Help
 * I:information
 * M:Magnifier
 * B:bullet
 * P:picture       [file]
 * C:checkbox    [name]
 * R:right 
Carsten  Rose's avatar
Carsten Rose committed
61
 * s:sip
62
 *
63
64
65
66
 * A:    A:[u:p:m]
 * G:    G:[N|..]
 */

67
68
69
70
71

const NAME_URL = 'url';
const NAME_MAIL = 'mail';
const NAME_PAGE = 'page';
const NAME_TEXT = 'text';
Carsten  Rose's avatar
Carsten Rose committed
72
73
74
const NAME_DOWNLOAD = DOWNLOAD_EXPORT_FILENAME;
const NAME_DOWNLOAD_ELEMENTS = 'downloadElements';  // array with element sources
const NAME_DOWNLOAD_MODE = 'mode';
75
const NAME_BOOTSTRAP_BUTTON = 'bootstrapButton';
76
77
const NAME_ALT_TEXT = 'altText';
const NAME_TOOL_TIP = 'toolTip';
78
const NAME_TOOL_TIP_JS = 'toolTipJs';
79
80
const NAME_IMAGE = 'image';
const NAME_IMAGE_TITLE = 'imageTitle';
81
82
const NAME_GLYPH = 'glyph';
const NAME_GLYPH_TITLE = 'glyphTitle';
83
84
85
86
87
88
89
const NAME_DELETE = 'delete';
const NAME_RENDER = 'render';
const NAME_TARGET = 'target';
const NAME_LINK_CLASS = 'linkClass';
const NAME_LINK_CLASS_DEFAULT = 'linkClassDefault';
const NAME_QUESTION = 'question';
const NAME_ENCRYPTION = 'encryption';
Carsten  Rose's avatar
Carsten Rose committed
90
const NAME_SIP = 'sip';
91
const NAME_URL_PARAM = 'param';
92
const NAME_RIGHT = 'picturePositionRight';
93
const NAME_ACTION_DELETE = 'actionDelete';
Carsten  Rose's avatar
Carsten Rose committed
94
const NAME_EXTRA_CONTENT_WRAP = 'extraContentWrap';
Carsten  Rose's avatar
Carsten Rose committed
95
const NAME_FILE = 'file';
96
97
98
99
100
101
102
103

const FINAL_HREF = 'finalHref';
const FINAL_ANCHOR = 'finalAnchor';
const FINAL_CONTENT = 'finalContent';
const FINAL_SYMBOL = 'finalSymbol';
const FINAL_TOOL_TIP = 'finalToolTip';
const FINAL_CLASS = 'finalClass';
const FINAL_QUESTION = 'finalQuestion';
104

105
106
107
const LINK_ANCHOR = 'linkAnchor';
const LINK_PICTURE = 'linkPicture';

108
109
const NO_CLASS = 'no_class';

Carsten  Rose's avatar
Carsten Rose committed
110
111
const DEFAULT_BULLET_COLOR = 'green';
const DEFAULT_CHECK_COLOR = 'green';
112
const DEFAULT_RENDER_MODE = '0';
Carsten  Rose's avatar
Carsten Rose committed
113
114
const DEFAULT_QUESTION_TEXT = 'Please confirm';
const DEFAULT_QUESTION_LEVEL = 'info';
115
const DEFAULT_ACTION_DELETE = 'r';
Carsten  Rose's avatar
Carsten Rose committed
116

Carsten  Rose's avatar
Carsten Rose committed
117
118
119
120
const QUESTION_INDEX_TEXT = 0;
const QUESTION_INDEX_LEVEL = 1;
const QUESTION_INDEX_BUTTON_OK = 2;
const QUESTION_INDEX_BUTTON_FALSE = 3;
121
122
const QUESTION_INDEX_TIMEOUT = 4;
const QUESTION_INDEX_FLAG_MODAL = 5;
Carsten  Rose's avatar
Carsten Rose committed
123

124
125
126
127
/**
 * Class Link
 * @package qfq
 */
128
129
class Link {

Carsten  Rose's avatar
Carsten Rose committed
130
    /**
131
     * @var Sip
Carsten  Rose's avatar
Carsten Rose committed
132
133
134
     */
    private $sip = null;

135
136
137
138
139
    /**
     * @var Store
     */
    private $store = null;

Carsten  Rose's avatar
Carsten Rose committed
140
    private $phpUnit;
141
    private $renderControl = array();
142
    private $linkClassSelector = array(TOKEN_CLASS_INTERNAL => "internal ", TOKEN_CLASS_EXTERNAL => "external ");
143
144
    private $cssLinkClassInternal = '';
    private $cssLinkClassExternal = '';
Carsten  Rose's avatar
Carsten Rose committed
145
    private $ttContentUid = '';
146

147
    private $callTable = [
Carsten  Rose's avatar
Carsten Rose committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        TOKEN_URL => 'buildUrl',
        TOKEN_MAIL => 'buildMail',
        TOKEN_PAGE => 'buildPage',
        TOKEN_DOWNLOAD => 'buildDownload',
        TOKEN_TOOL_TIP => 'buildToolTip',
        TOKEN_PICTURE => 'buildPicture',
        TOKEN_BULLET => 'buildBullet',
        TOKEN_CHECK => 'buildCheck',
        TOKEN_DELETE => 'buildDeleteIcon',
        TOKEN_ACTION_DELETE => 'buildActionDelete',
        TOKEN_EDIT => 'buildEdit',
        TOKEN_HELP => 'buildHelp',
        TOKEN_INFO => 'buildInfo',
        TOKEN_NEW => 'buildNew',
        TOKEN_SHOW => 'buildShow',
        TOKEN_FILE => 'buildFile',
164
        TOKEN_FILE_DEPRECATED => 'buildFile',
Carsten  Rose's avatar
Carsten Rose committed
165
        TOKEN_GLYPH => 'buildGlyph',
166
        TOKEN_BOOTSTRAP_BUTTON => 'buildBootstrapButton',
167
168
    ];

169
    private $tableVarName = [
Carsten  Rose's avatar
Carsten Rose committed
170
171
172
173
174
175
176
        TOKEN_URL => NAME_URL,
        TOKEN_MAIL => NAME_MAIL,
        TOKEN_PAGE => NAME_PAGE,
        TOKEN_DOWNLOAD => NAME_DOWNLOAD,
        TOKEN_DOWNLOAD_MODE => NAME_DOWNLOAD_MODE,
        TOKEN_TEXT => NAME_TEXT,
        TOKEN_ALT_TEXT => NAME_ALT_TEXT,
177
        TOKEN_BOOTSTRAP_BUTTON => NAME_BOOTSTRAP_BUTTON,
Carsten  Rose's avatar
Carsten Rose committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        TOKEN_TOOL_TIP => NAME_TOOL_TIP,
        TOKEN_PICTURE => NAME_IMAGE,
        TOKEN_BULLET => NAME_IMAGE,
        TOKEN_CHECK => NAME_IMAGE,
        TOKEN_DELETE => NAME_IMAGE,
        TOKEN_EDIT => NAME_IMAGE,
        TOKEN_HELP => NAME_IMAGE,
        TOKEN_INFO => NAME_IMAGE,
        TOKEN_NEW => NAME_IMAGE,
        TOKEN_SHOW => NAME_IMAGE,
        TOKEN_GLYPH => NAME_IMAGE,
        TOKEN_RENDER => NAME_RENDER,
        TOKEN_TARGET => NAME_TARGET,
        TOKEN_CLASS => NAME_LINK_CLASS,
        TOKEN_QUESTION => NAME_QUESTION,
        TOKEN_ENCRYPTION => NAME_ENCRYPTION,
        TOKEN_SIP => NAME_SIP,
        TOKEN_URL_PARAM => NAME_URL_PARAM,
        TOKEN_RIGHT => NAME_RIGHT,
        TOKEN_ACTION_DELETE => NAME_ACTION_DELETE,
        TOKEN_FILE => NAME_FILE,
199
        TOKEN_FILE_DEPRECATED => NAME_FILE,
200
201
    ];

Carsten  Rose's avatar
Carsten Rose committed
202
    // Used to find double definitions.
203
    private $tokenMapping = [
Carsten  Rose's avatar
Carsten Rose committed
204
205
206
        TOKEN_URL => LINK_ANCHOR,
        TOKEN_MAIL => LINK_ANCHOR,
        TOKEN_PAGE => LINK_ANCHOR,
Carsten  Rose's avatar
Carsten Rose committed
207
        TOKEN_DOWNLOAD => LINK_ANCHOR,
Carsten  Rose's avatar
Carsten Rose committed
208

209
        TOKEN_PICTURE => LINK_PICTURE,
Carsten  Rose's avatar
Carsten Rose committed
210
211
212
213
214
215
216
217
218
        TOKEN_BULLET => LINK_PICTURE,
        TOKEN_CHECK => LINK_PICTURE,
        TOKEN_DELETE => LINK_PICTURE,
        TOKEN_EDIT => LINK_PICTURE,
        TOKEN_HELP => LINK_PICTURE,
        TOKEN_INFO => LINK_PICTURE,
        TOKEN_NEW => LINK_PICTURE,
        TOKEN_SHOW => LINK_PICTURE,
        TOKEN_GLYPH => LINK_PICTURE,
219
    ];
220

221
222
223
    /**
     * __construct
     *
224
     * @param Sip $sip
225
     * @param bool $phpUnit
226
     */
227
    public function __construct(Sip $sip, $phpUnit = false) {
Carsten  Rose's avatar
Carsten Rose committed
228
        $this->phpUnit = $phpUnit;
229
230
231
232
233

        if ($phpUnit) {
            $_SERVER['REQUEST_URI'] = 'localhost';
        }

Carsten  Rose's avatar
Carsten Rose committed
234
        $this->sip = $sip;
Carsten  Rose's avatar
Carsten Rose committed
235
        $this->store = Store::getInstance('', $phpUnit);
236
237
        $this->cssLinkClassInternal = $this->store->getVar(SYSTEM_CSS_LINK_CLASS_INTERNAL, STORE_SYSTEM);
        $this->cssLinkClassExternal = $this->store->getVar(SYSTEM_CSS_LINK_CLASS_EXTERNAL, STORE_SYSTEM);
Carsten  Rose's avatar
Carsten Rose committed
238
        $this->ttContentUid = $this->store->getVar(TYPO3_TT_CONTENT_UID, STORE_TYPO3);
239
240
241
242
243
244
245
246
247

        /*
         * mode:
         * 0: no output
         * 1: text
         * 2: url
         * 3: <a href=url>url</a>
         * 4: <a href=url>Text</a>
         *
Carsten  Rose's avatar
Carsten Rose committed
248
         *  r=render mode, u=url, t:text and/or image.
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
         *
         *                  [r][u][t] = mode
         */

        $this->renderControl[0][0][0] = 0;
        $this->renderControl[0][0][1] = 0;
        $this->renderControl[0][1][0] = 3;
        $this->renderControl[0][1][1] = 4;

        $this->renderControl[1][0][0] = 0;
        $this->renderControl[1][0][1] = 1;
        $this->renderControl[1][1][0] = 3;
        $this->renderControl[1][1][1] = 4;

        $this->renderControl[2][0][0] = 0;
        $this->renderControl[2][0][1] = 0;
        $this->renderControl[2][1][0] = 0;
        $this->renderControl[2][1][1] = 4;

        $this->renderControl[3][0][0] = 0;
        $this->renderControl[3][0][1] = 1;
        $this->renderControl[3][1][0] = 2;
        $this->renderControl[3][1][1] = 1;

        $this->renderControl[4][0][0] = 0;
        $this->renderControl[4][0][1] = 1;
        $this->renderControl[4][1][0] = 2;
        $this->renderControl[4][1][1] = 2;

        $this->renderControl[5][0][0] = 0;
        $this->renderControl[5][0][1] = 0;
        $this->renderControl[5][1][0] = 0;
        $this->renderControl[5][1][1] = 0;
282

283
    }
284

285
    /**
286
287
     * In render mode 3,4,5 there is no '<a href ...>'. Nevertheless, tooltip and BS Button should be displaye.
     * Do this by applying a '<span>' attribute around the text.
288
     *
289
290
291
292
     * @param array $vars
     * @param       $keyName
     *
     * @return string
293
     */
294
295
296
297
    private function wrapLinkTextOnly(array $vars, $keyName) {

        $text = $vars[$keyName];
        if ($vars[NAME_BOOTSTRAP_BUTTON] == '' && $vars[FINAL_TOOL_TIP] == '') {
298
299
300
            return $text;
        }

301
302
303
304
305
306
307
        $attributes = Support::doAttribute('title', $vars[FINAL_TOOL_TIP]);

        if ($vars[NAME_BOOTSTRAP_BUTTON] != '') {
            $attributes .= Support::doAttribute('class', [$vars[NAME_BOOTSTRAP_BUTTON], 'disabled']);
        }

        return Support::wrapTag("<span $attributes>", $text);
308
309
310

    }

311
312
313
    /**
     * Build the whole link
     *
314
     * @param string $str Qualifier with params. 'report'-syntax. F.e.:  A:u:www.example.com|G:P:home.gif|t:Home"
Carsten  Rose's avatar
Carsten Rose committed
315
316
317
     *
     * @return string The complete HTML encoded Link like <a href='http://example.com' class='external'><img
     *                src='iconf.gif' title='help text'>Description</a>
318
     * @throws UserReportException
319
     */
Carsten  Rose's avatar
Carsten Rose committed
320
    public function renderLink($str) {
321

322
        $tokenGiven = array();
323

324
        if (empty($str)) {
325
            return '';
326
        }
327

328
        $vars = $this->fillParameter($str, $tokenGiven);
Carsten  Rose's avatar
Carsten Rose committed
329
        $vars = $this->processParameter($vars, $tokenGiven);
330
        $mode = $this->getModeRender($vars);
331

Carsten  Rose's avatar
Carsten Rose committed
332
333
334
335
        if (isset($tokenGiven[TOKEN_DOWNLOAD]) && $tokenGiven[TOKEN_DOWNLOAD] === true) {
            $this->store->setVar(SYSTEM_DOWNLOAD_POPUP, DOWNLOAD_POPUP_REQUEST, STORE_SYSTEM);
        }

336
        $link = '';
337

338
339
        // 0-4 URL, plain email
        // 10-14 encrypted email
340
341
342
343
344
        switch ($mode) {
            // 0: No Output
            case '0':
            case '10':
            case '20':
345
            $link = "";
346
347
348
349
                break;

            // 1: 'text'
            case '1':
350
//                $link = $vars[FINAL_CONTENT];
351
                $link = $this->wrapLinkTextOnly($vars, FINAL_CONTENT);
352
353
                break;
            case '11':
354
//                $link = $vars[FINAL_CONTENT];
355
                $link = $this->wrapLinkTextOnly($vars, FINAL_CONTENT);
356
//                $link = $this->encryptMailtoJS($vars, false);
357
358
359
360
                break;

            // 2: 'url'
            case '2':
361
//                $link = $vars[FINAL_HREF];
362
                $link = $this->wrapLinkTextOnly($vars, FINAL_HREF);
363
364
                break;
            case '12':
365
//                $link = $vars[FINAL_HREF];
366
                $link = $this->wrapLinkTextOnly($vars, FINAL_HREF);
367
//                $link = $this->encryptMailtoJS($vars, false);
368
369
                break;

370
            // 3: <a href=url ...>url</a>
371
            case '3':
Carsten  Rose's avatar
Carsten Rose committed
372
//                $link = $htmlUrl . $vars[NAME_URL] . '</a>' . $vars[NAME_TOOL_TIP_JS][1];
373
                $link = Support::wrapTag($vars[FINAL_ANCHOR], $vars[FINAL_HREF]);
374
375
                break;
            case '13':
376
377
378
                $link = Support::wrapTag($vars[FINAL_ANCHOR], $vars[FINAL_HREF]);
//                $vars[NAME_TEXT] = $vars[NAME_MAIL];
//                $link = $this->encryptMailtoJS($vars, true);
379
380
                break;

381
            // 4: <a href=url ...>Text</a>
382
            case '4':
383
                $link = Support::wrapTag($vars[FINAL_ANCHOR], $vars[FINAL_CONTENT]);
384
385
                break;
            case '14':
386
387
                $link = Support::wrapTag($vars[FINAL_ANCHOR], $vars[FINAL_CONTENT]);
//                $link = $this->encryptMailtoJS($vars, true);
388
389
390
391
392
                break;
            case '21':
            case '22':
            case '23':
            case '24':
393
            //TODO: Alter Code, umstellen auf JS Client von RO. Vorlage koennte 'Delete' in Subrecord sein.
Carsten  Rose's avatar
Carsten Rose committed
394
            $link = "<a href=\"javascript: void(0);\" onClick=\"var del = new FR.Delete({recordId:'',sip:'',forward:'" .
395
                $vars[NAME_PAGE] . "'});\" " . $vars[NAME_LINK_CLASS] . ">" . $vars[NAME_TEXT] . "</a>";
396
            break;
397
398
        }

399
400
401
        return $link;
    }

402
    /**
403
     * Order $param. Parameter with priority are hardcoded at the moment.
404
405
     *
     * @param array $param
Carsten  Rose's avatar
Carsten Rose committed
406
     *
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
     * @return array
     */
    private function paramPriority(array $param) {
        $prio = array();
        $regular = array();

        foreach ($param as $value) {
            switch (substr($value, 0, 1)) {
                case TOKEN_DOWNLOAD:
                    $prio[] = $value;
                    break;
                default:
                    $regular[] = $value;
                    break;
            }
        }

        return array_merge($prio, $regular);
    }
426

427
    /**
428
429
     * Iterate over all given token.
     *
430
     * @param string $str
Carsten  Rose's avatar
Carsten Rose committed
431
     * @param array $tokenGiven
Carsten  Rose's avatar
Carsten Rose committed
432
     *
433
     * @return array
434
     * @throws UserReportException
435
     */
436
    private function fillParameter($str, array &$tokenGiven) {
437

Carsten  Rose's avatar
Carsten Rose committed
438
        // Define all possible vars: no more isset().
439
440
        $vars = $this->initVars();
        $flagArray = array();
441

Carsten  Rose's avatar
Carsten Rose committed
442
        // str="u:http://www.example.com|c:i|t:Hello World|q:Do you really want to delete the record 25:warn:yes:no"
Carsten  Rose's avatar
Carsten Rose committed
443
        $param = explode(PARAM_DELIMITER, $str);
444

445
446
        $param = $this->paramPriority($param);

Carsten  Rose's avatar
Carsten Rose committed
447
        // Parse all parameter, fill variables.
448
        foreach ($param as $item) {
449

450
            // Skip empty entries
451
452
453
            if ($item === '') {
                continue;
            }
454

455
456
457
458
            // u:www.example.com
            $arr = explode(":", $item, 2);
            $key = isset($arr[0]) ? $arr[0] : '';
            $value = isset($arr[1]) ? $arr[1] : '';
459

460
            // Bookkeeping defined parameter.
461
462
463
464
465
466
            if (isset($tokenGiven[$key])) {
                throw new UserReportException ("Multiple definitions for key '$key'", ERROR_MULTIPLE_DEFINITION);
            }
            $tokenGiven[$key] = true;

            if (!isset($this->tableVarName[$key])) {
467
                throw new UserReportException ("Unknown link qualifier: '$key' - do you forget the one character qualifier?", ERROR_UNKNOWN_LINK_QUALIFIER);
468
            }
Carsten  Rose's avatar
Carsten Rose committed
469

470
471
            $keyName = $this->tableVarName[$key]; // convert token to name

Carsten  Rose's avatar
Carsten Rose committed
472
            $value = $this->checkForEmptyValue($key, $value);
473
            $value = $this->checkValue($key, $value);
474

475
            // Store value
476
            if (isset($tokenGiven[TOKEN_DOWNLOAD]) && ($key == TOKEN_PAGE || $key == TOKEN_URL || $key == TOKEN_URL_PARAM || $key == TOKEN_FILE || $key == TOKEN_FILE_DEPRECATED)) {
Carsten  Rose's avatar
Carsten Rose committed
477
478
479
480
481
482

                $vars[NAME_DOWNLOAD_ELEMENTS][] = $key . ':' . $value;

                unset($tokenGiven[$key]); // Skip Bookkeeping for TOKEN_URL_PARAM | TOKEN_FILE | TOKEN_URL.
                continue;
            } else {
483
484
485
486
                // TOKEN_GLYPH should not treated as an regular image. Same applies to the other Glyph symbols, but those don't have a value, and therefore do not fill $vars['image'].
                if ($key != TOKEN_GLYPH) {
                    $vars[$keyName] = $value;
                }
Carsten  Rose's avatar
Carsten Rose committed
487
488
            }

489
490
491
492
493
            // Check for double anchor or picture definition
            if (isset($this->tokenMapping[$key])) {
                $type = $this->tokenMapping[$key];

                if (isset($flagArray[$type])) {
Carsten  Rose's avatar
Carsten Rose committed
494
                    throw new UserReportException ("Multiple definitions of url/mail/page/download or picture", ERROR_MULTIPLE_DEFINITION);
495
496
497
498
499
500
501
                }
                $flagArray[$type] = true;

//                if ($type === LINK_PICTURE) {
//                    $build = 'build' . strtoupper($keyName[0]) . substr($keyName, 1);
//                    $this->$build($vars, $value);
//                }
502
503
            }

504
505
506
507
            if (isset($this->callTable[$key])) {
                $build = $this->callTable[$key];
                $vars = $this->$build($vars, $value);
            }
508
509
        }

510
        // Final Checks
Carsten  Rose's avatar
Carsten Rose committed
511
        $this->checkParam($tokenGiven, $vars);
512

513
514
515
516
517
518
519
520
521
        return $vars;
    }

    /**
     * Cleans and make existing the standard vars used every time to render a link.
     *
     * @return array
     */
    private function initVars() {
522

523
        return [
Carsten  Rose's avatar
Carsten Rose committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
            NAME_MAIL => '',
            NAME_URL => '',
            NAME_PAGE => '',

            NAME_TEXT => '',
            NAME_ALT_TEXT => '',
            NAME_BOOTSTRAP_BUTTON => '',
            NAME_IMAGE => '',
            NAME_IMAGE_TITLE => '',
            NAME_GLYPH => '',
            NAME_GLYPH_TITLE => '',
            NAME_QUESTION => '',
            NAME_TARGET => '',
            NAME_TOOL_TIP => '',
            NAME_TOOL_TIP_JS => '',
            NAME_URL_PARAM => '',
Carsten  Rose's avatar
Carsten Rose committed
540
            NAME_EXTRA_CONTENT_WRAP => '',
Carsten  Rose's avatar
Carsten Rose committed
541
            NAME_DOWNLOAD_MODE => '',
Carsten  Rose's avatar
Carsten Rose committed
542
            NAME_DOWNLOAD_ELEMENTS => array(),
543

Carsten  Rose's avatar
Carsten Rose committed
544
545
546
547
548
            NAME_RENDER => '0',
            NAME_RIGHT => 'l',
            NAME_SIP => '0',
            NAME_ENCRYPTION => '0',
            NAME_DELETE => '',
549

Carsten  Rose's avatar
Carsten Rose committed
550
            NAME_LINK_CLASS => '', // class name
551
552
            NAME_LINK_CLASS_DEFAULT => '', // Depending of 'as page' or 'as url'. Only used if class is not explizit set.

Carsten  Rose's avatar
Carsten Rose committed
553
            NAME_ACTION_DELETE => '',
554

Carsten  Rose's avatar
Carsten Rose committed
555
556
557
558
559
560
            FINAL_HREF => '',
            FINAL_CONTENT => '',
            FINAL_SYMBOL => '',
            FINAL_TOOL_TIP => '',
            FINAL_CLASS => '',
            FINAL_QUESTION => '',
561
        ];
562

563
564
    }

565
    /**
Carsten  Rose's avatar
Carsten Rose committed
566
     * Verify Empty values. If appropriate, set defaults, if not throw an exception.
567
     *
Carsten  Rose's avatar
Carsten Rose committed
568
     * @param string $key
Carsten  Rose's avatar
Carsten Rose committed
569
     * @param string $value
Carsten  Rose's avatar
Carsten Rose committed
570
     *
571
572
573
     * @return string
     * @throws UserReportException
     */
Carsten  Rose's avatar
Carsten Rose committed
574
575
576
577
578
    private function checkForEmptyValue($key, $value) {

        if ($value !== '') {
            return $value;
        }
Carsten  Rose's avatar
Carsten Rose committed
579
580
        $value = '';

581
582
583
        switch ($key) {
            case TOKEN_URL:
            case TOKEN_MAIL:
584
            case TOKEN_GLYPH:
585
586
587
588
589
590
591
592
593
594
595
596
597
598
                throw new UserReportException ("Missing value for token '$key'", ERROR_MISSING_VALUE);
                break;
            case TOKEN_PAGE:
                $value = $this->store->getVar(TYPO3_PAGE_ID, STORE_TYPO3); // If no pageid|pagealias is defined, take current page
                break;
            case TOKEN_RIGHT:
                $value = 'r';
                break;
            case TOKEN_ENCRYPTION:
                $value = '1';
                break;
            case TOKEN_SIP:
                $value = '1';
                break;
599
600
601
            case TOKEN_BOOTSTRAP_BUTTON:
                $value = '1';
                break;
Carsten  Rose's avatar
Carsten Rose committed
602
603
604
            case TOKEN_QUESTION:
                $value = DEFAULT_QUESTION_TEXT;
                break;
605
606
607
            case TOKEN_RENDER:
                $value = DEFAULT_RENDER_MODE;
                break;
608
609
610
            case TOKEN_ACTION_DELETE:
                $value = DEFAULT_ACTION_DELETE;
                break;
611
612
613
614
615
616
617
            default:
        }

        return $value;
    }

    /**
618
619
     * Validate value for token
     *
620
621
     * @param $key
     * @param $value
Carsten  Rose's avatar
Carsten Rose committed
622
     *
623
624
625
626
627
628
629
630
     * @return mixed
     * @throws UserReportException
     */
    private function checkValue($key, $value) {
        switch ($key) {
            case TOKEN_ENCRYPTION:
            case TOKEN_SIP:
                if ($value !== '0' && $value !== '1') {
631
                    throw new UserReportException ("Invalid value for token '$key': '$value''", ERROR_INVALID_VALUE);
632
633
                }
                break;
634
635
636
637
638
639
640
641
642
643
            case TOKEN_ACTION_DELETE:
                switch ($value) {
                    case TOKEN_ACTION_DELETE_AJAX:
                    case TOKEN_ACTION_DELETE_REPORT:
                    case TOKEN_ACTION_DELETE_CLOSE:
                        break;
                    default:
                        throw new UserReportException ("Invalid value for token '$key': '$value''", ERROR_INVALID_VALUE);
                }
                break;
644
645
646
647
648
            case TOKEN_BOOTSTRAP_BUTTON:
                if ($value == '1') {
                    $value = 'btn-default';
                }
                break;
649
650
651
652
653
654
            default:
        }

        return $value;
    }

655
656
657
658
    /**
     * Check for double definition.
     *
     * @param array $tokenGiven
Carsten  Rose's avatar
Carsten Rose committed
659
     *
660
661
     * @throws UserReportException
     */
Carsten  Rose's avatar
Carsten Rose committed
662
    private function checkParam(array $tokenGiven, array $vars) {
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
        $countLinkAnchor = 0;
        $countLinkPicture = 0;

        foreach ($tokenGiven as $token => $value) {
            if (isset($this->tokenMapping[$token])) {
                switch ($this->tokenMapping[$token]) {
                    case LINK_ANCHOR:
                        $countLinkAnchor++;
                        break;
                    case LINK_PICTURE:
                        $countLinkPicture++;
                        break;
                    default:
                        break;
                }
            }
        }

        if ($countLinkAnchor > 1) {
682
683
            throw new UserReportException ("Multiple URL / PAGE and MAILTO definition", ERROR_MULTIPLE_URL_PAGE_MAILTO_DEFINITION);
        }
684

685
686
687
688
689
690
        if ($countLinkPicture > 1) {
            throw new UserReportException ("Multiple definitions for token picture/bullet/check/edit...delete'" . TOKEN_PAGE . "'", ERROR_MULTIPLE_DEFINITION);
        }

        if (isset($tokenGiven[TOKEN_MAIL]) && isset($tokenGiven[TOKEN_TARGET])) {
            throw new UserReportException ("Token Mail and Target at the same time not possible'" . TOKEN_PAGE . "'", ERROR_MULTIPLE_DEFINITION);
691
        }
Carsten  Rose's avatar
Carsten Rose committed
692
693
694
695

        if (isset($tokenGiven[TOKEN_DOWNLOAD]) && count($vars[NAME_DOWNLOAD_ELEMENTS]) == 0) {
            throw new UserReportException ("Missing element sources for download", ERROR_MISSING_REQUIRED_PARAMETER);
        }
696
    }
697
698

    /**
699
     * Compute final link parameter.
700
     *
Carsten  Rose's avatar
Carsten Rose committed
701
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
702
     * @param array $tokenGiven
Carsten  Rose's avatar
Carsten Rose committed
703
     *
704
705
     * @return string
     * @throws UserReportException
706
     */
Carsten  Rose's avatar
Carsten Rose committed
707
    private function processParameter(array $vars, array $tokenGiven) {
708

Carsten  Rose's avatar
Carsten Rose committed
709
        $vars[FINAL_HREF] = $this->doHref($vars, $tokenGiven); // must be called before doToolTip()
710
711
712
        $vars[FINAL_TOOL_TIP] = $this->doToolTip($vars);
        $vars[FINAL_CLASS] = $this->doCssClass($vars);
        $vars[FINAL_SYMBOL] = $this->doSymbol($vars);
Carsten  Rose's avatar
Carsten Rose committed
713
714
        $vars[FINAL_CONTENT] = $this->doContent($vars); // must be called after doSymbol()
        $vars[FINAL_QUESTION] = $this->doQuestion($vars);
715
        $vars[FINAL_ANCHOR] = $this->doAnchor($vars);
716

717
        return $vars;
718
    }
719

Carsten  Rose's avatar
Carsten Rose committed
720
721
    /**
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
722
     *
Carsten  Rose's avatar
Carsten Rose committed
723
724
725
726
727
728
729
730
731
732
733
     * @return string
     * @throws UserFormException
     */
    private function getDownloadModeNCheck(array $vars) {

        $cnt = count($vars[NAME_DOWNLOAD_ELEMENTS]);
        $mode = $vars[NAME_DOWNLOAD_MODE];

        // Determine default.
        if ($mode == '') {
            if ($cnt == 1) {
734
                $mode = (substr($vars[NAME_DOWNLOAD_ELEMENTS][0], 0, 1) == TOKEN_FILE_DEPRECATED) ? DOWNLOAD_MODE_FILE : DOWNLOAD_MODE_PDF;
Carsten  Rose's avatar
Carsten Rose committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
            } else {
                $mode = DOWNLOAD_MODE_PDF;
            }
        }

        // Do some checks.
        switch ($mode) {
            case DOWNLOAD_MODE_PDF:
            case DOWNLOAD_MODE_ZIP:
                break;
            case DOWNLOAD_MODE_FILE:
            case DOWNLOAD_MODE_EXCEL:
                if ($cnt > 1) {
                    throw new UserFormException("With 'downloadMode' = 'file' or 'excle', only one element source is allowed.", ERROR_DOWNLOAD_UNEXPECTED_NUMBER_OF_SOURCES);
                }
                break;
            default:
                throw new UserFormException("Unknown mode: $mode", ERROR_UNKNOWN_MODE);
                break;

        }

        return $mode;
    }

760
    /**
761
     * Concat final HREF string
762
     *
763
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
764
     * @param array $tokenGiven
Carsten  Rose's avatar
Carsten Rose committed
765
     *
766
     * @return string
Carsten  Rose's avatar
Carsten Rose committed
767
     * @throws CodeException
768
     * @throws UserReportException
769
     */
Carsten  Rose's avatar
Carsten Rose committed
770
    private function doHref(array &$vars, array $tokenGiven) {
771
        $urlNParam = '';
772

Carsten  Rose's avatar
Carsten Rose committed
773
774
775
776
777
778
779
780
781
782
783
784
785

        if (isset($tokenGiven[TOKEN_DOWNLOAD])) {
            // Message in download popup.
            $altText = ($vars[NAME_ALT_TEXT] == '') ? 'Please wait' : addslashes($vars[NAME_ALT_TEXT]);
            $vars[NAME_EXTRA_CONTENT_WRAP] = str_replace(DOWNLOAD_POPUP_REPLACE_TEXT, $altText, $vars[NAME_EXTRA_CONTENT_WRAP]);
            $vars[NAME_EXTRA_CONTENT_WRAP] = str_replace(DOWNLOAD_POPUP_REPLACE_TITLE, 'Download: ' . addslashes($vars[NAME_DOWNLOAD]), $vars[NAME_EXTRA_CONTENT_WRAP]);

            $tmpUrlParam[DOWNLOAD_MODE] = $this->getDownloadModeNCheck($vars);
            $tmpUrlParam[DOWNLOAD_EXPORT_FILENAME] = $vars[NAME_DOWNLOAD];
            $tmpUrlParam[SIP_DOWNLOAD_PARAMETER] = base64_encode(implode(PARAM_DELIMITER, $vars[NAME_DOWNLOAD_ELEMENTS]));
            $vars[NAME_URL_PARAM] = KeyValueStringParser::unparse($tmpUrlParam, '=', '&');
        }

786
787
788
789
        if ($vars[NAME_ACTION_DELETE] !== '') {
            $vars[NAME_URL_PARAM] = $this->adjustDeleteParameter($vars[NAME_ACTION_DELETE], $vars[NAME_URL_PARAM]);
        }

790
        if ($vars[NAME_MAIL] === '') {
791
792
793
794
795

            if (substr($vars[NAME_URL], 0, 1) === '?') {
                $vars[NAME_URL] = INDEX_PHP . $vars[NAME_URL];
            }

796
797
            // Either NAME_URL is empty or NAME_PAGE is empty
            $urlNParam = Support::concatUrlParam($vars[NAME_URL] . $vars[NAME_PAGE], $vars[NAME_URL_PARAM]);
798

Carsten  Rose's avatar
Carsten Rose committed
799
            if ($vars[NAME_SIP] === "1") {
800
801
                $paramArray = $this->sip->queryStringToSip($urlNParam, RETURN_ARRAY);
                $urlNParam = $paramArray['_url'];
802

803
                if (Support::findInSet(SYSTEM_SHOW_DEBUG_INFO_YES, $this->store->getVar(SYSTEM_SHOW_DEBUG_INFO, STORE_SYSTEM))) {
Carsten  Rose's avatar
Carsten Rose committed
804
                    $vars[NAME_TOOL_TIP] .= PHP_EOL . PHP_EOL . $this->sip->debugSip($paramArray);
805
806
                }
            }
807
808
            // Link: MAILTO
        } else {
809
            // If there is no encryption: handle the mailto as an ordinary URL
810
            if ($vars[NAME_ENCRYPTION] === '1') {
811
                throw new UserReportException ("Oops, sorry: encrypted mail not implemented ...", ERROR_NOT_IMPLEMENTED);
812
813
            } else {
                $urlNParam = "mailto:" . $vars[NAME_MAIL];
814
815
816
            }
        }

817
818
        return $urlNParam;
    }
819

820
821
822
    /**
     * @param $tokenActionDelete
     * @param $nameUrlParam
Carsten  Rose's avatar
Carsten Rose committed
823
     *
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
     * @return string
     * @throws UserReportException
     */
    private function adjustDeleteParameter($tokenActionDelete, $nameUrlParam) {

        $kvp = new KeyValueStringParser();

        // Split in: [p => 'r=100&table=note&..', 'D' => ''... ],
//        $param = $kvp->parse($nameUrlParam, ':', '|');

//        Support::setIfNotSet($param, TOKEN_URL_PARAM);

        switch ($tokenActionDelete) {
            case TOKEN_ACTION_DELETE_AJAX:
                // TODO: Implement for AJAX (subrecord)
                throw new UserReportException ("Not implemented!", ERROR_NOT_IMPLEMENTED);
                break;
            case TOKEN_ACTION_DELETE_REPORT:
                $nameUrlParam .= '&' . SIP_MODE_ANSWER . '=' . MODE_HTML;
                $nameUrlParam .= '&' . SIP_TARGET_URL . '=' . $_SERVER['REQUEST_URI'];
                break;
            case TOKEN_ACTION_DELETE_CLOSE:
                // TODO: Implement for Form (primary Record wird geloescht)
                throw new UserReportException ("Not implemented!", ERROR_NOT_IMPLEMENTED);
                break;
            default:
                throw new UserReportException ("Invalid value for token '" . TOKEN_ACTION_DELETE . "': '$tokenActionDelete''", ERROR_INVALID_VALUE);
        }

        return $nameUrlParam;
    }

856
857
858
859
    /**
     * Return $vars[NAME_TOOL_TIP]. If $vars[NAME_TOOL_TIP] is empty, set $vars[NAME_GLYPH_TITLE] as tooltip.
     *
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
860
     *
861
862
863
     * @return mixed
     */
    private function doToolTip(array $vars) {
864

865
866
867
        // Set default tooltip
        if ($vars[NAME_TOOL_TIP] == '') {
            $vars[NAME_TOOL_TIP] = $vars[NAME_GLYPH_TITLE];
Carsten  Rose's avatar
Carsten Rose committed
868
        }
869

870
        return $vars[NAME_TOOL_TIP];
871
    }
872

873
    /**
874
     * Parse CSS Class Settings
875
     *
876
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
877
     *
878
     * @return    string
879
     */
880
    private function doCssClass(array $vars) {
881

882
        $class = ($vars[NAME_LINK_CLASS] === '') ? $vars[NAME_LINK_CLASS_DEFAULT] : $vars[NAME_LINK_CLASS];
Carsten  Rose's avatar
Carsten Rose committed
883

884
885
886
887
888
889
890
891
892
893
894
        switch ($class) {
            case TOKEN_CLASS_NONE:
                $class = '';
                break;
            case TOKEN_CLASS_INTERNAL:
            case TOKEN_CLASS_EXTERNAL:
                $class = $this->linkClassSelector[$vars[NAME_LINK_CLASS]] . ' ';
                break;
            default:
                break;
        }
895

896
897
898
        if ($class === NO_CLASS) {
            $class = '';
        }
899

900
        //TODO: Old way to detect if BS Button should be rendered - should be replaced by 'b:'
901
        if ($vars[NAME_GLYPH] !== '' && $vars[NAME_EXTRA_CONTENT_WRAP] == '' && $vars[NAME_BOOTSTRAP_BUTTON] == '') {
902
            $class = 'btn btn-default ' . $class;
903
        }
904

905
        return implode(' ', [$vars[NAME_BOOTSTRAP_BUTTON], $class]);
906
    }
907

908
909
910
    /**
     * Create Image HTML Tag
     *
911
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
912
     *
913
     * @return string
914
     */
915
    private function doSymbol(array $vars) {
916
        $tags = '';
917
        $html = '';
918
919

        // Build Image
920
        if ($vars[NAME_IMAGE] !== '') {
921
922
            $tags .= Support::doAttribute('alt', $vars[NAME_ALT_TEXT]);
            $tags .= Support::doAttribute('src', $vars[NAME_IMAGE]);
923
            $tags .= Support::doAttribute('title', $vars[FINAL_TOOL_TIP] === '' ? $vars[NAME_IMAGE_TITLE] : $vars[FINAL_TOOL_TIP]);
924
            $html .= '<img ' . $tags . '>';
925
926
        }

927
928
929
930
931
932
        if ($vars[NAME_GLYPH] !== '') {
            $tags = Support::doAttribute('class', 'glyphicon ' . $vars[NAME_GLYPH]);
            $html .= Support::wrapTag('<span ' . $tags . '>', '', false);
        }

        return $html;
933
    }
934

935
936
937
938
939
    /**
     * Concat Text and/or Image and or Glyph. $vars[NAME_RIGHT_PICTURE_POSITION]
     * Concat Text and/or Image and or Glyph. Depending of $vars[NAME_RIGHT_PICTURE_POSITION], swapp the position,
     *
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
940
     *
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
     * @return string
     */
    private function doContent(array $vars) {

        $htmlImage = $vars[FINAL_SYMBOL];

        // Compose Image & Text
        if ($htmlImage === '') {
            $content = $vars[NAME_TEXT];
        } elseif ($vars[NAME_TEXT] === '') {
            $content = $htmlImage;
        } else {
            if ($vars[NAME_RIGHT] === "l") {
                $content = implode(' ', [$htmlImage, $vars[NAME_TEXT]]);
            } else {
                $content = implode(' ', [$vars[NAME_TEXT], $htmlImage]);
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
960
961
962
963
        if ($vars[NAME_EXTRA_CONTENT_WRAP] != '') {
            $content = Support::wrapTag($vars[NAME_EXTRA_CONTENT_WRAP], $content);
        }

964
965
966
        return $content;
    }

Carsten  Rose's avatar
Carsten Rose committed
967
968
969
970
971
    /**
     * Build 'alert' JS. $vars['question'] = '<text>:<color>:<button ok>:<button fail>'
     * Return JS Code to place in '<a>' tag. Be carefull: function creates a uniqe 'id' tag.
     *
     * @param array $vars
Carsten  Rose's avatar
Carsten Rose committed
972
     *
Carsten  Rose's avatar
Carsten Rose committed
973
974
975
976
977
978
979
980
981
     * @return string
     */
    private function doQuestion(array $vars) {

        if ($vars[NAME_QUESTION] === '') {
            return '';
        }

        $arr = OnArray::explodeWithoutEscaped(':', $vars[NAME_QUESTION]);
982
        $arr = array_merge($arr, ['', '', '', '', '', '']);
Carsten  Rose's avatar
Carsten Rose committed
983
984
985
986
987
988
989
990

        $id = ($this->phpUnit === true) ? '12345' : uniqid('a_');
        $content = Support::doAttribute('id', $id);

        $text = $arr[QUESTION_INDEX_TEXT] === '' ? DEFAULT_QUESTION_TEXT : $arr[QUESTION_INDEX_TEXT];
        $level = ($arr[QUESTION_INDEX_LEVEL] === '') ? DEFAULT_QUESTION_LEVEL : $arr[QUESTION_INDEX_LEVEL];
        $ok = ($arr[QUESTION_INDEX_BUTTON_OK] === '') ? 'Ok' : $arr[QUESTION_INDEX_BUTTON_OK];
        $cancel = ($arr[QUESTION_INDEX_BUTTON_FALSE] === '') ? 'Cancel' : $arr[QUESTION_INDEX_BUTTON_FALSE];
991
992
993
        $timeout = ($arr[QUESTION_INDEX_TIMEOUT] === '') ? '0' : $arr[QUESTION_INDEX_TIMEOUT] * 1000;
        $flagModalStatus = ($arr[QUESTION_INDEX_FLAG_MODAL] === '') ? '1' : $arr[QUESTION_INDEX_FLAG_MODAL];
        $flagModal = ($flagModalStatus === "1") ? 'true' : 'false';
Carsten  Rose's avatar
Carsten Rose committed
994
995

        $js = <<<EOF
996
var alert = new QfqNS.Alert({ message: '$text', type: '$level', modal: $flagModal, timeout: $timeout, buttons: [
Carsten  Rose's avatar
Carsten Rose committed
997
998
    { label: '$ok', eventName: 'ok' },
    { label: '$cancel',eventName: 'cancel'}
999
] } );
Carsten  Rose's avatar
Carsten Rose committed
1000
alert.on('alert.ok', function() {