Customizing SMF > SMF Coding Discussion

Some new coding techniques

(1/4) > >>

Yoshi:
Hi all!

I've been playing around with SMF some more lately, writing a couple of mods, hence some more bug reports around here.
I decided to write a lightweight Mod Site for personal use. It's nothing compared to the mod site here, though :P

I wanted to learn some new coding techniques for handling such mods, or rather plugins. However you want to call them.
Maybe also some general techniques will be useful.

This is my "bootstrapping" file:

--- Code: ---<?php

function ModHome()
{
    global $context, $user_info, $txt, $scripturl;
    
    // This is just a quick redirector for the main functions. Nothing special.
    if (!isset($_REQUEST['sa']))
        $_REQUEST['sa'] = 'home';
    
    // Actions, array('Source function', 'Template function')
    $actions = array(
        'author' => array('ByAuthor', 'byauthor'),
        'home' => array('BrowseHome', 'home'),
        'browseall' => array('BrowseMods', 'browseall'),
        'view' => array('ViewMod', 'view'),
    );
    
    // Load the language and templates...
    loadLanguage('ModSite');
    loadTemplate('ModSite');
    
    // Setup the linktree for a bit...
    $context['linktree'][] = array(
        'url' => $scripturl . '?action=mods',
        'name' => $txt['modsite']
    );
    
    // Then setup everything from here!
    $context['sub_template'] = 'modsite_' . $actions[$_REQUEST['sa']][1];
    $actions[$_REQUEST['sa']][0]();
}

function BrowseHome()
{
    global $context, $user_info, $smcFunc, $modSettings, $txt;
    
    // Okay, gather all of our materials. Latest mods first.
    $latestmods_result = $smcFunc['db_query']('', '
        SELECT id, name
        FROM {db_prefix}modsite_mods
        ORDER BY id DESC
        LIMIT 0,{int:limit}',
        array(
            // To how many would we like to limit 'em?
            'limit' => 5
        ));
    
    // Start empty, of course.
    $latestmods = array();
    
    // Then start filling it like a geek.
    while ($lmod = $smcFunc['db_fetch_assoc']($latestmods_result))
        $latestmods[] = $lmod;
    
    // Now build up the context.
    $context['modsite']['home'] = array(
        'latestmods' => $latestmods,
        
    );
}

function ByAuthor()
{
    global $context, $txt, $smcFunc, $user_profile;
    
    if (!loadMemberData($_REQUEST['uid']))
        fatal_lang_error('user_no_exists');
        
    $context['modsite']['author'] = $user_profile[$_REQUEST['uid']];
    
    // This is just a simple list of all mods by this author... So keep it simple!
    $result = $smcFunc['db_query']('', '
        SELECT id, name, shortdesc
        FROM {db_prefix}modsite_mods
        WHERE authorid = {int:id}
        ORDER BY id DESC',
        array(
            'id' => $_REQUEST['uid'],
        ));
        
    $mods = array();
    while ($mod = $smcFunc['db_fetch_assoc']($result))
    {
        $mods[] = array(
            'id' => $mod['id'],
            'name' => $mod['name'],
            'sd' => parse_bbc($mod['shortdesc']),
        );
    }
        
    $context['modsite']['mods'] = $mods;
    
    // Setup the linktree again...
    $context['linktree'][] = array(
        'url' => $scripturl . '?action=mods;sa=author;uid=' . $_REQUEST['uid'],
        'name' => $txt['browse_by_user']
    );
}

function BrowseMods()
{
    
}

function BrowseMod()
{
    
}

