~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Factory/DisplayProfileFactory.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 * (DisplayProfileFactory.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Factory;
10
 
 
11
 
 
12
 
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
 
use Xibo\Entity\DisplayProfile;
14
 
use Xibo\Exception\NotFoundException;
15
 
use Xibo\Service\ConfigServiceInterface;
16
 
use Xibo\Service\LogServiceInterface;
17
 
use Xibo\Service\SanitizerServiceInterface;
18
 
use Xibo\Storage\StorageServiceInterface;
19
 
 
20
 
/**
21
 
 * Class DisplayProfileFactory
22
 
 * @package Xibo\Factory
23
 
 */
24
 
class DisplayProfileFactory extends BaseFactory
25
 
{
26
 
    /**
27
 
     * @var ConfigServiceInterface
28
 
     */
29
 
    private $config;
30
 
 
31
 
    /** @var EventDispatcherInterface  */
32
 
    private $dispatcher;
33
 
 
34
 
    /**
35
 
     * @var CommandFactory
36
 
     */
37
 
    private $commandFactory;
38
 
 
39
 
    /**
40
 
     * Construct a factory
41
 
     * @param StorageServiceInterface $store
42
 
     * @param LogServiceInterface $log
43
 
     * @param SanitizerServiceInterface $sanitizerService
44
 
     * @param ConfigServiceInterface $config
45
 
     * @param EventDispatcherInterface $dispatcher
46
 
     * @param CommandFactory $commandFactory
47
 
     */
48
 
    public function __construct($store, $log, $sanitizerService, $config, $dispatcher, $commandFactory)
49
 
    {
50
 
        $this->setCommonDependencies($store, $log, $sanitizerService);
51
 
 
52
 
        $this->config = $config;
53
 
        $this->dispatcher = $dispatcher;
54
 
        $this->commandFactory = $commandFactory;
55
 
    }
56
 
 
57
 
    /**
58
 
     * @return DisplayProfile
59
 
     */
60
 
    public function createEmpty()
61
 
    {
62
 
        return new DisplayProfile(
63
 
            $this->getStore(),
64
 
            $this->getLog(),
65
 
            $this->config,
66
 
            $this->dispatcher,
67
 
            $this->commandFactory
68
 
        );
69
 
    }
70
 
 
71
 
    /**
72
 
     * @param int $displayProfileId
73
 
     * @return DisplayProfile
74
 
     * @throws NotFoundException
75
 
     */
76
 
    public function getById($displayProfileId)
77
 
    {
78
 
        $profiles = $this->query(null, ['disableUserCheck' => 1, 'displayProfileId' => $displayProfileId]);
79
 
 
80
 
        if (count($profiles) <= 0)
81
 
            throw new NotFoundException();
82
 
 
83
 
        $profile = $profiles[0];
84
 
        /* @var DisplayProfile $profile */
85
 
 
86
 
        $profile->load();
87
 
        return $profile;
88
 
    }
89
 
 
90
 
    /**
91
 
     * @param string $type
92
 
     * @return DisplayProfile
93
 
     * @throws NotFoundException
94
 
     */
95
 
    public function getDefaultByType($type)
96
 
    {
97
 
        $profiles = $this->query(null, ['disableUserCheck' => 1, 'type' => $type, 'isDefault' => 1]);
98
 
 
99
 
        if (count($profiles) <= 0)
100
 
            throw new NotFoundException();
101
 
 
102
 
        $profile = $profiles[0];
103
 
        /* @var DisplayProfile $profile */
104
 
 
105
 
        $profile->load();
106
 
        return $profile;
107
 
    }
108
 
 
109
 
    /**
110
 
     * @param $clientType
111
 
     * @return DisplayProfile
112
 
     */
113
 
    public function getUnknownProfile($clientType)
114
 
    {
115
 
        $profile = $this->createEmpty();
116
 
        $profile->type = 'unknown';
117
 
        $profile->setClientType($clientType);
118
 
        $profile->load();
119
 
        return $profile;
120
 
    }
121
 
 
122
 
    /**
123
 
     * Get by Command Id
124
 
     * @param $commandId
125
 
     * @return array[DisplayProfile]
126
 
     * @throws NotFoundException
127
 
     */
128
 
    public function getByCommandId($commandId)
129
 
    {
130
 
        return $this->query(null, ['disableUserCheck' => 1, 'commandId' => $commandId]);
131
 
    }
132
 
 
133
 
    /**
134
 
     * @param array $sortOrder
135
 
     * @param array $filterBy
136
 
     * @return DisplayProfile[]
137
 
     * @throws NotFoundException
138
 
     */
139
 
    public function query($sortOrder = null, $filterBy = [])
140
 
    {
141
 
        $profiles = array();
142
 
 
143
 
        try {
144
 
            $params = array();
145
 
            $select = 'SELECT displayProfileId, name, type, config, isDefault, userId ';
146
 
 
147
 
            $body = ' FROM `displayprofile` WHERE 1 = 1 ';
148
 
 
149
 
            if ($this->getSanitizer()->getInt('displayProfileId', $filterBy) !== null) {
150
 
                $body .= ' AND displayProfileId = :displayProfileId ';
151
 
                $params['displayProfileId'] = $this->getSanitizer()->getInt('displayProfileId', $filterBy);
152
 
            }
153
 
 
154
 
            if ($this->getSanitizer()->getInt('isDefault', $filterBy) !== null) {
155
 
                $body .= ' AND isDefault = :isDefault ';
156
 
                $params['isDefault'] = $this->getSanitizer()->getInt('isDefault', $filterBy);
157
 
            }
158
 
 
159
 
            // Filter by DisplayProfile Name?
160
 
            if ($this->getSanitizer()->getString('displayProfile', $filterBy) != null) {
161
 
                // convert into a space delimited array
162
 
                $names = explode(' ', $this->getSanitizer()->getString('displayProfile', $filterBy));
163
 
 
164
 
                $i = 0;
165
 
                foreach ($names as $searchName) {
166
 
                    $i++;
167
 
                    // Not like, or like?
168
 
                    if (substr($searchName, 0, 1) == '-') {
169
 
                        $body .= " AND  `displayprofile`.name NOT LIKE :search$i ";
170
 
                        $params['search' . $i] = '%' . ltrim(($searchName), '-') . '%';
171
 
                    }
172
 
                    else {
173
 
                        $body .= " AND  `displayprofile`.name LIKE :search$i ";
174
 
                        $params['search' . $i] = '%' . $searchName . '%';
175
 
                    }
176
 
                }
177
 
            }
178
 
 
179
 
            if ($this->getSanitizer()->getString('type', $filterBy) != null) {
180
 
                $body .= ' AND type = :type ';
181
 
                $params['type'] = $this->getSanitizer()->getString('type', $filterBy);
182
 
            }
183
 
 
184
 
            if ($this->getSanitizer()->getInt('commandId', $filterBy) !== null) {
185
 
                $body .= '
186
 
                    AND `displayprofile`.displayProfileId IN (
187
 
                        SELECT `lkcommanddisplayprofile`.displayProfileId
188
 
                          FROM `lkcommanddisplayprofile`
189
 
                         WHERE `lkcommanddisplayprofile`.commandId = :commandId
190
 
                    )
191
 
                ';
192
 
 
193
 
                $params['commandId'] = $this->getSanitizer()->getInt('commandId', $filterBy);
194
 
            }
195
 
 
196
 
            // Sorting?
197
 
            $order = '';
198
 
            if (is_array($sortOrder))
199
 
                $order .= 'ORDER BY ' . implode(',', $sortOrder);
200
 
 
201
 
            $limit = '';
202
 
            // Paging
203
 
            if ($filterBy !== null && $this->getSanitizer()->getInt('start', $filterBy) !== null && $this->getSanitizer()->getInt('length', $filterBy) !== null) {
204
 
                $limit = ' LIMIT ' . intval($this->getSanitizer()->getInt('start', $filterBy), 0) . ', ' . $this->getSanitizer()->getInt('length', 10, $filterBy);
205
 
            }
206
 
 
207
 
            $sql = $select . $body . $order . $limit;
208
 
 
209
 
 
210
 
 
211
 
            foreach ($this->getStore()->select($sql, $params) as $row) {
212
 
                $profiles[] = $this->createEmpty()->hydrate($row);
213
 
            }
214
 
 
215
 
            // Paging
216
 
            if ($limit != '' && count($profiles) > 0) {
217
 
                $results = $this->getStore()->select('SELECT COUNT(*) AS total ' . $body, $params);
218
 
                $this->_countLast = intval($results[0]['total']);
219
 
            }
220
 
 
221
 
            return $profiles;
222
 
 
223
 
        } catch (\Exception $e) {
224
 
 
225
 
            $this->getLog()->error($e);
226
 
 
227
 
            throw new NotFoundException();
228
 
        }
229
 
    }
230
 
}
 
 
b'\\ No newline at end of file'