/**
  * Format the activity description
  * @param $description
  * @return string
  */
 private function formatDescription($description)
 {
     //handle worklog fields
     if (SugarAutoLoader::fileExists('custom/include/SugarFields/Fields/Worklog/SugarFieldWorklogHelpers.php') && SugarFieldWorklogHelpers::isJson($description)) {
         $description = SugarFieldWorklogHelpers::decodeJsonValue($description);
     }
     $description = htmlspecialchars($description, ENT_QUOTES);
     $description = nl2br($description);
     return $description;
 }
 /**
  * This method adds an entry with the given string to the given bean and attribute.  This
  * function assumes the given field name of the given bean is a worklog field.
  * @param $bean
  * @param $field
  * @param $logEntry
  */
 public static function addLogEntry($bean, $field, $logEntry)
 {
     $value = '';
     $worklogs = array();
     if (!empty($bean->id) && (!isset($bean->new_with_id) || $bean->new_with_id == false)) {
         if (isset($bean->fetched_row[$field]) && !empty($bean->fetched_row[$field])) {
             $fetched_row = html_entity_decode($bean->fetched_row[$field], ENT_QUOTES);
             //if the stored value is json
             if (SugarFieldWorklogHelpers::isJson($fetched_row)) {
                 $value = $fetched_row;
                 $worklogs = json_decode($fetched_row, true);
             } else {
                 $value = $fetched_row;
                 //reformat the db value to add old logs into one message
                 $worklogs[0] = array('msg' => $fetched_row);
             }
         }
     }
     //if weve already updated the worklog during this save and the value is already json
     if (SugarFieldWorklogHelpers::isJson($bean->{$field})) {
         $worklogAdditions = json_decode($bean->{$field}, true);
         foreach ($worklogAdditions as $key => $worklogAddition) {
             if (isset($worklogAddition['tsp']) && isset($worklogAddition['usr']) && isset($worklogAddition['msg'])) {
                 $worklogs[$key] = $worklogAddition;
             }
         }
         $value = json_encode($worklogs);
     }
     if (empty($logEntry)) {
         $bean->{$field} = $value;
     } else {
         global $current_user;
         $timestamp = time();
         if (isset($worklogs[$timestamp])) {
             $worklogs[$timestamp]['msg'] .= "\n" . $logEntry;
         } else {
             $worklogs[$timestamp] = array('tsp' => $timestamp, 'usr' => $current_user->id, 'msg' => $logEntry);
         }
         $bean->{$field} = json_encode($worklogs);
     }
 }