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

Jump to: navigation, search
m (→‎Code example: I knew the </span></span> tags would pop out if I removed the opening tags)
(→‎Code example: use <pre> instead of <code> because it works)
Line 24: Line 24:
=='''Code example'''==
=='''Code example'''==
This code is for SMF 1.1:
This code is for SMF 1.1:
<div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><code>echo &#039;
<!-- <div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div> -->
<pre>echo &#039;
&nbsp;&nbsp;&nbsp;&lt;ul&gt;&#039;;
&nbsp;&nbsp;&nbsp;&lt;ul&gt;&#039;;


Line 48: Line 49:
&nbsp;&nbsp;&nbsp;&lt;/ul&gt;&#039;;
&nbsp;&nbsp;&nbsp;&lt;/ul&gt;&#039;;


</code>
</pre>
And this is the SMF 2.0 equivalent:
And this is the SMF 2.0 equivalent:
<div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><code>echo &#039;
<!-- <div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div>-->
<pre>echo &#039;
&nbsp;&nbsp;&nbsp;&lt;ul&gt;&#039;;
&nbsp;&nbsp;&nbsp;&lt;ul&gt;&#039;;


Line 76: Line 78:
echo &#039;
echo &#039;
&nbsp;&nbsp;&nbsp;&lt;/ul&gt;&#039;;
&nbsp;&nbsp;&nbsp;&lt;/ul&gt;&#039;;
</code>[[#post_overview|" class="bbc_link">Back to overview]]
</pre>[[#post_overview|" class="bbc_link">Back to overview]]




Line 87: Line 89:
'''Main menu'''
'''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:
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:
</span><div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><code>&nbsp;&nbsp;&nbsp;if ($context&#91;&#039;allow_calendar&#039;])
</span><div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><pre>&nbsp;&nbsp;&nbsp;if ($context&#91;&#039;allow_calendar&#039;])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo ($current_action == &#039;calendar&#039; || $context&#91;&#039;browser&#039;]&#91;&#039;is_ie4&#039;]) ? &#039;&lt;td class=&quot;maintab_active_&#039; . $first . &#039;&quot;&gt;&amp;nbsp;&lt;/td&gt;&#039; : &#039;&#039; , &#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo ($current_action == &#039;calendar&#039; || $context&#91;&#039;browser&#039;]&#91;&#039;is_ie4&#039;]) ? &#039;&lt;td class=&quot;maintab_active_&#039; . $first . &#039;&quot;&gt;&amp;nbsp;&lt;/td&gt;&#039; : &#039;&#039; , &#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td valign=&quot;top&quot; class=&quot;maintab_&#039; , $current_action == &#039;calendar&#039; ? &#039;active_back&#039; : &#039;back&#039; , &#039;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td valign=&quot;top&quot; class=&quot;maintab_&#039; , $current_action == &#039;calendar&#039; ? &#039;active_back&#039; : &#039;back&#039; , &#039;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&quot;&#039;, $scripturl, &#039;?action=calendar&quot;&gt;&#039; , $txt&#91;&#039;calendar24&#039;] , &#039;&lt;/a&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&quot;&#039;, $scripturl, &#039;?action=calendar&quot;&gt;&#039; , $txt&#91;&#039;calendar24&#039;] , &#039;&lt;/a&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;&#039; , $current_action == &#039;calendar&#039; ? &#039;&lt;td class=&quot;maintab_active_&#039; . $last . &#039;&quot;&gt;&amp;nbsp;&lt;/td&gt;&#039; : &#039;&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;&#039; , $current_action == &#039;calendar&#039; ? &#039;&lt;td class=&quot;maintab_active_&#039; . $last . &#039;&quot;&gt;&amp;nbsp;&lt;/td&gt;&#039; : &#039;&#039;;
</code>
</pre>
SMF 2.0 has moved all the menu items to Subs.php, in a &quot;setupMenuContext&quot; 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 &quot;setupMenuContext&quot; 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:
<div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;calendar&#039; =&gt; array(
<div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;calendar&#039; =&gt; array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;title&#039; =&gt; $txt&#91;&#039;calendar&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;title&#039; =&gt; $txt&#91;&#039;calendar&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;href&#039; =&gt; $scripturl . &#039;?action=calendar&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;href&#039; =&gt; $scripturl . &#039;?action=calendar&#039;,
Line 111: Line 113:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
</code>In Subs.php. A new feature is that each button can have several &quot;sub-buttons&quot;. In the future, some themes may render these sub-buttons as a dropdown menu. This is done on the simplemachines.org community forum.
</pre>In Subs.php. A new feature is that each button can have several &quot;sub-buttons&quot;. In the future, some themes may render these sub-buttons as a dropdown menu. This is done on the simplemachines.org community forum.
[[#post_overview|" class="bbc_link">Back to overview]]
[[#post_overview|" class="bbc_link">Back to overview]]


Line 117: Line 119:
'''Administration menu'''
'''Administration menu'''
In SMF 1.1, the administration menu was defined in the adminIndex() function in Subs.php. For example, the &quot;Forum&quot; section looks like:
In SMF 1.1, the administration menu was defined in the adminIndex() function in Subs.php. For example, the &quot;Forum&quot; section looks like:
</span><div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><code>&nbsp;&nbsp;&nbsp;// Admin area &#039;Forum&#039;.
</span><div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><pre>&nbsp;&nbsp;&nbsp;// Admin area &#039;Forum&#039;.
&nbsp;&nbsp;&nbsp;if (allowedTo(array(&#039;manage_boards&#039;, &#039;admin_forum&#039;, &#039;manage_smileys&#039;, &#039;manage_attachments&#039;, &#039;moderate_forum&#039;)))
&nbsp;&nbsp;&nbsp;if (allowedTo(array(&#039;manage_boards&#039;, &#039;admin_forum&#039;, &#039;manage_smileys&#039;, &#039;manage_attachments&#039;, &#039;moderate_forum&#039;)))
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;{
Line 142: Line 144:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;}
</code>
</pre>
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:
<div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;layout&#039; =&gt; array(
<div class="codeheader">: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a></div><pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;layout&#039; =&gt; array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;title&#039; =&gt; $txt&#91;&#039;layout_controls&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;title&#039; =&gt; $txt&#91;&#039;layout_controls&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;permission&#039; =&gt; array(&#039;manage_boards&#039;, &#039;admin_forum&#039;, &#039;manage_smileys&#039;, &#039;manage_attachments&#039;, &#039;moderate_forum&#039;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;permission&#039; =&gt; array(&#039;manage_boards&#039;, &#039;admin_forum&#039;, &#039;manage_smileys&#039;, &#039;manage_attachments&#039;, &#039;moderate_forum&#039;),
Line 186: Line 188:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),
</code>
</pre>


[[Category:Customizing SMF]]
[[Category:Customizing SMF]]
[[Category:Developing SMF]]
[[Category:Developing SMF]]

Revision as of 20:54, 4 January 2011

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:

: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a>
   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:

: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a>
         '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:

: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a>
   // 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:

: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation"></a>
      '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: