ส่วนขยาย(Extension) จะทำให้ เว็บบอร์ด phpbb มีประสิทธิภาพ การทำงานที่ดีขึ้น โดยสามารถ ปรับเพิ่มเพิ่มแก้ไข้ ในส่วนต่างๆของ เว็บบอร์ด phpbb ได้ตามอิสระ ซึ่งในขั้นตอนพัฒนา ไม่จำเป้นที่จะต้องเข้าไปยุ่งเกี่ยว Code เดิมที่มีอยู่(แต่อาจจะมีบางครั่งที่ต้องเข้าไปแก้ไข้ เพิ่มเติม) นั้นก็ช่วยเราไม่ต้องกังวลกับการผิดพลาดที่อาจจะเกิดขึ้นได้เพราะหาก Code ของ เว็บบอร์ด phpbb เกิดความผิดพลาดก้อาจจะกระทบต่อการทำงานส่วนอื่นๆ ด้วย โดยการสร้าง Extension นั้นก็มีวิธีการพัฒนาที่ไม่ซับซ่อนมาก เพียงแต่จะมีเพียงการทำงานที่จะต้องเป็นไปตามที่เราต้องการเท่านั้น ที่จะยากนิดหน่อย
ซึ่งวันนี้เราจะนำเสนอ การสร้าง Extension ที่ใช่สำหรับการสร้างนามแฝง
ขั้นตอนการพัฒนา
1.สร้างโฟลเดอร์สำหรับเป้น ส่วนขยาย(Extension) ชือว่า m_anonymus_post
โค้ด: เลือกทั้งหมด
2.1 m_anonymus_post_info
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_anonymus_post\acp;
if (!defined('IN_PHPBB')) {
exit;
}
class m_anonymus_post_info {
function module() {
return array(
'filename' => '\mindphp\m_anonymus_post\acp\m_anonymus_post_module',
'title' => 'ACP_ANONYMUS_TITLE',
'version' => '1.0.0',
'modes' => array(
'settings' => array('title' => 'ACP_ANONYMUS_MODES_TITLE', 'auth' => 'ext_mindphp/m_anonymus_post', 'cat' => array('ANONYMUS_MOD')),
),
);
}
}
โค้ด: เลือกทั้งหมด
<?php
/**
*
* @package Share On
* @copyright (c) 2013 Vinny
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace mindphp\m_anonymus_post\acp;
class m_anonymus_post_module {
var $u_action;
function main($id, $mode) {
global $db, $user, $auth, $template, $cache, $request;
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
$user->add_lang_ext('mindphp/m_anonymus_post', 'acp/m_anonymus_post');
$this->tpl_name = 'm_anonymus_post_body';
$this->page_title = $user->lang('ACP_ANONYMUS_TITLE');
add_form_key('mindphp/m_anonymus_post');
if ($request->is_set_post('submit')) {
if (!check_form_key('mindphp/m_anonymus_post')) {
trigger_error('FORM_INVALID');
}
$config->set('m_anonymus_post_enabled', $request->variable('m_anonymus_post_enabled', 0));
$config->set('m_anonymus_post_name', $request->variable('m_anonymus_post_name', ''));
trigger_error($user->lang('ACP_ANONYMUS_SETTING_SAVED') . adm_back_link($this->u_action));
}
$template->assign_vars(array(
'U_ACTION' => $this->u_action,
'ANONYMUS_ENABLED' => $config['m_anonymus_post_enabled'],
'S_ANONYMUS_NAME' => $config['m_anonymus_post_name'],
));
}
}
3.1.สร้างโฟลเดอร์ style แล้วสร้างไฟล์ html ชื่อ alias_body
โค้ด: เลือกทั้งหมด
<!-- INCLUDE overall_header.html -->
<h1>{L_SETTINGS}</h1>
<form id="acp_board" method="post" action="{U_ACTION}">
<fieldset>
<dl>
<dt><label for="m_anonymus_post_enabled">{L_ACP_ANONYMUS_GOODBYE}</label></dt>
<dd><input type="radio" class="radio" name="m_anonymus_post_enabled" value="1" <!-- IF ANONYMUS_ENABLED -->checked="checked"<!-- ENDIF -->/> {L_YES}
<input type="radio" class="radio" name="m_anonymus_post_enabled" value="0" <!-- IF not ANONYMUS_ENABLED -->checked="checked"<!-- ENDIF --> /> {L_NO}</dd>
</dl>
<dl>
<dt><label for="m_anonymus_post_name">{L_ACP_ANONYMUS_NAME}</label><br/><span>{L_ACP_ANONYMUS_NAME_EXPLAIN}</span></dt>
<dd><input id="incognito_name" type="text" name="m_anonymus_post_name" value="{S_ANONYMUS_NAME}"/></dd>
</dl>
<p class="submit-buttons">
<input class="button1" type="submit" id="submit" name="submit" value="{L_SUBMIT}" />
<input class="button2" type="reset" id="reset" name="reset" value="{L_RESET}" />
</p>
{S_FORM_TOKEN}
</fieldset>
</form>
<!-- INCLUDE overall_footer.html -->
4.1 ชื่อว่า install_m_anonymus_post
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_anonymus_post\migrations;
class install_m_anonymus_post extends \phpbb\db\migration\migration {
public function effectively_installed() {
return isset($this->config['m_anonymus_post_version']) && version_compare($this->config['m_sub_comment'], '1.0.0', '>=');
}
static public function depends_on() {
return array('\phpbb\db\migration\data\v310\dev');
}
// สร้างคอลลัม เพื่อเก็บ id ที่จริง ของผู้ใช้งาน
public function update_schema() {
return array(
'add_columns' => array(
$this->table_prefix . 'posts' => array(
'real_poster_id' => array('UINT:8', null),
),
),
);
}
public function revert_schema() {
return array(
'drop_columns' => array(
$this->table_prefix . 'posts' => array(
'real_poster_id',
),
),
'drop_tables' => array(
$this->table_prefix . 'anonymus',
),
);
}
public function update_data() {
return array(
array('config.add', array('m_anonymus_post_enabled', 1)),
array('module.add', array(
'acp',
'ACP_CAT_DOT_MODS',
'ACP_ANONYMUS_TITLE'
)),
array('module.add', array(
'acp',
'ACP_ANONYMUS_TITLE',
array(
'module_basename' => '\mindphp\m_anonymus_post\acp\m_anonymus_post_module',
'modes' => array('settings'),
),
)),
);
}
}
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_anonymus_post\migrations;
class install_m_anonymus_post_1 extends \phpbb\db\migration\migration {
public function effectively_installed() {
return isset($this->config['m_anonymus_post_name']);
}
static public function depends_on() {
return array('\mindphp\m_anonymus_post\migrations\install_m_anonymus_postt_0');
}
public function update_data() {
return array(
array('config.add', array('m_anonymus_post_name', '')),
);
}
}
5.1 สร้างโฟลเดอร์ en แล้วสร้างไฟล์ html ชือว่า m_anonymus_post
โค้ด: เลือกทั้งหมด
<?php
/**
*
* @package phpBB Extension - Incognito
* @copyright (c) 2015 steveurkel
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
if (!defined('IN_PHPBB')) {
exit;
}
if (empty($lang) || !is_array($lang)) {
$lang = array();
}
$lang = array_merge($lang, array(
'ANONYMUS' => 'Post as anonymous',
'ANONYMUS_EXPLAIN' => 'Check this field if you want to post anonymous. This cannot be reverted once the post has been sent.',
'ACP_ANONYMUS_TITLE' => 'Anonymus management menu ',
'ACP_ANONYMUS_MODES_TITLE' => 'Setting using',
'ACP_ANONYMUS_GOODBYE' => 'Post as anonymous active?',
'ACP_ANONYMUS_SETTING_SAVED' => 'Settings have been saved successfully!',
'ACP_ANONYMUS_NAME' => 'Displayed username',
'ACP_ANONYMUS_NAME_EXPLAIN' => 'Entered value will be shown as username for post. When left empty the standard value will be shown.',
));
โค้ด: เลือกทั้งหมด
<?php
/**
*
* @package phpBB Extension - Incognito
* @copyright (c) 2015 steveurkel
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
if (!defined('IN_PHPBB')) {
exit;
}
if (empty($lang) || !is_array($lang)) {
$lang = array();
}
$lang = array_merge($lang, array(
'ANONYMUS' => 'Post as anonymous',
'ANONYMUS_EXPLAIN' => 'Check this field if you want to post anonymous. This cannot be reverted once the post has been sent.',
'ACP_ANONYMUS_TITLE' => 'Anonymus management menu ',
'ACP_ANONYMUS_MODES_TITLE' => 'Setting using',
'ACP_ANONYMUS_GOODBYE' => 'Post as anonymous active?',
'ACP_ANONYMUS_SETTING_SAVED' => 'Settings have been saved successfully!',
'ACP_ANONYMUS_NAME' => 'Displayed username',
'ACP_ANONYMUS_NAME_EXPLAIN' => 'Entered value will be shown as username for post. When left empty the standard value will be shown.',
));
โค้ด: เลือกทั้งหมด
<?php
/**
*
* @package phpBB Extension - Incognito
* @copyright (c) 2015 steveurkel
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
if (!defined('IN_PHPBB')) {
exit;
}
if (empty($lang) || !is_array($lang)) {
$lang = array();
}
$lang = array_merge($lang, array(
'ANONYMUS' => 'โพสต์ที่ไม่ระบุชื่อ',
'ANONYMUS_EXPLAIN' => 'ตรวจสอบข้อมูลนี้ถ้าคุณต้องการให้โพสต์ที่ไม่ระบุชื่อ นี้ไม่สามารถหวนกลับโพสต์ครั้งหนึ่งเคยได้รับการส่ง',
'ACP_ANONYMUS_TITLE' => 'เมนูการจัดการนามแฝง',
'ACP_ANONYMUS_MODES_TITLE' => 'ตั้งค่าการใช้งาน',
'ACP_ANONYMUS_GOODBYE' => 'โพสต์ที่ไม่ระบุชื่อที่ใช้งาน?',
'ACP_ANONYMUS_SETTING_SAVED' => 'การตั้งค่าได้รับการบันทึกเรียบร้อยแล้ว!',
'ACP_ANONYMUS_NAME' => 'แสดงชื่อผู้ใช้',
'ACP_ANONYMUS_NAME_EXPLAIN' => 'ค่าที่ป้อนจะแสดงเป็นชื่อผู้โพสต์ เมื่อเว้นว่างค่ามาตรฐานจะแสดง',
));
โค้ด: เลือกทั้งหมด
<?php
/**
*
* @package phpBB Extension - Incognito
* @copyright (c) 2015 steveurkel
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
if (!defined('IN_PHPBB')) {
exit;
}
if (empty($lang) || !is_array($lang)) {
$lang = array();
}
$lang = array_merge($lang, array(
'ANONYMUS' => 'โพสต์ที่ไม่ระบุชื่อ',
'ANONYMUS_EXPLAIN' => 'ตรวจสอบข้อมูลนี้ถ้าคุณต้องการให้โพสต์ที่ไม่ระบุชื่อ นี้ไม่สามารถหวนกลับโพสต์ครั้งหนึ่งเคยได้รับการส่ง',
'ACP_ANONYMUS_TITLE' => 'เมนูการจัดการนามแฝง',
'ACP_ANONYMUS_MODES_TITLE' => 'ตั้งค่าการใช้งาน',
'ACP_ANONYMUS_GOODBYE' => 'โพสต์ที่ไม่ระบุชื่อที่ใช้งาน?',
'ACP_ANONYMUS_SETTING_SAVED' => 'การตั้งค่าได้รับการบันทึกเรียบร้อยแล้ว!',
'ACP_ANONYMUS_NAME' => 'แสดงชื่อผู้ใช้',
'ACP_ANONYMUS_NAME_EXPLAIN' => 'ค่าที่ป้อนจะแสดงเป็นชื่อผู้โพสต์ เมื่อเว้นว่างค่ามาตรฐานจะแสดง',
));
โค้ด: เลือกทั้งหมด
<?php
/**
*
* @package phpBB Extension - Incognito
* @copyright (c) 2015 steveurkel
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace mindphp\m_anonymus_post\event;
/**
* @ignore
*/
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class listener implements EventSubscriberInterface {
static public function getSubscribedEvents() {
return array(
'core.user_setup' => 'load_language_on_setup',
'core.posting_modify_template_vars' => 'modify_posting_template',
'core.submit_post_modify_sql_data' => 'modify_post_sql_data',
);
}
public function __construct(\phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\db\driver\driver_interface $db, $table_prefix) {
$this->config = $config;
$this->template = $template;
$this->user = $user;
$this->request = $request;
$this->db = $db;
$this->table_prefix = $table_prefix;
}
public function load_language_on_setup($event) {
$lang_set_ext = $event['lang_set_ext'];
$lang_set_ext[] = array(
'ext_name' => 'mindphp/m_anonymus_post',
'lang_set' => 'm_anonymus_post',
);
$event['lang_set_ext'] = $lang_set_ext;
}
public function modify_posting_template($event) {
if ((int) $this->request->variable('mod_anonymous', 0, true) != 0) { //ตรวจสอบว่ามีการเปิดใช้งาน Extension หรือไม
$this->template->assign_vars(array(
'S_ANONYM_CHECKED' => 'checked="checked"', // กำหนดค่า เพื่อเป็นตัว check box
));
}
$this->template->assign_vars(array(
'ANONYMUS_ENABLED' => $this->config['m_anonymus_post_enabled'], // กำหนดค่า เพื่อเปิดใช้งานงานการแสดงผลที่ แทมเพลต
));
}
public function modify_post_sql_data($event) {
$post_sql_data = $event['sql_data'];
if ((int) $this->request->variable('mod_anonymous', 0, true) != 0) { ตรวจสอบว่ามีการ เลือกใช้นามแฝงหรือเปล่า
//set all poster info to ANONYMOUS - store the real poster id for admin tracking reasons
$post_sql_data[POSTS_TABLE]['sql']['real_poster_id'] = $post_sql_data[POSTS_TABLE]['sql']['poster_id'];
$post_sql_data[POSTS_TABLE]['sql']['poster_id'] = ANONYMOUS;
$post_sql_data[POSTS_TABLE]['sql']['post_username'] = ($this->config['m_anonymus_post_name'] != '' ? $this->config['m_anonymus_post_name'] : '');
$post_sql_data[TOPICS_TABLE]['sql']['topic_last_poster_id'] = ANONYMOUS;
$post_sql_data[TOPICS_TABLE]['sql']['topic_last_poster_name'] = ($this->config['m_anonymus_post_name'] != '' ? $this->config['m_anonymus_post_name'] : ''); //กำหนดชื่อ ผู้โพสเป็น นามแฝงที่กำหนดไว้ กรณีเป็นการตอบกลับโพส
$post_sql_data[TOPICS_TABLE]['sql']['topic_last_poster_colour'] = '';
//in case of first post of topic (new/edit) set poster info to ANONYMOUS
switch ($event['post_mode']) {
case 'edit_topic':
case 'edit_first_post':
case 'post' :
$post_sql_data[TOPICS_TABLE]['sql']['topic_poster'] = ANONYMOUS;
//$post_sql_data[TOPICS_TABLE]['sql']['topic_first_post_id'] = ANONYMOUS;
$post_sql_data[TOPICS_TABLE]['sql']['topic_first_poster_name'] = ($this->config['m_anonymus_post_name'] != '' ? $this->config['m_anonymus_post_name'] : ''); //กำหนดชื่อ ผู้โพสเป็น นามแฝงที่กำหนดไว้ กรณีเป็นผู้ตั้งกระทู้
$post_sql_data[TOPICS_TABLE]['sql']['topic_first_poster_colour'] = '';
break;
default : //nothing to do here
break;
}
//don't update user stats - so remove this info from sql data
unset($post_sql_data[USERS_TABLE]['stat']);
//reset user data for now
$this->user->data['user_id'] = ANONYMOUS;
$this->user->data['user_colour'] = '';
$this->user->data['is_registered'] = false;
$event['username'] = $this->config['m_anonymus_post_name'] != '' ? $this->config['m_anonymus_post_name'] : ''; //กำหนดชื่อ ผู้ใช้ใช้งานเป็น นามแฝงที่กำหนดไว้
}
$event['sql_data'] = $post_sql_data;
}
}
โค้ด: เลือกทั้งหมด
services:
mindphp.m_anonymus_post.listener:
class: mindphp\m_anonymus_post\event\listener
arguments:
- @config
- @template
- @user
- @request
- @dbal.conn
- %core.table_prefix%
tags:
- { name: event.listener }