wizardhelper.class.inc.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. require_once('../application/uiwizard.class.inc.php');
  3. class WizardHelper
  4. {
  5. protected $m_aData;
  6. public function __construct()
  7. {
  8. }
  9. public function GetTargetObject()
  10. {
  11. $oObj = MetaModel::NewObject($this->m_aData['m_sClass']);
  12. foreach($this->m_aData['m_aCurrentValues'] as $iIndex => $value)
  13. {
  14. $sAttCode = array_search($iIndex, $this->m_aData['m_oFieldsMap']);
  15. // Because this is stored in a Javascript array, unused indexes
  16. // are filled with null values
  17. if ( ($sAttCode !== false) && ($value !== null))
  18. {
  19. $oAttDef = MetaModel::GetAttributeDef($this->m_aData['m_sClass'], $sAttCode);
  20. if (($oAttDef->IsLinkSet()) && ($value != '') )
  21. {
  22. // special handling for lists
  23. // assumes this is handled as an array of objects
  24. // thus encoded in json like: [ { name:'link1', 'id': 123}, { name:'link2', 'id': 124}...]
  25. $aData = json_decode($value, true); // true means decode as a hash array (not an object)
  26. // Check what are the meaningful attributes
  27. $aFields = $this->GetLinkedWizardStructure($oAttDef);
  28. $sLinkedClass = $oAttDef->GetLinkedClass();
  29. $aLinkedObjectsArray = array();
  30. if (!is_array($aData))
  31. {
  32. echo ("aData: '$aData' (value: '$value')\n");
  33. }
  34. foreach($aData as $aLinkedObject)
  35. {
  36. $oLinkedObj = MetaModel::NewObject($sLinkedClass);
  37. foreach($aFields as $sLinkedAttCode)
  38. {
  39. if ( isset($aLinkedObject[$sLinkedAttCode]) && ($aLinkedObject[$sLinkedAttCode] !== null) )
  40. {
  41. $sLinkedAttDef = MetaModel::GetAttributeDef($sLinkedClass, $sLinkedAttCode);
  42. if (($sLinkedAttDef->IsExternalKey()) && ($aLinkedObject[$sLinkedAttCode] != '') )
  43. {
  44. // For external keys: load the target object so that external fields
  45. // get filled too
  46. $oTargetObj = MetaModel::GetObject($sLinkedAttDef->GetTargetClass(), $aLinkedObject[$sLinkedAttCode]);
  47. $oLinkedObj->Set($sLinkedAttCode, $oTargetObj);
  48. }
  49. else
  50. {
  51. $oLinkedObj->Set($sLinkedAttCode, $aLinkedObject[$sLinkedAttCode]);
  52. }
  53. }
  54. }
  55. $aLinkedObjectsArray[] = $oLinkedObj;
  56. }
  57. $oSet = DBObjectSet::FromArray($sLinkedClass, $aLinkedObjectsArray);
  58. $oObj->Set($sAttCode, $oSet);
  59. }
  60. else if (($oAttDef->IsExternalKey()) && ($value != '') )
  61. {
  62. // For external keys: load the target object so that external fields
  63. // get filled too
  64. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value);
  65. $oObj->Set($sAttCode, $oTargetObj);
  66. }
  67. else
  68. {
  69. $oObj->Set($sAttCode, $value);
  70. }
  71. }
  72. }
  73. return $oObj;
  74. }
  75. public function GetFieldsForDefaultValue()
  76. {
  77. return $this->m_aData['m_aDefaultValueRequested'];
  78. }
  79. public function SetDefaultValue($sAttCode, $value)
  80. {
  81. // Protect against a request for a non existing field
  82. if (isset($this->m_aData['m_oFieldsMap'][$sAttCode]))
  83. {
  84. $iIndex = $this->m_aData['m_oFieldsMap'][$sAttCode];
  85. $oAttDef = MetaModel::GetAttributeDef($this->m_aData['m_sClass'], $sAttCode);
  86. if ($oAttDef->GetEditClass() == 'List')
  87. {
  88. // special handling for lists
  89. // this as to be handled as an array of objects
  90. // thus encoded in json like: [ { name:'link1', 'id': 123}, { name:'link2', 'id': 124}...]
  91. // NOT YET IMPLEMENTED !!
  92. $sLinkedClass = $oAttDef->GetLinkedClass();
  93. $oSet = $value;
  94. $aData = array();
  95. $aFields = $this->GetLinkedWizardStructure($oAttDef);
  96. while($oSet->fetch())
  97. {
  98. foreach($aFields as $sLinkedAttCode)
  99. {
  100. $aRow[$sAttCode] = $oLinkedObj->Get($sLinkedAttCode);
  101. }
  102. $aData[] = $aRow;
  103. }
  104. $this->m_aData['m_aDefaultValue'][$iIndex] = json_encode($aData);
  105. }
  106. else
  107. {
  108. // Normal handling for all other scalar attributes
  109. $this->m_aData['m_aDefaultValue'][$iIndex] = $value;
  110. }
  111. }
  112. }
  113. public function GetFieldsForAllowedValues()
  114. {
  115. return $this->m_aData['m_aAllowedValuesRequested'];
  116. }
  117. public function SetAllowedValuesHtml($sAttCode, $sHtml)
  118. {
  119. // Protect against a request for a non existing field
  120. if (isset($this->m_aData['m_oFieldsMap'][$sAttCode]))
  121. {
  122. $iIndex = $this->m_aData['m_oFieldsMap'][$sAttCode];
  123. $this->m_aData['m_aAllowedValues'][$iIndex] = $sHtml;
  124. }
  125. }
  126. public function ToJSON()
  127. {
  128. return json_encode($this->m_aData);
  129. }
  130. static public function FromJSON($sJSON)
  131. {
  132. $oWizHelper = new WizardHelper();
  133. if (get_magic_quotes_gpc())
  134. {
  135. $sJSON = stripslashes($sJSON);
  136. }
  137. $aData = json_decode($sJSON, true); // true means hash array instead of object
  138. $oWizHelper->m_aData = $aData;
  139. return $oWizHelper;
  140. }
  141. protected function GetLinkedWizardStructure($oAttDef)
  142. {
  143. $oWizard = new UIWizard(null, $oAttDef->GetLinkedClass());
  144. $aWizardSteps = $oWizard->GetWizardStructure();
  145. $aFields = array();
  146. $sExtKeyToMeCode = $oAttDef->GetExtKeyToMe();
  147. // Retrieve as a flat list, all the attributes that are needed to create
  148. // an object of the linked class and put them into a flat array, except
  149. // the attribute 'ext_key_to_me' which is a constant in our case
  150. foreach($aWizardSteps as $sDummy => $aMainSteps)
  151. {
  152. // 2 entries: 'mandatory' and 'optional'
  153. foreach($aMainSteps as $aSteps)
  154. {
  155. // One entry for each step of the wizard
  156. foreach($aSteps as $sAttCode)
  157. {
  158. if ($sAttCode != $sExtKeyToMeCode)
  159. {
  160. $aFields[] = $sAttCode;
  161. }
  162. }
  163. }
  164. }
  165. return $aFields;
  166. }
  167. static function ParseJsonSet($oMe, $sLinkClass, $sExtKeyToMe, $sJsonSet)
  168. {
  169. $aSet = json_decode($sJsonSet, true); // true means hash array instead of object
  170. $oSet = CMDBObjectSet::FromScratch($sLinkClass);
  171. foreach($aSet as $aLinkObj)
  172. {
  173. $oLink = MetaModel::NewObject($sLinkClass);
  174. foreach($aLinkObj as $sAttCode => $value)
  175. {
  176. $oAttDef = MetaModel::GetAttributeDef($sLinkClass, $sAttCode);
  177. if (($oAttDef->IsExternalKey()) && ($value != '') )
  178. {
  179. // For external keys: load the target object so that external fields
  180. // get filled too
  181. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value);
  182. $oLink->Set($sAttCode, $oTargetObj);
  183. }
  184. $oLink->Set($sAttCode, $value);
  185. }
  186. $oLink->Set($sExtKeyToMe, $oMe->GetKey());
  187. $oSet->AddObject($oLink);
  188. }
  189. return $oSet;
  190. }
  191. }
  192. ?>