function ViewMod()
{
    global $context, $user_info, $user_profile, $smcFunc, $scripturl, $settings;
    
    // Load the mod data. Grab the ID too so it's complete.
    $mod_data_result = $smcFunc['db_query']('', '
        SELECT id, name, authorid, descr
        FROM {db_prefix}modsite_mods
        WHERE id = {int:modid}',
        array(
            'modid' => $_REQUEST['mod'],
        ));
        
    // Since we have only one result (probably), only fetch it once.
    $mod_data = $smcFunc['db_fetch_assoc']($mod_data_result);
    
    // Also load the attachments.
    $mod_attach_result = $smcFunc['db_query']('', '
        SELECT id, is_current, filename
        FROM {db_prefix}modsite_attachments
        WHERE modid = {int:modid}',
        array(
            'modid' => $mod_data['id']
        ));
        
    // Okay, fetch it and while we are at it sort them.
    $attachments = array();
    while ($attach = $smcFunc['db_fetch_assoc']($mod_attach_result))
    {
        // Okay, sort 'em out!
        $current = $attach['is_current'] ? 'current' : 'archive';
        
        // Get rid of this one since we are sorting it out anyway.
        unset($attach['is_current']);
        
        // Put it in the respective group.
        $attachments[$current][$attach['id']] = $attach;
    }
    
    // Load the member data.
    loadMemberData($mod_data['authorid']);
    
    // And setup the mod context.
    $context['currentmod'] = array(
        'author' => $user_profile[$mod_data['authorid']],
        'name' => $mod_data['name'],
        'id' => $mod_data['id'],
        'description' => parse_bbc($mod_data['descr']),
        'is_own' => $mod_data['authorid'] === $user_info['id'],
        'attachments' => $attachments,
    );
    
    // Setup the linktree, too.
    $context['linktree'][] = array(
        'url' => $scripturl . '?action=mods;sa=view;mod=' . $_REQUEST['mod'],
        'name' => $mod_data['name'],
    );
}

?>
--- End code ---

This is my template:

--- Code: ---<?php

// ModSite template

function template_modsite_home()
{
    global $context, $txt, $scripturl;
    echo '
        <div class="cat_bar">
            <h3 class="catbg">
                ', $txt['modsite_latestmods'], '
            </h3>
        </div>
        <div class="plainbox">';
        
        //echo var_dump($context['modsite']['home']['latestmods']);
        $i = 1;
        foreach ($context['modsite']['home']['latestmods'] as $mod)
        {
            echo $i, '. <a href="', $scripturl, '?action=mods;sa=view;mod=', $mod['id'], '">', $mod['name'], '</a><br />';
            $i++;
        }
        
        echo '
        </div>
        <br class="clear" />';
}

function template_modsite_browseall()
{
}

function template_modsite_view()
{
    global $context, $txt, $scripturl, $modSettings, $settings;
    echo '
        <div class="cat_bar">
            <h3 class="catbg">
                ', $context['currentmod']['name'], '
            </h3>
        </div>
        <table style="width:100%;">
            <tr>
                <td style="width:50%;">
                    <div class="title_bar">
                        <h3 class="titlebg">
                            ', $txt['downloads'], '
                        </h3>
                    </div>
                    <span class="upperframe"><span></span></span>
                    <div class="roundframe">';
                    
                    if (!empty($context['currentmod']['attachments']['current']))
                    {
                        echo '
                        <strong>' . $txt['latest_version'] . ':</strong><br />';
                        
                        // Go through all of the attachments and create a link.
                        foreach ($context['currentmod']['attachments']['current'] as $attach)
                        {
                            echo '
                        <a href="', $scripturl, '?action=mods;sa=dlattach;attachid=', $attach['id'], '">', $attach['filename'], '</a><br />';   
                        }
                        
                        echo '<br />';
                    }
                    echo '
                    </div>
                    <span class="lowerframe"><span></span></span>
                </td>
                <td style="width:50%;">
                    <div class="title_bar">
                        <h3 class="titlebg">
                            ', $txt['author_info'], '
                        </h3>
                    </div>
                    <span class="upperframe"><span></span></span>
                    <div class="roundframe">
                        <div class="centertext">
                            <a href="', $scripturl, '?action=profile;u=', $context['currentmod']['author']['id_member'], '" class="subject">
                                ', $context['currentmod']['author']['member_name'], '
                            </a>
                            ', !empty($context['currentmod']['author']['member_group_color']) ? '
                            <div style="color:' . $context['currentmod']['author']['member_group_color'] . ';">
                                ' . $context['currentmod']['author']['member_group'] . '
                            </div>' : $context['currentmod']['author']['member_group'];
                            
                            $stars = explode('#', $context['currentmod']['author']['stars']);
                            echo str_repeat('<img src="' . $settings['images_url'] . '/' . $stars[1] . '" alt="#" />', $stars[0]), '
                            
                            ', !empty($context['currentmod']['author']['avatar']) ? '<br /><img src="' . $context['currentmod']['author']['avatar'] . '" style="max-height:' . $modSettings['avatar_max_height_external'] . 'px;max-width:' . $modSettings['avatar_max_width_external'] . 'px;" alt="" />' : '',
                               !empty($context['currentmod']['author']['personal_text']) ? '<br /><em>"' . $context['currentmod']['author']['personal_text'] . '"</em>' : '', '
                               
                            <br /><br /><a href="', $scripturl, '?action=mods;sa=author;uid=', $context['currentmod']['author']['id_member'], '">', $txt['view_more_author'], '</a>
                        </div>
                    </div>
                    <span class="lowerframe"><span></span></span>
                </td>
            </tr>
        </table>
        <div class="title_bar">
            <h3 class="titlebg">
                ', $txt['description'], '
            </h3>
        </div>
        <div class="plainbox">
            ', $context['currentmod']['description'], '
            ', var_dump($context['currentmod']), '
        </div>
        <br class="clear" />';
}

function template_modsite_byauthor()
{
    global $context, $txt, $scripturl;
    
    echo '
        <div class="cat_bar">
            <h3 class="catbg">
                ', sprintf($txt['mods_by'], $context['modsite']['author']['member_name']), '
            </h3>
        </div>';
        
    foreach ($context['modsite']['mods'] as $mod)
    {
        echo '
        <div class="title_bar">
            <h3 class="titlebg">
                <a href="', $scripturl, '?action=mods;sa=view;mod=', $mod['id'], '">', $mod['name'], '</a>
            </h3>
        </div>
        <div class="plainbox">', $mod['sd'], '</div>';
    }
    
    echo '
        <br class="clear" />';
}

?>
--- End code ---

And these are my language strings:

--- Code: ---<?php

$txt['modsite_latestmods'] = 'Latest mods';
$txt['author_info'] = 'Author info';
$txt['latest_version'] = 'Current version(s)';
$txt['old_versions'] = 'Archive';

$txt['modsite'] = 'Mod Site';
$txt['downloads'] = 'Downloads';
$txt['description'] = 'Description';

$txt['view_more_author'] = 'View more mods by this author';

$txt['user_no_exists'] = 'This user doesn\'t exist.';
$txt['browse_by_user'] = 'Browse by author';

$txt['mods_by'] = 'Mods by %s';

?>
--- End code ---

Live example:
http://map3cms.co.cc/?action=mods

Does anybody have some huge/tiny suggestions for that?
I know the code isn't valid for the mod site by far. Maybe also some suggestions on that?

Oh, I know that I can use OOP, by the way, but I just didn't feel like it lol.

Thanks a bunch!

Labradoodle-360:
Nice start my young padawan.

Iomega0318:
No kidding man, nice job! I'd love to see this as a mod!

vbgamer45:
Good start I do like the view mod page looks nice.

Yoshi:
Thanks all! :)

I might be releasing it as a package, not sure, maybe a DIY package. Oh well, we'll see :)

Lately I finished the attachments a bit more, so it now also displays the archived, non-current files, in the Downloads box. When that lot is finished, I'll start with editing and deleting mods, including a separate page for managing added files.

I'll keep you updated :)

Though, did ya lot find any obvious errors I skipped over? :P

Navigation

[0] Message Index

[#] Next page

Go to full version