~xibo-maintainers/xibo/tempel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2015 Spring Signage Ltd
 *
 * This file (Tag.php) is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */


namespace Xibo\Entity;
use Xibo\Service\LogServiceInterface;
use Xibo\Storage\StorageServiceInterface;


/**
 * Class Tag
 * @package Xibo\Entity
 *
 * @SWG\Definition()
 */
class Tag implements \JsonSerializable
{
    use EntityTrait;

    /**
     * @SWG\Property(description="The Tag ID")
     * @var int
     */
    public $tagId;

    /**
     * @SWG\Property(description="The Tag Name")
     * @var string
     */
    public $tag;

    /**
     * @SWG\Property(description="An array of layoutIDs with this Tag")
     * @var int[]
     */
    public $layoutIds = [];
    
    /**
     * @SWG\Property(description="An array of campaignIDs with this Tag")
     * @var int[]
     */
    public $campaignIds = [];    

    /**
     * @SWG\Property(description="An array of mediaIds with this Tag")
     * @var int[]
     */
    public $mediaIds = [];

    private $originalLayoutIds = [];
    private $originalMediaIds = [];
    private $originalCampaignIds = [];

    /**
     * Entity constructor.
     * @param StorageServiceInterface $store
     * @param LogServiceInterface $log
     */
    public function __construct($store, $log)
    {
        $this->setCommonDependencies($store, $log);
    }

    public function __clone()
    {
        $this->tagId = null;
    }

    /**
     * Assign Layout
     * @param int $layoutId
     */
    public function assignLayout($layoutId)
    {
        $this->load();

        if (!in_array($layoutId, $this->layoutIds))
            $this->layoutIds[] = $layoutId;
    }

    /**
     * Unassign Layout
     * @param int $layoutId
     */
    public function unassignLayout($layoutId)
    {
        $this->load();

        $this->layoutIds = array_diff($this->layoutIds, [$layoutId]);
    }

    /**
     * Assign Media
     * @param int $mediaId
     */
    public function assignMedia($mediaId)
    {
        $this->load();

        if (!in_array($mediaId, $this->mediaIds))
            $this->mediaIds[] = $mediaId;
    }

    /**
     * Unassign Media
     * @param int $mediaId
     */
    public function unassignMedia($mediaId)
    {
        $this->load();

        $this->mediaIds = array_diff($this->mediaIds, [$mediaId]);
    }
    
    /**
     * Assign Campaign
     * @param int $campaignId
     */
    public function assignCampaign($campaignId)
    {
        $this->load();

        if (!in_array($campaignId, $this->campaignIds))
            $this->campaignIds[] = $campaignId;
    }

    /**
     * Unassign Campaign
     * @param int $campaignId
     */
    public function unassignCampaign($campaignId)
    {
        $this->load();

        $this->campaignIds = array_diff($this->campaignIds, [$campaignId]);
    }

    /**
     * Load
     */
    public function load()
    {
        if ($this->tagId == null || $this->loaded)
            return;

        $this->layoutIds = [];
        foreach ($this->getStore()->select('SELECT layoutId FROM `lktaglayout` WHERE tagId = :tagId', ['tagId' => $this->tagId]) as $row) {
            $this->layoutIds[] = $row['layoutId'];
        }

        if (DBVERSION >= 129) {
            $this->campaignIds = [];
            foreach ($this->getStore()->select('SELECT campaignId FROM `lktagcampaign` WHERE tagId = :tagId', ['tagId' => $this->tagId]) as $row) {
                $this->campaignIds[] = $row['campaignId'];
            }
        }

        $this->mediaIds = [];
        foreach ($this->getStore()->select('SELECT mediaId FROM `lktagmedia` WHERE tagId = :tagId', ['tagId' => $this->tagId]) as $row) {
            $this->mediaIds[] = $row['mediaId'];
        }

        // Set the originals
        $this->originalLayoutIds = $this->layoutIds;
        $this->originalCampaignIds = $this->campaignIds;
        $this->originalMediaIds = $this->mediaIds;
        
        $this->loaded = true;
    }

    /**
     * Save
     */
    public function save()
    {
        // If the tag doesn't exist already - save it
        if ($this->tagId == null || $this->tagId == 0)
            $this->add();

        // Manage the links to layouts and media
        $this->linkLayouts();
        $this->linkCampaigns();
        $this->linkMedia();
        $this->removeAssignments();

        $this->getLog()->debug('Saving Tag: %s, %d', $this->tag, $this->tagId);
    }

    /**
     * Remove Assignments
     */
    public function removeAssignments()
    {
        $this->unlinkLayouts();
        $this->unlinkCampaigns();
        $this->unlinkMedia();
    }

    /**
     * Add a tag
     * @throws \PDOException
     */
    private function add()
    {
        $this->tagId = $this->getStore()->insert('INSERT INTO `tag` (tag) VALUES (:tag) ON DUPLICATE KEY UPDATE tag = tag', array('tag' => $this->tag));
    }

