วิธีการสร้าง Extension สำหรับสร้างนามแฝง ใน phpbb

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
Ittichai_chupol
PHP VIP Members
PHP VIP Members
โพสต์: 5410
ลงทะเบียนเมื่อ: 19/09/2018 10:33 am

วิธีการสร้าง Extension สำหรับสร้างนามแฝง ใน phpbb

โพสต์ที่ยังไม่ได้อ่าน โดย Ittichai_chupol »

เว็บบอร์ด phpbb นั้นเป็บเว็บบอร์ดที่มี cod และ ฟั่งชั่น ตางๆ ที่สนับสนุนการใช้งาน ในส่วนต่างๆของเว็บ มาให้เป้นพื้นฐาน ตั้งแต่แรกติดตั้งอยู่ นั้นจึงทำให้ผุ้พัฒนา ไม่ต้องเสียเวลากับการที่จะต้องสร้าง ส่วนต่างขึ้นมาเองทั้งหมด เพียงแค่ปรับปรุง หรือ เสริมเพียงเเค่บางส่วนเข้าไปเท่านั้น ก็ทำให้เว็บบอร์ด สามารถทำงาน ตามที่เราต้องการ โดยวิธีการที่เราจะทำให้เว็บบอร์ด phpbb สามารถทำในส่วนที่ไม่ได้มีมากับตอนติดตั้งครั่งแรกนั้น คือการส่ร้าง ส่วนขยาย(Extension)
ส่วนขยาย(Extension) จะทำให้ เว็บบอร์ด phpbb มีประสิทธิภาพ การทำงานที่ดีขึ้น โดยสามารถ ปรับเพิ่มเพิ่มแก้ไข้ ในส่วนต่างๆของ เว็บบอร์ด phpbb ได้ตามอิสระ ซึ่งในขั้นตอนพัฒนา ไม่จำเป้นที่จะต้องเข้าไปยุ่งเกี่ยว Code เดิมที่มีอยู่(แต่อาจจะมีบางครั่งที่ต้องเข้าไปแก้ไข้ เพิ่มเติม) นั้นก็ช่วยเราไม่ต้องกังวลกับการผิดพลาดที่อาจจะเกิดขึ้นได้เพราะหาก Code ของ เว็บบอร์ด phpbb เกิดความผิดพลาดก้อาจจะกระทบต่อการทำงานส่วนอื่นๆ ด้วย โดยการสร้าง Extension นั้นก็มีวิธีการพัฒนาที่ไม่ซับซ่อนมาก เพียงแต่จะมีเพียงการทำงานที่จะต้องเป็นไปตามที่เราต้องการเท่านั้น ที่จะยากนิดหน่อย
ซึ่งวันนี้เราจะนำเสนอ การสร้าง Extension ที่ใช่สำหรับการสร้างนามแฝง

ขั้นตอนการพัฒนา

1.สร้างโฟลเดอร์สำหรับเป้น ส่วนขยาย(Extension) ชือว่า m_anonymus_post
2.จากนั้นสร้างโฟลเดอร์ ACP แล้วสร้างไฟล์ php 2 ไฟล์
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')),
            ),
        );
    }

}


2.2 m_anonymus_post_module

โค้ด: เลือกทั้งหมด

<?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.จากนั้นสร้างโฟลเดอร์ amd
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} &nbsp;
                       <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}" />&nbsp;
            <input class="button2" type="reset" id="reset" name="reset" value="{L_RESET}" />
        </p>

        {S_FORM_TOKEN}
    </fieldset>
</form>
<!-- INCLUDE overall_footer.html -->


4.จากนั้นสร้างโฟลเดอร์ migrations แล้วสร้างไฟล์ php 2 ไฟล์

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'),
                    ),
                )),
        );
    }

}
4.2 ชื่อว่า install_m_anonymus_post_1

โค้ด: เลือกทั้งหมด

<?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.จากนั้นสร้างโฟลเดอร์ language สร้างโฟลเดอร์ 2 โฟลเดอร์

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.',
        ));
5.1.1 จากนั้น สร้าง สร้างโฟลเดอร์ ชื่อ acp แล้วก็สร้างสร้างไฟล์ html ชือว่า acp_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.',
          ));
5.2 สร้างโฟลเดอร์ th แล้วสร้างไฟล์ 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' => 'โพสต์ที่ไม่ระบุชื่อ',
    'ANONYMUS_EXPLAIN' => 'ตรวจสอบข้อมูลนี้ถ้าคุณต้องการให้โพสต์ที่ไม่ระบุชื่อ นี้ไม่สามารถหวนกลับโพสต์ครั้งหนึ่งเคยได้รับการส่ง',
    'ACP_ANONYMUS_TITLE' => 'เมนูการจัดการนามแฝง',
    'ACP_ANONYMUS_MODES_TITLE' => 'ตั้งค่าการใช้งาน',
    'ACP_ANONYMUS_GOODBYE' => 'โพสต์ที่ไม่ระบุชื่อที่ใช้งาน?',
    'ACP_ANONYMUS_SETTING_SAVED' => 'การตั้งค่าได้รับการบันทึกเรียบร้อยแล้ว!',
    'ACP_ANONYMUS_NAME' => 'แสดงชื่อผู้ใช้',
    'ACP_ANONYMUS_NAME_EXPLAIN' => 'ค่าที่ป้อนจะแสดงเป็นชื่อผู้โพสต์ เมื่อเว้นว่างค่ามาตรฐานจะแสดง',
          ));
5.2.1 จากนั้น สร้าง สร้างโฟลเดอร์ ชื่อ acp แล้วก็สร้างสร้างไฟล์ html ชือว่า acp_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' => 'โพสต์ที่ไม่ระบุชื่อ',
    'ANONYMUS_EXPLAIN' => 'ตรวจสอบข้อมูลนี้ถ้าคุณต้องการให้โพสต์ที่ไม่ระบุชื่อ นี้ไม่สามารถหวนกลับโพสต์ครั้งหนึ่งเคยได้รับการส่ง',
    'ACP_ANONYMUS_TITLE' => 'เมนูการจัดการนามแฝง',
    'ACP_ANONYMUS_MODES_TITLE' => 'ตั้งค่าการใช้งาน',
    'ACP_ANONYMUS_GOODBYE' => 'โพสต์ที่ไม่ระบุชื่อที่ใช้งาน?',
    'ACP_ANONYMUS_SETTING_SAVED' => 'การตั้งค่าได้รับการบันทึกเรียบร้อยแล้ว!',
    'ACP_ANONYMUS_NAME' => 'แสดงชื่อผู้ใช้',
    'ACP_ANONYMUS_NAME_EXPLAIN' => 'ค่าที่ป้อนจะแสดงเป็นชื่อผู้โพสต์ เมื่อเว้นว่างค่ามาตรฐานจะแสดง',
          ));
6.จากนั้นสร้างโฟลเดอร์ event แล้วสร้างไฟล์ php ชือว่า listenner

โค้ด: เลือกทั้งหมด

<?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;
    }

}
7.จากนั้นสร้างโฟลเดอร์ config แล้วก้สร้างไฟล์ yml ชื่อว่า services

โค้ด: เลือกทั้งหมด

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 }
ประโยชนื คือ ใช้สำหรับผู่ที่ไม่ต้องการแสดงชื่อจริงๆ เพื่อใช้โพส
ขอให้วันนี้เป็นวันที่ดี
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 59