job_form.html 25 KB

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