setup.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $('#theForm').submit(); // Submit the form
  11. return true;
  12. }
  13. function DoSubmit(sMsg, iStep)
  14. {
  15. var bResult = true;
  16. switch(iStep)
  17. {
  18. case 0: // Select either install or upgrade or nothing to select...
  19. if ( ($("input:radio").length > 0) && ($("input:radio:checked").length < 1))
  20. {
  21. alert('Please select either install or upgrade');
  22. bResult = false;
  23. }
  24. break;
  25. case 1: // Licence agreement
  26. if ($('#licence_ok:checked').length < 1)
  27. {
  28. alert('Please accept the licence agreement before continuing.');
  29. bResult = false;
  30. }
  31. break;
  32. case 2: // Database server selection
  33. if ($('#db_server').val() == '')
  34. {
  35. alert('Please specify a database server. Use "localhost" for a local DB server.');
  36. bResult = false;
  37. }
  38. else if ($('#db_user').val() == '')
  39. {
  40. alert('Please specify a user name to connect to the database.');
  41. bResult = false;
  42. }
  43. break;
  44. case 3: // Database instance selection
  45. if ($("input[@type=radio]:checked").length < 1)
  46. {
  47. alert('Please specify a database name');
  48. bResult = false;
  49. }
  50. else if( ($("#new_db:checked").length == 1))
  51. {
  52. if ($('#new_db_name').val() == '')
  53. {
  54. alert('Please specify the name of the database to create');
  55. bResult = false;
  56. }
  57. else if (!NameIsValid($('#new_db_name').val()))
  58. {
  59. alert($('#new_db_name').val()+' is not a valid database name. Please limit yourself to letters, numbers and the underscore character.');
  60. bResult = false;
  61. }
  62. }
  63. else if ($("#current_db:checked").length == 1)
  64. {
  65. // Special case (DB enumeration failed, user must enter DB name)
  66. if ($("#current_db_name").val() == '')
  67. {
  68. alert('Please specify the name of the database.');
  69. bResult = false;
  70. }
  71. else
  72. {
  73. // Copy the typed value as the value of the radio
  74. $("#current_db").val($("#current_db_name").val());
  75. }
  76. }
  77. if( ($('#db_prefix').val() != '') && (!NameIsValid($('#db_prefix').val())) )
  78. {
  79. alert($('#db_prefix').val()+' is not a valid table name. Please limit yourself to letters, numbers and the underscore character.');
  80. bResult = false;
  81. }
  82. break;
  83. case 4: // Choice of iTop modules
  84. break;
  85. case 5: // Administrator account
  86. if ($('#auth_user').val() == '')
  87. {
  88. alert('Please specify a login name for the administrator account');
  89. bResult = false;
  90. }
  91. else if ($('#auth_pwd').val() != $('#auth_pwd2').val())
  92. {
  93. alert('Retyped password does not match! Please verify the password.');
  94. bResult = false;
  95. }
  96. break;
  97. case 6: // Sample data selection
  98. break;
  99. case 7: // Display Summary: launch DoUpdateDBSchema to start the asynchronous update
  100. bResult = DoUpdateDBSchema();
  101. break;
  102. // Email test page
  103. case 10:
  104. if ($('#to').val() == '')
  105. {
  106. alert('Please specify a destination address');
  107. bResult = false;
  108. }
  109. }
  110. if (bResult && (sMsg != ''))
  111. {
  112. $('#setup').block({message: '<img src="../images/indicator.gif">&nbsp;'+sMsg});
  113. }
  114. return bResult;
  115. }
  116. function DoUpdateDBSchema()
  117. {
  118. try
  119. {
  120. // Call the asynchronous page that performs the creation/update of the DB Schema
  121. $('#log').html('');
  122. $('#setup').block({message: '<p>Updating DB schema...<br/><div id=\"progress\">0%</div></p>'});
  123. $('#progress').progression( {Current:5, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  124. $('#log').load( 'ajax.dataloader.php',
  125. {
  126. 'operation': 'update_db_schema',
  127. 'selected_modules': GetSelectedModules(),
  128. 'mode': $(':input[name=mode]').val()
  129. },
  130. DoUpdateProfiles, 'html');
  131. }
  132. catch(err)
  133. {
  134. alert('An exception occured: '+err);
  135. }
  136. return false; // Do NOT submit the form yet
  137. }
  138. function DoUpdateProfiles(response, status, xhr)
  139. {
  140. if (status == 'error')
  141. {
  142. $('#setup').unblock();
  143. return; // An error occurred !
  144. }
  145. try
  146. {
  147. // Call the asynchronous page that performs the creation/update of the DB Schema
  148. $('#log').html('');
  149. $('#setup').block({message: '<p>Updating Profiles...<br/><div id=\"progress\">0%</div></p>'});
  150. $('#progress').progression( {Current:40, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  151. $('#log').load( 'ajax.dataloader.php',
  152. {
  153. 'operation': 'after_db_create',
  154. 'selected_modules': GetSelectedModules(),
  155. 'mode': $(':input[name=mode]').val(),
  156. 'auth_user': $(':input[name=auth_user]').val(),
  157. 'auth_pwd': $(':input[name=auth_pwd]').val(),
  158. 'language': $(':input[name=language]').val()
  159. },
  160. DoLoadDataAsynchronous, 'html');
  161. // $('#log').ajaxError(
  162. // function(e, xhr, settings, exception)
  163. // {
  164. // bStopAysncProcess = true;
  165. // alert('Fatal error detected: '+ xhr.responseText);
  166. // $('#log').append(xhr.responseText);
  167. // $('#setup').unblock();
  168. // } );
  169. }
  170. catch(err)
  171. {
  172. alert('An exception occured: '+err);
  173. }
  174. return true; // Continue loading the data
  175. }
  176. var aFilesToLoad = new Array();
  177. var iCounter = 0;
  178. function DoLoadDataAsynchronous(response, status, xhr)
  179. {
  180. if (status == 'error')
  181. {
  182. $('#setup').unblock();
  183. return; // An error occurred !
  184. }
  185. try
  186. {
  187. // The array aFilesToLoad is populated by this function dynamically written on the server
  188. PopulateDataFilesList();
  189. iCurrent = 60;
  190. if (aFilesToLoad.length == 0)
  191. {
  192. $('#progress').progression( {Current: 100} );
  193. }
  194. else
  195. {
  196. $('#log').html('');
  197. $('#setup').block({message: '<p>Loading data...<br/><div id=\"progress\">0%</div></p>'});
  198. $('#progress').progression( {Current: 60, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  199. // $('#log').ajaxError(
  200. // function(e, xhr, settings, exception)
  201. // {
  202. // bStopAysncProcess = true;
  203. // alert('Fatal error detected: '+ xhr.responseText);
  204. // $('#log').append(xhr.responseText);
  205. // $('#setup').unblock();
  206. // } );
  207. }
  208. LoadNextDataFile('', '', '');
  209. }
  210. catch(err)
  211. {
  212. alert('An exception occured: '+err);
  213. }
  214. return true; // Continue
  215. }
  216. function LoadNextDataFile(response, status, xhr)
  217. {
  218. if (status == 'error')
  219. {
  220. $('#setup').unblock();
  221. return; // Stop here
  222. }
  223. try
  224. {
  225. if (iCounter < aFilesToLoad.length)
  226. {
  227. if (iCounter == (aFilesToLoad.length - 1))
  228. {
  229. // Last file in the list (or only 1 file), this completes the session
  230. sSessionStatus = 'end';
  231. }
  232. else if (iCounter == 0)
  233. {
  234. // First file in the list, start the session
  235. sSessionStatus = 'start';
  236. }
  237. else
  238. {
  239. sSessionStatus = 'continue';
  240. }
  241. iPercent = 60+Math.round((40.0 * (1+iCounter)) / aFilesToLoad.length);
  242. sFileName = aFilesToLoad[iCounter];
  243. //alert('Loading file '+sFileName+' ('+iPercent+' %) - '+sSessionStatus);
  244. $("#progress").progression({ Current: iPercent, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' });
  245. iCounter++;
  246. $('#log').load( 'ajax.dataloader.php', { 'operation': 'load_data', 'file': sFileName, 'percent': iPercent, 'session_status': sSessionStatus }, LoadNextDataFile, 'html');
  247. }
  248. else
  249. {
  250. // We're done
  251. $("#progress").progression({ Current: 100, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' });
  252. $('#setup').unblock();
  253. $('#GoToNextStep').submit(); // Use the hidden form to navigate to the next step
  254. }
  255. }
  256. catch(err)
  257. {
  258. alert('An exception occurred: '+err);
  259. }
  260. }
  261. function GetSelectedModules()
  262. {
  263. var aModules = new Array();
  264. $(':input[name^=module]').each(function() { aModules.push($(this).val()); } );
  265. return aModules.join(',');
  266. }