wizardhelper.class.inc.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class WizardHelper
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'/application/uiwizard.class.inc.php');
  25. class WizardHelper
  26. {
  27. protected $m_aData;
  28. public function __construct()
  29. {
  30. }
  31. /**
  32. * Constructs the PHP target object from the parameters sent to the web page by the wizard
  33. * @param boolean $bReadUploadedFiles True to also ready any uploaded file (for blob/document fields)
  34. * @return object
  35. */
  36. public function GetTargetObject($bReadUploadedFiles = false)
  37. {
  38. if (isset($this->m_aData['m_oCurrentValues']['id']))
  39. {
  40. $oObj = MetaModel::GetObject($this->m_aData['m_sClass'], $this->m_aData['m_oCurrentValues']['id']);
  41. }
  42. else
  43. {
  44. $oObj = MetaModel::NewObject($this->m_aData['m_sClass']);
  45. }
  46. foreach($this->m_aData['m_oCurrentValues'] as $sAttCode => $value)
  47. {
  48. // Because this is stored in a Javascript array, unused indexes
  49. // are filled with null values and unused keys (stored as strings) contain $$NULL$$
  50. if ( ($sAttCode !='id') && ($sAttCode !== false) && ($value !== null) && ($value !== '$$NULL$$'))
  51. {
  52. $oAttDef = MetaModel::GetAttributeDef($this->m_aData['m_sClass'], $sAttCode);
  53. if (($oAttDef->IsLinkSet()) && ($value != '') )
  54. {
  55. // special handling for lists
  56. // assumes this is handled as an array of objects
  57. // thus encoded in json like: [ { name:'link1', 'id': 123}, { name:'link2', 'id': 124}...]
  58. $aData = json_decode($value, true); // true means decode as a hash array (not an object)
  59. // Check what are the meaningful attributes
  60. $aFields = $this->GetLinkedWizardStructure($oAttDef);
  61. $sLinkedClass = $oAttDef->GetLinkedClass();
  62. $aLinkedObjectsArray = array();
  63. if (!is_array($aData))
  64. {
  65. echo ("aData: '$aData' (value: '$value')\n");
  66. }
  67. foreach($aData as $aLinkedObject)
  68. {
  69. $oLinkedObj = MetaModel::NewObject($sLinkedClass);
  70. foreach($aFields as $sLinkedAttCode)
  71. {
  72. if ( isset($aLinkedObject[$sLinkedAttCode]) && ($aLinkedObject[$sLinkedAttCode] !== null) )
  73. {
  74. $sLinkedAttDef = MetaModel::GetAttributeDef($sLinkedClass, $sLinkedAttCode);
  75. if (($sLinkedAttDef->IsExternalKey()) && ($aLinkedObject[$sLinkedAttCode] != '') && ($aLinkedObject[$sLinkedAttCode] > 0) )
  76. {
  77. // For external keys: load the target object so that external fields
  78. // get filled too
  79. $oTargetObj = MetaModel::GetObject($sLinkedAttDef->GetTargetClass(), $aLinkedObject[$sLinkedAttCode]);
  80. $oLinkedObj->Set($sLinkedAttCode, $oTargetObj);
  81. }
  82. else
  83. {
  84. $oLinkedObj->Set($sLinkedAttCode, $aLinkedObject[$sLinkedAttCode]);
  85. }
  86. }
  87. }
  88. $aLinkedObjectsArray[] = $oLinkedObj;
  89. }
  90. $oSet = DBObjectSet::FromArray($sLinkedClass, $aLinkedObjectsArray);
  91. $oObj->Set($sAttCode, $oSet);
  92. }
  93. else if ( $oAttDef->GetEditClass() == 'Document' )
  94. {
  95. if ($bReadUploadedFiles)
  96. {
  97. $oDocument = utils::ReadPostedDocument('attr_'.$sAttCode, 'fcontents');
  98. $oObj->Set($sAttCode, $oDocument);
  99. }
  100. else
  101. {
  102. // Create a new empty document, just for displaying the file name
  103. $oDocument = new ormDocument(null, '', $value);
  104. $oObj->Set($sAttCode, $oDocument);
  105. }
  106. }
  107. else if (($oAttDef->IsExternalKey()) && (!empty($value)) && ($value > 0) )
  108. {
  109. // For external keys: load the target object so that external fields
  110. // get filled too
  111. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value, false);
  112. if ($oTargetObj)
  113. {
  114. $oObj->Set($sAttCode, $oTargetObj);
  115. }
  116. else
  117. {
  118. // May happen for security reasons (portal, see ticket #1074)
  119. $oObj->Set($sAttCode, $value);
  120. }
  121. }
  122. else
  123. {
  124. $oObj->Set($sAttCode, $value);
  125. }
  126. }
  127. }
  128. if (isset($this->m_aData['m_sState']) && !empty($this->m_aData['m_sState']))
  129. {
  130. $oObj->Set(MetaModel::GetStateAttributeCode($this->m_aData['m_sClass']), $this->m_aData['m_sState']);
  131. }
  132. $oObj->DoComputeValues();
  133. return $oObj;
  134. }
  135. public function GetFieldsForDefaultValue()
  136. {
  137. return $this->m_aData['m_aDefaultValueRequested'];
  138. }
  139. public function SetDefaultValue($sAttCode, $value)
  140. {
  141. // Protect against a request for a non existing field
  142. if (isset($this->m_aData['m_oFieldsMap'][$sAttCode]))
  143. {
  144. $oAttDef = MetaModel::GetAttributeDef($this->m_aData['m_sClass'], $sAttCode);
  145. if ($oAttDef->GetEditClass() == 'List')
  146. {
  147. // special handling for lists
  148. // this as to be handled as an array of objects
  149. // thus encoded in json like: [ { name:'link1', 'id': 123}, { name:'link2', 'id': 124}...]
  150. // NOT YET IMPLEMENTED !!
  151. $sLinkedClass = $oAttDef->GetLinkedClass();
  152. $oSet = $value;
  153. $aData = array();
  154. $aFields = $this->GetLinkedWizardStructure($oAttDef);
  155. while($oSet->fetch())
  156. {
  157. foreach($aFields as $sLinkedAttCode)
  158. {
  159. $aRow[$sAttCode] = $oLinkedObj->Get($sLinkedAttCode);
  160. }
  161. $aData[] = $aRow;
  162. }
  163. $this->m_aData['m_oDefaultValue'][$sAttCode] = json_encode($aData);
  164. }
  165. else
  166. {
  167. // Normal handling for all other scalar attributes
  168. $this->m_aData['m_oDefaultValue'][$sAttCode] = $value;
  169. }
  170. }
  171. }
  172. public function GetFieldsForAllowedValues()
  173. {
  174. return $this->m_aData['m_aAllowedValuesRequested'];
  175. }
  176. public function SetAllowedValuesHtml($sAttCode, $sHtml)
  177. {
  178. // Protect against a request for a non existing field
  179. if (isset($this->m_aData['m_oFieldsMap'][$sAttCode]))
  180. {
  181. $this->m_aData['m_oAllowedValues'][$sAttCode] = $sHtml;
  182. }
  183. }
  184. public function ToJSON()
  185. {
  186. return json_encode($this->m_aData);
  187. }
  188. static public function FromJSON($sJSON)
  189. {
  190. $oWizHelper = new WizardHelper();
  191. if (get_magic_quotes_gpc())
  192. {
  193. $sJSON = stripslashes($sJSON);
  194. }
  195. $aData = json_decode($sJSON, true); // true means hash array instead of object
  196. $oWizHelper->m_aData = $aData;
  197. return $oWizHelper;
  198. }
  199. protected function GetLinkedWizardStructure($oAttDef)
  200. {
  201. $oWizard = new UIWizard(null, $oAttDef->GetLinkedClass());
  202. $aWizardSteps = $oWizard->GetWizardStructure();
  203. $aFields = array();
  204. $sExtKeyToMeCode = $oAttDef->GetExtKeyToMe();
  205. // Retrieve as a flat list, all the attributes that are needed to create
  206. // an object of the linked class and put them into a flat array, except
  207. // the attribute 'ext_key_to_me' which is a constant in our case
  208. foreach($aWizardSteps as $sDummy => $aMainSteps)
  209. {
  210. // 2 entries: 'mandatory' and 'optional'
  211. foreach($aMainSteps as $aSteps)
  212. {
  213. // One entry for each step of the wizard
  214. foreach($aSteps as $sAttCode)
  215. {
  216. if ($sAttCode != $sExtKeyToMeCode)
  217. {
  218. $aFields[] = $sAttCode;
  219. }
  220. }
  221. }
  222. }
  223. return $aFields;
  224. }
  225. public function GetTargetClass()
  226. {
  227. return $this->m_aData['m_sClass'];
  228. }
  229. public function GetFormPrefix()
  230. {
  231. return $this->m_aData['m_sFormPrefix'];
  232. }
  233. public function GetIdForField($sFieldName)
  234. {
  235. $sResult = '';
  236. // It may happen that the field we'd like to update does not
  237. // exist in the form. For example, if the field should be hidden/read-only
  238. // in the current state of the object
  239. if (isset($this->m_aData['m_oFieldsMap'][$sFieldName]))
  240. {
  241. $sResult = $this->m_aData['m_oFieldsMap'][$sFieldName];
  242. }
  243. return $sResult;
  244. }
  245. static function ParseJsonSet($oMe, $sLinkClass, $sExtKeyToMe, $sJsonSet)
  246. {
  247. $aSet = json_decode($sJsonSet, true); // true means hash array instead of object
  248. $oSet = CMDBObjectSet::FromScratch($sLinkClass);
  249. foreach($aSet as $aLinkObj)
  250. {
  251. $oLink = MetaModel::NewObject($sLinkClass);
  252. foreach($aLinkObj as $sAttCode => $value)
  253. {
  254. $oAttDef = MetaModel::GetAttributeDef($sLinkClass, $sAttCode);
  255. if (($oAttDef->IsExternalKey()) && ($value != '') && ($value > 0))
  256. {
  257. // For external keys: load the target object so that external fields
  258. // get filled too
  259. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value);
  260. $oLink->Set($sAttCode, $oTargetObj);
  261. }
  262. $oLink->Set($sAttCode, $value);
  263. }
  264. $oLink->Set($sExtKeyToMe, $oMe->GetKey());
  265. $oSet->AddObject($oLink);
  266. }
  267. return $oSet;
  268. }
  269. }
  270. ?>