Meet Sitellite's successor: Elefant CMS
A modern PHP framework and content management system based on the improved features of PHP 5.3+. Elefant is an extremely fast and easy to use CMS that inherits all the best of Sitellite, without the fat. Learn more »
Modifying User Add/Edit Forms
Assumptions: You've successfully installed Sitellite, tested it and found everything to be working.
Project: Add some additional fields to the User form. The User form can be found by logging into your Sitellite installation, then go to Control Panel -> Users -> click on a specific username.
There are many user related fields here already. However, our particular project requires a few additional fields that aren't here. We need to add some.
Add the new fields to your sitellite_user table in your sitellite database.
If you aren't sure how to do this, you should visit www.mysql.com and lookup the alter table syntax. Below is the standard sitellite_user table. The additional fields that I've added all begin with the prefix "apt_". Once you've added you're new fields, we'll be ready to move on to the next step.
+---------------------------+----------------------+
| Field | Type |
+---------------------------+----------------------+
| id | int(11) |
| username | varchar(48) |
| password | varchar(64) |
| firstname | varchar(32) |
| lastname | varchar(32) |
| email | varchar(42) |
| role | varchar(32) |
| team | varchar(32) |
| disabled | enum('no','yes') |
| tips | enum('on','off') |
| lang | varchar(12) |
| session_id | varchar(32) |
| expires | timestamp |
| company | varchar(48) |
| position | varchar(48) |
| apt_certifications | varchar(64) |
| apt_join_mailing_list | enum('false','true') |
| apt_join_volunteer_list | enum('false','true') |
| apt_join_workshop_list | enum('false','true') |
| apt_subject_comment | varchar(128) |
| apt_membership_expiration | datetime |
| apt_officer_title | varchar(128) |
| website | varchar(72) |
| jabber_id | varchar(48) |
| sms_address | varchar(72) |
| phone | varchar(24) |
| cell | varchar(24) |
| home | varchar(24) |
| fax | varchar(24) |
| address1 | varchar(72) |
| address2 | varchar(72) |
| city | varchar(48) |
| province | varchar(48) |
| postal_code | varchar(16) |
| country | varchar(48) |
| teams | char(255) |
| public | enum('yes','no') |
| profile | text |
| sig | text |
| registered | datetime |
| modified | timestamp |
+---------------------------+----------------------|
Add a Text Field to the User Form
You'll need to modify saf/Session/Manager.php. Open up this file and scroll down to function &getAddForm ().Your first modification will be within this function. Now scroll down to the widget that adds the country field to your User form (the add-new-user version). It should look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->extra = 'maxlength="48"';
We are going to add our text field widget (apt_officer_title) immediately under the country widget. After we've added our widget, this section should now look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->extra = 'maxlength="48"';
$w =& $form->addWidget ('text', 'apt_officer_title');
$w->alt = intl_get ('APT Officer Title');
$w->extra = 'maxlength="48"';
$w->extra = 'size="48"';
Now we'll need to scroll down to function &getEditForm ($item). Your next modification will be within this function. Now scroll down to the widget that adds the country field to your User form (the edit-existing-user version). It should look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->setValue ($user->country);
$w->extra = 'maxlength="48"';
We are going to add our text field widget (apt_officer_title) immediately under the country widget. After we've added our widget, this section should now look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->setValue ($user->country);
$w->extra = 'maxlength="48"';
$w =& $form->addWidget ('text', 'apt_officer_title');
$w->alt = intl_get ('APT Officer Title');
$w->setValue ($user->apt_officer_title);
$w->extra = 'maxlength="48"';
$w->extra = 'size="48"';
Of course, your text field will likely have a different name then mine. In that case, you should note the following regarding formatting your widget.
#1 $w =& $form->addWidget ('text', 'apt_officer_title');
The first argument 'text' indicates that this will be a text field in the User form (we'll take a look at other types of form fields later). The second argument 'apt_officer_title' must match the new field name you added to the sitellite_user table. If you added a new field in the sitellite_user table named 'favorite_color' then your version of this line would look like this
$w =& $form->addWidget ('text', 'favorite_color');
#2 $w->alt = intl_get ('APT Officer Title');
This is just a human readable label that will appear next to the new field in you User form. based on the example above, you would probably want to change this to the following:
$w->alt = intl_get ('Favorite Color');
However, you can really change this to anything you like ...
#3 $w->setValue ($user->apt_officer_title);
This sets the value of that field when the form opens. Presumably, you wanted to set this field in the form to the same value stored in that field in the database. However, if you were very fond of the color 'purple', you could assign that string to the new form field by doing this:
$w->setValue ('purple');
At this point you should test the User form by logging in as the administrator, going to Control Panel -> Users -> click on a specific user. Your new field should appear just underneath the country field in this form. Make sure that you can enter data into this field, save it to the database and view it again when you open up that user's information in the User form. Likewise, you make sure your new field appears as expected in the Add New User form.
Add a Select Field to the User Form
Again, you'll need to modify saf/Session/Manager.php. Starting with function &getAddForm ().Scroll down to the widget that adds the country field to your User form (the add-new-user version). It should look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->extra = 'maxlength="48"';
We are going to add our text field widget (apt_join_mailing_list) immediately under the country widget. After we've added our widget, this section should now look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->extra = 'maxlength="48"';
$w =& $form->addWidget ('select', 'apt_join_mailing_list');
$w->alt = intl_get ('APT Mailing List');
$w->setValues(array(
"false" => "No",
"true" => "Yes"
));
$w->setValue ("false");
Now we'll need to scroll down to function &getEditForm ($item). Your next modification will be within this function. Now scroll down to the widget that adds the country field to your User form (the edit-existing-user version). It should look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->setValue ($user->country);
$w->extra = 'maxlength="48"';
We are going to add our text field widget (apt_join_mailing_list) immediately under the country widget. After we've added our widget, this section should now look like this:
$w =& $form->addWidget ('text', 'country');
$w->alt = intl_get ('Country');
$w->setValue ($user->country);
$w->extra = 'maxlength="48"';
$w =& $form->addWidget ('select', 'apt_join_mailing_list');
$w->alt = intl_get ('APT Mailing List');
$w->setValues(array(
"false" => "No",
"true" => "Yes"
));
$w->setValue ($user->apt_join_mailing_list);
Again, your select field will likely have a different name then mine. In that case, the rules above for formatting your widget still hold true. One item that's a little different is the $w-setValue() method.
$w->setValues(array( "false" => "No", "true" => "Yes" ));
This array provides values in the form of Option-Value => Option-Display. In this case, "false" is the value that get sent to the database when "No" is displayed. Likewise, "true" is the value that gets sent to the database when "Yes" is displayed.
$w->setValue ("false");
You can assign a default value to appear when adding a new user. In this case, the option-dislpay that appears in your form will be "No", because you've assigned the option-value of "false".
At this point you should test the User form by logging in as the administrator, going to Control Panel -> Users -> click on a specific user. Your new field should appear just underneath the country field in this form. Make sure that you can enter data into this field, save it to the database and view it again when you open up that user's information in the User form. Likewise, you make sure your new field appears as expected in the Add New User form.
Create a New Tab for Your New Fields
Sometimes you'll only need to add one or two additional fields that can fit easily and sensibly onto one of the existing tabs. At other times you may need to add a larger grouping of fields that might make more sense to group together in their own new tab. Adding a new tab is fairly straightforward, but it does involve modifying a couple of files.
Let's start with the file that we've been working on: saf/Session/Manager.php. Each new tab starts with a couple lines of code that looks similar to one of the following:
$w =& $form->addWidget ('tab', 'tab1');
$w->title = intl_get ('Account');
$w =& $form->addWidget ('tab', 'tab2');
$w->title = intl_get ('Contact');
$w =& $form->addWidget ('tab', 'tab3');
$w->title = intl_get ('APT');
And, while there may be serveral tab creation sections, there is only one tab ending sections.
$w =& $form->addWidget ('tab', 'tab-end');
If we wanted to create a new tab, we could begin our tab immediately after the last widget in the tab3 section. I would create a tab4 definition and then add the fields that I wanted to display in this new tab. Remember, there is only one tab-end section no matter how many tab creation sections there are. Also remember, you'll need to create your tab in both function &getAddForm () and function &getEditForm ($item).
$w =& $form->addWidget ('tab', 'tab4');
$w->title = intl_get ('APT');
$w =& $form->addWidget ('text', 'apt_officer_title');
$w->alt = intl_get ('APT Officer Title');
$w->setValue ($user->apt_officer_title);
$w->extra = 'maxlength="48"';
$w->extra = 'size="48"';
$w =& $form->addWidget ('select', 'apt_join_mailing_list');
$w->alt = intl_get ('APT Mailing List');
$w->setValues(array(
"false" => "No",
"true" => "Yes"
));
$w->setValue ($user->apt_join_mailing_list);
$w =& $form->addWidget ('tab', 'tab-end');
There are two other files that will need to be modified in order for your new tab to work correctly. These files are
inc/app/usradm/boxes/add/user/index.php
inc/app/usradm/boxes/edit/user/index.php
Both files will have a section that looks similar to this:
unset ($vals['_list']); unset ($vals['tab1']); unset ($vals['tab2']); unset ($vals['tab3']); unset ($vals['tab-end']); unset ($vals['password_verify']); unset ($vals['submit_button']);
You just need to add a section for your new tab name in both files. The result will look similar to this:
unset ($vals['_list']); unset ($vals['tab1']); unset ($vals['tab2']); unset ($vals['tab3']); unset ($vals['tab4']); unset ($vals['tab-end']); unset ($vals['password_verify']); unset ($vals['submit_button']);
After your done, your new tab with your new fields should appear on the User form.
