4webby web applications
27
Mar 2007

FreakAuth_light: custom userprofile - PART 1

In this first tutorial I'll explain how to handle custom userprofile advanced crud functionality in FreakAuth_light 1.0.2-Beta .

I assume you have already installed both CI 1.5.2 and FreakAuth_light 1.0.2-Beta and that you are a bit familiar with them.

If you wanna play with custom userprofile in FreakAuth_light you should also have it enable in the FreakAith_light configuration:

application/config/freakauth_light.php
  1. //...
  2.  
  3. //TRUE/FALSE (boolean).  Whether to use custom user profile
  4. $config['FAL_create_user_profile'] = TRUE;   
  5.  
  6. //..

It's furthermore important that You understood the way CI handles form validation.

WHAT IS THE CUSTOM USERPROFILE CRUD?

Well as the name suggests the custom userprofile CRUD is a functionality similar to CI SCAFFOLDING. While with the CI SCAFFOLDING is more general and scaffolds every table of your DB providing a general functionality, the custom userprofile CRUD is related to the user_profile table of your database. Compared to the CI SCAFFOLDING the custom userprofile CRUD has the advantage of allowing to set your validation fields and validation rules.

The custom userprofile has been designed to allow to easily handle the need to add custom descriptions fields for your registered users besides:

  • username
  • password
  • e-mail

One thing is really important: the custom userprofile CRUD is an additional feature that should simplify your programming life. If you don't understand its functionality you can always build your controllers in the traditional way with CI validation, and DB select/insert/update functionalities.

To use the CUSTOM USERPROFILE CRUD of FreakAuth_light we need to:

  • add our custom userprofile fields in the DB table user_profile
  • define them, their relative validation labels and rules in your application/config/freakauth_light.php

ADDING CUSTOM USER_PROFILE FIELDS IN DB

Use your DB management software (phpMyAdmin I guess) to add your needed fields.

You can call your fields as you like and define them as you like in terms of data type.

ADDING CUSTOM USER_PROFILE FIELDS IN THE CONFIG FILE

Open your application/config/freakauth_light.php and go at the end of the file where you will find the following

  1. //----------------------------------------------------
  2. // CUSTOM USER FIELDS SETTINGS (FA_user_profile table)
  3. //----------------------------------------------------
  4. //you can set how many custom validation fields as you want
  5. //in the DB table they can be of any type (varchar, text, int etc.)
  6. //...
  7. //you can call your fields in DB as you like
  8. //The array keys in this config refer to the name of the fields in DB tables
  9. $config['FAL_user_profile_fields_names']= array('field_1'=>'name',
  10. 'field_2'=>'surname',
  11. 'call_me_nicely'=>'FreaKauth© rocks',
  12. //'4'=>'whatyouwant',
  13. //'5'=>'whatyouwant',
  14. );

  15. //set the validation rules for your custom user_profile fields here
  16. //if you need callback validation functions, remember to include them in your controllers
  17. $config['FAL_user_profile_fields_validation_rules']= array('field_1'=>'trim|required',
  18. 'field_2'=>'trim|required|max_length[50]',
  19. 'call_me_nicely'=>'trim|required|max_length[3]',
  20. //'field_4'=>'whatyouwant',
  21. //'field_5'=>'whatyouwant',
  22. );

The first array $config['FAL_user_profile_fields_names'] specifies :

  • $key--> name of the field in DB
  • $value--> the label that will be used during validation

The second array $config['FAL_user_profile_fields_validation_rules'] specifies:

  • $key--> name of the field in DB
  • $value--> validation rule for CI validation class

Therefore, summarising in both config arrays the $key refers to the name of the field in DB. If You:

  • add in the database table user_profile a field called 'first_name'
  • you want to set that field label as "first name" for validation
  • you want to set that field as required for your validation

iin your config arrays You should write:

 

  1. $config['FAL_user_profile_fields_names']= array('first_name'=>'name',
  2. );
  3. $config['FAL_user_profile_fields_validation_rules']= array('first_name'=>'trim|required',
  4. );

What if you have add the fields in DB and you have forgotten to add them in the config file application/config/freakauth_light.php?!?

Well no probs! The system will automagically bring the fields from DB if You didn't define them in the config of if you set less rules/fields in config with respect to the number of the fields in the table user_profile.

the system will use the table field name as field for the table fields not ruled in config, and won't set any rule for them.

USING LANGUAGE FILES IN FIELDS LABELS DEFINITION

If you want to use language file definitions in defining your labels you should add the following line before the array $config['FAL_user_profile_fields_names']

  1. $CI =&get_instance();
  2. $CI->lang->load('my_language_file');
  3. $config['FAL_user_profile_fields_names']= array('first_name'=>$CI->lang->line('my_lang_label')
  4. );

BACKEND ISSUES

Well if you followed the above steps properly you should now see all the custom user_profile fields when you view/edit and user from the backend.

To see your changes in the backend administration you don't need to change a line in the admin/users.php file!

FRONTEND ISSUES

We can use the custom userprofile crud in two circumstances for frontend:

  1. to add registration fields to our registration form
  2. to allow the registered user to set not-required additional profile fields after registering

Case 1 presents two problems in order to be fully authomathed with FAL custom userprofile CRUD:

  1. you should modified the user_temp table in order to temporarily store the additional custom userprofile fields
  2. probably you want to use only some user_profile fields as required in your registration, and use others to be optionally filled after registration. This can't be handled by the 'original' custom userprofile CRUD becaus, as clarified above it uses all fields of the user_profile DB table.

I'll present some solutions to case 1 in next tutorial.

Considering case 2 let's see how we can easily build a frontend controller to show the user custom profile and to allow the owner of the profile to edit it.

I will consider case 2 in the second part of this tutorial.

Go to FreakAuth_light: custom userprofile - PART 2