plugin building, activate coding makes site inaccessible
#1
Problems:
  • When I add in the function for faceclaim_activate my site goes blank (for both admin end and front end).
  • If I take out all the code inside function faceclaim_activate() { // empty } my site goes back to working.
  • Plugin "Install & Activate" never changes but each time I click it it adds an additional setting group (same gid and everything) to the plugin settings area, which I then have to delete the extra each time, I never have the option to deactivate or uninstall.

Function Activate Code:
function faceclaim_activate()
{
 global $db

 $fq_active = $db->simple_select('templategroups', "COUNT (*) as count", "title = 'Face Claim Directory Templates'");
 $fc_active = $db->fetch_field($fq_active, "count");
 $db->free_result($fq_active);

 if($fc_active < 1)
 {
 $fcdt = array(
 "prefix" => "faceclaim",
 "title" => "Face Claim Directory Templates",
 );
 $db->insert_query("templategroups", $fcdt);
 }

 $fcdt = array(
 "tid" => NULL,
 "title" => 'faceclaim',
 "template" => $db->escape_string('<html>
 <head>
 <title>{$mybb->settings['bbname']} - Face Claim Directory</title>
 {$headerinclude}
 </head>
 <body>
 {$header}
 
 <table width ="100%" border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}">
 <tr>
 <td colspan="2" class="thead"><strong>Face Claim Listing</strong></td>
 </tr>
 <tr>
 <td class="tcat" colspan="2">Character</td>
 <td class="tcat">Face Claim</td>
 </tr>
 </table>
 <table width ="100%" border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}">
 {$fc_dir}
 </table>
 
 {$footer}
 </body>
 </html>'),
 "sid" => "-2",
 "version" => $mybb->version + 1
 "dateline" => time(),
 );
 $db->insert_query("templates", $fcdt);
 
 $fcdt = array(
 "tid" => NULL,
 "title" => 'faceclaim_entry',
 "template" => $db->escape_string('<tr>
 <td class="trow1" width="50px">
 <a href="{$users['profilelink']}">
 <img src="{$users['avatar']}" onerror="this.onerror=null;this.src='/images/default_avatar.png';" width="50px" height="50px" style="border-radius: 50%;" />
 </a>
 </td>
 <td class="trow1">
 <a href="/member.php?action=profile&amp;uid={$users['profilelink']}">
 {$users['username']}
 </a>
 </td>
 <td class="trow1">
 {$users['faceclaim']}
 </td>
</tr>'),
 "sid" => "-2",
 "version" => $mybb->version + 1,
 "dateline" => time(),
 );
 $db->insert_query("templates", $fcdt);
 
 $fcdt = array(
 "tid" => NULL,
 "title" => 'faceclaim_empty',
 "template" => $db->escape_string('<tr>
 <td class="trow1" colspan="2" align="center">
 <i>There are currently no characters with face claims applicable. Please check back at a later date.</i>
 </td>
</tr>'),
 "sid" => "-2",
 "version" => $mybb->version + 1,
 "dateline" => time(),
 );
 $db->insert_query("templates", $fcdt);

 rebuild_settings();
}

Entire Plugin Code:
if(!defined("IN_MYBB"))
{
 die("Direct initialization of this file is not allowed.");
}


function faceclaim_info()
{
 return array(
 "name" => "Face Claim Directory",
 "description" => "Shows the face claim of characters in a list with character and player listed.",
 "website" => "https://tay.kaonshosting.com/",
 "author" => "Tay (isoldehn)",
 "authorsite" => "https://tay.kaonshosting.com/",
 "version" => "1.0",
 "guid" => "",
 "codename" => "fcdirectory",
 "compatibility" => "18"
 );
}

function faceclaim_install()
{
 global $db, $mybb;

 $fcsetting_group = array(
    'name' => 'faceclaim_settings',
    'title' => 'Face Claim Directory',
    'description' => 'All FC Directory settings are located here.',
    'disporder' => 5, // The order your setting group will display
    'isdefault' => 0
 );
 $gid = $db->insert_query("settinggroups", $fcsetting_group);

 $fcsetting_array = array(
    'faceclaim_toggle' => array(
        'title' => 'Enable / Disable',
        'description' => 'Choose whether you would like to enable or disable this plugin.',
        'optionscode' => 'yesno',
        'value' => '0', // 0 = NO | 1 = YES
        'disporder' => 1
    ),
        'faceclaim_usergroups' => array(
        'title' => 'Usergroups',
        'description' => 'From the following selections, choose the usergroups you would like to display on the Face Claim Directory.',
        'optionscode' => "groupselect",
        'value' => '',
        'disporder' => 2
    ),
 );
 foreach($fcsetting_array as $name => $setting)
 {
    $setting['name'] = $name;
    $setting['gid'] = $gid;
    $db->insert_query('settings', $setting);
 }

 rebuild_settings();
}

function faceclaim_is_installed()
{
    global $db;
    if($db->table_exists("faceclaim_settings"))
    {
        return true;
    }
    return false;
}

function faceclaim_uninstall()
{
 global $db;

 $db->delete_query('settings', "name IN ('faceclaim_toggle','faceclaim_usergroups'");
 $db->delete_query('settinggroups', "name = 'fcsetting_group'");
 $db->delete_query('templategroups', "title = 'Face Claim Directory Templates'");

 rebuild_settings();
}

function faceclaim_activate()
{
 global $db

 $fq_active = $db->simple_select('templategroups', "COUNT (*) as count", "title = 'Face Claim Directory Templates'");
 $fc_active = $db->fetch_field($fq_active, "count");
 $db->free_result($fq_active);

 if($fc_active < 1)
 {
 $fcdt = array(
 "prefix" => "faceclaim",
 "title" => "Face Claim Directory Templates",
 );
 $db->insert_query("templategroups", $fcdt);
 }

 $fcdt = array(
 "tid" => NULL,
 "title" => 'faceclaim',
 "template" => $db->escape_string('<html>
 <head>
 <title>{$mybb->settings['bbname']} - Face Claim Directory</title>
 {$headerinclude}
 </head>
 <body>
 {$header}
 
 <table width ="100%" border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}">
 <tr>
 <td colspan="2" class="thead"><strong>Face Claim Listing</strong></td>
 </tr>
 <tr>
 <td class="tcat" colspan="2">Character</td>
 <td class="tcat">Face Claim</td>
 </tr>
 </table>
 <table width ="100%" border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}">
 {$fc_dir}
 </table>
 
 {$footer}
 </body>
 </html>'),
 "sid" => "-2",
 "version" => $mybb->version + 1
 "dateline" => time(),
 );
 $db->insert_query("templates", $fcdt);
 
 $fcdt = array(
 "tid" => NULL,
 "title" => 'faceclaim_entry',
 "template" => $db->escape_string('<tr>
 <td class="trow1" width="50px">
 <a href="{$users['profilelink']}">
 <img src="{$users['avatar']}" onerror="this.onerror=null;this.src='/images/default_avatar.png';" width="50px" height="50px" style="border-radius: 50%;" />
 </a>
 </td>
 <td class="trow1">
 <a href="/member.php?action=profile&amp;uid={$users['profilelink']}">
 {$users['username']}
 </a>
 </td>
 <td class="trow1">
 {$users['faceclaim']}
 </td>
</tr>'),
 "sid" => "-2",
 "version" => $mybb->version + 1,
 "dateline" => time(),
 );
 $db->insert_query("templates", $fcdt);
 
 $fcdt = array(
 "tid" => NULL,
 "title" => 'faceclaim_empty',
 "template" => $db->escape_string('<tr>
 <td class="trow1" colspan="2" align="center">
 <i>There are currently no characters with face claims applicable. Please check back at a later date.</i>
 </td>
</tr>'),
 "sid" => "-2",
 "version" => $mybb->version + 1,
 "dateline" => time(),
 );
 $db->insert_query("templates", $fcdt);

 rebuild_settings();
}

function faceclaim_deactivate()
{
 global $db;

 $db->delete_query('templates', "title LIKE 'faceclaim_%' AND sid='-2'");

 rebuild_settings();
}

Would someone be able to point me in the right direction for fixing this issue? I am COMPLETELY new to creating plugins and this is my first attempt. I have followed the guides here that MyBB has provided and then followed the guide here to create templates on activation.
> SELECT finger FROM hand WHERE id='3'
Reply
#2
In your _activate function, the ; at the end of the "global $db" line is missing.
Reply
#3
(2021-01-02, 09:30 PM)doylecc Wrote: In your _activate function, the ; at the end of the "global $db" line is missing.

Thanks! Added the semi colon in and it no longer causes the same error.

HOWEVER, I clicked Install & Activate and it moved to the top with active plugins. Settings were created and I can confirm the settings have 'tables/rows' via phpMyAdmin. Yet NONE of the templates that are supposed to install on activate because I can't activate. This is the only option I have and every time I click it it just makes duplicate Admin CP settings for this plugin.
> SELECT finger FROM hand WHERE id='3'
Reply
#4
For a plugin to determine whether it is installed or not requires making a function called _is_installed. Often this is done by checking if a certain field or table exists in the database. In your plugin, your code in the _is_installed function will always be evaluating to false because you are adding settings, not creating a table.

faceclaim_is_installed()
{
global $db;
$query = $db->simple_select("settinggroups", "*", "name='faceclaim_settings'");
if($db->num_rows($query) >= 1)
{
return true;
}
return false;
}

Your other problem is the template prefix means your template title needs to start with the prefix followed by an underscore, then the rest. At least one of your templates titles does not follow that rule.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)