Login

My Profile - Maintenance Table

This post is part of a series on our proof-of-concept module "My Profile". We are going to detail a maintenance table, presumably for a system administrator. The maintenance table allows an administrator to add, modify and delete records.

Before we get in to the code, let's refresh folks on what "My Profile" does and how it fits in to the rest of our Drupal installation.

My Profile adds a simple text field to each Drupal (website) user account. That text field is called "Alias".

A My Profile Alias is optional for user accounts.

That means that some users will have an alias, and other's not.

That also means that a user can only have one alias. You cannot add a second alias to a user.

So when we design our "Add Alias" routines for our administrator, we want to make sure the Administrator can only select user's who do not have an alias already.

Once a user is assigned a My Profile alias, then the administrator can update or delete (the user's alias).

Here is what our maintenance page is going to look like. We have just 3 users on the example website. You'll notice that we are using hyper links to our three methods (add, update and delete). We'll detail how to make a Drupal path (URL) to a custom form or function later.

Using multiple links avoids potential conflicts or complexities that using multiple forms invites. It's a simple but effective design pattern.

Here is the function that creates the above display:

function maint_table() {
 
    drupal_set_title("My Profile Maintenance Functions");
    $header = array("User","Alias","Modify","Delete");
    $rows['data'] = array();
 
    $sql = "select b.name, a.id, a.alias, a.uid 
        from {my_profile} a join {users} b on a.uid = b.uid";
    $dbResult = db_query($sql);
    $count = 0;
    if ($dbResult) {
        while ($row = db_fetch_object($dbResult)) {
            $edit_link = 
               l('Edit','my_profile/modify/'.$row->id.'/'.$row->name);
            $del_link = 
               l('Delete','my_profile/delete/'.$row->id.'/'.$row->name);
            $rows[] = array($row->name,$row->alias,$edit_link,$del_link);
        }
    }
    if (count($rows) == 1)
        $rows[] = array("No Colors Entered Yet");
 
    $add_link = l("Add A User Alias","my_profile/admin/select_users");
    return '<p>&nbsp;</p>'.$add_link."<br/>".theme_table($header, $rows);
}

Explanation:

We do a simple join between the users table and the my_profile table. That gives us a list of user's who have been assigned an alias. We build the array structure expected by the theme_table() function. We create a unique edit and delete link.

Note! A good follow-up exercise would be to remove the sql from this function and move the logic in to our database access object dbMy_profile .

We map the link url's (Drupal paths) to our custom code using hook_menu().

For example, the routine above creates a edit link that looks like this http://MYHOST/my_profile/modify/3/sandyshore. "3" is the numeric identifier for "sandyshore's" user record. The word "modify" will instruct our form and handlers that we are doing a modify (as opposed to a delete).

Below is the pertinent portions of our hook_menu() implementation:

function my_profile_menu() {
   $items['my_profile/maintenance'] = array(
        'page callback' => 'maint_table',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
    );
    $items['my_profile/%/%/%'] = array(
        'load arguments'=>array(4),
        'page callback' => 'drupal_get_form',
        'page arguments'=>array('modify_my_profile_form',1,2,3),   
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
    );
    $items['my_profile/admin/select_users'] = array(
        'page callback' => 'drupal_get_form',
        'page arguments'=>array('admin_select_user_form'),
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
    );
 
}

Explanation:

The first element (my_profile/maintenance) maps our maintenance table page (i.e. the maint_table function).

The second element (my_profile/%/%/%) is designed to give a little bit more flexibility. Instead of creating a separate element for modify and delete we let the caller pass that value. The "/%" is a placeholder. We also let the caller pass the record is and the user's name.

The third element maps to our add method.

We we will detail the add, modify and delete forms in the next post.