MyBB Community Forums

Full Version: My plugin breaks the plugin page in admincp?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm having issues with the plugin I'm creating. Right now its not doing aything as when I upload it to inc/plugins folder it destroys the page for plugins in the ACP. By destroy I mean it hides the list of installed/active plugins and the uninstalled/not active plugins. Could someone skim over my code and see if I'm doing something wrong?

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}

$plugins->add_hook('portal_start', 'randomstreamer_promotion');

function otgs_info()
{
	return array(
		"name"			=> "OTGS",
		"description"	=> "Team G Twitch Streamer",
		"website"		=> "http://goltrix2580.com",
		"author"		=> "TheCrzyDoctor",
		"authorsite"	=> "http://goltrix2580.com",
		"version"		=> "0.01",
		"codename"		=> "otgs",
		"compatibility" => "18*"
	);
}

function otgs_install()
{
	global $mybb, $db;
	
	$gid = $db->insert_query('settinggroups', array(
													'name' => 'Official Team G Streamer',
													'title' => 'OTGS',
													'description' => 'Solution to pick random streamer to promote based on user groups',
												));
	
	$cfg = array (
		array(
			"name" => "OTGS Enabled",
			"title" => "Enable OTGS",
			"description" => "Turn OTGS On/Off",
			"optioncode" => "yesno",
			"value" => "1",
			"isdefault" => 1,
			"disporder" => 1,
			"gid" => $gid,
		),
		array(
			"name" => "User Group to Promote",
			"title" => 'User Group to Promote",
			"description" => "Pick a user group to promote.",
			"optionscode' => "groupselect",
			"isdefault" => 1,
			"value" => "",
			"disporder" => 0,
			"gid" => $gid,
		),
	);
	
	// insert array into database
	foreach ($cfg as $settings){
		$db->insert_query("settings", $settings);
	}
	
	rebuild_settings();
}

function otgs_is_installed()
{
	global $db;
	// Checks to see if the settings are in the table. If so the plugin is installed.
	$query = $db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'");
	$row = $db->num_rows($query);
	$settings_exists = !empty($row);
	
	return $settings_exists
}

function otgs_uninstall()
{
	global $db;
	
	$settingsGroupId = $ db->fetch_field(
		$db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'"), "gid";
	);
	
	$db->delete_query("settinggroups", "gid=", (int)$settingsGroupId);
	$db->delete_query("settings", "gid=", (int)$settingsGroupId);
	
	rebuild_settings();
}

function otgs_activate()
{
	global $db;
	$db->update_query("settings", array("value" => 1), "name='OTGS Enabled'");
}

function otgs_deactivate()
{
	global $db;
	$db->update_query("settings", array("value" => 0), "name='OTGS Enabled'");
}

function randomstreamer_promotion(){
	// This is most likely completely all wrong (still reading up on how to do this..)

	// Bring in db to use database
	global $db, $streamer;
	
		
	// pull text from custom field	
	//$query = $db->query("SELECT * FROM profilefields WHERE twitchaccount IS NOT NULL");
	
	// lets get how many rows are in the query
	//$max_number = $db->num_rows($query);
	
	// lets generate a random number to select what streamer to promote.
	//$streamer_num = rand(1, $max_number);		
	
	// set text from query to variable.
	//$var = $db->fetch_field($query)) // still need to figure out how to get the $treamer_num row from the query.
	
	$streamer = "thecrzydoctor"; // hard coded for testing as I still need to setup the custom profile field and integrate checking what group a person belongs to.
	
	echo $streamer; // now if i use {$streamer} inside my portal template I should get $var which would be a single row since I'm using simple_select?
	
	
}
You have a syntax error on line 81 - a space after the "$" for a variable. Change the code to:

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.");
}

$plugins->add_hook('portal_start', 'randomstreamer_promotion');

function otgs_info()
{
    return array(
        "name"            => "OTGS",
        "description"    => "Team G Twitch Streamer",
        "website"        => "http://goltrix2580.com",
        "author"        => "TheCrzyDoctor",
        "authorsite"    => "http://goltrix2580.com",
        "version"        => "0.01",
        "codename"        => "otgs",
        "compatibility" => "18*"
    );
}

function otgs_install()
{
    global $mybb, $db;
    
    $gid = $db->insert_query('settinggroups', array(
                                                    'name' => 'Official Team G Streamer',
                                                    'title' => 'OTGS',
                                                    'description' => 'Solution to pick random streamer to promote based on user groups',
                                                ));
    
    $cfg = array (
        array(
            "name" => "OTGS Enabled",
            "title" => "Enable OTGS",
            "description" => "Turn OTGS On/Off",
            "optioncode" => "yesno",
            "value" => "1",
            "isdefault" => 1,
            "disporder" => 1,
            "gid" => $gid,
        ),
        array(
            "name" => "User Group to Promote",
            "title" => 'User Group to Promote",
            "description" => "Pick a user group to promote.",
            "optionscode' => "groupselect",
            "isdefault" => 1,
            "value" => "",
            "disporder" => 0,
            "gid" => $gid,
        ),
    );
    
    // insert array into database
    foreach ($cfg as $settings){
        $db->insert_query("settings", $settings);
    }
    
    rebuild_settings();
}

function otgs_is_installed()
{
    global $db;
    // Checks to see if the settings are in the table. If so the plugin is installed.
    $query = $db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'");
    $row = $db->num_rows($query);
    $settings_exists = !empty($row);
    
    return $settings_exists
}

function otgs_uninstall()
{
    global $db;
    
    $settingsGroupId = $db->fetch_field(
        $db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'"), "gid";
    );
    
    $db->delete_query("settinggroups", "gid=", (int)$settingsGroupId);
    $db->delete_query("settings", "gid=", (int)$settingsGroupId);
    
    rebuild_settings();
}

function otgs_activate()
{
    global $db;
    $db->update_query("settings", array("value" => 1), "name='OTGS Enabled'");
}

function otgs_deactivate()
{
    global $db;
    $db->update_query("settings", array("value" => 0), "name='OTGS Enabled'");
}

function randomstreamer_promotion(){
    // This is most likely completely all wrong (still reading up on how to do this..)

    // Bring in db to use database
    global $db, $streamer;
    
        
    // pull text from custom field    
    //$query = $db->query("SELECT * FROM profilefields WHERE twitchaccount IS NOT NULL");
    
    // lets get how many rows are in the query
    //$max_number = $db->num_rows($query);
    
    // lets generate a random number to select what streamer to promote.
    //$streamer_num = rand(1, $max_number);        
    
    // set text from query to variable.
    //$var = $db->fetch_field($query)) // still need to figure out how to get the $treamer_num row from the query.
    
    $streamer = "thecrzydoctor"; // hard coded for testing as I still need to setup the custom profile field and integrate checking what group a person belongs to.
    
    echo $streamer; // now if i use {$streamer} inside my portal template I should get $var which would be a single row since I'm using simple_select?
    
    
} 
Thank you so much. After this round of PUBG, I'll be getting back to working on this. I hope to have this fully working by the end of the night tonight.

EDIT - I changed the php to what you said to. Straight up copy and paste. Still not showing in my plugin page. None of the plugins i have installed are either. I attached what my plugin page looks like. If i delete the otgs.php from inc/plugins folder all my plugins show up just fine.

[attachment=39894]
function otgs_is_installed()
{
    global $db;
    // Checks to see if the settings are in the table. If so the plugin is installed.
    $query = $db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'");
    $row = $db->num_rows($query);
    $settings_exists = !empty($row);
    
    return $settings_exists
}

to

function otgs_is_installed() 
{
   global $cache;
   $activeplugins = $cache->read("plugins");
   if(array_key_exists("otgs", $activeplugins['active']))
   {
     return true;
   }
   return false;
   }
}

might work?
Page is still showing up like the screen shot shows. As this is a php file does it need the closing tag of ?> at the end of it? I'm guessing it does not as the document Plugin Basics does not have it. This is so depressing to me. Stayed up late all last weekend to read up on how to make a plugin and I can't get it working... I just want to give up, but I'm to stubborn and want to get this to work! lol...
Based on your original code on the first post:

Line #48
"title" => 'User Group to Promote",

Line #50
"optionscode' => "groupselect",

Line #74
return $settings_exists

Line #82
$db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'"), "gid";
You can see the errors above.
(2018-01-27, 11:41 PM)RateU Wrote: [ -> ]Based on your original code on the first post:

Line #48
"title" => 'User Group to Promote",

Line #50
"optionscode' => "groupselect",

Line #74
return $settings_exists

Line #82
$db->simple_select("settinggroups", "gid", "name='Official Team G Streamer'"), "gid";
You can see the errors above.



I had all except line 82 fixed. Removing the ; fixed the issue. Thank you so much for the help! I really appreciate.
You're welcome.

Seems you're trying to display random member based on custom profile fields on Portal?
Yes. I want to eventually grab everyone one in a specific user group and then choose a random one. I still have to integrate the Twitch API to check if they are live and if so show that one if not pick another random person. Thanks to you I now have a usable hard coded twitch stream (mine) to show with no issues. 

I'm a python programmer so I'm still learning php and figured this would be a good way to do so. Though the next step is to get all users with the selected user group set via settings and see if they have a custom profile field filled in. Along with adding a custom profile field when the plugin is installed. So much to read and try and learn.
You can try to join users table with userfields table inside a query so you can filter the results based on usergroup and profile fields value. Store the results inside an array. shuffle it. Do an iteration after it is shuffled. Inside the iteration, use the API to check the user. If he/she is "Live", then break. Output the result.
Pages: 1 2