setup.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 DoUpdateDBSchema to start the asynchronous update
  129. bResult = DoUpdateDBSchema();
  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 DoUpdateDBSchema()
  150. {
  151. try
  152. {
  153. // Call the asynchronous page that performs the creation/update of the DB Schema
  154. $('#log').html('');
  155. $('#setup').block({message: '<p><span id="setup_msg">Updating DB schema...</span><br/><div id=\"progress\">0%</div></p>'});
  156. $('#progress').progression( {Current:5, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  157. $('#log').load( 'ajax.dataloader.php',
  158. {
  159. 'operation': 'update_db_schema',
  160. 'selected_modules': GetSelectedModules(),
  161. 'mode': $(':input[name=mode]').val()
  162. },
  163. DoUpdateProfiles, 'html');
  164. }
  165. catch(err)
  166. {
  167. alert('An exception occured: '+err);
  168. }
  169. return false; // Do NOT submit the form yet
  170. }
  171. function DoUpdateProfiles(response, status, xhr)
  172. {
  173. if (status == 'error')
  174. {
  175. $('#setup').unblock();
  176. return; // An error occurred !
  177. }
  178. try
  179. {
  180. // Call the asynchronous page that performs the creation/update of the DB Schema
  181. $('#log').html('');
  182. $('#setup_msg').text('Updating Profiles...');
  183. $('#progress').progression( {Current:40, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  184. $('#log').load( 'ajax.dataloader.php',
  185. {
  186. 'operation': 'after_db_create',
  187. 'selected_modules': GetSelectedModules(),
  188. 'mode': $(':input[name=mode]').val(),
  189. 'auth_user': $(':input[name=auth_user]').val(),
  190. 'auth_pwd': $(':input[name=auth_pwd]').val(),
  191. 'language': $(':input[name=language]').val()
  192. },
  193. DoLoadDataAsynchronous, 'html');
  194. // $('#log').ajaxError(
  195. // function(e, xhr, settings, exception)
  196. // {
  197. // bStopAysncProcess = true;
  198. // alert('Fatal error detected: '+ xhr.responseText);
  199. // $('#log').append(xhr.responseText);
  200. // $('#setup').unblock();
  201. // } );
  202. }
  203. catch(err)
  204. {
  205. alert('An exception occured: '+err);
  206. }
  207. return true; // Continue loading the data
  208. }
  209. var aFilesToLoad = new Array();
  210. var iCounter = 0;
  211. function DoLoadDataAsynchronous(response, status, xhr)
  212. {
  213. if (status == 'error')
  214. {
  215. $('#setup').unblock();
  216. return; // An error occurred !
  217. }
  218. try
  219. {
  220. // The array aFilesToLoad is populated by this function dynamically written on the server
  221. PopulateDataFilesList();
  222. iCurrent = 60;
  223. if (aFilesToLoad.length == 0)
  224. {
  225. $('#progress').progression( {Current: 100} );
  226. }
  227. else
  228. {
  229. $('#log').html('');
  230. $('#setup_msg').text('Loading data...');
  231. $('#progress').progression( {Current: 60, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  232. // $('#log').ajaxError(
  233. // function(e, xhr, settings, exception)
  234. // {
  235. // bStopAysncProcess = true;
  236. // alert('Fatal error detected: '+ xhr.responseText);
  237. // $('#log').append(xhr.responseText);
  238. // $('#setup').unblock();
  239. // } );
  240. }
  241. LoadNextDataFile('', '', '');
  242. }
  243. catch(err)
  244. {
  245. alert('An exception occured: '+err);
  246. }
  247. return true; // Continue
  248. }
  249. function LoadNextDataFile(response, status, xhr)
  250. {
  251. if (status == 'error')
  252. {
  253. $('#setup').unblock();
  254. return; // Stop here
  255. }
  256. try
  257. {
  258. if (iCounter < aFilesToLoad.length)
  259. {
  260. if (iCounter == (aFilesToLoad.length - 1))
  261. {
  262. // Last file in the list (or only 1 file), this completes the session
  263. sSessionStatus = 'end';
  264. }
  265. else if (iCounter == 0)
  266. {
  267. // First file in the list, start the session
  268. sSessionStatus = 'start';
  269. }
  270. else
  271. {
  272. sSessionStatus = 'continue';
  273. }
  274. iPercent = 60+Math.round((40.0 * (1+iCounter)) / aFilesToLoad.length);
  275. sFileName = aFilesToLoad[iCounter];
  276. //alert('Loading file '+sFileName+' ('+iPercent+' %) - '+sSessionStatus);
  277. $("#progress").progression({ Current: iPercent, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' });
  278. iCounter++;
  279. $('#log').load( 'ajax.dataloader.php', { 'operation': 'load_data', 'file': sFileName, 'percent': iPercent, 'session_status': sSessionStatus }, LoadNextDataFile, 'html');
  280. }
  281. else
  282. {
  283. // We're done
  284. $("#progress").progression({ Current: 100, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' });
  285. //$('#setup').unblock();
  286. $('#GoToNextStep').submit(); // Use the hidden form to navigate to the next step
  287. }
  288. }
  289. catch(err)
  290. {
  291. alert('An exception occurred: '+err);
  292. }
  293. }
  294. function GetSelectedModules()
  295. {
  296. var aModules = new Array();
  297. $(':input[name^=module]').each(function() { aModules.push($(this).val()); } );
  298. return aModules.join(',');
  299. }