ExtensionRepository.php 5.71 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Domain\Repository;

4 5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * 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.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16 17

/**
18
 * Repository for \T3o\TerFe2\Domain\Model\Extension
19
 */
20
class ExtensionRepository extends \T3o\TerFe2\Domain\Repository\AbstractRepository
21 22 23
{

    /**
24
     * @var bool
25
     */
Markus Sommer's avatar
Markus Sommer committed
26
    protected $showInsecure = true;
27 28 29 30

    /**
     * Allow the listing of insecure extensions or not
     *
31
     * @param bool $showInsecure
32 33 34 35 36 37 38 39 40
     */
    public function setShowInsecure($showInsecure)
    {
        $this->showInsecure = (bool)$showInsecure;
    }

    /**
     * Returns the showInsecure state
     *
41
     * @return bool
42 43 44 45 46 47 48 49 50
     */
    public function getShowInsecure()
    {
        return (bool)$this->showInsecure;
    }

    /**
     * Build basis constraint
     *
51 52
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
     * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint
53
     */
54
    protected function match(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface $constraint)
55 56 57 58 59 60
    {
        if ($this->showInsecure) {
            $query->matching($constraint);
            return;
        }

61
        $query->matching($query->logicalAnd([
62 63
            $query->logicalNot($query->equals('lastVersion.reviewState', -1)),
            $constraint
64
        ]));
65 66 67 68 69 70 71 72
    }

    /**
     * Returns all extensions
     *
     * @param int $offset
     * @param int $count
     * @param array $ordering
73
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
74
     */
75
    public function findAll($offset = 0, $count = 0, array $ordering = [])
76 77
    {
        if (empty($ordering)) {
78
            $ordering = ['lastVersion.title' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING];
79 80 81 82 83 84 85 86 87 88
        }
        $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
     *
89
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
     */
    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
     *
113
     * @param int $latestCount Count of extensions
114
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface Objects
115 116 117
     */
    public function findLatest($latestCount = 0)
    {
118
        $ordering = ['lastVersion.uploadDate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING];
119 120 121 122 123
        return $this->findAll(0, $latestCount, $ordering);
    }

    /**
     * @param string $frontendUser
124
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
125 126 127 128 129
     */
    public function findByFrontendUser($frontendUser)
    {
        $query = $this->createQuery();
        $query->setOrderings(
130
            ['extKey' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING]
131
        );
132
        $query->matching($query->equals('frontendUser', $frontendUser));
133 134 135 136 137 138

        return $query->execute();
    }

    /**
     * @param string $frontendUser
139
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
140 141 142 143 144
     */
    public function findByFrontendUserAndExpiring($frontendUser)
    {
        $query = $this->createQuery();
        $query->setOrderings(
145
            ['expire' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING]
146 147 148 149 150
        );

        $olderThanOneYear = strtotime('-1 year');
        $query->matching(
            $query->logicalAnd(
151
                [
152 153 154 155
                    $query->equals('frontendUser', $frontendUser),
                    $query->equals('versions', 0),
                    $query->greaterThan('expire', 0),
                    $query->lessThanOrEqual('tstamp', $olderThanOneYear)
156
                ]
157 158 159 160 161 162 163
            )
        );

        return $query->execute();
    }

    /**
164
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
165 166
     * @param string $frontendUser
     *
167
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
168
     */
169
    public function findAllOtherFromFrontendUser(\T3o\TerFe2\Domain\Model\Extension $extension, $frontendUser)
170 171
    {
        $query = $this->createQuery();
Markus Sommer's avatar
Markus Sommer committed
172 173
        $query->getQuerySettings()->setRespectStoragePage(false);
        $query->getQuerySettings()->setRespectSysLanguage(false);
174
        $constraints = [
175
            $query->equals('frontendUser', $frontendUser),
176
            $query->logicalAnd([
177 178
                $query->logicalNot(
                    $query->equals('uid', $extension->getUid())
Markus Sommer's avatar
Markus Sommer committed
179 180 181 182
                ),
                $query->logicalNot(
                    $query->equals('lastVersion.state', 'obsolete')
                )
183
            ]),
184 185
            $query->greaterThanOrEqual('lastVersion.review_state', '0'),
            $query->greaterThanOrEqual('versions', '1')
186
        ];
187 188 189 190 191
        $query->matching($query->logicalAnd($constraints));

        return $query->execute();
    }
}