Skip to content

Instantly share code, notes, and snippets.

@dtbaker
Last active January 2, 2016 22:53
Show Gist options
  • Save dtbaker/ce8bf9006d56ae6eaac5 to your computer and use it in GitHub Desktop.
Save dtbaker/ce8bf9006d56ae6eaac5 to your computer and use it in GitHub Desktop.
Modify columns from the UCM table output (i.e. remove columns from Customer table)
<?php
// This example removes the "Customer" and "Group" and "Staff" columns from the main customer page.
// It also adds some new columns to the "Customer" and "Job" listing tables.
// Upload this file to includes/plugin_custom_table_hooks/custom_table_hooks.php in the UCM folder
class module_custom_table_hooks extends module_base{
public function init(){
hook_add('table_process_data','module_custom_table_hooks::hook_table_process_data');
}
public static function hook_table_process_data($callback, $table_manager){
echo "DEBUG: Processing hooks for table: ".$table_manager->table_id . '<br> Available columns are: '; echo implode(', ', array_keys( $table_manager->columns ) );
switch($table_manager->table_id){
case 'customer_list':
if(isset($table_manager->columns['company_name'])){
unset($table_manager->columns['company_name']);
}
if(isset($table_manager->columns['customer_group'])){
unset($table_manager->columns['customer_group']);
}
if(isset($table_manager->columns['customer_staff'])){
unset($table_manager->columns['customer_staff']);
}
// This will add new columns to the main "Customer" listing page.
if ( ! is_closure( $table_manager->row_callback ) ) {
$table_manager->row_callback = function () {
return array();
};
}
$old_row_callback = $table_manager->row_callback;
$new_row_callback = function ( $row_data ) use ( $old_row_callback ) {
$result = $old_row_callback( $row_data );
if ( ! is_array( $result ) ) {
$result = $row_data;
}
// populate data to display in the new rows defined below.
// 'extra_column_1' here is to match the 'extra_column_1' further down
$result['extra_column_1'] = "Some Data Here";
$result['extra_column_2'] = "Customer ID: " . $row_data['customer_id'];
// grab a list of all the customers invoicse and do some calculations.
$customer_invoices = module_invoice::get_invoices(array('customer_id'=>$row_data['customer_id']));
$total_due = 0;
$total_paid = 0;
foreach($customer_invoices as $invoice){
$invoice = module_invoice::get_invoice($invoice['invoice_id']);
$total_due += $invoice['total_amount_due'];
$total_paid += $invoice['total_amount_paid'];
}
$result['extra_column_3'] = "Paid: ".dollar($total_paid).", Due: ".dollar($total_due);
return $result;
};
$table_manager->row_callback = $new_row_callback;
$table_manager->columns['extra_column_1'] = array(
'title' => 'Col 1',
);
$table_manager->columns['extra_column_2'] = array(
'title' => 'Col 2',
);
$table_manager->columns['extra_column_3'] = array(
'title' => 'Col 3',
);
break;
case 'job_list':
// This will add new columns to the main "Jobs" listing page.
if ( ! is_closure( $table_manager->row_callback ) ) {
$table_manager->row_callback = function () {
return array();
};
}
$old_row_callback = $table_manager->row_callback;
$new_row_callback = function ( $row_data ) use ( $old_row_callback ) {
$result = $old_row_callback( $row_data );
if ( ! is_array( $result ) ) {
$result = $row_data;
}
// populate data to display in the new rows defined below.
// 'extra_column_1' here is to match the 'extra_column_1' further down
$result['extra_column_1'] = "Some Data Here";
$result['extra_column_2'] = "Customer/Job ID: " . $row_data['customer_id'].'/'.$row_data['job_id'];
// grab a list of all the customers invoicse and do some calculations.
$customer_invoices = module_invoice::get_invoices(array('job_id'=>$row_data['job_id']));
$total_due = 0;
$total_paid = 0;
foreach($customer_invoices as $invoice){
$invoice = module_invoice::get_invoice($invoice['invoice_id']);
$total_due += $invoice['total_amount_due'];
$total_paid += $invoice['total_amount_paid'];
}
$result['extra_column_3'] = "Paid: ".dollar($total_paid).", Due: ".dollar($total_due);
return $result;
};
$table_manager->row_callback = $new_row_callback;
$table_manager->columns['extra_column_1'] = array(
'title' => 'Col 1',
);
$table_manager->columns['extra_column_2'] = array(
'title' => 'Col 2',
);
$table_manager->columns['extra_column_3'] = array(
'title' => 'Col 3',
);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment