Jump to content

Make Setsetting Support Array


Blesta Addons

Recommended Posts

today i'm working in settings class for my plugin , i though that is better to make setsetting support array instead of 1 key/value .

 

why ?

 

this will help us as developper to add array of setting for one plugin . for exemple

Array (
[0] => stdClass Object ( [key] => amazons3_access_key [value] => [encrypted] => 1 [level] => system )
[1] => stdClass Object ( [key] => amazons3_bucket [value] => [encrypted] => 0 [level] => system )
.....
.....
.....
[XX] => stdClass Object ( [key] => announcement_plugin [value] => Array (
             [0] => stdClass Object ( [key] => setting_1 [value] => true )
             [1] => stdClass Object ( [key] => setting_2 [value] => 325 )
            )
[encrypted] => 0 
[level] => system 
)

as we can delete this group of setting in one shoot if uninstalling the plugin .

 

 

 

 

Link to comment
Share on other sites

EDIT , i got what i want by a simple tip , so i will share it .

 

let say you need to add settings to your plugin "my_plugin" ; so :

$value = array('setting_1' => 12 , 'setting_2'=> "desc" , 'setting_3'=> "here some text"); // build the array of setting
$this->Settings->setSetting("my_plugin", serialize($value) , $encrypted = null ); // save the settings 

the magic code here is serialize that convert array to string

 

when you need to use the setting just call the function

$value = $this->Settings->getSetting("my_plugin");

$vars = unserialize($value->value));

now you have a array of setting inthe $vars .

 

hope this can help others .

Link to comment
Share on other sites

You should probably be using Company::setSetting() instead, since plugins are installed per-company.

 

Also, you can use setSettings() after you create the settings for quick update. But install/uninstall could just loop through all settings your plugin creates.

$settings = array('setting1', 'setting2', 'setting3');

// uninstall all plugin settings:
foreach ($settings as $setting) {
    $this->Company->unsetSetting($company_id, $setting);
}
Link to comment
Share on other sites

 

You should probably be using Company::setSetting() instead, since plugins are installed per-company.

 

Also, you can use setSettings() after you create the settings for quick update. But install/uninstall could just loop through all settings your plugin creates.

$settings = array('setting1', 'setting2', 'setting3');

// uninstall all plugin settings:
foreach ($settings as $setting) {
    $this->Company->unsetSetting($company_id, $setting);
}

 

i was inthe road to ASK the question about the company_id and setsettings, as there are no way to make  settings per company .

 

your comment was in the correct time, i will try and see . 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...