luci-mod-admin-full: allow writing empty crontab config
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / crontab.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2013 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local fs = require "nixio.fs"
6 local cronfile = "/etc/crontabs/root" 
7
8 f = SimpleForm("crontab", translate("Scheduled Tasks"), translate("This is the system crontab in which scheduled tasks can be defined."))
9
10 t = f:field(TextValue, "crons")
11 t.rmempty = true
12 t.rows = 10
13 function t.cfgvalue()
14         return fs.readfile(cronfile) or ""
15 end
16
17 function f.handle(self, state, data)
18         if state == FORM_VALID then
19                 if data.crons then
20                         fs.writefile(cronfile, data.crons:gsub("\r\n", "\n"))
21                         luci.sys.call("/usr/bin/crontab %q" % cronfile)
22                 else
23                         fs.writefile(cronfile, "")
24                 end
25         end
26         return true
27 end
28
29 return f