/* 
   The JitterBug bug tracking system
   user setable pulldown code

   Copyright (C) Andrew Tridgell 1999

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "jitterbug.h"

/* get the names of the pulldowns */
char **pulldown_names(void)
{
	static char **ret;
	if (!ret) {
		ret = load_dir_list(xsprintf("%s/%s", root_directory(), lp_pulldown_directory()), NULL);
	}
	return ret;
}

/* get the list of the named pulldown */
char **pulldown_list(char *name)
{
	return load_file_list(
			      xsprintf("%s/%s/%s", 
				       root_directory(), 
				       lp_pulldown_directory(),
				       name), NULL);
}

void display_pulldowns(char *id)
{
	int i;
	char **pulldowns;
	char *def=NULL, *p;

	pulldowns = pulldown_names();
	if (!pulldowns) return;

	printf("<br>\n");

	for (i=0;pulldowns[i];i++) {
		char **list = pulldown_list(pulldowns[i]);
		printf("%s: ", pulldowns[i]);
		if (id) {
			def = load_file(xsprintf("%s.%s", id, pulldowns[i]), NULL, 0);
		} else {
			def = cgi_variable(xsprintf("pulldown..%s", pulldowns[i]));
		}
		if (def && (p=strchr(def,'\n'))) *p = 0;
		html_select_list(list, xsprintf("pulldown.%s.%s", id?id:"", pulldowns[i]), def);
		printf("&nbsp;&nbsp;");
	}
	printf("<br>\n");
}

void display_pulldowns_fixed(char *id)
{
	int i;
	char **pulldowns;
	char *def, *p;

	pulldowns = pulldown_names();
	if (!pulldowns) return;

	printf("<br>\n");

	for (i=0;pulldowns[i];i++) {
		def = load_file(xsprintf("%s.%s", id, pulldowns[i]), NULL, 0);
		if (def && (p=strchr(def,'\n'))) *p = 0;
		printf("<b>%s:</b> %s&nbsp;&nbsp;", pulldowns[i], def?def:"");
	}
	printf("<br>\n");
}



void pulldowns_do_changes(char *id)
{
	int i;
	char **pulldowns = pulldown_names();
	if (!pulldowns) return;

	for (i=0;pulldowns[i];i++) {
		char *v;
		if ((v=cgi_variable(xsprintf("pulldown.%s.%s", id, pulldowns[i])))) {
			if (!*v) {
				unlink(xsprintf("%s.%s", id, pulldowns[i]));
			} else {
				char **list = pulldown_list(pulldowns[i]);
				if (in_list(list, v)) {
					save_file(xsprintf("%s.%s", id, pulldowns[i]), v);
					add_audit(id, "changed pulldown %s.%s", id, pulldowns[i]);
				}
			}
		}
	}
}


int pulldown_match(char *id)
{
	int i;
	char *buf;
	char **pulldowns;

	pulldowns = pulldown_names();
	if (!pulldowns) return 1;

	for (i=0;pulldowns[i];i++) {
		char **list;
		char *v;

		v = cgi_variable(xsprintf("pulldown..%s", pulldowns[i]));
		if (!v || !*v) continue;
		
		list = pulldown_list(pulldowns[i]);
		buf = load_file(xsprintf("%s.%s", id, pulldowns[i]), NULL, 0);
		if (!buf) return 0;
		trim_string(buf, NULL, "\n");
		if (strcmp(v, buf)!=0) return 0;
	}

	return 1;
}


void pulldown_hiddens(void)
{
	char **pulldowns;
	int i;

	pulldowns = pulldown_names();
	if (!pulldowns) return;

	for (i=0;pulldowns[i];i++) {
		char *v;

		v = cgi_variable(xsprintf("pulldown..%s", pulldowns[i]));
		if (!v || !*v) continue;
		
		html_input_hidden(xsprintf("pulldown..%s", pulldowns[i]), v);
	}
}

char *pulldown_url_globals(char *p)
{
	char **pulldowns;
	int i;

	pulldowns = pulldown_names();
	if (!pulldowns) return p;

	for (i=0;pulldowns[i];i++) {
		char *v;

		v = cgi_variable(xsprintf("pulldown..%s", pulldowns[i]));
		if (!v || !*v) continue;
		
		p += html_url_var(p, xsprintf("pulldown..%s", pulldowns[i]), v);
	}

	return p;
}
