wizardhelper.class.inc.php 9.8 KB

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