wizardhelper.class.inc.php 8.9 KB

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