~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Entity/Campaign.php

  • Committer: GitHub
  • Author(s): Dan Garner
  • Date: 2016-11-16 11:51:16 UTC
  • Revision ID: git-v1:1cdd39222ff9fdd3fcc0bb8924cc706a24b18905
Bugfix/1.8.0 pack1 (#227)

* Library Tidy inaccessible
* Enabled notifications tidy task by default
* Implementation of "parent settings" so that TwitterMetro can use the API key/secret from Twitter.
* Fix for StatsArchive task infinite loop
* Fix for StatsArchive task processing ahead of now
* Flip clock to support countdown from a particular date, and daily countdown. xibosignage/xibo#936
* Fix date parsing when a timezone has been set
* Only save campaign if changes were made.
* Fix display notifications for removing dups
* XMR priv socket sleep to 100 ms
* Fix removal of Twig sources on release archive build. xibosignage/xibo#940
* Improve campaign layout assign/unassign xibosignage/xibo#941
* Tidy up unnecessary save in MediaInventory. Better logging for errors with sendfile from XMDS.
* Fix display status icon xibosignage/xibo#943
* Updated caching logic for required files
* Rewrite the nonce cache (required file) so that it uses a key per file instead of one storage object.
* Rework required files again - move back into the DB, but optimize nonce creation to ease contention.
* Further improvements to DB activity, basic connection management and stats reporting. Also deadlock protection statement.
* XMDS not updating bytes requested.
* More logging for retry deadlock loop
* Correct bandwidth logging. Fix deadlock loop.
* Improvements to media download (proper async). Fix for module downloads being copied rather than moved.
* Swagger doc fixes
* Convert XTR into a select and run single process script. Should fix it for windows.

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
    private $permissions = [];
82
82
    private $events = [];
83
83
 
 
84
    /** @var bool Have the Layout assignments changed? */
 
85
    private $layoutAssignmentsChanged = false;
 
86
 
84
87
    /**
85
88
     * @var PermissionFactory
86
89
     */
256
259
    {
257
260
        $this->load();
258
261
 
259
 
        if (!in_array($layout, $this->layouts))
 
262
        $layout->displayOrder = ($layout->displayOrder == null || $layout->displayOrder == 0) ? count($this->layouts) + 1 : $layout->displayOrder;
 
263
 
 
264
        $found = false;
 
265
        foreach ($this->layouts as $existingLayout) {
 
266
            if ($existingLayout->getId() === $layout->getId() && $existingLayout->displayOrder === $layout->displayOrder) {
 
267
                $found = true;
 
268
                break;
 
269
            }
 
270
        }
 
271
 
 
272
        if (!$found) {
 
273
            $this->layoutAssignmentsChanged = true;
260
274
            $this->layouts[] = $layout;
 
275
        }
261
276
    }
262
277
 
263
278
    /**
268
283
    {
269
284
        $this->load();
270
285
 
271
 
        $this->getLog()->debug('Unassigning Layout [%s] from Campaign [%s]. Display Order %d. Count before assign = %d', $layout, $this, $layout->displayOrder, count($this->layouts));
 
286
        $countBefore = count($this->layouts);
 
287
        $this->getLog()->debug('Unassigning Layout [%s] from Campaign [%s]. Display Order %d. Count before assign = %d', $layout, $this, $layout->displayOrder, $countBefore);
272
288
 
273
289
        $this->layouts = array_udiff($this->layouts, [$layout], function ($a, $b) {
274
290
            /**
276
292
             * @var Layout $b
277
293
             */
278
294
            // Are we a layout that has been configured with a display order, or are we a complete layout removal?
279
 
            if ($a->displayOrder === null || $b->displayOrder === null)
 
295
            if ($a->displayOrder == null || $b->displayOrder == null)
280
296
                $return = ($a->getId() - $b->getId());
281
297
            else
282
298
                $return = ($a->getId() - $b->getId()) + ($a->displayOrder - $b->displayOrder);
283
299
 
284
 
            $this->getLog()->debug('Comparing a [%d, %d] with b [%d, %d]. Return = %d', $a->layoutId, $a->displayOrder, $b->layoutId, $b->displayOrder, $return);
 
300
            //$this->getLog()->debug('Comparing a [%d, %d] with b [%d, %d]. Return = %d', $a->layoutId, $a->displayOrder, $b->layoutId, $b->displayOrder, $return);
285
301
            return $return;
286
302
        });
287
303
 
288
 
        $this->getLog()->debug('Count after unassign = %d', count($this->layouts));
 
304
        $countAfter = count($this->layouts);
 
305
        $this->getLog()->debug('Count after unassign = %d', $countAfter);
 
306
 
 
307
        if ($countBefore !== $countAfter)
 
308
            $this->layoutAssignmentsChanged = true;
289
309
    }
290
310
 
291
311
    private function add()
311
331
     */
312
332
    private function manageAssignments()
