Current time: 04-21-2014, 12:17 PM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 4 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy way to remove settings
06-08-2010, 04:33 AM (This post was last modified: 06-08-2010 04:34 AM by Steven.)
Post: #1
Easy way to remove settings
I'm currently rewriting my Advanced Forum Signatures plugin like I said I would ages ago (I got a job and haven't had much free time) and while doing so realized I could have done much better when deleting my plugin's settings when deactivating. Before it looked like this:

Code:
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_enabled'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_cache'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_userid'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_custom_title'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_gradient'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_gradient_start'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_gradient_end'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_custom_image'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_font'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_fontsize'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_fontcolor'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_show_url'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_short_copyright'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_allow_url_params'");
$db->query("DELETE FROM `".TABLE_PREFIX."settinggroups` WHERE `name`='afsignatures'");

Horrifying, right? Using SQL's LIKE and wildcards, I'm doing this in the new version:

Code:
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name` LIKE 'afs\_%'");
$db->query("DELETE FROM `".TABLE_PREFIX."settinggroups` WHERE `name`='afsignatures'");

Niiiiice.

So next time you update your plugin/write a new one, remember this simple trick (if you're not already doing it).

[Image: 422.png]
Visit this user's website Find all posts by this user
Quote this message in a reply
06-08-2010, 10:30 AM
Post: #2
RE: Easy way to remove settings
That's pretty neat; thanks for the share. Big Grin

[Image: Ij9Y7.png]
Photoshop Bliss V3 FREE - ACR Theme FREE
Visit this user's website Find all posts by this user
Quote this message in a reply
06-08-2010, 11:52 AM
Post: #3
RE: Easy way to remove settings
Thanks for the share mate.

Really helpfull Smile

Facebook Connect for MyBB
Visit this user's website Find all posts by this user
Quote this message in a reply
06-08-2010, 12:19 PM
Post: #4
RE: Easy way to remove settings
I prefer this way:

PHP Code:
function sample_info()
{
    return array
    (
        
'name' => 'Sample Plugin',
        
'description' => 'This is just an example.',
        
'author' => 'Sebastian Wunderlich',
        
'version' => '1.0',
        
'compatibility' => '*',
        
'codename' => 'sample'
    
);


PHP Code:
function sample_activate()
{
    global 
$db;
    
$info sample_info();
    
$setting_group_array = array
    (
        
'name' => $info['codename'],
        
'title' => $info['name'],
        
'description'=>'Here you can edit ' $info['name'] . ' settings.',
        
'disporder' => 1,
        
'isdefault' => 0
    
);
    
$db->insert_query('settinggroups'$setting_group_array);
    
$group=$db->insert_id();
    
$settings = array
    (
        
'sample_setting_1' => array
        (
            
'Sample Setting 1',
            
'Description text 1',
            
'text',
            
'sample text'
        
),
        
'sample_setting_2' => array
        (
            
'Sample Setting 2',
            
'Description text 2',
            
'yesno',
            
0
        
),
        
'sample_setting_3' => array
        (
            
'Sample Setting 3',
            
'Description text 3',
            
'onoff',
            
0
        
)
    );
    
$i 1;
    foreach(
$settings as $name => $sinfo)
    {
        
$insert_array = array
        (
            
'name' => $name,
            
'title' => $db->escape_string($sinfo[0]),
            
'description' => $db->escape_string($sinfo[1]),
            
'optionscode' => $db->escape_string($sinfo[2]),
            
'value' => $db->escape_string($sinfo[3]),
            
'gid' => $group,
            
'disporder' => $i,
            
'isdefault' => 0
        
);
        
$db->insert_query('settings'$insert_array);
        
$i++;
    }
    
rebuild_settings();


PHP Code:
function sample_deactivate()
{
    global 
$db;
    
$info sample_info();
    
$result $db->simple_select('settinggroups''gid''name="' $info['codename'] . '"', array('limit' => 1));
    
$group $db->fetch_array($result);
    if(!empty(
$group['gid']))
    {
        
$db->delete_query('settinggroups''gid="' $group['gid'] . '"');
        
$db->delete_query('settings''gid="' $group['gid'] . '"');
        
rebuild_settings();
    }


You can use the same functions on every plugins. You just need to replace:
PHP Code:
$info sample_info(); 
with the info function of the plugin that's all!
Visit this user's website Find all posts by this user
Quote this message in a reply
06-08-2010, 01:18 PM
Post: #5
RE: Easy way to remove settings
Wow, I've been learning to make plugins a complete different way from you. Toungue

[Image: Ij9Y7.png]
Photoshop Bliss V3 FREE - ACR Theme FREE
Visit this user's website Find all posts by this user
Quote this message in a reply
06-08-2010, 04:42 PM
Post: #6
RE: Easy way to remove settings
Very handy, querschlaeger! I also used an array for the settings, but I think I'll stick to SQL for deactivation.

[Image: 422.png]
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Contact Us | MyBB | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication