1<?php
2  /**************************************************************************\
3  * phpGroupWare - widget_lists						     *
4  * http://www.phpgroupware.org                                              *
5  * This program is part of the GNU project, see http://www.gnu.org/         *
6  *                                                                          *
7  * Copyright 2003 Free Software Foundation, Inc.                            *
8  *                                                                          *
9  * Originally Written by Jonathan Alberto Rivera Gomez - jarg at co.com.mx  *
10  * Current Maintained by Jonathan Alberto Rivera Gomez - jarg at co.com.mx  *
11  * --------------------------------------------                             *
12  * Development of this application was funded by http://www.sogrp.com       *
13  * --------------------------------------------                             *
14  *  This program is Free Software; you can redistribute it and/or modify it *
15  *  under the terms of the GNU General Public License as published by the   *
16  *  Free Software Foundation; either version 2 of the License, or (at your  *
17  *  option) any later version.                                              *
18  \**************************************************************************/
19	class widget_lists
20	{
21		var $title;
22		var $form_name;
23		var $use_combos;
24
25		/**
26		* Set the parameters for the list box with all options
27		*
28		* @param string $title The title of the widget
29		* @param array $form_name The name form where it will be this widget
30		* @param string $use_combos Use True if you want to have combos in your widget
31		*/
32		function widget_lists($title, $form_name, $use_combos=True, $use_filter=False)
33		{
34			$this->template	= &$GLOBALS['phpgw']->template;
35			$this->title = $title;
36			$this->form_name = $form_name;
37			$this->use_combos = $use_combos;
38			$this->use_filter = $use_filter;
39		}
40
41		/**
42		* Draw the widget
43		*
44		* @return string The html code for this widget
45		*/
46		function get_widget()
47		{
48			$this->template->set_root($GLOBALS['phpgw']->common->get_tpl_dir('addressbook'));
49			$this->template->set_file(array('widget_list_t' => 'widget_lists.tpl'));
50			$this->template->set_block('widget_list_t', 'many_actions', 'many_actions');
51			$this->template->set_block('widget_list_t', 'combos', 'combos');
52			$this->template->set_block('widget_list_t', 'option_filter', 'option_filter');
53
54			$this->template->set_var('lang_general_title', $this->title);
55			$this->template->set_var('widget_list_form_name', $this->form_name);
56
57			if($this->use_combos)
58			{
59				$this->template->set_var('lang_left_combo_title', $this->left_combo_title);
60				$this->template->set_var('left_combo', $this->left_combo);
61				$this->template->set_var('lang_right_combo_title', $this->right_combo_title);
62				$this->template->set_var('right_combo', $this->right_combo);
63				$this->template->parse('combos_lists', 'combos');
64			}
65
66			if($this->use_filter)
67			{
68				$this->template->set_var('filter_by_label',
69							 lang('Filter by: '));
70				$this->template->set_var('filter_by_option_list',
71							 $this->get_list_filters());
72				$this->template->set_var('search_by_label',
73							 lang('Search: '));
74				$this->template->set_var('all_option_list_filter',
75							 $this->get_list_filters());
76				$this->template->parse('all_option_list_filter_body',
77						       'option_filter');
78			}
79
80			if(is_array($this->selected_option_list))
81			{
82				$this->all_option_list = array_diff($this->all_option_list, $this->selected_option_list);
83			}
84			else
85			{
86				$this->all_option_list = $this->all_option_list;
87			}
88
89			$this->template->set_var('lang_all_option_list_title', $this->all_option_list_title);
90			$this->template->set_var('all_option_list_name', $this->all_option_list_name);
91			$this->template->set_var('all_option_list', $this->get_option_list($this->all_option_list));
92			$this->template->set_var('lang_selected_option_list_title', $this->selected_option_list_title);
93			$this->template->set_var('selected_option_list_name', $this->selected_option_list_name);
94			$this->template->set_var('selected_option_list', $this->get_option_list($this->selected_option_list));
95
96			if($this->right_combo_link_opt)
97			{
98				$this->template->set_var('current_opt', $this->right_combo_name);
99			}
100			elseif($this->left_combo_link_opt)
101			{
102				$this->template->set_var('current_opt', $this->left_combo_name);
103			}
104
105			$this->template->set_var('th_bg',   $GLOBALS['phpgw_info']['theme']['th_bg']);
106			$this->template->set_var('th_text', $GLOBALS['phpgw_info']['theme']['th_text']);
107			$this->template->set_var('row_on',  $GLOBALS['phpgw_info']['theme']['row_on']);
108			$this->template->set_var('row_off', $GLOBALS['phpgw_info']['theme']['row_off']);
109			$this->template->set_var('row_text',$GLOBALS['phpgw_info']['theme']['row_text']);
110
111			return $this->template->fp('out', 'many_actions');
112		}
113
114		/**
115		* Set the parameters for the list box with all options
116		*
117		* @param string $title The title of the list box
118		* @param array $list_name The name of the list box
119		* @param string $list The array with the data for this list box ($list[$key] = $value)
120		* @return mixed The option list box which have all options
121		*/
122		function set_all_option_list($title, $list_name, $list=array())
123		{
124			if(!is_array($list))
125			{
126				$list = array();
127			}
128
129			$this->all_option_list_title = $title;
130			$this->all_option_list = $list;
131			$this->all_option_list_name = $list_name;
132		}
133
134		/**
135		* Set the parameters for the list box with selected options
136		*
137		* @param string $title The title of the list box
138		* @param array $list_name The name of the list box
139		* @param string $list The array with the data for this list box ($list[$key] = $value)
140		* @return mixed The option list box which have selected options
141		*/
142		function set_selected_option_list($title, $list_name, $list=array())
143		{
144			if(!is_array($list))
145			{
146				$list = array();
147			}
148
149			$this->selected_option_list_title = $title;
150			$this->selected_option_list = $list;
151			$this->selected_option_list_name = $list_name;
152			$this->old_option_list = $list;
153		}
154
155		/**
156		* Set the parameters for the left combo
157		*
158		* @param string $title The title of the combo
159		* @param array $list_name The name of the combo
160		* @param string $list The array with the data for this list box ($list[$key] = $value)
161		* @param string $selected The option selected for this combo
162		* @param boolean $use_js For use javascript
163		* @return mixed The left combo box
164		*/
165		function set_left_combo($title, $list_name, $list=array(), $selected='', $use_js=False, $link_opt=False)
166		{
167			$this->left_combo_title = $title;
168			$this->left_combo_name = $list_name;
169			$this->left_combo = $this->get_combo($list_name, $list, $selected, $use_js);
170			$this->left_combo_link_opt = $link_opt;
171		}
172
173		function set_left_text($title, $list_name, $value='')
174		{
175			$this->left_combo_title = $title;
176			$this->left_combo = $this->get_text($list_name, $value);
177		}
178
179		/**
180		* Set the parameters for the right combo
181		*
182		* @param string $title The title of the combo
183		* @param array $list_name The name of the combo
184		* @param string $list The array with the data for this list box ($list[$key] = $value)
185		* @param string $selected The option selected for this combo
186		* @param boolean $use_js For use javascript
187		* @return mixed The right combo box
188		*/
189		function set_right_combo($title, $list_name, $list=array(), $selected='', $use_js=False, $link_opt=False)
190		{
191			$this->right_combo_title = $title;
192			$this->right_combo_name = $list_name;
193			$this->right_combo = $this->get_combo($list_name, $list, $selected, $use_js);
194			$this->right_combo_link_opt = $link_opt;
195		}
196
197		/**
198		* Get the records to process, new and deleted
199		*
200		* @return array The array with all new records and deleted
201		* ($array = array(delete => values, insert => values, edit => values))
202		*/
203		function get_resul_list()
204		{
205			$pos = strpos($this->selected_option_list_name, '[]');
206			$var_option_name = $pos?substr($this->selected_option_list_name,0,$pos):$this->selected_option_list_name;
207			return $this->diff_arrays(array_keys($this->old_option_list), get_var($var_option_name));
208		}
209
210		/**
211		* Compare two arrays and return the diferences
212		*
213		* @param array $old_array The array with old options
214		* @param array $new_array The array with new options
215		* @return array The array with diferences
216		* ($array = array(delete => values, insert => values, edit => values))
217		*/
218		function diff_arrays($old_array=array(), $new_array=array())
219		{
220			if(!is_array($old_array))
221			{
222				$old_array =  array();
223			}
224
225			if(!is_array($new_array))
226			{
227				$new_array =  array();
228			}
229
230			$result['delete'] = array_diff($old_array, $new_array);
231			$result['insert'] = array_diff($new_array, $old_array);
232			$result['edit'] = array_intersect($old_array, $new_array);
233			return $result;
234		}
235
236		/**
237		* Get the combo box
238		*
239		* @param string $name The name of the combo
240		* @param array $list The array with the data for this list box ($list[$key] = $value)
241		* @param string $selected The option selected for this combo
242		* @param boolean $use_js For use javascript
243		* @return mixed The combo box
244		*/
245		function get_combo($name, $list=array(), $selected='', $use_js=False)
246		{
247			$js_str = $use_js?'onChange="this.form.submit();"':'';
248			$str = '<select name="'.$name.'" '. $js_str .'  style="width:220">'.$this->get_option_list($list, $selected).'</select>';
249			return $str;
250		}
251
252		function get_text($list_name, $value)
253		{
254			$str = '<input type="text" name="'.$list_name.'" value="'.$value.'">';
255			return $str;
256		}
257
258		/**
259		* Make the option list html code
260		*
261		* @param array $list The array with the data for this list box ($list[$key] = $value)
262		* @param string $selected The option what you selected
263		* @return string The html code with all options
264		*/
265		function get_option_list($list=array(), $selected='')
266		{
267			$selected_option[$selected] = ' selected';
268			if(is_array($list))
269			{
270				foreach($list as $key => $data)
271				{
272					$str .= '<option value="'.$key.'" ' . $selected_option[$key] . '>'.$data.'</option>';
273				}
274			}
275			return $str;
276		}
277
278		/**
279		* Get the javascript function for use in the form
280		*
281		* @return string The javascript function for use in the form
282		*/
283		function get_onsubmit_js_string()
284		{
285			return 'onsubmit="process_list(\''
286				.$this->all_option_list_name
287				.'\',\''
288				.$this->selected_option_list_name.'\')"';
289		}
290
291		function set_list_filters($list_options, $selected='')
292		{
293			if(is_array($list_options))
294			{
295				$sel_opt[$selected] = 'selected';
296				foreach($list_options as $key => $value)
297				{
298					$opt .= '<option value="'.$key.'"'.$sel_opt[$key].'>'.$value.'</option>';
299				}
300				$this->filter_option_list = $opt;
301			}
302			else
303			{
304				$this->filter_option_list = $list_options;
305			}
306		}
307
308		function get_list_filters()
309		{
310			return $this->filter_option_list;
311		}
312
313		function get_onload_js_string()
314		{
315			return 'onload="setUpVisual(\''
316				.$this->form_name
317				.'\',\''.$this->all_option_list_name
318				.'\',\'searchautocomplete\');obj1.bldUpdate();"';
319		}
320
321		/**
322		* Get the javascript functions which are necesary for this widget
323		*
324		* @return string The javascript functions
325		*/
326		function java_script()
327		{
328			$tmp= '
329			<SCRIPT LANGUAGE="JavaScript">
330
331			function move_cbo(sboxname, cboxname) {
332				sbox = document.'.$this->form_name.'.elements[sboxname];
333				cbox = document.'.$this->form_name.'.elements[cboxname];
334				if(sbox.length > 0)
335				{
336					sel_opt = sbox.options[sbox.selectedIndex].text;
337				}
338				else
339				{
340					sel_opt="";
341				}
342				sbox.length = 0;
343				for(c = 0; c < cbox.length; c++)
344				{
345					var no = new Option();
346					no.value = cbox[c].value;
347					no.text = cbox[c].text;
348					if(no.text == sel_opt)
349					{
350						i = c;
351					}
352					sbox[c] = no;
353				}
354				if(i>0)
355				{
356					sbox.options[i].selected = true;
357				}
358			}
359
360			function process_list(allboxname, myboxname) {
361				mybox = document.'.$this->form_name.'.elements[myboxname];
362				for(c = 0; c < mybox.options.length; c++)
363				{
364					mybox.options[c].selected = true;
365				}
366			}
367
368			</script>'
369				.
370			'<script src="'.$GLOBALS['phpgw']->link("/phpgwapi/js/contacts/selectboxes.js").'"> </script>';
371			return $tmp;
372		}
373	}
374?>
375