Contact CRUD class.
[fa-stable.git] / includes / ui / contacts_view.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root. '/includes/ui/simple_crud_class.inc');
13 include_once($path_to_root . "/includes/db/crm_contacts_db.inc");
14
15 /*
16         View/Edit class for contacts
17 */
18 class contacts extends simple_crud {
19         var $id;
20
21         function contacts($name, $id, $class, $subclass=null) {
22                 $this->simple_crud($name);
23
24                 $this->class = $class;
25                 $this->subclass = $subclass;
26                 $this->entity = $id;
27         }
28
29         function listing() {
30                 br();
31
32                         $contacts = get_crm_persons($this->class, $this->subclass, $this->entity);
33 //                              check_value('show_inactive'));
34                         start_table(TABLESTYLE, "width=80%");
35
36                         $th = array(_('Assignment'), _("Reference"), _("Full Name"), _("Phone"), _("Sec Phone"), _("Fax"),
37                                 _("email"),  '',
38                                 "&nbsp;");
39                 //      inactive_control_column($th);
40
41                         table_header($th);
42                         $k = 0; 
43                         while ($myrow = db_fetch($contacts)) 
44                         {
45                                 alt_table_row_color($k);
46                                 label_cell($myrow["description"]);
47                                 label_cell($myrow["ref"]);
48                                 label_cell($myrow["name"].' '.$myrow["name2"]);
49                                 label_cell($myrow["phone"]);
50                                 label_cell($myrow["phone2"]);
51                                 label_cell($myrow["fax"]);
52                                 email_cell($myrow["email"]);
53
54                 //      inactive_control_cell($myrow["id"], $myrow["inactive"], 'crm_contacts', 'id');
55
56                                 edit_button_cell("{$this->name}Edit".$myrow["id"], _("Edit"));
57
58                         // delete button removes only category unless this is the last conatct for this person
59                                 delete_button_cell("{$this->name}Delete".$myrow["contact_id"], _("Delete"));
60                                 end_row();
61                         }
62         
63 //inactive_control_row($th);
64                         end_table(1);
65         }
66         
67         function editor()
68         {
69                 br();
70
71                 start_outer_table(TABLESTYLE, "width=80%");
72
73                 table_section(1);
74                 table_section_title(_("Contact data"));
75
76                 text_row(_("First Name:"), 'name', @$this->data['name'], 35, 40);
77                 text_row(_("Last Name:"), 'name2', @$this->data['name2'], 35, 40);
78                 text_row(_("Reference:"), 'ref', @$this->data['ref'], 35, 40);
79
80                 crm_category_types_list_row(_("Contact active for:"), 'assgn', @$this->data['contacts'],
81                         array('subclass' => @$this->subclass,
82                                 'class' => @$this->class, 
83                                 'multi' =>true)
84                                 );
85
86                 text_row(_("Phone:"), 'phone', @$this->data['phone'], 32, 30);
87                 text_row(_("Secondary Phone Number:"), 'phone2', @$this->data['phone2'], 32, 30);
88                 text_row(_("Fax Number:"), 'fax', @$this->data['fax'], 32, 30);
89                 email_row(_("E-mail:"), 'email', @$this->data['email'], 35, 55);
90
91                 table_section(2);
92                 textarea_row(_("Address:"), 'address', @$this->data['address'], 30, 4);
93                 languages_list_row( _("Document Language:"), 'lang', @$this->data['lang'], _("Customer default"));
94
95                 textarea_row(_("Notes:"), 'notes', @$this->data['notes'], 30, 4);
96                 end_outer_table(1);
97
98 //              submit_add_or_update_center($this->selected_id == '0', '', 'both', 
99 //                      $this->selected_id=='0', $this->name);
100         }
101
102         function db_insert() {
103                         return add_crm_person($_POST['ref'], $_POST['name'], $_POST['name2'], $_POST['address'],
104                                 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], $_POST['lang'],
105                                 $_POST['notes'], $_POST['assgn'], $this->entity);
106         }
107
108         function db_update() {
109                         return update_crm_person($this->selected_id, $_POST['ref'], $_POST['name'], $_POST['name2'], 
110                                 $_POST['address'], $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], 
111                                 $_POST['lang'], $_POST['notes'], $_POST['assgn'], $this->entity);
112                 
113         }
114
115         function insert_check() {
116                 if (strlen($_POST['name']) == 0) {
117                         display_error(_("The contact name cannot be empty."));
118                         set_focus('name');
119                         return false;
120                 }
121                 if (strlen($_POST['ref']) == 0) {
122                         display_error(_("Contact reference cannot be empty."));
123                         set_focus('ref');
124                         return false;
125                 }
126                 if (count(@$_POST['assgn']) == 0) {
127                         display_error(_("You have to select at least one category."));
128                         set_focus('assgn');
129                         return false;
130                 }
131                 return true; 
132         }
133
134         function db_read() {
135                 return get_crm_person($this->selected_id);
136         }
137
138         function delete_check() {
139                 return true;
140         }
141         //
142         //      Delete single contact only (unless this is last contact for this person)
143         //
144         function db_delete() {
145                 $rel = get_crm_contact($this->selected_id);
146                 if (delete_crm_contact($this->selected_id)) {
147                         // if this is the last relation - delete contact data
148                         $rels = get_person_contacts($rel['person_id']);
149                         if (count($rels)==0) {
150                                 return delete_crm_person($rel['person_id']);
151                         }
152                 }
153                 return false;
154         }
155 }
156
157 ?>