wizardhelper.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. // Copyright (C) 2010-2016 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-2016 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 read 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') && ($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. elseif($sLinkedAttDef instanceof AttributeDateTime)
  83. {
  84. $sDate = $aLinkedObject[$sLinkedAttCode];
  85. if($sDate !== null && $sDate !== '')
  86. {
  87. $oDateTimeFormat = AttributeDateTime::GetFormat();
  88. $oDate = $oDateTimeFormat->Parse($sDate);
  89. $sDate = $oDate->format('Y-m-d H:i:s');
  90. }
  91. $oLinkedObj->Set($sLinkedAttCode, $sDate);
  92. }
  93. else
  94. {
  95. $oLinkedObj->Set($sLinkedAttCode, $aLinkedObject[$sLinkedAttCode]);
  96. }
  97. }
  98. }
  99. $aLinkedObjectsArray[] = $oLinkedObj;
  100. }
  101. $oSet = DBObjectSet::FromArray($sLinkedClass, $aLinkedObjectsArray);
  102. $oObj->Set($sAttCode, $oSet);
  103. }
  104. else if ( $oAttDef->GetEditClass() == 'Document' )
  105. {
  106. if ($bReadUploadedFiles)
  107. {
  108. $oDocument = utils::ReadPostedDocument('attr_'.$sAttCode, 'fcontents');
  109. $oObj->Set($sAttCode, $oDocument);
  110. }
  111. else
  112. {
  113. // Create a new empty document, just for displaying the file name
  114. $oDocument = new ormDocument(null, '', $value);
  115. $oObj->Set($sAttCode, $oDocument);
  116. }
  117. }
  118. else if ( $oAttDef->GetEditClass() == 'Image' )
  119. {
  120. if ($bReadUploadedFiles)
  121. {
  122. $oDocument = utils::ReadPostedDocument('attr_'.$sAttCode, 'fcontents');
  123. $oObj->Set($sAttCode, $oDocument);
  124. }
  125. else
  126. {
  127. // Create a new empty document, just for displaying the file name
  128. $oDocument = new ormDocument(null, '', $value);
  129. $oObj->Set($sAttCode, $oDocument);
  130. }
  131. }
  132. else if (($oAttDef->IsExternalKey()) && (!empty($value)) && ($value > 0) )
  133. {
  134. // For external keys: load the target object so that external fields
  135. // get filled too
  136. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value, false);
  137. if ($oTargetObj)
  138. {
  139. $oObj->Set($sAttCode, $oTargetObj);
  140. }
  141. else
  142. {
  143. // May happen for security reasons (portal, see ticket #1074)
  144. $oObj->Set($sAttCode, $value);
  145. }
  146. }
  147. else if ($oAttDef instanceof AttributeDateTime) // AttributeDate is derived from AttributeDateTime
  148. {
  149. if ($value != null)
  150. {
  151. $oDate = $oAttDef->GetFormat()->Parse($value);
  152. if ($oDate instanceof DateTime)
  153. {
  154. $value = $oDate->format($oAttDef->GetInternalFormat());
  155. }
  156. else
  157. {
  158. $value = null;
  159. }
  160. }
  161. $oObj->Set($sAttCode, $value);
  162. }
  163. else
  164. {
  165. $oObj->Set($sAttCode, $value);
  166. }
  167. }
  168. }
  169. if (isset($this->m_aData['m_sState']) && !empty($this->m_aData['m_sState']))
  170. {
  171. $oObj->Set(MetaModel::GetStateAttributeCode($this->m_aData['m_sClass']), $this->m_aData['m_sState']);
  172. }
  173. $oObj->DoComputeValues();
  174. return $oObj;
  175. }
  176. public function GetFieldsForDefaultValue()
  177. {
  178. return $this->m_aData['m_aDefaultValueRequested'];
  179. }
  180. public function SetDefaultValue($sAttCode, $value)
  181. {
  182. // Protect against a request for a non existing field
  183. if (isset($this->m_aData['m_oFieldsMap'][$sAttCode]))
  184. {
  185. $oAttDef = MetaModel::GetAttributeDef($this->m_aData['m_sClass'], $sAttCode);
  186. if ($oAttDef->GetEditClass() == 'List')
  187. {
  188. // special handling for lists
  189. // this as to be handled as an array of objects
  190. // thus encoded in json like: [ { name:'link1', 'id': 123}, { name:'link2', 'id': 124}...]
  191. // NOT YET IMPLEMENTED !!
  192. $sLinkedClass = $oAttDef->GetLinkedClass();
  193. $oSet = $value;
  194. $aData = array();
  195. $aFields = $this->GetLinkedWizardStructure($oAttDef);
  196. while($oSet->fetch())
  197. {
  198. foreach($aFields as $sLinkedAttCode)
  199. {
  200. $aRow[$sAttCode] = $oLinkedObj->Get($sLinkedAttCode);
  201. }
  202. $aData[] = $aRow;
  203. }
  204. $this->m_aData['m_oDefaultValue'][$sAttCode] = json_encode($aData);
  205. }
  206. else
  207. {
  208. // Normal handling for all other scalar attributes
  209. $this->m_aData['m_oDefaultValue'][$sAttCode] = $value;
  210. }
  211. }
  212. }
  213. public function GetFieldsForAllowedValues()
  214. {
  215. return $this->m_aData['m_aAllowedValuesRequested'];
  216. }
  217. public function SetAllowedValuesHtml($sAttCode, $sHtml)
  218. {
  219. // Protect against a request for a non existing field
  220. if (isset($this->m_aData['m_oFieldsMap'][$sAttCode]))
  221. {
  222. $this->m_aData['m_oAllowedValues'][$sAttCode] = $sHtml;
  223. }
  224. }
  225. public function ToJSON()
  226. {
  227. return json_encode($this->m_aData);
  228. }
  229. static public function FromJSON($sJSON)
  230. {
  231. $oWizHelper = new WizardHelper();
  232. if (get_magic_quotes_gpc())
  233. {
  234. $sJSON = stripslashes($sJSON);
  235. }
  236. $aData = json_decode($sJSON, true); // true means hash array instead of object
  237. $oWizHelper->m_aData = $aData;
  238. return $oWizHelper;
  239. }
  240. protected function GetLinkedWizardStructure($oAttDef)
  241. {
  242. $oWizard = new UIWizard(null, $oAttDef->GetLinkedClass());
  243. $aWizardSteps = $oWizard->GetWizardStructure();
  244. $aFields = array();
  245. $sExtKeyToMeCode = $oAttDef->GetExtKeyToMe();
  246. // Retrieve as a flat list, all the attributes that are needed to create
  247. // an object of the linked class and put them into a flat array, except
  248. // the attribute 'ext_key_to_me' which is a constant in our case
  249. foreach($aWizardSteps as $sDummy => $aMainSteps)
  250. {
  251. // 2 entries: 'mandatory' and 'optional'
  252. foreach($aMainSteps as $aSteps)
  253. {
  254. // One entry for each step of the wizard
  255. foreach($aSteps as $sAttCode)
  256. {
  257. if ($sAttCode != $sExtKeyToMeCode)
  258. {
  259. $aFields[] = $sAttCode;
  260. }
  261. }
  262. }
  263. }
  264. return $aFields;
  265. }
  266. public function GetTargetClass()
  267. {
  268. return $this->m_aData['m_sClass'];
  269. }
  270. public function GetFormPrefix()
  271. {
  272. return $this->m_aData['m_sFormPrefix'];
  273. }
  274. public function GetInitialState()
  275. {
  276. return isset($this->m_aData['m_sInitialState']) ? $this->m_aData['m_sInitialState'] : null;
  277. }
  278. public function GetStimulus()
  279. {
  280. return isset($this->m_aData['m_sStimulus']) ? $this->m_aData['m_sStimulus'] : null;
  281. }
  282. public function GetIdForField($sFieldName)
  283. {
  284. $sResult = '';
  285. // It may happen that the field we'd like to update does not
  286. // exist in the form. For example, if the field should be hidden/read-only
  287. // in the current state of the object
  288. if (isset($this->m_aData['m_oFieldsMap'][$sFieldName]))
  289. {
  290. $sResult = $this->m_aData['m_oFieldsMap'][$sFieldName];
  291. }
  292. return $sResult;
  293. }
  294. static function ParseJsonSet($oMe, $sLinkClass, $sExtKeyToMe, $sJsonSet)
  295. {
  296. $aSet = json_decode($sJsonSet, true); // true means hash array instead of object
  297. $oSet = CMDBObjectSet::FromScratch($sLinkClass);
  298. foreach($aSet as $aLinkObj)
  299. {
  300. $oLink = MetaModel::NewObject($sLinkClass);
  301. foreach($aLinkObj as $sAttCode => $value)
  302. {
  303. $oAttDef = MetaModel::GetAttributeDef($sLinkClass, $sAttCode);
  304. if (($oAttDef->IsExternalKey()) && ($value != '') && ($value > 0))
  305. {
  306. // For external keys: load the target object so that external fields
  307. // get filled too
  308. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value);
  309. $oLink->Set($sAttCode, $oTargetObj);
  310. }
  311. $oLink->Set($sAttCode, $value);
  312. }
  313. $oLink->Set($sExtKeyToMe, $oMe->GetKey());
  314. $oSet->AddObject($oLink);
  315. }
  316. return $oSet;
  317. }
  318. }