job_form.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. {% extends "base.html" %}
  2. {% block title %}{{ 'Éditer' if job else 'Nouveau job' }}{% endblock %}
  3. {% block content %}
  4. <div class="max-w-2xl">
  5. <h1 class="text-xl font-bold text-gray-900 mb-6">
  6. {{ 'Éditer « ' + job.name + ' »' if job else 'Nouveau job de sauvegarde' }}
  7. </h1>
  8. <form method="post"
  9. action="{{ url_for('jobs.job_edit', job_id=job.id) if job else url_for('jobs.job_new') }}"
  10. class="space-y-6">
  11. {# ── Infos générales ── #}
  12. <div class="bg-white rounded-xl border border-gray-200 p-6 space-y-4">
  13. <h2 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">Général</h2>
  14. <div>
  15. <label class="block text-sm font-medium text-gray-700 mb-1">Nom du job</label>
  16. <input type="text" name="name" required
  17. value="{{ job.name if job else '' }}"
  18. placeholder="ex: Nextcloud quotidien"
  19. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  20. </div>
  21. <div>
  22. <label class="block text-sm font-medium text-gray-700 mb-1">Type</label>
  23. <select name="type" id="job-type"
  24. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  25. {% for val, label in [('ynh_app','Application YunoHost'), ('ynh_system','Système YunoHost'),
  26. ('mysql','MySQL'), ('postgresql','PostgreSQL'),
  27. ('custom_dir','Répertoire custom')] %}
  28. <option value="{{ val }}"
  29. {% if job and job.type == val %}selected{% endif %}
  30. >
  31. {{ label }}
  32. </option>
  33. {% endfor %}
  34. </select>
  35. </div>
  36. {# Type-specific config : ynh_app #}
  37. <div id="cfg-ynh_app" class="type-cfg space-y-3">
  38. <div>
  39. <label class="block text-sm font-medium text-gray-700 mb-1">Application YunoHost</label>
  40. <select name="app_id" id="app_id"
  41. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  42. {% set current_app_id = (job.config_json | fromjson).get('app_id', '') if job and job.config_json else '' %}
  43. {% for ynh_app in ynh_apps %}
  44. <option value="{{ ynh_app.id }}"
  45. {% if ynh_app.id == current_app_id %}selected{% endif %}>
  46. {% set _display = ynh_app.label or ynh_app.name %}
  47. {{ ynh_app.id }}{% if _display %} — {{ _display }}{% endif %}
  48. </option>
  49. {% endfor %}
  50. {% if not ynh_apps %}
  51. <option value="">Aucune application trouvée</option>
  52. {% endif %}
  53. </select>
  54. </div>
  55. <div class="flex items-center gap-2">
  56. {% set core_only = (job.config_json | fromjson).get('core_only', False) if job and job.config_json else False %}
  57. <input type="checkbox" name="core_only" value="1" id="core_only"
  58. {% if core_only %}checked{% endif %}
  59. class="rounded border-gray-300 text-blue-600">
  60. <label for="core_only" class="text-sm text-gray-700">
  61. Core only (BACKUP_CORE_ONLY=1) — exclut les données utilisateur
  62. </label>
  63. </div>
  64. </div>
  65. {# Type-specific config : ynh_system — sélection des hooks #}
  66. {% set _sys_hooks = [
  67. ('conf_ynh_settings', 'Paramètres YunoHost', false),
  68. ('conf_ynh_firewall', 'Firewall', false),
  69. ('conf_ssowat', 'Portail SSO', false),
  70. ('conf_nginx', 'Nginx', false),
  71. ('conf_ynh_certs', 'Certificats SSL', false),
  72. ('conf_ynh_domain', 'Domaines', false),
  73. ('conf_ynh_user', 'Utilisateurs LDAP', false),
  74. ('data_home', 'Répertoires home /home/*', true),
  75. ('data_mail', 'Courriers Dovecot', true),
  76. ] %}
  77. {% set _saved_hooks = (job.config_json | fromjson).get('hooks', []) if job and job.config_json and job.type == 'ynh_system' else [] %}
  78. <div id="cfg-ynh_system" class="type-cfg hidden space-y-2">
  79. <p class="text-xs text-gray-500 mb-2">
  80. Tous cochés = sauvegarde complète. Décochez les hooks volumineux pour alléger l'archive.
  81. </p>
  82. {% for hook_id, hook_label, heavy in _sys_hooks %}
  83. <div class="flex items-center gap-2">
  84. <input type="checkbox" name="system_hooks" value="{{ hook_id }}" id="hook_{{ hook_id }}"
  85. {% if not _saved_hooks or hook_id in _saved_hooks %}checked{% endif %}
  86. class="rounded border-gray-300 text-blue-600">
  87. <label for="hook_{{ hook_id }}" class="text-sm text-gray-700">
  88. {{ hook_label }}{% if heavy %} <span class="text-xs text-amber-600 font-medium">(volumineux)</span>{% endif %}
  89. </label>
  90. </div>
  91. {% endfor %}
  92. </div>
  93. {# Type-specific config : mysql #}
  94. {% set db_cfg = (job.config_json | fromjson) if job and job.config_json else {} %}
  95. <div id="cfg-mysql" class="type-cfg hidden space-y-3">
  96. <div>
  97. <label class="block text-sm font-medium text-gray-700 mb-1">Base de données</label>
  98. <select id="db-select-mysql" name="db_database"
  99. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
  100. {% set cur_db = db_cfg.get('database', '') if job and job.type == 'mysql' else '' %}
  101. {% if cur_db %}
  102. <option value="{{ cur_db }}" selected>{{ cur_db }}</option>
  103. {% else %}
  104. <option value="">Chargement…</option>
  105. {% endif %}
  106. </select>
  107. <p class="text-xs text-gray-400 mt-1">
  108. Dump via <code class="bg-gray-100 px-1 rounded">sudo mysqldump</code> — aucun mot de passe requis.
  109. </p>
  110. </div>
  111. </div>
  112. {# Type-specific config : postgresql #}
  113. <div id="cfg-postgresql" class="type-cfg hidden space-y-3">
  114. <div>
  115. <label class="block text-sm font-medium text-gray-700 mb-1">Base de données</label>
  116. <select id="db-select-postgresql" name="db_database"
  117. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
  118. {% set cur_db = db_cfg.get('database', '') if job and job.type == 'postgresql' else '' %}
  119. {% if cur_db %}
  120. <option value="{{ cur_db }}" selected>{{ cur_db }}</option>
  121. {% else %}
  122. <option value="">Chargement…</option>
  123. {% endif %}
  124. </select>
  125. <p class="text-xs text-gray-400 mt-1">
  126. Dump via <code class="bg-gray-100 px-1 rounded">sudo -u postgres pg_dump</code> — aucun mot de passe requis.
  127. </p>
  128. </div>
  129. </div>
  130. {# Type-specific config : custom_dir #}
  131. {% set cd_cfg = (job.config_json | fromjson) if job and job.config_json and job.type == 'custom_dir' else {} %}
  132. {% set cd_restore = cd_cfg.get('restore', {}) %}
  133. <div id="cfg-custom_dir" class="type-cfg hidden space-y-4">
  134. <div>
  135. <label class="block text-sm font-medium text-gray-700 mb-1">Chemin à sauvegarder</label>
  136. <input type="text" name="source_path"
  137. value="{{ cd_cfg.get('source_path', '') }}"
  138. placeholder="/opt/monapp"
  139. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
  140. </div>
  141. <div>
  142. <label class="block text-sm font-medium text-gray-700 mb-1">Exclusions <span class="font-normal text-gray-400">(une par ligne)</span></label>
  143. <textarea name="excludes" rows="3" placeholder="cache/&#10;logs/&#10;*.tmp"
  144. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">{{ cd_cfg.get('excludes', []) | join('\n') }}</textarea>
  145. </div>
  146. {# Section restauration (optionnel, collapsible) #}
  147. <details {% if cd_restore %}open{% endif %} class="border border-gray-200 rounded-lg">
  148. <summary class="px-4 py-3 text-sm font-medium text-gray-700 cursor-pointer hover:bg-gray-50 select-none">
  149. Configuration de restauration <span class="text-gray-400 font-normal">(optionnel)</span>
  150. </summary>
  151. <div class="px-4 pb-4 pt-2 space-y-4 border-t border-gray-100">
  152. <div>
  153. <p class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-2">Utilisateur système</p>
  154. <div class="grid grid-cols-3 gap-3">
  155. <div>
  156. <label class="block text-xs text-gray-600 mb-1">Nom</label>
  157. <input type="text" name="restore_user_name"
  158. value="{{ cd_restore.get('system_user', {}).get('name', '') }}"
  159. placeholder="monapp" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  160. </div>
  161. <div>
  162. <label class="block text-xs text-gray-600 mb-1">Home</label>
  163. <input type="text" name="restore_user_home"
  164. value="{{ cd_restore.get('system_user', {}).get('home', '') }}"
  165. placeholder="/opt/monapp" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  166. </div>
  167. <div>
  168. <label class="block text-xs text-gray-600 mb-1">Shell</label>
  169. <input type="text" name="restore_user_shell"
  170. value="{{ cd_restore.get('system_user', {}).get('shell', '/bin/false') }}"
  171. placeholder="/bin/false" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  172. </div>
  173. </div>
  174. </div>
  175. <div>
  176. <p class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-2">Service systemd</p>
  177. <div class="grid grid-cols-2 gap-3">
  178. <div>
  179. <label class="block text-xs text-gray-600 mb-1">Nom du service</label>
  180. <input type="text" name="restore_service_name"
  181. value="{{ cd_restore.get('systemd_service', {}).get('name', '') }}"
  182. placeholder="monapp" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  183. </div>
  184. <div>
  185. <label class="block text-xs text-gray-600 mb-1">Fichier .service</label>
  186. <input type="text" name="restore_service_file"
  187. value="{{ cd_restore.get('systemd_service', {}).get('service_file', '') }}"
  188. placeholder="/opt/monapp/monapp.service" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  189. </div>
  190. </div>
  191. </div>
  192. <div>
  193. <p class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-2">Permissions</p>
  194. <div class="grid grid-cols-2 gap-3">
  195. <div>
  196. <label class="block text-xs text-gray-600 mb-1">Propriétaire (user:group)</label>
  197. <input type="text" name="restore_perm_owner"
  198. value="{{ cd_restore.get('permissions', {}).get('owner', '') }}"
  199. placeholder="monapp:monapp" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  200. </div>
  201. <div>
  202. <label class="block text-xs text-gray-600 mb-1">Mode (chmod)</label>
  203. <input type="text" name="restore_perm_mode"
  204. value="{{ cd_restore.get('permissions', {}).get('mode', '') }}"
  205. placeholder="750" class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">
  206. </div>
  207. </div>
  208. </div>
  209. <div>
  210. <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wide mb-2">
  211. Commandes post-restauration <span class="font-normal text-gray-400">(une par ligne)</span>
  212. </label>
  213. <textarea name="restore_post_cmds" rows="3"
  214. placeholder="systemctl restart monapp"
  215. class="w-full border border-gray-300 rounded px-2 py-1.5 text-xs font-mono focus:outline-none focus:ring-1 focus:ring-blue-500">{{ cd_restore.get('post_restore_commands', []) | join('\n') }}</textarea>
  216. </div>
  217. </div>
  218. </details>
  219. </div>
  220. </div>
  221. {# ── Planification ── #}
  222. {% set has_cron = job and job.cron_expr %}
  223. <div class="bg-white rounded-xl border border-gray-200 p-6 space-y-4">
  224. <h2 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">Planification</h2>
  225. <div class="flex items-center gap-3">
  226. <input type="checkbox" id="scheduled" name="scheduled" value="1"
  227. {% if not job or has_cron %}checked{% endif %}
  228. class="rounded border-gray-300 text-blue-600">
  229. <label for="scheduled" class="text-sm font-medium text-gray-700">Planification automatique</label>
  230. </div>
  231. <div id="cron-field" {% if job and not has_cron %}class="hidden"{% endif %}>
  232. <label class="block text-sm font-medium text-gray-700 mb-1">Expression cron</label>
  233. <input type="text" name="cron_expr" id="cron_expr"
  234. value="{{ job.cron_expr if has_cron else '0 3 * * *' }}"
  235. placeholder="0 3 * * *"
  236. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-blue-500">
  237. <p class="text-xs text-gray-400 mt-1">
  238. Format : minute heure jour mois jour_semaine
  239. — ex: <code class="bg-gray-100 px-1 rounded">0 3 * * *</code> = tous les jours à 3h
  240. · <code class="bg-gray-100 px-1 rounded">0 3 * * 1</code> = chaque lundi à 3h
  241. </p>
  242. </div>
  243. </div>
  244. {# ── Rétention ── #}
  245. {% set gfs_cfg = (job.retention_gfs_config | fromjson) if job and job.retention_gfs_config else {} %}
  246. <div class="bg-white rounded-xl border border-gray-200 p-6 space-y-4">
  247. <h2 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">Rétention</h2>
  248. <div>
  249. <label class="block text-sm font-medium text-gray-700 mb-1">Mode</label>
  250. <select name="retention_mode" id="retention_mode"
  251. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  252. {% for val, label in [('count','Garder les N dernières archives'),
  253. ('daily','1 archive par jour sur N jours glissants'),
  254. ('gfs','Grand-Père-Père-Fils (GFS)')] %}
  255. <option value="{{ val }}"
  256. {% if job and job.retention_mode == val %}selected{% endif %}>
  257. {{ label }}
  258. </option>
  259. {% endfor %}
  260. </select>
  261. </div>
  262. {# Mode count / daily : un seul champ N #}
  263. <div id="retention-simple">
  264. <label class="block text-sm font-medium text-gray-700 mb-1">Valeur (N)</label>
  265. <input type="number" name="retention_value" id="retention_value" min="1" max="365"
  266. value="{{ job.retention_value if job and job.retention_mode != 'gfs' else 7 }}"
  267. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  268. </div>
  269. {# Mode GFS : trois champs Fils / Père / Grand-Père #}
  270. <div id="retention-gfs" class="hidden space-y-3">
  271. <div class="grid grid-cols-3 gap-4">
  272. <div>
  273. <label class="block text-sm font-medium text-gray-700 mb-1">Fils (journaliers)</label>
  274. <input type="number" name="gfs_daily" min="1" max="31"
  275. value="{{ gfs_cfg.get('daily', 7) }}"
  276. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  277. <p class="text-xs text-gray-400 mt-1">dernières archives</p>
  278. </div>
  279. <div>
  280. <label class="block text-sm font-medium text-gray-700 mb-1">Père (hebdos)</label>
  281. <input type="number" name="gfs_weekly" min="1" max="52"
  282. value="{{ gfs_cfg.get('weekly', 4) }}"
  283. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  284. <p class="text-xs text-gray-400 mt-1">semaines glissantes</p>
  285. </div>
  286. <div>
  287. <label class="block text-sm font-medium text-gray-700 mb-1">Grand-Père (mensuels)</label>
  288. <input type="number" name="gfs_monthly" min="1" max="60"
  289. value="{{ gfs_cfg.get('monthly', 12) }}"
  290. class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
  291. <p class="text-xs text-gray-400 mt-1">mois glissants</p>
  292. </div>
  293. </div>
  294. </div>
  295. <p id="retention-help" class="text-xs text-gray-400"></p>
  296. </div>
  297. {# ── Destination ── #}
  298. <div class="bg-white rounded-xl border border-gray-200 p-6 space-y-3">
  299. <h2 class="text-sm font-semibold text-gray-700 uppercase tracking-wide">Transfert après sauvegarde</h2>
  300. {% if destinations or remote_instances %}
  301. {% set sel_dest_ids = job.job_destinations | selectattr('dest_type','eq','ssh') | map(attribute='dest_id') | list if job else [] %}
  302. {% set sel_inst_ids = job.job_destinations | selectattr('dest_type','eq','instance') | map(attribute='dest_id') | list if job else [] %}
  303. {% if destinations %}
  304. <p class="text-xs font-semibold text-gray-500 uppercase tracking-wide">SSH / rsync</p>
  305. {% for d in destinations %}
  306. <div class="flex items-center gap-2">
  307. <input type="checkbox" name="transfer_targets" value="dest:{{ d.id }}"
  308. id="tr_dest_{{ d.id }}"
  309. {% if d.id in sel_dest_ids %}checked{% endif %}
  310. class="rounded border-gray-300 text-blue-600">
  311. <label for="tr_dest_{{ d.id }}" class="text-sm text-gray-700">
  312. {{ d.name }} <span class="font-mono text-xs text-gray-400">— {{ d.remote_str }}</span>
  313. </label>
  314. </div>
  315. {% endfor %}
  316. {% endif %}
  317. {% if remote_instances %}
  318. <p class="text-xs font-semibold text-gray-500 uppercase tracking-wide {% if destinations %}mt-2{% endif %}">Instances fédérées</p>
  319. {% for inst in remote_instances %}
  320. <div class="flex items-center gap-2">
  321. <input type="checkbox" name="transfer_targets" value="inst:{{ inst.id }}"
  322. id="tr_inst_{{ inst.id }}"
  323. {% if inst.id in sel_inst_ids %}checked{% endif %}
  324. class="rounded border-gray-300 text-blue-600">
  325. <label for="tr_inst_{{ inst.id }}" class="text-sm text-gray-700">
  326. {{ inst.name }} <span class="font-mono text-xs text-gray-400">— {{ inst.url }}</span>
  327. </label>
  328. </div>
  329. {% endfor %}
  330. {% endif %}
  331. {% else %}
  332. <p class="text-xs text-gray-400">
  333. Aucune destination configurée.
  334. <a href="{{ url_for('dest.destination_new') }}" class="text-blue-600 hover:underline">Créer une destination SSH →</a>
  335. &nbsp;|&nbsp;
  336. <a href="{{ url_for('cfg.settings') }}?tab=instances" class="text-blue-600 hover:underline">Ajouter une instance fédérée →</a>
  337. </p>
  338. {% endif %}
  339. </div>
  340. {# ── Options ── #}
  341. <div class="bg-white rounded-xl border border-gray-200 p-6">
  342. <div class="flex items-center gap-3">
  343. <input type="checkbox" name="enabled" value="1" id="enabled"
  344. {% if not job or job.enabled %}checked{% endif %}
  345. class="rounded border-gray-300 text-blue-600">
  346. <label for="enabled" class="text-sm font-medium text-gray-700">Job activé</label>
  347. </div>
  348. </div>
  349. {# ── Actions ── #}
  350. <div class="flex gap-3">
  351. <button type="submit" class="btn-primary btn-md">
  352. {{ 'Enregistrer' if job else 'Créer le job' }}
  353. </button>
  354. <a href="{{ url_for('jobs.index') }}" class="btn-secondary btn-md">Annuler</a>
  355. </div>
  356. </form>
  357. </div>
  358. <script>
  359. const retentionHelp = {
  360. count: "Ex : N=7 → conserve les 7 dernières archives, supprime les plus anciennes.",
  361. daily: "Ex : N=30 → conserve 1 archive par jour sur les 30 derniers jours. Les doublons du même jour sont supprimés.",
  362. gfs: "Ex : Fils=7, Père=4, GP=12 → 7 archives récentes + 1 par semaine sur 4 semaines + 1 par mois sur 12 mois. Une archive peut couvrir plusieurs niveaux."
  363. };
  364. const dbCache = {};
  365. function loadDatabases(dbType) {
  366. const sel = document.getElementById('db-select-' + dbType);
  367. if (!sel) return;
  368. if (dbCache[dbType]) {
  369. populateSelect(sel, dbCache[dbType]);
  370. return;
  371. }
  372. fetch("{{ url_for('cfg.internal_databases', db_type='__TYPE__') }}".replace('__TYPE__', dbType))
  373. .then(r => r.json())
  374. .then(dbs => {
  375. dbCache[dbType] = dbs;
  376. populateSelect(sel, dbs);
  377. })
  378. .catch(() => {
  379. sel.innerHTML = '<option value="">Impossible de charger les bases</option>';
  380. });
  381. }
  382. function populateSelect(sel, dbs) {
  383. const current = sel.querySelector('option[selected]')?.value || '';
  384. sel.innerHTML = '';
  385. if (!dbs.length) {
  386. sel.innerHTML = '<option value="">Aucune base trouvée</option>';
  387. return;
  388. }
  389. dbs.forEach(db => {
  390. const opt = document.createElement('option');
  391. opt.value = db;
  392. opt.textContent = db;
  393. if (db === current) opt.selected = true;
  394. sel.appendChild(opt);
  395. });
  396. }
  397. function showTypeConfig() {
  398. document.querySelectorAll('.type-cfg').forEach(el => {
  399. el.classList.add('hidden');
  400. el.querySelectorAll('input, select, textarea').forEach(f => f.disabled = true);
  401. });
  402. const type = document.getElementById('job-type').value;
  403. const el = document.getElementById('cfg-' + type);
  404. if (el) {
  405. el.classList.remove('hidden');
  406. el.querySelectorAll('input, select, textarea').forEach(f => f.disabled = false);
  407. }
  408. if (type === 'mysql' || type === 'postgresql') loadDatabases(type);
  409. }
  410. function updateRetentionMode() {
  411. const mode = document.getElementById('retention_mode').value;
  412. const simple = document.getElementById('retention-simple');
  413. const gfsBlock = document.getElementById('retention-gfs');
  414. const simpleInput = document.getElementById('retention_value');
  415. const gfsInputs = gfsBlock.querySelectorAll('input');
  416. if (mode === 'gfs') {
  417. simple.classList.add('hidden');
  418. simpleInput.disabled = true;
  419. simpleInput.required = false;
  420. gfsBlock.classList.remove('hidden');
  421. gfsInputs.forEach(i => { i.disabled = false; i.required = true; });
  422. } else {
  423. simple.classList.remove('hidden');
  424. simpleInput.disabled = false;
  425. simpleInput.required = true;
  426. gfsBlock.classList.add('hidden');
  427. gfsInputs.forEach(i => { i.disabled = true; i.required = false; });
  428. }
  429. document.getElementById('retention-help').textContent = retentionHelp[mode] || '';
  430. }
  431. // Auto-remplissage du nom si le champ est vide
  432. const nameInput = document.querySelector('[name=name]');
  433. function suggestName() {
  434. if (nameInput.value.trim()) return; // ne pas écraser si déjà renseigné
  435. const type = document.getElementById('job-type').value;
  436. let suggestion = '';
  437. if (type === 'ynh_app') {
  438. const sel = document.getElementById('app_id');
  439. suggestion = sel ? sel.value : '';
  440. } else if (type === 'ynh_system') {
  441. suggestion = 'Système YunoHost';
  442. } else if (type === 'mysql') {
  443. const sel = document.getElementById('db-select-mysql');
  444. suggestion = sel ? sel.value : '';
  445. } else if (type === 'postgresql') {
  446. const sel = document.getElementById('db-select-postgresql');
  447. suggestion = sel ? sel.value : '';
  448. } else if (type === 'custom_dir') {
  449. const src = document.querySelector('[name=source_path]');
  450. if (src && src.value.trim()) {
  451. const parts = src.value.trim().replace(/\/+$/, '').split('/');
  452. suggestion = parts[parts.length - 1] || '';
  453. }
  454. }
  455. if (suggestion) nameInput.value = suggestion;
  456. }
  457. document.getElementById('job-type').addEventListener('change', function() { showTypeConfig(); suggestName(); });
  458. const appSel = document.getElementById('app_id');
  459. if (appSel) appSel.addEventListener('change', function() {
  460. {% if not job %}
  461. const opt = appSel.options[appSel.selectedIndex];
  462. if (opt) {
  463. const sep = opt.text.indexOf(' — ');
  464. nameInput.value = sep !== -1 ? opt.text.slice(sep + 3) : opt.text;
  465. }
  466. {% else %}
  467. suggestName();
  468. {% endif %}
  469. });
  470. ['db-select-mysql', 'db-select-postgresql'].forEach(id => {
  471. const el = document.getElementById(id);
  472. if (el) el.addEventListener('change', suggestName);
  473. });
  474. const srcPath = document.querySelector('[name=source_path]');
  475. if (srcPath) srcPath.addEventListener('input', suggestName);
  476. document.getElementById('retention_mode').addEventListener('change', updateRetentionMode);
  477. showTypeConfig();
  478. suggestName(); // pré-remplit le nom si champ vide à l'ouverture du formulaire
  479. updateRetentionMode();
  480. // Toggle planification cron
  481. const scheduledChk = document.getElementById('scheduled');
  482. const cronField = document.getElementById('cron-field');
  483. const cronInput = document.getElementById('cron_expr');
  484. function toggleCron() {
  485. if (scheduledChk.checked) {
  486. cronField.classList.remove('hidden');
  487. cronInput.disabled = false;
  488. cronInput.required = true;
  489. } else {
  490. cronField.classList.add('hidden');
  491. cronInput.disabled = true;
  492. cronInput.required = false;
  493. }
  494. }
  495. scheduledChk.addEventListener('change', toggleCron);
  496. toggleCron();
  497. </script>
  498. {% endblock %}