setup.js 7.9 KB

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