Hiya,
I'm trying to extend the Config class.. but there is zero documentation for that.. I'm simply guessing. The namespace has changed from 1.7 to 1.7.2 and it wiped my additions.. so I'm guessing I'm doing something wrong.. Should I ask for it on GitHub or is this a forum type request thing? Who is doing the documentation? Is there documentation? It seems like one out of every 10 functions has a small comment or such attached, but they aren't really "this does this" type comments.. and classes.. christ.. I would really rather not bug people every time I need a new thing, so I've figured out how a lot of it works.. but I just want to clarify before steaming ahead.. does this look anything close to right?
How to add a page of custom settings to the osTicket Admin interface.
Note: Below is an approximation
Had to modify: /scp/settings.php:
$settingOptions=array('Custom' => 'Custom Settings',
...
That first parameter is the second part of the following file, so name it what you want.
Then create a file: /include/staff/settings-Custom.inc.php
if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin() || !$config) die('Access Denied');
?>
<h2>Custom Settings and Options</h2>
<form action="settings.php?t=Custom" method="post" id="save">
<?php csrf_token(); ?>
<input type="hidden" name="t" value="specialpage" >
<table class="form_table settings_table" width="940" border="0" cellspacing="0" cellpadding="2">
... input's and formatting for your settings here..
<tr><td width="220" class="required">Important thing:</td>
<td>
<input type="radio" name="important_setting" value="0" <?php echo !$config?'checked="checked"':''; ?> />
Should this important setting be on? <em>(highly recommended)</em>
</td>
</tr>
</table>
<p style="padding-left;">
<input class="button" type="submit" name="submit" value="Save Changes">
<input class="button" type="reset" name="reset" value="Reset Changes">
</p>
</form>
Hmm, but how do we find this page? It doesn't have a menu item! edit : /include/class.nav.php
function getSubMenus(){
...
$subnav=array();
switch(strtolower($k)){
...
case 'settings':
$subnav=array('desc'=>'System Preferences','href'=>'settings.php?t=system','iconclass'=>'preferences');
...
$subnav=array('desc'=>'Custom settings.','href'=>'settings.php?t=custom','iconclass'=>'preferences'); //Added menu item!
...
Alright, now we need some persistence, the extended Config class: (inside /include/class.config.php for now, but should be in a file like: class.custom-config.php and require the original)
How osTicket settings starts:
class OsticketConfig extends Config {
var $table = CONFIG_TABLE;
var $section = 'core';
I'm guessing I do something like:
/**
* Special Custom settings that should persist beyond upgrades.. ;-)
* <USERMENTION username="author">@author</USERMENTION> Grizly
*/
class CustomConfig extends Config{
var $table = CONFIG_TABLE;
var $section = 'custom';
/**
* Class constructor
* <USERMENTION username="param">@param</USERMENTION> string $section Make sure we specified our namespace above!
* <USERMENTION username="return">@return</USERMENTION> boolean
*/
function CustomConfig($section=null){
parent:($section);
return true;
}
/**
* Called when Admin page is saved. (or is it? I dunno.. how do you get it to do that?)
* Routes changes to the correct update function.. (is this necessary when you only have a single page of options?)
* <USERMENTION username="param">@param</USERMENTION> array $vars
* <USERMENTION username="param">@param</USERMENTION> unknown $errors
* <USERMENTION username="return">@return</USERMENTION> boolean|Ambigous <boolean, Ambigous, resource>
*/
function updateSettings($vars, &$errors) {
if(!$vars || $errors)
return false;
switch(strtolower($vars)) {
case 'specialpage':
return $this->updateCustomSettings($vars, $errors);
break;
default:
$errors='Unknown setting option. Get technical support.';
}
return false;
}
/**
* Find our precious setting
* eg: $customSettings = new CustomSettings();
* $important_setting = $customSettings->someThingImportant();
* <USERMENTION username="return">@return</USERMENTION> Ambigous <string, multitype:>
*/
function someThingImportant(){
return $this->get('important_setting');
}
/**
* Save our custom setting into the DB
*
* <USERMENTION username="param">@param</USERMENTION> unknown $vars
* <USERMENTION username="param">@param</USERMENTION> unknown $errors
* <USERMENTION username="return">@return</USERMENTION> boolean|Ambigous <resource, unknown>
*/
function updateCustomSettings($vars, &$errors) {
if ($errors)
return false;
$sql = 'UPDATE ' . CONFIG_TABLE . ' SET `updated`=NOW() '
. ',`value`=' . db_input($vars)
. " WHERE `key`='important_setting'";
return (db_query($sql));
}
}
Alright, so now I have to go back to settings.php and add some form of handler to //Handle a POST so that the setting actually gets checked? Otherwise all the persistence in the world will be ignored?
//Handle a POST.
if($_POST && !$errors) {
if($cfg && $cfg->updateSettings($_POST,$errors)) {
$msg=Format:($settingOptions[$_POST).' Updated Successfully';
$cfg->reload();
}elseif($customcfg && $customcfg->updateSettings($_POST,$errors)){ //Check for our custom settings! (you did instantiate a $customcfg object earlier right?)
$msg=Format:($settingOptions[$_POST).' Updated Custom Settings';
$customcfg->reload();
} elseif(!$errors) {
$errors='Unable to update settings - correct errors below and try again';
}
}
Yes? is so complicated..
Some guidance would be amazing! Anybody else done this?
I mean I get it, can't preserve mods through upgrades (yet), but surely I'm doing something wrong. All my other code is in external files, added as includes or require_onces to make rebasing as simple as possible.. but I just keep breaking this stuff. Have written a few unit tests, but its hard when you want to modify things like inbound emails, so you actually NEED an inbound email to test with.. gah.. I suppose I should simply setup an entire test arena to play with rather than fscking with my prod server.. users would probably be happier!
Maybe I'm going about it ALL WRONG.. hope so.. hope there is a much simpler way to achieve that. (also, you may find that the above doesn't work for you, it wasn't supposed to be a guide, just the bits that I remember I had to screw around with to add a setting to the admin settings.. which worked in the old one, and is now working in the new one, but currently I'm not running the class above, I just added stuff to the OstConfig class.. which seemed a lot easier, and of course, will break when the new version arrives)
Okies.. I'm getting a coffee.