Mod authors: Changes in SMF 2.0: Difference between revisions From Online Manual

Jump to: navigation, search
(HTML clean up)
m (Bot: Automated text replacement (-  + ))
 
(6 intermediate revisions by 4 users not shown)
Line 30: Line 30:
| $smcFunc['db_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.
You will find a more in-depth and detailed information about the SMF 2.0 functions in our [[$smcFunc|SMF 2.0 Database Functions]] topic on the community forums.


=== Column Names ===
=== Column Names ===
Line 41: Line 41:


  echo '
  echo '
     <ul>';
     <ul>';
   
   
  // Get all the topics in board 1
  // Get all the topics in board 1
Line 56: Line 56:
     // Echo this result
     // Echo this result
     echo '
     echo '
       &lt;li&gt;', $row['subject'], ' by ', $row['posterName'], '&lt;/li&gt;';
       <li>', $row['subject'], ' by ', $row['posterName'], '</li>';
  }
  }
   
   
Line 62: Line 62:
   
   
  echo '
  echo '
     &lt;/ul&gt;';
     </ul>';


And this is the SMF 2.0 equivalent:
And this is the SMF 2.0 equivalent:
  echo '
  echo '
     &lt;ul&gt;';
     <ul>';
   
   
  // Get all the topics in board 1
  // Get all the topics in board 1
Line 76: Line 76:
     LIMIT 10",
     LIMIT 10",
     array(
     array(
       'id_board' =&gt; 1,
       'id_board' => 1,
     ));
     ));
      
      
Line 84: Line 84:
     // Echo this result
     // Echo this result
     echo '
     echo '
       &lt;li&gt;', $row['subject'], ' by ', $row['poster_name'], '&lt;/li&gt;';
       <li>', $row['subject'], ' by ', $row['poster_name'], '</li>';
  }
  }
   
   
Line 90: Line 90:
   
   
  echo '
  echo '
     &lt;/ul&gt;';
     </ul>';


== $ID_MEMBER ==
== $ID_MEMBER ==
Line 99: Line 99:
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:
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'])
     if ($context['allow_calendar'])
       echo ($current_action == 'calendar' || $context['browser']['is_ie4']) ? '&lt;td class="maintab_active_' . $first . '"&gt;&amp;nbsp;&lt;/td&gt;' : '' , '
       echo ($current_action == 'calendar' || $context['browser']['is_ie4']) ? '<td class="maintab_active_' . $first . '"> </td>' : '' , '
             &lt;td valign="top" class="maintab_' , $current_action == 'calendar' ? 'active_back' : 'back' , '"&gt;
             <td valign="top" class="maintab_' , $current_action == 'calendar' ? 'active_back' : 'back' , '">
                 &lt;a href="', $scripturl, '?action=calendar"&gt;' , $txt['calendar24'] , '&lt;/a&gt;
                 <a href="', $scripturl, '?action=calendar">' , $txt['calendar24'] , '</a>
             &lt;/td&gt;' , $current_action == 'calendar' ? '&lt;td class="maintab_active_' . $last . '"&gt;&amp;nbsp;&lt;/td&gt;' : '';
             </td>' , $current_action == 'calendar' ? '<td class="maintab_active_' . $last . '"> </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:
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 the added button appears on all themes). As an example, the Calendar button has become: (Subs.php)
         'calendar' =&gt; array(
         'calendar' => array(
             'title' =&gt; $txt['calendar'],
             'title' => $txt['calendar'],
             'href' =&gt; $scripturl . '?action=calendar',
             'href' => $scripturl . '?action=calendar',
             'show' =&gt; $context['allow_calendar'],
             'show' => $context['allow_calendar'],
             'sub_buttons' =&gt; array(
             'sub_buttons' => array(
               'view' =&gt; array(
               'view' => array(
                   'title' =&gt; $txt['calendar_menu'],
                   'title' => $txt['calendar_menu'],
                   'href' =&gt; $scripturl . '?action=calendar',
                   'href' => $scripturl . '?action=calendar',
                   'show' =&gt; true,
                   'show' => true,
               ),
               ),
               'post' =&gt; array(
               'post' => array(
                   'title' =&gt; $txt['calendar_post_event'],
                   'title' => $txt['calendar_post_event'],
                   'href' =&gt; $scripturl . '?action=calendar;sa=post;sesc=' . $context['session_id'],
                   'href' => $scripturl . '?action=calendar;sa=post;sesc=' . $context['session_id'],
                   'show' =&gt; allowedTo('calendar_post'),
                   '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.
A new feature is that each button can have several "sub-buttons". Curve-based themes may render these sub-buttons as a dropdown menu. This is done on the simplemachines.org community forum.


=== Administration menu ===
=== Administration menu ===
Line 131: Line 131:
     {
     {
       $context['admin_areas']['layout'] = array(
       $context['admin_areas']['layout'] = array(
           'title' =&gt; $txt['layout_controls'],
           'title' => $txt['layout_controls'],
           'areas' =&gt; array()
           'areas' => array()
       );
       );
   
   
       if (allowedTo('manage_boards'))
       if (allowedTo('manage_boards'))
           $context['admin_areas']['layout']['areas']['manage_boards'] =  '&lt;a href="' . $scripturl . '?action=manageboards"&gt;' . $txt[4] . '&lt;/a&gt;';
           $context['admin_areas']['layout']['areas']['manage_boards'] =  '<a href="' . $scripturl . '?action=manageboards">' . $txt[4] . '</a>';
   
   
       if (allowedTo(array('admin_forum', 'moderate_forum')))
       if (allowedTo(array('admin_forum', 'moderate_forum')))
           $context['admin_areas']['layout']['areas']['posts_and_topics'] = '&lt;a href="' . $scripturl . '?action=postsettings"&gt;' . $txt['manageposts'] . '&lt;/a&gt;';
           $context['admin_areas']['layout']['areas']['posts_and_topics'] = '<a href="' . $scripturl . '?action=postsettings">' . $txt['manageposts'] . '</a>';
       if (allowedTo('admin_forum'))
       if (allowedTo('admin_forum'))
       {
       {
           $context['admin_areas']['layout']['areas']['manage_calendar'] = '&lt;a href="' . $scripturl . '?action=managecalendar"&gt;' . $txt['manage_calendar'] . '&lt;/a&gt;';
           $context['admin_areas']['layout']['areas']['manage_calendar'] = '<a href="' . $scripturl . '?action=managecalendar">' . $txt['manage_calendar'] . '</a>';
           $context['admin_areas']['layout']['areas']['manage_search'] = '&lt;a href="' . $scripturl . '?action=managesearch"&gt;' . $txt['manage_search'] . '&lt;/a&gt;';
           $context['admin_areas']['layout']['areas']['manage_search'] = '<a href="' . $scripturl . '?action=managesearch">' . $txt['manage_search'] . '</a>';
       }
       }
       if (allowedTo('manage_smileys'))
       if (allowedTo('manage_smileys'))
           $context['admin_areas']['layout']['areas']['manage_smileys'] = '&lt;a href="' . $scripturl . '?action=smileys"&gt;' . $txt['smileys_manage'] . '&lt;/a&gt;';
           $context['admin_areas']['layout']['areas']['manage_smileys'] = '<a href="' . $scripturl . '?action=smileys">' . $txt['smileys_manage'] . '</a>';
   
   
       if (allowedTo('manage_attachments'))
       if (allowedTo('manage_attachments'))
           $context['admin_areas']['layout']['areas']['manage_attachments'] = '&lt;a href="' . $scripturl . '?action=manageattachments"&gt;' . $txt['smf201'] . '&lt;/a&gt;';
           $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:
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' =&gt; array(
       'layout' => array(
           'title' =&gt; $txt['layout_controls'],
           'title' => $txt['layout_controls'],
           'permission' =&gt; array('manage_boards', 'admin_forum', 'manage_smileys', 'manage_attachments', 'moderate_forum'),
           'permission' => array('manage_boards', 'admin_forum', 'manage_smileys', 'manage_attachments', 'moderate_forum'),
           'areas' =&gt; array(
           'areas' => array(
             'manageboards' =&gt; array(
             'manageboards' => array(
                 'label' =&gt; $txt['admin_boards'],
                 'label' => $txt['admin_boards'],
                 'file' =&gt; 'ManageBoards.php',
                 'file' => 'ManageBoards.php',
                 'function' =&gt; 'ManageBoards',
                 'function' => 'ManageBoards',
                 'permission' =&gt; array('manage_boards'),
                 'permission' => array('manage_boards'),
             ),
             ),
             'postsettings' =&gt; array(
             'postsettings' => array(
                 'label' =&gt; $txt['manageposts'],
                 'label' => $txt['manageposts'],
                 'file' =&gt; 'ManagePosts.php',
                 'file' => 'ManagePosts.php',
                 'function' =&gt; 'ManagePostSettings',
                 'function' => 'ManagePostSettings',
                 'permission' =&gt; array('admin_forum', 'moderate_forum'),
                 'permission' => array('admin_forum', 'moderate_forum'),
             ),
             ),
             'managecalendar' =&gt; array(
             'managecalendar' => array(
                 'label' =&gt; $txt['manage_calendar'],
                 'label' => $txt['manage_calendar'],
                 'file' =&gt; 'ManageCalendar.php',
                 'file' => 'ManageCalendar.php',
                 'function' =&gt; 'ManageCalendar',
                 'function' => 'ManageCalendar',
                 'permission' =&gt; array('admin_forum'),
                 'permission' => array('admin_forum'),
             ),
             ),
             'managesearch' =&gt; array(
             'managesearch' => array(
                 'label' =&gt; $txt['manage_search'],
                 'label' => $txt['manage_search'],
                 'file' =&gt; 'ManageSearch.php',
                 'file' => 'ManageSearch.php',
                 'function' =&gt; 'ManageSearch',
                 'function' => 'ManageSearch',
                 'permission' =&gt; array('admin_forum'),
                 'permission' => array('admin_forum'),
             ),
             ),
             'smileys' =&gt; array(
             'smileys' => array(
                 'label' =&gt; $txt['smileys_manage'],
                 'label' => $txt['smileys_manage'],
                 'file' =&gt; 'ManageSmileys.php',
                 'file' => 'ManageSmileys.php',
                 'function' =&gt; 'ManageSmileys',
                 'function' => 'ManageSmileys',
                 'permission' =&gt; array('manage_smileys'),
                 'permission' => array('manage_smileys'),
             ),
             ),
             'manageattachments' =&gt; array(
             'manageattachments' => array(
                 'label' =&gt; $txt['attachments_avatars'],
                 'label' => $txt['attachments_avatars'],
                 'file' =&gt; 'ManageAttachments.php',
                 'file' => 'ManageAttachments.php',
                 'function' =&gt; 'ManageAttachments',
                 'function' => 'ManageAttachments',
                 'permission' =&gt; array('manage_attachments'),
                 'permission' => array('manage_attachments'),
             ),
             ),
           ),
           ),

Latest revision as of 12:11, 1 July 2013

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.

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 RDBMS systems.

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 in-depth and detailed information about the SMF 2.0 functions in our SMF 2.0 Database Functions topic on the community forums.

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

Code example

This code is for SMF 1.1:

echo '
    '; // 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 '
  • ', $row['subject'], ' by ', $row['posterName'], '
  • '; } mysql_free_result($result); echo '

';

And this is the SMF 2.0 equivalent:

echo '
    '; // 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 '
  • ', $row['subject'], ' by ', $row['poster_name'], '
  • '; } $smcFunc['db_free_result']($result); echo '

';

$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']) ? ' ' : , '

               <a href="', $scripturl, '?action=calendar">' , $txt['calendar24'] , '</a>

' , $current_action == 'calendar' ? ' ' : ;

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 the added button appears on all themes). As an example, the Calendar button has become: (Subs.php)

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

A new feature is that each button can have several "sub-buttons". Curve-based themes may render these sub-buttons as a dropdown menu. This is done on the simplemachines.org community forum.

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: