luci-app-olsr: handle empty result for non-status tables
[oweals/luci.git] / modules / luci-mod-system / 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"),
9         translate("This is the system crontab in which scheduled tasks can be defined.") ..
10         translate("<br/>Note: you need to manually restart the cron service if the " ..
11                 "crontab file was empty before editing."))
12
13 t = f:field(TextValue, "crons")
14 f.forcewrite = true
15 t.rmempty = true
16 t.rows = 10
17 function t.cfgvalue()
18         return fs.readfile(cronfile) or ""
19 end
20
21 function f.handle(self, state, data)
22         if state == FORM_VALID then
23                 if data.crons then
24                         fs.writefile(cronfile, data.crons:gsub("\r\n", "\n"))
25                         luci.sys.call("/usr/bin/crontab %q" % cronfile)
26                 else
27                         fs.writefile(cronfile, "")
28                 end
29         end
30         return true
31 end
32
33 return f