~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Factory/RequiredFileFactory.php

  • Committer: Dan Garner
  • Date: 2016-02-04 14:13:07 UTC
  • mto: (454.4.101)
  • mto: This revision was merged to the branch mainline in revision 483.
  • Revision ID: git-v1:f9078f575b16a62a51e2f3d7bc15581058fb0747
Problem with DataSet form putting a 0 in library image references (should be empty)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
namespace Xibo\Factory;
10
10
 
 
11
 
11
12
use Xibo\Entity\RequiredFile;
12
13
use Xibo\Exception\NotFoundException;
13
 
use Xibo\Service\LogServiceInterface;
14
 
use Xibo\Service\SanitizerServiceInterface;
15
 
use Xibo\Storage\StorageServiceInterface;
 
14
use Xibo\Helper\Log;
 
15
use Xibo\Helper\Sanitize;
 
16
use Xibo\Storage\PDOConnect;
16
17
 
17
 
/**
18
 
 * Class RequiredFileFactory
19
 
 * @package Xibo\Factory
20
 
 */
21
18
class RequiredFileFactory extends BaseFactory
22
19
{
23
 
    private $statement = null;
24
 
 
25
 
    /**
26
 
     * Construct a factory
27
 
     * @param StorageServiceInterface $store
28
 
     * @param LogServiceInterface $log
29
 
     * @param SanitizerServiceInterface $sanitizerService
30
 
     */
31
 
    public function __construct($store, $log, $sanitizerService)
32
 
    {
33
 
        $this->setCommonDependencies($store, $log, $sanitizerService);
34
 
    }
35
 
 
36
 
    /**
37
 
     * @return RequiredFile
38
 
     */
39
 
    public function createEmpty()
40
 
    {
41
 
        return new RequiredFile($this->getStore(), $this->getLog());
42
 
    }
43
 
 
44
 
    /**
45
 
     * @param array $params
46
 
     * @return RequiredFile[]
47
 
     */
48
 
    private function query($params)
49
 
    {
50
 
        $files = [];
51
 
 
52
 
        if ($this->statement === null) {
53
 
            $this->statement = $this->getStore()->getConnection()->prepare('
54
 
              SELECT * 
55
 
                FROM `requiredfile` 
56
 
               WHERE `displayId` = :displayId
57
 
                  AND `type` = :type 
58
 
                  AND `itemId` = :itemId
59
 
              ');
60
 
        }
61
 
 
62
 
        $this->statement->execute($params);
63
 
 
64
 
        foreach ($this->statement->fetchAll(\PDO::FETCH_ASSOC) as $item) {
65
 
            $files[] = $this->createEmpty()->hydrate($item);
66
 
        }
67
 
 
68
 
        return $files;
69
 
    }
70
 
 
71
 
    /**
72
 
     * @param int $displayId
73
 
     * @param int $layoutId
74
 
     * @return RequiredFile
75
 
     * @throws NotFoundException
76
 
     */
77
 
    public function getByDisplayAndLayout($displayId, $layoutId)
78
 
    {
79
 
        $result = $this->query(['displayId' => $displayId, 'type' => 'L', 'itemId' => $layoutId]);
80
 
 
81
 
        if (count($result) <= 0)
82
 
            throw new NotFoundException(__('Required file not found for Display and Layout Combination'));
83
 
 
84
 
        return $result[0];
85
 
    }
86
 
 
87
 
    /**
88
 
     * @param int $displayId
89
 
     * @param int $mediaId
90
 
     * @return RequiredFile
91
 
     * @throws NotFoundException
92
 
     */
93
 
    public function getByDisplayAndMedia($displayId, $mediaId)
94
 
    {
95
 
        $result = $this->query(['displayId' => $displayId, 'type' => 'M', 'itemId' => $mediaId]);
96
 
 
97
 
        if (count($result) <= 0)
98
 
            throw new NotFoundException(__('Required file not found for Display and Media Combination'));
99
 
 
100
 
        return $result[0];
101
 
    }
102
 
 
103
 
    /**
104
 
     * @param int $displayId
105
 
     * @param int $widgetId
106
 
     * @return RequiredFile
107
 
     * @throws NotFoundException
108
 
     */
109
 
    public function getByDisplayAndWidget($displayId, $widgetId)
110
 
    {
111
 
        $result = $this->query(['displayId' => $displayId, 'type' => 'W', 'itemId' => $widgetId]);
112
 
 
113
 
        if (count($result) <= 0)
114
 
            throw new NotFoundException(__('Required file not found for Display and Layout Widget'));
115
 
 
116
 
        return $result[0];
 
20
    /**
 
21
     * @param string $nonce
 
22
     * @return RequiredFile
 
23
     * @throws NotFoundException
 
24
     */
 
25
    public static function getByNonce($nonce)
 
26
    {
 
27
        $nonce = RequiredFileFactory::query(null, ['nonce' => $nonce]);
 
28
 
 
29
        if (count($nonce) <= 0)
 
30
            throw new NotFoundException();
 
31
 
 
32
        return $nonce[0];
 
33
    }
 
34
 
 
35
    /**
 
36
     * @param int $displayId
 
37
     * @param int $layoutId
 
38
     * @return RequiredFile
 
39
     * @throws NotFoundException
 
40
     */
 
41
    public static function getByDisplayAndLayout($displayId, $layoutId)
 
42
    {
 
43
        $files = RequiredFileFactory::query(null, ['displayId' => $displayId, 'layoutId' => $layoutId]);
 
44
 
 
45
        if (count($files) <= 0)
 
46
            throw new NotFoundException();
 
47
 
 
48
        return $files[0];
 
49
    }
 
50
 
 
51
    /**
 
52
     * @param int $displayId
 
53
     * @param int $mediaId
 
54
     * @return RequiredFile
 
55
     * @throws NotFoundException
 
56
     */
 
57
    public static function getByDisplayAndMedia($displayId, $mediaId)
 
58
    {
 
59
        $files = RequiredFileFactory::query(null, ['displayId' => $displayId, 'mediaId' => $mediaId]);
 
60
 
 
61
        if (count($files) <= 0)
 
62
            throw new NotFoundException();
 
63
 
 
64
        return $files[0];
 
65
    }
 
66
 
 
67
    /**
 
68
     * @param int $displayId
 
69
     * @param int $layoutId
 
70
     * @param int $regionId
 
71
     * @param int $mediaId
 
72
     * @return RequiredFile
 
73
     * @throws NotFoundException
 
74
     */
 
75
    public static function getByDisplayAndResource($displayId, $layoutId, $regionId, $mediaId)
 
76
    {
 
77
        $files = RequiredFileFactory::query(null, ['displayId' => $displayId, 'layoutId' => $layoutId, 'regionId' => $regionId, 'mediaId' => $mediaId]);
 
78
 
 
79
        if (count($files) <= 0)
 
80
            throw new NotFoundException();
 
81
 
 
82
        return $files[0];
117
83
    }
118
84
 
119
85
    /**
120
86
     * Create for layout
121
87
     * @param $displayId
 
88
     * @param $requestKey
122
89
     * @param $layoutId
123
90
     * @param $size
124
91
     * @param $path
125
92
     * @return RequiredFile
126
93
     */
127
 
    public function createForLayout($displayId, $layoutId, $size, $path)
 
94
    public static function createForLayout($displayId, $requestKey, $layoutId, $size, $path)
128
95
    {
129
96
        try {
130
 
            $requiredFile = $this->getByDisplayAndLayout($displayId, $layoutId);
 
97
            $nonce = RequiredFileFactory::getByDisplayAndLayout($displayId, $layoutId);
131
98
        }
132
99
        catch (NotFoundException $e) {
133
 
            $requiredFile = $this->createEmpty();
 
100
            $nonce = new RequiredFile();
134
101
        }
135
102
 
136
 
        $requiredFile->displayId = $displayId;
137
 
        $requiredFile->type = 'L';
138
 
        $requiredFile->itemId = $layoutId;
139
 
        $requiredFile->size = $size;
140
 
        $requiredFile->path = $path;
141
 
        return $requiredFile;
 
103
        $nonce->displayId = $displayId;
 
104
        $nonce->requestKey = $requestKey;
 
105
        $nonce->layoutId = $layoutId;
 
106
        $nonce->size = $size;
 
107
        $nonce->storedAs = $path;
 
108
        return $nonce;
142
109
    }
143
110
 
144
111
    /**
145
112
     * Create for Get Resource
146
113
     * @param $displayId
147
 
     * @param $widgetId
 
114
     * @param $requestKey
 
115
     * @param $layoutId
 
116
     * @param $regionId
 
117
     * @param $mediaId
148
118
     * @return RequiredFile
149
119
     */
150
 
    public function createForGetResource($displayId, $widgetId)
 
120
    public static function createForGetResource($displayId, $requestKey, $layoutId, $regionId, $mediaId)
151
121
    {
152
122
        try {
153
 
            $requiredFile = $this->getByDisplayAndWidget($displayId, $widgetId);
 
123
            $nonce = RequiredFileFactory::getByDisplayAndResource($displayId, $layoutId, $regionId, $mediaId);
154
124
        }
155
125
        catch (NotFoundException $e) {
156
 
            $requiredFile = $this->createEmpty();
 
126
            $nonce = new RequiredFile();
157
127
        }
158
128
 
159
 
        $requiredFile->displayId = $displayId;
160
 
        $requiredFile->type = 'W';
161
 
        $requiredFile->itemId = $widgetId;
162
 
        return $requiredFile;
 
129
        $nonce->displayId = $displayId;
 
130
        $nonce->requestKey = $requestKey;
 
131
        $nonce->layoutId = $layoutId;
 
132
        $nonce->regionId = $regionId;
 
133
        $nonce->mediaId = $mediaId;
 
134
        return $nonce;
163
135
    }
164
136
 
165
137
    /**
166
138
     * Create for Media
167
139
     * @param $displayId
 
140
     * @param $requestKey
168
141
     * @param $mediaId
169
142
     * @param $size
170
143
     * @param $path
171
144
     * @return RequiredFile
172
145
     */
173
 
    public function createForMedia($displayId, $mediaId, $size, $path)
 
146
    public static function createForMedia($displayId, $requestKey, $mediaId, $size, $path)
174
147
    {
175
148
        try {
176
 
            $requiredFile = $this->getByDisplayAndMedia($displayId, $mediaId);
 
149
            $nonce = RequiredFileFactory::getByDisplayAndMedia($displayId, $mediaId);
177
150
        }
178
151
        catch (NotFoundException $e) {
179
 
            $requiredFile = $this->createEmpty();
180
 
        }
181
 
 
182
 
        $requiredFile->displayId = $displayId;
183
 
        $requiredFile->type = 'M';
184
 
        $requiredFile->itemId = $mediaId;
185
 
        $requiredFile->size = $size;
186
 
        $requiredFile->path = $path;
187
 
        return $requiredFile;
 
152
            $nonce = new RequiredFile();
 
153
        }
 
154
 
 
155
        $nonce->displayId = $displayId;
 
156
        $nonce->requestKey = $requestKey;
 
157
        $nonce->mediaId = $mediaId;
 
158
        $nonce->size = $size;
 
159
        $nonce->storedAs = $path;
 
160
        return $nonce;
 
161
    }
 
162
 
 
163
    public static function query($sortOrder = null, $filterBy = null)
 
164
    {
 
165
        $entries = [];
 
166
        $params = [];
 
167
        $sql = '
 
168
            SELECT rfId,
 
169
                `requiredfile`.requestKey,
 
170
                `requiredfile`.nonce,
 
171
                `requiredfile`.expiry,
 
172
                `requiredfile`.lastUsed,
 
173
                `requiredfile`.displayId,
 
174
                `requiredfile`.size,
 
175
                `requiredfile`.storedAs,
 
176
                `requiredfile`.layoutId,
 
177
                `requiredfile`.regionId,
 
178
                `requiredfile`.mediaId,
 
179
                `requiredfile`.bytesRequested,
 
180
                `requiredfile`.complete
 
181
             FROM `requiredfile`
 
182
            WHERE 1 = 1
 
183
        ';
 
184
 
 
185
        if (Sanitize::getString('nonce', $filterBy) !== null) {
 
186
            $sql .= ' AND requiredfile.nonce = :nonce';
 
187
            $params['nonce'] = Sanitize::getString('nonce', $filterBy);
 
188
        }
 
189
 
 
190
        if (Sanitize::getInt('displayId', $filterBy) !== null) {
 
191
            $sql .= ' AND requiredfile.displayId = :displayId';
 
192
            $params['displayId'] = Sanitize::getInt('displayId', $filterBy);
 
193
        }
 
194
 
 
195
        if (Sanitize::getInt('layoutId', $filterBy) !== null) {
 
196
            $sql .= ' AND requiredfile.layoutId = :layoutId';
 
197
            $params['layoutId'] = Sanitize::getInt('layoutId', $filterBy);
 
198
        }
 
199
 
 
200
        if (Sanitize::getInt('regionId', $filterBy) !== null) {
 
201
            $sql .= ' AND requiredfile.regionId = :regionId';
 
202
            $params['regionId'] = Sanitize::getInt('regionId', $filterBy);
 
203
        }
 
204
 
 
205
        if (Sanitize::getInt('mediaId', $filterBy) !== null) {
 
206
            $sql .= ' AND requiredfile.mediaId = :mediaId';
 
207
            $params['mediaId'] = Sanitize::getInt('mediaId', $filterBy);
 
208
        }
 
209
 
 
210
        // Sorting?
 
211
        if (is_array($sortOrder))
 
212
            $sql .= 'ORDER BY ' . implode(',', $sortOrder);
 
213
 
 
214
        Log::sql($sql, $params);
 
215
 
 
216
        foreach (PDOConnect::select($sql, $params) as $row) {
 
217
            $entries[] = (new RequiredFile())->hydrate($row, ['intProperties' => ['expires', 'lastUsed', 'size']]);
 
218
        }
 
219
 
 
220
        return $entries;
188
221
    }
189
222
}
 
 
b'\\ No newline at end of file'