wizardhelper.class.inc.php 7.2 KB

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