23
23
namespace Xibo\Factory;
25
26
use Xibo\Entity\Campaign;
27
27
use Xibo\Exception\NotFoundException;
28
use Xibo\Service\LogServiceInterface;
29
use Xibo\Service\SanitizerServiceInterface;
30
use Xibo\Storage\StorageServiceInterface;
33
* Class CampaignFactory
34
* @package Xibo\Factory
36
class CampaignFactory extends BaseFactory
39
* @var PermissionFactory
41
private $permissionFactory;
44
* @var ScheduleFactory
46
private $scheduleFactory;
51
private $displayFactory;
60
* @param StorageServiceInterface $store
61
* @param LogServiceInterface $log
62
* @param SanitizerServiceInterface $sanitizerService
64
* @param UserFactory $userFactory
65
* @param PermissionFactory $permissionFactory
66
* @param ScheduleFactory $scheduleFactory
67
* @param DisplayFactory $displayFactory
69
public function __construct($store, $log, $sanitizerService, $user, $userFactory, $permissionFactory, $scheduleFactory, $displayFactory, $tagFactory)
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;
82
public function createEmpty()
84
return new Campaign($this->getStore(), $this->getLog(), $this->permissionFactory, $this->scheduleFactory, $this->displayFactory, $this->tagFactory);
94
public function create($name, $userId, $tags)
96
$campaign = $this->createEmpty();
97
$campaign->ownerId = $userId;
98
$campaign->campaign = $name;
101
$campaign->tags = $this->tagFactory->tagsFromString($tags);
107
32
* Get Campaign by ID
108
33
* @param int $campaignId
109
34
* @return Campaign
110
35
* @throws NotFoundException
112
public function getById($campaignId)
37
public static function getById($campaignId)
114
$this->getLog()->debug('CampaignFactory getById(%d)', $campaignId);
116
$campaigns = $this->query(null, array('disableUserCheck' => 1, 'campaignId' => $campaignId, 'isLayoutSpecific' => -1, 'excludeTemplates' => -1));
39
$campaigns = CampaignFactory::query(null, array('campaignId' => $campaignId));
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'));
128
* Get Campaign by Owner Id
129
* @param int $ownerId
130
* @return array[Campaign]
132
public function getByOwnerId($ownerId)
134
return $this->query(null, array('ownerId' => $ownerId, 'excludeTemplates' => -1));
138
* Get Campaign by Layout
139
* @param int $layoutId
140
* @return array[Campaign]
142
public function getByLayoutId($layoutId)
144
return $this->query(null, array('disableUserCheck' => 1, 'layoutId' => $layoutId, 'excludeTemplates' => -1));
149
51
* @param array $sortOrder
150
52
* @param array $filterBy
151
53
* @return array[Campaign]
153
public function query($sortOrder = null, $filterBy = array(), $options = array())
55
public static function query($sortOrder = null, $filterBy = array())
155
57
if ($sortOrder == null)
156
$sortOrder = array('campaign');
58
$sortOrder = array('Campaign');
158
60
$campaigns = array();
159
61
$params = array();
162
SELECT `campaign`.campaignId, `campaign`.campaign, `campaign`.isLayoutSpecific, `campaign`.userId AS ownerId,
165
FROM lkcampaignlayout
166
WHERE lkcampaignlayout.campaignId = `campaign`.campaignId
168
MAX(CASE WHEN `campaign`.IsLayoutSpecific = 1 THEN `layout`.retired ELSE 0 END) AS retired
171
// Didn't exist before 129
172
if (DBVERSION >= 129) {
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
185
LEFT OUTER JOIN `lkcampaignlayout`
186
ON lkcampaignlayout.CampaignID = campaign.CampaignID
187
LEFT OUTER JOIN `layout`
188
ON lkcampaignlayout.LayoutID = layout.LayoutID
193
$this->viewPermissionSql('Xibo\Entity\Campaign', $body, $params, '`campaign`.campaignId', '`campaign`.userId', $filterBy);
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);
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);
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);
213
if ($this->getSanitizer()->getString('layoutId', 0, $filterBy) != 0) {
215
$body .= " AND `lkcampaignlayout`.layoutId = :layoutId ";
216
$params['layoutId'] = $this->getSanitizer()->getString('layoutId', 0, $filterBy);
219
if ($this->getSanitizer()->getString('hasLayouts', 0, $filterBy) != 0) {
223
FROM lkcampaignlayout
224
WHERE lkcampaignlayout.campaignId = `campaign`.campaignId
227
$body .= ($this->getSanitizer()->getString('hasLayouts', 0, $filterBy) == 1) ? " = 0 " : " > 0";
231
if ($this->getSanitizer()->getString('tags', $filterBy) != '') {
233
$tagFilter = $this->getSanitizer()->getString('tags', $filterBy);
235
if (trim($tagFilter) === '--no-tag') {
236
$body .= ' AND `campaign`.campaignID NOT IN (
237
SELECT `lktagcampaign`.campaignId
239
INNER JOIN `lktagcampaign`
240
ON `lktagcampaign`.tagId = `tag`.tagId
244
$body .= " AND campaign.campaignID IN (
245
SELECT lktagcampaign.campaignId
247
INNER JOIN lktagcampaign
248
ON lktagcampaign.tagId = tag.tagId
251
foreach (explode(',', $tagFilter) as $tag) {
255
$body .= " WHERE tag LIKE :tags$i ";
257
$body .= " OR tag LIKE :tags$i ";
259
$params['tags' . $i] = '%' . $tag . '%';
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 ";
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);
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));
271
82
foreach($names as $searchName) {
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) . '%';
280
$body .= " AND campaign.Campaign LIKE :search$i ";
91
$sql .= " AND campaign.Campaign LIKE :search$i ";
281
92
$params['search' . $i] = '%' . ltrim($searchName) . '%';
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)) ";
291
$body .= " AND `campaign`.campaignId IN (SELECT `campaignId` FROM `lkcampaignlayout` WHERE layoutId IN (SELECT layoutId FROM lktaglayout WHERE tagId = 1)) ";
295
$group = 'GROUP BY `campaign`.CampaignID, Campaign, IsLayoutSpecific, `campaign`.userId ';
297
if ($this->getSanitizer()->getInt('retired', -1, $filterBy) != -1) {
298
$group .= ' HAVING retired = :retired ';
299
$params['retired'] = $this->getSanitizer()->getInt('retired', $filterBy);
301
if ($this->getSanitizer()->getInt('includeCampaignId', $filterBy) !== null) {
302
$group .= ' OR campaign.campaignId = :includeCampaignId ';
303
$params['includeCampaignId'] = $this->getSanitizer()->getInt('includeCampaignId', $filterBy);
97
$sql .= 'GROUP BY campaign.CampaignID, Campaign, IsLayoutSpecific, `campaign`.userId ';
309
100
if (is_array($sortOrder))
310
$order .= 'ORDER BY ' . implode(',', $sortOrder);
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);
318
$intProperties = ['intProperties' => ['numberLayouts']];
321
if ($this->getSanitizer()->getInt('totalDuration', 0, $options) != 0) {
322
$select .= ", SUM(`layout`.duration) AS totalDuration";
323
$intProperties = ['intProperties' => ['numberLayouts', 'totalDuration', 'displayOrder']];
326
$sql = $select . $body . $group . $order . $limit;
329
foreach ($this->getStore()->select($sql, $params) as $row) {
330
$campaigns[] = $this->createEmpty()->hydrate($row, $intProperties);
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);
103
\Xibo\Helper\Log::sql($sql, $params);
105
foreach (\PDOConnect::select($sql, $params) as $row) {
107
$campaign = new Campaign();
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']);
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))
123
$campaigns[] = $campaign;
339
126
return $campaigns;
b'\\ No newline at end of file'