Skip to content

Commit

Permalink
topic-descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Sep 30, 2023
1 parent c000d34 commit 8153a87
Show file tree
Hide file tree
Showing 14 changed files with 1,859 additions and 1,197 deletions.
33 changes: 0 additions & 33 deletions TopicDescriptions/english.xml

This file was deleted.

45 changes: 0 additions & 45 deletions TopicDescriptions/package-info.xml

This file was deleted.

15 changes: 0 additions & 15 deletions TopicDescriptions/uninstall_2.php

This file was deleted.

1,236 changes: 618 additions & 618 deletions TopicDescriptions/LICENSE.txt → topic-descriptions/LICENSE.txt

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions topic-descriptions/ManageTopicDescriptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

function ModifyTopicDescriptionsSettings(bool $return_config = false)
{
global $context, $txt, $sourcedir, $scripturl, $smcFunc, $modSettings;

$config_vars = array(
array('title', 'settings'),
array('check', 'topic_descriptions_enable'),
'',
array('check', 'topic_descriptions_topics'),
array('int', 'topic_descriptions_maxlen', 4),
array('title', 'admin_boards'),
array('desc', 'select_boards_from_list'),
array(isset($_GET['save']) ? 'text' : 'callback', 'topic_descriptions_boards'),
);

if ($return_config)
return $config_vars;

$boards = isset($modSettings['topic_descriptions_boards'])
? explode(',', $modSettings['topic_descriptions_boards'])
: [];

$request = $smcFunc['db_query']('', '
SELECT id_board, b.name, child_level, c.name AS cat_name, id_cat
FROM {db_prefix}boards AS b
JOIN {db_prefix}categories AS c USING (id_cat)
WHERE redirect = {string:empty_string}
ORDER BY board_order',
array(
'empty_string' => '',
)
);
$i = -1;
$a = array();
$context['categories'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (!isset($a[$row['id_cat']]))
{
$context['categories'][++$i] = array(
'id' => $row['id_cat'],
'name' => $row['cat_name'],
'boards' => []
);
$a[$row['id_cat']] = true;
}

$context['categories'][$i]['boards'][] = array(
'id' => $row['id_board'],
'name' => $row['name'],
'child_level' => (int) $row['child_level'],
'selected' => in_array($row['id_board'], $boards)
);
}
$smcFunc['db_free_result']($request);

require_once $sourcedir . '/ManageServer.php';

if (isset($_GET['save']))
{
checkSession();

if (isset($_POST['topic_descriptions_boards']) && is_array($_POST['topic_descriptions_boards']))
$_POST['topic_descriptions_boards'] = implode(',', array_filter($_POST['topic_descriptions_boards'], 'ctype_digit'));

saveDBSettings($config_vars);
$_SESSION['adm-save'] = true;
redirectexit('action=admin;area=modsettings;sa=topicdescriptions');
}

if (!defined('SMF_VERSION'))
add_integration_function('integrate_buffer', 'topic_descriptions_buffer', false);

$context['post_url'] = $scripturl . '?action=admin;area=modsettings;sa=topicdescriptions;save';
$context['page_title'] = $txt['topic_descriptions'] . ' - ' . $txt['settings'];
$context['sub_template'] = 'show_settings';
$context['html_headers'] .= '
<style>
#admin_form_wrapper fieldset > ul {
display: grid;
}
@media (min-width: 720px) {
#admin_form_wrapper fieldset > ul {
grid-template-columns: 1fr 1fr;
}
}
@media (min-width: 1200px) {
#admin_form_wrapper fieldset > ul {
grid-template-columns: 1fr 1fr 1fr;
}
}
#admin_form_wrapper ul {
padding-' . ($context['right_to_left'] ? 'right' : 'left') . ': 1em;
}
</style>';

loadTemplate('ManageTopicDescriptions');
loadLanguage('TopicDescriptions');
prepareDBSettingContext($config_vars);

if (defined('SMF_VERSION'))
{
// Two tokens because saving these settings requires both save_inline_permissions and saveDBSettings
createToken('admin-mp');
createToken('admin-dbsc');
}
}
84 changes: 84 additions & 0 deletions topic-descriptions/ManageTopicDescriptions.template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

function template_callback_topic_descriptions_boards(): void
{
global $context;

echo '
</dl>';

foreach ($context['categories'] as $category)
{
echo '
<fieldset>
<legend>
', $category['name'], '
</legend>
<ul class="reset">';

for ($i = 0, $n = count($category['boards']); $i < $n; $i++)
{
echo '
<li><label><input type="checkbox" name="topic_descriptions_boards[]" value="', $category['boards'][$i]['id'], '"', $category['boards'][$i]['selected'] ? ' checked' : '', '>', $category['boards'][$i]['name'], '</label>';

// Nest child boards inside another list.
$curr_child_level = $category['boards'][$i]['child_level'];
$next_child_level = $category['boards'][$i + 1]['child_level'] ?? 0;

if ($next_child_level > $curr_child_level)
{
echo '
<ul>';
}
else
{
// Close child board lists until we reach a common level
// with the next board.
while ($next_child_level < $curr_child_level--)
{
echo '
</li>
</ul>';
}

echo '</li>';
}
}

echo '
</ul>
</fieldset>';
}

echo '
<script>
for (const div of document.forms.admin_form_wrapper)
if (div.nodeName == "FIELDSET")
{
let allChecked = true;
for (let o of div.elements)
if (o.nodeName == "INPUT" && o.type == "checkbox")
allChecked &= o.checked;
var
a = document.createElement("legend"),
b = document.createElement("input"),
c = document.createElement("label");
b.type = "checkbox";
b.checked = allChecked;
c.appendChild(b);
c.appendChild(document.createTextNode(div.firstElementChild.textContent));
a.appendChild(c);
div.firstElementChild.replaceWith(a);
b.addEventListener("click", function(els)
{
for (const o of els)
if (o.nodeName == "INPUT" && o.type == "checkbox")
o.checked = this.checked;
}.bind(b, div.elements));
}
</script>
<dl>';
}
30 changes: 30 additions & 0 deletions topic-descriptions/Subs-TopicDescriptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

function topic_descriptions_load_theme(): void
{
loadLanguage('TopicDescriptions');
}

function topic_descriptions_admin_areas(array &$admin_areas): void
{
global $txt;

$admin_areas['config']['areas']['modsettings']['subsections']['topicdescriptions'] = array($txt['topic_descriptions']);
}

function topic_descriptions_modify_modifications(array &$sub_actions): void
{
$sub_actions['topicdescriptions'] = 'ModifyTopicDescriptionsSettings';
}

function topic_descriptions_buffer(string $buffer): string
{
return preg_replace_callback(
'/<form\K[^?]+\?action=admin;area=modsettings;sa=topicdescriptions/',
function($m)
{
return '<form id="admin_form_wrapper"' . $m[0];
},
$buffer
);
}
10 changes: 10 additions & 0 deletions topic-descriptions/TopicDescriptions.english.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$txt['topic_descriptions'] = 'Topic Descriptions';
$txt['topic_description'] = 'Description';
$txt['error_long_description'] = 'The description exceeds the maximum allowed length (%1$d characters).';
$txt['topic_descriptions_maxlen'] = 'Set max length of descriptions<div class="smalltext">Default: 25</div>';
$txt['topic_descriptions_enable'] = 'Enable Topic Descriptions?';
$txt['topic_descriptions_post_desc'] = 'Description (<i>Optional</i>):';
$txt['select_boards_from_list'] = 'Select boards which apply';
$txt['topic_descriptions_topics'] = 'Enable the description in the topic?';
52 changes: 26 additions & 26 deletions TopicDescriptions/install_2.php → topic-descriptions/install.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php

if (!defined('SMF'))
die('Hacking attempt...');

global $smcFunc;

db_extend('packages');

$smcFunc['db_add_column']("{db_prefix}topics",
array(
'name' => 'description',
'type' => 'text',
'null' => true,
), array(), 'do_nothing', 'fatal'
);

// RC5 bug aw man :(

/*$hooks = array(
'integrate_modify_modifications' => 'integrateTopicdescSettings',
);
foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);*/

<?php

if (!defined('SMF'))
die('Hacking attempt...');

global $smcFunc;

db_extend('packages');

$smcFunc['db_add_column']("{db_prefix}topics",
array(
'name' => 'description',
'type' => 'text',
'null' => true,
), array(), 'do_nothing', 'fatal'
);

// RC5 bug aw man :(

/*$hooks = array(
'integrate_modify_modifications' => 'integrateTopicdescSettings',
);
foreach ($hooks as $hook => $function)
add_integration_function($hook, $function);*/

?>
Loading

0 comments on commit 8153a87

Please sign in to comment.