From d5f7cedaacf16a86729a3990736082930b871f01 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 13 Nov 2024 10:50:01 -0100 Subject: [PATCH] feat: Add feature to enable campaign access based on library --- code/web/sys/Community/Campaign.php | 53 +++++++++++++++++++ .../sys/Community/CampaignLibraryAccess.php | 9 ++++ .../community_engagement_updates.php | 11 ++++ 3 files changed, 73 insertions(+) create mode 100644 code/web/sys/Community/CampaignLibraryAccess.php diff --git a/code/web/sys/Community/Campaign.php b/code/web/sys/Community/Campaign.php index 2db2c687ab..c4f8012d39 100644 --- a/code/web/sys/Community/Campaign.php +++ b/code/web/sys/Community/Campaign.php @@ -4,6 +4,7 @@ require_once ROOT_DIR . '/sys/Community/UserCampaign.php'; require_once ROOT_DIR . '/sys/Community/Reward.php'; require_once ROOT_DIR . '/sys/Community/CampaignPatronTypeAccess.php'; +require_once ROOT_DIR . '/sys/Community/CampaignLibraryAccess.php'; require_once ROOT_DIR . '/sys/Account/User.php'; @@ -24,12 +25,14 @@ class Campaign extends DataObject { private $_availableMilestones; protected $_allowPatronTypeAccess; + protected $_allowLibraryAccess; public static function getObjectStructure($context = ''): array { $milestoneList = Milestone::getMilestoneList(); $milestoneStructure = CampaignMilestone::getObjectStructure($context); unset($milestoneStructure['campaignId']); + $libraryList = Library::getLibraryList(false); $patronTypeList = PType::getPatronTypeList(); $rewardList = Reward::getRewardList(); return [ @@ -99,6 +102,15 @@ public static function getObjectStructure($context = ''): array { 'values' => $patronTypeList, 'hideInLists' => false, ], + 'allowLibraryAccess' => [ + 'property' => 'allowLibraryAccess', + 'type' => 'multiSelect', + 'listStyle' => 'checkboxSimple', + 'label' => 'Library Access', + 'description' => 'Define what libraries should have access to this campaign', + 'values' => $libraryList, + 'hideInLists' => false, + ], ]; } @@ -115,6 +127,19 @@ public function getPatronTypeAccess() { return $this->_allowPatronTypeAccess; } + public function getLibraryAccess(): ?array { + if (!isset($this->_allowLibraryAccess) && $this->id) { + $this->_allowLibraryAccess = []; + $libraryLink = new CampaignLibraryAccess(); + $libraryLink->campaignId = $this->id; + $libraryLink->find(); + while ($libraryLink->fetch()) { + $this->_allowLibraryAccess[$libraryLink->libraryId] = $libraryLink->libraryId; + } + } + return $this->_allowLibraryAccess; + } + public function savePatronTypeAccess() { if (isset($this->_allowPatronTypeAccess) && is_array($this->_allowPatronTypeAccess)) { $this->clearPatronTypeAccess(); @@ -129,6 +154,20 @@ public function savePatronTypeAccess() { } } + public function saveLibraryAccess() { + if (isset($this->_allowLibraryAccess) && is_array($this->_allowLibraryAccess)) { + $this->clearLibraryAccess(); + + foreach ($this->_allowLibraryAccess as $libraryId) { + $libraryLink = new CampaignLibraryAccess(); + $libraryLink->campaignId = $this->id; + $libraryLink->libraryId = $libraryId; + $libraryLink->insert(); + } + unset($this->_allowLibraryAccess); + } + } + private function clearPatronTypeAccess() { //Delete links to the patron types $link = new CampaignPatronTypeAccess(); @@ -136,6 +175,13 @@ private function clearPatronTypeAccess() { return $link->delete(true); } + private function clearLibraryAccess() { + //Delete links to the libraries + $libraryLink = new CampaignLibraryAccess(); + $libraryLink->campaignId = $this->id; + return $libraryLink->delete(true); + } + public function getUsers() { if (is_null($this->_users)) { $this->_users = []; @@ -180,6 +226,8 @@ public function getUsersForCampaign() { public function __get($name) { if ($name == 'allowPatronTypeAccess') { return $this->getPatronTypeAccess(); + } else if ($name == 'allowLibraryAccess') { + return $this->getLibraryAccess(); } else if ($name == 'availableMilestones') { return $this->getMilestones(); } else { @@ -190,6 +238,8 @@ public function __get($name) { public function __set($name, $value) { if ($name == 'allowPatronTypeAccess') { $this->_allowPatronTypeAccess = $value; + } else if ($name == 'allowLibraryAccess') { + $this->_allowLibraryAccess = $value; } else if ($name == 'availableMilestones') { $this->_availableMilestones = $value; } else { @@ -232,6 +282,7 @@ public function update($context = '') { $ret = parent::update(); if ($ret !== FALSE) { $this->savePatronTypeAccess(); + $this->saveLibraryAccess(); $this->saveMilestones(); } return $ret; @@ -246,6 +297,7 @@ public function insert($context = '') { $ret = parent::insert(); if ($ret !== FALSE) { $this->savePatronTypeAccess(); + $this->saveLibraryAccess(); $this->saveMilestones(); } return $ret; @@ -255,6 +307,7 @@ public function delete($useWhere = false) : int { $ret = parent::delete($useWhere); if ($ret && !empty($this->id)) { $this->clearPatronTypeAccess(); + $this->clearLibraryAccess(); } return $ret; } diff --git a/code/web/sys/Community/CampaignLibraryAccess.php b/code/web/sys/Community/CampaignLibraryAccess.php new file mode 100644 index 0000000000..02ffe916a5 --- /dev/null +++ b/code/web/sys/Community/CampaignLibraryAccess.php @@ -0,0 +1,9 @@ + [ + 'title' => 'Create Campaign Library Access', + 'description' => 'Add table for library campaign access', + 'sql' => [ + "CREATE TABLE ce_campaign_library_access ( + id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + campaignId INT NOT NULL, + libraryId INT NOT NULL + ) ENGINE = InnoDB", + ], + ], 'create_milestones' => [ 'title' => 'Create Milestones', 'description' => 'Add table for milestones',