    /**
     * Link all assigned layouts
     */
    private function linkLayouts()
    {
        $layoutsToLink = array_diff($this->layoutIds, $this->originalLayoutIds);

        $this->getLog()->debug('Linking %d layouts to Tag %s', count($layoutsToLink), $this->tag);

        // Layouts that are in layoutIds but not in originalLayoutIds
        foreach ($layoutsToLink as $layoutId) {
            $this->getStore()->update('INSERT INTO `lktaglayout` (tagId, layoutId) VALUES (:tagId, :layoutId) ON DUPLICATE KEY UPDATE layoutId = layoutId', array(
                'tagId' => $this->tagId,
                'layoutId' => $layoutId
            ));
        }
    }

    /**
     * Unlink all assigned Layouts
     */
    private function unlinkLayouts()
    {
        // Layouts that are in the originalLayoutIds but not in the current layoutIds
        $layoutsToUnlink = array_diff($this->originalLayoutIds, $this->layoutIds);

        $this->getLog()->debug('Unlinking %d layouts from Tag %s', count($layoutsToUnlink), $this->tag);

        if (count($layoutsToUnlink) <= 0)
            return;

        // Unlink any layouts that are NOT in the collection
        $params = ['tagId' => $this->tagId];

        $sql = 'DELETE FROM `lktaglayout` WHERE tagId = :tagId AND layoutId IN (0';

        $i = 0;
        foreach ($layoutsToUnlink as $layoutId) {
            $i++;
            $sql .= ',:layoutId' . $i;
            $params['layoutId' . $i] = $layoutId;
        }

        $sql .= ')';



        $this->getStore()->update($sql, $params);
    }
    
    
    /**
     * Link all assigned campaigns
     */
    private function linkCampaigns()
    {
        // Didn't exist before 129
        if (DBVERSION < 129)
            return;

        $campaignsToLink = array_diff($this->campaignIds, $this->originalCampaignIds);

        $this->getLog()->debug('Linking %d campaigns to Tag %s', count($campaignsToLink), $this->tag);

        // Campaigns that are in campaignIds but not in originalCampaignIds
        foreach ($campaignsToLink as $campaignId) {
            $this->getStore()->update('INSERT INTO `lktagcampaign` (tagId, CampaignId) VALUES (:tagId, :CampaignId) ON DUPLICATE KEY UPDATE campaignId = campaignId', array(
                'tagId' => $this->tagId,
                'CampaignId' => $campaignId
            ));
        }
    }

    /**
     * Unlink all assigned campaigns
     */
    private function unlinkCampaigns()
    {
        // Didn't exist before 129
        if (DBVERSION < 129)
            return;

        // Campaigns that are in the originalCampaignIds but not in the current campaignIds
        $campaignsToUnlink = array_diff($this->originalCampaignIds, $this->campaignIds);

        $this->getLog()->debug('Unlinking %d campaigns from Tag %s', count($campaignsToUnlink), $this->tag);

        if (count($campaignsToUnlink) <= 0)
            return;

        // Unlink any campaigns that are NOT in the collection
        $params = ['tagId' => $this->tagId];

        $sql = 'DELETE FROM `lktagcampaign` WHERE tagId = :tagId AND CampaignId IN (0';

        $i = 0;
        foreach ($campaignsToUnlink as $campaignId) {
            $i++;
            $sql .= ',:CampaignId' . $i;
            $params['CampaignId' . $i] = $campaignId;
        }

        $sql .= ')';



        $this->getStore()->update($sql, $params);
    }

    /**
     * Link all assigned media
     */
    private function linkMedia()
    {
        $mediaToLink = array_diff($this->mediaIds, $this->originalMediaIds);

        $this->getLog()->debug('Linking %d media to Tag %s', count($mediaToLink), $this->tag);

        foreach ($mediaToLink as $mediaId) {
            $this->getStore()->update('INSERT INTO `lktagmedia` (tagId, mediaId) VALUES (:tagId, :mediaId) ON DUPLICATE KEY UPDATE mediaId = mediaId', array(
                'tagId' => $this->tagId,
                'mediaId' => $mediaId
            ));
        }
    }

    /**
     * Unlink all assigned media
     */
    private function unlinkMedia()
    {
        $mediaToUnlink = array_diff($this->originalMediaIds, $this->mediaIds);

        $this->getLog()->debug('Unlinking %d media from Tag %s', count($mediaToUnlink), $this->tag);

        // Unlink any layouts that are NOT in the collection
        if (count($mediaToUnlink) <= 0)
            return;

        $params = ['tagId' => $this->tagId];

        $sql = 'DELETE FROM `lktagmedia` WHERE tagId = :tagId AND mediaId IN (0';

        $i = 0;
        foreach ($mediaToUnlink as $mediaId) {
            $i++;
            $sql .= ',:mediaId' . $i;
            $params['mediaId' . $i] = $mediaId;
        }

        $sql .= ')';



        $this->getStore()->update($sql, $params);
    }
}