wizardhelper.class.inc.php 8.3 KB

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