Thursday, April 3, 2008

Adding new sections in a table view

Inside of the actions.ini file, if you want to use a xataface function you have to surround it with semicolons. So for example, {getDealerID()} would return the dealer id.


So the first thing I wanted to talk about is how to add custom sections to the viewing of a table. By default, the view of a record in a table will display all the columns for that record. But what happens if you want to display so other information from another table that had a relationship with that table record?

For example, say I have a table of companies and this company had a relationship to a table called news. I could setup a relationship in the relationship.ini file, but this would just create a tab in the record view to go click. But what if I wanted to view it would having to go into a tab. Like this:




The way to do this is to first create a function in the table delegate class to that will retrieve that field. So basically we make a custom field:


function field__recent_news(&$record){
//first parameter is table,
//second is the where part of the query
return df_get_records_array('news', array('company_id'=>$record->val('company_id'), 'owner_type'=>'COMPANY','-sort'=>'published_date desc','-limit'=>10));
}


The df_get_records_array function uses the first parameter as the table we are interested in, and the second parameter is the part used to in the where part of the query. So in this case, we are using the news table, where we want the company_id of the row equal to the record company id, etc, etc. Note, we use the -sort here because that is like an action as opposed to a simple additional of a query variable.

The next part is to add the section function:


function section__recent_news(&$record){

return array(
'class'=>'left',
'records'=>$record->val('recent_news'), //there is a calculated field from the function field_section
'label'=>'Recent Updates',
'url'=>DATAFACE_SITE_HREF.'?-action=list&-table=news&company_id='.$record->val('company_id').'&owner_type=COMPANY'
);
}


The name of this function can actually be section__XXX, but we just give it a good name. Here the class attribute means what side it will be displayed in. The records are what we are grabbing. So remember that function we made above that makes a custom field, we are now grabbing this field. I think by default it will display the first column of the table (that is not the primary key or foreign key). Label is the label of the section, and url is the condition I think the url has to fulfill to make sure that this function gets run.

No comments: