PageLayoutView.php 7.68 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

namespace Lu\LuSearch\Hooks\PageLayoutView;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\DocumentTemplate;
use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore;
use \TYPO3\CMS\Backend\View\PageLayoutView;
use \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;

/**
 * Contains a preview rendering
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 1.0.0
 * @since   02.09.2020
 *
 * @package Lu\LuSearch\Hooks\PageLayoutView
 */
class NewContentElementPreviewRenderer implements PageLayoutViewDrawItemHookInterface
{
    /**
     * Extension key
     *
     * @var string
     */
    const KEY = 'lu_search';

    /**
     * Path to the locallang file
     *
     * @var string
     */
    const LLPATH = 'LLL:EXT:'.self::KEY.'/Resources/Private/Language/locallang.xlf:';

    /**
     * Flexform information
     *
     * @var array
     */
    public $flexformData = [];

    /**
     * Pre-processes the preview rendering of a content element of type "My new content element"
     *
     * @param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject  Calling parent object
     * @param bool                                   $drawItem      Whether to draw the item using the default functionality
     * @param string                                 $headerContent Header content
     * @param string                                 $itemContent   Item content
     * @param array                                  $row           Record row of tt_content
     *
     * @return void
     */
    public function preProcess(PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row)
    {
        // Notice: Always check if your extions, or you will overwrite other plugin data!
        if ($row['list_type'] == 'lusearch_pi1') {
            $itemContent        = '<p><strong><img src="/typo3conf/ext/lu_search/Resources/Public/Icons/Extension.svg" width="32" height="32"> '.$this->getLanguageService()->sL(self::LLPATH.'extension.title').'</strong></p>';
            $this->flexformData = GeneralUtility::xml2array($row['pi_flexform']);

            // Add additional plugin configuration info
            if ($this->getFieldFromFlexform('settings.limitToDomain') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.limit_to_domain');
                $itemContent .= ' <strong>'.$this->getParentSite()->getBase()->getHost().'</strong>'.'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchNews') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_news').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchContacts') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_contacts').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchContent') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_content').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchEvents') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_events').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
98
99
100
101
102
103
104
105
106
107
108
109
            if ($this->getFieldFromFlexform('settings.searchCourse') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_course').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchCourseContinue') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_course_continue').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchProgram') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_program').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
            if ($this->getFieldFromFlexform('settings.searchProgramContinue') == 1) {
                $itemContent .= $this->getLanguageService()->sL(self::LLPATH.'plugin.search_program_continue').'<i class="fa fa-check" style="margin-left: 20px;"></i><br/>';
            }
Dainis Abols's avatar
Dainis Abols committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

            // Make pre format
            $itemContent = '<span class="exampleContent"><pre style="white-space:normal; margin-top: 10px;">'.$itemContent.'</pre></span>';

            $drawItem = false;
        }
    }

    /**
     * Get field value from flexform configuration,
     * including checks if flexform configuration is available
     *
     * @param string $key   name of the key
     * @param string $sheet name of the sheet
     *
     * @return string|NULL if nothing found, value if found
     */
    public function getFieldFromFlexform($key, $sheet = 'sDEF')
    {
        $flexform = $this->flexformData;
        if (isset($flexform['data'])) {
            $flexform = $flexform['data'];
            if (isset($flexform[$sheet]['lDEF'][$key]['vDEF'])
            ) {
                return $flexform[$sheet]['lDEF'][$key]['vDEF'];
            }
        }

        return null;
    }

    /**
     * Return language service instance
     *
     * @return \TYPO3\CMS\Core\Localization\LanguageService
     */
    public function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }

    /**
     * Recursive method for finding site objects
     *
     * @param int $current_id
     *
     * @return array|\TYPO3\CMS\Core\Site\Entity\Site
     */
    private function getParentSite($current_id = 0)
    {
        // Get current ID if parent ID = 0
        if (!$current_id) {
            $current_id = (int)GeneralUtility::_GP('id');
        }
        $parent_id = $this->getParentId($current_id);

        // Check if parent exists
        if ($parent_id < 1) {
            return [];
        }

        // Try to find site info
        try {
            $sites = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($parent_id);
        } catch (SiteNotFoundException $e) {
            $sites = [];
        }

        // Go up, if no site founf
        if (empty($sites)) {
            return $this->getParentSite($parent_id);
        }

        // Return site data
        return $sites;
    }

    /**
     * Fetch current page parent id
     *
     * @param $current_id
     *
     * @return int
     */
    private function getParentId($current_id)
    {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages')->createQueryBuilder();

        return (int)$queryBuilder->select('pid')->from('pages')->where(
            $queryBuilder->expr()->eq('uid', $current_id)
        )->execute()->fetchColumn(0);
    }
}