Other How to create a Cron Entry to move threads - Cách tạo Cron Entry để di chuyển chủ đề (với options!)

PVS

Super Moderator
Thành viên BQT
Tham gia
28/02/2015
Bài viết
16,735
Được Like
12,681
How to create a Cron Entry to move threads - Cách tạo Cron Entry để di chuyển chủ đề (với options!) 1.0

Bài viết này sẽ hướng dẫn bạn làm thế nào để tạo một cron entry mới để di chuyển tất cả các chủ đề với một tiền tố được quy định từ diễn đàn X sang diễn đàn Y và chạy nó một lần một ngày.

Bước 1 - Tạo Add-on

Đầu tiên chúng ta cần tạo một add-on mới. Đây là một bước rất đơn giản. Để có thể tạo bạn cần phải bật Debug Mode.

Để tạo một add-on mới đi đến AdminCP -> Home -> List Add-ons và nhấn vào nút Create Add-on. Trong màn hình tiếp theo bạn cần phải điền vào các lĩnh vực này để tạo ra các add-on. Sử dụng các thông tin dưới đây:

Add-on ID: MoveThreadsCron
Title: Cron to Move Threads Automatically
Add-on is active: Checked
Version String: 1.0.0
Version ID: 1

Và các lĩnh vực còn lại chỉ cho phép để trống. Chúng ta không cần phải điền. Add-on này là chỉ đơn giản, vì vậy chúng ta không có bất kỳ quá trình cài đặt hoặc một cái gì đó.

Nhấp vào Lưu Add-on và tiện ích của bạn được tạo ra!

Bước 2 - Xác định các tùy chọn

Hãy tạo ra một số tùy chọn để tạo ra tiện ích tùy biến hơn một chút. Những gì chúng ta sẽ làm ở đây là tạo ra một entry cron để di chuyển chủ đề với một tiền tố cụ thể bên trong một diễn đàn specifc sang diễn đàn specifc khác. Vì vậy, thay vì chỉ định tất cả những thông tin bên trong file php của cron, chúng ta hãy tạo ra tùy chọn cho điều đó.

Để tạo ra một lựa chọn, đầu tiên chúng ta cần phải tạo ra một lựa chọn nhóm cho addon, vì vậy hãy vào AdminCP -> Options và nhấp vào nút +Add Option Group. Điều này sẽ mang đến cho bạn một trang mới:

1.PNG

Sử dụng các thông tin dưới đây để điền vào các lĩnh vực bắt buộc. Chúng ta đang tạo ra một nhóm tùy chọn mới để giữ tất cả các lựa chọn của chúng ta cho add-on này.

  • Group ID: MoveThreadCron
  • Title: Cron to Move Threads Automatically
  • Description: Options to the addon Cron to Move Threads Automatically.
  • Display Order: It is your choice, but I always use: 1000000
  • Display this group in debug mode only: NOT checked
  • Add-on: Cron to Move Threads Automatically
Sau đó, chúng ta hiện nay có thể tạo ra những lựa chọn mới. Tất cả các tùy chọn mới sẽ được đặt bên trong nhóm tùy chọn vừa tạo ra này. Nếu bạn đã theo bước của tôi, bạn sẽ có trong trang này:

2.PNG

Để thêm một lựa chọn mới, nhấn vào nút + Add Option.

Lựa chọn đầu tiên này sẽ giữ id diễn đàn mà chúng ta muốn để có được các chủ đề. Thực hiện theo các thông tin dưới đây để điền vào các lĩnh vực bắt buộc.

Option 1

  • Option ID: mtcFromForum
  • Add-on: Cron to Move Threads Automatically
  • Title: Forum to get threads
  • Explanation: Insert the Forum ID of the forum you want to get threads from.
  • Edit Format: SpinBox
  • Format Parameters:
    Min = 1
  • Data Type: Unsigned Integer
  • ... some other fields in the middle, just let they in blank ...
  • Cron to Move Threads Automatically: Checked, value = 10
------------------------------------------------- // ---------------------------------------

Option 2

  • Option ID: mtcFromPrefix
  • Add-on: Cron to Move Threads Automatically
  • Title: Get Threads with this prefix
  • Explanation: Insert the Prefix ID of the prefix you want to get threads. The cron will only take ALL threads that have this specific prefix.
  • Edit Format: SpinBox
  • Format Parameters:
    Min = 1
  • Data Type: Unsigned Integer
  • ... some other fields in the middle, just let they in blank ...
  • Cron to Move Threads Automatically: Checked, value = 20
