Mod authors: Changes in SMF 2.0 From Online Manual

Revision as of 05:33, 28 January 2011 by Groundup (talk | contribs) (Still requires a lot more cleanup but I started)
Jump to: navigation, search

SMF 2.0 has had some large code changes, a lot of which affect how mods are written. Code has been restructured for greater efficiency, several new features have been added (which make certain mods obsolete), etc. Due to the volume of changes, almost all mods designed for SMF 1.1 will not work with SMF 2.0. This document is a short introduction on converting SMF 1.1 mods to SMF 2.0. It is not an exhaustive reference, but should help you get your mod up and running on SMF 2.0.

Overview

Database Queries----SMF 1.1 only supported MySQL, and hence, MySQL functions are usually used in mods. In SMF 2.0, a database abstraction layer has been added, allowing for different database systems to be used (at the time of writing, MySQL, PostgreSQL, and SQLite are supported). As such, all MySQL function calls willl need to be changed to use the new SMF functions.

Because multiple database systems are supported, it is recommended that you do not use MySQL-specific SQL features (eg. ON DUPLICATE KEY UPDATE. If you stick to SQL92 SQL features, you can be assured that your mod should work on the majority of modern <acronym title="Relational Database Management Systems">RDBMS systems</acronym>.

The main functions are listed below:

Function used in SMF 1.1 SMF 2.0 equivalent
db_query$smcFunc['db_query']
mysql_fetch_assoc$smcFunc['db_fetch_assoc']
mysql_fetch_row$smcFunc['db_fetch_row']
mysql_num_rows$smcFunc['db_num_rows']
mysql_free_result$smcFunc['db_free_result']
mysql_real_escape_string or mysql_escape_string$smcFunc['db_escape_string']

You will find a more indepth and detailed information about the SMF 2.0 functions in our SMF 2.0 Database Functions topic on the community forums.

Back to overview


Column Names

In SMF 2.0, all uppercase column names have been changed. This was done because some database systems do not support uppercase column names. The index columns that had uppercase names in SMF 1.1 (eg. ID_MSG, ID_TOPIC, ID_MEMBER) have had their names changes to lowercase (id_msg, id_topic, id_member). Additionally, any columns that used camel case (memberName) have changed to use an underscore between words (member_name)

An example is below Back to overview

Code example

This code is for SMF 1.1:

echo '
   <ul>';

// Get all the topics in board 1
$result = db_query("
   SELECT t.ID_TOPIC, m.posterName, m.subject
   FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m)
   WHERE t.ID_BOARD = 1
      AND m.ID_MSG = t.ID_FIRST_MSG
   LIMIT 10", __FILE__, __LINE__);
   
// Loop through all results 
while ($row = mysql_fetch_assoc($result))
{
   // Echo this result
   echo '
      <li>', $row['subject'], ' by ', $row['posterName'], '</li>';
}

mysql_free_result($result);

echo '
   </ul>';

And this is the SMF 2.0 equivalent:

echo '
   <ul>';

// Get all the topics in board 1
$result = $smcFunc['db_query']('', "
   SELECT t.id_topic, m.poster_name, m.subject
   FROM {db_prefix}topics AS t
      INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
   WHERE t.id_board = {int:id_board}
   LIMIT 10",
   array(
      'id_board' => 1,
   ));
   
// Loop through all results 
while ($row = $smcFunc['db_fetch_assoc']($result))
{
   // Echo this result
   echo '
      <li>', $row['subject'], ' by ', $row['poster_name'], '</li>';
}

$smcFunc['db_free_result']($result);

echo '
   </ul>';

" class="bbc_link">Back to overview


$ID_MEMBER In SMF 1.1, the $ID_MEMBER variable contained the current user's ID. SMF 2.0 has removed this variable. Any code using $ID_MEMBER will need to be edited so it uses either $user_info['id'] or $context['user']['id'].

Menus---- Main menu In SMF 1.1, each theme had its own main menu in index.template.php. For example, the code for the Calendar button in the SMF 1.1 default theme (Core) looks like this:

   if ($context['allow_calendar'])
      echo ($current_action == 'calendar' || $context['browser']['is_ie4']) ? '<td class="maintab_active_' . $first . '">&nbsp;</td>' : '' , '
            <td valign="top" class="maintab_' , $current_action == 'calendar' ? 'active_back' : 'back' , '">
               <a href="', $scripturl, '?action=calendar">' , $txt['calendar24'] , '</a>
            </td>' , $current_action == 'calendar' ? '<td class="maintab_active_' . $last . '">&nbsp;</td>' : '';

SMF 2.0 has moved all the menu items to Subs.php, in a "setupMenuContext" function. This simplifies theme writing, and makes it easier for mod authors (just edit the Subs.php file, and a button appears on all themes). As an example, the Calendar button has become:

         'calendar' => array(
            'title' => $txt['calendar'],
            'href' => $scripturl . '?action=calendar',
            'show' => $context['allow_calendar'],
            'sub_buttons' => array(
               'view' => array(
                  'title' => $txt['calendar_menu'],
                  'href' => $scripturl . '?action=calendar',
                  'show' => true,
               ),
               'post' => array(
                  'title' => $txt['calendar_post_event'],
                  'href' => $scripturl . '?action=calendar;sa=post;sesc=' . $context['session_id'],
                  'show' => allowedTo('calendar_post'),
               ),
            ),
         ),

In Subs.php. A new feature is that each button can have several "sub-buttons". In the future, some themes may render these sub-buttons as a dropdown menu. This is done on the simplemachines.org community forum.

" class="bbc_link">Back to overview

Administration menu In SMF 1.1, the administration menu was defined in the adminIndex() function in Subs.php. For example, the "Forum" section looks like:

   // Admin area 'Forum'.
   if (allowedTo(array('manage_boards', 'admin_forum', 'manage_smileys', 'manage_attachments', 'moderate_forum')))
   {
      $context['admin_areas']['layout'] = array(
         'title' => $txt['layout_controls'],
         'areas' => array()
      );

      if (allowedTo('manage_boards'))
         $context['admin_areas']['layout']['areas']['manage_boards'] =  '<a href="' . $scripturl . '?action=manageboards">' . $txt[4] . '</a>';

      if (allowedTo(array('admin_forum', 'moderate_forum')))
         $context['admin_areas']['layout']['areas']['posts_and_topics'] = '<a href="' . $scripturl . '?action=postsettings">' . $txt['manageposts'] . '</a>';
      if (allowedTo('admin_forum'))
      {
         $context['admin_areas']['layout']['areas']['manage_calendar'] = '<a href="' . $scripturl . '?action=managecalendar">' . $txt['manage_calendar'] . '</a>';
         $context['admin_areas']['layout']['areas']['manage_search'] = '<a href="' . $scripturl . '?action=managesearch">' . $txt['manage_search'] . '</a>';
      }
      if (allowedTo('manage_smileys'))
         $context['admin_areas']['layout']['areas']['manage_smileys'] = '<a href="' . $scripturl . '?action=smileys">' . $txt['smileys_manage'] . '</a>';

      if (allowedTo('manage_attachments'))
         $context['admin_areas']['layout']['areas']['manage_attachments'] = '<a href="' . $scripturl . '?action=manageattachments">' . $txt['smf201'] . '</a>';
         
   }

Much like the main menu, SMF 2.0 has seperated the menu items from the actual menu itself (which is handled by Subs-Menu.php and the theme). The code for this same menu looks a lot simpler in SMF 2.0:

      'layout' => array(
         'title' => $txt['layout_controls'],
         'permission' => array('manage_boards', 'admin_forum', 'manage_smileys', 'manage_attachments', 'moderate_forum'),
         'areas' => array(
            'manageboards' => array(
               'label' => $txt['admin_boards'],
               'file' => 'ManageBoards.php',
               'function' => 'ManageBoards',
               'permission' => array('manage_boards'),
            ),
            'postsettings' => array(
               'label' => $txt['manageposts'],
               'file' => 'ManagePosts.php',
               'function' => 'ManagePostSettings',
               'permission' => array('admin_forum', 'moderate_forum'),
            ),
            'managecalendar' => array(
               'label' => $txt['manage_calendar'],
               'file' => 'ManageCalendar.php',
               'function' => 'ManageCalendar',
               'permission' => array('admin_forum'),
            ),
            'managesearch' => array(
               'label' => $txt['manage_search'],
               'file' => 'ManageSearch.php',
               'function' => 'ManageSearch',
               'permission' => array('admin_forum'),
            ),
            'smileys' => array(
               'label' => $txt['smileys_manage'],
               'file' => 'ManageSmileys.php',
               'function' => 'ManageSmileys',
               'permission' => array('manage_smileys'),
            ),
            'manageattachments' => array(
               'label' => $txt['attachments_avatars'],
               'file' => 'ManageAttachments.php',
               'function' => 'ManageAttachments',
               'permission' => array('manage_attachments'),
            ),
         ),
      ),


Advertisement: