wizardhelper.class.inc.php 7.0 KB

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