Skip to content

Commit

Permalink
pkp#8333 Updated code to consider the possibility of having NULL inst…
Browse files Browse the repository at this point in the history
…ead of 0
  • Loading branch information
jonasraoni committed Jun 7, 2023
1 parent a815486 commit 0f045fc
Show file tree
Hide file tree
Showing 23 changed files with 90 additions and 91 deletions.
2 changes: 1 addition & 1 deletion classes/context/ContextDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function resequence()
$result = $this->retrieve('SELECT ' . $this->primaryKeyColumn . ' AS context_id FROM ' . $this->tableName . ' ORDER BY seq');
$i = 1;
for ($i = 1; $row = (array) $result->current(); $i += 2 && $result->next()) {
$this->update('UPDATE ' . $this->tableName . ' SET seq = ? WHERE ' . $this->primaryKeyColumn . ' = ?', [$i, $row['context_id']]);
$this->update('UPDATE ' . $this->tableName . ' SET seq = ? WHERE COALESCE(' . $this->primaryKeyColumn . ', 0) = ?', [$i, (int) $row['context_id']]);
$result->next();
$i += 2;
}
Expand Down
6 changes: 3 additions & 3 deletions classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ public function fromRow(object $row): EmailTemplate
$schema = $this->schemaService->get($this->schema);
$contextDao = Application::getContextDAO();

