芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/lib/Entity/Action.php
. */ namespace Xibo\Entity; use Carbon\Carbon; use Xibo\Factory\PermissionFactory; use Xibo\Helper\DateFormatHelper; use Xibo\Service\LogServiceInterface; use Xibo\Storage\StorageServiceInterface; use Xibo\Support\Exception\InvalidArgumentException; /** * Class Action * @package Xibo\Entity * * @SWG\Definition() */ class Action implements \JsonSerializable { use EntityTrait; /** * @SWG\Property(description="The Action Id") * @var int */ public $actionId; /** * @SWG\Property(description="The Owner Id") * @var int */ public $ownerId; /** * @SWG\Property(description="The Action trigger type") * @var string */ public $triggerType; /** * @SWG\Property(description="The Action trigger code") * @var string */ public $triggerCode; /** * @SWG\Property(description="The Action type") * @var string */ public $actionType; /** * @SWG\Property(description="The Action source (layout, region or widget)") * @var string */ public $source; /** * @SWG\Property(description="The Action source Id (layoutId, regionId or widgetId)") * @var int */ public $sourceId; /** * @SWG\Property(description="The Action target (region)") * @var string */ public $target; /** * @SWG\Property(description="The Action target Id (regionId)") * @var int */ public $targetId; /** * @SWG\Property(description="Widget ID that will be loaded as a result of navigate to Widget Action type") * @var int */ public $widgetId; /** * @SWG\Property(description="Layout Code identifier") * @var string */ public $layoutCode; /** @var \Xibo\Factory\PermissionFactory */ private $permissionFactory; /** * Entity constructor. * @param StorageServiceInterface $store * @param LogServiceInterface $log * @param PermissionFactory $permissionFactory */ public function __construct($store, $log, $permissionFactory) { $this->setCommonDependencies($store, $log); $this->permissionFactory = $permissionFactory; } public function __clone() { $this->hash = null; $this->actionId = null; } /** * Get the Id * @return int */ public function getId() { return $this->actionId; } /** * Get the OwnerId * @return int */ public function getOwnerId() { return $this->ownerId; } /** * Sets the Owner * @param int $ownerId */ public function setOwner($ownerId) { $this->ownerId = $ownerId; } /** * @return string */ public function __toString() { return sprintf('ActionId %d, Trigger Type %s, Trigger Code %s, Action Type %s, Source %s, SourceId %s, Target %s, TargetId %d', $this->actionId, $this->triggerType, $this->triggerCode, $this->actionType, $this->source, $this->sourceId, $this->target, $this->targetId); } /** * @throws InvalidArgumentException */ public function validate() { if ($this->target == 'region' && $this->targetId == null) { throw new InvalidArgumentException(__('Please select a Region'), 'targetId'); } if ($this->triggerType === 'webhook' && $this->triggerCode === null) { throw new InvalidArgumentException(__('Please provide trigger code'), 'triggerCode'); } if (!in_array($this->triggerType, ['touch', 'webhook'])) { throw new InvalidArgumentException(__('Invalid trigger type'), 'triggerType'); } if (!in_array($this->actionType, ['next', 'previous', 'navLayout', 'navWidget'])) { throw new InvalidArgumentException(__('Invalid action type'), 'actionType'); } if (!in_array(strtolower($this->source), ['layout', 'region', 'widget'])) { throw new InvalidArgumentException(__('Invalid source'), 'source'); } if (!in_array(strtolower($this->target), ['region', 'screen'])) { throw new InvalidArgumentException(__('Invalid target'), 'target'); } if ($this->actionType === 'navLayout' && $this->layoutCode == '') { throw new InvalidArgumentException(__('Please enter Layout code'), 'layoutCode'); } if ($this->actionType === 'navWidget' && $this->widgetId == null) { throw new InvalidArgumentException(__('Please select a Widget'), 'widgetId'); } } /** * @param array $options * @throws InvalidArgumentException */ public function save($options = []) { $options = array_merge([ 'validate' => true, 'notifyLayout' => false, 'layoutId' => null ], $options); $this->getLog()->debug('Saving ' . $this); if ($options['validate']) { $this->validate(); } if ($this->actionId == null || $this->actionId == 0) { $this->add(); $this->loaded = true; } else { $this->update(); } if ($options['notifyLayout'] && $options['layoutId'] != null) { $this->notifyLayout($options['layoutId']); } } public function add() { $this->actionId = $this->getStore()->insert('INSERT INTO `action` (ownerId, triggerType, triggerCode, actionType, source, sourceId, target, targetId, widgetId, layoutCode) VALUES (:ownerId, :triggerType, :triggerCode, :actionType, :source, :sourceId, :target, :targetId, :widgetId, :layoutCode)', [ 'ownerId' => $this->ownerId, 'triggerType' => $this->triggerType, 'triggerCode' => $this->triggerCode, 'actionType' => $this->actionType, 'source' => $this->source, 'sourceId' => $this->sourceId, 'target' => $this->target, 'targetId' => $this->targetId, 'widgetId' => $this->widgetId, 'layoutCode' => $this->layoutCode ]); } public function update() { $this->getStore()->update('UPDATE `action` SET ownerId = :ownerId, triggerType = :triggerType, triggerCode = :triggerCode, actionType = :actionType, source = :source, sourceId = :sourceId, target = :target, targetId = :targetId, widgetId = :widgetId, layoutCode = :layoutCode WHERE actionId = :actionId', [ 'ownerId' => $this->ownerId, 'triggerType' => $this->triggerType, 'triggerCode' => $this->triggerCode, 'actionType' => $this->actionType, 'source' => $this->source, 'sourceId' => $this->sourceId, 'target' => $this->target, 'targetId' => $this->targetId, 'actionId' => $this->actionId, 'widgetId' => $this->widgetId, 'layoutCode' => $this->layoutCode ]); } public function delete() { $this->getStore()->update('DELETE FROM `action` WHERE actionId = :actionId', ['actionId' => $this->actionId]); } /** * Notify the Layout (set to building) * @param $layoutId */ public function notifyLayout($layoutId) { $this->getLog()->debug(sprintf('Saving Interactive Action ID %d triggered layout ID %d build', $this->actionId, $layoutId)); $this->getStore()->update(' UPDATE `layout` SET `status` = 3, `modifiedDT` = :modifiedDt WHERE layoutId = :layoutId ', [ 'layoutId' => $layoutId, 'modifiedDt' => Carbon::now()->format(DateFormatHelper::getSystemFormat()) ]); } }