setup.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. function NameIsValid(name)
  2. {
  3. sName = new String(name);
  4. if (sName.match(/^[A-Za-z][A-Za-z0-9_]*$/)) return true;
  5. return false;
  6. }
  7. function DoGoBack(iStep)
  8. {
  9. $('input[name=operation]').val('step'+iStep);
  10. $(':button').attr('disabled', 'disabled');
  11. $('#theForm').submit(); // Submit the form
  12. return true;
  13. }
  14. function DoSubmit(sMsg, iStep)
  15. {
  16. var bResult = true;
  17. switch(iStep)
  18. {
  19. case 0: // Select either install or upgrade or nothing to select...
  20. if ( ($("input:radio").length > 0) && ($("input:radio:checked").length < 1))
  21. {
  22. alert('Please select either install or upgrade');
  23. bResult = false;
  24. }
  25. break;
  26. case 1: // Licence agreement
  27. if ($('#licence_ok:checked').length < 1)
  28. {
  29. alert('Please accept the licence agreement before continuing.');
  30. bResult = false;
  31. }
  32. break;
  33. case 2: // Database server selection
  34. if ($('#db_server').val() == '')
  35. {
  36. alert('Please specify a database server. Use "localhost" for a local DB server.');
  37. bResult = false;
  38. }
  39. else if ($('#db_user').val() == '')
  40. {
  41. alert('Please specify a user name to connect to the database.');
  42. bResult = false;
  43. }
  44. break;
  45. case 3: // Database instance selection
  46. if ($("input[@type=radio]:checked").length < 1)
  47. {
  48. alert('Please specify a database name');
  49. bResult = false;
  50. }
  51. else if( ($("#new_db:checked").length == 1))
  52. {
  53. if ($('#new_db_name').val() == '')
  54. {
  55. alert('Please specify the name of the database to create');
  56. bResult = false;
  57. }
  58. else if (!NameIsValid($('#new_db_name').val()))
  59. {
  60. alert($('#new_db_name').val()+' is not a valid database name. Please limit yourself to letters, numbers and the underscore character.');
  61. bResult = false;
  62. }
  63. }
  64. else if ($("#current_db:checked").length == 1)
  65. {
  66. // Special case (DB enumeration failed, user must enter DB name)
  67. if ($("#current_db_name").val() == '')
  68. {
  69. alert('Please specify the name of the database.');
  70. bResult = false;
  71. }
  72. else
  73. {
  74. // Copy the typed value as the value of the radio
  75. $("#current_db").val($("#current_db_name").val());
  76. }
  77. }
  78. if( ($('#db_prefix').val() != '') && (!NameIsValid($('#db_prefix').val())) )
  79. {
  80. alert($('#db_prefix').val()+' is not a valid table name. Please limit yourself to letters, numbers and the underscore character.');
  81. bResult = false;
  82. }
  83. break;
  84. case 4: // Choice of iTop modules
  85. break;
  86. case 5: // Administrator account
  87. if ($('#auth_user').val() == '')
  88. {
  89. alert('Please specify a login name for the administrator account');
  90. bResult = false;
  91. }
  92. else if ($('#auth_pwd').val() != $('#auth_pwd2').val())
  93. {
  94. alert('Retyped password does not match! Please verify the password.');
  95. bResult = false;
  96. }
  97. break;
  98. case 6: // application path
  99. var appPath = new String($('#application_path').val());
  100. if (appPath == '')
  101. {
  102. alert('Please specify the URL to the application');
  103. bResult = false;
  104. }
  105. else
  106. {
  107. var bMatch = appPath.match(/^http(?:s)?\:\/\//);
  108. if (!bMatch)
  109. {
  110. alert('"'+appPath+'" does not look like a valid URL for the application...\nPlease check your input.');
  111. bResult = false;
  112. }
  113. else
  114. {
  115. // Make sure that the root URL ends with a slash
  116. var bMatch = appPath.match(/\/$/);
  117. if (!bMatch)
  118. {
  119. // If not, add a slash at the end
  120. appPath += '/';
  121. $('#application_path').val(appPath);
  122. }
  123. }
  124. }
  125. break;
  126. case 7: // Sample data selection
  127. break;
  128. case 8: // Display Summary: launch DoCompileDataModel to start the asynchronous update
  129. bResult = DoCompileDataModel();
  130. break;
  131. // Email test page
  132. case 10:
  133. if ($('#to').val() == '')
  134. {
  135. alert('Please specify a destination address');
  136. bResult = false;
  137. }
  138. }
  139. if (bResult)
  140. {
  141. $(':button').attr('disabled', 'disabled');
  142. if ((sMsg != ''))
  143. {
  144. $('#setup').block({message: '<img src="../images/indicator.gif">&nbsp;'+sMsg});
  145. }
  146. }
  147. return bResult;
  148. }
  149. function DoCompileDataModel()
  150. {
  151. $('#log').html('');
  152. $('#setup').block({message: '<p><span id="setup_msg">Preparing data model...</span><br/><div id=\"progress\">0%</div></p>'});
  153. $('#progress').progression( {Current:5, Maximum: 100, aBackgroundImg: GetAbsoluteUrlAppRoot()+'setup/orange-progress.gif', aTextColor: '#000000'} );
  154. var sSelectedModules = GetSelectedModules();
  155. var sMode = $(':input[name=mode]').val();
  156. var sSourceDir = $(':input[name=source_dir]').val();
  157. var sTargetDir = $(':input[name=target_dir]').val();
  158. // Call the asynchronous page that performs the compilation of the data model and the creation of the configuration file
  159. AsyncCompileDataModel(sSelectedModules, sMode, sSourceDir, sTargetDir, '', function(response, status, xhr) {
  160. $('#log').html(response);
  161. DoUpdateDBSchema();
  162. });
  163. }
  164. function DoUpdateDBSchema()
  165. {
  166. $('#log').html('');
  167. $('#setup').block({message: '<p><span id="setup_msg">Updating DB schema...</span><br/><div id=\"progress\">5%</div></p>'});
  168. $('#progress').progression( {Current:10, Maximum: 100, aBackgroundImg: GetAbsoluteUrlAppRoot()+'setup/orange-progress.gif', aTextColor: '#000000'} );
  169. var sSelectedModules = GetSelectedModules();
  170. var sMode = $(':input[name=mode]').val();
  171. var sModulesDir = $(':input[name=target_dir]').val();
  172. var sDBServer = $(':input[name=db_server]').val();
  173. var sDBUser = $(':input[name=db_user]').val();
  174. var sDBPwd = $(':input[name=db_pwd]').val();
  175. var sDBName = $(':input[name=db_name]').val();
  176. var sNewDBName = $(':input[name=new_db_name]').val();
  177. var sDBPrefix = $(':input[name=db_prefix]').val();
  178. // Call the asynchronous page that performs the creation/update of the DB Schema
  179. AsyncUpdateDBSchema(sSelectedModules, sMode, sModulesDir, sDBServer, sDBUser, sDBPwd, sDBName, sNewDBName, sDBPrefix, function(response, status, xhr) {
  180. $('#log').html(response);
  181. DoUpdateProfiles();
  182. });
  183. }
  184. function DoUpdateProfiles()
  185. {
  186. $('#log').html('');
  187. $('#setup_msg').text('Updating Profiles...');
  188. $('#progress').progression( {Current:40, Maximum: 100, aBackgroundImg: GetAbsoluteUrlAppRoot()+'setup/orange-progress.gif', aTextColor: '#000000'} );
  189. var sSelectedModules = GetSelectedModules();
  190. var sMode = $(':input[name=mode]').val();
  191. var sModulesDir = $(':input[name=target_dir]').val();
  192. var sDBServer = $(':input[name=db_server]').val();
  193. var sDBUser = $(':input[name=db_user]').val();
  194. var sDBPwd = $(':input[name=db_pwd]').val();
  195. var sDBName = $(':input[name=db_name]').val();
  196. var sNewDBName = $(':input[name=new_db_name]').val();
  197. var sDBPrefix = $(':input[name=db_prefix]').val();
  198. var sAuthUser = $(':input[name=auth_user]').val();
  199. var sAuthPwd = $(':input[name=auth_pwd]').val();
  200. var sLanguage = $(':input[name=language]').val();
  201. // Call the asynchronous page that performs the creation/update of the DB Schema
  202. AsyncUpdateProfiles(sSelectedModules, sMode, sModulesDir, sDBServer, sDBUser, sDBPwd, sDBName, sNewDBName, sDBPrefix, sAuthUser, sAuthPwd, sLanguage, function(response, status, xhr) {
  203. $('#log').html(response);
  204. DoLoadDataAsynchronous();
  205. });
  206. }
  207. var aFilesToLoad = new Array();
  208. var iCounter = 0;
  209. function DoLoadDataAsynchronous(response, status, xhr)
  210. {
  211. if (status == 'error')
  212. {
  213. $('#setup').unblock();
  214. return; // An error occurred !
  215. }
  216. try
  217. {
  218. // The array aFilesToLoad is populated by this function dynamically written on the server
  219. PopulateDataFilesList();
  220. iCurrent = 60;
  221. if (aFilesToLoad.length == 0)
  222. {
  223. $('#progress').progression( {Current: 100} );
  224. }
  225. else
  226. {
  227. $('#log').html('');
  228. $('#setup_msg').text('Loading data...');
  229. $('#progress').progression( {Current: 60, Maximum: 100, aBackgroundImg: GetAbsoluteUrlAppRoot()+'setup/orange-progress.gif', aTextColor: '#000000'} );
  230. // $('#log').ajaxError(
  231. // function(e, xhr, settings, exception)
  232. // {
  233. // bStopAysncProcess = true;
  234. // alert('Fatal error detected: '+ xhr.responseText);
  235. // $('#log').append(xhr.responseText);
  236. // $('#setup').unblock();
  237. // } );
  238. }
  239. LoadNextDataFile('', '', '');
  240. }
  241. catch(err)
  242. {
  243. alert('An exception occured: '+err);
  244. }
  245. return true; // Continue
  246. }
  247. function LoadNextDataFile(response, status, xhr)
  248. {
  249. if (status == 'error')
  250. {
  251. $('#setup').unblock();
  252. return; // Stop here
  253. }
  254. try
  255. {
  256. if (iCounter < aFilesToLoad.length)
  257. {
  258. if (iCounter == (aFilesToLoad.length - 1))
  259. {
  260. // Last file in the list (or only 1 file), this completes the session
  261. sSessionStatus = 'end';
  262. }
  263. else if (iCounter == 0)
  264. {
  265. // First file in the list, start the session
  266. sSessionStatus = 'start';
  267. }
  268. else
  269. {
  270. sSessionStatus = 'continue';
  271. }
  272. iPercent = 60+Math.round((40.0 * (1+iCounter)) / aFilesToLoad.length);
  273. sFileName = aFilesToLoad[iCounter];
  274. //alert('Loading file '+sFileName+' ('+iPercent+' %) - '+sSessionStatus);
  275. $("#progress").progression({ Current: iPercent, Maximum: 100, aBackgroundImg: GetAbsoluteUrlAppRoot()+'setup/orange-progress.gif', aTextColor: '#000000' });
  276. iCounter++;
  277. $('#log').load( GetAbsoluteUrlAppRoot()+'setup/ajax.dataloader.php',
  278. {
  279. 'selected_modules': GetSelectedModules(),
  280. 'db_server': $(':input[name=db_server]').val(),
  281. 'db_user': $(':input[name=db_user]').val(),
  282. 'db_pwd': $(':input[name=db_pwd]').val(),
  283. 'db_name': $(':input[name=db_name]').val(),
  284. 'new_db_name': $(':input[name=new_db_name]').val(),
  285. 'db_prefix': $(':input[name=db_prefix]').val(),
  286. 'modules_dir': $(':input[name=target_dir]').val(),
  287. 'operation': 'load_data',
  288. 'file': sFileName,
  289. 'percent': iPercent,
  290. 'session_status': sSessionStatus
  291. },
  292. LoadNextDataFile, 'html');
  293. }
  294. else
  295. {
  296. // We're done
  297. $("#progress").progression({ Current: 100, Maximum: 100, aBackgroundImg: GetAbsoluteUrlAppRoot()+'setup/orange-progress.gif', aTextColor: '#000000' });
  298. //$('#setup').unblock();
  299. $('#GoToNextStep').submit(); // Use the hidden form to navigate to the next step
  300. }
  301. }
  302. catch(err)
  303. {
  304. alert('An exception occurred: '+err);
  305. }
  306. }
  307. function GetSelectedModules()
  308. {
  309. var aModules = new Array();
  310. $(':input[name^=module]').each(function() { aModules.push($(this).val()); } );
  311. return aModules.join(',');
  312. }
  313. function WizardAsyncAction(sActionCode, oParams)
  314. {
  315. var sStepClass = $('#_class').val();
  316. var sStepState = $('#_state').val();
  317. var oMap = { operation: 'async_action', step_class: sStepClass, step_state: sStepState, code: sActionCode, params: oParams };
  318. $.post(GetAbsoluteUrlAppRoot()+'setup/ajax.dataloader.php', oMap, function(data) {
  319. $('#async_action').html(data);
  320. });
  321. }