uiwizard.class.inc.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class UIWizard
  3. {
  4. protected $m_oPage;
  5. protected $m_sClass;
  6. protected $m_sTargetState;
  7. protected $m_aWizardSteps;
  8. public function __construct($oPage, $sClass, $sTargetState = '')
  9. {
  10. $this->m_oPage = $oPage;
  11. $this->m_sClass = $sClass;
  12. if (empty($sTargetState))
  13. {
  14. $sTargetState = MetaModel::GetDefaultState($sClass);
  15. }
  16. $this->m_sTargetState = $sTargetState;
  17. $this->m_aWizardSteps = $this->ComputeWizardStructure();
  18. }
  19. public function GetObjectClass() { return $this->m_sClass; }
  20. public function GetTargetState() { return $this->m_sTargetState; }
  21. public function GetWizardStructure() { return $this->m_aWizardSteps; }
  22. /**
  23. * Displays one step of the wizard
  24. */
  25. public function DisplayWizardStep($aStep, $iStepIndex, &$iMaxInputId, &$aFieldsMap, $bFinishEnabled = false)
  26. {
  27. $this->m_oPage->add("<div class=\"wizContainer\" id=\"wizStep$iStepIndex\" style=\"display:none;\">\n");
  28. $this->m_oPage->add("<a name=\"step$iStepIndex\" />\n");
  29. $aStates = MetaModel::EnumStates($this->m_sClass);
  30. $aDetails = array();
  31. $sJSHandlerCode = ''; // Javascript code to be executed each time this step of the wizard is entered
  32. foreach($aStep as $sAttCode)
  33. {
  34. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  35. $sAttLabel = $oAttDef->GetLabel();
  36. $iOptions = isset($aStates[$this->m_sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$this->m_sTargetState]['attribute_list'][$sAttCode] : 0;
  37. $aPrerequisites = $oAttDef->GetPrerequisiteAttributes();
  38. if ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT))
  39. {
  40. $aFields[$sAttCode] = array();
  41. foreach($aPrerequisites as $sCode)
  42. {
  43. $aFields[$sAttCode][$sCode] = '';
  44. }
  45. }
  46. if (count($aPrerequisites) > 0)
  47. {
  48. $aOptions[] = 'Prerequisites: '.implode(', ', $aPrerequisites);
  49. }
  50. $sFieldFlag = ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE)) ? ' <span class="hilite">*</span>' : '';
  51. $oDefaultValuesSet = $oAttDef->GetDefaultValue(); // @@@ TO DO: get the object's current value if the object exists
  52. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($this->m_oPage, $this->m_sClass, $sAttCode, $oAttDef, $oDefaultValuesSet, '', "att_$iMaxInputId");
  53. $aFieldsMap[$iMaxInputId] = $sAttCode;
  54. $aDetails[] = array('label' => $oAttDef->GetLabel().$sFieldFlag, 'value' => "<div id=\"field_$iMaxInputId\">$sHTMLValue</div>");
  55. if ($oAttDef->GetValuesDef() != null)
  56. {
  57. $sJSHandlerCode .= "\toWizardHelper.RequestAllowedValues('$sAttCode');\n";
  58. }
  59. if ($oAttDef->GetDefaultValue() != null)
  60. {
  61. $sJSHandlerCode .= "\toWizardHelper.RequestDefaultValue('$sAttCode');\n";
  62. }
  63. if ($oAttDef->IsLinkSet())
  64. {
  65. $sJSHandlerCode .= "\toLinkWidgetatt_$iMaxInputId.Init();";
  66. }
  67. $iMaxInputId++;
  68. }
  69. //$aDetails[] = array('label' => '', 'value' => '<input type="button" value="Next &gt;&gt;">');
  70. $this->m_oPage->details($aDetails);
  71. $sBackButtonDisabled = ($iStepIndex <= 1) ? 'disabled' : '';
  72. $sDisabled = $bFinishEnabled ? '' : 'disabled';
  73. $nbSteps = count($this->m_aWizardSteps['mandatory']) + count($this->m_aWizardSteps['optional']);
  74. $this->m_oPage->add("<div style=\"text-align:center\">
  75. <input type=\"button\" value=\"&lt;&lt; Back \" $sBackButtonDisabled onClick=\"GoToStep($iStepIndex, $iStepIndex - 1)\">
  76. <input type=\"button\" value=\" Next &gt;&gt;\" onClick=\"GoToStep($iStepIndex, 1+$iStepIndex)\">
  77. <input type=\"button\" value=\" Finish \" $sDisabled onClick=\"GoToStep($iStepIndex, 1+$nbSteps)\">
  78. </div>\n");
  79. $this->m_oPage->add("
  80. <script>
  81. function OnEnterStep{$iStepIndex}()
  82. {
  83. oWizardHelper.ResetQuery();
  84. $sJSHandlerCode
  85. oWizardHelper.AjaxQueryServer();
  86. }
  87. </script>\n");
  88. $this->m_oPage->add("</div>\n\n");
  89. }
  90. /**
  91. * Display the final step of the wizard: a confirmation screen
  92. */
  93. public function DisplayFinalStep($iStepIndex, $aFieldsMap)
  94. {
  95. $this->m_oPage->add("<div class=\"wizContainer\" id=\"wizStep$iStepIndex\" style=\"display:none;\">\n");
  96. $this->m_oPage->add("<a name=\"step$iStepIndex\" />\n");
  97. $this->m_oPage->P("Final step: confirmation");
  98. $this->m_oPage->add("<form method=\"post\" action=\"../pages/UI.php\">\n");
  99. $this->m_oPage->add("<input type=\"hidden\" name=\"operation\" value=\"wizard_apply_new\">\n");
  100. $this->m_oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  101. $this->m_oPage->add("<input type=\"hidden\" id=\"wizard_json_obj\" name=\"json_obj\" value=\"\">\n");
  102. $this->m_oPage->add("<script>\n");
  103. $this->m_oPage->add("function OnEnterStep$iStepIndex() {\n");
  104. foreach($aFieldsMap as $iInputId => $sAttCode)
  105. {
  106. $this->m_oPage->add("\toWizardHelper.UpdateCurrentValue('$sAttCode');\n");
  107. }
  108. $this->m_oPage->add("\toWizardHelper.Preview('object_preview');\n");
  109. $this->m_oPage->add("\t$('#wizard_json_obj').val(oWizardHelper.ToJSON());\n");
  110. $this->m_oPage->add("}\n");
  111. $this->m_oPage->add("</script>\n");
  112. $this->m_oPage->add("<div id=\"object_preview\">\n");
  113. $this->m_oPage->add("</div>\n");
  114. $this->m_oPage->add("<input type=\"submit\" value=\"Create {$this->m_sClass}\">\n");
  115. $this->m_oPage->add("</form>\n");
  116. $this->m_oPage->add("</div>\n");
  117. }
  118. /**
  119. * Compute the order of the fields & pages in the wizard
  120. * @param $oPage iTopWebPage The current page (used to display error messages)
  121. * @param $sClass string Name of the class
  122. * @param $sStateCode string Code of the target state of the object
  123. * @return hash Two dimensional array: each element represents the list of fields for a given page
  124. */
  125. protected function ComputeWizardStructure()
  126. {
  127. $aWizardSteps = array( 'mandatory' => array(), 'optional' => array());
  128. $aFieldsDone = array(); // Store all the fields that are already covered by a previous step of the wizard
  129. $aStates = MetaModel::EnumStates($this->m_sClass);
  130. if ( (!empty($this->m_sTargetState)) && (count($aStates[$this->m_sTargetState]['attribute_list']) > 0) )
  131. {
  132. // Check all the fields that *must* be included in the wizard for this
  133. // particular target state
  134. $aFields = array();
  135. foreach($aStates[$this->m_sTargetState]['attribute_list'] as $sAttCode => $iOptions)
  136. {
  137. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  138. $sAttLabel = $oAttDef->GetLabel();
  139. if ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT))
  140. {
  141. $aPrerequisites = $oAttDef->GetPrerequisiteAttributes();
  142. $aFields[$sAttCode] = array();
  143. foreach($aPrerequisites as $sCode)
  144. {
  145. $aFields[$sAttCode][$sCode] = '';
  146. }
  147. }
  148. }
  149. // Now use the dependencies between the fields to order them
  150. while(count($aFields) > 0)
  151. {
  152. $aCurrentStep = array();
  153. foreach($aFields as $sAttCode => $aDependencies)
  154. {
  155. // All fields with no remaining dependencies can be entered at this
  156. // step of the wizard
  157. if (count($aDependencies) == 0)
  158. {
  159. $aCurrentStep[] = $sAttCode;
  160. $aFieldsDone[$sAttCode] = '';
  161. unset($aFields[$sAttCode]);
  162. // Remove this field from the dependencies of the other fields
  163. foreach($aFields as $sUpdatedCode => $aDummy)
  164. {
  165. // remove the dependency
  166. unset($aFields[$sUpdatedCode][$sAttCode]);
  167. }
  168. }
  169. }
  170. if (count($aCurrentStep) == 0)
  171. {
  172. // This step of the wizard would contain NO field !
  173. echo "<strong>Error:</strong> Circular reference in the dependencies between the fields.";
  174. print_r($aFields);
  175. break;
  176. }
  177. $aWizardSteps['mandatory'][] = $aCurrentStep;
  178. }
  179. }
  180. // Now computes the steps to fill the optional fields
  181. $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass);
  182. $aFields = array(); // reset
  183. foreach(MetaModel::ListAttributeDefs($this->m_sClass) as $sAttCode=>$oAttDef)
  184. {
  185. $iOptions = (isset($aStates[$this->m_sTargetState]['attribute_list'][$sAttCode])) ? $aStates[$this->m_sTargetState]['attribute_list'][$sAttCode] : 0;
  186. if ( ($sStateAttCode != $sAttCode) &&
  187. (!$oAttDef->IsExternalField()) &&
  188. (($iOptions & (OPT_ATT_HIDDEN | OPT_ATT_READONLY)) == 0) &&
  189. (!isset($aFieldsDone[$sAttCode])) )
  190. {
  191. // 'State', external fields, read-only and hidden fields
  192. // and fields that are already listed in the wizard
  193. // are removed from the 'optional' part of the wizard
  194. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  195. $aPrerequisites = $oAttDef->GetPrerequisiteAttributes();
  196. $aFields[$sAttCode] = array();
  197. foreach($aPrerequisites as $sCode)
  198. {
  199. if (!isset($aFieldsDone[$sCode]))
  200. {
  201. // retain only the dependencies that were not covered
  202. // in the 'mandatory' part of the wizard
  203. $aFields[$sAttCode][$sCode] = '';
  204. }
  205. }
  206. }
  207. }
  208. // Now use the dependencies between the fields to order them
  209. while(count($aFields) > 0)
  210. {
  211. $aCurrentStep = array();
  212. foreach($aFields as $sAttCode => $aDependencies)
  213. {
  214. // All fields with no remaining dependencies can be entered at this
  215. // step of the wizard
  216. if (count($aDependencies) == 0)
  217. {
  218. $aCurrentStep[] = $sAttCode;
  219. $aFieldsDone[$sAttCode] = '';
  220. unset($aFields[$sAttCode]);
  221. // Remove this field from the dependencies of the other fields
  222. foreach($aFields as $sUpdatedCode => $aDummy)
  223. {
  224. // remove the dependency
  225. unset($aFields[$sUpdatedCode][$sAttCode]);
  226. }
  227. }
  228. }
  229. if (count($aCurrentStep) == 0)
  230. {
  231. // This step of the wizard would contain NO field !
  232. $oPage->add("<strong>Error:</strong> Circular reference in the dependencies between the fields.");
  233. print_r($aFields);
  234. break;
  235. }
  236. $aWizardSteps['optional'][] = $aCurrentStep;
  237. }
  238. return $aWizardSteps;
  239. }
  240. }
  241. ?>