settings.js 10 KB

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