• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 10266633887

06 Aug 2024 12:36PM UTC coverage: 23.309%. Remained the same
10266633887

push

github

koertho
do not rely on request object from request stack in DC_Table_Utils

0 of 1 new or added line in 1 file covered. (0.0%)

1320 of 5663 relevant lines covered (23.31%)

1.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Driver/DC_Table_Utils.php
1
<?php
2

3
/*
4
 * Copyright (c) 2021 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Driver;
10

11
use Contao\Controller;
12
use Contao\DataContainer;
13
use Contao\DC_Table;
14
use Contao\Environment;
15
use Contao\Model;
16
use Contao\RequestToken;
17
use Contao\System;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20

21
class DC_Table_Utils extends DC_Table
22
{
23
    /**
24
     * Initialize the object.
25
     *
26
     * @param string $strTable
27
     * @param array  $arrModule
28
     */
29
    public function __construct($strTable, $arrModule = [])
30
    {
31
        DataContainer::__construct();
×
32

33
        /** @var SessionInterface $objSession */
34
        $objSession = System::getContainer()->get('session');
×
NEW
35
        $request = System::getContainer()->get('request_stack')->getCurrentRequest() ?? new Request();
×
36

37
        // Check the request token (see #4007)
38
        if ($request->query->has('act')) {
×
39
            if (!$request->query->get('rt') || !RequestToken::validate($request->query->get('rt'))) {
×
40
                $objSession->set('INVALID_TOKEN_URL', Environment::get('request'));
×
41
                $this->redirect('contao/confirm.php');
×
42
            }
43
        }
44

45
        Controller::loadDataContainer($strTable);
×
46

47
        $this->intId = $request->query->get('id');
×
48

49
        // Clear the clipboard
50
        if ($request->query->has('clipboard')) {
×
51
            $objSession->set('CLIPBOARD', []);
×
52
            $this->redirect($this->getReferer());
×
53
        }
54

55
        // Check whether the table is defined
56
        if ('' == $strTable || !isset($GLOBALS['TL_DCA'][$strTable])) {
×
57
            System::getContainer()->get('monolog.logger.contao')->log('Could not load the data container configuration for "'.$strTable.'"', __METHOD__, TL_ERROR);
×
58
            trigger_error('Could not load the data container configuration', E_USER_ERROR);
×
59
        }
60

61
        // Set IDs and redirect
62
        if ('tl_select' == $request->request->get('FORM_SUBMIT')) {
×
63
            $ids = $request->request->get('IDS');
×
64

65
            if (empty($ids) || !\is_array($ids)) {
×
66
                $this->reload();
×
67
            }
68

69
            $session = $objSession->all();
×
70
            $session['CURRENT']['IDS'] = $ids;
×
71
            $objSession->replace($session);
×
72

73
            if ($request->request->has('edit')) {
×
74
                $this->redirect(str_replace('act=select', 'act=editAll', \Environment::get('request')));
×
75
            } elseif ($request->request->has('delete')) {
×
76
                $this->redirect(str_replace('act=select', 'act=deleteAll', \Environment::get('request')));
×
77
            } elseif ($request->request->has('override')) {
×
78
                $this->redirect(str_replace('act=select', 'act=overrideAll', \Environment::get('request')));
×
79
            } elseif ($request->request->has('cut') || $request->request->has('copy')) {
×
80
                $arrClipboard = $objSession->get('CLIPBOARD');
×
81

82
                $arrClipboard[$strTable] = [
×
83
                    'id' => $ids,
×
84
                    'mode' => ($request->request->has('cut') ? 'cutAll' : 'copyAll'),
×
85
                ];
×
86

87
                $objSession->set('CLIPBOARD', $arrClipboard);
×
88

89
                // Support copyAll in the list view (see #7499)
90
                if ($request->request->has('copy') && $GLOBALS['TL_DCA'][$strTable]['list']['sorting']['mode'] < 4) {
×
91
                    $this->redirect(str_replace('act=select', 'act=copyAll', \Environment::get('request')));
×
92
                }
93

94
                $this->redirect($this->getReferer());
×
95
            }
96
        }
97

98
        $this->strTable = $strTable;
×
99
        $this->ptable = $GLOBALS['TL_DCA'][$this->strTable]['config']['ptable'] ?? null;
×
100
        $this->ctable = $GLOBALS['TL_DCA'][$this->strTable]['config']['ctable'] ?? null;
×
101
        $this->treeView = isset($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['mode']) && \in_array($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['mode'], [5, 6]);
×
102
        $this->root = null;
×
103
        $this->arrModule = $arrModule;
×
104

105
        // FIX: Don't call onload_callbacks for performance reasons
106

107
        // Get the IDs of all root records (tree view)
108
        if ($this->treeView) {
×
109
            $table = (6 == $GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['mode']) ? $this->ptable : $this->strTable;
×
110

111
            // Unless there are any root records specified, use all records with parent ID 0
112
            if (!isset($GLOBALS['TL_DCA'][$table]['list']['sorting']['root']) || (isset($GLOBALS['TL_DCA'][$table]['list']['sorting']['root']) && false === $GLOBALS['TL_DCA'][$table]['list']['sorting']['root'])) {
×
113
                $objIds = $this->Database->prepare('SELECT id FROM '.$table.' WHERE pid=?'.($this->Database->fieldExists('sorting', $table) ? ' ORDER BY sorting' : ''))->execute(0);
×
114

115
                if ($objIds->numRows > 0) {
×
116
                    $this->root = $objIds->fetchEach('id');
×
117
                }
118
            } // Get root records from global configuration file
119
            elseif (isset($GLOBALS['TL_DCA'][$table]['list']['sorting']['root']) && \is_array($GLOBALS['TL_DCA'][$table]['list']['sorting']['root'])) {
×
120
                $this->root = $this->eliminateNestedPages($GLOBALS['TL_DCA'][$table]['list']['sorting']['root'], $table, $this->Database->fieldExists('sorting', $table));
×
121
            }
122
        } // Get the IDs of all root records (list view or parent view)
123
        elseif (isset($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['root']) && \is_array($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['root'])) {
×
124
            $this->root = array_unique($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['root']);
×
125
        }
126

127
        $route = $request->attributes->get('_route');
×
128

129
        // Store the current referer
130
        if (!empty($this->ctable) && !$request->query->get('act') && !$request->query->get('key') && !$request->query->get('token') && 'contao_backend' == $route
×
131
            && !Environment::get('isAjaxRequest')) {
×
132
            $strKey = $request->query->get('popup') ? 'popupReferer' : 'referer';
×
133
            $strRefererId = $request->attributes->get('_contao_referer_id');
×
134

135
            $session = $objSession->get($strKey);
×
136
            $session[$strRefererId][$this->strTable] = substr(\Environment::get('requestUri'), \strlen(\Environment::get('path')) + 1);
×
137
            $objSession->set($strKey, $session);
×
138
        }
139
    }
