function smarty_function_convert_worklog($params, &$smarty)
{
    if (empty($params['log'])) {
        $smarty->trigger_error("Worklog: missing 'log' parameter");
    }
    global $current_user;
    return SugarFieldWorklogHelpers::decodeJsonValue(trim(html_entity_decode($params['log'], ENT_QUOTES)), $current_user, true);
}
 /**
  * Extends save to add handleWorklogSave
  * @param $bean
  * @param $params
  * @param string $field
  * @param array $properties
  * @param string $prefix
  */
 public function save($bean, $params, $field, $properties, $prefix = '')
 {
     parent::save($bean, $params, $field, $properties, $prefix);
     SugarFieldWorklogHelpers::addLogEntry($bean, $field, $bean->{$field});
 }
 /**
  * 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);
     }
 }