Trying to access array offset on value of type bool. Fixed.
[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 __construct($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                 parent::__construct($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                         start_table(TABLESTYLE, "width='80%'");
39
40                         $th = array(_('Assignment'), _("Reference"), _("Full Name"), _("Phone"), _("Sec Phone"), _("Fax"),
41                                 _("email"),  '',
42                                 "&nbsp;");
43
44                         table_header($th);
45                         $k = 0; 
46
47                         $last = $myrow = db_fetch($contacts);
48                         while ($last)
49                         {
50                                         
51                                 if (@$myrow['id'] != $last['id']) {
52                                         alt_table_row_color($k);
53                                         label_cell(implode('<br>',$description));
54                                         label_cell($last["ref"]);
55                                         label_cell($last["name"].' '.$last["name2"]);
56                                         label_cell($last["phone"]);
57                                         label_cell($last["phone2"]);
58                                         label_cell($last["fax"]);
59                                         email_cell($last["email"]);
60
61                                         edit_button_cell("{$this->name}Edit[{$last['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[{$last['id']}]", _("Delete"));
65                                         end_row();
66                                         $description = array();
67                                         $last = $myrow;
68                                 }
69
70                                 if ($myrow) {
71                                         $description[] = $myrow['description'];
72                                         $myrow = db_fetch($contacts);
73                                 }
74                         }
75
76                         //inactive_control_row($th);
77                         end_table(1);
78         }
79         
80         function editor_view()
81         {
82                 br();
83
84                 start_outer_table(TABLESTYLE);
85
86                 table_section(1);
87                 table_section_title(_("Contact data"));
88
89                 text_row(_("First Name:"), 'name', @$this->data['name'], 35, 40);
90                 text_row(_("Last Name:"), 'name2', @$this->data['name2'], 35, 40);
91                 text_row(_("Reference:"), 'ref', @$this->data['ref'], 35, 40);
92
93                 crm_category_types_list_row(_("Contact active for:"), 'assgn', @$this->data['contacts'],
94                         array('subclass' => @$this->subclass,
95                                 'class' => @$this->class, 
96                                 'multi' =>true)
97                                 );
98
99                 text_row(_("Phone:"), 'phone', @$this->data['phone'], 32, 30);
100                 text_row(_("Secondary Phone Number:"), 'phone2', @$this->data['phone2'], 32, 30);
101                 text_row(_("Fax Number:"), 'fax', @$this->data['fax'], 32, 30);
102                 email_row(_("E-mail:"), 'email', @$this->data['email'], 35, 55);
103
104                 table_section(2);
105                 table_section_title("&nbsp;");
106                 textarea_row(_("Address:"), 'address', @$this->data['address'], 30, 4);
107                 languages_list_row( _("Document Language:"), 'lang', @$this->data['lang'], _("Customer default"));
108
109                 textarea_row(_("Notes:"), 'notes', @$this->data['notes'], 30, 4);
110                 end_outer_table(1);
111         }
112
113         function db_insert() {
114                         $dat = $this->data;
115                         return add_crm_person($dat['ref'], $dat['name'], $dat['name2'], $dat['address'],
116                                 $dat['phone'], $dat['phone2'], $dat['fax'], $dat['email'], $dat['lang'],
117                                 $dat['notes'], $dat['contacts'], $this->entity);
118         }
119
120         function db_update() {
121                         return update_crm_person($this->selected_id, $_POST['ref'], $_POST['name'], $_POST['name2'], 
122                                 $_POST['address'], $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], 
123                                 $_POST['lang'], $_POST['notes'], $_POST['assgn'], $this->entity, $this->class);
124         }
125
126         function insert_check() {
127                 if (strlen($_POST['name']) == 0) {
128                         display_error(_("The contact name cannot be empty."));
129                         set_focus('name');
130                         return false;
131                 }
132                 if (strlen($_POST['ref']) == 0) {
133                         display_error(_("Contact reference cannot be empty."));
134                         set_focus('ref');
135                         return false;
136                 }
137                 if (count_array(@$_POST['assgn']) == 0) {
138                         display_error(_("You have to select at least one category."));
139                         set_focus('assgn');
140                         return false;
141                 }
142                 return true; 
143         }
144
145         function db_read() {
146                 return get_crm_person($this->selected_id);
147         }
148
149         function delete_check() {
150                 return true;
151         }
152         //
153         //      Delete all contacts for person in current class/entity
154         //
155         function db_delete() {
156                 if (delete_crm_contacts($this->selected_id, $this->class, $this->entity)) {
157                         // if this is the last relation - delete contact data
158                         $rels = get_person_contacts($this->selected_id);
159                         if (count($rels)==0) {
160                                 return delete_crm_person($this->selected_id);
161                         }
162                 }
163                 return false;
164         }
165 }
166