settings.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * @description Lets you change settings.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. settings = {}
  6. settings.createConfig = function() {
  7. var msg = '',
  8. action;
  9. action = function(data) {
  10. var dbName = data.dbName || '',
  11. dbUser = data.dbUser || '',
  12. dbPassword = data.dbPassword || '',
  13. dbHost = data.dbHost || '',
  14. dbTablePrefix = data.dbTablePrefix || '',
  15. params;
  16. if (dbUser.length<1) {
  17. basicModal.error('dbUser');
  18. return false;
  19. }
  20. if (dbHost.length<1) dbHost = 'localhost';
  21. if (dbName.length<1) dbName = 'lychee';
  22. params = {
  23. dbName,
  24. dbUser,
  25. dbPassword,
  26. dbHost,
  27. dbTablePrefix
  28. }
  29. api.post('Database::createConfig', params, function(data) {
  30. if (data!==true) {
  31. // Connection failed
  32. if (data.indexOf('Warning: Connection failed!')!==-1) {
  33. basicModal.show({
  34. body: '<p>Unable to connect to host database because access was denied. Double-check your host, username and password and ensure that access from your current location is permitted.</p>',
  35. buttons: {
  36. action: {
  37. title: 'Retry',
  38. fn: settings.createConfig
  39. }
  40. }
  41. });
  42. return false;
  43. }
  44. // Creation failed
  45. if (data.indexOf('Warning: Creation failed!')!==-1) {
  46. basicModal.show({
  47. body: '<p>Unable to create the database. Double-check your host, username and password and ensure that the specified user has the rights to modify and add content to the database.</p>',
  48. buttons: {
  49. action: {
  50. title: 'Retry',
  51. fn: settings.createConfig
  52. }
  53. }
  54. });
  55. return false;
  56. }
  57. // Could not create file
  58. if (data.indexOf('Warning: Could not create file!')!==-1) {
  59. basicModal.show({
  60. body: "<p>Unable to save this configuration. Permission denied in <b>'data/'</b>. Please set the read, write and execute rights for others in <b>'data/'</b> and <b>'uploads/'</b>. Take a look at the readme for more information.</p>",
  61. buttons: {
  62. action: {
  63. title: 'Retry',
  64. fn: settings.createConfig
  65. }
  66. }
  67. });
  68. return false;
  69. }
  70. // Something went wrong
  71. basicModal.show({
  72. body: '<p>Something unexpected happened. Please try again and check your installation and server. Take a look at the readme for more information.</p>',
  73. buttons: {
  74. action: {
  75. title: 'Retry',
  76. fn: settings.createConfig
  77. }
  78. }
  79. });
  80. return false;
  81. } else {
  82. // Configuration successful
  83. window.location.reload();
  84. }
  85. });
  86. }
  87. msg = `
  88. <p>
  89. Enter your database connection details below:
  90. <input data-name='dbHost' class='text' type='text' placeholder='Database Host (optional)' value=''>
  91. <input data-name='dbUser' class='text' type='text' placeholder='Database Username' value=''>
  92. <input data-name='dbPassword' class='text' type='password' placeholder='Database Password' value=''>
  93. </p>
  94. <p>
  95. Lychee will create its own database. If required, you can enter the name of an existing database instead:
  96. <input data-name='dbName' class='text' type='text' placeholder='Database Name (optional)' value=''>
  97. <input data-name='dbTablePrefix' class='text' type='text' placeholder='Table prefix (optional)' value=''>
  98. </p>
  99. `
  100. basicModal.show({
  101. body: msg,
  102. buttons: {
  103. action: {
  104. title: 'Connect',
  105. fn: action
  106. }
  107. }
  108. });
  109. }
  110. settings.createLogin = function() {
  111. var action,
  112. msg = '';
  113. action = function(data) {
  114. var params,
  115. username = data.username,
  116. password = data.password;
  117. if (username.length<1) {
  118. basicModal.error('username');
  119. return false;
  120. }
  121. if (password.length<1) {
  122. basicModal.error('password');
  123. return false;
  124. }
  125. basicModal.close();
  126. params = {
  127. username,
  128. password
  129. }
  130. api.post('Settings::setLogin', params, function(data) {
  131. if (data!==true) {
  132. basicModal.show({
  133. body: '<p>Unable to save login. Please try again with another username and password!</p>',
  134. buttons: {
  135. action: {
  136. title: 'Retry',
  137. fn: settings.createLogin
  138. }
  139. }
  140. });
  141. }
  142. });
  143. }
  144. msg = `
  145. <p>
  146. Enter a username and password for your installation:
  147. <input data-name='username' class='text' type='text' placeholder='New Username' value=''>
  148. <input data-name='password' class='text' type='password' placeholder='New Password' value=''>
  149. </p>
  150. `
  151. basicModal.show({
  152. body: msg,
  153. buttons: {
  154. action: {
  155. title: 'Create Login',
  156. fn: action
  157. }
  158. }
  159. });
  160. }
  161. settings.setLogin = function() {
  162. var msg = '',
  163. action;
  164. action = function(data) {
  165. var oldPassword = data.oldPassword || '',
  166. username = data.username || '',
  167. password = data.password || '',
  168. params;
  169. if (oldPassword.length<1) {
  170. basicModal.error('oldPassword');
  171. return false;
  172. }
  173. if (username.length<1) {
  174. basicModal.error('username');
  175. return false;
  176. }
  177. if (password.length<1) {
  178. basicModal.error('password');
  179. return false;
  180. }
  181. basicModal.close();
  182. params = {
  183. oldPassword,
  184. username,
  185. password
  186. }
  187. api.post('Settings::setLogin', params, function(data) {
  188. if (data!==true) lychee.error(null, params, data);
  189. });
  190. }
  191. msg = `
  192. <p>
  193. Enter your current password:
  194. <input data-name='oldPassword' class='text' type='password' placeholder='Current Password' value=''>
  195. </p>
  196. <p>
  197. Your username and password will be changed to the following:
  198. <input data-name='username' class='text' type='text' placeholder='New Username' value=''>
  199. <input data-name='password' class='text' type='password' placeholder='New Password' value=''>
  200. </p>
  201. `
  202. basicModal.show({
  203. body: msg,
  204. buttons: {
  205. action: {
  206. title: 'Change Login',
  207. fn: action
  208. },
  209. cancel: {
  210. title: 'Cancel',
  211. fn: basicModal.close
  212. }
  213. }
  214. });
  215. }
  216. settings.setSorting = function() {
  217. var sortingPhotos = [],
  218. sortingAlbums = [],
  219. action,
  220. msg = '';
  221. action = function() {
  222. var params;
  223. sortingAlbums[0] = $('.basicModal select#settings_albums_type').val();
  224. sortingAlbums[1] = $('.basicModal select#settings_albums_order').val();
  225. sortingPhotos[0] = $('.basicModal select#settings_photos_type').val();
  226. sortingPhotos[1] = $('.basicModal select#settings_photos_order').val();
  227. basicModal.close();
  228. albums.refresh();
  229. params = {
  230. typeAlbums: sortingAlbums[0],
  231. orderAlbums: sortingAlbums[1],
  232. typePhotos: sortingPhotos[0],
  233. orderPhotos: sortingPhotos[1]
  234. }
  235. api.post('Settings::setSorting', params, function(data) {
  236. if (data===true) {
  237. lychee.sortingAlbums = 'ORDER BY ' + sortingAlbums[0] + ' ' + sortingAlbums[1];
  238. lychee.sortingPhotos = 'ORDER BY ' + sortingPhotos[0] + ' ' + sortingPhotos[1];
  239. lychee.load();
  240. } else lychee.error(null, params, data);
  241. });
  242. }
  243. msg = `
  244. <p>
  245. Sort albums by
  246. <span class="select">
  247. <select id='settings_albums_type'>
  248. <option value='id'>Creation Time</option>
  249. <option value='title'>Title</option>
  250. <option value='description'>Description</option>
  251. <option value='public'>Public</option>
  252. </select>
  253. </span>
  254. in an
  255. <span class="select">
  256. <select id='settings_albums_order'>
  257. <option value='ASC'>Ascending</option>
  258. <option value='DESC'>Descending</option>
  259. </select>
  260. </span>
  261. order.
  262. </p>
  263. <p>
  264. Sort photos by
  265. <span class="select">
  266. <select id='settings_photos_type'>
  267. <option value='id'>Upload Time</option>
  268. <option value='takestamp'>Take Date</option>
  269. <option value='title'>Title</option>
  270. <option value='description'>Description</option>
  271. <option value='public'>Public</option>
  272. <option value='star'>Star</option>
  273. <option value='type'>Photo Format</option>
  274. </select>
  275. </span>
  276. in an
  277. <span class="select">
  278. <select id='settings_photos_order'>
  279. <option value='ASC'>Ascending</option>
  280. <option value='DESC'>Descending</option>
  281. </select>
  282. </span>
  283. order.
  284. </p>
  285. `
  286. basicModal.show({
  287. body: msg,
  288. buttons: {
  289. action: {
  290. title: 'Change Sorting',
  291. fn: action
  292. },
  293. cancel: {
  294. title: 'Cancel',
  295. fn: basicModal.close
  296. }
  297. }
  298. });
  299. if (lychee.sortingAlbums!=='') {
  300. sortingAlbums = lychee.sortingAlbums.replace('ORDER BY ', '').split(' ');
  301. $('.basicModal select#settings_albums_type').val(sortingAlbums[0]);
  302. $('.basicModal select#settings_albums_order').val(sortingAlbums[1]);
  303. }
  304. if (lychee.sortingPhotos!=='') {
  305. sortingPhotos = lychee.sortingPhotos.replace('ORDER BY ', '').split(' ');
  306. $('.basicModal select#settings_photos_type').val(sortingPhotos[0]);
  307. $('.basicModal select#settings_photos_order').val(sortingPhotos[1]);
  308. }
  309. }
  310. settings.setDropboxKey = function(callback) {
  311. var action,
  312. msg = "";
  313. action = function(data) {
  314. var key = data.key;
  315. if (data.key.length<1) {
  316. basicModal.error('key');
  317. return false;
  318. }
  319. basicModal.close();
  320. api.post('Settings::setDropboxKey', { key }, function(data) {
  321. if (data===true) {
  322. lychee.dropboxKey = key;
  323. if (callback) lychee.loadDropbox(callback);
  324. } else lychee.error(null, params, data);
  325. });
  326. }
  327. msg = `
  328. <p>
  329. In order to import photos from your Dropbox, you need a valid drop-ins app key from <a href='https://www.dropbox.com/developers/apps/create'>their website</a>. Generate yourself a personal key and enter it below:
  330. <input class='text' data-name='key' type='text' placeholder='Dropbox API Key' value='${ lychee.dropboxKey }'>
  331. </p>
  332. `
  333. basicModal.show({
  334. body: msg,
  335. buttons: {
  336. action: {
  337. title: 'Set Dropbox Key',
  338. fn: action
  339. },
  340. cancel: {
  341. title: 'Cancel',
  342. fn: basicModal.close
  343. }
  344. }
  345. });
  346. }