Passed
Pull Request — master (#64)
by
unknown
08:10
created

AbstractSearchController::convertFormDate()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 8
nop 2
dl 0
loc 14
rs 8.8571
1
<?php
2
namespace EWW\Dpf\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\GeneralUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
abstract class AbstractSearchController extends \EWW\Dpf\Controller\AbstractController
20
{
21
    // search terms
22
    private static $terms   = ['_id', 'OWNER_ID', 'submitter', 'project'];
23
24
    // search matches
25
    private static $matches = ['title', 'abstract', 'author', 'language', 'tag', 'corporation', 'doctype', 'collections'];
26
27
    /**
28
     * get results from elastic search
29
     * @param  array $query elasticsearch search query
30
     * @return array        results
31
     */
32
    public function getResultList($query, $type)
33
    {
34
35
        $elasticSearch = new \EWW\Dpf\Services\ElasticSearch();
36
        $results = $elasticSearch->search($query, $type);
37
38
        return $results;
39
    }
40
41
    /**
42
     * prepare fulltext query
43
     * @param  string $searchString
44
     * @return array query
45
     */
46
    public function searchFulltext($searchString)
47
    {
48
        // don't return query if searchString is empty
49
        if (empty($searchString)) {
50
            return null;
51
        }
52
53
        $searchString = $this->escapeQuery(trim($searchString));
54
55
        $query['body']['query']['bool']['should'][0]['query_string']['query']                       = $searchString;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.
Loading history...
56
        $query['body']['query']['bool']['should'][1]['has_child']['query']['query_string']['query'] = $searchString;
57
        $query['body']['query']['bool']['minimum_should_match'] = "1"; // 1
58
        $query['body']['query']['bool']['should'][1]['has_child']['child_type'] = "datastream"; // 1
59
60
        $query = $this->resultsFilter($query, false);
61
62
        return $query;
63
64
    }
65
66
    /**
67
     * build array for elasticsearch
68
     * @return array Elasticsearch query array
69
     */
70
    public function extendedSearch($searchArray = array())
71
    {
72
73
        $query  = array();
74
        $filter = array();
75
        foreach ($searchArray as $key => $qry) {
76
            $qry = trim($qry);
77
78
            if (!empty($qry) && in_array($key, self::$matches)) {
79
80
                $query['body']['query']['bool']['must'][] = array('match' => array($key => $qry));
81
82
            } elseif (!empty($qry) && in_array($key, self::$terms)) {
83
84
                $query['body']['query']['bool']['must'][] = array('term' => array($key => $qry));
85
86
            } elseif (!empty($qry) && $key == 'from') {
87
88
                if ($dateTime = $this->convertFormDate($qry, false)) {
89
                    $filter['gte'] = $dateTime->format('Y-m-d');
90
                }
91
92
            } elseif (!empty($qry) && $key == 'till') {
93
94
                if ($dateTime = $this->convertFormDate($qry, true)) {
95
                    $filter['lte'] = $dateTime->format('Y-m-d');
96
                }
97
98
            }
99
        }
100
101
        if (isset($filter['gte']) || isset($filter['lte'])) {
102
103
            $query['body']['query']['bool']['must'][] = array('range' => array('distribution_date' => $filter));
104
105
        }
106
107
        $showDeleted = ($searchArray['showDeleted'] == 'true') ? true : false;
108
        $query = $this->resultsFilter($query, $showDeleted);
109
        return $query;
110
111
    }
112
113
    /**
114
     * build array for elasticsearch resultfilter
115
     * @param array Elasticsearch query array
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Controller\Elasticsearch was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
     * @return array Elasticsearch queryFilter array
117
     */
118
    public function resultsFilter($query, $showDeleted = false)
119
    {
120
121
        $queryFilter = array();
122
123
        // Frontend only
124
        $searchResultsFilter = $this->settings['searchResultsFilter'];
125
        if(!empty($searchResultsFilter)) {
126
127
            // add doctypes
128
            if($searchResultsFilter['doctype']) {
129
130
                $uids = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $searchResultsFilter['doctype']);
131
                $documentTypeRepository = $this->documentTypeRepository;
132
                $documentTypes = array();
133
                foreach($uids as $uid) {
134
                    $documentType = $documentTypeRepository->findByUid($uid);
135
                    $documentTypes[] = $documentType->getName();
136
                };
137
                $searchResultsFilter['doctype'] = implode(',', $documentTypes);
138
            }
139
140
            // add date filter
141
            $dateFilter = array();
142
            if ($searchResultsFilter['from']) {
143
144
                $from     = date('d.m.Y', $searchResultsFilter['from']);
145
                $dateTime = $this->convertFormDate($from, false);
146
                $dateFilter['gte'] = $dateTime->format('Y-m-d');
147
                unset($searchResultsFilter['from']);
148
149
            }
150
151
            if ($searchResultsFilter['till']) {
152
153
                $till          = date('d.m.Y', $searchResultsFilter['till']);
154
                $dateTime = $this->convertFormDate($till, true);
155
                $dateFilter['lte'] = $dateTime->format('Y-m-d');
156
                unset($searchResultsFilter['till']);
157
158
            }
159
160
            if (isset($dateFilter['gte']) || isset($dateFilter['lte'])) {
161
162
                $queryFilter['body']['query']['bool']['must'][] = array('range' => array('distribution_date' => $dateFilter));
163
164
            }
165
166
            foreach ($searchResultsFilter as $key => $qry) {
167
168
                if(!empty($qry)) {
169
                    $queryFilter['body']['query']['bool']['must'][] = array('match' => array($key => $qry));
170
                }
171
172
            }
173
174
        }
175
176
        // document must be active
177
        if($showDeleted == false) {
178
179
            $queryFilter['body']['query']['bool']['must'][]['term']['STATE'] = 'A';
180
181
        };
182
183
        // add owner id
184
        $client = $this->clientRepository->findAll()->current();
185
        $queryFilter['body']['query']['bool']['must'][]['term']['OWNER_ID'] = $client->getOwnerId();
186
187
        $queryFilter = array_merge_recursive($queryFilter, $query);
188
        return $queryFilter;
189
    }
190
191
    /**
192
     * Convert date from form input into DateTime object.
193
     *
194
     * A 4 character string is taken for a year and the returning
195
     * DateTime object is supplemented with either the 01. Jan or 31. Dec
196
     * depending on the $intervalEnd parameter. This allows querying time
197
     * intervals like `2000 to 2003`.
198
     *
199
     * @param  string    $dateString  Date literal from form
200
     * @param  bool      $intervalEnd Fills missing values with the maximum possible date if true
201
     * @return DateTime               Determined date
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Controller\DateTime was not found. Did you mean DateTime? If so, make sure to prefix the type with \.
Loading history...
202
     */
203
    public function convertFormDate($dateString, $intervalEnd = false)
204
    {
205
        try {
206
            if (strlen($dateString) == 4) {
207
                // assuming year
208
                $year  = $dateString;
209
                $month = $intervalEnd ? "12" : "01";
210
                $day   = $intervalEnd ? "31" : "01";
211
                return new \DateTime("$year-$month-$day");
0 ignored issues
show
Bug Best Practice introduced by
The expression return new DateTime(EncapsedNode) returns the type DateTime which is incompatible with the documented return type EWW\Dpf\Controller\DateTime.
Loading history...
212
            } else {
213
                return new \DateTime($dateString);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new DateTime($dateString) returns the type DateTime which is incompatible with the documented return type EWW\Dpf\Controller\DateTime.
Loading history...
214
            }
215
        } catch (\Exception $_) {
216
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type EWW\Dpf\Controller\DateTime.
Loading history...
217
        }
218
    }
219
220
    /**
221
     * escapes lucene reserved characters from string
222
     * @param $string
223
     * @return mixed
224
     */
225
    private function escapeQuery($string)
226
    {
227
        $luceneReservedCharacters = preg_quote('+-&|!(){}[]^"~?:\\');
228
        $string                   = preg_replace_callback(
229
            '/([' . $luceneReservedCharacters . '])/',
230
            function ($matches) {
231
                return '\\' . $matches[0];
232
            },
233
            $string
234
        );
235
236
        return $string;
237
    }
238
239
    /**
240
     * converts a date from dd.mm.yy to yyyy-dd-mm
241
     * @param $date
242
     * @return string
243
     */
244
    public function formatDate($date)
245
    {
246
        // convert date from dd.mm.yyy to yyyy-dd-mm
247
        $date = explode(".", $date);
248
        return $date[2] . '-' . $date[1] . '-' . $date[0];
249
    }
250
}
251