SugarCRM: Change color of Meetings in Calendar according to Assigned User

sugarcrmdevelopers —  April 9, 2013

This post will help you change the colors of Meetings in Calendar depending on the user it was assign. This is similar with the other tutorial about changing colors in Calendar according to status but has additional editing required.

* Note that this is not upgrade safe

1. Open /modules/Calendar/CalendarDisplay.php and there you can see the $activity_colors array with color values depending on the type of record. Add the ff. codes below to the __construct of the class:

global $db;
// Get all users
$sqlQuery = "SELECT u.id, uc.calendar_color_c FROM users u LEFT JOIN users_cstm uc ON u.id=uc.id_c WHERE u.deleted=0";
$sqlResult = $db->query($sqlQuery);
// Loop through the records
while($row = $db->fetchByAssoc($sqlResult)) {
   // Check record
   if (!empty($row['id])) {
      // Check if record doesn't have a color
      if (empty($row['calendar_color_c'])) {
          // Assign default color
          $row['calendar_color_c'] = '#028025';
      }
      // Add each record to $activity_colors
      $this->activity_colors[$row['id']] = array('body' = >$row['calendar_color_c'], 'border' => $row['calendar_color_c']);
   }
}

* Note that this example assumes that you have a custom field for users which each user has assigned color.

2. Open /modules/Calendar/Cal.js and find the function CAL.create_item=function(params) and change this codes:

el.style.backgroundColor=CAL.activity_colors[item.module_name]['body'];
el.style.borderColor=CAL.activity_colors[item.module_name]['border'];

with this codes:

if(item.module_name == "Meetings"){
    el.style.backgroundColor=CAL.activity_colors[item.assigned_user_id]['body'];
    el.style.borderColor=CAL.activity_colors[item.assigned_user_id]['border'];
}
else {
    el.style.backgroundColor=CAL.activity_colors[item.module_name]['body'];
    el.style.borderColor=CAL.activity_colors[item.module_name]['border'];
}

* this will set the color when the record is Meeting

This will work when you go to Calendar module but when you try to create a new Meeting in Calendar or adjust the date of the Meeting, it will case error because the assigned user id is not automatically passed when the calendar makes an ajax request. To fix this, follow step 3.

3. Open /modules/Calendar/CalendarUtils.php and find get_fields function and add the assigned user id to the Meetings:

static function get_fields() {
    return array(
           'Meetings' => array(
               'name',
               'duration_hours',
               'duration_minutes',
               'status',
               'assigned_user_id',
            ),
            'Calls' => array(
               'name',
               'duration_hours',
               'duration_minutes',
               'status',
            ),
            'Tasks' => array(
               'name',
               'status',
             ),
         );
}

OR find get_sendback_array function and add the assigned_user_id in the $arr variable:

$arr = array(
'access' => 'yes',
'type' => strtolower($bean->object_name),
'module_name' => $bean->module_dir,
'user_id' => $GLOBALS['current_user']->id,
'detail' => 1,
'edit' => 1,
'name' => $bean->name,
'record' => $bean->id,
'users' => $user_ids,
'assigned_user_id' => $bean->assigned_user_id,
);