~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Entity/Layout.php

  • Committer: Dan Garner
  • Date: 2016-06-28 15:02:11 UTC
  • mto: This revision was merged to the branch mainline in revision 528.
  • Revision ID: git-v1:51031805c36c1d366fa330b2c2320d1927c57003
Fixes for upgrade steps

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
namespace Xibo\Entity;
22
22
 
23
 
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
 
use Xibo\Event\LayoutBuildEvent;
25
 
use Xibo\Event\LayoutBuildRegionEvent;
26
 
use Xibo\Exception\DuplicateEntityException;
27
 
use Xibo\Exception\InvalidArgumentException;
28
23
use Xibo\Exception\NotFoundException;
29
24
use Xibo\Factory\CampaignFactory;
30
25
use Xibo\Factory\DataSetFactory;
192
187
    public $statusMessage;
193
188
 
194
189
    // Child items
195
 
    /** @var Region[]  */
196
190
    public $regions = [];
197
 
 
198
191
    public $tags = [];
199
192
    public $permissions = [];
200
193
    public $campaigns = [];
219
212
        'saveTags' => false,
220
213
        'setBuildRequired' => true,
221
214
        'validate' => false,
222
 
        'audit' => false,
223
 
        'notify' => false
 
215
        'audit' => false
224
216
    ];
225
217
 
226
218
    /**
233
225
     */
234
226
    private $date;
235
227
 
236
 
    /** @var  EventDispatcherInterface */
237
 
    private $dispatcher;
238
 
 
239
228
    /**
240
229
     * @var PermissionFactory
241
230
     */
277
266
     * @param LogServiceInterface $log
278
267
     * @param ConfigServiceInterface $config
279
268
     * @param DateServiceInterface $date
280
 
     * @param EventDispatcherInterface $eventDispatcher
281
269
     * @param PermissionFactory $permissionFactory
282
270
     * @param RegionFactory $regionFactory
283
271
     * @param TagFactory $tagFactory
286
274
     * @param MediaFactory $mediaFactory
287
275
     * @param ModuleFactory $moduleFactory
288
276
     */
289
 
    public function __construct($store, $log, $config, $date, $eventDispatcher, $permissionFactory, $regionFactory, $tagFactory, $campaignFactory, $layoutFactory, $mediaFactory, $moduleFactory)
 
277
    public function __construct($store, $log, $config, $date, $permissionFactory, $regionFactory, $tagFactory, $campaignFactory, $layoutFactory, $mediaFactory, $moduleFactory)
290
278
    {
291
279
        $this->setCommonDependencies($store, $log);
292
280
        $this->setPermissionsClass('Xibo\Entity\Campaign');
293
281
        $this->config = $config;
294
282
        $this->date = $date;
295
 
        $this->dispatcher = $eventDispatcher;
296
283
        $this->permissionFactory = $permissionFactory;
297
284
        $this->regionFactory = $regionFactory;
298
285
        $this->tagFactory = $tagFactory;
367
354
    /**
368
355
     * Set the status of this layout to indicate a build is required
369
356
     */
370
 
    private function setBuildRequired()
 
357
    public function setBuildRequired()
371
358
    {
372
359
        $this->status = 3;
373
360
    }
391
378
 
392
379
    /**
393
380
     * Get Widgets assigned to this Layout
394
 
     * @return Widget[]
 
381
     * @return array[Widget]
395
382
     */
396
383
    public function getWidgets()
397
384
    {
483
470
        $this->hash = $this->hash();
484
471
        $this->loaded = true;
485
472
 
486
 
        $this->getLog()->debug('Loaded %s', $this->layoutId);
 
473
        $this->getLog()->debug('Loaded %s' . $this->layoutId);
487
474
    }
488
475
 
489
476
    /**
525
512
        // New or existing layout
526
513
        if ($this->layoutId == null || $this->layoutId == 0) {
527
514
            $this->add();
528
 
        } else if (($this->hash() != $this->hash && $options['saveLayout']) || $options['setBuildRequired']) {
 
515
        } else if ($this->hash() != $this->hash && $options['saveLayout']) {
529
516
            $this->update($options);
530
517
        }
531
518
 
572
559
        $this->getLog()->debug('Save finished for %s', $this);
573
560
 
574
561
        if ($options['audit'])
575
 
            $this->getLog()->audit('Layout', $this->layoutId, 'Saved', $this->getChangedProperties());
 
562
            $this->getLog()->audit('Layout', $this->layoutId, 'Saved', $this);
576
563
    }
577
564
 
578
565
    /**
592
579
 
593
580
        $this->getLog()->debug('Deleting %s', $this);
594
581
 
595
 
        // We cannot delete the default default
596
 
        if ($this->layoutId == $this->config->GetSetting('DEFAULT_LAYOUT'))
597
 
            throw new InvalidArgumentException(__('This layout is used as the global default and cannot be deleted'), 'layoutId');
598
 
 
599
582
        // Delete Permissions
600
583
        foreach ($this->permissions as $permission) {
601
584
            /* @var Permission $permission */
629
612
        $campaign->delete();
630
613
 
631
614
        // Remove the Layout from any display defaults
632
 
        $this->getStore()->update('UPDATE `display` SET defaultlayoutid = :defaultLayoutId WHERE defaultlayoutid = :layoutId', [
633
 
            'layoutId' => $this->layoutId,
634
 
            'defaultLayoutId' => $this->config->GetSetting('DEFAULT_LAYOUT')
635
 
        ]);
 
615
        $this->getStore()->update('UPDATE `display` SET defaultlayoutid = 4 WHERE defaultlayoutid = :layoutId', array('layoutId' => $this->layoutId));
636
616
 
637
617
        // Remove the Layout (now it is orphaned it can be deleted safely)
638
618
        $this->getStore()->update('DELETE FROM `layout` WHERE layoutid = :layoutId', array('layoutId' => $this->layoutId));
650
630
    {
651
631
        // We must provide either a template or a resolution
652
632
        if ($this->width == 0 || $this->height == 0)
653
 
            throw new InvalidArgumentException(__('The layout dimensions cannot be empty'), 'width/height');
 
633
            throw new \InvalidArgumentException(__('The layout dimensions cannot be empty'));
654
634
 
655
635
        // Validation
656
636
        if (strlen($this->layout) > 50 || strlen($this->layout) < 1)
657
 
            throw new InvalidArgumentException(__("Layout Name must be between 1 and 50 characters"), 'name');
 
637
            throw new \InvalidArgumentException(__("Layout Name must be between 1 and 50 characters"));
658
638
 
659
639
        if (strlen($this->description) > 254)
660
 
            throw new InvalidArgumentException(__("Description can not be longer than 254 characters"), 'description');
 
640
            throw new \InvalidArgumentException(__("Description can not be longer than 254 characters"));
661
641
 
662
642
        // Check for duplicates
663
643
        $duplicates = $this->layoutFactory->query(null, array('userId' => $this->ownerId, 'layoutExact' => $this->layout, 'notLayoutId' => $this->layoutId));
664
644
 
665
645
        if (count($duplicates) > 0)
666
 
            throw new DuplicateEntityException(sprintf(__("You already own a layout called '%s'. Please choose another name."), $this->layout));
 
646
            throw new \InvalidArgumentException(sprintf(__("You already own a layout called '%s'. Please choose another name."), $this->layout));
667
647
    }
668
648
 
669
649
    /**
753
733
        $layoutNode->setAttribute('height', $this->height);
754
734
        $layoutNode->setAttribute('bgcolor', $this->backgroundColor);
755
735
        $layoutNode->setAttribute('schemaVersion', $this->schemaVersion);
756
 
 
757
 
        // Only set the z-index if present
758
 
        if ($this->backgroundzIndex != 0)
759
 
            $layoutNode->setAttribute('zindex', $this->backgroundzIndex);
 
736
        $layoutNode->setAttribute('zindex', $this->backgroundzIndex);
760
737
 
761
738
        if ($this->backgroundImageId != 0) {
762
739
            // Get stored as
825
802
            $regionNode->setAttribute('height', $region->height);
826
803
            $regionNode->setAttribute('top', $region->top);
827
804
            $regionNode->setAttribute('left', $region->left);
828
 
 
829
 
            // Only set the zIndex if present
830
 
            if ($region->zIndex != 0)
831
 
                $regionNode->setAttribute('zindex', $region->zIndex);
 
805
            $regionNode->setAttribute('zindex', $region->zIndex);
832
806
 
833
807
            $layoutNode->appendChild($regionNode);
834
808
 
835
809
            // Region Duration
836
810
            $region->duration = 0;
837
811
 
838
 
            // Region Options
839
 
            $regionOptionsNode = $document->createElement('options');
840
 
 
841
 
            foreach ($region->regionOptions as $regionOption) {
842
 
                $regionOptionNode = $document->createElement($regionOption->option, $regionOption->value);
843
 
                $regionOptionsNode->appendChild($regionOptionNode);
844
 
            }
845
 
 
846
 
            $regionNode->appendChild($regionOptionsNode);
847
 
 
848
 
            // Store region look to work out duration calc
 
812
            // Region Loop
849
813
            $regionLoop = $region->getOptionValue('loop', 0);
850
814
 
851
815
            // Get a count of widgets in this region
923
887
                    // Inject the URI
924
888
                    $uriInjected = false;
925
889
                    if ($module->getModule()->regionSpecific == 0) {
926
 
                        $media = $this->mediaFactory->getById($widget->getPrimaryMediaId());
 
890
                        $media = $this->mediaFactory->getById($widget->mediaIds[0]);
927
891
                        $optionNode = $document->createElement('uri', $media->storedAs);
928
892
                        $optionsNode->appendChild($optionNode);
929
893
                        $uriInjected = true;
989
953
            if ($this->duration < $region->duration)
990
954
                $this->duration = $region->duration;
991
955
 
992
 
            $event = new LayoutBuildRegionEvent($region->regionId, $regionNode);
993
 
            $this->dispatcher->dispatch($event::NAME, $event);
994
956
            // End of region loop.
995
957
        }
996
958
 
1012
974
 
1013
975
        $this->status = ($status < $this->status) ? $status : $this->status;
1014
976
 
1015
 
        // Fire a layout.build event, passing the layout and the generated document.
1016
 
        $event = new LayoutBuildEvent($this, $document);
1017
 
        $this->dispatcher->dispatch($event::NAME, $event);
1018
 
 
1019
977
        return $document->saveXML();
1020
978
    }
1021
979
 
1031
989
            'includeData' => false
1032
990
        ], $options);
1033
991
 
1034
 
        // Load the complete layout
1035
 
        $this->load();
1036
 
 
1037
992
        // We export to a ZIP file
1038
993
        $zip = new \ZipArchive();
1039
994
        $result = $zip->open($fileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
1040
995
        if ($result !== true)
1041
996
            throw new \InvalidArgumentException(__('Can\'t create ZIP. Error Code: ' . $result));
1042
997
 
1043
 
        // Add a mapping file for the region names
1044
 
        $regionMapping = [];
1045
 
        foreach ($this->regions as $region) {
1046
 
            /** @var Region $region */
1047
 
            $regionMapping[$region->regionId] = $region->name;
1048
 
        }
1049
 
 
1050
998
        // Add layout information to the ZIP
1051
999
        $zip->addFromString('layout.json', json_encode([
1052
1000
            'layout' => $this->layout,
1053
 
            'description' => $this->description,
1054
 
            'regions' => $regionMapping
 
1001
            'description' => $this->description
1055
1002
        ]));
1056
1003
 
1057
1004
        // Add the layout XLF
1188
1135
            $this->duration = 0;
1189
1136
 
1190
1137
            // Save the resulting XLF
1191
 
            try {
1192
 
                file_put_contents($path, $this->toXlf());
1193
 
            } catch (\Exception $e) {
1194
 
                $this->getLog()->error('Cannot build Layout. Unexpected error: ' . $e->getMessage());
1195
 
 
1196
 
                // Will continue and save the status as 4
1197
 
                $this->status = 4;
1198
 
                $this->statusMessage = 'Unexpected Error';
1199
 
            }
 
1138
            file_put_contents($path, $this->toXlf());
1200
1139
 
1201
1140
            $this->save([
1202
1141
                'saveRegions' => true,
1204
1143
                'manageRegionAssignments' => false,
1205
1144
                'saveTags' => false,
1206
1145
                'setBuildRequired' => false,
1207
 
                'audit' => false,
1208
 
                'validate' => false,
1209
1146
                'notify' => $options['notify']
1210
1147
            ]);
1211
1148
        }