313
333
    {
314
 
        $this->getLog()->debug('Managing Assignments on %s', $this);
315
 
        $this->unlinkLayouts();
316
 
        $this->linkLayouts();
 
334
        if ($this->layoutAssignmentsChanged) {
 
335
            $this->getLog()->debug('Managing Assignments on ' . $this);
 
336
            $this->unlinkLayouts();
 
337
            $this->linkLayouts();
 
338
        } else {
 
339
            $this->getLog()->debug('Assignments have not changed on ' . $this);
 
340
        }
317
341
    }
318
342
 
319
343
    /**
325
349
        if (count($this->layouts) <= 0)
326
350
            return;
327
351
 
328
 
        $sql = 'INSERT INTO `lkcampaignlayout` (CampaignID, LayoutID, DisplayOrder) VALUES (:campaignId, :layoutId, :displayOrder) ON DUPLICATE KEY UPDATE layoutId = :layoutId2';
329
 
 
330
352
        // Sort the layouts by their display order
331
353
        usort($this->layouts, function($a, $b) {
332
354
            /** @var Layout $a */
342
364
 
343
365
        // Update the layouts, in order to have display order 1 to n
344
366
        $i = 0;
 
367
        $sql = 'INSERT INTO `lkcampaignlayout` (CampaignID, LayoutID, DisplayOrder) VALUES ';
 
368
        $params = [];
 
369
 
345
370
        foreach ($this->layouts as $layout) {
346
371
            $i++;
347
372
            $layout->displayOrder = $i;
348
 
        }
349
 
 
350
 
        foreach ($this->layouts as $layout) {
351
 
 
352
 
            $this->getStore()->insert($sql, array(
353
 
                'campaignId' => $this->campaignId,
354
 
                'displayOrder' => $layout->displayOrder,
355
 
                'layoutId' => $layout->layoutId,
356
 
                'layoutId2' => $layout->layoutId
357
 
            ));
358
 
        }
 
373
 
 
374
            $sql .= '(:campaignId_' . $i . ', :layoutId_' . $i . ', :displayOrder_' . $i . '),';
 
375
            $params['campaignId_' . $i] = $this->campaignId;
 
376
            $params['layoutId_' . $i] = $layout->layoutId;
 
377
            $params['displayOrder_' . $i] = $layout->displayOrder;
 
378
        }
 
379
 
 
380
        $sql = rtrim($sql, ',');
 
381
 
 
382
        $this->getStore()->update($sql, $params);
359
383
    }
360
384
 
361
385
    /**
363
387
     */
364
388
    private function unlinkLayouts()
365
389
    {
366
 
        // Unlink any layouts that are NOT in the collection
367
 
        $params = ['campaignId' => $this->campaignId];
368
 
 
369
 
        if (count($this->layouts) <= 0) {
370
 
            $sql = ' DELETE FROM `lkcampaignlayout` WHERE campaignId = :campaignId ';
371
 
        }
372
 
        else {
373
 
 
374
 
            $sql = '
375
 
                  SELECT lkCampaignLayoutId
376
 
                    FROM `lkcampaignlayout`
377
 
                   WHERE campaignId = :campaignId AND (
378
 
            ';
379
 
 
380
 
            $i = 0;
381
 
            foreach ($this->layouts as $layout) {
382
 
                $i++;
383
 
 
384
 
                if ($i > 1)
385
 
                    $sql .= ' OR ';
386
 
 
387
 
                $sql .= ' (layoutId = :layoutId' . $i . ' AND displayOrder = :displayOrder' . $i . ') ';
388
 
                $params['layoutId' . $i] = $layout->layoutId;
389
 
                $params['displayOrder' . $i] = $layout->displayOrder;
390
 
            }
391
 
 
392
 
            $sql .= ')';
393
 
 
394
 
            // Get the lkid's for the delete
395
 
 
396
 
            $ids = $this->getStore()->select($sql, $params);
397
 
 
398
 
            $ids = array_map(function ($element) {
399
 
                return $element['lkCampaignLayoutId'];
400
 
            }, $ids);
401
 
 
402
 
            if (count($ids) <= 0)
403
 
                $ids[] = 0;
404
 
 
405
 
            $sql = '
406
 
              DELETE FROM `lkcampaignlayout`
407
 
               WHERE campaignId = :campaignId
408
 
                AND lkCampaignLayoutId NOT IN (' . implode(',', $ids) . ') ';
409
 
 
410
 
            // Reset params to just be the campaign id
411
 
            $params = ['campaignId' => $this->campaignId];
412
 
        }
413
 
 
414
 
        $this->getStore()->update($sql, $params);
 
390
        // Delete all the links
 
391
        $this->getStore()->update('DELETE FROM `lkcampaignlayout` WHERE campaignId = :campaignId', ['campaignId' => $this->campaignId]);
415
392
    }
416
393
 
417
394
    /**