Skip to content

Commit

Permalink
UNC 1.0.4
Browse files Browse the repository at this point in the history
Signed-off-by: Dark❶ <dark-1@users.noreply.github.com>
  • Loading branch information
Dark❶ committed Feb 9, 2021
1 parent 0acf122 commit c337577
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 81 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"type": "phpbb-extension",
"description": "User Notification Control [UNC]",
"homepage": "https://phpbb.com/customise/db/extension/user_notification_control",
"version": "1.0.3",
"time": "2021-01-21",
"version": "1.0.4",
"time": "2021-02-08",
"keywords": [
"phpbb",
"extension",
Expand Down
3 changes: 2 additions & 1 deletion config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ services:
class: dark1\usernotificationcontrol\core\unc_helper
arguments:
- '@user'
- '@language'
- '@ext.manager'
- '@dispatcher'
- '@notification.type_collection'
- '@notification.method_collection'
- '%core.php_ext%'
Expand All @@ -25,7 +27,6 @@ services:
- '@template'
- '@user'
- '@config'
- '@dispatcher'
- '@dark1.usernotificationcontrol.unc_table'
- '@dark1.usernotificationcontrol.unc_helper'

Expand Down
75 changes: 11 additions & 64 deletions controller/acp_main.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use phpbb\template\template;
use phpbb\user;
use phpbb\config\config;
use phpbb\event\dispatcher_interface as dispatcher;
use dark1\usernotificationcontrol\core\unc_table;
use dark1\usernotificationcontrol\core\unc_helper;

Expand All @@ -31,9 +30,6 @@ class acp_main extends acp_base
/** @var config */
protected $config;

/** @var dispatcher */
protected $dispatcher;

/** @var unc_table */
protected $unc_table;

Expand All @@ -49,16 +45,14 @@ class acp_main extends acp_base
* @param template $template Template object
* @param user $user User object
* @param config config Config object
* @param dispatcher $dispatcher Dispatcher object
* @param unc_table $unc_table UNC Table object
* @param unc_helper $unc_helper UNC Helper object
*/
public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, dispatcher $dispatcher, unc_table $unc_table, unc_helper $unc_helper)
public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, unc_table $unc_table, unc_helper $unc_helper)
{
parent::__construct($language, $log, $request, $template, $user);

$this->config = $config;
$this->dispatcher = $dispatcher;
$this->unc_table = $unc_table;
$this->unc_helper = $unc_helper;
}
Expand Down Expand Up @@ -91,14 +85,17 @@ public function handle()
$this->success_form_on_submit();
}

// Add Required Lang File(s)
$this->unc_helper->add_lang();

// Get No Notify Matrix & display the Options
$notify_matrix = $this->unc_table->get_notify_method_type_matrix();
$this->output_notification_methods_types($notify_matrix);
$this->display_notification_methods_types($notify_matrix);

// Set output variables for display in the template
$this->template->assign_vars([
'UNC_ENABLE' => $this->config['dark1_unc_enable'],
'UNC_NOTICE' => $this->language->lang('ACP_UNC_NO_LANG_KEY_NOTICE', $this->get_lang_key('')),
'UNC_NOTICE' => $this->language->lang('ACP_UNC_NO_LANG_KEY_NOTICE', $this->unc_helper->get_lang_key('')),
]);
}