------------------------------------------------- // ---------------------------------------

Option 3

  • Option ID: mtcToForum
  • Add-on: Cron to Move Threads Automatically
  • Title: Destination Forum
  • Explanation: Insert the ID of the forum that we will move threads to. This is the destination forum of the threads.
  • Edit Format: SpinBox
  • Format Parameters:
    Min = 1
  • Data Type: Unsigned Integer
  • ... some other fields in the middle, just let they in blank ...
  • Cron to Move Threads Automatically: Checked, value = 30
------------------------------------------------- // ---------------------------------------

Option 4

  • Option ID: mtcToPrefix
  • Add-on: Cron to Move Threads Automatically
  • Title: Destination Prefix
  • Explanation: Insert the ID of the prefix that we will set to the moved threads.
  • Edit Format: SpinBox
  • Format Parameters:
    Min = 1
  • Data Type: Unsigned Integer
  • ... some other fields in the middle, just let they in blank ...
  • Cron to Move Threads Automatically: Checked, value = 40
------------------------------------------------- // ---------------------------------------


Option 5

  • Option ID: mtcAmmount
  • Add-on: Cron to Move Threads Automatically
  • Title: Ammount of Threads
  • Explanation: This is the ammount of threads that the cron job will look to move to the destination forum.
  • Edit Format: SpinBox
  • Format Parameters:
    Min = 1
  • Data Type: Unsigned Integer
  • ... some other fields in the middle, just let they in blank ...
  • Cron to Move Threads Automatically: Checked, value = 50
Sau khi làm tất cả điều đó, bạn nên kết thúc với điều này:

3.png
3.png

Như bạn thấy mỗi một trong những lựa chọn là tự giải thích. Điều quan trọng là để điền vào các lĩnh vực.

Kể từ khi chúng ta đang ở đúng nơi, điền vào tất cả các tùy chọn này với những gì bạn muốn. Điều quan trọng là điền vào tất cả chúng một cách chính xác.

Nhưng nơi nào tôi nhận được tiền tố hoặc ID diễn đàn?

Để nhận được ID diễn đàn bạn đi đến AdminCP -> Applications -> Display Node Tree và bấm vào diễn đàn cần chọn. Hãy nhìn vào URL của bạn. Nó sẽ giống như thế này: http://example/example/admin.php?nodes/main-forum.2/edit

Số 2 là ID diễn đàn của bạn.


Điều tương tự bạn có thể làm với Tiền tố chủ đề.

Đến AdminCP -> Applications -> Thread Prefixes và bấm vào tiền tố để lựa chọn. Hãy nhìn vào URL của bạn. Nó sẽ giống như thế này: http://example/example/admin.php?thread-prefixes/videos.1/edit

Số 1 là ID tiền tố của bạn.

Bước 3 - Coding!

Bây giờ chúng ta thiết lập tất cả những gì chúng ta phải làm cho tiện ích này làm việc, nó chỉ là thiếu mã cron entry.

Vì vậy, chúng ta hãy làm điều đó!

Tới thư mục gốc cài đặt XenForo của bạn và tìm thư mục library. Bên trong của nó, bạn sẽ cần phải tạo ra một thư mục mới, thư mục cho add-on của chúng ta. Đổi tên thư mục thành addonMoveThreadCron. Bạn sẽ kết thúc với điều này:

4.PNG