$supportedLocalesJson = $row->context_id === PKPApplication::CONTEXT_SITE ?
DB::table('site')->first()->supported_locales :
DB::table($contextDao->settingsTableName)
$supportedLocalesJson = (int) $row->context_id === PKPApplication::CONTEXT_SITE
? DB::table('site')->first()->supported_locales
: DB::table($contextDao->settingsTableName)
->where($contextDao->primaryKeyColumn, $row->context_id)
->where('setting_name', 'supportedLocales')
->value('setting_value');
Expand Down
52 changes: 26 additions & 26 deletions classes/filter/FilterDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ class FilterDAO extends \PKP\db\DAO
* @param array $settings key-value pairs that can be directly written
* via \PKP\core\DataObject::setData().
* @param bool $asTemplate
* @param int $contextId the context the filter should be installed into
* @param ?int $contextId the context the filter should be installed into
* @param array $subFilters sub-filters (only allowed when the filter is a CompositeFilter)
* @param bool $persist whether to actually persist the filter
*
* @return PersistableFilter|boolean the new filter if installation successful, otherwise 'false'.
*/
public function configureObject($filterClassName, $filterGroupSymbolic, $settings = [], $asTemplate = false, $contextId = 0, $subFilters = [], $persist = true)
public function configureObject($filterClassName, $filterGroupSymbolic, $settings = [], $asTemplate = false, $contextId = null, $subFilters = [], $persist = true)
{
// Retrieve the filter group from the database.
$filterGroupDao = DAORegistry::getDAO('FilterGroupDAO'); /** @var FilterGroupDAO $filterGroupDao */
Expand Down Expand Up @@ -127,11 +127,11 @@ public function configureObject($filterClassName, $filterGroupSymbolic, $setting
* Insert a new filter instance (transformation).
*
* @param PersistableFilter $filter The configured filter instance to be persisted
* @param int $contextId
* @param ?int $contextId
*
* @return int the new filter id
*/
public function insertObject($filter, $contextId = \PKP\core\PKPApplication::CONTEXT_ID_NONE)
public function insertObject($filter, $contextId = null)
{
$filterGroup = $filter->getFilterGroup();
assert($filterGroup->getSymbolic() != FILTER_GROUP_TEMPORARY_ONLY);
Expand All @@ -141,12 +141,12 @@ public function insertObject($filter, $contextId = \PKP\core\PKPApplication::CON
(filter_group_id, context_id, display_name, class_name, is_template, parent_filter_id, seq)
VALUES (?, ?, ?, ?, ?, ?, ?)'),
[
(int) $filterGroup->getId(),
(int) $contextId,
(int) $filterGroup->getId() ?: null,
(int) $contextId ?: null,
$filter->getDisplayName(),
$filter->getClassName(),
$filter->getIsTemplate() ? 1 : 0,
(int) $filter->getParentFilterId(),
(int) $filter->getParentFilterId() ?: null,
(int) $filter->getSequence()
]
);
Expand Down Expand Up @@ -187,7 +187,7 @@ public function getObjectById($filterId, $allowSubfilter = false)
{
$result = $this->retrieve(
'SELECT * FROM filters
WHERE ' . ($allowSubfilter ? '' : 'parent_filter_id = 0 AND ') . '
WHERE ' . ($allowSubfilter ? '' : 'parent_filter_id IS NULL AND ') . '
filter_id = ?',
[(int) $filterId]
);
Expand All @@ -200,20 +200,20 @@ public function getObjectById($filterId, $allowSubfilter = false)
* (transformations) that are based on the given class.
*
* @param string $className
* @param int $contextId
* @param ?int $contextId
* @param bool $getTemplates set true if you want filter templates
* rather than actual transformations
* @param bool $allowSubfilters
*
* @return DAOResultFactory<PersistableFilter>
*/
public function getObjectsByClass($className, $contextId = \PKP\core\PKPApplication::CONTEXT_ID_NONE, $getTemplates = false, $allowSubfilters = false)
public function getObjectsByClass($className, $contextId = null, $getTemplates = false, $allowSubfilters = false)
{
$result = $this->retrieve(
'SELECT * FROM filters
WHERE context_id = ? AND
'SELECT * FROM filters
WHERE COALESCE(context_id, 0) = ? AND
LOWER(class_name) = LOWER(?) AND
' . ($allowSubfilters ? '' : ' parent_filter_id = 0 AND ') . '
' . ($allowSubfilters ? '' : ' parent_filter_id IS NULL AND ') . '
' . ($getTemplates ? ' is_template = 1' : ' is_template = 0'),
[(int) $contextId, $className]
);
Expand All @@ -228,20 +228,20 @@ public function getObjectsByClass($className, $contextId = \PKP\core\PKPApplicat
*
* @param string $groupSymbolic
* @param string $className
* @param int $contextId
* @param ?int $contextId
* @param bool $getTemplates set true if you want filter templates
* rather than actual transformations
* @param bool $allowSubfilters
*
* @return DAOResultFactory<PersistableFilter>
*/
public function getObjectsByGroupAndClass($groupSymbolic, $className, $contextId = \PKP\core\PKPApplication::CONTEXT_ID_NONE, $getTemplates = false, $allowSubfilters = false)
public function getObjectsByGroupAndClass($groupSymbolic, $className, $contextId = null, $getTemplates = false, $allowSubfilters = false)
{
$result = $this->retrieve(
'SELECT f.* FROM filters f' .
' INNER JOIN filter_groups fg ON f.filter_group_id = fg.filter_group_id' .
' WHERE fg.symbolic = ? AND f.context_id = ? AND LOWER(f.class_name) = LOWER(?)' .
' ' . ($allowSubfilters ? '' : 'AND f.parent_filter_id = 0') .
' WHERE fg.symbolic = ? AND COALESCE(f.context_id, 0) = ? AND LOWER(f.class_name) = LOWER(?)' .
' ' . ($allowSubfilters ? '' : 'AND f.parent_filter_id IS NULL') .
' AND ' . ($getTemplates ? 'f.is_template = 1' : 'f.is_template = 0'),
[$groupSymbolic, (int) $contextId, $className]
);
Expand Down Expand Up @@ -280,7 +280,7 @@ public function getObjectsByTypeDescription($inputTypeDescription, $outputTypeDe
' INNER JOIN filter_groups fg ON f.filter_group_id = fg.filter_group_id' .
' WHERE LOWER(fg.input_type) LIKE LOWER(?)' .
' AND LOWER(fg.output_type) LIKE LOWER(?)' .
' AND f.parent_filter_id = 0 AND f.is_template = 0',
' AND f.parent_filter_id IS NULL AND f.is_template = 0',
[$inputTypeDescription, $outputTypeDescription]
);

Expand Down Expand Up @@ -325,7 +325,7 @@ public function getObjectsByTypeDescription($inputTypeDescription, $outputTypeDe
* will be returned when $checkRuntimeEnvironment is set to 'true'.
*
* @param string $groupSymbolic
* @param int $contextId returns filters from context 0 and
* @param ?int $contextId returns filters from context 0 and
* the given filters of all contexts if set to null
* @param bool $getTemplates set true if you want filter templates
* rather than actual transformations
Expand All @@ -334,15 +334,15 @@ public function getObjectsByTypeDescription($inputTypeDescription, $outputTypeDe
*
* @return array filter instances (transformations) in the given group
*/
public function getObjectsByGroup($groupSymbolic, $contextId = \PKP\core\PKPApplication::CONTEXT_ID_NONE, $getTemplates = false, $checkRuntimeEnvironment = true)
public function getObjectsByGroup($groupSymbolic, $contextId = null, $getTemplates = false, $checkRuntimeEnvironment = true)
{
// 1) Get all available transformations in the group.
$result = $this->retrieve(
'SELECT f.* FROM filters f' .
' INNER JOIN filter_groups fg ON f.filter_group_id = fg.filter_group_id' .
' WHERE LOWER(fg.symbolic) = LOWER(?) AND ' . ($getTemplates ? 'f.is_template = 1' : 'f.is_template = 0') .
' ' . ($contextId ? 'AND f.context_id in (0, ' . (int)$contextId . ')' : '') .
' AND f.parent_filter_id = 0',
' ' . ($contextId ? 'AND COALESCE(f.context_id, 0) IN (0, ' . (int)$contextId . ')' : '') .
' AND f.parent_filter_id IS NULL',
[$groupSymbolic]
);

Expand Down Expand Up @@ -381,11 +381,11 @@ class_name = ?,
seq = ?
WHERE filter_id = ?',
[
(int) $filterGroup->getId(),
(int) $filterGroup->getId() ?: null,
$filter->getDisplayName(),
$filter->getClassName(),
$filter->getIsTemplate() ? 1 : 0,
(int) $filter->getParentFilterId(),
(int) $filter->getParentFilterId() ?: null,
(int) $filter->getSequence(),
(int) $filter->getId()
]
Expand Down Expand Up @@ -541,13 +541,13 @@ public function _fromRow($row)
$lockedFilters[$filterId] = true;

// Instantiate the filter.
$filter = $this->_newDataObject($row['class_name'], (int)$row['filter_group_id']);
$filter = $this->_newDataObject($row['class_name'], (int)$row['filter_group_id'] ?: null);

// Configure the filter instance
$filter->setId((int)$row['filter_id']);
$filter->setDisplayName($row['display_name']);
$filter->setIsTemplate((bool)$row['is_template']);
$filter->setParentFilterId((int)$row['parent_filter_id']);
$filter->setParentFilterId((int) $row['parent_filter_id'] ?: null);
$filter->setSequence((int)$row['seq']);
$this->getDataObjectSettings('filter_settings', 'filter_id', $row['filter_id'], $filter);

Expand Down
4 changes: 2 additions & 2 deletions classes/filter/FilterGroupDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function getObjectById($filterGroupId)
{
$result = $this->retrieve(
'SELECT * FROM filter_groups' .
' WHERE filter_group_id = ?',
[$filterGroupId]
' WHERE COALESCE(filter_group_id, 0) = ?',
[(int) $filterGroupId]
);
$row = (array) $result->current();
return $row ? $this->_fromRow($row) : null;
Expand Down
4 changes: 2 additions & 2 deletions classes/filter/PersistableFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getIsTemplate()
/**
* Set the parent filter id
*
* @param int $parentFilterId
* @param ?int $parentFilterId
*/
public function setParentFilterId($parentFilterId)
{
Expand All @@ -136,7 +136,7 @@ public function setParentFilterId($parentFilterId)
/**
* Get the parent filter id
*
* @return int
* @return ?int
*/
public function getParentFilterId()
{
Expand Down
2 changes: 1 addition & 1 deletion classes/log/EmailLogDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function insertObject($entry)
$this->datetimeToDB($entry->getDateSent())
),
[
$entry->getSenderId(),
$entry->getSenderId() ?: null,
$entry->getEventType(),
$entry->getAssocType(),
$entry->getAssocId(),
Expand Down
14 changes: 7 additions & 7 deletions classes/navigationMenu/NavigationMenuDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getById($navigationMenuId, $contextId = null)
}
$result = $this->retrieve(
'SELECT * FROM navigation_menus WHERE navigation_menu_id = ?' .
($contextId !== null ? ' AND context_id = ?' : ''),
($contextId !== null ? ' AND COALESCE(context_id, 0) = ?' : ''),
$params
);

Expand All @@ -71,7 +71,7 @@ public function getById($navigationMenuId, $contextId = null)
*/
public function getByContextId($contextId)
{
$result = $this->retrieve('SELECT * FROM navigation_menus WHERE context_id = ?', [(int) $contextId]);
$result = $this->retrieve('SELECT * FROM navigation_menus WHERE COALESCE(context_id, 0) = ?', [(int) $contextId]);
return new DAOResultFactory($result, $this, '_fromRow');
}

Expand All @@ -85,7 +85,7 @@ public function getByContextId($contextId)
*/
public function getByArea($contextId, $areaName)
{
$result = $this->retrieve('SELECT * FROM navigation_menus WHERE area_name = ? and context_id = ?', [$areaName, (int) $contextId]);
$result = $this->retrieve('SELECT * FROM navigation_menus WHERE area_name = ? AND COALESCE(context_id, 0) = ?', [$areaName, (int) $contextId]);
return new DAOResultFactory($result, $this, '_fromRow');
}

Expand All @@ -99,7 +99,7 @@ public function getByArea($contextId, $areaName)
*/
public function getByTitle($contextId, $title)
{
$result = $this->retrieve('SELECT * FROM navigation_menus WHERE context_id = ? and title = ?', [(int) $contextId, $title]);
$result = $this->retrieve('SELECT * FROM navigation_menus WHERE COALESCE(context_id, 0) = ? AND title = ?', [(int) $contextId, $title]);
$row = (array) $result->current();
return $row ? $this->_fromRow($row) : null;
}
Expand All @@ -114,7 +114,7 @@ public function getByTitle($contextId, $title)
*/
public function navigationMenuExistsByTitle($contextId, $title)
{
$result = $this->retrieve('SELECT COUNT(*) AS row_count FROM navigation_menus WHERE title = ? AND context_id = ?', [$title, (int) $contextId]);
$result = $this->retrieve('SELECT COUNT(*) AS row_count FROM navigation_menus WHERE title = ? AND COALESCE(context_id, 0) = ?', [$title, (int) $contextId]);
$row = (array) $result->current();
return $row && $row['row_count'] != 0;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public function insertObject($navigationMenu)
{
$this->update(
'INSERT INTO navigation_menus (title, area_name, context_id) VALUES (?, ?, ?)',
[$navigationMenu->getTitle(), $navigationMenu->getAreaName(), (int) $navigationMenu->getContextId()]
[$navigationMenu->getTitle(), $navigationMenu->getAreaName(), (int) $navigationMenu->getContextId() ?: null]
);
$navigationMenu->setId($this->getInsertId());
return $navigationMenu->getId();
Expand All @@ -182,7 +182,7 @@ public function updateObject($navigationMenu)
[
$navigationMenu->getTitle(),
$navigationMenu->getAreaName(),
(int) $navigationMenu->getContextId(),
(int) $navigationMenu->getContextId() ?: null,
(int) $navigationMenu->getId(),
]
);
Expand Down
10 changes: 5 additions & 5 deletions classes/navigationMenu/NavigationMenuItemAssignmentDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public function getByMenuIdAndParentId($menuId, $parentId)
{
$result = $this->retrieve(
'SELECT nmh.*
FROM navigation_menu_item_assignments as nmh
WHERE nmh.navigation_menu_id = ?
AND nmh.parent_id = ?',
FROM navigation_menu_item_assignments as nmh
WHERE nmh.navigation_menu_id = ?
AND COALESCE(nmh.parent_id, 0) = ?',
[(int) $menuId, (int) $parentId]
);
return new DAOResultFactory($result, $this, '_fromRow');
Expand Down Expand Up @@ -180,7 +180,7 @@ public function updateObject($navigationMenuItemAssignment)
[
(int) $navigationMenuItemAssignment->getMenuId(),
(int) $navigationMenuItemAssignment->getMenuItemId(),
(int) $navigationMenuItemAssignment->getParentId(),
(int) $navigationMenuItemAssignment->getParentId() ?: null,
(int) $navigationMenuItemAssignment->getSequence(),
(int) $navigationMenuItemAssignment->getId(),
]
Expand All @@ -207,7 +207,7 @@ public function insertObject($assignment)
[
(int) $assignment->getMenuId(),
(int) $assignment->getMenuItemId(),
(int) $assignment->getParentId(),
(int) $assignment->getParentId() ?: null,
(int) $assignment->getSequence(),
]
);
Expand Down
20 changes: 10 additions & 10 deletions classes/navigationMenu/NavigationMenuItemDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getById($navigationMenuItemId)
public function getByPath($contextId, $path)
{
$result = $this->retrieve(
'SELECT * FROM navigation_menu_items WHERE path = ? and context_id = ? and type= ?',
'SELECT * FROM navigation_menu_items WHERE path = ? AND COALESCE(context_id, 0) = ? AND type = ?',
[$path, (int) $contextId, 'NMI_TYPE_CUSTOM']
);

Expand All @@ -74,7 +74,7 @@ public function getByPath($contextId, $path)
public function getByContextId($contextId)
{
$result = $this->retrieve(
'SELECT * FROM navigation_menu_items WHERE context_id = ?',
'SELECT * FROM navigation_menu_items WHERE COALESCE(context_id, 0) = ?',
[(int) $contextId]
);

Expand Down Expand Up @@ -114,11 +114,11 @@ public function getByTypeAndTitleLocaleKey($contextId, $menuItemType, $menuItemT
{
$result = $this->retrieve(
'SELECT *
FROM navigation_menu_items
LEFT JOIN navigation_menu_item_settings ON (navigation_menu_items.navigation_menu_item_id = navigation_menu_item_settings.navigation_menu_item_id)
WHERE navigation_menu_items.type = ?
AND (navigation_menu_item_settings.setting_name = \'titleLocaleKey\' and navigation_menu_item_settings.setting_value = ?)
AND navigation_menu_items.context_id = ?',
FROM navigation_menu_items
LEFT JOIN navigation_menu_item_settings ON (navigation_menu_items.navigation_menu_item_id = navigation_menu_item_settings.navigation_menu_item_id)
WHERE navigation_menu_items.type = ?
AND (navigation_menu_item_settings.setting_name = \'titleLocaleKey\' and navigation_menu_item_settings.setting_value = ?)
AND COALESCE(navigation_menu_items.context_id, 0) = ?',
[$menuItemType, $menuItemTitleLocaleKey, (int) $contextId]
);
$row = (array) $result->current();
Expand All @@ -141,7 +141,7 @@ public function getByType($type, $contextId = null)
}
$result = $this->retrieve(
'SELECT * FROM navigation_menu_items WHERE type = ?' .
($contextId !== null ? ' AND context_id = ?' : ''),
($contextId !== null ? ' AND COALESCE(context_id, 0) = ?' : ''),
$params
);
return new DAOResultFactory($result, $this, '_fromRow');
Expand Down Expand Up @@ -223,7 +223,7 @@ public function insertObject($navigationMenuItem)
(?, ?, ?)',
[
$navigationMenuItem->getPath(),
(int) $navigationMenuItem->getContextId(),
(int) $navigationMenuItem->getContextId() ?: null,
$navigationMenuItem->getType(),
]
);
Expand Down Expand Up @@ -253,7 +253,7 @@ public function updateObject($navigationMenuItem)
WHERE navigation_menu_item_id = ?',
[
$navigationMenuItem->getPath(),
(int) $navigationMenuItem->getContextId(),
(int) $navigationMenuItem->getContextId() ?: null,
$navigationMenuItem->getType(),
(int) $navigationMenuItem->getId(),
]
Expand Down
Loading

0 comments on commit 0f045fc

Please sign in to comment.