~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Factory/CampaignFactory.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:
22
22
 
23
23
namespace Xibo\Factory;
24
24
 
 
25
 
25
26
use Xibo\Entity\Campaign;
26
 
use Xibo\Entity\User;
27
27
use Xibo\Exception\NotFoundException;
28
 
use Xibo\Service\LogServiceInterface;
29
 
use Xibo\Service\SanitizerServiceInterface;
30
 
use Xibo\Storage\StorageServiceInterface;
31
28
 
32
 
/**
33
 
 * Class CampaignFactory
34
 
 * @package Xibo\Factory
35
 
 */
36
 
class CampaignFactory extends BaseFactory
 
29
class CampaignFactory
37
30
{
38
31
    /**
39
 
     * @var PermissionFactory
40
 
     */
41
 
    private $permissionFactory;
42
 
 
43
 
    /**
44
 
     * @var ScheduleFactory
45
 
     */
46
 
    private $scheduleFactory;
47
 
 
48
 
    /**
49
 
     * @var DisplayFactory
50
 
     */
51
 
    private $displayFactory;
52
 
    
53
 
    /**
54
 
     * @var TagFactory
55
 
     */
56
 
    private $tagFactory;
57
 
 
58
 
    /**
59
 
     * Construct a factory
60
 
     * @param StorageServiceInterface $store
61
 
     * @param LogServiceInterface $log
62
 
     * @param SanitizerServiceInterface $sanitizerService
63
 
     * @param User $user
64
 
     * @param UserFactory $userFactory
65
 
     * @param PermissionFactory $permissionFactory
66
 
     * @param ScheduleFactory $scheduleFactory
67
 
     * @param DisplayFactory $displayFactory
68
 
     */
69
 
    public function __construct($store, $log, $sanitizerService, $user, $userFactory, $permissionFactory, $scheduleFactory, $displayFactory, $tagFactory)
70
 
    {
71
 
        $this->setCommonDependencies($store, $log, $sanitizerService);
72
 
        $this->setAclDependencies($user, $userFactory);
73
 
        $this->permissionFactory = $permissionFactory;
74
 
        $this->scheduleFactory = $scheduleFactory;
75
 
        $this->displayFactory = $displayFactory;
76
 
        $this->tagFactory = $tagFactory;
77
 
    }
78
 
 
79
 
    /**
80
 
     * @return Campaign
81
 
     */
82
 
    public function createEmpty()
83
 
    {
84
 
        return new Campaign($this->getStore(), $this->getLog(), $this->permissionFactory, $this->scheduleFactory, $this->displayFactory, $this->tagFactory);
85
 
    }
86
 
 
87
 
    /**
88
 
     * Create Campaign
89
 
     * @param string $name
90
 
     * @param int $userId
91
 
     * @param string $tags
92
 
     * @return Campaign
93
 
     */
94
 
    public function create($name, $userId, $tags)
95
 
    {
96
 
        $campaign = $this->createEmpty();
97
 
        $campaign->ownerId = $userId;
98
 
        $campaign->campaign = $name;
99
 
        
100
 
        // Create some tags
101
 
        $campaign->tags = $this->tagFactory->tagsFromString($tags);
102
 
 
103
 
        return $campaign;
104
 
    }
105
 
 
106
 
    /**
107
32
     * Get Campaign by ID
108
33
     * @param int $campaignId
109
34
     * @return Campaign
110
35
     * @throws NotFoundException
111
36
     */
112
 
    public function getById($campaignId)
 
37
    public static function getById($campaignId)
113
38
    {
114
 
        $this->getLog()->debug('CampaignFactory getById(%d)', $campaignId);
115
 
 
116
 
        $campaigns = $this->query(null, array('disableUserCheck' => 1, 'campaignId' => $campaignId, 'isLayoutSpecific' => -1, 'excludeTemplates' => -1));
 
39
        $campaigns = CampaignFactory::query(null, array('campaignId' => $campaignId));
117
40
 
118
41
        if (count($campaigns) <= 0) {
119
 
            $this->getLog()->debug('Campaign not found with ID %d', $campaignId);
120
42
            throw new NotFoundException(\__('Campaign not found'));
121
43
        }
122
44
 
125
47
    }
126
48
 
127
49
    /**
128
 
     * Get Campaign by Owner Id
129
 
     * @param int $ownerId
130
 
     * @return array[Campaign]
131
 
     */
132
 
    public function getByOwnerId($ownerId)
133
 
    {
134
 
        return $this->query(null, array('ownerId' => $ownerId, 'excludeTemplates' => -1));
135
 
    }
136
 
 
137
 
    /**
138
 
     * Get Campaign by Layout
139
 
     * @param int $layoutId
140
 
     * @return array[Campaign]
141
 
     */
142
 
    public function getByLayoutId($layoutId)
143
 
    {
144
 
        return $this->query(null, array('disableUserCheck' => 1, 'layoutId' => $layoutId, 'excludeTemplates' => -1));
145
 
    }
146
 
 
147
 
    /**
148
50
     * Query Campaigns
149
51
     * @param array $sortOrder
150
52
     * @param array $filterBy
151
53
     * @return array[Campaign]
152
54
     */
153
 
    public function query($sortOrder = null, $filterBy = array(), $options = array())
 
55
    public static function query($sortOrder = null, $filterBy = array())
154
56
    {
155
57
        if ($sortOrder == null)
156
 
            $sortOrder = array('campaign');
 
58
            $sortOrder = array('Campaign');
157
59
 
158
60
        $campaigns = array();
159
61
        $params = array();
160
62
 
161
 
        $select = '
162
 
        SELECT `campaign`.campaignId, `campaign`.campaign, `campaign`.isLayoutSpecific, `campaign`.userId AS ownerId,
163
 
            (
164
 
                SELECT COUNT(*)
165
 
                FROM lkcampaignlayout
166
 
                WHERE lkcampaignlayout.campaignId = `campaign`.campaignId
167
 
            ) AS numberLayouts,
168
 
            MAX(CASE WHEN `campaign`.IsLayoutSpecific = 1 THEN `layout`.retired ELSE 0 END) AS retired
169
 
        ';
170
 
 
171
 
        // Didn't exist before 129
172
 
        if (DBVERSION >= 129) {
173
 
            $select .= ',
174
 
                (
175
 
                    SELECT GROUP_CONCAT(DISTINCT tag) 
176
 
                    FROM tag INNER JOIN lktagcampaign ON lktagcampaign.tagId = tag.tagId 
177
 
                    WHERE lktagcampaign.campaignId = campaign.CampaignID 
178
 
                    GROUP BY lktagcampaign.campaignId
179
 
                ) AS tags
180
 
            ';
181
 
        }
182
 
 
183
 
        $body  = '
184
 
            FROM `campaign`
185
 
              LEFT OUTER JOIN `lkcampaignlayout`
186
 
              ON lkcampaignlayout.CampaignID = campaign.CampaignID
187
 
              LEFT OUTER JOIN `layout`
188
 
              ON lkcampaignlayout.LayoutID = layout.LayoutID
189
 
           WHERE 1 = 1
190
 
        ';
191
 
 
192
 
        // View Permissions
193
 
        $this->viewPermissionSql('Xibo\Entity\Campaign', $body, $params, '`campaign`.campaignId', '`campaign`.userId', $filterBy);
194
 
 
195
 
        if ($this->getSanitizer()->getString('isLayoutSpecific', 0, $filterBy) != -1) {
196
 
            // Exclude layout specific campaigns
197
 
            $body .= " AND `campaign`.isLayoutSpecific = :isLayoutSpecific ";
198
 
            $params['isLayoutSpecific'] = $this->getSanitizer()->getString('isLayoutSpecific', 0, $filterBy);
199
 
        }
200
 
 
201
 
        if ($this->getSanitizer()->getString('campaignId', 0, $filterBy) != 0) {
202
 
            // Join Campaign back onto it again
203
 
            $body .= " AND `campaign`.campaignId = :campaignId ";
204
 
            $params['campaignId'] = $this->getSanitizer()->getString('campaignId', 0, $filterBy);
205
 
        }
206
 
 
207
 
        if ($this->getSanitizer()->getString('ownerId', 0, $filterBy) != 0) {
208
 
            // Join Campaign back onto it again
209
 
            $body .= " AND `campaign`.userId = :ownerId ";
210
 
            $params['ownerId'] = $this->getSanitizer()->getString('ownerId', 0, $filterBy);
211
 
        }
212
 
 
213
 
        if ($this->getSanitizer()->getString('layoutId', 0, $filterBy) != 0) {
214
 
            // Filter by Layout
215
 
            $body .= " AND `lkcampaignlayout`.layoutId = :layoutId ";
216
 
            $params['layoutId'] = $this->getSanitizer()->getString('layoutId', 0, $filterBy);
217
 
        }
218
 
        
219
 
        if ($this->getSanitizer()->getString('hasLayouts', 0, $filterBy) != 0) {
220
 
 
221
 
            $body .= " AND (
222
 
                SELECT COUNT(*)
223
 
                FROM lkcampaignlayout
224
 
                WHERE lkcampaignlayout.campaignId = `campaign`.campaignId
225
 
                )";
226
 
    
227
 
            $body .= ($this->getSanitizer()->getString('hasLayouts', 0, $filterBy) == 1) ? " = 0 " : " > 0";
228
 
        }
229
 
 
230
 
        // Tags
231
 
        if ($this->getSanitizer()->getString('tags', $filterBy) != '') {
232
 
 
233
 
            $tagFilter = $this->getSanitizer()->getString('tags', $filterBy);
234
 
 
235
 
            if (trim($tagFilter) === '--no-tag') {
236
 
                $body .= ' AND `campaign`.campaignID NOT IN (
237
 
                    SELECT `lktagcampaign`.campaignId
238
 
                     FROM `tag`
239
 
                        INNER JOIN `lktagcampaign`
240
 
                        ON `lktagcampaign`.tagId = `tag`.tagId
241
 
                    )
242
 
                ';
243
 
            } else {
244
 
                $body .= " AND campaign.campaignID IN (
245
 
                SELECT lktagcampaign.campaignId
246
 
                  FROM tag
247
 
                    INNER JOIN lktagcampaign
248
 
                    ON lktagcampaign.tagId = tag.tagId
249
 
                ";
250
 
                $i = 0;
251
 
                foreach (explode(',', $tagFilter) as $tag) {
252
 
                    $i++;
253
 
 
254
 
                    if ($i == 1)
255
 
                        $body .= " WHERE tag LIKE :tags$i ";
256
 
                    else
257
 
                        $body .= " OR tag LIKE :tags$i ";
258
 
 
259
 
                    $params['tags' . $i] = '%' . $tag . '%';
260
 
                }
261
 
 
262
 
                $body .= " ) ";
263
 
            }
264
 
        }
