1<?php
2  /**************************************************************************\
3  * phpGroupWare - uicategorize_contacts                                     *
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
20	class uicategorize_contacts
21	{
22		var $public_functions = array(
23			'index' => True,
24			'java_script' => True
25			);
26
27		function uicategorize_contacts()
28		{
29			$this->template	= &$GLOBALS['phpgw']->template;
30			$this->lists = CreateObject('addressbook.widget_lists', 'Categories', 'categorize_contacts_form');
31			$this->cat = CreateObject('phpgwapi.categories');
32			$this->contacts = CreateObject('phpgwapi.contacts');
33		}
34
35		function index()
36		{
37			$this->get_vars();
38
39			$this->lists->set_left_combo('Category', 'all_cats', $this->get_categories(), $this->selected_cat, True);
40			$this->lists->set_all_option_list('All Persons', 'person_all[]', $this->get_all_persons());
41			$this->lists->set_selected_option_list('Current Persons', 'person_current[]',
42							       $this->get_persons_by_cat($this->selected_cat));
43
44			switch($this->action)
45			{
46			case 'save':
47				$persons = $this->lists->get_resul_list();
48				$this->save_categories_by_person($this->selected_cat, $persons);
49				Header('Location: '
50				       . $GLOBALS['phpgw']->link('/index.php','menuaction=addressbook.uicategorize_contacts.index'));
51				$GLOBALS['phpgw']->common->phpgw_exit();
52				break;
53			case 'cancel':
54				Header('Location: '
55				       . $GLOBALS['phpgw']->link('/index.php','menuaction=addressbook.uiaddressbook.index&section=Persons'));
56				$GLOBALS['phpgw']->common->phpgw_exit();
57				break;
58			}
59			$this->draw_form();
60		}
61
62		function draw_form()
63		{
64			$this->template->set_file(array('manage_cats_t' => 'categorize_contacts.tpl'));
65
66			$list_widget = $this->lists->get_widget();
67			$onsubjs = $this->lists->get_onsubmit_js_string();
68			$GLOBALS['phpgw']->common->phpgw_header();
69			echo parse_navbar();
70			$this->template->set_var('action',
71						 $GLOBALS['phpgw']->link('/index.php',
72									 'menuaction=addressbook.uicategorize_contacts.index'));
73			$this->template->set_var('form_name', 'categorize_contacts_form');
74			$this->template->set_var('onsubjs', $onsubjs);
75			$this->template->set_var('widget_lists', $list_widget);
76			$this->template->pfp('out', 'manage_cats_t');
77			$GLOBALS['phpgw']->common->phpgw_exit();
78		}
79
80		function save_categories_by_person($selected_cat, $persons=array())
81		{
82			$delete = $persons['delete'];
83			$insert = $persons['insert'];
84			$edit = $persons['edit'];
85
86			if($selected_cat==-1)
87			{
88				return;
89			}
90
91			if(is_array($delete))
92			{
93				foreach($delete as $person_id)
94				{
95					$cats = $this->contacts->get_cats_by_person($person_id);
96					foreach($cats as $key => $value)
97					{
98						if($value == '' || $value==$selected_cat)
99						{
100							unset($cats[$key]);
101						}
102					}
103					$this->contacts->edit_category($person_id, $cats, PHPGW_SQL_RUN_SQL);
104				}
105			}
106
107			if(is_array($insert))
108			{
109				foreach($insert as $person_id)
110				{
111					$cats = $this->contacts->get_cats_by_person($person_id);
112					$cats[] = $selected_cat;
113					foreach($cats as $key => $value)
114					{
115						if($value == '')
116						{
117							unset($cats[$key]);
118						}
119					}
120					$this->contacts->edit_category($person_id, $cats, PHPGW_SQL_RUN_SQL);
121				}
122			}
123		}
124
125		function get_all_persons()
126		{
127			return $this->get_persons();
128		}
129
130		function get_persons_by_cat($cat_id='')
131		{
132			if($cat_id)
133			{
134				return $this->get_persons($cat_id);
135			}
136		}
137
138		function get_persons($cat_id=PHPGW_CONTACTS_CATEGORIES_ALL, $access=PHPGW_CONTACTS_ALL)
139		{
140			$criteria = $this->contacts->criteria_for_index(
141				$GLOBALS['phpgw_info']['user']['account_id'], $access, $cat_id);
142			$persons = $this->contacts->get_persons(
143				array('person_id', 'per_full_name'),
144				'', '', 'last_name','ASC','',$criteria);
145
146			if(is_array($persons))
147			{
148				foreach($persons as $key => $data)
149				{
150					$persons_data[$data['person_id']] = $data['per_full_name'];
151				}
152				asort($persons_data);
153			}
154			return $persons_data;
155		}
156
157		function get_categories()
158		{
159			$cats = $this->cat->return_array($type,$start,$limit,$query,$sort,$order,False);
160			$categories[-1] = 'Select one...';
161			if(is_array($cats))
162			{
163				foreach($cats as $data)
164				{
165					$categories[$data['id']] = $data['name'];
166				}
167			}
168			return $categories;
169		}
170
171		function java_script()
172		{
173			return $this->lists->java_script();
174		}
175
176		function get_vars()
177		{
178			$save = get_var('save', array('post', 'get'));
179			$cancel = get_var('cancel', array('post', 'get'));
180
181			if($save)
182			{
183				$this->action = 'save';
184			}
185			elseif($cancel)
186			{
187				$this->action = 'cancel';
188			}
189			$this->selected_cat = get_var('all_cats', array('post', 'get'));
190		}
191	}
192?>
193