#877 REST/JSON More flexibility on case log updates (in particular, it is now possible to write the entire case log), remains compatible with the previous API

SVN:trunk[3078]
This commit is contained in:
Romain Quetiez
2014-02-06 11:40:26 +00:00
parent bc0f48721b
commit cba75527b3
3 changed files with 127 additions and 10 deletions

View File

@@ -944,7 +944,14 @@ class RestUtils
foreach ($aFields as $sAttCode => $value)
{
$realValue = self::MakeValue($sClass, $sAttCode, $value);
$oObject->Set($sAttCode, $realValue);
try
{
$oObject->Set($sAttCode, $realValue);
}
catch (Exception $e)
{
throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
}
}
return $oObject;
}
@@ -964,7 +971,14 @@ class RestUtils
foreach ($aFields as $sAttCode => $value)
{
$realValue = self::MakeValue($sClass, $sAttCode, $value);
$oObject->Set($sAttCode, $realValue);
try
{
$oObject->Set($sAttCode, $realValue);
}
catch (Exception $e)
{
throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
}
}
return $oObject;
}

View File

@@ -1915,7 +1915,12 @@ class AttributeCaseLog extends AttributeLongText
// Facilitate things: allow the user to Set the value from a string
public function MakeRealValue($proposedValue, $oHostObj)
{
if (!($proposedValue instanceof ormCaseLog))
if ($proposedValue instanceof ormCaseLog)
{
// Passthrough
$ret = $proposedValue;
}
else
{
// Append the new value if an instance of the object is supplied
//
@@ -1937,13 +1942,21 @@ class AttributeCaseLog extends AttributeLongText
{
$oCaseLog = new ormCaseLog();
}
if (strlen($proposedValue) > 0)
if ($proposedValue instanceof stdClass)
{
$oCaseLog->AddLogEntry(parent::MakeRealValue($proposedValue, $oHostObj));
$oCaseLog->AddLogEntryFromJSON($proposedValue);
}
return $oCaseLog;
else
{
if (strlen($proposedValue) > 0)
{
$oCaseLog->AddLogEntry(parent::MakeRealValue($proposedValue, $oHostObj));
}
}
$ret = $oCaseLog;
}
return $proposedValue;
return $ret;
}
public function GetSQLExpressions($sPrefix = '')
@@ -2077,8 +2090,28 @@ class AttributeCaseLog extends AttributeLongText
*/
public function FromJSONToValue($json)
{
// Passthrough: new text to append to the log
return $json;
if (is_string($json))
{
// Will be correctly handled in MakeRealValue
$ret = $json;
}
else
{
if (isset($json->add_item))
{
// Will be correctly handled in MakeRealValue
$ret = $json->add_item;
if (!isset($ret->message))
{
throw new Exception("Missing mandatory entry: 'message'");
}
}
else
{
$ret = ormCaseLog::FromJSON($json);
}
}
return $ret;
}
}

View File

@@ -48,6 +48,20 @@ class ormCaseLog {
return $this->m_sLog;
}
public static function FromJSON($oJson)
{
if (!isset($oJson->items))
{
throw new Exception("Missing 'items' elements");
}
$oCaseLog = new ormCaseLog();
foreach($oJson->items as $oItem)
{
$oCaseLog->AddLogEntryFromJSON($oItem);
}
return $oCaseLog;
}
/**
* Return a value that will be further JSON encoded
*/
@@ -86,6 +100,7 @@ class ormCaseLog {
$aEntries[] = array(
'date' => $sDate,
'user_login' => $this->m_aIndex[$index]['user_name'],
'user_id' => $this->m_aIndex[$index]['user_id'],
'message' => $sTextEntry
);
}
@@ -289,7 +304,62 @@ class ormCaseLog {
}
$this->m_bModified = true;
}
public function AddLogEntryFromJSON($oJson)
{
$sText = isset($oJson->message) ? $oJson->message : '';
if (isset($oJson->user_id))
{
if (!UserRights::IsAdministrator())
{
throw new Exception("Only administrators can set the user id", RestResult::UNAUTHORIZED);
}
try
{
$oUser = RestUtils::FindObjectFromKey('User', $oJson->user_id);
}
catch(Exception $e)
{
throw new Exception('user_id: '.$e->getMessage(), $e->getCode());
}
$iUserId = $oUser->GetKey();
$sOnBehalfOf = $oUser->GetFriendlyName();
}
else
{
$iUserId = UserRights::GetUserId();
$sOnBehalfOf = UserRights::GetUserFriendlyName();
}
if (isset($oJson->date))
{
$oDate = new DateTime($oJson->date);
$iDate = (int) $oDate->format('U');
}
else
{
$iDate = time();
}
$sDate = date(Dict::S('UI:CaseLog:DateFormat'), $iDate);
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$iSepLength = strlen($sSeparator);
$iTextlength = strlen($sText);
$this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
$this->m_aIndex[] = array(
'user_name' => $sOnBehalfOf,
'user_id' => $iUserId,
'date' => $iDate,
'text_length' => $iTextlength,
'separator_length' => $iSepLength,
);
$this->m_bModified = true;
}
public function GetModifiedEntry()
{
$sModifiedEntry = '';