Expand Down Expand Up @@ -145,40 +142,8 @@ private function request_notify_method_type_matrix()
* @return void
* @access private
*/
private function output_notification_methods_types($notify_matrix)
private function display_notification_methods_types($notify_matrix)
{
$add_ext_lang = $this->unc_helper->get_lang_unc_custom();

/**
* Event to modify the similar topics template block
*
* @event dark1.usernotificationcontrol.add_ext_lang
*
* @var array add_ext_lang Array with [(string) '<vendor>/<extension>' => (string|array) '<lang>' | ['<lang1>', '<lang2>']]
*
* @since 1.0.2
*/
$vars = ['add_ext_lang'];
extract($this->dispatcher->trigger_event('dark1.usernotificationcontrol.add_ext_lang', compact($vars)));

// Add Language File(s) that are Required
$this->language->add_lang('ucp');
if (!empty($add_ext_lang))
{
foreach ($add_ext_lang as $ext => $langs)
{
$langs = is_string($langs) ? [$langs] : $langs;

if (is_array($langs))
{
foreach ($langs as $lang)
{
$this->language->add_lang((string) $lang, (string) $ext);
}
}
}
}

// Get phpBB Notification
$notification_methods = $this->unc_helper->get_subscription_methods();
$notification_types_groups = $this->unc_helper->get_subscription_types();
Expand All @@ -190,29 +155,28 @@ private function output_notification_methods_types($notify_matrix)
{
$this->template->assign_block_vars($block_method, [
'METHOD' => $method_data['id'],
'NAME' => $this->get_lang_key($method_data['lang'], true, $method_data['id']),
'NAME' => $this->unc_helper->get_lang_key($method_data['lang'], true, $method_data['id']),
]);
}

foreach ($notification_types_groups as $group => $notification_types)
{
$this->template->assign_block_vars($block_type, [
'GROUP_NAME' => $this->get_lang_key($group, true, $group),
'GROUP_NAME' => $this->unc_helper->get_lang_key($group, true, $group),
]);

foreach ($notification_types as $type => $type_data)
{
$this->template->assign_block_vars($block_type, [
'TYPE' => $type_data['id'],
'NAME' => $this->get_lang_key($type_data['lang']),
'EXPLAIN' => $this->get_lang_key($type_data['lang'] . '_EXPLAIN', false),
'NAME' => $this->unc_helper->get_lang_key($type_data['lang']),
'EXPLAIN' => $this->unc_helper->get_lang_key($type_data['lang'] . '_EXPLAIN', false),
]);

foreach ($notification_methods as $method => $method_data)
{
$this->template->assign_block_vars($block_type . '.' . $block_method, [
'METHOD' => $method_data['id'],
'NAME' => $this->language->lang($method_data['lang']),
'SUBSCRIBED' => isset($notify_matrix[$method_data['id']][$type_data['id']]) ? ($notify_matrix[$method_data['id']][$type_data['id']] ? 1 : -1) : 0 ,
]);
}
Expand All @@ -224,21 +188,4 @@ private function output_notification_methods_types($notify_matrix)
strtoupper($block_type) . '_COLS' => (count($notification_methods) * 3) + 1,
]);
}

/**
* Get all of the subscription methods
*
* @param string $lang_key
* @param bool $warn
* @param string $sub
*
* @return string Array of methods
* @access private
*/
private function get_lang_key($lang_key, $warn = true, $sub = '')
{
$warn = $warn ? ' ⚠️ ' . $this->language->lang('ACP_UNC_NO_LANG_KEY') : '';

return $this->language->is_set($lang_key) ? $this->language->lang($lang_key) : $warn . (!empty($sub) ? ' : ' . $sub : '');
}
}
78 changes: 71 additions & 7 deletions core/unc_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* @ignore
*/
use phpbb\user;
use phpbb\language\language;
use phpbb\extension\manager as ext_manager;
use phpbb\event\dispatcher_interface as dispatcher;
use phpbb\notification\type\type_interface;
use phpbb\notification\method\method_interface;
use phpbb\finder;
Expand All @@ -27,9 +29,15 @@ class unc_helper
/** @var user */
protected $user;

/** @var language */
protected $language;

/** @var ext_manager */
protected $ext_manager;

/** @var dispatcher */
protected $dispatcher;

/** @var array Notification Types */
protected $notification_types;

Expand All @@ -42,16 +50,20 @@ class unc_helper
/**
* Constructor for User Notification Control Core Table Class.
*
* @param language $language Language object
* @param user $user User object
* @param ext_manager $ext_manager phpBB Extension Manager
* @param dispatcher $dispatcher Dispatcher object
* @param array $notification_types phpBB Notification Types
* @param array $notification_methods phpBB Notification Methods
* @param string $php_ext phpBB php ext
*/
public function __construct(user $user, ext_manager $ext_manager, $notification_types, $notification_methods, $php_ext)
public function __construct(user $user, language $language, ext_manager $ext_manager, dispatcher $dispatcher, $notification_types, $notification_methods, $php_ext)
{
$this->user = $user;
$this->language = $language;
$this->ext_manager = $ext_manager;
$this->dispatcher = $dispatcher;
$this->notification_types = $notification_types;
$this->notification_methods = $notification_methods;
$this->php_ext = $php_ext;
Expand Down Expand Up @@ -138,7 +150,6 @@ public function get_subscription_types()
*/
public function get_lang_unc_custom()
{
$lang_ary = [];
$ext_name = 'dark1/usernotificationcontrol';
$ext_lang = 'lang_unc_custom';

Expand All @@ -149,17 +160,70 @@ public function get_lang_unc_custom()
->prefix($ext_lang)
->suffix('.'.$this->php_ext)
->directory("language/".$this->user->lang_name)
//->extension_directory("/language/".$this->user->lang_name)
//->core_path("language/".$this->user->lang_name)
->get_files();

// Check if exists
if (current($lang_file_path))
$lang_ary = (current($lang_file_path)) ? [$ext_name => $ext_lang] : [];

return $lang_ary;
}



/**
* Add Required Lang File(s).
*
* @return void
* @access public
*/
public function add_lang()
{
$add_ext_lang = $this->get_lang_unc_custom();

/**
* Event to modify the similar topics template block
*
* @event dark1.usernotificationcontrol.add_ext_lang
*
* @var array add_ext_lang Array with [(string) '<vendor>/<extension>' => (string|array) '<lang>' | ['<lang1>', '<lang2>']]
*
* @since 1.0.2
* @changed 1.0.4 The add_ext_lang now accepts array of lang(s)
*/
$vars = ['add_ext_lang'];
extract($this->dispatcher->trigger_event('dark1.usernotificationcontrol.add_ext_lang', compact($vars)));

// Add Language File(s) that are Required
$this->language->add_lang('ucp');
if (!empty($add_ext_lang))
{
$lang_ary = [$ext_name => $ext_lang];
foreach ($add_ext_lang as $ext => $langs)
{
$langs = is_string($langs) ? [$langs] : $langs;
foreach ($langs as $lang)
{
$this->language->add_lang((string) $lang, (string) $ext);
}
}
}
}

return $lang_ary;


/**
* Get all of the subscription methods
*
* @param string $lang_key
* @param bool $warn
* @param string $sub
*
* @return string Array of methods
* @access public
*/
public function get_lang_key($lang_key, $warn = true, $sub = '')
{
$warn = $warn ? ' ⚠️ ' . $this->language->lang('ACP_UNC_NO_LANG_KEY') : '';
return $this->language->is_set($lang_key) ? $this->language->lang($lang_key) : $warn . (!empty($sub) ? ' : ' . $sub : '');
}

}
14 changes: 7 additions & 7 deletions core/unc_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,23 @@ public function update_user_notifications_table($notify_matrix, $update)
{
if ($update)
{
foreach ($notify_matrix as $notification_method => $ary_type)
foreach ($notify_matrix as $notification_method => $notification_type_ary)
{
$sql_ary = [ 0 => '', 1 => '' ];

foreach ($ary_type as $notification_type => $value)
foreach ($notification_type_ary as $notification_type => $notify)
{
$sql_ary[$value] .= (empty($sql_ary[$value]) ? '' : ' OR ' ) . "item_type = '" . $this->db->sql_escape($notification_type) . "'";
$sql_ary[$notify] .= (empty($sql_ary[$notify]) ? '' : ' OR ' ) . "item_type = '" . $this->db->sql_escape($notification_type) . "'";
}

foreach ($sql_ary as $key => $value)
foreach ($sql_ary as $notify => $item_type_str)
{
if (!empty($value))
if (!empty($item_type_str))
{
$sql = 'UPDATE ' . USER_NOTIFICATIONS_TABLE .
' SET notify = ' . (int) $key .
' SET notify = ' . (int) $notify .
" WHERE method = '" . $this->db->sql_escape($notification_method) . "'" .
' AND (' . (string) $value . ')';
' AND (' . (string) $item_type_str . ')';
$this->db->sql_query($sql);
}
}
Expand Down

0 comments on commit c337577

Please sign in to comment.