first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/IndividualSubscription.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class IndividualSubscription
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see IndividualSubscriptionDAO
|
||||
*
|
||||
* @brief Basic class describing an individual (non-institutional) subscription.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use PKP\db\DAORegistry;
|
||||
|
||||
class IndividualSubscription extends Subscription
|
||||
{
|
||||
/**
|
||||
* Check whether subscription is valid
|
||||
*
|
||||
* @param null|mixed $checkDate
|
||||
*/
|
||||
public function isValid($check = self::SUBSCRIPTION_DATE_BOTH, $checkDate = null)
|
||||
{
|
||||
$subscriptionDao = DAORegistry::getDAO('IndividualSubscriptionDAO'); /** @var IndividualSubscriptionDAO $subscriptionDao */
|
||||
return $subscriptionDao->isValidIndividualSubscription($this->getData('userId'), $this->getData('journalId'), $check, $checkDate);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\IndividualSubscription', '\IndividualSubscription');
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/IndividualSubscriptionDAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class IndividualSubscriptionDAO
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see IndividualSubscription
|
||||
*
|
||||
* @brief Operations for retrieving and modifying IndividualSubscription objects.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use PKP\core\Core;
|
||||
use PKP\db\DAOResultFactory;
|
||||
use PKP\db\DBResultRange;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
class IndividualSubscriptionDAO extends SubscriptionDAO
|
||||
{
|
||||
/**
|
||||
* Retrieve an individual subscription by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId Subscription ID
|
||||
* @param int $journalId Optional journal ID
|
||||
*
|
||||
* @return IndividualSubscription
|
||||
*/
|
||||
public function getById($subscriptionId, $journalId = null)
|
||||
{
|
||||
$params = [(int) $subscriptionId];
|
||||
if ($journalId) {
|
||||
$params[] = (int) $journalId;
|
||||
}
|
||||
$result = $this->retrieve(
|
||||
'SELECT s.*
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
WHERE st.institutional = 0
|
||||
AND s.subscription_id = ?
|
||||
' . ($journalId ? ' AND s.journal_id = ?' : ''),
|
||||
$params
|
||||
);
|
||||
$row = (array) $result->current();
|
||||
return $row ? $this->_fromRow($row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve individual subscription by user ID for journal.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return IndividualSubscription
|
||||
*/
|
||||
public function getByUserIdForJournal($userId, $journalId)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*
|
||||
FROM
|
||||
subscriptions s,
|
||||
subscription_types st
|
||||
WHERE s.type_id = st.type_id
|
||||
AND st.institutional = 0
|
||||
AND s.user_id = ?
|
||||
AND s.journal_id = ?',
|
||||
[(int) $userId, (int) $journalId]
|
||||
);
|
||||
$row = (array) $result->current();
|
||||
return $row ? $this->_fromRow($row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve individual subscriptions by user ID.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<IndividualSubscription> Object containing IndividualSubscriptions
|
||||
*/
|
||||
public function getByUserId($userId, $rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*
|
||||
FROM
|
||||
subscriptions s,
|
||||
subscription_types st
|
||||
WHERE s.type_id = st.type_id
|
||||
AND st.institutional = 0
|
||||
AND s.user_id = ?',
|
||||
[(int) $userId],
|
||||
$rangeInfo
|
||||
);
|
||||
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of individual subscriptions with given status for journal.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param int $status
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCount($journalId, $status = null)
|
||||
{
|
||||
$params = [(int) $journalId];
|
||||
if ($status !== null) {
|
||||
$params[] = (int) $status;
|
||||
}
|
||||
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM subscriptions s,
|
||||
subscription_types st
|
||||
WHERE s.type_id = st.type_id AND
|
||||
st.institutional = 0 AND
|
||||
s.journal_id = ?
|
||||
' . ($status !== null ? ' AND s.status = ?' : ''),
|
||||
$params
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? $row->row_count : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of individual subscriptions for a particular journal.
|
||||
*
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSubscribedUserCount($journalId)
|
||||
{
|
||||
return $this->getStatusCount($journalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an individual subscription exists for a given subscriptionId.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionExists($subscriptionId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM
|
||||
subscriptions s,
|
||||
subscription_types st
|
||||
WHERE s.type_id = st.type_id
|
||||
AND st.institutional = 0
|
||||
AND s.subscription_id = ?',
|
||||
[(int) $subscriptionId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an individual subscription exists for a given user.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
* @param int $userId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionExistsByUser($subscriptionId, $userId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM
|
||||
subscriptions s,
|
||||
subscription_types st
|
||||
WHERE s.type_id = st.type_id
|
||||
AND st.institutional = 0
|
||||
AND s.subscription_id = ?
|
||||
AND s.user_id = ?',
|
||||
[(int) $subscriptionId, (int) $userId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an individual subscription exists for a given user and journal.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionExistsByUserForJournal($userId, $journalId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM
|
||||
subscriptions s,
|
||||
subscription_types st
|
||||
WHERE s.type_id = st.type_id
|
||||
AND st.institutional = 0
|
||||
AND s.user_id = ?
|
||||
AND s.journal_id = ?',
|
||||
[(int) $userId, (int) $journalId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generator function to create object.
|
||||
*
|
||||
* @return IndividualSubscription
|
||||
*/
|
||||
public function newDataObject()
|
||||
{
|
||||
return new IndividualSubscription();
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to return an IndividualSubscription object from a row.
|
||||
*
|
||||
* @param array $row
|
||||
*
|
||||
* @return IndividualSubscription
|
||||
*/
|
||||
public function _fromRow($row)
|
||||
{
|
||||
$individualSubscription = parent::_fromRow($row);
|
||||
Hook::call('IndividualSubscriptionDAO::_fromRow', [&$individualSubscription, &$row]);
|
||||
|
||||
return $individualSubscription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new individual subscription.
|
||||
*
|
||||
* @param IndividualSubscription $individualSubscription
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function insertObject($individualSubscription)
|
||||
{
|
||||
return $this->_insertObject($individualSubscription);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing individual subscription.
|
||||
*
|
||||
* @param IndividualSubscription $individualSubscription
|
||||
*/
|
||||
public function updateObject($individualSubscription)
|
||||
{
|
||||
$this->_updateObject($individualSubscription);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an individual subscription by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
* @param int $journalId
|
||||
*/
|
||||
public function deleteById($subscriptionId, $journalId = null)
|
||||
{
|
||||
$params = [(int) $subscriptionId];
|
||||
if ($journalId) {
|
||||
$params[] = (int) $journalId;
|
||||
}
|
||||
$this->update('DELETE FROM subscriptions WHERE subscription_id = ?' . ($journalId ? ' AND journal_id = ?' : ''), $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete individual subscriptions by journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteByJournalId($journalId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT subscription_id FROM subscriptions WHERE journal_id = ?', [(int) $journalId]);
|
||||
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete individual subscriptions by user ID.
|
||||
*
|
||||
* @param int $userId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteByUserId($userId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT subscription_id FROM subscriptions WHERE user_id = ?', [(int) $userId]);
|
||||
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete individual subscription by user ID and journal ID.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteByUserIdForJournal($userId, $journalId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT subscription_id FROM subscriptions WHERE user_id = ? AND journal_id = ?', [(int) $userId, (int) $journalId]);
|
||||
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all individual subscriptions by subscription type ID.
|
||||
*
|
||||
* @param int $subscriptionTypeId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteByTypeId($subscriptionTypeId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT subscription_id FROM subscriptions WHERE type_id = ?', [(int) $subscriptionTypeId]);
|
||||
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all individual subscriptions.
|
||||
*
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<IndividualSubscription> Object containing IndividualSubscriptions
|
||||
*/
|
||||
public function getAll($rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*
|
||||
FROM
|
||||
subscriptions s,
|
||||
subscription_types st,
|
||||
users u
|
||||
WHERE s.type_id = st.type_id
|
||||
AND st.institutional = 0
|
||||
AND s.user_id = u.user_id
|
||||
ORDER BY u.user_id, s.subscription_id',
|
||||
[],
|
||||
$rangeInfo
|
||||
);
|
||||
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve individual subscriptions matching a particular journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param int $status
|
||||
* @param int $searchField
|
||||
* @param string $searchMatch "is" or "contains" or "startsWith"
|
||||
* @param string $search to look in $searchField for
|
||||
* @param int $dateField
|
||||
* @param string $dateFrom date to search from
|
||||
* @param string $dateTo date to search to
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<IndividualSubscription> Object containing matching IndividualSubscriptions
|
||||
*/
|
||||
public function getByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null)
|
||||
{
|
||||
$params = array_merge($this->getFetchParameters(), [(int) $journalId]);
|
||||
$result = $this->retrieveRange(
|
||||
$sql = 'SELECT s.*, ' . $this->getFetchColumns() . '
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
JOIN users u ON (s.user_id = u.user_id)
|
||||
' . $this->getFetchJoins() . '
|
||||
WHERE
|
||||
st.institutional = 0
|
||||
AND s.journal_id = ? ' .
|
||||
parent::_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params) . ' ' .
|
||||
'ORDER BY u.user_id, s.subscription_id',
|
||||
$params,
|
||||
$rangeInfo
|
||||
);
|
||||
return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted in subscription grid paging
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether user with ID has a valid individual subscription for a given journal.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $journalId
|
||||
* @param int $check Check using either start date, end date, or both (default)
|
||||
* @param string $checkDate (YYYY-MM-DD) Use this date instead of current date
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidIndividualSubscription($userId, $journalId, $check = Subscription::SUBSCRIPTION_DATE_BOTH, $checkDate = null)
|
||||
{
|
||||
if (empty($userId) || empty($journalId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$today = $this->dateToDB(Core::getCurrentDate());
|
||||
|
||||
if ($checkDate == null) {
|
||||
$checkDate = $today;
|
||||
} else {
|
||||
$checkDate = $this->dateToDB($checkDate);
|
||||
}
|
||||
|
||||
switch ($check) {
|
||||
case Subscription::SUBSCRIPTION_DATE_START:
|
||||
$dateSql = sprintf('%s >= s.date_start AND %s >= s.date_start', $checkDate, $today);
|
||||
break;
|
||||
case Subscription::SUBSCRIPTION_DATE_END:
|
||||
$dateSql = sprintf('%s <= s.date_end AND %s >= s.date_start', $checkDate, $today);
|
||||
break;
|
||||
default:
|
||||
$dateSql = sprintf('%s >= s.date_start AND %s <= s.date_end', $checkDate, $checkDate);
|
||||
}
|
||||
|
||||
$result = $this->retrieve(
|
||||
'
|
||||
SELECT s.subscription_id AS subscription_id
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
WHERE s.user_id = ?
|
||||
AND s.journal_id = ?
|
||||
AND s.status = ' . Subscription::SUBSCRIPTION_STATUS_ACTIVE . '
|
||||
AND st.institutional = 0
|
||||
AND (st.duration IS NULL OR (' . $dateSql . '))
|
||||
AND (st.format = ' . SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_ONLINE . '
|
||||
OR st.format = ' . SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE . ')',
|
||||
[(int) $userId, (int) $journalId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->subscription_id : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve active individual subscriptions matching a particular end date and journal ID.
|
||||
*
|
||||
* @param string $dateEnd (YYYY-MM-DD)
|
||||
* @param int $journalId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<IndividualSubscription> Object containing matching IndividualSubscriptions
|
||||
*/
|
||||
public function getByDateEnd($dateEnd, $journalId, $rangeInfo = null)
|
||||
{
|
||||
$dateEnd = explode('-', $dateEnd);
|
||||
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
JOIN users u ON (u.user_id = s.user_id)
|
||||
WHERE s.status = ' . Subscription::SUBSCRIPTION_STATUS_ACTIVE . '
|
||||
AND st.institutional = 0
|
||||
AND EXTRACT(YEAR FROM s.date_end) = ?
|
||||
AND EXTRACT(MONTH FROM s.date_end) = ?
|
||||
AND EXTRACT(DAY FROM s.date_end) = ?
|
||||
AND s.journal_id = ?
|
||||
ORDER BY u.user_id, s.subscription_id',
|
||||
[$dateEnd[0], $dateEnd[1], $dateEnd[2], (int) $journalId],
|
||||
$rangeInfo
|
||||
);
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Renew an individual subscription by dateEnd + duration of subscription type
|
||||
* if the individual subscription is expired, renew to current date + duration
|
||||
*
|
||||
* @param IndividualSubscription $individualSubscription
|
||||
*/
|
||||
public function renewSubscription($individualSubscription)
|
||||
{
|
||||
return $this->_renewSubscription($individualSubscription);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\IndividualSubscriptionDAO', '\IndividualSubscriptionDAO');
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @defgroup subscription Subscription
|
||||
* Implement subscriptions, subscription management, and subscription checking.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file classes/subscription/InstitutionalSubscription.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class InstitutionalSubscription
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see InstitutionalSubscriptionDAO
|
||||
*
|
||||
* @brief Basic class describing an institutional subscription.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use PKP\db\DAORegistry;
|
||||
|
||||
class InstitutionalSubscription extends Subscription
|
||||
{
|
||||
//
|
||||
// Get/set methods
|
||||
//
|
||||
|
||||
/**
|
||||
* Get the institution ID of the subscription.
|
||||
*/
|
||||
public function getInstitutionId(): int
|
||||
{
|
||||
return $this->getData('institutionId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the institution ID of the subscription.
|
||||
*/
|
||||
public function setInstitutionId(int $institutionId): void
|
||||
{
|
||||
$this->setData('institutionId', $institutionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mailing address of the institutionalSubscription.
|
||||
*/
|
||||
public function getInstitutionMailingAddress(): string
|
||||
{
|
||||
return $this->getData('mailingAddress');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the mailing address of the institutionalSubscription.
|
||||
*/
|
||||
public function setInstitutionMailingAddress(string $mailingAddress): void
|
||||
{
|
||||
$this->setData('mailingAddress', $mailingAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get institutionalSubscription domain string.
|
||||
*/
|
||||
public function getDomain(): string
|
||||
{
|
||||
return $this->getData('domain');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set institutionalSubscription domain string.
|
||||
*/
|
||||
public function setDomain(string $domain): void
|
||||
{
|
||||
$this->setData('domain', $domain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether subscription is valid
|
||||
*
|
||||
* @param ?int $check SUBSCRIPTION_DATE_... Test using either start date, end date, or both (default)
|
||||
* @param ?string $checkDate (YYYY-MM-DD) Use this date instead of current date
|
||||
*
|
||||
* @return int|false Found subscription ID, or false for none.
|
||||
*/
|
||||
public function isValid(string $domain, string $IP, ?int $check = self::SUBSCRIPTION_DATE_BOTH, ?string $checkDate = null)
|
||||
{
|
||||
$subscriptionDao = DAORegistry::getDAO('InstitutionalSubscriptionDAO'); /** @var InstitutionalSubscriptionDAO $subscriptionDao */
|
||||
return $subscriptionDao->isValidInstitutionalSubscription($domain, $IP, $this->getData('journalId'), $check, $checkDate);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\InstitutionalSubscription', '\InstitutionalSubscription');
|
||||
}
|
||||
@@ -0,0 +1,678 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/InstitutionalSubscriptionDAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class InstitutionalSubscriptionDAO
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see InstitutionalSubscription
|
||||
*
|
||||
* @brief Operations for retrieving and modifying InstitutionalSubscription objects.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use APP\core\Application;
|
||||
use PKP\core\Core;
|
||||
use PKP\db\DAOResultFactory;
|
||||
use PKP\db\DBResultRange;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
class InstitutionalSubscriptionDAO extends SubscriptionDAO
|
||||
{
|
||||
public const SUBSCRIPTION_INSTITUTION_NAME = 0x20;
|
||||
public const SUBSCRIPTION_DOMAIN = 0x21;
|
||||
public const SUBSCRIPTION_IP_RANGE = 0x22;
|
||||
|
||||
/**
|
||||
* Retrieve an institutional subscription by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId Subscription ID
|
||||
* @param int $journalId Journal ID
|
||||
*
|
||||
* @return InstitutionalSubscription
|
||||
*/
|
||||
public function getById($subscriptionId, $journalId = null)
|
||||
{
|
||||
$params = [(int) $subscriptionId];
|
||||
if ($journalId) {
|
||||
$params[] = (int) $journalId;
|
||||
}
|
||||
$result = $this->retrieve(
|
||||
'SELECT s.*, iss.*
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON s.type_id = st.type_id
|
||||
JOIN institutional_subscriptions iss ON s.subscription_id = iss.subscription_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.subscription_id = ?
|
||||
' . ($journalId ? ' AND s.journal_id = ?' : ''),
|
||||
$params
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? $this->_fromRow((array) $row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve institutional subscriptions by user ID.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<InstitutionalSubscription> Object containing matching InstitutionalSubscriptions
|
||||
*/
|
||||
public function getByUserId($userId, $rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*, iss.*
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON st.type_id = s.type_id
|
||||
JOIN institutional_subscriptions iss ON s.subscription_id = iss.subscription_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.user_id = ?',
|
||||
[(int) $userId],
|
||||
$rangeInfo
|
||||
);
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve institutional subscriptions by user ID and journal ID.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $journalId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<InstitutionalSubscription> Object containing matching InstitutionalSubscriptions
|
||||
*/
|
||||
public function getByUserIdForJournal($userId, $journalId, $rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*, iss.*
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON s.type_id = st.type_id
|
||||
JOIN institutional_subscriptions iss ON s.subscription_id = iss.subscription_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.user_id = ?
|
||||
AND s.journal_id = ?',
|
||||
[(int) $userId, (int) $journalId],
|
||||
$rangeInfo
|
||||
);
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of institutional subscriptions with given status for journal.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param null|mixed $status
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCount($journalId, $status = null)
|
||||
{
|
||||
$params = [(int) $journalId];
|
||||
if ($status !== null) {
|
||||
$params[] = (int) $status;
|
||||
}
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON s.type_id = st.type_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.journal_id = ?
|
||||
' . ($status !== null ? ' AND s.status = ?' : ''),
|
||||
$params
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? $row->row_count : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of institutional subscriptions for a particular journal.
|
||||
*
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSubscribedUserCount($journalId)
|
||||
{
|
||||
return $this->getStatusCount($journalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an institutional subscription exists for a given subscriptionId.
|
||||
*
|
||||
* @param int $subscriptionId Subscription ID
|
||||
* @param int $journalId Optional journal ID
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionExists($subscriptionId, $journalId = null)
|
||||
{
|
||||
$params = [(int) $subscriptionId];
|
||||
if ($journalId) {
|
||||
$params[] = (int) $journalId;
|
||||
}
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON s.type_id = st.type_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.subscription_id = ?
|
||||
' . ($journalId ? ' AND s.journal_id = ?' : ''),
|
||||
$params
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an institutional subscription exists for a given user.
|
||||
*
|
||||
* @param int $subscriptionId Subscription ID
|
||||
* @param int $userId User ID
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionExistsByUser($subscriptionId, $userId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON s.type_id = st.type_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.subscription_id = ?
|
||||
AND s.user_id = ?',
|
||||
[(int) $subscriptionId, (int) $userId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an institutional subscription exists for a given user and journal.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionExistsByUserForJournal($userId, $journalId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON s.type_id = st.type_id
|
||||
WHERE
|
||||
st.institutional = 1
|
||||
AND s.user_id = ?
|
||||
AND s.journal_id = ?',
|
||||
[(int) $userId, (int) $journalId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new institutional subscription.
|
||||
*
|
||||
* @param InstitutionalSubscription $institutionalSubscription
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function insertObject($institutionalSubscription)
|
||||
{
|
||||
$subscriptionId = null;
|
||||
if ($this->_insertObject($institutionalSubscription)) {
|
||||
$subscriptionId = $institutionalSubscription->getId();
|
||||
|
||||
$this->update(
|
||||
'INSERT INTO institutional_subscriptions
|
||||
(subscription_id, institution_id, mailing_address, domain)
|
||||
VALUES
|
||||
(?, ?, ?, ?)',
|
||||
[
|
||||
(int) $subscriptionId,
|
||||
(int) $institutionalSubscription->getInstitutionId(),
|
||||
$institutionalSubscription->getInstitutionMailingAddress(),
|
||||
$institutionalSubscription->getDomain()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return $subscriptionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing institutional subscription.
|
||||
*
|
||||
* @param InstitutionalSubscription $institutionalSubscription
|
||||
*/
|
||||
public function updateObject($institutionalSubscription)
|
||||
{
|
||||
$this->_updateObject($institutionalSubscription);
|
||||
|
||||
$this->update(
|
||||
'UPDATE institutional_subscriptions
|
||||
SET institution_id = ?,
|
||||
mailing_address = ?,
|
||||
domain = ?
|
||||
WHERE subscription_id = ?',
|
||||
[
|
||||
$institutionalSubscription->getInstitutionId(),
|
||||
$institutionalSubscription->getInstitutionMailingAddress(),
|
||||
$institutionalSubscription->getDomain(),
|
||||
(int) $institutionalSubscription->getId()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an institutional subscription by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
* @param null|mixed $journalId
|
||||
*/
|
||||
public function deleteById($subscriptionId, $journalId = null)
|
||||
{
|
||||
if (!$this->subscriptionExists($subscriptionId, $journalId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->update('DELETE FROM subscriptions WHERE subscription_id = ?', [(int) $subscriptionId]);
|
||||
$this->update('DELETE FROM institutional_subscriptions WHERE subscription_id = ?', [(int) $subscriptionId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete institutional subscriptions by journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
*/
|
||||
public function deleteByJournalId($journalId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT s.subscription_id AS subscription_id FROM subscriptions s WHERE s.journal_id = ?', [(int) $journalId]);
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete institutional subscriptions by user ID.
|
||||
*
|
||||
* @param int $userId
|
||||
*/
|
||||
public function deleteByUserId($userId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT s.subscription_id AS subscription_id FROM subscriptions s WHERE s.user_id = ?', [(int) $userId]);
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete institutional subscriptions by user ID and journal ID.
|
||||
*
|
||||
* @param int $userId User ID
|
||||
* @param int $journalId Journal ID
|
||||
*/
|
||||
public function deleteByUserIdForJournal($userId, $journalId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT s.subscription_id AS subscription_id FROM subscriptions s WHERE s.user_id = ? AND s.journal_id = ?', [(int) $userId, (int) $journalId]);
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all institutional subscriptions by subscription type ID.
|
||||
*
|
||||
* @param int $subscriptionTypeId Subscription type ID
|
||||
*/
|
||||
public function deleteByTypeId($subscriptionTypeId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT s.subscription_id AS subscription_id FROM subscriptions s WHERE s.type_id = ?', [(int) $subscriptionTypeId]);
|
||||
foreach ($result as $row) {
|
||||
$this->deleteById($row->subscription_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all institutional subscriptions.
|
||||
*
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<InstitutionalSubscription> Object containing InstitutionalSubscriptions
|
||||
*/
|
||||
public function getAll($rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*, iss.*
|
||||
' . $this->getInstitutionNameFetchColumns() . '
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
JOIN institutional_subscriptions iss ON (s.subscription_id = iss.subscription_id)
|
||||
' . $this->getInstitutionNameFetchJoins() . '
|
||||
WHERE st.institutional = 1
|
||||
ORDER BY institution_name ASC, s.subscription_id',
|
||||
$this->getInstitutionNameFetchParameters(),
|
||||
$rangeInfo
|
||||
);
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve institutional subscriptions matching a particular journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param int $status
|
||||
* @param int $searchField
|
||||
* @param string $searchMatch "is" or "contains" or "startsWith"
|
||||
* @param string $search to look in $searchField for
|
||||
* @param int $dateField
|
||||
* @param string $dateFrom date to search from
|
||||
* @param string $dateTo date to search to
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<InstitutionalSubscription> Object containing matching Subscriptions
|
||||
*/
|
||||
public function getByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null)
|
||||
{
|
||||
$params = array_merge($this->getInstitutionNameFetchParameters(), $this->getFetchParameters(), [(int) $journalId]);
|
||||
$institutionFetch = $ipRangeFetch = '';
|
||||
$searchSql = $this->_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params);
|
||||
|
||||
if (!empty($search)) {
|
||||
switch ($searchField) {
|
||||
case self::SUBSCRIPTION_INSTITUTION_NAME:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(insl.setting_value) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(insl.setting_value) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(insl) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$institutionFetch = 'JOIN institution_settings insl ON (insl.institution_id = iss.institution_id AND insl.setting_name = \'name\')';
|
||||
$params[] = $search;
|
||||
break;
|
||||
case self::SUBSCRIPTION_DOMAIN:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(iss.domain) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(iss.domain) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(iss.domain) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case self::SUBSCRIPTION_IP_RANGE:
|
||||
if ($searchMatch === 'inip') {
|
||||
$searchSql = ' AND LOWER(inip.ip_string) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(inip.ip_string) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(inip.ip_string) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$ipRangeFetch = ' JOIN institution_ip inip ON (inip.institution_id = iss.institution_id)';
|
||||
$params[] = $search;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->retrieveRange(
|
||||
$sql = 'SELECT DISTINCT s.*, iss.institution_id, iss.mailing_address, iss.domain,
|
||||
' . $this->getInstitutionNameFetchColumns() . ',
|
||||
' . $this->getFetchColumns() . '
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
JOIN users u ON (s.user_id = u.user_id)
|
||||
JOIN institutional_subscriptions iss ON (s.subscription_id = iss.subscription_id)
|
||||
' . $institutionFetch . '
|
||||
' . $ipRangeFetch . '
|
||||
' . $this->getInstitutionNameFetchJoins() . '
|
||||
' . $this->getFetchJoins() . '
|
||||
WHERE st.institutional = 1 AND s.journal_id = ?
|
||||
' . $searchSql . ' ORDER BY institution_name ASC, s.subscription_id',
|
||||
$params,
|
||||
$rangeInfo
|
||||
);
|
||||
|
||||
return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted in subscription grid paging
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether there is a valid institutional subscription for a given journal.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param string $IP
|
||||
* @param int $journalId
|
||||
* @param int $check Test using either start date, end date, or both (default)
|
||||
* @param string $checkDate (YYYY-MM-DD) Use this date instead of current date
|
||||
*
|
||||
* @return int|false Found subscription ID, or false for none.
|
||||
*/
|
||||
public function isValidInstitutionalSubscription($domain, $IP, $journalId, $check = Subscription::SUBSCRIPTION_DATE_BOTH, $checkDate = null)
|
||||
{
|
||||
if (empty($journalId) || (empty($domain) && empty($IP))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$today = $this->dateToDB(Core::getCurrentDate());
|
||||
|
||||
if ($checkDate == null) {
|
||||
$checkDate = $today;
|
||||
} else {
|
||||
$checkDate = $this->dateToDB($checkDate);
|
||||
}
|
||||
|
||||
switch ($check) {
|
||||
case Subscription::SUBSCRIPTION_DATE_START:
|
||||
$dateSql = sprintf('%s >= s.date_start AND %s >= s.date_start', $checkDate, $today);
|
||||
break;
|
||||
case Subscription::SUBSCRIPTION_DATE_END:
|
||||
$dateSql = sprintf('%s <= s.date_end AND %s >= s.date_start', $checkDate, $today);
|
||||
break;
|
||||
default:
|
||||
$dateSql = sprintf('%s BETWEEN s.date_start AND s.date_end', $checkDate);
|
||||
}
|
||||
|
||||
// Check if domain match
|
||||
if (!empty($domain)) {
|
||||
$result = $this->retrieve(
|
||||
'
|
||||
SELECT iss.subscription_id
|
||||
FROM institutional_subscriptions iss
|
||||
JOIN subscriptions s ON (iss.subscription_id = s.subscription_id)
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
WHERE POSITION(UPPER(LPAD(iss.domain, LENGTH(iss.domain)+1, \'.\')) IN UPPER(LPAD(?, LENGTH(?)+1, \'.\'))) != 0
|
||||
AND iss.domain != \'\'
|
||||
AND s.journal_id = ?
|
||||
AND s.status = ' . Subscription::SUBSCRIPTION_STATUS_ACTIVE . '
|
||||
AND st.institutional = 1
|
||||
AND ((st.duration IS NULL) OR (st.duration IS NOT NULL AND (' . $dateSql . ')))
|
||||
AND (st.format = ' . SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_ONLINE . '
|
||||
OR st.format = ' . SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE . ')',
|
||||
[$domain, $domain, (int) $journalId]
|
||||
);
|
||||
$row = $result->current();
|
||||
if ($row) {
|
||||
return $row->subscription_id;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for IP match
|
||||
if (!empty($IP)) {
|
||||
$IP = sprintf('%u', ip2long($IP));
|
||||
$result = $this->retrieve(
|
||||
'SELECT iss.subscription_id
|
||||
FROM institutional_subscriptions iss
|
||||
JOIN institution_ip iip ON (iip.institution_id = iss.institution_id)
|
||||
JOIN subscriptions s ON (iss.subscription_id = s.subscription_id)
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
WHERE s.journal_id = ?
|
||||
AND ? BETWEEN iip.ip_start AND COALESCE(iip.ip_end, iip.ip_start)
|
||||
AND s.status = ' . Subscription::SUBSCRIPTION_STATUS_ACTIVE . '
|
||||
AND st.institutional = 1
|
||||
AND (
|
||||
st.duration IS NULL
|
||||
OR (' . $dateSql . ')
|
||||
)
|
||||
AND (
|
||||
st.format = ' . SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_ONLINE . '
|
||||
OR st.format = ' . SubscriptionType::SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE . '
|
||||
)',
|
||||
[(int) $journalId, $IP]
|
||||
);
|
||||
$row = $result->current();
|
||||
if ($row) {
|
||||
return $row->subscription_id;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve active institutional subscriptions matching a particular end date and journal ID.
|
||||
*
|
||||
* @param string $dateEnd (YYYY-MM-DD)
|
||||
* @param int $journalId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<InstitutionalSubscription> Object containing matching InstitutionalSubscriptions
|
||||
*/
|
||||
public function getByDateEnd($dateEnd, $journalId, $rangeInfo = null)
|
||||
{
|
||||
$dateEnd = explode('-', $dateEnd);
|
||||
|
||||
$params = array_merge([$dateEnd[0], $dateEnd[1], $dateEnd[2], (int) $journalId], $this->getInstitutionNameFetchParameters());
|
||||
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT s.*, iss.*
|
||||
' . $this->getInstitutionNameFetchColumns() . ',
|
||||
FROM subscriptions s
|
||||
JOIN subscription_types st ON (s.type_id = st.type_id)
|
||||
JOIN institutional_subscriptions iss ON (s.subscription_id = iss.subscription_id)
|
||||
' . $this->getInstitutionNameFetchJoins() . '
|
||||
WHERE s.status = ' . Subscription::SUBSCRIPTION_STATUS_ACTIVE . '
|
||||
AND st.institutional = 1
|
||||
AND EXTRACT(YEAR FROM s.date_end) = ?
|
||||
AND EXTRACT(MONTH FROM s.date_end) = ?
|
||||
AND EXTRACT(DAY FROM s.date_end) = ?
|
||||
AND s.journal_id = ?
|
||||
ORDER BY institution_name ASC, s.subscription_id',
|
||||
$params,
|
||||
$rangeInfo
|
||||
);
|
||||
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Renew an institutional subscription by dateEnd + duration of subscription type
|
||||
* if the institutional subscription is expired, renew to current date + duration
|
||||
*
|
||||
* @param InstitutionalSubscription $institutionalSubscription
|
||||
*/
|
||||
public function renewSubscription($institutionalSubscription)
|
||||
{
|
||||
return $this->_renewSubscription($institutionalSubscription);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generator function to create object.
|
||||
*
|
||||
* @return InstitutionalSubscription
|
||||
*/
|
||||
public function newDataObject()
|
||||
{
|
||||
return new InstitutionalSubscription();
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to return an InstitutionalSubscription object from a row.
|
||||
*
|
||||
* @param array $row
|
||||
*
|
||||
* @return InstitutionalSubscription
|
||||
*/
|
||||
public function _fromRow($row)
|
||||
{
|
||||
/** @var InstitutionalSubscription */
|
||||
$institutionalSubscription = parent::_fromRow($row);
|
||||
|
||||
$institutionalSubscription->setInstitutionId($row['institution_id']);
|
||||
$institutionalSubscription->setInstitutionMailingAddress($row['mailing_address']);
|
||||
$institutionalSubscription->setDomain($row['domain']);
|
||||
|
||||
Hook::call('InstitutionalSubscriptionDAO::_fromRow', [&$institutionalSubscription, &$row]);
|
||||
|
||||
return $institutionalSubscription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of extra parameters to bind to the institution fetch queries.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getInstitutionNameFetchParameters()
|
||||
{
|
||||
$locale = Locale::getLocale();
|
||||
$journal = Application::get()->getRequest()->getContext();
|
||||
$primaryLocale = $journal->getPrimaryLocale();
|
||||
return [
|
||||
'name', $locale,
|
||||
'name', $primaryLocale,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a SQL snippet of extra columns to fetch during institution fetch queries.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getInstitutionNameFetchColumns()
|
||||
{
|
||||
return 'COALESCE(isal.setting_value, isapl.setting_value) AS institution_name';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a SQL snippet of extra joins to include during institution fetch queries.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getInstitutionNameFetchJoins()
|
||||
{
|
||||
return 'LEFT JOIN institution_settings isal ON (isal.institution_id = iss.institution_id AND isal.setting_name = ? AND isal.locale = ?)
|
||||
LEFT JOIN institution_settings isapl ON (isapl.institution_id = iss.institution_id AND isapl.setting_name = ? AND isapl.locale = ?)';
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\InstitutionalSubscriptionDAO', '\InstitutionalSubscriptionDAO');
|
||||
foreach ([
|
||||
'SUBSCRIPTION_INSTITUTION_NAME',
|
||||
'SUBSCRIPTION_DOMAIN',
|
||||
'SUBSCRIPTION_IP_RANGE',
|
||||
] as $constantName) {
|
||||
define($constantName, constant('\InstitutionalSubscriptionDAO::' . $constantName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/Subscription.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Subscription
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see SubscriptionDAO
|
||||
*
|
||||
* @brief Basic class describing a subscription.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use PKP\db\DAORegistry;
|
||||
|
||||
class Subscription extends \PKP\core\DataObject
|
||||
{
|
||||
public const SUBSCRIPTION_STATUS_ACTIVE = 1;
|
||||
public const SUBSCRIPTION_STATUS_NEEDS_INFORMATION = 2;
|
||||
public const SUBSCRIPTION_STATUS_NEEDS_APPROVAL = 3;
|
||||
public const SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT = 4;
|
||||
public const SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT = 5;
|
||||
public const SUBSCRIPTION_STATUS_OTHER = 16;
|
||||
|
||||
public const SUBSCRIPTION_DATE_START = 1;
|
||||
public const SUBSCRIPTION_DATE_END = 2;
|
||||
public const SUBSCRIPTION_DATE_BOTH = 3;
|
||||
|
||||
public const SUBSCRIPTION_YEAR_OFFSET_PAST = '-10';
|
||||
public const SUBSCRIPTION_YEAR_OFFSET_FUTURE = '+10';
|
||||
|
||||
//
|
||||
// Get/set methods
|
||||
//
|
||||
|
||||
/**
|
||||
* Get the journal ID of the subscription.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getJournalId()
|
||||
{
|
||||
return $this->getData('journalId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the journal ID of the subscription.
|
||||
*
|
||||
* @param int $journalId
|
||||
*/
|
||||
public function setJournalId($journalId)
|
||||
{
|
||||
$this->setData('journalId', $journalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user ID of the subscription.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getUserId()
|
||||
{
|
||||
return $this->getData('userId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user ID of the subscription.
|
||||
*
|
||||
* @param int $userId
|
||||
*/
|
||||
public function setUserId($userId)
|
||||
{
|
||||
$this->setData('userId', $userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's full name of the subscription.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserFullName()
|
||||
{
|
||||
return Repo::user()->get($this->getData('userId'), true)->getFullName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's email of the subscription.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserEmail()
|
||||
{
|
||||
return Repo::user()->get($this->getData('userId'), true)->getEmail();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subscription type ID of the subscription.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTypeId()
|
||||
{
|
||||
return $this->getData('typeId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the subscription type ID of the subscription.
|
||||
*
|
||||
* @param int $typeId
|
||||
*/
|
||||
public function setTypeId($typeId)
|
||||
{
|
||||
$this->setData('typeId', $typeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subscription type name of the subscription.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSubscriptionTypeName()
|
||||
{
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
return $subscriptionTypeDao->getSubscriptionTypeName($this->getData('typeId'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subscription type name of the subscription.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSubscriptionTypeSummaryString()
|
||||
{
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($this->getData('typeId'));
|
||||
return $subscriptionType->getSummaryString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subscription type institutional flag for the subscription.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSubscriptionTypeInstitutional()
|
||||
{
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
return $subscriptionTypeDao->getSubscriptionTypeInstitutional($this->getData('typeId'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the subscription type is non-expiring for the subscription.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isNonExpiring()
|
||||
{
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($this->getTypeId());
|
||||
return $subscriptionType->getNonExpiring();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription start date.
|
||||
*
|
||||
* @return string (YYYY-MM-DD)
|
||||
*/
|
||||
public function getDateStart()
|
||||
{
|
||||
return $this->getData('dateStart');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription start date.
|
||||
*
|
||||
* @param string $dateStart (YYYY-MM-DD)
|
||||
*/
|
||||
public function setDateStart($dateStart)
|
||||
{
|
||||
$this->setData('dateStart', $dateStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription end date.
|
||||
*
|
||||
* @return string (YYYY-MM-DD)
|
||||
*/
|
||||
public function getDateEnd()
|
||||
{
|
||||
return $this->getData('dateEnd');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription end date.
|
||||
*
|
||||
* @param string $dateEnd (YYYY-MM-DD)
|
||||
*/
|
||||
public function setDateEnd($dateEnd)
|
||||
{
|
||||
$this->setData('dateEnd', $dateEnd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription status.
|
||||
*
|
||||
* @return int SUBSCRIPTION_STATUS_...
|
||||
*/
|
||||
public function getStatus()
|
||||
{
|
||||
return $this->getData('status');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription status.
|
||||
*
|
||||
* @param int $status SUBSCRIPTION_STATUS_...
|
||||
*/
|
||||
public function setStatus($status)
|
||||
{
|
||||
$this->setData('status', $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription status string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getStatusString()
|
||||
{
|
||||
switch ($this->getData('status')) {
|
||||
case self::SUBSCRIPTION_STATUS_ACTIVE:
|
||||
return __('subscriptions.status.active');
|
||||
case self::SUBSCRIPTION_STATUS_NEEDS_INFORMATION:
|
||||
return __('subscriptions.status.needsInformation');
|
||||
case self::SUBSCRIPTION_STATUS_NEEDS_APPROVAL:
|
||||
return __('subscriptions.status.needsApproval');
|
||||
case self::SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT:
|
||||
return __('subscriptions.status.awaitingManualPayment');
|
||||
case self::SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT:
|
||||
return __('subscriptions.status.awaitingOnlinePayment');
|
||||
case self::SUBSCRIPTION_STATUS_OTHER:
|
||||
return __('subscriptions.status.other');
|
||||
default:
|
||||
return __('subscriptions.status');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription membership.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMembership()
|
||||
{
|
||||
return $this->getData('membership');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription membership.
|
||||
*
|
||||
* @param string $membership
|
||||
*/
|
||||
public function setMembership($membership)
|
||||
{
|
||||
$this->setData('membership', $membership);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription reference number.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getReferenceNumber()
|
||||
{
|
||||
return $this->getData('referenceNumber');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription reference number.
|
||||
*
|
||||
* @param string $referenceNumber
|
||||
*/
|
||||
public function setReferenceNumber($referenceNumber)
|
||||
{
|
||||
$this->setData('referenceNumber', $referenceNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription notes.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNotes()
|
||||
{
|
||||
return $this->getData('notes');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription notes.
|
||||
*
|
||||
* @param string $notes
|
||||
*/
|
||||
public function setNotes($notes)
|
||||
{
|
||||
$this->setData('notes', $notes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether subscription is expired
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired()
|
||||
{
|
||||
if (strtotime($this->getData('dateEnd')) < time()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\Subscription', '\Subscription');
|
||||
foreach ([
|
||||
'SUBSCRIPTION_STATUS_ACTIVE',
|
||||
'SUBSCRIPTION_STATUS_NEEDS_INFORMATION',
|
||||
'SUBSCRIPTION_STATUS_NEEDS_APPROVAL',
|
||||
'SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT',
|
||||
'SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT',
|
||||
'SUBSCRIPTION_STATUS_OTHER',
|
||||
'SUBSCRIPTION_DATE_START',
|
||||
'SUBSCRIPTION_DATE_END',
|
||||
'SUBSCRIPTION_DATE_BOTH',
|
||||
'SUBSCRIPTION_YEAR_OFFSET_PAST',
|
||||
'SUBSCRIPTION_YEAR_OFFSET_FUTURE',
|
||||
] as $constantName) {
|
||||
define($constantName, constant('\Subscription::' . $constantName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/SubscriptionAction.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class SubscriptionAction
|
||||
*
|
||||
* @ingroup subscriptions
|
||||
*
|
||||
* Common actions for subscription management functions.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use APP\core\Request;
|
||||
use APP\facades\Repo;
|
||||
use APP\notification\NotificationManager;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use PKP\mail\Mailable;
|
||||
use PKP\notification\PKPNotification;
|
||||
|
||||
class SubscriptionAction
|
||||
{
|
||||
/**
|
||||
* Send notification email to Subscription Manager when online payment is completed.
|
||||
*/
|
||||
public static function sendOnlinePaymentNotificationEmail(Request $request, Mailable $mailable): void
|
||||
{
|
||||
$journal = $request->getJournal();
|
||||
|
||||
$subscriptionContactName = $journal->getData('subscriptionName');
|
||||
$subscriptionContactEmail = $journal->getData('subscriptionEmail');
|
||||
|
||||
if (empty($subscriptionContactEmail)) {
|
||||
$subscriptionContactEmail = $journal->getData('contactEmail');
|
||||
$subscriptionContactName = $journal->getData('contactName');
|
||||
}
|
||||
|
||||
if (empty($subscriptionContactEmail)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$template = Repo::emailTemplate()->getByKey($journal->getId(), $mailable::getEmailTemplateKey());
|
||||
$mailable
|
||||
->sender($request->getUser())
|
||||
->replyTo($subscriptionContactEmail, $subscriptionContactName)
|
||||
->to($subscriptionContactEmail, $subscriptionContactName)
|
||||
->subject($template->getLocalizedData('subject', $journal->getPrimaryLocale()))
|
||||
->body($template->getLocalizedData('body', $journal->getPrimaryLocale()));
|
||||
|
||||
try {
|
||||
Mail::send($mailable);
|
||||
} catch (Exception $e) {
|
||||
$notificationMgr = new NotificationManager();
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$request->getUser()->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_ERROR,
|
||||
['contents' => __('email.compose.error')]
|
||||
);
|
||||
error_log($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\SubscriptionAction', '\SubscriptionAction');
|
||||
}
|
||||
@@ -0,0 +1,540 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/SubscriptionDAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class SubscriptionDAO
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see Subscription
|
||||
*
|
||||
* @brief Abstract class for retrieving and modifying subscriptions.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\db\DAOResultFactory;
|
||||
use PKP\db\DBResultRange;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\identity\Identity;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
abstract class SubscriptionDAO extends \PKP\db\DAO
|
||||
{
|
||||
public const SUBSCRIPTION_MEMBERSHIP = 2;
|
||||
public const SUBSCRIPTION_REFERENCE_NUMBER = 3;
|
||||
public const SUBSCRIPTION_NOTES = 4;
|
||||
|
||||
/**
|
||||
* Retrieve subscription by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
*
|
||||
* @return Subscription
|
||||
*/
|
||||
abstract public function getById($subscriptionId);
|
||||
|
||||
/**
|
||||
* Retrieve subscription journal ID by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
*
|
||||
* @return int|false
|
||||
*/
|
||||
public function getSubscriptionJournalId($subscriptionId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT journal_id FROM subscriptions WHERE subscription_id = ?',
|
||||
[(int) $subscriptionId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? $row->journal_id : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve subscription status options as associative array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getStatusOptions()
|
||||
{
|
||||
return [
|
||||
Subscription::SUBSCRIPTION_STATUS_ACTIVE => 'subscriptions.status.active',
|
||||
Subscription::SUBSCRIPTION_STATUS_NEEDS_INFORMATION => 'subscriptions.status.needsInformation',
|
||||
Subscription::SUBSCRIPTION_STATUS_NEEDS_APPROVAL => 'subscriptions.status.needsApproval',
|
||||
Subscription::SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT => 'subscriptions.status.awaitingManualPayment',
|
||||
Subscription::SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT => 'subscriptions.status.awaitingOnlinePayment',
|
||||
Subscription::SUBSCRIPTION_STATUS_OTHER => 'subscriptions.status.other'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return number of subscriptions with given status.
|
||||
*
|
||||
* @param int $status
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getStatusCount($status);
|
||||
|
||||
/**
|
||||
* Check if subscription exists for a given subscriptionId.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function subscriptionExists($subscriptionId);
|
||||
|
||||
/**
|
||||
* Check if subscription exists given a user.
|
||||
*
|
||||
* @param int $subscriptionId
|
||||
* @param int $userId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function subscriptionExistsByUser($subscriptionId, $userId);
|
||||
|
||||
/**
|
||||
* Check if subscription exists given a user and journal.
|
||||
*
|
||||
* @param int $userId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function subscriptionExistsByUserForJournal($userId, $journalId);
|
||||
|
||||
/**
|
||||
* Insert a subscription.
|
||||
*
|
||||
* @param Subscription $subscription
|
||||
*/
|
||||
abstract public function insertObject($subscription);
|
||||
|
||||
/**
|
||||
* Update existing subscription.
|
||||
*
|
||||
* @param Subscription $subscription
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function updateObject($subscription);
|
||||
|
||||
/**
|
||||
* Delete subscription by subscription ID.
|
||||
*
|
||||
* @param int $subscriptionId Subscription ID
|
||||
* @param int $journalId Journal ID
|
||||
*/
|
||||
abstract public function deleteById($subscriptionId, $journalId);
|
||||
|
||||
/**
|
||||
* Delete subscriptions by journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function deleteByJournalId($journalId);
|
||||
|
||||
/**
|
||||
* Delete subscriptions by user ID.
|
||||
*
|
||||
* @param int $userId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function deleteByUserId($userId);
|
||||
|
||||
/**
|
||||
* Delete all subscriptions by subscription type ID.
|
||||
*
|
||||
* @param int $subscriptionTypeId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function deleteByTypeId($subscriptionTypeId);
|
||||
|
||||
/**
|
||||
* Retrieve all subscriptions.
|
||||
*
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<Subscription> Object containing Subscriptions
|
||||
*/
|
||||
abstract public function getAll($rangeInfo = null);
|
||||
|
||||
/**
|
||||
* Retrieve subscriptions matching a particular journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param int $status
|
||||
* @param int $searchField
|
||||
* @param string $searchMatch "is" or "contains" or "startsWith"
|
||||
* @param string $search to look in $searchField for
|
||||
* @param int $dateField
|
||||
* @param string $dateFrom date to search from
|
||||
* @param string $dateTo date to search to
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<Subscription> Object containing matching Subscriptions
|
||||
*/
|
||||
abstract public function getByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null);
|
||||
|
||||
/**
|
||||
* Retrieve subscriptions matching a particular end date and journal ID.
|
||||
*
|
||||
* @param string $dateEnd (YYYY-MM-DD)
|
||||
* @param int $journalId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<Subscription> Object containing matching Subscriptions
|
||||
*/
|
||||
abstract public function getByDateEnd($dateEnd, $journalId, $rangeInfo = null);
|
||||
|
||||
/**
|
||||
* Function to renew a subscription by dateEnd + duration of subscription type
|
||||
* if the subscription is expired, renew to current date + duration
|
||||
*
|
||||
* @param Subscription $subscription
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function renewSubscription($subscription);
|
||||
|
||||
/**
|
||||
* Internal function to generate subscription based search query.
|
||||
*
|
||||
* @param null|mixed $status
|
||||
* @param null|mixed $searchField
|
||||
* @param null|mixed $searchMatch
|
||||
* @param null|mixed $search
|
||||
* @param null|mixed $dateField
|
||||
* @param null|mixed $dateFrom
|
||||
* @param null|mixed $dateTo
|
||||
* @param null|mixed $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _generateSearchSQL($status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, &$params = null)
|
||||
{
|
||||
$searchSql = '';
|
||||
$userDao = Repo::user()->dao;
|
||||
if (!empty($search)) {
|
||||
switch ($searchField) {
|
||||
case Identity::IDENTITY_SETTING_GIVENNAME:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(COALESCE(ugl.setting_value,ugpl.setting_value)) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(COALESCE(ugl.setting_value,ugpl.setting_value)) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(COALESCE(ugl,ugpl)) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case Identity::IDENTITY_SETTING_FAMILYNAME:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(COALESCE(ufl.setting_value,ufpl.setting_value)) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(COALESCE(ufl.setting_value,ufpl.setting_value)) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(COALESCE(ufl.setting_value,ufpl.setting_value)) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case $userDao::USER_FIELD_USERNAME:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(u.username) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(u.username) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(u.username) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case $userDao::USER_FIELD_EMAIL:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(u.email) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(u.email) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(u.email) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case self::SUBSCRIPTION_MEMBERSHIP:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(s.membership) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(s.membership) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case self::SUBSCRIPTION_REFERENCE_NUMBER:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(s.reference_number) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(s.reference_number) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
case self::SUBSCRIPTION_NOTES:
|
||||
if ($searchMatch === 'is') {
|
||||
$searchSql = ' AND LOWER(s.notes) = LOWER(?)';
|
||||
} elseif ($searchMatch === 'contains') {
|
||||
$searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
|
||||
$search = '%' . $search . '%';
|
||||
} else { // $searchMatch === 'startsWith'
|
||||
$searchSql = ' AND LOWER(s.notes) LIKE LOWER(?)';
|
||||
$search = $search . '%';
|
||||
}
|
||||
$params[] = $search;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($dateFrom) || !empty($dateTo)) {
|
||||
switch ($dateField) {
|
||||
case Subscription::SUBSCRIPTION_DATE_START:
|
||||
if (!empty($dateFrom)) {
|
||||
$searchSql .= ' AND s.date_start >= ' . $this->datetimeToDB($dateFrom);
|
||||
}
|
||||
if (!empty($dateTo)) {
|
||||
$searchSql .= ' AND s.date_start <= ' . $this->datetimeToDB($dateTo);
|
||||
}
|
||||
break;
|
||||
case Subscription::SUBSCRIPTION_DATE_END:
|
||||
if (!empty($dateFrom)) {
|
||||
$searchSql .= ' AND s.date_end >= ' . $this->datetimeToDB($dateFrom);
|
||||
}
|
||||
if (!empty($dateTo)) {
|
||||
$searchSql .= ' AND s.date_end <= ' . $this->datetimeToDB($dateTo);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($status)) {
|
||||
$searchSql .= ' AND s.status = ' . (int) $status;
|
||||
}
|
||||
|
||||
return $searchSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of extra parameters to bind to the user fetch queries.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFetchParameters()
|
||||
{
|
||||
$locale = Locale::getLocale();
|
||||
// the users register for the site, thus
|
||||
// the site primary locale should be the default locale
|
||||
$site = Application::get()->getRequest()->getSite();
|
||||
$primaryLocale = $site->getPrimaryLocale();
|
||||
return [
|
||||
Identity::IDENTITY_SETTING_GIVENNAME, $locale,
|
||||
Identity::IDENTITY_SETTING_GIVENNAME, $primaryLocale,
|
||||
Identity::IDENTITY_SETTING_FAMILYNAME, $locale,
|
||||
Identity::IDENTITY_SETTING_FAMILYNAME, $primaryLocale,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a SQL snippet of extra columns to fetch during user fetch queries.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFetchColumns()
|
||||
{
|
||||
return 'COALESCE(ugl.setting_value, ugpl.setting_value) AS user_given,
|
||||
CASE WHEN ugl.setting_value <> \'\' THEN ufl.setting_value ELSE ufpl.setting_value END AS user_family';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a SQL snippet of extra joins to include during user fetch queries.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFetchJoins()
|
||||
{
|
||||
return 'LEFT JOIN user_settings ugl ON (u.user_id = ugl.user_id AND ugl.setting_name = ? AND ugl.locale = ?)
|
||||
LEFT JOIN user_settings ugpl ON (u.user_id = ugpl.user_id AND ugpl.setting_name = ? AND ugpl.locale = ?)
|
||||
LEFT JOIN user_settings ufl ON (u.user_id = ufl.user_id AND ufl.setting_name = ? AND ufl.locale = ?)
|
||||
LEFT JOIN user_settings ufpl ON (u.user_id = ufpl.user_id AND ufpl.setting_name = ? AND ufpl.locale = ?)';
|
||||
}
|
||||
|
||||
/**
|
||||
* Generator function to create object.
|
||||
*
|
||||
* @return Subscription
|
||||
*/
|
||||
abstract public function newDataObject();
|
||||
|
||||
/**
|
||||
* Internal function to return a Subscription object from a row.
|
||||
*
|
||||
* @param array $row
|
||||
*
|
||||
* @return Subscription
|
||||
*/
|
||||
public function _fromRow($row)
|
||||
{
|
||||
$subscription = $this->newDataObject();
|
||||
$subscription->setId($row['subscription_id']);
|
||||
$subscription->setJournalId($row['journal_id']);
|
||||
$subscription->setUserId($row['user_id']);
|
||||
$subscription->setTypeId($row['type_id']);
|
||||
$subscription->setDateStart($this->dateFromDB($row['date_start']));
|
||||
$subscription->setDateEnd($this->dateFromDB($row['date_end']));
|
||||
$subscription->setStatus($row['status']);
|
||||
$subscription->setMembership($row['membership']);
|
||||
$subscription->setReferenceNumber($row['reference_number']);
|
||||
$subscription->setNotes($row['notes']);
|
||||
|
||||
Hook::call('SubscriptionDAO::_fromRow', [&$subscription, &$row]);
|
||||
|
||||
return $subscription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to insert a new Subscription.
|
||||
*
|
||||
* @param Subscription $subscription
|
||||
*
|
||||
* @return int Subscription ID
|
||||
*/
|
||||
public function _insertObject($subscription)
|
||||
{
|
||||
$dateStart = $subscription->getDateStart();
|
||||
$dateEnd = $subscription->getDateEnd();
|
||||
$this->update(
|
||||
sprintf(
|
||||
'INSERT INTO subscriptions
|
||||
(journal_id, user_id, type_id, date_start, date_end, status, membership, reference_number, notes)
|
||||
VALUES
|
||||
(?, ?, ?, %s, %s, ?, ?, ?, ?)',
|
||||
$dateStart !== null ? $this->dateToDB($dateStart) : 'null',
|
||||
$dateEnd !== null ? $this->datetimeToDB($dateEnd) : 'null'
|
||||
),
|
||||
[
|
||||
(int) $subscription->getJournalId(),
|
||||
(int) $subscription->getUserId(),
|
||||
(int) $subscription->getTypeId(),
|
||||
(int) $subscription->getStatus(),
|
||||
$subscription->getMembership(),
|
||||
$subscription->getReferenceNumber(),
|
||||
$subscription->getNotes()
|
||||
]
|
||||
);
|
||||
|
||||
$subscriptionId = $this->getInsertId();
|
||||
$subscription->setId($subscriptionId);
|
||||
|
||||
return $subscriptionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to update a Subscription.
|
||||
*
|
||||
* @param Subscription $subscription
|
||||
*/
|
||||
public function _updateObject($subscription)
|
||||
{
|
||||
$dateStart = $subscription->getDateStart();
|
||||
$dateEnd = $subscription->getDateEnd();
|
||||
$this->update(
|
||||
sprintf(
|
||||
'UPDATE subscriptions
|
||||
SET
|
||||
journal_id = ?,
|
||||
user_id = ?,
|
||||
type_id = ?,
|
||||
date_start = %s,
|
||||
date_end = %s,
|
||||
status = ?,
|
||||
membership = ?,
|
||||
reference_number = ?,
|
||||
notes = ?
|
||||
WHERE subscription_id = ?',
|
||||
$dateStart !== null ? $this->dateToDB($dateStart) : 'null',
|
||||
$dateEnd !== null ? $this->datetimeToDB($dateEnd) : 'null'
|
||||
),
|
||||
[
|
||||
(int) $subscription->getJournalId(),
|
||||
(int) $subscription->getUserId(),
|
||||
(int) $subscription->getTypeId(),
|
||||
(int) $subscription->getStatus(),
|
||||
$subscription->getMembership(),
|
||||
$subscription->getReferenceNumber(),
|
||||
$subscription->getNotes(),
|
||||
(int) $subscription->getId()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to renew a subscription by dateEnd + duration of subscription type
|
||||
* if the subscription is expired, renew to current date + duration
|
||||
*
|
||||
* @param Subscription $subscription
|
||||
*/
|
||||
public function _renewSubscription($subscription)
|
||||
{
|
||||
if ($subscription->isNonExpiring()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($subscription->getTypeId());
|
||||
|
||||
$duration = $subscriptionType->getDuration();
|
||||
$dateEnd = strtotime($subscription->getDateEnd());
|
||||
|
||||
// if the subscription is expired, extend it to today + duration of subscription
|
||||
$time = time();
|
||||
if ($dateEnd < $time) {
|
||||
$dateEnd = $time;
|
||||
}
|
||||
|
||||
$subscription->setDateEnd(mktime(23, 59, 59, date('m', $dateEnd) + $duration, date('d', $dateEnd), date('Y', $dateEnd)));
|
||||
$this->updateObject($subscription);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\SubscriptionDAO', '\SubscriptionDAO');
|
||||
foreach ([
|
||||
'SUBSCRIPTION_MEMBERSHIP',
|
||||
'SUBSCRIPTION_REFERENCE_NUMBER',
|
||||
'SUBSCRIPTION_NOTES',
|
||||
] as $constantName) {
|
||||
define($constantName, constant('\SubscriptionDAO::' . $constantName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,385 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/SubscriptionType.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class SubscriptionType
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see SubscriptionTypeDAO
|
||||
*
|
||||
* @brief Basic class describing a subscription type.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use PKP\facades\Locale;
|
||||
|
||||
class SubscriptionType extends \PKP\core\DataObject
|
||||
{
|
||||
// Subscription type formats
|
||||
public const SUBSCRIPTION_TYPE_FORMAT_ONLINE = 1;
|
||||
public const SUBSCRIPTION_TYPE_FORMAT_PRINT = 16;
|
||||
public const SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE = 17;
|
||||
|
||||
//
|
||||
// Get/set methods
|
||||
//
|
||||
|
||||
/**
|
||||
* Get the journal ID of the subscription type.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getJournalId()
|
||||
{
|
||||
return $this->getData('journalId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the journal ID of the subscription type.
|
||||
*
|
||||
* @param int $journalId
|
||||
*/
|
||||
public function setJournalId($journalId)
|
||||
{
|
||||
return $this->setData('journalId', $journalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the localized subscription type name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalizedName()
|
||||
{
|
||||
return $this->getLocalizedData('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type name.
|
||||
*
|
||||
* @param string $locale
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName($locale)
|
||||
{
|
||||
return $this->getData('name', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setName($name, $locale)
|
||||
{
|
||||
return $this->setData('name', $name, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the localized subscription type description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalizedDescription()
|
||||
{
|
||||
return $this->getLocalizedData('description');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type description.
|
||||
*
|
||||
* @param string $locale
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription($locale)
|
||||
{
|
||||
return $this->getData('description', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type description.
|
||||
*
|
||||
* @param string $description
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setDescription($description, $locale)
|
||||
{
|
||||
return $this->setData('description', $description, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type cost.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getCost()
|
||||
{
|
||||
return $this->getData('cost');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type cost.
|
||||
*
|
||||
* @param float $cost
|
||||
*/
|
||||
public function setCost($cost)
|
||||
{
|
||||
return $this->setData('cost', $cost);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type currency code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrencyCodeAlpha()
|
||||
{
|
||||
return $this->getData('currencyCodeAlpha');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type currency code.
|
||||
*
|
||||
* @param string $currencyCodeAlpha
|
||||
*/
|
||||
public function setCurrencyCodeAlpha($currencyCodeAlpha)
|
||||
{
|
||||
return $this->setData('currencyCodeAlpha', $currencyCodeAlpha);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type currency string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrencyString()
|
||||
{
|
||||
$currency = Locale::getCurrencies()->getByLetterCode($this->getData('currencyCodeAlpha'));
|
||||
return $currency ? $currency->getLocalName() : 'subscriptionTypes.currency';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type currency abbreviated string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCurrencyStringShort()
|
||||
{
|
||||
$currency = Locale::getCurrencies()->getByLetterCode($this->getData('currencyCodeAlpha'));
|
||||
return $currency ? $currency->getLetterCode() : 'subscriptionTypes.currency';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type nonExpiring.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getNonExpiring()
|
||||
{
|
||||
return $this->getDuration() == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type duration.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getDuration()
|
||||
{
|
||||
return $this->getData('duration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type duration.
|
||||
*
|
||||
* @param int $duration
|
||||
*/
|
||||
public function setDuration($duration)
|
||||
{
|
||||
return $this->setData('duration', $duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type duration in years and months.
|
||||
*
|
||||
* @param string $locale
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDurationYearsMonths($locale = null)
|
||||
{
|
||||
if (!$this->getDuration()) {
|
||||
return __('subscriptionTypes.nonExpiring', [], $locale);
|
||||
}
|
||||
|
||||
$years = (int)floor($this->getDuration() / 12);
|
||||
$months = (int)fmod($this->getDuration(), 12);
|
||||
$yearsMonths = '';
|
||||
|
||||
if ($years == 1) {
|
||||
$yearsMonths = '1 ' . __('subscriptionTypes.year', [], $locale);
|
||||
} elseif ($years > 1) {
|
||||
$yearsMonths = $years . ' ' . __('subscriptionTypes.years', [], $locale);
|
||||
}
|
||||
|
||||
if ($months == 1) {
|
||||
$yearsMonths .= $yearsMonths == '' ? '1 ' : ' 1 ';
|
||||
$yearsMonths .= __('subscriptionTypes.month', [], $locale);
|
||||
} elseif ($months > 1) {
|
||||
$yearsMonths .= $yearsMonths == '' ? $months . ' ' : ' ' . $months . ' ';
|
||||
$yearsMonths .= __('subscriptionTypes.months', [], $locale);
|
||||
}
|
||||
|
||||
return $yearsMonths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type format.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFormat()
|
||||
{
|
||||
return $this->getData('format');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type format.
|
||||
*
|
||||
* @param int $format
|
||||
*/
|
||||
public function setFormat($format)
|
||||
{
|
||||
return $this->setData('format', $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type format locale key.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFormatString()
|
||||
{
|
||||
switch ($this->getData('format')) {
|
||||
case self::SUBSCRIPTION_TYPE_FORMAT_ONLINE:
|
||||
return 'subscriptionTypes.format.online';
|
||||
case self::SUBSCRIPTION_TYPE_FORMAT_PRINT:
|
||||
return 'subscriptionTypes.format.print';
|
||||
case self::SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE:
|
||||
return 'subscriptionTypes.format.printOnline';
|
||||
default:
|
||||
return 'subscriptionTypes.format';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this subscription type is for an institution.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getInstitutional()
|
||||
{
|
||||
return $this->getData('institutional');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not this subscription type is for an institution.
|
||||
*
|
||||
* @param bool $institutional
|
||||
*/
|
||||
public function setInstitutional($institutional)
|
||||
{
|
||||
return $this->setData('institutional', $institutional);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this subscription type requires a membership.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getMembership()
|
||||
{
|
||||
return $this->getData('membership');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not this subscription type requires a membership.
|
||||
*
|
||||
* @param bool $membership
|
||||
*/
|
||||
public function setMembership($membership)
|
||||
{
|
||||
return $this->setData('membership', $membership);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this subscription type should be publicly visible.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getDisablePublicDisplay()
|
||||
{
|
||||
return $this->getData('disable_public_display');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether or not this subscription type should be publicly visible.
|
||||
*
|
||||
* @param bool $disablePublicDisplay
|
||||
*/
|
||||
public function setDisablePublicDisplay($disablePublicDisplay)
|
||||
{
|
||||
return $this->setData('disable_public_display', $disablePublicDisplay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type display sequence.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getSequence()
|
||||
{
|
||||
return $this->getData('sequence');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscription type display sequence.
|
||||
*
|
||||
* @param float $sequence
|
||||
*/
|
||||
public function setSequence($sequence)
|
||||
{
|
||||
return $this->setData('sequence', $sequence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscription type summary in the form: TypeName - Duration - Cost (CurrencyShort).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSummaryString()
|
||||
{
|
||||
return $this->getLocalizedName() . ' - ' . $this->getDurationYearsMonths() . ' - ' . sprintf('%.2f', $this->getCost()) . ' ' . $this->getCurrencyStringShort();
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\SubscriptionType', '\SubscriptionType');
|
||||
foreach ([
|
||||
'SUBSCRIPTION_TYPE_FORMAT_ONLINE',
|
||||
'SUBSCRIPTION_TYPE_FORMAT_PRINT',
|
||||
'SUBSCRIPTION_TYPE_FORMAT_PRINT_ONLINE',
|
||||
] as $constantName) {
|
||||
define($constantName, constant('\SubscriptionType::' . $constantName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/SubscriptionTypeDAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class SubscriptionTypeDAO
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @see SubscriptionType
|
||||
*
|
||||
* @brief Operations for retrieving and modifying SubscriptionType objects.
|
||||
*/
|
||||
|
||||
namespace APP\subscription;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\db\DAOResultFactory;
|
||||
use PKP\db\DBResultRange;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
class SubscriptionTypeDAO extends \PKP\db\DAO
|
||||
{
|
||||
/**
|
||||
* Create a new subscription type.
|
||||
*
|
||||
* @return SubscriptionType
|
||||
*/
|
||||
public function newDataObject()
|
||||
{
|
||||
return new SubscriptionType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a subscription type by ID.
|
||||
*
|
||||
* @param int $typeId
|
||||
* @param int $journalId optional
|
||||
*
|
||||
* @return ?SubscriptionType
|
||||
*/
|
||||
public function getById($typeId, $journalId = null)
|
||||
{
|
||||
$params = [(int) $typeId];
|
||||
if ($journalId) {
|
||||
$params[] = (int) $journalId;
|
||||
}
|
||||
|
||||
$result = $this->retrieve(
|
||||
'SELECT * FROM subscription_types WHERE type_id = ?' .
|
||||
($journalId ? ' AND journal_id = ?' : ''),
|
||||
$params
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? $this->_fromRow((array) $row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve subscription type name by ID.
|
||||
*
|
||||
* @param int $typeId
|
||||
*
|
||||
* @return string?
|
||||
*/
|
||||
public function getSubscriptionTypeName($typeId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COALESCE(l.setting_value, p.setting_value) as subscription_type_name FROM subscription_type_settings l LEFT JOIN subscription_type_settings p ON (p.type_id = ? AND p.setting_name = ? AND p.locale = ?) WHERE l.type_id = ? AND l.setting_name = ? AND l.locale = ?',
|
||||
[
|
||||
(int) $typeId, 'name', Locale::getLocale(),
|
||||
(int) $typeId, 'name', Locale::getPrimaryLocale()
|
||||
]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? $row->subscription_type_name : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve institutional flag by ID.
|
||||
*
|
||||
* @param int $typeId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSubscriptionTypeInstitutional($typeId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT institutional FROM subscription_types WHERE type_id = ?',
|
||||
[(int) $typeId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->institutional : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve membership flag by ID.
|
||||
*
|
||||
* @param int $typeId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSubscriptionTypeMembership($typeId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT membership FROM subscription_types WHERE type_id = ?',
|
||||
[(int) $typeId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->membership : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve public display flag by ID.
|
||||
*
|
||||
* @param int $typeId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSubscriptionTypeDisablePublicDisplay($typeId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT disable_public_display FROM subscription_types WHERE type_id = ?',
|
||||
[(int) $typeId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->disable_public_display : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a subscription type exists with the given type id for a journal.
|
||||
*
|
||||
* @param int $typeId
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionTypeExistsByTypeId($typeId, $journalId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT COUNT(*) AS row_count
|
||||
FROM subscription_types
|
||||
WHERE type_id = ?
|
||||
AND journal_id = ?',
|
||||
[(int) $typeId, (int) $journalId]
|
||||
);
|
||||
$row = $result->current();
|
||||
return $row ? (bool) $row->row_count : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to return a SubscriptionType object from a row.
|
||||
*
|
||||
* @param array $row
|
||||
*
|
||||
* @return SubscriptionType
|
||||
*/
|
||||
public function _fromRow($row)
|
||||
{
|
||||
$subscriptionType = $this->newDataObject();
|
||||
$subscriptionType->setId($row['type_id']);
|
||||
$subscriptionType->setJournalId($row['journal_id']);
|
||||
$subscriptionType->setCost($row['cost']);
|
||||
$subscriptionType->setCurrencyCodeAlpha($row['currency_code_alpha']);
|
||||
$subscriptionType->setDuration($row['duration']);
|
||||
$subscriptionType->setFormat($row['format']);
|
||||
$subscriptionType->setInstitutional($row['institutional']);
|
||||
$subscriptionType->setMembership($row['membership']);
|
||||
$subscriptionType->setDisablePublicDisplay($row['disable_public_display']);
|
||||
$subscriptionType->setSequence($row['seq']);
|
||||
|
||||
$this->getDataObjectSettings('subscription_type_settings', 'type_id', $row['type_id'], $subscriptionType);
|
||||
|
||||
Hook::call('SubscriptionTypeDAO::_fromRow', [&$subscriptionType, &$row]);
|
||||
|
||||
return $subscriptionType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of field names for which localized data is used.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLocaleFieldNames()
|
||||
{
|
||||
return ['name', 'description'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the localized settings for this object
|
||||
*
|
||||
* @param object $subscriptionType
|
||||
*/
|
||||
public function updateLocaleFields($subscriptionType)
|
||||
{
|
||||
$this->updateDataObjectSettings('subscription_type_settings', $subscriptionType, [
|
||||
'type_id' => $subscriptionType->getId()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new SubscriptionType.
|
||||
*
|
||||
* @param SubscriptionType $subscriptionType
|
||||
*
|
||||
* @return int Inserted subscription type ID
|
||||
*/
|
||||
public function insertObject($subscriptionType)
|
||||
{
|
||||
$this->update(
|
||||
'INSERT INTO subscription_types
|
||||
(journal_id, cost, currency_code_alpha, duration, format, institutional, membership, disable_public_display, seq)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
[
|
||||
(int) $subscriptionType->getJournalId(),
|
||||
(float) $subscriptionType->getCost(),
|
||||
$subscriptionType->getCurrencyCodeAlpha(),
|
||||
$subscriptionType->getDuration(),
|
||||
$subscriptionType->getFormat(),
|
||||
(int) $subscriptionType->getInstitutional(),
|
||||
$subscriptionType->getMembership(),
|
||||
(int) $subscriptionType->getDisablePublicDisplay(),
|
||||
(float) $subscriptionType->getSequence(),
|
||||
]
|
||||
);
|
||||
|
||||
$subscriptionType->setId($this->getInsertId());
|
||||
$this->updateLocaleFields($subscriptionType);
|
||||
return $subscriptionType->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing subscription type.
|
||||
*
|
||||
* @param SubscriptionType $subscriptionType
|
||||
*/
|
||||
public function updateObject($subscriptionType)
|
||||
{
|
||||
$this->update(
|
||||
'UPDATE subscription_types
|
||||
SET
|
||||
journal_id = ?,
|
||||
cost = ?,
|
||||
currency_code_alpha = ?,
|
||||
duration = ?,
|
||||
format = ?,
|
||||
institutional = ?,
|
||||
membership = ?,
|
||||
disable_public_display = ?,
|
||||
seq = ?
|
||||
WHERE type_id = ?',
|
||||
[
|
||||
(int) $subscriptionType->getJournalId(),
|
||||
$subscriptionType->getCost(),
|
||||
$subscriptionType->getCurrencyCodeAlpha(),
|
||||
$subscriptionType->getDuration(),
|
||||
$subscriptionType->getFormat(),
|
||||
(int) $subscriptionType->getInstitutional(),
|
||||
$subscriptionType->getMembership(),
|
||||
(int) $subscriptionType->getDisablePublicDisplay(),
|
||||
(float) $subscriptionType->getSequence(),
|
||||
(int) $subscriptionType->getId(),
|
||||
]
|
||||
);
|
||||
$this->updateLocaleFields($subscriptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a subscription type by ID. Note that all subscriptions with this
|
||||
* type ID are also deleted.
|
||||
*
|
||||
* @param int $typeId Subscription type ID
|
||||
* @param int $journalId Optional journal ID
|
||||
*/
|
||||
public function deleteById($typeId, $journalId = null)
|
||||
{
|
||||
$subscriptionType = $this->getById($typeId, $journalId);
|
||||
if ($subscriptionType) {
|
||||
/** @var InstitutionalSubscriptionDAO|IndividualSubscriptionDAO */
|
||||
$subscriptionDao = DAORegistry::getDAO($subscriptionType->getInstitutional() ? 'InstitutionalSubscriptionDAO' : 'IndividualSubscriptionDAO');
|
||||
$subscriptionDao->deleteById($typeId);
|
||||
$this->update('DELETE FROM subscription_types WHERE type_id = ?', [(int) $typeId]);
|
||||
$this->update('DELETE FROM subscription_type_settings WHERE type_id = ?', [(int) $typeId]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete subscription types by journal ID. Note that all subscriptions with
|
||||
* corresponding types are also deleted.
|
||||
*
|
||||
* @param int $journalId
|
||||
*/
|
||||
public function deleteByJournal($journalId)
|
||||
{
|
||||
$result = $this->retrieve(
|
||||
'SELECT type_id
|
||||
FROM subscription_types
|
||||
WHERE journal_id = ?',
|
||||
[(int) $journalId]
|
||||
);
|
||||
foreach ($result as $row) {
|
||||
$typeId = $row->type_id;
|
||||
$this->deleteById($typeId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve subscription types matching a particular journal ID.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<SubscriptionType> Object containing matching SubscriptionTypes
|
||||
*/
|
||||
public function getByJournalId($journalId, $rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
$sql = 'SELECT * FROM subscription_types WHERE journal_id = ? ORDER BY seq',
|
||||
$params = [(int) $journalId],
|
||||
$rangeInfo
|
||||
);
|
||||
return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted in subscription type grid paging
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve subscription types matching a particular journal ID and institutional flag.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param bool $institutional
|
||||
* @param bool|null $disablePublicDisplay
|
||||
* @param ?DBResultRange $rangeInfo
|
||||
*
|
||||
* @return DAOResultFactory<SubscriptionType> Object containing matching SubscriptionTypes
|
||||
*/
|
||||
public function getByInstitutional($journalId, $institutional = false, $disablePublicDisplay = null, $rangeInfo = null)
|
||||
{
|
||||
$result = $this->retrieveRange(
|
||||
'SELECT *
|
||||
FROM subscription_types
|
||||
WHERE journal_id = ?
|
||||
AND institutional = ?
|
||||
' . ($disablePublicDisplay === true ? 'AND disable_public_display = 1' : '') . '
|
||||
' . ($disablePublicDisplay === false ? 'AND disable_public_display = 0' : '') . '
|
||||
ORDER BY seq',
|
||||
[(int) $journalId, (int) $institutional],
|
||||
$rangeInfo
|
||||
);
|
||||
|
||||
return new DAOResultFactory($result, $this, '_fromRow');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if at least one subscription type exists for a given journal by institutional flag.
|
||||
*
|
||||
* @param int $journalId
|
||||
* @param bool $institutional
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function subscriptionTypesExistByInstitutional($journalId, $institutional = false)
|
||||
{
|
||||
$result = DB::table('subscription_types')
|
||||
->where('journal_id', (int) $journalId)
|
||||
->where('institutional', (int) $institutional)
|
||||
->first();
|
||||
|
||||
return is_null($result) ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sequentially renumber subscription types in their sequence order.
|
||||
*/
|
||||
public function resequenceSubscriptionTypes($journalId)
|
||||
{
|
||||
$result = $this->retrieve('SELECT type_id FROM subscription_types WHERE journal_id = ? ORDER BY seq', [(int) $journalId]);
|
||||
|
||||
for ($i = 1; $row = $result->current(); $i++) {
|
||||
$this->update('UPDATE subscription_types SET seq = ? WHERE type_id = ?', [(int) $i, (int) $row->type_id]);
|
||||
$result->next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\SubscriptionTypeDAO', '\SubscriptionTypeDAO');
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/form/PaymentTypesForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class PaymentTypesForm
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @brief Permit configuration of the various payment types.
|
||||
*/
|
||||
|
||||
namespace APP\subscription\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\journal\JournalDAO;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
|
||||
class PaymentTypesForm extends Form
|
||||
{
|
||||
/** @var array the setting names */
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('payments/paymentTypesForm.tpl');
|
||||
|
||||
$this->settings = [
|
||||
'publicationFee' => 'float',
|
||||
'purchaseArticleFeeEnabled' => 'bool',
|
||||
'purchaseArticleFee' => 'float',
|
||||
'purchaseIssueFeeEnabled' => 'bool',
|
||||
'purchaseIssueFee' => 'float',
|
||||
'membershipFee' => 'float',
|
||||
'restrictOnlyPdf' => 'bool',
|
||||
];
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'publicationFee', 'optional', 'manager.payment.form.numeric', function ($publicationFee) {
|
||||
return is_numeric($publicationFee) && $publicationFee >= 0;
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'purchaseArticleFee', 'optional', 'manager.payment.form.numeric', function ($purchaseArticleFee) {
|
||||
return is_numeric($purchaseArticleFee) && $purchaseArticleFee >= 0;
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'purchaseIssueFee', 'optional', 'manager.payment.form.numeric', function ($purchaseIssueFee) {
|
||||
return is_numeric($purchaseIssueFee) && $purchaseIssueFee >= 0;
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'membershipFee', 'optional', 'manager.payment.form.numeric', function ($membershipFee) {
|
||||
return is_numeric($membershipFee) && $membershipFee >= 0;
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current group group.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$journal = Application::get()->getRequest()->getContext();
|
||||
foreach (array_keys($this->settings) as $settingName) {
|
||||
$this->setData($settingName, $journal->getData($settingName));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(array_keys($this->settings));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
parent::execute(...$functionArgs);
|
||||
$journal = Application::get()->getRequest()->getJournal();
|
||||
foreach (array_keys($this->settings) as $settingName) {
|
||||
$journal->setData($settingName, $this->getData($settingName));
|
||||
}
|
||||
$journalDao = DAORegistry::getDAO('JournalDAO'); /** @var JournalDAO $journalDao */
|
||||
$journalDao->updateObject($journal);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\form\PaymentTypesForm', '\PaymentTypesForm');
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/form/SubscriptionForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class SubscriptionForm
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @brief Base form class for subscription create/edits.
|
||||
*/
|
||||
|
||||
namespace APP\subscription\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use APP\mail\mailables\SubscriptionNotify;
|
||||
use APP\subscription\Subscription;
|
||||
use APP\subscription\SubscriptionDAO;
|
||||
use APP\subscription\SubscriptionTypeDAO;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\form\Form;
|
||||
|
||||
class SubscriptionForm extends Form
|
||||
{
|
||||
/** @var Subscription the subscription being created/edited */
|
||||
public $subscription;
|
||||
|
||||
/** @var int the user associated with the subscription */
|
||||
public $userId;
|
||||
|
||||
/** @var array of subscription types */
|
||||
public $subscriptionTypes;
|
||||
|
||||
/** @var array valid subscription status values */
|
||||
public $validStatus;
|
||||
|
||||
/** @var array valid user country values */
|
||||
public $validCountries;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string? $template Template to use for form presentation
|
||||
* @param int $subscriptionId The subscription ID for this subscription; null for new subscription
|
||||
*/
|
||||
public function __construct($template, $subscriptionId = null)
|
||||
{
|
||||
parent::__construct($template);
|
||||
|
||||
$subscriptionId = isset($subscriptionId) ? (int) $subscriptionId : null;
|
||||
|
||||
$this->subscription = null;
|
||||
$this->subscriptionTypes = null;
|
||||
|
||||
$this->validStatus = SubscriptionDAO::getStatusOptions();
|
||||
|
||||
$this->validCountries = [];
|
||||
foreach (Locale::getCountries() as $country) {
|
||||
$this->validCountries[$country->getAlpha2()] = $country->getLocalName();
|
||||
}
|
||||
asort($this->validCountries);
|
||||
|
||||
// User is provided and valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'userId', 'required', 'manager.subscriptions.form.userIdRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'userId', 'required', 'manager.subscriptions.form.userIdValid', fn ($userId) => !!Repo::user()->get($userId)));
|
||||
|
||||
// Subscription status is provided and valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'status', 'required', 'manager.subscriptions.form.statusRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorInSet($this, 'status', 'required', 'manager.subscriptions.form.statusValid', array_keys($this->validStatus)));
|
||||
// Subscription type is provided
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'typeId', 'required', 'manager.subscriptions.form.typeIdRequired'));
|
||||
// Notify email flag is valid value
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorBoolean($this, 'notifyEmail', 'manager.subscriptions.form.notifyEmailValid'));
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the form.
|
||||
*
|
||||
* @copydoc Form::fetch
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$templateMgr->assign([
|
||||
'subscriptionId' => $this->subscription ? $this->subscription->getId() : null,
|
||||
'yearOffsetPast' => Subscription::SUBSCRIPTION_YEAR_OFFSET_PAST,
|
||||
'yearOffsetFuture' => Subscription::SUBSCRIPTION_YEAR_OFFSET_FUTURE,
|
||||
'validStatus' => $this->validStatus,
|
||||
'subscriptionTypes' => $this->subscriptionTypes,
|
||||
]);
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current subscription.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
if (isset($this->subscription)) {
|
||||
$subscription = $this->subscription;
|
||||
$this->_data = [
|
||||
'status' => $subscription->getStatus(),
|
||||
'userId' => $subscription->getUserId(),
|
||||
'typeId' => $subscription->getTypeId(),
|
||||
'dateStart' => $subscription->getDateStart(),
|
||||
'dateEnd' => $subscription->getDateEnd(),
|
||||
'membership' => $subscription->getMembership(),
|
||||
'referenceNumber' => $subscription->getReferenceNumber(),
|
||||
'notes' => $subscription->getNotes()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['status', 'userId', 'typeId', 'membership', 'referenceNumber', 'notes', 'notifyEmail', 'dateStart', 'dateEnd']);
|
||||
|
||||
// If subscription type requires it, membership is provided
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$needMembership = $subscriptionTypeDao->getSubscriptionTypeMembership($this->getData('typeId'));
|
||||
|
||||
if ($needMembership) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'membership', 'required', 'manager.subscriptions.form.membershipRequired'));
|
||||
}
|
||||
|
||||
// If subscription type requires it, start and end dates are provided
|
||||
$subscriptionType = $subscriptionTypeDao->getById($this->getData('typeId'));
|
||||
$nonExpiring = $subscriptionType->getNonExpiring();
|
||||
|
||||
if (!$nonExpiring) {
|
||||
// Start date is provided and is valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'dateStart', 'required', 'manager.subscriptions.form.dateStartRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateStart', 'required', 'manager.subscriptions.form.dateStartValid', function ($dateStart) {
|
||||
$dateStartYear = date('Y', strtotime($dateStart));
|
||||
$minYear = date('Y') + Subscription::SUBSCRIPTION_YEAR_OFFSET_PAST;
|
||||
$maxYear = date('Y') + Subscription::SUBSCRIPTION_YEAR_OFFSET_FUTURE;
|
||||
return ($dateStartYear >= $minYear && $dateStartYear <= $maxYear);
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateStart', 'required', 'manager.subscriptions.form.dateStartValid', function ($dateStart) {
|
||||
$dateStartMonth = date('m', strtotime($dateStart));
|
||||
return ($dateStartMonth >= 1 && $dateStartMonth <= 12);
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateStart', 'required', 'manager.subscriptions.form.dateStartValid', function ($dateStart) {
|
||||
$dateStartDay = date('d', strtotime($dateStart));
|
||||
return ($dateStartDay >= 1 && $dateStartDay <= 31);
|
||||
}));
|
||||
|
||||
// End date is provided and is valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'dateEnd', 'required', 'manager.subscriptions.form.dateEndRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateEnd', 'required', 'manager.subscriptions.form.dateEndValid', function ($dateEnd) {
|
||||
$dateEndYear = date('Y', strtotime($dateEnd));
|
||||
$minYear = date('Y') + Subscription::SUBSCRIPTION_YEAR_OFFSET_PAST;
|
||||
$maxYear = date('Y') + Subscription::SUBSCRIPTION_YEAR_OFFSET_FUTURE;
|
||||
return ($dateEndYear >= $minYear && $dateEndYear <= $maxYear);
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateEnd', 'required', 'manager.subscriptions.form.dateEndValid', function ($dateEnd) {
|
||||
$dateEndMonth = date('m', strtotime($dateEnd));
|
||||
return ($dateEndMonth >= 1 && $dateEndMonth <= 12);
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateEnd', 'required', 'manager.subscriptions.form.dateEndValid', function ($dateEnd) {
|
||||
$dateEndDay = date('d', strtotime($dateEnd));
|
||||
return ($dateEndDay >= 1 && $dateEndDay <= 31);
|
||||
}));
|
||||
} else {
|
||||
// Is non-expiring; ensure that start/end dates weren't entered.
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateStart', 'optional', 'manager.subscriptions.form.dateStartEmpty', function ($dateStart) {
|
||||
return empty($dateStart);
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'dateEnd', 'optional', 'manager.subscriptions.form.dateEndEmpty', function ($dateEnd) {
|
||||
return empty($dateEnd);
|
||||
}));
|
||||
}
|
||||
|
||||
// If notify email is requested, ensure subscription contact name and email exist.
|
||||
if ($this->getData('notifyEmail')) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'notifyEmail', 'optional', 'manager.subscriptions.form.subscriptionContactRequired', function () {
|
||||
$request = Application::get()->getRequest();
|
||||
$journal = $request->getJournal();
|
||||
$subscriptionName = $journal->getData('subscriptionName');
|
||||
$subscriptionEmail = $journal->getData('subscriptionEmail');
|
||||
return $subscriptionName != '' && $subscriptionEmail != '';
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
$journal = $request->getJournal();
|
||||
$subscription = & $this->subscription;
|
||||
|
||||
parent::execute(...$functionArgs);
|
||||
|
||||
$subscription->setJournalId($journal->getId());
|
||||
$subscription->setStatus($this->getData('status'));
|
||||
$subscription->setUserId($this->getData('userId'));
|
||||
$subscription->setTypeId($this->getData('typeId'));
|
||||
$subscription->setMembership($this->getData('membership') ? $this->getData('membership') : null);
|
||||
$subscription->setReferenceNumber($this->getData('referenceNumber') ? $this->getData('referenceNumber') : null);
|
||||
$subscription->setNotes($this->getData('notes') ? $this->getData('notes') : null);
|
||||
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($subscription->getTypeId());
|
||||
if (!$subscriptionType->getNonExpiring()) {
|
||||
$subscription->setDateStart($this->getData('dateStart'));
|
||||
$dateEnd = strtotime($this->getData('dateEnd'));
|
||||
$subscription->setDateEnd(mktime(23, 59, 59, (int) date('m', $dateEnd), (int) date('d', $dateEnd), (int) date('Y', $dateEnd)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function to prepare notification email
|
||||
*/
|
||||
protected function _prepareNotificationEmail(): SubscriptionNotify
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
$context = $request->getJournal();
|
||||
$user = Repo::user()->get($this->subscription->getUserId());
|
||||
$subscriptionEmail = $context->getData('subscriptionEmail');
|
||||
$subscriptionName = $context->getData('subscriptionName');
|
||||
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($this->subscription->getTypeId(), $context->getId());
|
||||
|
||||
$template = Repo::emailTemplate()->getByKey($context->getId(), SubscriptionNotify::getEmailTemplateKey());
|
||||
$mailable = new SubscriptionNotify($context, $this->subscription, $subscriptionType);
|
||||
$mailable
|
||||
->recipients([$user])
|
||||
->from($subscriptionEmail, $subscriptionName)
|
||||
->subject($template->getLocalizedData('subject'))
|
||||
->body($template->getLocalizedData('body'));
|
||||
|
||||
return $mailable;
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\form\SubscriptionForm', '\SubscriptionForm');
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/form/SubscriptionPolicyForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class SubscriptionPolicyForm
|
||||
*
|
||||
* @ingroup manager_form
|
||||
*
|
||||
* @brief Form for managers to setup subscription policies.
|
||||
*/
|
||||
|
||||
namespace APP\subscription\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\journal\JournalDAO;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\config\Config;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_MONTHS_MIN', '1');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_MONTHS_MAX', '12');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_WEEKS_MIN', '1');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_WEEKS_MAX', '3');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_AFTER_MONTHS_MIN', '1');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_AFTER_MONTHS_MAX', '12');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_AFTER_WEEKS_MIN', '1');
|
||||
define('SUBSCRIPTION_EXPIRY_REMINDER_AFTER_WEEKS_MAX', '3');
|
||||
|
||||
class SubscriptionPolicyForm extends Form
|
||||
{
|
||||
/** @var array keys are valid expiry reminder months */
|
||||
public $validNumMonthsBeforeExpiry;
|
||||
|
||||
/** @var array keys are valid expiry reminder weeks */
|
||||
public $validNumWeeksBeforeExpiry;
|
||||
|
||||
/** @var array keys are valid expiry reminder months */
|
||||
public $validNumMonthsAfterExpiry;
|
||||
|
||||
/** @var array keys are valid expiry reminder weeks */
|
||||
public $validNumWeeksAfterExpiry;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->validNumMonthsBeforeExpiry = [0 => __('common.disabled')];
|
||||
for ($i = SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_MONTHS_MIN; $i <= SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_MONTHS_MAX; $i++) {
|
||||
$this->validNumMonthsBeforeExpiry[$i] = __('manager.subscriptionPolicies.xMonths', ['x' => $i]);
|
||||
}
|
||||
|
||||
$this->validNumWeeksBeforeExpiry = [0 => __('common.disabled')];
|
||||
for ($i = SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_WEEKS_MIN; $i <= SUBSCRIPTION_EXPIRY_REMINDER_BEFORE_WEEKS_MAX; $i++) {
|
||||
$this->validNumWeeksBeforeExpiry[$i] = __('manager.subscriptionPolicies.xWeeks', ['x' => $i]);
|
||||
}
|
||||
|
||||
$this->validNumMonthsAfterExpiry = [0 => __('common.disabled')];
|
||||
for ($i = SUBSCRIPTION_EXPIRY_REMINDER_AFTER_MONTHS_MIN; $i <= SUBSCRIPTION_EXPIRY_REMINDER_AFTER_MONTHS_MAX; $i++) {
|
||||
$this->validNumMonthsAfterExpiry[$i] = __('manager.subscriptionPolicies.xMonths', ['x' => $i]);
|
||||
}
|
||||
|
||||
$this->validNumWeeksAfterExpiry = [0 => __('common.disabled')];
|
||||
for ($i = SUBSCRIPTION_EXPIRY_REMINDER_AFTER_WEEKS_MIN; $i <= SUBSCRIPTION_EXPIRY_REMINDER_AFTER_WEEKS_MAX; $i++) {
|
||||
$this->validNumWeeksAfterExpiry[$i] = __('manager.subscriptionPolicies.xWeeks', ['x' => $i]);
|
||||
}
|
||||
|
||||
parent::__construct('payments/subscriptionPolicyForm.tpl');
|
||||
|
||||
// If provided, subscription contact email is valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorEmail($this, 'subscriptionEmail', 'optional', 'manager.subscriptionPolicies.subscriptionContactEmailValid'));
|
||||
|
||||
// If provided expiry reminder months before value is valid value
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorInSet($this, 'numMonthsBeforeSubscriptionExpiryReminder', 'optional', 'manager.subscriptionPolicies.numMonthsBeforeSubscriptionExpiryReminderValid', array_keys($this->validNumMonthsBeforeExpiry)));
|
||||
|
||||
// If provided expiry reminder weeks before value is valid value
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorInSet($this, 'numWeeksBeforeSubscriptionExpiryReminder', 'optional', 'manager.subscriptionPolicies.numWeeksBeforeSubscriptionExpiryReminderValid', array_keys($this->validNumWeeksBeforeExpiry)));
|
||||
|
||||
// If provided expiry reminder months after value is valid value
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorInSet($this, 'numMonthsAfterSubscriptionExpiryReminder', 'optional', 'manager.subscriptionPolicies.numMonthsAfterSubscriptionExpiryReminderValid', array_keys($this->validNumMonthsAfterExpiry)));
|
||||
|
||||
// If provided expiry reminder weeks after value is valid value
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorInSet($this, 'numWeeksAfterSubscriptionExpiryReminder', 'optional', 'manager.subscriptionPolicies.numWeeksAfterSubscriptionExpiryReminderValid', array_keys($this->validNumWeeksAfterExpiry)));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::fetch()
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
$paymentManager = Application::getPaymentManager($request->getContext());
|
||||
$templateMgr = TemplateManager::getManager();
|
||||
$templateMgr->assign([
|
||||
'validNumMonthsBeforeExpiry' => $this->validNumMonthsBeforeExpiry,
|
||||
'validNumWeeksBeforeExpiry' => $this->validNumWeeksBeforeExpiry,
|
||||
'validNumMonthsAfterExpiry' => $this->validNumMonthsAfterExpiry,
|
||||
'validNumWeeksAfterExpiry' => $this->validNumWeeksAfterExpiry,
|
||||
'scheduledTasksEnabled' => (bool) Config::getVar('general', 'scheduled_tasks'),
|
||||
'paymentsEnabled' => $paymentManager->isConfigured(),
|
||||
]);
|
||||
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current subscription policies.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
$journal = $request->getJournal();
|
||||
$this->_data = [
|
||||
'subscriptionName' => $journal->getData('subscriptionName'),
|
||||
'subscriptionEmail' => $journal->getData('subscriptionEmail'),
|
||||
'subscriptionPhone' => $journal->getData('subscriptionPhone'),
|
||||
'subscriptionMailingAddress' => $journal->getData('subscriptionMailingAddress'),
|
||||
'subscriptionAdditionalInformation' => $journal->getData('subscriptionAdditionalInformation'),
|
||||
'enableOpenAccessNotification' => $journal->getData('enableOpenAccessNotification'),
|
||||
'subscriptionExpiryPartial' => $journal->getData('subscriptionExpiryPartial'),
|
||||
'enableSubscriptionOnlinePaymentNotificationPurchaseIndividual' => $journal->getData('enableSubscriptionOnlinePaymentNotificationPurchaseIndividual'),
|
||||
'enableSubscriptionOnlinePaymentNotificationPurchaseInstitutional' => $journal->getData('enableSubscriptionOnlinePaymentNotificationPurchaseInstitutional'),
|
||||
'enableSubscriptionOnlinePaymentNotificationRenewIndividual' => $journal->getData('enableSubscriptionOnlinePaymentNotificationRenewIndividual'),
|
||||
'enableSubscriptionOnlinePaymentNotificationRenewInstitutional' => $journal->getData('enableSubscriptionOnlinePaymentNotificationRenewInstitutional'),
|
||||
'numMonthsBeforeSubscriptionExpiryReminder' => $journal->getData('numMonthsBeforeSubscriptionExpiryReminder'),
|
||||
'numWeeksBeforeSubscriptionExpiryReminder' => $journal->getData('numWeeksBeforeSubscriptionExpiryReminder'),
|
||||
'numMonthsAfterSubscriptionExpiryReminder' => $journal->getData('numMonthsAfterSubscriptionExpiryReminder'),
|
||||
'numWeeksAfterSubscriptionExpiryReminder' => $journal->getData('numWeeksAfterSubscriptionExpiryReminder'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['subscriptionName', 'subscriptionEmail', 'subscriptionPhone', 'subscriptionMailingAddress', 'subscriptionAdditionalInformation', 'enableOpenAccessNotification', 'subscriptionExpiryPartial', 'enableSubscriptionOnlinePaymentNotificationPurchaseIndividual', 'enableSubscriptionOnlinePaymentNotificationPurchaseInstitutional', 'enableSubscriptionOnlinePaymentNotificationRenewIndividual', 'enableSubscriptionOnlinePaymentNotificationRenewInstitutional', 'numMonthsBeforeSubscriptionExpiryReminder', 'numWeeksBeforeSubscriptionExpiryReminder', 'numWeeksAfterSubscriptionExpiryReminder', 'numMonthsAfterSubscriptionExpiryReminder']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the names of the fields for which localized settings are used
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLocaleFieldNames()
|
||||
{
|
||||
return ['subscriptionAdditionalInformation'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
$journal = $request->getJournal();
|
||||
|
||||
$journal->setData('subscriptionName', $this->getData('subscriptionName'));
|
||||
$journal->setData('subscriptionEmail', $this->getData('subscriptionEmail'));
|
||||
$journal->setData('subscriptionPhone', $this->getData('subscriptionPhone'));
|
||||
$journal->setData('subscriptionMailingAddress', $this->getData('subscriptionMailingAddress'));
|
||||
$journal->setData('subscriptionAdditionalInformation', $this->getData('subscriptionAdditionalInformation')); // Localized
|
||||
$journal->setData('enableOpenAccessNotification', $this->getData('enableOpenAccessNotification') == null ? 0 : $this->getData('enableOpenAccessNotification'));
|
||||
$journal->setData('subscriptionExpiryPartial', $this->getData('subscriptionExpiryPartial') == null ? 0 : $this->getData('subscriptionExpiryPartial'));
|
||||
$journal->setData('enableSubscriptionOnlinePaymentNotificationPurchaseIndividual', $this->getData('enableSubscriptionOnlinePaymentNotificationPurchaseIndividual') == null ? 0 : $this->getData('enableSubscriptionOnlinePaymentNotificationPurchaseIndividual'));
|
||||
$journal->setData('enableSubscriptionOnlinePaymentNotificationPurchaseInstitutional', $this->getData('enableSubscriptionOnlinePaymentNotificationPurchaseInstitutional') == null ? 0 : $this->getData('enableSubscriptionOnlinePaymentNotificationPurchaseInstitutional'));
|
||||
$journal->setData('enableSubscriptionOnlinePaymentNotificationRenewIndividual', $this->getData('enableSubscriptionOnlinePaymentNotificationRenewIndividual') == null ? 0 : $this->getData('enableSubscriptionOnlinePaymentNotificationRenewIndividual'));
|
||||
$journal->setData('enableSubscriptionOnlinePaymentNotificationRenewInstitutional', $this->getData('enableSubscriptionOnlinePaymentNotificationRenewInstitutional') == null ? 0 : $this->getData('enableSubscriptionOnlinePaymentNotificationRenewInstitutional'));
|
||||
$journal->setData('numMonthsBeforeSubscriptionExpiryReminder', $this->getData('numMonthsBeforeSubscriptionExpiryReminder'));
|
||||
$journal->setData('numWeeksBeforeSubscriptionExpiryReminder', $this->getData('numWeeksBeforeSubscriptionExpiryReminder'));
|
||||
$journal->setData('numMonthsAfterSubscriptionExpiryReminder', $this->getData('numMonthsAfterSubscriptionExpiryReminder'));
|
||||
$journal->setData('numWeeksAfterSubscriptionExpiryReminder', $this->getData('numWeeksAfterSubscriptionExpiryReminder'));
|
||||
|
||||
parent::execute(...$functionArgs);
|
||||
|
||||
$journalDao = DAORegistry::getDAO('JournalDAO'); /** @var JournalDAO $journalDao */
|
||||
$journalDao->updateObject($journal);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\form\SubscriptionPolicyForm', '\SubscriptionPolicyForm');
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/form/UserIndividualSubscriptionForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserIndividualSubscriptionForm
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @brief Form class for user purchase of individual subscription.
|
||||
*/
|
||||
|
||||
namespace APP\subscription\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\core\Request;
|
||||
use APP\payment\ojs\OJSPaymentManager;
|
||||
use APP\subscription\IndividualSubscription;
|
||||
use APP\subscription\IndividualSubscriptionDAO;
|
||||
use APP\subscription\Subscription;
|
||||
use APP\subscription\SubscriptionTypeDAO;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\form\Form;
|
||||
|
||||
class UserIndividualSubscriptionForm extends Form
|
||||
{
|
||||
/** @var Request */
|
||||
public $request;
|
||||
|
||||
/** @var int userId int the user associated with the subscription */
|
||||
public $userId;
|
||||
|
||||
/** @var IndividualSubscription subscription the subscription being purchased */
|
||||
public $subscription;
|
||||
|
||||
/** @var array Subscription types */
|
||||
public $subscriptionTypes;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $userId
|
||||
* @param int $subscriptionId
|
||||
*/
|
||||
public function __construct($request, $userId = null, $subscriptionId = null)
|
||||
{
|
||||
parent::__construct('frontend/pages/purchaseIndividualSubscription.tpl');
|
||||
|
||||
$this->userId = isset($userId) ? (int) $userId : null;
|
||||
$this->subscription = null;
|
||||
$this->request = $request;
|
||||
|
||||
$subscriptionId = isset($subscriptionId) ? (int) $subscriptionId : null;
|
||||
|
||||
if (isset($subscriptionId)) {
|
||||
$subscriptionDao = DAORegistry::getDAO('IndividualSubscriptionDAO'); /** @var IndividualSubscriptionDAO $subscriptionDao */
|
||||
if ($subscriptionDao->subscriptionExists($subscriptionId)) {
|
||||
$this->subscription = $subscriptionDao->getById($subscriptionId);
|
||||
}
|
||||
}
|
||||
|
||||
$journal = $this->request->getJournal();
|
||||
$journalId = $journal->getId();
|
||||
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionTypes = $subscriptionTypeDao->getByInstitutional($journalId, false, false);
|
||||
$this->subscriptionTypes = $subscriptionTypes->toAssociativeArray();
|
||||
|
||||
// Ensure subscription type is valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'typeId', 'required', 'user.subscriptions.form.typeIdValid', function ($typeId) use ($journalId) {
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
return $subscriptionTypeDao->subscriptionTypeExistsByTypeId($typeId, $journalId) && !$subscriptionTypeDao->getSubscriptionTypeInstitutional($typeId) && !$subscriptionTypeDao->getSubscriptionTypeDisablePublicDisplay($typeId);
|
||||
}));
|
||||
|
||||
// Ensure that user does not already have a subscription for this journal
|
||||
if (!isset($subscriptionId)) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'userId', 'required', 'user.subscriptions.form.subscriptionExists', [DAORegistry::getDAO('IndividualSubscriptionDAO'), 'subscriptionExistsByUserForJournal'], [$journalId], true));
|
||||
} else {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'userId', 'required', 'user.subscriptions.form.subscriptionExists', function ($userId) use ($journalId, $subscriptionId) {
|
||||
$subscriptionDao = DAORegistry::getDAO('IndividualSubscriptionDAO'); /** @var IndividualSubscriptionDAO $subscriptionDao */
|
||||
$checkId = $subscriptionDao->getByUserIdForJournal($userId, $journalId);
|
||||
return ($checkId == 0 || $checkId == $subscriptionId) ? true : false;
|
||||
}));
|
||||
}
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current subscription.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
if (isset($this->subscription)) {
|
||||
$subscription = $this->subscription;
|
||||
|
||||
$this->_data = [
|
||||
'typeId' => $subscription->getTypeId(),
|
||||
'membership' => $subscription->getMembership()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::display
|
||||
*
|
||||
* @param null|mixed $request
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function display($request = null, $template = null)
|
||||
{
|
||||
if (is_null($request)) {
|
||||
$request = $this->request;
|
||||
}
|
||||
$templateMgr = TemplateManager::getManager($this->request);
|
||||
$templateMgr->assign([
|
||||
'subscriptionId' => $this->subscription ? $this->subscription->getId() : null,
|
||||
'subscriptionTypes' => array_map(
|
||||
function ($subscriptionType) {
|
||||
return $subscriptionType->getLocalizedName() . ' (' . $subscriptionType->getCost() . ' ' . $subscriptionType->getCurrencyCodeAlpha() . ')';
|
||||
},
|
||||
$this->subscriptionTypes
|
||||
),
|
||||
]);
|
||||
parent::display($request, $template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['typeId', 'membership']);
|
||||
|
||||
// If subscription type requires it, membership is provided
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$needMembership = $subscriptionTypeDao->getSubscriptionTypeMembership($this->getData('typeId'));
|
||||
|
||||
if ($needMembership) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'membership', 'required', 'user.subscriptions.form.membershipRequired'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$journal = $this->request->getJournal();
|
||||
$journalId = $journal->getId();
|
||||
$typeId = $this->getData('typeId');
|
||||
$individualSubscriptionDao = DAORegistry::getDAO('IndividualSubscriptionDAO'); /** @var IndividualSubscriptionDAO $individualSubscriptionDao */
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($typeId, $journalId);
|
||||
$nonExpiring = $subscriptionType->getNonExpiring();
|
||||
$today = date('Y-m-d');
|
||||
$insert = false;
|
||||
|
||||
parent::execute(...$functionArgs);
|
||||
|
||||
if (!isset($this->subscription)) {
|
||||
$subscription = $individualSubscriptionDao->newDataObject();
|
||||
$subscription->setJournalId($journalId);
|
||||
$subscription->setUserId($this->userId);
|
||||
$subscription->setReferenceNumber(null);
|
||||
$subscription->setNotes(null);
|
||||
|
||||
$insert = true;
|
||||
} else {
|
||||
$subscription = $this->subscription;
|
||||
}
|
||||
|
||||
$paymentManager = Application::getPaymentManager($journal);
|
||||
$paymentPlugin = $paymentManager->getPaymentPlugin();
|
||||
|
||||
if ($paymentPlugin->getName() == 'ManualPayment') {
|
||||
$subscription->setStatus(Subscription::SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT);
|
||||
} else {
|
||||
$subscription->setStatus(Subscription::SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT);
|
||||
}
|
||||
|
||||
$subscription->setTypeId($typeId);
|
||||
$subscription->setMembership($this->getData('membership') ? $this->getData('membership') : null);
|
||||
$subscription->setDateStart($nonExpiring ? null : $today);
|
||||
$subscription->setDateEnd($nonExpiring ? null : $today);
|
||||
|
||||
if ($subscription->getId()) {
|
||||
$individualSubscriptionDao->updateObject($subscription);
|
||||
} else {
|
||||
$individualSubscriptionDao->insertObject($subscription);
|
||||
}
|
||||
|
||||
$queuedPayment = $paymentManager->createQueuedPayment($this->request, OJSPaymentManager::PAYMENT_TYPE_PURCHASE_SUBSCRIPTION, $this->userId, $subscription->getId(), $subscriptionType->getCost(), $subscriptionType->getCurrencyCodeAlpha());
|
||||
$paymentManager->queuePayment($queuedPayment);
|
||||
|
||||
$paymentForm = $paymentManager->getPaymentForm($queuedPayment);
|
||||
$paymentForm->display($this->request);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\form\UserIndividualSubscriptionForm', '\UserIndividualSubscriptionForm');
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/subscription/form/UserInstitutionalSubscriptionForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserInstitutionalSubscriptionForm
|
||||
*
|
||||
* @ingroup subscription
|
||||
*
|
||||
* @brief Form class for user purchase of institutional subscription.
|
||||
*/
|
||||
|
||||
namespace APP\subscription\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\core\Request;
|
||||
use APP\facades\Repo;
|
||||
use APP\payment\ojs\OJSPaymentManager;
|
||||
use APP\subscription\InstitutionalSubscription;
|
||||
use APP\subscription\InstitutionalSubscriptionDAO;
|
||||
use APP\subscription\Subscription;
|
||||
use APP\subscription\SubscriptionTypeDAO;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\core\PKPString;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\form\Form;
|
||||
use PKP\institution\Institution;
|
||||
|
||||
class UserInstitutionalSubscriptionForm extends Form
|
||||
{
|
||||
/** @var Request */
|
||||
public $request;
|
||||
|
||||
/** @var int userId the user associated with the subscription */
|
||||
public $userId;
|
||||
|
||||
/** @var InstitutionalSubscription the subscription being purchased */
|
||||
public $subscription;
|
||||
|
||||
/** @var array subscription types */
|
||||
public $subscriptionTypes;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $userId
|
||||
* @param int $subscriptionId
|
||||
*/
|
||||
public function __construct($request, $userId = null, $subscriptionId = null)
|
||||
{
|
||||
parent::__construct('frontend/pages/purchaseInstitutionalSubscription.tpl');
|
||||
|
||||
$this->userId = isset($userId) ? (int) $userId : null;
|
||||
$this->subscription = null;
|
||||
$this->request = $request;
|
||||
|
||||
$subscriptionId = isset($subscriptionId) ? (int) $subscriptionId : null;
|
||||
|
||||
if (isset($subscriptionId)) {
|
||||
$subscriptionDao = DAORegistry::getDAO('InstitutionalSubscriptionDAO'); /** @var InstitutionalSubscriptionDAO $subscriptionDao */
|
||||
if ($subscriptionDao->subscriptionExists($subscriptionId)) {
|
||||
$this->subscription = $subscriptionDao->getById($subscriptionId);
|
||||
}
|
||||
}
|
||||
|
||||
$journal = $this->request->getJournal();
|
||||
$journalId = $journal->getId();
|
||||
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$subscriptionTypes = $subscriptionTypeDao->getByInstitutional($journalId, true, false);
|
||||
$this->subscriptionTypes = $subscriptionTypes->toArray();
|
||||
|
||||
// Ensure subscription type is valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'typeId', 'required', 'user.subscriptions.form.typeIdValid', function ($typeId) use ($journalId) {
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
return $subscriptionTypeDao->subscriptionTypeExistsByTypeId($typeId, $journalId) && $subscriptionTypeDao->getSubscriptionTypeInstitutional($typeId) && !$subscriptionTypeDao->getSubscriptionTypeDisablePublicDisplay($typeId);
|
||||
}));
|
||||
|
||||
// Ensure institution name is provided
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'institutionName', 'required', 'user.subscriptions.form.institutionNameRequired'));
|
||||
|
||||
// If provided, domain is valid
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorRegExp($this, 'domain', 'optional', 'user.subscriptions.form.domainValid', '/^' .
|
||||
'[A-Z0-9]+([\-_\.][A-Z0-9]+)*' .
|
||||
'\.' .
|
||||
'[A-Z]{2,4}' .
|
||||
'$/i'));
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current subscription.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
if (isset($this->subscription)) {
|
||||
$subscription = $this->subscription;
|
||||
$institution = Repo::institution()->get($this->subscription->getInstitutionId());
|
||||
$this->_data = [
|
||||
'institutionName' => $institution->getLocalizedName(),
|
||||
'institutionMailingAddress' => $subscription->getInstitutionMailingAddress(),
|
||||
'domain' => $subscription->getDomain(),
|
||||
'ipRanges' => $institution->getIPRanges()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::display
|
||||
*
|
||||
* @param null|mixed $request
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function display($request = null, $template = null)
|
||||
{
|
||||
if (is_null($request)) {
|
||||
$request = $this->request;
|
||||
}
|
||||
$templateMgr = TemplateManager::getManager($this->request);
|
||||
$templateMgr->assign([
|
||||
'subscriptionId' => $this->subscription ? $this->subscription->getId() : null,
|
||||
'subscriptionTypes' => $this->subscriptionTypes,
|
||||
]);
|
||||
parent::display($request, $template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['typeId', 'membership', 'institutionName', 'institutionMailingAddress', 'domain', 'ipRanges']);
|
||||
|
||||
// If subscription type requires it, membership is provided
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$needMembership = $subscriptionTypeDao->getSubscriptionTypeMembership($this->getData('typeId'));
|
||||
|
||||
if ($needMembership) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'membership', 'required', 'user.subscriptions.form.membershipRequired'));
|
||||
}
|
||||
|
||||
// Check if IP range has been provided
|
||||
$ipRanges = $this->getData('ipRanges');
|
||||
$ipRangeProvided = !empty(trim($ipRanges));
|
||||
|
||||
// Domain or at least one IP range has been provided
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'domain', 'required', 'user.subscriptions.form.domainIPRangeRequired', function ($domain) use ($ipRangeProvided) {
|
||||
return ($domain != '' || $ipRangeProvided) ? true : false;
|
||||
}));
|
||||
|
||||
// If provided ensure IP ranges have IP address format; IP addresses may contain wildcards
|
||||
if ($ipRangeProvided) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'ipRanges', 'required', 'manager.subscriptions.form.ipRangeValid', function ($ipRanges) {
|
||||
foreach (explode("\r\n", trim($ipRanges)) as $ipRange) {
|
||||
if (!PKPString::regexp_match(
|
||||
'/^' .
|
||||
// IP4 address (with or w/o wildcards) or IP4 address range (with or w/o wildcards) or CIDR IP4 address
|
||||
'((([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])([.]([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])){3}((\s)*[' . Institution::IP_RANGE_RANGE . '](\s)*([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])([.]([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])){3}){0,1})|(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])([.]([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])){3}([\/](([3][0-2]{0,1})|([1-2]{0,1}[0-9])))))' .
|
||||
'$/i',
|
||||
trim($ipRange)
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$journal = $this->request->getJournal();
|
||||
$journalId = $journal->getId();
|
||||
$typeId = $this->getData('typeId');
|
||||
$subscriptionTypeDao = DAORegistry::getDAO('SubscriptionTypeDAO'); /** @var SubscriptionTypeDAO $subscriptionTypeDao */
|
||||
$institutionalSubscriptionDao = DAORegistry::getDAO('InstitutionalSubscriptionDAO'); /** @var InstitutionalSubscriptionDAO $institutionalSubscriptionDao */
|
||||
$subscriptionType = $subscriptionTypeDao->getById($typeId);
|
||||
$nonExpiring = $subscriptionType->getNonExpiring();
|
||||
$today = date('Y-m-d');
|
||||
|
||||
if (!isset($this->subscription)) {
|
||||
$subscription = $institutionalSubscriptionDao->newDataObject();
|
||||
$subscription->setJournalId($journalId);
|
||||
$subscription->setUserId($this->userId);
|
||||
$subscription->setReferenceNumber(null);
|
||||
$subscription->setNotes(null);
|
||||
} else {
|
||||
$subscription = $this->subscription;
|
||||
}
|
||||
|
||||
$paymentManager = Application::getPaymentManager($journal);
|
||||
$paymentPlugin = $paymentManager->getPaymentPlugin();
|
||||
|
||||
if ($paymentPlugin->getName() == 'ManualPayment') {
|
||||
$subscription->setStatus(Subscription::SUBSCRIPTION_STATUS_AWAITING_MANUAL_PAYMENT);
|
||||
} else {
|
||||
$subscription->setStatus(Subscription::SUBSCRIPTION_STATUS_AWAITING_ONLINE_PAYMENT);
|
||||
}
|
||||
|
||||
$subscription->setTypeId($typeId);
|
||||
$subscription->setMembership($this->getData('membership') ? $this->getData('membership') : null);
|
||||
$subscription->setDateStart($nonExpiring ? null : $today);
|
||||
$subscription->setDateEnd($nonExpiring ? null : $today);
|
||||
$subscription->setInstitutionMailingAddress($this->getData('institutionMailingAddress'));
|
||||
$subscription->setDomain($this->getData('domain'));
|
||||
|
||||
$institution = Repo::institution()->newDataObject();
|
||||
$institution->setContextId($journalId);
|
||||
$institution->setName($this->getData('institutionName'), Locale::getLocale());
|
||||
$ipRanges = $this->getData('ipRanges');
|
||||
$ipRanges = explode("\r\n", trim($ipRanges));
|
||||
$institution->setIPRanges($ipRanges);
|
||||
$institutionId = Repo::institution()->add($institution);
|
||||
$subscription->setInstitutionId($institutionId);
|
||||
|
||||
if ($subscription->getId()) {
|
||||
$institutionalSubscriptionDao->updateObject($subscription);
|
||||
} else {
|
||||
$institutionalSubscriptionDao->insertObject($subscription);
|
||||
}
|
||||
|
||||
$queuedPayment = $paymentManager->createQueuedPayment($this->request, OJSPaymentManager::PAYMENT_TYPE_PURCHASE_SUBSCRIPTION, $this->userId, $subscription->getId(), $subscriptionType->getCost(), $subscriptionType->getCurrencyCodeAlpha());
|
||||
$paymentManager->queuePayment($queuedPayment);
|
||||
|
||||
$paymentForm = $paymentManager->getPaymentForm($queuedPayment);
|
||||
$paymentForm->display($this->request);
|
||||
parent::execute(...$functionArgs);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\APP\subscription\form\UserInstitutionalSubscriptionForm', '\UserInstitutionalSubscriptionForm');
|
||||
}
|
||||
Reference in New Issue
Block a user