ExtensionRepository.php 11 KB
Newer Older
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
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
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  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!
 ******************************************************************/

/**
 * Repository for Tx_TerFe2_Domain_Model_Extension
 */
class Tx_TerFe2_Domain_Repository_ExtensionRepository extends Tx_TerFe2_Domain_Repository_AbstractRepository
{

    /**
     * @var Tx_TerFe2_Domain_Repository_SearchRepository
     */
    protected $searchRepository;

    /**
     * @var boolean
     */
    protected $showInsecure = TRUE;


    /**
     * Allow the listing of insecure extensions or not
     *
     * @param boolean $showInsecure
     * @return void
     */
    public function setShowInsecure($showInsecure)
    {
        $this->showInsecure = (bool)$showInsecure;
    }


    /**
     * Returns the showInsecure state
     *
     * @return boolean
     */
    public function getShowInsecure()
    {
        return (bool)$this->showInsecure;
    }


    /**
     * Build basis constraint
     *
69
70
71
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
     * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint
     * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
72
     */
73
    protected function match(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    {
        if ($this->showInsecure) {
            $query->matching($constraint);
            return;
        }

        $query->matching($query->logicalAnd(
            $query->logicalNot($query->equals('lastVersion.reviewState', -1)),
            $constraint
        ));
    }


    /**
     * Returns all extensions
     *
     * @param int $offset
     * @param int $count
     * @param array $ordering
93
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
94
95
96
97
     */
    public function findAll($offset = 0, $count = 0, array $ordering = array())
    {
        if (empty($ordering)) {
98
            $ordering = array('lastVersion.title' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
99
100
101
102
103
104
105
106
107
108
        }
        $query = $this->createQuery($offset, $count, $ordering);
        // Filter empty title
        $this->match($query, $query->logicalNot($query->equals('lastVersion.title', '')));
        return $query->execute();
    }

    /**
     * Returns all objects of this repository
     *
109
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
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
     */
    public function findAllAdmin()
    {
        $result = $this->createQuery()->execute();
        return $result;
    }

    /**
     * Returns one extension by extension key respecting the reviewstate
     *
     * @param $extensionKey
     * @return object
     */
    public function findOneByExtKey($extensionKey)
    {
        $query = $this->createQuery(0, 1);
        $this->match($query, $query->equals('extKey', $extensionKey));
        return $query->execute()->getFirst();
    }


    /**
     * Returns new and updated extensions
     *
     * @param integer $latestCount Count of extensions
135
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage Objects
136
137
138
     */
    public function findLatest($latestCount = 0)
    {
139
        $ordering = array('lastVersion.uploadDate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING);
140
141
142
143
144
145
146
        return $this->findAll(0, $latestCount, $ordering);
    }

    /**
     * Returns all extensions with a tag
     *
     * @param Tx_TerFe2_Domain_Model_Tag $tag The Tag to search for
147
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage Objects
148
149
150
151
152
153
154
155
156
157
158
159
160
     */
    public function findByTag(Tx_TerFe2_Domain_Model_Tag $tag)
    {
        $query = $this->createQuery();
        $this->match($query, $query->contains('tags', $tag));
        return $query->execute();
    }


    /**
     * Returns all extensions by an author
     *
     * @param Tx_TerFe2_Domain_Model_Author $author The Author to search for
161
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage Objects
162
163
164
165
166
167
168
169
170
171
     */
    public function findByAuthor(Tx_TerFe2_Domain_Model_Author $author)
    {
        $uids = $this->searchRepository->findUidsByAuthor($author);

        // Workaround to enable paginate
        $query = $this->createQuery();
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
        $query->getQuerySettings()->setRespectSysLanguage(FALSE);
        $query->setOrderings(
172
            array('extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
173
174
175
176
177
178
179
180
181
        );
        $this->match($query, $query->in('uid', $uids));

        return $query->execute();
    }

    /**
     *
     * @param string $frontendUser
182
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage Objects
183
184
185
186
187
     */
    public function findByFrontendUser($frontendUser)
    {
        $query = $this->createQuery();
        $query->setOrderings(
188
            array('extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
189
190
191
192
193
194
195
196
197
        );

        $this->match($query, $query->like('frontendUser', $frontendUser));
        return $query->execute();
    }

    /**
     *
     * @param string $frontendUser
198
     * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult|NULL
199
200
201
202
203
     */
    public function findByFrontendUserAndExpiring($frontendUser)
    {
        $query = $this->createQuery();
        $query->setOrderings(
204
            array('expire' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        );

        $olderThanOneYear = strtotime('-1 year');
        $query->matching(
            $query->logicalAnd(
                array(
                    $query->equals('frontendUser', $frontendUser),
                    $query->equals('versions', 0),
                    $query->greaterThan('expire', 0),
                    $query->lessThanOrEqual('tstamp', $olderThanOneYear)
                )
            )
        );

        return $query->execute();
    }

    /**
     * Search extensions by search words and filters
     *
     * @param string $searchWords Search string
     * @param array $filters Filter extension list
     * @param array $ordering $ordering Ordering <-> Direction
228
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage Objects
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
     */
    public function findBySearchWordsAndFilters($searchWords = NULL, array $filters = NULL, array $ordering = NULL)
    {
        $query = $this->createQuery();
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
        $query->getQuerySettings()->setRespectSysLanguage(FALSE);
        $query->setOrderings($ordering);
        $searchWordsForExtensionModel = $GLOBALS['TYPO3_DB']->escapeStrForLike(
            $GLOBALS['TYPO3_DB']->quoteStr(
                $searchWords,
                'tx_terfe2_domain_model_extension'
            ),
            'tx_terfe2_domain_model_extension'
        );
        $searchWordsForVersionModel = $GLOBALS['TYPO3_DB']->escapeStrForLike(
            $GLOBALS['TYPO3_DB']->quoteStr(
                $searchWords,
                'tx_terfe2_domain_model_version'
            ),
            'tx_terfe2_domain_model_version'
        );
        $searchWordsForAuthorModel = $GLOBALS['TYPO3_DB']->escapeStrForLike(
            $GLOBALS['TYPO3_DB']->quoteStr(
                $searchWords,
                'tx_terfe2_domain_model_author'
            ),
            'tx_terfe2_domain_model_author'
        );
        $constraints = array(
            $query->equals('extKey', $searchWords),
            $query->like('extKey', '%' . $searchWordsForExtensionModel . '%'),
            $query->like('lastVersion.title', '%' . $searchWordsForVersionModel . '%'),
            $query->like('lastVersion.description', '%' . $searchWordsForVersionModel . '%'),
            $query->like('lastVersion.author.name', '%' . $searchWordsForAuthorModel . '%'),
            $query->equals('lastVersion.author.username', $searchWords),
            $query->equals('lastVersion.author.email', $searchWords),
        );
        $query->matching($query->logicalOr($constraints));

        return $query->execute();
    }


    /**
     * Returns count of extensions with given extKey and versionNumber
     *
     * @param string $extKey Extension Key
     * @param integer $versionNumber Version of the extension
     * @return integer Result count
     */
    public function countByExtKeyAndVersionNumber($extKey, $versionNumber)
    {
        $query = $this->createQuery();
        $this->match($query, $query->logicalAnd(
            $query->equals('extKey', $extKey),
            $query->greaterThanOrEqual('lastVersion.versionNumber', (int)$versionNumber)
        ));
        return $query->execute()->count();
    }


    /**
     * Get domain models given by an array of extension keys
     *
     * Unknown keys are silently ignored. So check later if you have all the models you need.
     *
     * @param array $extKeys
296
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
     */
    public function findByExtKeys(array $extKeys)
    {
        // Workaround to enable paginate
        $query = $this->createQuery();
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
        $query->getQuerySettings()->setRespectSysLanguage(FALSE);
        $query->matching($query->in('ext_key', $extKeys));

        return $query->execute();
    }

    /**
     * @param Tx_TerFe2_Domain_Model_Extension $extension
     * @param string $frontendUser
     *
313
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
     */
    public function findAllOtherFromFrontendUser(Tx_TerFe2_Domain_Model_Extension $extension, $frontendUser)
    {
        $query = $this->createQuery();
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
        $query->getQuerySettings()->setRespectSysLanguage(FALSE);
        $constraints = array(
            $query->equals('frontendUser', $frontendUser),
            $query->logicalAnd(
                $query->logicalNot(
                    $query->equals('uid', $extension->getUid())
                ), $query->logicalNot(
                $query->equals('lastVersion.state', 'obsolete')
            )
            ),
            $query->greaterThanOrEqual('lastVersion.review_state', '0'),
            $query->greaterThanOrEqual('versions', '1')
        );
        $query->matching($query->logicalAnd($constraints));

        return $query->execute();
    }
}