Преглед изворни кода

#869 REST JSON was not outputing case log attributes (implemented in a structured way)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3054 a333f486-631f-4898-b8df-5754b55c2be0
romainq пре 11 година
родитељ
комит
b5849da948
2 измењених фајлова са 78 додато и 0 уклоњено
  1. 19 0
      core/attributedef.class.inc.php
  2. 59 0
      core/ormcaselog.class.inc.php

+ 19 - 0
core/attributedef.class.inc.php

@@ -2061,6 +2061,25 @@ class AttributeCaseLog extends AttributeLongText
 			return '';
 		}
 	}
+
+	/**
+	 * Helper to get a value that will be JSON encoded
+	 * The operation is the opposite to FromJSONToValue	 
+	 */	 	
+	public function GetForJSON($value)
+	{
+		return $value->GetForJSON();
+	}
+
+	/**
+	 * Helper to form a value, given JSON decoded data
+	 * The operation is the opposite to GetForJSON	 
+	 */	 	
+	public function FromJSONToValue($json)
+	{
+		// Passthrough: new text to append to the log
+		return $json;
+	}
 }
 
 /**

+ 59 - 0
core/ormcaselog.class.inc.php

@@ -48,6 +48,65 @@ class ormCaseLog {
 		return $this->m_sLog;
 	}
 	
+	/**
+	 * Return a value that will be further JSON encoded	
+	 */	
+	public function GetForJSON()
+	{
+		$aEntries = array();
+		$iPos = 0;
+		for($index=count($this->m_aIndex)-1 ; $index >= 0 ; $index--)
+		{
+			$iPos += $this->m_aIndex[$index]['separator_length'];
+			$sTextEntry = substr($this->m_sLog, $iPos, $this->m_aIndex[$index]['text_length']);
+			$iPos += $this->m_aIndex[$index]['text_length'];
+
+			// Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
+			// therefore we have changed the format. To preserve the compatibility with existing
+			// installations of iTop, both format are allowed:
+			//     the 'date' item is either a DateTime object, or a unix timestamp
+			if (is_int($this->m_aIndex[$index]['date']))
+			{
+				// Unix timestamp
+				$sDate = date(Dict::S('UI:CaseLog:DateFormat'),$this->m_aIndex[$index]['date']);
+			}
+			elseif (is_object($this->m_aIndex[$index]['date']))
+			{
+				if (version_compare(phpversion(), '5.3.0', '>='))
+				{
+					// DateTime
+					$sDate = $this->m_aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
+				}
+				else
+				{
+					// No Warning... but the date is unknown
+					$sDate = '';
+				}
+			}
+			$aEntries[] = array(
+				'date' => $sDate,
+				'user_login' => $this->m_aIndex[$index]['user_name'],
+				'message' => $sTextEntry
+			);
+		}
+
+		// Process the case of an eventual remainder (quick migration of AttributeText fields)
+		if ($iPos < (strlen($this->m_sLog) - 1))
+		{
+			$sTextEntry = substr($this->m_sLog, $iPos);
+
+			$aEntries[] = array(
+				'date' => '',
+				'user_login' => '',
+				'message' => $sTextEntry
+			);
+		}
+
+		// Order by ascending date
+		$aRet = array('entries' => array_reverse($aEntries));
+		return $aRet;
+	}
+	
 	public function GetIndex()
 	{
 		return $this->m_aIndex;