MyBB Community Forums

Full Version: Create a Pluginlibrary-based plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi all dear developers,
since I'm working hard to create a new outstanding board with some advanced features, I had in mind to extend euantor's MyAlerts plugin with some moderation alerts and such. I'd like to share with you those new alert types, so I want to create a new "plugin for the plugin". I don't actually know how, I've read so many docs this afternoon but after testing it on my localhost I ended up with a complete fail.

So I'm asking for a start-up plugin "template" with install, activate, uninstall and install functions. I don't really know why the following doesn't work:

<?php
/**
 * MyAlerts Extension 1.0
 * 
 * Provides additional actions for @euantor's MyAlerts plugin.
 *
 * @package MyAlerts Extension
 * @author  Shade <[email protected]>
 * @license http://opensource.org/licenses/mit-license.php MIT license (same as MyAlerts)
 * @version 0.1
 */
 
if (!defined('IN_MYBB'))
{
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

if(!defined("PLUGINLIBRARY"))
{
	define("PLUGINLIBRARY", MYBB_ROOT."inc/plugins/pluginlibrary.php");
}

function myalertsext_info()
{
	return array(
		'name'          =>  'MyAlerts Extension',
		'description'   =>  'Provides additional actions for @euantor\'s <a href="http://community.mybb.com/thread-127444.html"><b>MyAlerts</b></a> plugin. <b>MyAlerts is required for MyAlerts Extension to work</b>.',
		'website'       =>  'http://euantor.com/myalerts',
		'author'        =>  'Shade',
		'authorsite'    =>  '',
		'version'       =>  '0.1',
		'guid'          =>  '',
		'compatibility' =>  '16*',
		);
}

function myalertsext_install()
{
	global $db, $PL;

	if (!file_exists(PLUGINLIBRARY))
	{
		flash_message("The selected plugin could not be installed because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
		admin_redirect("index.php?module=config-plugins");
	}
	
	$PL or require_once PLUGINLIBRARY;
	
	$PL->edit_core('myalertsext', 'warnings.php',
               array('search' => '$db->update_query("users", $updated_user, "uid=\'{$user[\'uid\']}\'");',
                     'before' => '$plugins->run_hooks(\"warnings_do_warn_end\");'),
               true);
			   
	$myalertsSettings = array(
		'rep'               =>  1,
		'warn'              =>  1,
		'revokewarn'        =>  1,
		'pm'                =>  1,
		'buddylist'         =>  1,
		'quoted'            =>  1,
		'post_threadauthor' =>  1,
		);
	$db->update_query('users', array('myalerts_settings' => $db->escape_string(json_encode($myalertsSettings))), '1 = 1');

}

function myalertsext_is_installed()
{
	global $db;
	return $db->table_exists('alerts');
}

function myalertsext_uninstall()
{
	global $db, $PL;
	
	if (!file_exists(PLUGINLIBRARY))
	{
		flash_message("The selected plugin could not be uninstalled because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
		admin_redirect("index.php?module=config-plugins");
	}

	$PL or require_once PLUGINLIBRARY;
	
	$PL->edit_core('myalertsext', 'warnings.php',
               array(),
               true);
	
	$PL->settings_delete('myalertsext', true);
			   
	$myalertsSettings = array(
		'rep'               =>  1,
		'pm'                =>  1,
		'buddylist'         =>  1,
		'quoted'            =>  1,
		'post_threadauthor' =>  1,
		);
	$db->update_query('users', array('myalerts_settings' => $db->escape_string(json_encode($myalertsSettings))), '1 = 1');

}

function myalertsext_activate()
{
	global $db, $PL, $lang;

	if (!$lang->myalertsext)
	{
		$lang->load('myalertsext');
	}
	
	if (!file_exists(PLUGINLIBRARY))
	{
		flash_message("The selected plugin could not be activated because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
		admin_redirect("index.php?module=config-plugins");
	}

	$PL or require_once PLUGINLIBRARY;
	
	$PL->settings('myalertsext',
		$lang->setting_group_myalertsext,
		$lang->setting_group_myalertsext_desc,
		array(
			'alert_warn' =>  array(
				'title'         =>  $lang->setting_myalertsext_alert_warn,
				'description'   =>  $lang->setting_myalertsext_alert_warn_desc,
				'value'         =>  '1',
				),
			'alert_revokewarn' =>  array(
				'title'         =>  $lang->setting_myalertsext_alert_revokewarn,
				'description'   =>  $lang->setting_myalertsext_alert_revokewarn_desc,
				'value'         =>  '1',
				),
			)
	);
}


function myalertsext_deactivate()
{
	global $db, $PL, $lang;
	
	if (!file_exists(PLUGINLIBRARY))
	{
		flash_message("The selected plugin could not be deactivated because <a href=\"http://mods.mybb.com/view/pluginlibrary\">PluginLibrary</a> is missing.", "error");
		admin_redirect("index.php?module=config-plugins");
	}

	$PL or require_once PLUGINLIBRARY;
}

My aim is to add some settings to existing MyAlerts ones and update myalerts_settings column in users table with additional actions json encoded. Can somebody help me out with this?

Regards,
Shade
Why not using MyAlert hooks? Sorry if misunderstood.
For the functions, yes this is what I'll do. But I'd like to add settings into ACP and update settings for users in database table and I can't manage how to do it.
The is_installed function is wrong. It checks for existance of a table you didn't create yourself. You need a condition for your own plugin. The plugin will never run it's install() routine if is_installed() already returns true before that.

There are probably lots of other errors, the myalerts_settings thing doesn't look like it should be done this way; but I don't really know as I'm not using that plugin.
Updating the settings for every user is actually quite a difficult problem to solve due to how alert settings currently work unfortunately. This is why I don't normally like serializing data into the DB... I really should stop cutting corners.
Serializing is bad, yes, but it could still made to work, if you simply assume the default value when it's not set, and ignore extraneous keys. So you could get by without even updating it at all.
Ok then, changed my code a little bit following euantor MyAlerts' wiki. Now it correctly adds, for example, 'warn' setting into myalerts_settings cell, but not for existing users. And if I understood correctly, this is not possible at the moment. That's quite a pity, I can't go on without updating settings for existing users...
(2012-12-11, 06:23 PM)frostschutz Wrote: [ -> ]Serializing is bad, yes, but it could still made to work, if you simply assume the default value when it's not set, and ignore extraneous keys. So you could get by without even updating it at all.

True. I use json_encode rather than serialize anyway so it's slightly easier.
I don't know why the following code does not work - it doesn't load the option in the alerts settings.

$plugins->add_hook('myalerts_possible_settings', 'myextension_alerts_setting');
function myextension_alerts_setting(&$possible_settings)
{
	global $lang;
	$possible_settings[] = 'revoke';
	$lang->load('myextension');
}
Could you post the language file?
Pages: 1 2