Fixed error on new contact add.
[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         var $entity;
21         var $sub_class;
22         var $class;
23         
24         function contacts($name, $id, $class, $subclass=null) {
25                 $fields = array('ref','name','name2','address', 'phone', 'phone2','fax',
26                         'email','lang','notes', 'assgn' =>array('fld'=>'contacts'));
27
28                 $this->simple_crud($name, $fields);
29                 $this->class = $class;
30                 $this->subclass = $subclass;
31                 $this->entity = $id;
32         }
33
34         function list_view() {
35                 br();
36
37                         $contacts = get_crm_persons($this->class, $this->subclass, $this->entity);
38 //                              check_value('show_inactive'));
39                         start_table(TABLESTYLE, "width=80%");
40
41                         $th = array(_('Assignment'), _("Reference"), _("Full Name"), _("Phone"), _("Sec Phone"), _("Fax"),
42                                 _("email"),  '',
43                                 "&nbsp;");
44                 //      inactive_control_column($th);
45
46                         table_header($th);
47                         $k = 0; 
48                         while ($myrow = db_fetch($contacts)) 
49                         {
50                                 alt_table_row_color($k);
51                                 label_cell($myrow["description"]);
52                                 label_cell($myrow["ref"]);
53                                 label_cell($myrow["name"].' '.$myrow["name2"]);
54                                 label_cell($myrow["phone"]);
55                                 label_cell($myrow["phone2"]);
56                                 label_cell($myrow["fax"]);
57                                 email_cell($myrow["email"]);
58
59                 //      inactive_control_cell($myrow["id"], $myrow["inactive"], 'crm_contacts', 'id');
60
61                                 edit_button_cell("{$this->name}Edit[{$myrow['id']}]", _("Edit"));
62
63                         // delete button removes only category unless this is the last contact for this person
64                                 delete_button_cell("{$this->name}Delete[{$myrow['contact_id']}]", _("Delete"));
65                                 end_row();
66                         }
67         
68 //inactive_control_row($th);
69                         end_table(1);
70         }
71         
72         function editor_view()
73         {
74                 br();
75
76                 start_outer_table(TABLESTYLE, "width=80%");
77
78                 table_section(1);
79                 table_section_title(_("Contact data"));
80
81                 text_row(_("First Name:"), 'name', @$this->data['name'], 35, 40);
82                 text_row(_("Last Name:"), 'name2', @$this->data['name2'], 35, 40);
83                 text_row(_("Reference:"), 'ref', @$this->data['ref'], 35, 40);
84
85                 crm_category_types_list_row(_("Contact active for:"), 'assgn', @$this->data['contacts'],
86                         array('subclass' => @$this->subclass,
87                                 'class' => @$this->class, 
88                                 'multi' =>true)
89                                 );
90
91                 text_row(_("Phone:"), 'phone', @$this->data['phone'], 32, 30);
92                 text_row(_("Secondary Phone Number:"), 'phone2', @$this->data['phone2'], 32, 30);
93                 text_row(_("Fax Number:"), 'fax', @$this->data['fax'], 32, 30);
94                 email_row(_("E-mail:"), 'email', @$this->data['email'], 35, 55);
95
96                 table_section(2);
97                 textarea_row(_("Address:"), 'address', @$this->data['address'], 30, 4);
98                 languages_list_row( _("Document Language:"), 'lang', @$this->data['lang'], _("Customer default"));
99
100                 textarea_row(_("Notes:"), 'notes', @$this->data['notes'], 30, 4);
101                 end_outer_table(1);
102         }
103
104         function db_insert() {
105                         $dat = $this->data;
106                         return add_crm_person($dat['ref'], $dat['name'], $dat['name2'], $dat['address'],
107                                 $dat['phone'], $dat['phone2'], $dat['fax'], $dat['email'], $dat['lang'],
108                                 $dat['notes'], $dat['contacts'], $this->entity);
109         }
110
111         function db_update() {
112                         return update_crm_person($this->selected_id, $_POST['ref'], $_POST['name'], $_POST['name2'], 
113                                 $_POST['address'], $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], 
114                                 $_POST['lang'], $_POST['notes'], $_POST['assgn'], $this->entity);
115                 
116         }
117
118         function insert_check() {
119                 if (strlen($_POST['name']) == 0) {
120                         display_error(_("The contact name cannot be empty."));
121                         set_focus('name');
122                         return false;
123                 }
124                 if (strlen($_POST['ref']) == 0) {
125                         display_error(_("Contact reference cannot be empty."));
126                         set_focus('ref');
127                         return false;
128                 }
129                 if (count(@$_POST['assgn']) == 0) {
130                         display_error(_("You have to select at least one category."));
131                         set_focus('assgn');
132                         return false;
133                 }
134                 return true; 
135         }
136
137         function db_read() {
138                 return get_crm_person($this->selected_id);
139         }
140
141         function delete_check() {
142                 return true;
143         }
144         //
145         //      Delete single contact only (unless this is last contact for this person)
146         //
147         function db_delete() {
148                 $rel = get_crm_contact($this->selected_id);
149                 if (delete_crm_contact($this->selected_id)) {
150                         // if this is the last relation - delete contact data
151                         $rels = get_person_contacts($rel['person_id']);
152                         if (count($rels)==0) {
153                                 return delete_crm_person($rel['person_id']);
154                         }
155                 }
156                 return false;
157         }
158 }
159
160 ?>