setup.js 7.5 KB

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