summaryrefslogtreecommitdiffstats
path: root/app/acf-util/skins-model.lua
blob: b717de65a5048b5505067faf21b65e8e80c18460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module (..., package.seeall)

require("modelfunctions")
fs = require("acf.fs")
format = require("acf.format")

local function set_skins(self, skin)
	local content = "\n"..(fs.read_file(self.conf.conffile) or "")
	local count
	content,count = string.gsub(content, "\n%s*skin%s*=[^\n]*", "\nskin="..skin)
	if count == 0 then
		content = "\nskin="..skin..content
	end
	fs.write_file(self.conf.conffile, string.sub(content,2))
end

local function list_skins(self)
	local skinarray = {}
	for skin in string.gmatch(self.conf.skindir, "[^,]+") do
		for i,file in ipairs(posix.dir(self.conf.wwwdir ..skin) or {}) do
			-- Ignore files that begins with a '.' and 'cgi-bin' and only list folders
			if not ((string.match(file, "^%.")) or (string.match(file, "^cgi[-]bin")) or (string.match(file, "^static")) or (posix.stat(self.conf.wwwdir .. skin .. file).type ~= "directory")) then
				local entry = cfe({ value=skin..file, label=file })
				local current = self.conf.skin
				entry.inuse = (skin..file == current)
				table.insert(skinarray, entry)
			end
		end
	end
	return skinarray
end


get = function (self)
	return cfe({ type="list", value=list_skins(self), label="Skins" })
end

get_update = function (self)
	local skin = cfe({ type="select", value="", label="Skin", option=list_skins(self) })
	if self and self.conf and self.conf.skin then
		skin.value = self.conf.skin
	end
	return cfe({ type="group", value={skin=skin}, label="Update Skin" })
end

update = function (self, newskin)
	local success = modelfunctions.validateselect(newskin.value.skin)
	if success then
		set_skins(self, newskin.value.skin.value)
		self.conf.skin = newskin.value.skin.value
	else
		newskin.errtxt = "Failed to set skin"
	end
	return newskin
end