From 1c88ae1a9781a84fd0bdb3b198697f688455bf7b Mon Sep 17 00:00:00 2001 From: Tatevik Date: Tue, 12 May 2026 21:17:55 +0400 Subject: [PATCH 1/2] Add preselect to subscribe page --- public_html/lists/admin/spageedit.php | 34 +++++++++++++++++++++-- public_html/lists/admin/subscribelib2.php | 27 ++++++++++++++++-- public_html/lists/index.php | 18 ++++++++++-- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/public_html/lists/admin/spageedit.php b/public_html/lists/admin/spageedit.php index e1f569790..12a74a1e8 100644 --- a/public_html/lists/admin/spageedit.php +++ b/public_html/lists/admin/spageedit.php @@ -108,10 +108,21 @@ } Sql_Query(sprintf('replace into %s (id,name,data) values(%d,"attributes","%s")', $tables['subscribepage_data'], $id, $attributes)); + $preselectList = 0; + if (!empty($_POST['preselectlist'])) { + $preselectList = (int) $_POST['preselectlist']; + } if (isset($_POST['list']) && is_array($_POST['list'])) { + if (!$preselectList || !isset($_POST['list'][$preselectList])) { + $preselectList = 0; + } Sql_Query(sprintf('replace into %s (id,name,data) values(%d,"lists","%s")', $tables['subscribepage_data'], $id, implode(',', $_POST['list']))); + } else { + $preselectList = 0; } + Sql_Query(sprintf('replace into %s (id,name,data) values(%d,"preselectlist","%d")', + $tables['subscribepage_data'], $id, $preselectList)); //## Store plugin data foreach ($GLOBALS['plugins'] as $pluginname => $plugin) { @@ -144,6 +155,7 @@ $data['unsubscribesubject'] = getConfig('unsubscribesubject'); $data['htmlchoice'] = 'htmlonly'; $data['emaildoubleentry'] = 'yes'; +$data['preselectlist'] = 0; $data['rssdefault'] = 'daily'; //Leftover from the preplugin era $data['rssintro'] = s('Please indicate how often you want to receive messages'); //Leftover from the preplugin era $selected_lists = array(); @@ -382,10 +394,26 @@ } } $listsHTML .= '

'; +$preselectList = (int) $data['preselectlist']; +if (!in_array($preselectList, $selected_lists)) { + $preselectList = 0; +} +$listsHTML .= sprintf('

', + empty($preselectList) ? 'checked="checked"' : '', + s('Do not preselect any list')); while ($row = Sql_Fetch_Array($req)) { - $listsHTML .= sprintf('
%s
', - $row['id'], $row['id'], in_array($row['id'], $selected_lists) ? 'checked="checked"' : '', - stripslashes($row['name']), htmlspecialchars(stripslashes($row['description']))); + $listSelected = in_array($row['id'], $selected_lists); + $listsHTML .= sprintf( + '
%s
', + $row['id'], + $row['id'], + $listSelected ? 'checked="checked"' : '', + stripslashes($row['name']), + $row['id'], + $preselectList == $row['id'] ? 'checked="checked"' : '', + s('Preselect'), + htmlspecialchars(stripslashes($row['description'])) + ); } $listsHTML .= ''; diff --git a/public_html/lists/admin/subscribelib2.php b/public_html/lists/admin/subscribelib2.php index cc014e97a..167f7fd76 100644 --- a/public_html/lists/admin/subscribelib2.php +++ b/public_html/lists/admin/subscribelib2.php @@ -716,9 +716,10 @@ /** * @param int $userid * @param string $lists_to_show + * @param int $preselect_list_id * @return string */ -function ListAvailableLists($userid = 0, $lists_to_show = '') +function ListAvailableLists($userid = 0, $lists_to_show = '', $preselect_list_id = 0) { global $tables; if (isset($_POST['list'])) { @@ -730,6 +731,9 @@ function ListAvailableLists($userid = 0, $lists_to_show = '') } else { $list = ''; } + $hasExplicitListSelection = isset($_POST['list']) + || (!isset($_POST['subscribe']) && isset($_GET['list']) && preg_match("/^(\d+,)*\d+$/", $_GET['list'])); + $preselect_list_id = (int) $preselect_list_id; $subselect = ''; $listset = array(); $subscribed = array(); @@ -766,7 +770,14 @@ function ListAvailableLists($userid = 0, $lists_to_show = '') while ($row = Sql_fetch_array($catresult)) { - $listspercategory[] = array('id' => $row ['id'], 'name' => $row ['name'], 'description' => $row ['description'], 'active' => $row ['active'], 'category' => $row ['category']); + $listspercategory[] = array( + 'id' => $row['id'], + 'name' => $row['name'], + 'description' => $row['description'], + 'active' => $row['active'], + 'category' => $row['category'], + 'preselect' => isset($row['preselect']) ? $row['preselect'] : 0, + ); } @@ -803,6 +814,12 @@ function ListAvailableLists($userid = 0, $lists_to_show = '') if (Sql_Affected_Rows()) { $html .= 'checked="checked"'; } + } elseif (!$hasExplicitListSelection) { + if ($preselect_list_id && $preselect_list_id == $listelement['id']) { + $html .= 'checked="checked"'; + } elseif (!$preselect_list_id && !empty($listelement['preselect'])) { + $html .= 'checked="checked"'; + } } @@ -844,6 +861,12 @@ function ListAvailableLists($userid = 0, $lists_to_show = '') if (Sql_Affected_Rows()) { $html .= 'checked="checked"'; } + } elseif (!$hasExplicitListSelection) { + if ($preselect_list_id && $preselect_list_id == $row['id']) { + $html .= 'checked="checked"'; + } elseif (!$preselect_list_id && !empty($row['preselect'])) { + $html .= 'checked="checked"'; + } } $html .= " />
'; $desc = nl2br(disableJavascript(stripslashes($row['description']))); diff --git a/public_html/lists/index.php b/public_html/lists/index.php index db494c541..81b52186e 100644 --- a/public_html/lists/index.php +++ b/public_html/lists/index.php @@ -532,7 +532,11 @@ function checkEmail() } } - $html .= ListAvailableLists($userid, $GLOBALS['pagedata']['lists']); + $html .= ListAvailableLists( + $userid, + $GLOBALS['pagedata']['lists'], + isset($GLOBALS['pagedata']['preselectlist']) ? (int) $GLOBALS['pagedata']['preselectlist'] : 0 + ); if (isBlackListedID($userid)) { $html .= $GLOBALS['strYouAreBlacklisted']; } @@ -690,7 +694,11 @@ function checkGroup(name,value) $html .= $plugin->displaySubscriptionChoice($GLOBALS['pagedata']); } } - $html .= ListAvailableLists('', $GLOBALS['pagedata']['lists']); + $html .= ListAvailableLists( + '', + $GLOBALS['pagedata']['lists'], + isset($GLOBALS['pagedata']['preselectlist']) ? (int) $GLOBALS['pagedata']['preselectlist'] : 0 + ); if (empty($GLOBALS['pagedata']['button'])) { $GLOBALS['pagedata']['button'] = $GLOBALS['strSubmit']; @@ -799,7 +807,11 @@ function subscribePage2($id) $html .= formStart(); $html .= '
'.strip_tags($GLOBALS['pagedata']['intro']).''; $html .= ListAttributes2011($attributes,$attributedata,$GLOBALS['pagedata']["htmlchoice"],0,$GLOBALS['pagedata']['emaildoubleentry']); - $html .= ListAvailableLists("",$GLOBALS['pagedata']["lists"]); + $html .= ListAvailableLists( + "", + $GLOBALS['pagedata']["lists"], + isset($GLOBALS['pagedata']['preselectlist']) ? (int) $GLOBALS['pagedata']['preselectlist'] : 0 + ); if (empty($GLOBALS['pagedata']['button'])) { $GLOBALS['pagedata']['button'] = $GLOBALS['strSubmit']; From 800ec4fec4b9d05952b3d4fbd273b46cf87f7f02 Mon Sep 17 00:00:00 2001 From: Tatevik Date: Thu, 14 May 2026 12:43:34 +0400 Subject: [PATCH 2/2] Refactor list preselection logic into `shouldPreselectList` helper function --- public_html/lists/admin/subscribelib2.php | 44 ++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/public_html/lists/admin/subscribelib2.php b/public_html/lists/admin/subscribelib2.php index 167f7fd76..eb5b6a16a 100644 --- a/public_html/lists/admin/subscribelib2.php +++ b/public_html/lists/admin/subscribelib2.php @@ -713,6 +713,24 @@ } } +/** + * Determine whether a list checkbox should be preselected. + * + * @param bool $hasExplicitListSelection + * @param int $preselect_list_id + * @param int $listId + * @param mixed $rowPreselect + * @return bool + */ +function shouldPreselectList($hasExplicitListSelection, $preselect_list_id, $listId, $rowPreselect) +{ + return !$hasExplicitListSelection && + ( + ($preselect_list_id && $preselect_list_id == $listId) || + (!$preselect_list_id && !empty($rowPreselect)) + ); +} + /** * @param int $userid * @param string $lists_to_show @@ -814,12 +832,13 @@ function ListAvailableLists($userid = 0, $lists_to_show = '', $preselect_list_id if (Sql_Affected_Rows()) { $html .= 'checked="checked"'; } - } elseif (!$hasExplicitListSelection) { - if ($preselect_list_id && $preselect_list_id == $listelement['id']) { - $html .= 'checked="checked"'; - } elseif (!$preselect_list_id && !empty($listelement['preselect'])) { - $html .= 'checked="checked"'; - } + } elseif (shouldPreselectList( + $hasExplicitListSelection, + $preselect_list_id, + $listelement['id'], + $listelement['preselect'] + )) { + $html .= 'checked="checked"'; } @@ -861,12 +880,13 @@ function ListAvailableLists($userid = 0, $lists_to_show = '', $preselect_list_id if (Sql_Affected_Rows()) { $html .= 'checked="checked"'; } - } elseif (!$hasExplicitListSelection) { - if ($preselect_list_id && $preselect_list_id == $row['id']) { - $html .= 'checked="checked"'; - } elseif (!$preselect_list_id && !empty($row['preselect'])) { - $html .= 'checked="checked"'; - } + } elseif (shouldPreselectList( + $hasExplicitListSelection, + $preselect_list_id, + $row['id'], + isset($row['preselect']) ? $row['preselect'] : 0 + )) { + $html .= 'checked="checked"'; } $html .= " />
'; $desc = nl2br(disableJavascript(stripslashes($row['description'])));