Bên trong thư mục mới, tạo một file mới có tên CronEntry.php. Mở tập tin này và đặt đoạn code sau:
Mã:
<?php
/**
* Cron entry of the tutorial How to create a Cron Entry to move threads (with options!).
*
* MoveThreadCron = Name of our folder (and add-on too!)
* CronEntry = name of this file!
*
*/
class MoveThreadCron_CronEntry
{
    /**
    * Move ammount of threads with a specified prefix from forum X to forum Y.
    */
    public static function runMoveThreads()
    {
        /* We have options, remember? So, we will just get all options using the line bellow. */
        $options = XenForo_Application::get('options');

        /*
        *
        * Since we already have created the options, we just have to get them and use it in our cron.
        * Let's put all our custom options in an array, so can be easy to use it before in the function.
        * If you don't remember the Options ID, just go back to Step 2 and see.
        *
        */
        $cronOptions = array(
            'fromForum'    => $options->mtcFromForum,
            'toForum'        => $options->mtcToForum,
            'fromPrefix'    => $options->mtcFromPrefix,
            'toPrefix'        => $options->mtcToPrefix,
            'ammount'        => $options->mtcAmmount
        );

        /*
        *
        * To get the thrads we want, we must use a model. There is some ways to create models, but since we are in a static class
        * we will use the above method. The model is the XenForo_Model_Threads which has various functions to interact with threads.
        * The file of this model is located at: library/XenForo/Model/Thread.php. You can take a look at there if you want.
        *
        */
        $threadModel = XenForo_Model::create('XenForo_Model_Thread');

        /*
        *
        * Now we have our model, we can use a method inside of it to get threads from a forum. But what method?
        * This model has the method getThreads() which we can use conditionals and/or options to get threads.
        * So this will be the method that we'll use. First of all let's declare the conditionals variable so we can
        * filter the results and get only certain specific threads based on our options.
        *
        */
        $conditionals = array(
            // we just want to get threads from one forum. This is set in the options
            'forum_id'    => $cronOptions['fromForum'],

            // same thing, we want only threads with a certain prefix id
            'prefix_id' => $cronOptions['fromPrefix']
        );

        /*
        * Now let's just set the option to fetch the threads. The only option to fetch that we have to use is the "ammount of threads".
        * This will limit the number of returned threads.
        */
        $fetchOptions = array(
            // only get this ammount of threads, please!
            'limit' => $cronOptions ['ammount']
        );

        /* 
        * Now, let's get those threads!
        */
        $threads = $threadModel->getThreads($conditionals, $fetchOptions);

        /* If the method return any threads, let's move them to the destination forum! */
        if ($threads)
        {
            /* Now, for each thread found, let's change the prefix and move the thread to the destination forum! */
            foreach ($threads as $thread)
            {
                /*
                * To move threads, we need to change a field in the database. To do that, we've always to use (almost always) the DataWriter.
                * The DataWriter will do almost all the job for us. We just need to create it.
                */
                /* Let's create the DataWriter associated on Threads */
                $dw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
   
                /* Associate the data that we have in the thread to the DataWriter instance */
                $dw->setExistingData($thread);
   
                /* Here it is the code to move this thread to the destination forum. Simple, eh?
                * We are just changing the ID of the NODE of this thread to the new ID, which is the destination forum ID
                */
                $dw->set('node_id', $cronOptions['toForum']);
   
                /* Now, let's change the prefix id. We already have the destination prefix set in the options, so let's use it. */
                $dw->set('prefix_id', $cronOptions['toPrefix']);
   
                /* Save all changes and we are done! */
                $dw->save();
            }
        }
    }
}

Hầu như tất cả các dòng từ tập tin đã được giải thích, vì vậy tôi sẽ không giải thích ở đây.

Bước 4 - Chèn một Cron Entry mới

Bây giờ chúng ta cần phải tạo một Cron Entry mới, vì vậy XenForo bây giờ sẽ có một nhiệm vụ mới để chạy!

Đi đến AdminCP -> Tools -> Cron Entries và click vào + Create New Cron Entry.

Điền vào các lĩnh vực với các thông tin sau đây:
  • Cron Entry ID: MoveThreadCronEntry
  • Title: Cron to Move Threads Automatically
  • Cron Callback: MoveThreadCron_CronEntry::runMoveThreads
  • Run on type of day: This is your choice.
  • Allow cron entry to run automatically when scheduled: Checked
  • Add-on: Cron to Move Threads Automatically
Click vào nút Save Cron Entry.

Bạn có thể kiểm tra định kỳ bằng cách chọn menu Controls của Cron và nhấn "Run".

Nếu bạn theo tất cả các bước như đã mô tả trong hướng dẫn này, addon của bạn sẽ được làm việc. Bạn cũng có thể sử dụng trí tưởng tượng để chèn thêm nhiều lựa chọn. Một cái gì đó như "Di chuyển chủ đề và sau đó thiết lập một tiêu đề mặc định". Có một loạt các điều bạn có thể làm.

5.PNG

Chúc các bạn thành công.


Nguồn: xenforo.com​
 

Đính kèm

  • MoveThreadsCron.zip
    3 KB · Lượt xem: 2

Hướng dẫn sử dụng

XenForo 1 XenForo 2
Translate by PVS

Dịch vụ XenForo của VNXF

Mr. Tuấn

Mobile/Zalo: 0988 488 096

Telegram: bluekpro

Email: [email protected]

Nhà Tài Trợ

Mút Xốp Không Gian
pallet Thịnh Phát
Top Bottom