Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Query and modify LoU alliance forum posts using the LoU-PHP class
PHP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
LICENSE
README.md
forum-manager.class.php

README.md

LoU-PHP Forum Manager

Version 0.0.2

Description

A class based on LoU-PHP that allows you modify or query Lord of Ultima forum posts. The query system allows you to search posts as easily as it is to create, edit, or delete them!

Requirements

This class requires a valid and authenticated LoU session using the LoU-PHP class.

Usage

After creating and authenticating a LoU session with LoU-PHP, the first thing is to create a Forum Manager instance.

<?php

    require_once( 'forum-manager.class.php' );
    $fm = new ForumManager();

?>

Forum Queries

The query methods used to search the forum are straightforward and chainable. Let's start by looking at an example of querying the main forum.

loadForum

The loadForum method allows you to query the main forum threads (usually in order to retrieve a list of sub-threads).

<?php

    // Load the entire forum
    $forum = $fm->loadForum()->find_many();

    // Load the 'Announcements' forum
    $forum = $fm->loadForum()->where( 'title', 'Announcements' )->find_one();

    // Retrieve only the ID of the 'Announcements' forum
    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();

?>

loadThreads

Once you have a forum id by using loadForum(), you can then query the threads in that forum.

<?php

    // Grab all threads in the 'Announcements' forum
    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();
    $threads = $fm->loadThreads( $forum->id )->find_many();

    // Retrieve only the threads created by 'PastorBones' from the 'Announcements' forum
    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();
    $threads = $fm->loadThreads( $forum->id )->where( 'first_post_author', 'PastorBones' )->find_many();

?>

loadPosts

Are you seeing a pattern here? The object of this class is to make things simple!

<?php

    // Let's get all posts under the thread 'Warroom' within the 'Announcements' forum
    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();
    $threads = $fm->loadThreads( $forum->id )->where( 'title', 'Warroom' )->find_one();
    $posts = $fm->loadPosts( $forum->id, $thread->id )->find_many();

    // How about pulling all posts written by 'PastorBones' from this same forum?
    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();
    $threads = $fm->loadThreads( $forum->id )->where( 'title', 'Warroom' )->find_one();
    $posts = $fm->loadPosts( $forum->id, $thread->id )->where( 'author', 'PastorBones' )->find_many();

?>

More about the query language

The load methods retrieve the particular set of data to query from LoU. The loadThreads and loadPosts methods both require one or more ID's to be given in order to retrieve that data. Once the data has been loaded, you can then select() the fields you want to be returned and/or place filters on which records to return using the where() method.

The find_one() and find_many() methods specify how many records to return. If you choose find_one() and there is more than one record available after the filters have been applied, only the first record will be returned. Vice versa, if you choose find_many() and only one record is available, it will return an array of records with only one record. Whichever method you choose will determine how you operate on that data once it's returned.

Also, the select() and where() methods are stackable. You may add as many as you like. As far as the select() method goes, you can simply provide an array of keys instead of stacking the methods. The stacked where() methods are applied using the AND operator. This means both conditions must be met for that record to be returned.

<?php

    $posts = $fm->loadPosts( $forum_id, $thread_id )->select( array( 'id', 'author', 'message' ) )->where( 'author_id', '354' )->where( 'author', 'PastorBones' )->find_one();

?>

Creating, Editing, and Deleting

Once you have retrieved the necessary ID #'s, you can modify the forum very easily. Let's see how easy it is to first check for a thread and create it if it doesn't exist...

<?php

    $thread_title = 'Warroom';

    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();
    $thread = $fm->loadThreads( $forum->id )->select( 'id' )->where( 'title', $thread_title )->find_one();

    // Create the thread if it doesn't exist
    if( empty( $thread ) )
    {
        // When you create a thread, it's actually created by making the first post
        // So we're going to need a message to post!
        $post_msg = 'Use this forum for posting scout reports';

        // Actually create the thread
        $fm->createThread( $forum->id, $thread_title, $post_msg );
    }

?>

So our thread is there, how do we add a post? Simple!

<?php

    $fm->createPost( $forum->id, $thread->id, $msg );

?>

How about deleting or editing a post? First we have to make sure it exists...

<?php

    $thread_title = 'Warroom';

    $forum = $fm->loadForum()->select( 'id' )->where( 'title', 'Announcements' )->find_one();
    $thread = $fm->loadThreads( $forum->id )->select( 'id' )->where( 'title', $thread_title )->find_one();
    $post = $fm->loadPost( $forum->id, $thread->id )->select( 'id' )->where( 'author', $post_author )->find_one();

    if( !empty( $post ) )
    {
        // Edit the post
        $newMsg = 'This post has been edited.'
        $fm->editPost( $forum->id, $thread->id, $post->id, $newMsg );

        // Let's just delete it altogether
        $fm->deletePost( $forum->id, $thread->id, $post->id );
    }

?>

In Conclusion

As you can see, this makes managing the LoU forum very easy. However, this is a new class and I expect that some of the methodology might change after implementing a more thorough query system. If you have any ideas of how to make it better, by all means fork it!

Changelog

10/22/2012 - Fixed bug when creating threads

License

Copyright 2012, Roger Mayfield

Licensed under the MIT License (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://opensource.org/licenses/mit-license.php

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Something went wrong with that request. Please try again.