Skip to content

Commit

Permalink
feat: Add feature to enable campaign access based on library
Browse files Browse the repository at this point in the history
  • Loading branch information
ammopt authored and AlexanderBlanchardAC committed Nov 13, 2024
1 parent 440edbd commit d5f7ced
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
53 changes: 53 additions & 0 deletions code/web/sys/Community/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -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 [
Expand Down Expand Up @@ -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,
],
];
}

Expand All @@ -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();
Expand All @@ -129,13 +154,34 @@ 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();
$link->campaignId = $this->id;
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 = [];
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -232,6 +282,7 @@ public function update($context = '') {
$ret = parent::update();
if ($ret !== FALSE) {
$this->savePatronTypeAccess();
$this->saveLibraryAccess();
$this->saveMilestones();
}
return $ret;
Expand All @@ -246,6 +297,7 @@ public function insert($context = '') {
$ret = parent::insert();
if ($ret !== FALSE) {
$this->savePatronTypeAccess();
$this->saveLibraryAccess();
$this->saveMilestones();
}
return $ret;
Expand All @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions code/web/sys/Community/CampaignLibraryAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php


class CampaignLibraryAccess extends DataObject {
public $__table = 'ce_campaign_library_access';
public $id;
public $campaignId;
public $libraryId;
}
11 changes: 11 additions & 0 deletions code/web/sys/DBMaintenance/community_engagement_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ function getCommunityEngagementUpdates() {
) ENGINE = InnoDB",
],
],
'create_campaign_library_access' => [
'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',
Expand Down

0 comments on commit d5f7ced

Please sign in to comment.