setup.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. try
  152. {
  153. // Call the asynchronous page that performs the compilation of the data model and the creation of the configuration file
  154. $('#log').html('');
  155. $('#setup').block({message: '<p><span id="setup_msg">Preparing data model...</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': 'compile_data_model',
  160. 'selected_modules': GetSelectedModules(),
  161. 'mode': $(':input[name=mode]').val(),
  162. 'source_dir': $(':input[name=source_dir]').val(),
  163. 'target_dir': $(':input[name=target_dir]').val()
  164. },
  165. DoUpdateDBSchema, 'html');
  166. }
  167. catch(err)
  168. {
  169. alert('An exception occured: '+err);
  170. }
  171. return false; // Do NOT submit the form yet
  172. }
  173. function DoUpdateDBSchema()
  174. {
  175. try
  176. {
  177. // Call the asynchronous page that performs the creation/update of the DB Schema
  178. $('#log').html('');
  179. $('#setup').block({message: '<p><span id="setup_msg">Updating DB schema...</span><br/><div id=\"progress\">5%</div></p>'});
  180. $('#progress').progression( {Current:10, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  181. $('#log').load( 'ajax.dataloader.php',
  182. {
  183. 'operation': 'update_db_schema',
  184. 'selected_modules': GetSelectedModules(),
  185. 'mode': $(':input[name=mode]').val(),
  186. 'db_server': $(':input[name=db_server]').val(),
  187. 'db_user': $(':input[name=db_user]').val(),
  188. 'db_pwd': $(':input[name=db_pwd]').val(),
  189. 'db_name': $(':input[name=db_name]').val(),
  190. 'new_db_name': $(':input[name=new_db_name]').val(),
  191. 'db_prefix': $(':input[name=db_prefix]').val(),
  192. 'modules_dir': $(':input[name=target_dir]').val()
  193. },
  194. DoUpdateProfiles, 'html');
  195. }
  196. catch(err)
  197. {
  198. alert('An exception occured: '+err);
  199. }
  200. return false; // Do NOT submit the form yet
  201. }
  202. function DoUpdateProfiles(response, status, xhr)
  203. {
  204. if (status == 'error')
  205. {
  206. $('#setup').unblock();
  207. return; // An error occurred !
  208. }
  209. try
  210. {
  211. // Call the asynchronous page that performs the creation/update of the DB Schema
  212. $('#log').html('');
  213. $('#setup_msg').text('Updating Profiles...');
  214. $('#progress').progression( {Current:40, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  215. $('#log').load( 'ajax.dataloader.php',
  216. {
  217. 'operation': 'after_db_create',
  218. 'selected_modules': GetSelectedModules(),
  219. 'mode': $(':input[name=mode]').val(),
  220. 'db_server': $(':input[name=db_server]').val(),
  221. 'db_user': $(':input[name=db_user]').val(),
  222. 'db_pwd': $(':input[name=db_pwd]').val(),
  223. 'db_name': $(':input[name=db_name]').val(),
  224. 'new_db_name': $(':input[name=new_db_name]').val(),
  225. 'db_prefix': $(':input[name=db_prefix]').val(),
  226. 'modules_dir': $(':input[name=target_dir]').val(),
  227. 'auth_user': $(':input[name=auth_user]').val(),
  228. 'auth_pwd': $(':input[name=auth_pwd]').val(),
  229. 'language': $(':input[name=language]').val()
  230. },
  231. DoLoadDataAsynchronous, 'html');
  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. catch(err)
  242. {
  243. alert('An exception occured: '+err);
  244. }
  245. return true; // Continue loading the data
  246. }
  247. var aFilesToLoad = new Array();
  248. var iCounter = 0;
  249. function DoLoadDataAsynchronous(response, status, xhr)
  250. {
  251. if (status == 'error')
  252. {
  253. $('#setup').unblock();
  254. return; // An error occurred !
  255. }
  256. try
  257. {
  258. // The array aFilesToLoad is populated by this function dynamically written on the server
  259. PopulateDataFilesList();
  260. iCurrent = 60;
  261. if (aFilesToLoad.length == 0)
  262. {
  263. $('#progress').progression( {Current: 100} );
  264. }
  265. else
  266. {
  267. $('#log').html('');
  268. $('#setup_msg').text('Loading data...');
  269. $('#progress').progression( {Current: 60, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000'} );
  270. // $('#log').ajaxError(
  271. // function(e, xhr, settings, exception)
  272. // {
  273. // bStopAysncProcess = true;
  274. // alert('Fatal error detected: '+ xhr.responseText);
  275. // $('#log').append(xhr.responseText);
  276. // $('#setup').unblock();
  277. // } );
  278. }
  279. LoadNextDataFile('', '', '');
  280. }
  281. catch(err)
  282. {
  283. alert('An exception occured: '+err);
  284. }
  285. return true; // Continue
  286. }
  287. function LoadNextDataFile(response, status, xhr)
  288. {
  289. if (status == 'error')
  290. {
  291. $('#setup').unblock();
  292. return; // Stop here
  293. }
  294. try
  295. {
  296. if (iCounter < aFilesToLoad.length)
  297. {
  298. if (iCounter == (aFilesToLoad.length - 1))
  299. {
  300. // Last file in the list (or only 1 file), this completes the session
  301. sSessionStatus = 'end';
  302. }
  303. else if (iCounter == 0)
  304. {
  305. // First file in the list, start the session
  306. sSessionStatus = 'start';
  307. }
  308. else
  309. {
  310. sSessionStatus = 'continue';
  311. }
  312. iPercent = 60+Math.round((40.0 * (1+iCounter)) / aFilesToLoad.length);
  313. sFileName = aFilesToLoad[iCounter];
  314. //alert('Loading file '+sFileName+' ('+iPercent+' %) - '+sSessionStatus);
  315. $("#progress").progression({ Current: iPercent, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' });
  316. iCounter++;
  317. $('#log').load( 'ajax.dataloader.php',
  318. {
  319. 'selected_modules': GetSelectedModules(),
  320. 'db_server': $(':input[name=db_server]').val(),
  321. 'db_user': $(':input[name=db_user]').val(),
  322. 'db_pwd': $(':input[name=db_pwd]').val(),
  323. 'db_name': $(':input[name=db_name]').val(),
  324. 'new_db_name': $(':input[name=new_db_name]').val(),
  325. 'db_prefix': $(':input[name=db_prefix]').val(),
  326. 'modules_dir': $(':input[name=target_dir]').val(),
  327. 'operation': 'load_data',
  328. 'file': sFileName,
  329. 'percent': iPercent,
  330. 'session_status': sSessionStatus
  331. },
  332. LoadNextDataFile, 'html');
  333. }
  334. else
  335. {
  336. // We're done
  337. $("#progress").progression({ Current: 100, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' });
  338. //$('#setup').unblock();
  339. $('#GoToNextStep').submit(); // Use the hidden form to navigate to the next step
  340. }
  341. }
  342. catch(err)
  343. {
  344. alert('An exception occurred: '+err);
  345. }
  346. }
  347. function GetSelectedModules()
  348. {
  349. var aModules = new Array();
  350. $(':input[name^=module]').each(function() { aModules.push($(this).val()); } );
  351. return aModules.join(',');
  352. }