265
 
 
266
 
        if ($this->getSanitizer()->getString('name', $filterBy) != '') {
 
63
        $sql  = "SELECT campaign.CampaignID, Campaign, IsLayoutSpecific, COUNT(lkcampaignlayout.LayoutID) AS NumLayouts, MIN(layout.retired) AS Retired, `campaign`.userId ";
 
64
        $sql .= "  FROM `campaign` ";
 
65
        $sql .= "   LEFT OUTER JOIN `lkcampaignlayout` ";
 
66
        $sql .= "   ON lkcampaignlayout.CampaignID = campaign.CampaignID ";
 
67
        $sql .= "   LEFT OUTER JOIN `layout` ";
 
68
        $sql .= "   ON lkcampaignlayout.LayoutID = layout.LayoutID ";
 
69
        $sql .= " WHERE 1 = 1 ";
 
70
 
 
71
        if (\Xibo\Helper\Sanitize::string('campaignId', 0, $filterBy) != 0) {
 
72
            // Join Campaign back onto it again
 
73
            $sql .= " AND `campaign`.campaignId = :campaignId ";
 
74
            $params['campaignId'] = \Xibo\Helper\Sanitize::string('campaignId', 0, $filterBy);
 
75
        }
 
76
 
 
77
        if (\Kit::GetParam('name', $filterBy, _STRING) != '') {
267
78
            // convert into a space delimited array
268
 
            $names = explode(' ', $this->getSanitizer()->getString('name', $filterBy));
 
79
            $names = explode(' ', \Kit::GetParam('name', $filterBy, _STRING));
269
80
 
270
81
            $i = 0;
271
82
            foreach($names as $searchName) {
273
84
 
274
85
                // Not like, or like?
275
86
                if (substr($searchName, 0, 1) == '-') {
276
 
                    $body .= " AND campaign.Campaign NOT LIKE :search$i ";
 
87
                    $sql .= " AND campaign.Campaign NOT LIKE :search$i ";
277
88
                    $params['search' . $i] = '%' . ltrim($searchName) . '%';
278
89
                }
279
90
                else {
280
 
                    $body .= " AND campaign.Campaign LIKE :search$i ";
 
91
                    $sql .= " AND campaign.Campaign LIKE :search$i ";
281
92
                    $params['search' . $i] = '%' . ltrim($searchName) . '%';
282
93
                }
283
94
            }
284
95
        }
285
96
 
286
 
        // Exclude templates by default
287
 
        if ($this->getSanitizer()->getInt('excludeTemplates', 1, $filterBy) != -1) {
288
 
            if ($this->getSanitizer()->getInt('excludeTemplates', 1, $filterBy) == 1) {
289
 
                $body .= " AND `campaign`.campaignId NOT IN (SELECT `campaignId` FROM `lkcampaignlayout` WHERE layoutId IN (SELECT layoutId FROM lktaglayout WHERE tagId = 1)) ";
290
 
            } else {
291
 
                $body .= " AND `campaign`.campaignId IN (SELECT `campaignId` FROM `lkcampaignlayout` WHERE layoutId IN (SELECT layoutId FROM lktaglayout WHERE tagId = 1)) ";
292
 
            }
293
 
        }
294
 
 
295
 
        $group = 'GROUP BY `campaign`.CampaignID, Campaign, IsLayoutSpecific, `campaign`.userId ';
296
 
 
297
 
        if ($this->getSanitizer()->getInt('retired', -1, $filterBy) != -1) {
298
 
            $group .= ' HAVING retired = :retired ';
299
 
            $params['retired'] = $this->getSanitizer()->getInt('retired', $filterBy);
300
 
 
301
 
            if ($this->getSanitizer()->getInt('includeCampaignId', $filterBy) !== null) {
302
 
                $group .= ' OR campaign.campaignId = :includeCampaignId ';
303
 
                $params['includeCampaignId'] = $this->getSanitizer()->getInt('includeCampaignId', $filterBy);
304
 
            }
305
 
        }
 
97
        $sql .= 'GROUP BY campaign.CampaignID, Campaign, IsLayoutSpecific, `campaign`.userId ';
306
98
 
307
99
        // Sorting?
308
 
        $order = '';
309
100
        if (is_array($sortOrder))
310
 
            $order .= 'ORDER BY ' . implode(',', $sortOrder);
311
 
 
312
 
        $limit = '';
313
 
        // Paging
314
 
        if ($filterBy !== null && $this->getSanitizer()->getInt('start', $filterBy) !== null && $this->getSanitizer()->getInt('length', $filterBy) !== null) {
315
 
            $limit = ' LIMIT ' . intval($this->getSanitizer()->getInt('start', $filterBy), 0) . ', ' . $this->getSanitizer()->getInt('length', 10, $filterBy);
316
 
        }
317
 
 
318
 
        $intProperties = ['intProperties' => ['numberLayouts']];
319
 
 
320
 
        // Layout durations
321
 
        if ($this->getSanitizer()->getInt('totalDuration', 0, $options) != 0) {
322
 
            $select .= ", SUM(`layout`.duration) AS totalDuration";
323
 
            $intProperties = ['intProperties' => ['numberLayouts', 'totalDuration', 'displayOrder']];
324
 
        }
325
 
 
326
 
        $sql = $select . $body . $group . $order . $limit;
327
 
 
328
 
 
329
 
        foreach ($this->getStore()->select($sql, $params) as $row) {
330
 
            $campaigns[] = $this->createEmpty()->hydrate($row, $intProperties);
331
 
        }
332
 
 
333
 
        // Paging
334
 
        if ($limit != '' && count($campaigns) > 0) {
335
 
            $results = $this->getStore()->select('SELECT COUNT(DISTINCT campaign.campaignId) AS total ' . $body, $params);
336
 
            $this->_countLast = intval($results[0]['total']);
 
101
            $sql .= 'ORDER BY ' . implode(',', $sortOrder);
 
102
 
 
103
        \Xibo\Helper\Log::sql($sql, $params);
 
104
 
 
105
        foreach (\PDOConnect::select($sql, $params) as $row) {
 
106
 
 
107
            $campaign = new Campaign();
 
108
 
 
109
            // Validate each param and add it to the array.
 
110
            $campaign->campaignId = \Xibo\Helper\Sanitize::int($row['CampaignID']);
 
111
            $campaign->campaign = \Xibo\Helper\Sanitize::string($row['Campaign']);
 
112
            $campaign->numberLayouts = \Xibo\Helper\Sanitize::int($row['NumLayouts']);
 
113
            $campaign->isLayout = (\Xibo\Helper\Sanitize::int($row['IsLayoutSpecific']) == 1);
 
114
            $campaign->retired = \Xibo\Helper\Sanitize::int($row['Retired']);
 
115
            $campaign->ownerId = \Xibo\Helper\Sanitize::int($row['userId']);
 
116
 
 
117
            // Filter out campaigns that have all retired layouts
 
118
            if (\Xibo\Helper\Sanitize::int('retired', -1, $filterBy) != -1) {
 
119
                if ($row['Retired'] != \Xibo\Helper\Sanitize::int('retired', $filterBy))
 
120
                    continue;
 
121
            }
 
122
 
 
123
            $campaigns[] = $campaign;
337
124
        }
338
125
 
339
126
        return $campaigns;
340
127
    }
341
 
 
342
 
}
 
128
}
 
 
b'\\ No newline at end of file'