140

141
    /**
142
     * Create a DataContainer instance from a given Model.
143
     *
144
     * @return static
145
     */
146
    public static function createFromModel(Model $model)
147
    {
148
        $table = $model->getTable();
×
149

150
        $dc = new static($table);
×
151

152
        $dc->strTable = $model->getTable();
×
153
        $dc->activeRecord = $model;
×
154
        $dc->intId = $model->id;
×
155

156
        return $dc;
×
157
    }
158

159
    /**
160
     * Create a DataContainer instance from given model data.
161
     *
162
     * @param Model  $model
163
     * @param string $field
164
     *
165
     * @return static
166
     */
167
    public static function createFromModelData(array $modelData, string $table, string $field = null)
168
    {
169
        $dc = new static($table);
×
170

171
        $dc->strTable = $table;
×
172
        $dc->activeRecord = null;
×
173

174
        if (isset($modelData['id']) && $modelData['id'] > 0) {
×
175
            $dc->activeRecord = System::getContainer()->get('huh.utils.model')->findModelInstanceByPk($table, $modelData['id']);
×
176
            $dc->intId = $modelData['id'];
×
177
        }
178

179
        if ($field) {
×
180
            $dc->strField = $field;
×
181
        }
182

183
        return $dc;
×
184
    }
185
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc