fuelphp custom configurations

I recently built my client management / quote / invoice system using fuelPHP, and needed to add some defaults settings when a new user registers to use the system.

For example, a user signs up, their details are stored in a users table, but users also have settings such as quote item rates (£30, £40, £300 etc) and quote item types (Hours, Days, Weekly), expose types (Parking, Telephone ill etc).

This information is used when a user, for instance, creates a quote, they will add items to the quote, and will be able to choose from their rates and types, so for example: web development, 40 Hours at £30 per hour.

So. Where do i store these defaults? I thought of storing them in a database tables, then copying the data to the real tables mapped to the user. That would certainly work, however, as i was learning fuelPHP, i thought i would use a custom config file, so i created this file in fuel/app/config/userdefaults.php:

[code]

return array(
‘Setting’ => array(
array(
‘key’ => ‘currency_symbol’,
‘name’ => ‘Currency Symbol’,
‘value’ => ‘£’
),
array(
‘key’ => ‘quote_accepted_message’,
‘name’ => ‘Quote Accepted Message’,
‘value’ => ‘Great! Thanks for accepting my quote.’
),
array(
‘key’ => ‘quote_rejected_message’,
‘name’ => ‘Quote Rejected Message’,
‘value’ => ‘Oh No! Sorry that you haven\’t accepted my quote.’
),
),

‘InvoiceType’ =>array(
array(
‘name’ => ‘30% Deposit’,
‘percentage’ => ’30’
),
array(
‘name’ => ‘30% Midway Payment’,
‘percentage’ => ’30’
),
array(
‘name’ => ‘40% On Completion’,
‘percentage’ => ’30’
),
array(
‘name’ => ‘Paayment In Full’,
‘percentage’ => ‘100’
),
array(
‘name’ => ‘50% Deposit’,
‘percentage’ => ’50’
),
array(
‘name’ => ‘50% On Completion’,
‘percentage’ => ’50’
),

),

‘Quoteitemtype’ => array(‘Hours’, ‘Days’, ‘Weekly’),

‘Quoteitemrate’ => array(’20’, ’30’, ’50’, ‘100’, ‘250’),

‘Expensetype’ => array(‘Office Equipment’, ‘Tools’, ‘Parking’, ‘Telephone Costs’),

);
[/code]

When a user signs up, I load this config and create the user and related objects dynamically, populating with the config data, like so:

[code]
$def = Config::get(‘userdefaults’);
foreach ($def as $class=>$data)
{
$m = ‘Model_’ .$class;
foreach ($data as $row)
{
$model = new $m;
if ( is_array($row) )
{
foreach ($row as $k=>$v)
{
$model->{$k} = $v;
}
$model->user_id = Auth::instance()->get(‘id’);
$model->save();
}
else
{
$model->name = $row;
$model->user_id = Auth::instance()->get(‘id’);
$model->save();
}
}
}
[/code]

As that was my first attempt, i will now change the actual config file to use a consistent format, using array keys as table fields as per the Setting array.




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *