1.นำโคดนี้ไปใส่ใน ext.php
โค้ด: เลือกทั้งหมด
function enable_step($old_state) {
switch ($old_state) {
case '': // Empty means nothing has run yet
// Enable board rules notifications
$phpbb_notifications = $this->container->get('notification_manager');
$phpbb_notifications->enable_notifications('notification.type.m_member_packages'); //ปรับเปลี่ยนตาม Extension ตามต้องการ
return 'notifications';
// เพิ่ม ประเภทใน การตั้งค่าของ ตั้งค่าส่วนตัว > ตั้งค่าการเตือน
break;
default:
// Run parent enable step method
return parent::enable_step($old_state);
break;
}
}
function disable_step($old_state) {
switch ($old_state) {
case '': // Empty means nothing has run yet
// Disable board rules notifications
$phpbb_notifications = $this->container->get('notification_manager');
$phpbb_notifications->disable_notifications('notification.type.m_member_packages'); //ปรับเปลี่ยนตาม Extension ตามต้องการ
return 'notifications';
break;
default:
// Run parent disable step method
return parent::disable_step($old_state);
break;
}
}
function purge_step($old_state) {
switch ($old_state) {
case '': // Empty means nothing has run yet
try {
// Purge board rules notifications
$phpbb_notifications = $this->container->get('notification_manager');
$phpbb_notifications->purge_notifications('notification.type.m_member_packages'); //ปรับเปลี่ยนตาม Extension ตามต้องการ
} catch (\phpbb\notification\exception $e) {
// continue
}
return 'notifications';
break;
default:
return parent::purge_step($old_state);
break;
}
}
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_member_packages\controller;
use Symfony\Component\DependencyInjection\Container;
class notifyhelper {
public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, Container $phpbb_container, $root_path, $php_ext) {
$this->config = $config;
$this->db = $db;
$this->request = $request;
$this->template = $template;
$this->user = $user;
$this->phpbb_container = $phpbb_container;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
}
public function test($i) {
echo '</pre>';
print_r($i);
echo '</pre>';
}
public function notify($type, $topic_id, $post_id, $post_subject, $poster_user, $liker_user, $post_username, $poster_id) {
$notification_data = array(
'topic_id' => (int) $topic_id,
'post_id' => (int) $post_id,
'post_subject' => $post_subject,
'user_id' => (int) $poster_user,
'requester_id' => (int) $liker_user,
'post_username' => $post_username,
'poster_id' => $poster_id,
);
$phpbb_notifications = $this->phpbb_container->get('notification_manager');
if ($notification_data['requester_id'] != $notification_data['user_id']) {
switch ($type) {
case 'add':
$phpbb_notifications->add_notifications('notification.type.m_member_packages', $notification_data); //ส่งค่าไปเพื่อบันทึกข้อมูลสำหรับการแจ้งเตือน
break;
case 'remove':
$phpbb_notifications->delete_notifications('notification.type.m_member_packages', $notification_data['user_id']); //
break;
}
}
}
}
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_member_packages\controller;
use Symfony\Component\DependencyInjection\Container;
class notifyhelper {
public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, Container $phpbb_container, $root_path, $php_ext) {
$this->config = $config;
$this->db = $db;
$this->request = $request;
$this->template = $template;
$this->user = $user;
$this->phpbb_container = $phpbb_container;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
}
public function test($i) {
echo '</pre>';
print_r($i);
echo '</pre>';
}
public function notify($type, $topic_id, $post_id, $post_subject, $poster_user, $liker_user, $post_username, $poster_id) {
$notification_data = array(
'topic_id' => (int) $topic_id,
'post_id' => (int) $post_id,
'post_subject' => $post_subject,
'user_id' => (int) $poster_user,
'requester_id' => (int) $liker_user,
'post_username' => $post_username,
'poster_id' => $poster_id,
);
$phpbb_notifications = $this->phpbb_container->get('notification_manager');
if ($notification_data['requester_id'] != $notification_data['user_id']) {
switch ($type) {
case 'add':
$phpbb_notifications->add_notifications('notification.type.m_member_packages', $notification_data);
break;
case 'remove':
$phpbb_notifications->delete_notifications('notification.type.m_member_packages', $notification_data['user_id']);
break;
}
}
}
}
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_member_packages\notification;
class m_member_packages extends \phpbb\notification\type\base {
protected $helper;
public function __construct(\phpbb\user_loader $user_loader, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, $user, \phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\controller\helper $helper, $phpbb_root_path, $php_ext, $notification_types_table, $notifications_table, $user_notifications_table, $table_prefix) {
$this->user_loader = $user_loader;
$this->db = $db;
$this->cache = $cache;
$this->user = $user;
$this->auth = $auth;
$this->config = $config;
$this->helper = $helper;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->notification_types_table = $notification_types_table;
$this->notifications_table = $notifications_table;
$this->user_notifications_table = $user_notifications_table;
$this->table_prefix = $table_prefix;
}
public function get_type() {
return 'notification.type.m_member_packages';
}
protected $language_key = 'NOTIFICATION_ADD_M_MEMBER_PAKAGE';
public static $notification_option = array(
'lang' => 'NOTIFICATION_TYPE_M_MEMBER_PAKAGE',
);
public function is_available() {
return true;
}
protected $user_loader;
public function set_user_loader(\phpbb\user_loader $user_loader) {
$this->user_loader = $user_loader;
}
public static function get_item_id($data) {
return $data['requester_id'];
}
public static function get_item_parent_id($data) {
return $data['post_id'];
}
public function find_users_for_notification($data, $options = array()) {
$update_notifications = array();
$sql = 'SELECT n.*
FROM ' . NOTIFICATIONS_TABLE . ' n, ' . NOTIFICATION_TYPES_TABLE . ' nt
WHERE n.notification_type_id = ' . (int) $this->notification_type_id . '
AND n.item_parent_id = ' . (int) self::get_item_parent_id($data) . '
AND n.notification_read = 0
AND nt.notification_type_id = n.notification_type_id
AND nt.notification_type_enabled = 1';
// echo $sql;
$result = $this->db->sql_query($sql);
$nofify_user = $this->db->sql_fetchrow($result);
if ($nofify_user) {
foreach ($nofify_user as $row) {
$notification = $this->notification_manager->get_item_type_class($this->get_type(), $nofify_user);
$update_responders = $notification->add_responders($data);
if (!empty($update_responders)) {
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE .
' SET ' . $this->db->sql_build_array('UPDATE', $update_responders) . '
WHERE notification_id = ' . $nofify_user['notification_id'];
$this->db->sql_query($sql);
}
}
} else {
$users = array();
$users[$data['user_id']] = array('');
$this->user_loader->load_users(array_keys($users));
return $this->check_user_notification_options(array_keys($users), $options);
}
}
public function get_avatar() {
$users = array($this->get_data('requester_id'));
$this->user_loader->load_users($users);
return $this->user_loader->get_avatar($this->get_data('requester_id'));
}
public function get_title() {
$responders = $this->get_data('responders');
$usernames = array();
if (!is_array($responders)) {
$responders = array();
}
$responders = array_merge(array(array(
'poster_id' => $this->get_data('requester_id'),
'username' => $this->get_data('post_username'),
)), $responders);
$responders_cnt = sizeof($responders);
$responders = $this->trim_user_ary($responders);
$trimmed_responders_cnt = $responders_cnt - sizeof($responders);
foreach ($responders as $responder) {
if ($responder['username']) {
$usernames[] = $responder['username'];
} else {
$usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile');
}
}
if ($trimmed_responders_cnt > 2) {
$usernames[] = $this->user->lang('NOTIFICATION_MANY_OTHERS');
} else if ($trimmed_responders_cnt) {
$usernames[] = $this->user->lang('NOTIFICATION_X_OTHERS', $trimmed_responders_cnt);
print_r($usernames);
}
return $this->user->lang(
$this->language_key, phpbb_generate_string_list($usernames, $this->user), $responders_cnt
);
}
public function get_url() {
return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t=&p=&s=#s" . $sid);
}
public function get_reference() {
return $this->user->lang(
'NOTIFICATION_ADD_LIKE_POST', censor_text($this->get_data('post_subject'))
);
}
public function get_email_template() {
return '@mindphp_m_member_packages/notification_m_member_packages_email';
}
public function get_email_template_variables() {
return array(
'USERNAME' => htmlspecialchars_decode($this->user_loader->get_username($this->get_data('poster_id'), 'username')),
'POST_TITLE' => htmlspecialchars_decode(censor_text($this->get_data('post_subject'))),
'USER_LINK' => htmlspecialchars_decode($this->user_loader->get_username($this->get_data('requester_id'), 'no_profile')),
'U_NEWEST_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->get_data('post_id')}&t={$this->item_parent_id}&e=1&view=unread#unread",
'U_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?fp={$this->get_data('post_id')}&t={$this->item_parent_id}",
);
}
public function create_insert_array($data, $pre_create_data = array()) {
$this->set_data('requester_id', $data['requester_id']);
$this->set_data('post_id', $data['post_id']);
$this->set_data('topic_id', $data['topic_id']);
$this->set_data('post_subject', $data['post_subject']);
$this->set_data('post_username', $data['post_username']);
$this->set_data('poster_id', $data['poster_id']);
print_r($data);
return parent::create_insert_array($data, $pre_create_data);
}
public function users_to_query() {
$responders = $this->get_data('responders');
$users = array(
$this->get_data('poster_id'),
);
if (is_array($responders)) {
foreach ($responders as $responder) {
$users[] = $responder['poster_id'];
}
}
return $this->trim_user_ary($users);
}
public function add_responders($data) {
// print_r($data);
// Do not add them as a responder if they were the original poster that created the notification
if ($this->get_data('poster_id') == $data['requester_id']) {
// คนที่ like ต้องไม่ตรงกับ เจ้าของโพสต์
return array();
}
$responders = $this->get_data('responders');
$responders = ($responders === null) ? array() : $responders;
if (sizeof($responders) > 25) {
return array();
}
foreach ($responders as $responder) {
// Do not add them as a responder multiple times
if ($responder['poster_id'] == $data['requester_id']) {
return array();
}
}
$responders[] = array(
'poster_id' => $data['requester_id'],
'username' => (($data['requester_id'] == ANONYMOUS) ? $data['poster_name'] : ''),
);
$this->set_data('responders', $responders);
$serialized_data = serialize($this->get_data(false));
// If the data is longer then 4000 characters, it would cause a SQL error.
// We don't add the username to the list if this is the case.
if (utf8_strlen($serialized_data) >= 4000) {
return array();
}
return array('notification_data' => $serialized_data);
}
public function trim_user_ary($users) {
if (sizeof($users) > 3) {
array_splice($users, 2);
}
return $users;
}
}
บทความที่เกี่ยวข้อง
การสร้าง Extension ใน phpbb
การสร้าง controller สำหรับ phpbb
Event listenner - เขียน Extension บน phpBB 3.1