uiwizard.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 UIWizard
  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. class UIWizard
  25. {
  26. protected $m_oPage;
  27. protected $m_sClass;
  28. protected $m_sTargetState;
  29. protected $m_aWizardSteps;
  30. public function __construct($oPage, $sClass, $sTargetState = '')
  31. {
  32. $this->m_oPage = $oPage;
  33. $this->m_sClass = $sClass;
  34. if (empty($sTargetState))
  35. {
  36. $sTargetState = MetaModel::GetDefaultState($sClass);
  37. }
  38. $this->m_sTargetState = $sTargetState;
  39. $this->m_aWizardSteps = $this->ComputeWizardStructure();
  40. }
  41. public function GetObjectClass() { return $this->m_sClass; }
  42. public function GetTargetState() { return $this->m_sTargetState; }
  43. public function GetWizardStructure() { return $this->m_aWizardSteps; }
  44. /**
  45. * Displays one step of the wizard
  46. */
  47. public function DisplayWizardStep($aStep, $iStepIndex, &$iMaxInputId, &$aFieldsMap, $bFinishEnabled = false, $aArgs = array())
  48. {
  49. if ($iStepIndex == 1) // one big form that contains everything, to make sure that the uploaded files are posted too
  50. {
  51. $this->m_oPage->add("<form method=\"post\" enctype=\"multipart/form-data\" action=\"../pages/UI.php\">\n");
  52. }
  53. $this->m_oPage->add("<div class=\"wizContainer\" id=\"wizStep$iStepIndex\" style=\"display:none;\">\n");
  54. $this->m_oPage->add("<a name=\"step$iStepIndex\" />\n");
  55. $aStates = MetaModel::EnumStates($this->m_sClass);
  56. $aDetails = array();
  57. $sJSHandlerCode = ''; // Javascript code to be executed each time this step of the wizard is entered
  58. foreach($aStep as $sAttCode)
  59. {
  60. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  61. if ($oAttDef->IsWritable())
  62. {
  63. $sAttLabel = $oAttDef->GetLabel();
  64. $iOptions = isset($aStates[$this->m_sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$this->m_sTargetState]['attribute_list'][$sAttCode] : 0;
  65. $aPrerequisites = $oAttDef->GetPrerequisiteAttributes();
  66. if ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT))
  67. {
  68. $aFields[$sAttCode] = array();
  69. foreach($aPrerequisites as $sCode)
  70. {
  71. $aFields[$sAttCode][$sCode] = '';
  72. }
  73. }
  74. if (count($aPrerequisites) > 0)
  75. {
  76. $aOptions[] = 'Prerequisites: '.implode(', ', $aPrerequisites);
  77. }
  78. $sFieldFlag = (($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE)) || (!$oAttDef->IsNullAllowed()) )? ' <span class="hilite">*</span>' : '';
  79. $oDefaultValuesSet = $oAttDef->GetDefaultValue(/* $oObject->ToArgs() */); // @@@ TO DO: get the object's current value if the object exists
  80. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($this->m_oPage, $this->m_sClass, $sAttCode, $oAttDef, $oDefaultValuesSet, '', "att_$iMaxInputId", '', $iOptions, $aArgs);
  81. $aFieldsMap["att_$iMaxInputId"] = $sAttCode;
  82. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().$sFieldFlag.'</span>', 'value' => "<span id=\"field_att_$iMaxInputId\">$sHTMLValue</span>");
  83. if ($oAttDef->GetValuesDef() != null)
  84. {
  85. $sJSHandlerCode .= "\toWizardHelper.RequestAllowedValues('$sAttCode');\n";
  86. }
  87. if ($oAttDef->GetDefaultValue() != null)
  88. {
  89. $sJSHandlerCode .= "\toWizardHelper.RequestDefaultValue('$sAttCode');\n";
  90. }
  91. if ($oAttDef->IsLinkSet())
  92. {
  93. $sJSHandlerCode .= "\toLinkWidgetatt_$iMaxInputId.Init();";
  94. }
  95. $iMaxInputId++;
  96. }
  97. }
  98. //$aDetails[] = array('label' => '', 'value' => '<input type="button" value="Next &gt;&gt;">');
  99. $this->m_oPage->details($aDetails);
  100. $sBackButtonDisabled = ($iStepIndex <= 1) ? 'disabled' : '';
  101. $sDisabled = $bFinishEnabled ? '' : 'disabled';
  102. $nbSteps = count($this->m_aWizardSteps['mandatory']) + count($this->m_aWizardSteps['optional']);
  103. $this->m_oPage->add("<div style=\"text-align:center\">
  104. <input type=\"button\" value=\"".Dict::S('UI:Button:Back')."\" $sBackButtonDisabled onClick=\"GoToStep($iStepIndex, $iStepIndex - 1)\" />
  105. <input type=\"button\" value=\"".Dict::S('UI:Button:Next')."\" onClick=\"GoToStep($iStepIndex, 1+$iStepIndex)\" />
  106. <input type=\"button\" value=\"".Dict::S('UI:Button:Finish')."\" $sDisabled onClick=\"GoToStep($iStepIndex, 1+$nbSteps)\" />
  107. </div>\n");
  108. $this->m_oPage->add("
  109. <script type=\"text/javascript\">
  110. function OnEnterStep{$iStepIndex}()
  111. {
  112. oWizardHelper.ResetQuery();
  113. oWizardHelper.UpdateWizard();
  114. $sJSHandlerCode
  115. oWizardHelper.AjaxQueryServer();
  116. }
  117. </script>\n");
  118. $this->m_oPage->add("</div>\n\n");
  119. }
  120. /**
  121. * Display the final step of the wizard: a confirmation screen
  122. */
  123. public function DisplayFinalStep($iStepIndex, $aFieldsMap)
  124. {
  125. $oAppContext = new ApplicationContext();
  126. $this->m_oPage->add("<div class=\"wizContainer\" id=\"wizStep$iStepIndex\" style=\"display:none;\">\n");
  127. $this->m_oPage->add("<a name=\"step$iStepIndex\" />\n");
  128. $this->m_oPage->P(Dict::S('UI:Wizard:FinalStepTitle'));
  129. $this->m_oPage->add("<input type=\"hidden\" name=\"operation\" value=\"wizard_apply_new\" />\n");
  130. $this->m_oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\" />\n");
  131. $this->m_oPage->add("<input type=\"hidden\" id=\"wizard_json_obj\" name=\"json_obj\" value=\"\" />\n");
  132. $this->m_oPage->add("<script type=\"text/javascript\">\n");
  133. $this->m_oPage->add("function OnEnterStep$iStepIndex() {\n");
  134. foreach($aFieldsMap as $iInputId => $sAttCode)
  135. {
  136. $this->m_oPage->add("\toWizardHelper.UpdateCurrentValue('$sAttCode');\n");
  137. }
  138. $this->m_oPage->add("\toWizardHelper.Preview('object_preview');\n");
  139. $this->m_oPage->add("\t$('#wizard_json_obj').val(oWizardHelper.ToJSON());\n");
  140. $this->m_oPage->add("}\n");
  141. $this->m_oPage->add("</script>\n");
  142. $this->m_oPage->add("<div id=\"object_preview\">\n");
  143. $this->m_oPage->add("</div>\n");
  144. $this->m_oPage->add($oAppContext->GetForForm());
  145. $this->m_oPage->add("<input type=\"button\" value=\"".Dict::S('UI:Button:Back')."\" onClick=\"GoToStep($iStepIndex, $iStepIndex - 1)\" />");
  146. $this->m_oPage->add("<input type=\"submit\" value=\"Create ".MetaModel::GetName($this->m_sClass)."\" />\n");
  147. $this->m_oPage->add("</div>\n");
  148. $this->m_oPage->add("</form>\n");
  149. }
  150. /**
  151. * Compute the order of the fields & pages in the wizard
  152. * @param $oPage iTopWebPage The current page (used to display error messages)
  153. * @param $sClass string Name of the class
  154. * @param $sStateCode string Code of the target state of the object
  155. * @return hash Two dimensional array: each element represents the list of fields for a given page
  156. */
  157. protected function ComputeWizardStructure()
  158. {
  159. $aWizardSteps = array( 'mandatory' => array(), 'optional' => array());
  160. $aFieldsDone = array(); // Store all the fields that are already covered by a previous step of the wizard
  161. $aStates = MetaModel::EnumStates($this->m_sClass);
  162. $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass);
  163. $aMandatoryAttributes = array();
  164. // Some attributes are always mandatory independently of the state machine (if any)
  165. foreach(MetaModel::GetAttributesList($this->m_sClass) as $sAttCode)
  166. {
  167. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  168. if (!$oAttDef->IsExternalField() && !$oAttDef->IsNullAllowed() &&
  169. $oAttDef->IsWritable() && ($sAttCode != $sStateAttCode) )
  170. {
  171. $aMandatoryAttributes[$sAttCode] = OPT_ATT_MANDATORY;
  172. }
  173. }
  174. // Now check the attributes that are mandatory in the specified state
  175. if ( (!empty($this->m_sTargetState)) && (count($aStates[$this->m_sTargetState]['attribute_list']) > 0) )
  176. {
  177. // Check all the fields that *must* be included in the wizard for this
  178. // particular target state
  179. $aFields = array();
  180. foreach($aStates[$this->m_sTargetState]['attribute_list'] as $sAttCode => $iOptions)
  181. {
  182. if ( (isset($aMandatoryAttributes[$sAttCode])) &&
  183. ($aMandatoryAttributes[$sAttCode] & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) )
  184. {
  185. $aMandatoryAttributes[$sAttCode] |= $iOptions;
  186. }
  187. else
  188. {
  189. $aMandatoryAttributes[$sAttCode] = $iOptions;
  190. }
  191. }
  192. }
  193. // Check all the fields that *must* be included in the wizard
  194. // i.e. all mandatory, must-change or must-prompt fields that are
  195. // not also read-only or hidden.
  196. // Some fields may be required (null not allowed) from the database
  197. // perspective, but hidden or read-only from the user interface perspective
  198. $aFields = array();
  199. foreach($aMandatoryAttributes as $sAttCode => $iOptions)
  200. {
  201. if ( ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) &&
  202. !($iOptions & (OPT_ATT_READONLY | OPT_ATT_HIDDEN)) )
  203. {
  204. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  205. $aPrerequisites = $oAttDef->GetPrerequisiteAttributes();
  206. $aFields[$sAttCode] = array();
  207. foreach($aPrerequisites as $sCode)
  208. {
  209. $aFields[$sAttCode][$sCode] = '';
  210. }
  211. }
  212. }
  213. // Now use the dependencies between the fields to order them
  214. // Start from the order of the 'details'
  215. $aList = MetaModel::FlattenZlist(MetaModel::GetZListItems($this->m_sClass, 'details'));
  216. $index = 0;
  217. $aOrder = array();
  218. foreach($aFields as $sAttCode => $void)
  219. {
  220. $aOrder[$sAttCode] = 999; // At the end of the list...
  221. }
  222. foreach($aList as $sAttCode)
  223. {
  224. if (array_key_exists($sAttCode, $aFields))
  225. {
  226. $aOrder[$sAttCode] = $index;
  227. }
  228. $index++;
  229. }
  230. foreach($aFields as $sAttCode => $aDependencies)
  231. {
  232. // All fields with no remaining dependencies can be entered at this
  233. // step of the wizard
  234. if (count($aDependencies) > 0)
  235. {
  236. $iMaxPos = 0;
  237. // Remove this field from the dependencies of the other fields
  238. foreach($aDependencies as $sDependentAttCode => $void)
  239. {
  240. // position the current field after the ones it depends on
  241. $iMaxPos = max($iMaxPos, 1+$aOrder[$sDependentAttCode]);
  242. }
  243. }
  244. }
  245. asort($aOrder);
  246. $aCurrentStep = array();
  247. foreach($aOrder as $sAttCode => $rank)
  248. {
  249. $aCurrentStep[] = $sAttCode;
  250. $aFieldsDone[$sAttCode] = '';
  251. }
  252. $aWizardSteps['mandatory'][] = $aCurrentStep;
  253. // Now computes the steps to fill the optional fields
  254. $aFields = array(); // reset
  255. foreach(MetaModel::ListAttributeDefs($this->m_sClass) as $sAttCode=>$oAttDef)
  256. {
  257. $iOptions = (isset($aStates[$this->m_sTargetState]['attribute_list'][$sAttCode])) ? $aStates[$this->m_sTargetState]['attribute_list'][$sAttCode] : 0;
  258. if ( ($sStateAttCode != $sAttCode) &&
  259. (!$oAttDef->IsExternalField()) &&
  260. (($iOptions & (OPT_ATT_HIDDEN | OPT_ATT_READONLY)) == 0) &&
  261. (!isset($aFieldsDone[$sAttCode])) )
  262. {
  263. // 'State', external fields, read-only and hidden fields
  264. // and fields that are already listed in the wizard
  265. // are removed from the 'optional' part of the wizard
  266. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  267. $aPrerequisites = $oAttDef->GetPrerequisiteAttributes();
  268. $aFields[$sAttCode] = array();
  269. foreach($aPrerequisites as $sCode)
  270. {
  271. if (!isset($aFieldsDone[$sCode]))
  272. {
  273. // retain only the dependencies that were not covered
  274. // in the 'mandatory' part of the wizard
  275. $aFields[$sAttCode][$sCode] = '';
  276. }
  277. }
  278. }
  279. }
  280. // Now use the dependencies between the fields to order them
  281. while(count($aFields) > 0)
  282. {
  283. $aCurrentStep = array();
  284. foreach($aFields as $sAttCode => $aDependencies)
  285. {
  286. // All fields with no remaining dependencies can be entered at this
  287. // step of the wizard
  288. if (count($aDependencies) == 0)
  289. {
  290. $aCurrentStep[] = $sAttCode;
  291. $aFieldsDone[$sAttCode] = '';
  292. unset($aFields[$sAttCode]);
  293. // Remove this field from the dependencies of the other fields
  294. foreach($aFields as $sUpdatedCode => $aDummy)
  295. {
  296. // remove the dependency
  297. unset($aFields[$sUpdatedCode][$sAttCode]);
  298. }
  299. }
  300. }
  301. if (count($aCurrentStep) == 0)
  302. {
  303. // This step of the wizard would contain NO field !
  304. $this->m_oPage->add(Dict::S('UI:Error:WizardCircularReferenceInDependencies'));
  305. print_r($aFields);
  306. break;
  307. }
  308. $aWizardSteps['optional'][] = $aCurrentStep;
  309. }
  310. return $aWizardSteps;
  311. }
  312. }
  313. ?>