wizardhelper.class.inc.php 8.7 KB

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