[go: up one dir, main page]

Menu

[3822d7]: / core / Contact.php  Maximize  Restore  History

Download this file

330 lines (291 with data), 10.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
namespace ICT\Core;
/* * ***************************************************************
* Copyright © 2014 ICT Innovations Pakistan All Rights Reserved *
* Developed By: Nasir Iqbal *
* Website : http://www.ictinnovations.com/ *
* Mail : nasir@ictinnovations.com *
* *************************************************************** */
class Contact
{
/** @const */
const COMPANY = -2;
private static $table = 'contact';
private static $table_link = 'contact_link';
private static $primary_key = 'contact_id';
private static $fields = array(
'contact_id',
'first_name',
'last_name',
'phone',
'email',
'address',
'custom1',
'custom2',
'custom3',
'description'
);
private static $read_only = array(
'contact_id'
);
/**
* @property-read integer $contact_id
* @var integer
*/
public $contact_id = NULL;
/** @var string */
public $first_name = NULL;
/** @var string */
public $last_name = NULL;
/** @var string */
public $phone = NULL;
/** @var string */
public $email = NULL;
/** @var string */
public $address = NULL;
/** @var string */
public $custom1 = NULL;
/** @var string */
public $custom2 = NULL;
/** @var string */
public $custom3 = NULL;
/** @var string */
public $description = '';
/**
* @property-read integer $user_id
* owner id of current record
* @var integer
*/
public $user_id = NULL;
public function __construct($contact_id = NULL)
{
if (!empty($contact_id) && $contact_id > 0) {
$this->contact_id = $contact_id;
$this->load();
} else if (Contact::COMPANY == $contact_id) {
$this->contact_id = $contact_id;
$title = Conf::get('company:title', 'ICTCore');
$aTitle = explode(' ', $title, 2);
$this->first_name = $aTitle[0];
$this->last_name = isset($aTitle[1]) ? $aTitle[1] : '';
$this->email = Conf::get('company:email', 'no-reply@example.com');
$this->phone = Conf::get('company:phone', '1111111111');
$this->address = Conf::get('company:address', 'PK');
}
}
public static function construct_from_array($aContact)
{
$oContact = new Contact();
foreach ($aContact as $field => $value) {
$oContact->$field = $value;
}
return $oContact;
}
public static function locate($contact, $contactField = 'phone')
{
// locate an existing contact or create it
$contactFilter = array($contactField => $contact);
$listContact = Contact::search($contactFilter);
if ($listContact) {
$aContact = array_shift($listContact);
$oContact = new Contact($aContact['contact_id']);
return $oContact;
}
return false;
}
public static function search($aFilter = array(), $full = false)
{
$aContact = array();
$from_str = self::$table." c LEFT JOIN ".self::$table_link." l ON c.contact_id=l.contact_id";
$aWhere = array();
foreach ($aFilter as $search_field => $search_value) {
switch ($search_field) {
case 'group_id':
$aWhere[] = "l.$search_field = $search_value";
break;
case 'contact_id':
$aWhere[] = "c.$search_field = $search_value";
break;
case 'phone':
$aWhere[] = "c.$search_field LIKE '%$search_value'";
break;
case 'email':
$aWhere[] = "c.$search_field = '$search_value'";
break;
case 'first_name':
case 'last_name':
$aWhere[] = "c.$search_field LIKE '%$search_value%'";
break;
case 'user_id':
case 'created_by':
$aWhere[] = "c.created_by = '$search_value'";
break;
case 'before':
$aWhere[] = "c.date_created <= $search_value";
break;
case 'after':
$aWhere[] = "c.date_created >= $search_value";
break;
}
}
if (!empty($aWhere)) {
$from_str .= ' WHERE ' . implode(' AND ', $aWhere);
}
if ($full) {
$query = "SELECT c.contact_id, c.first_name, c.last_name, c.phone, c.email, c.address, "
."c.custom1, c.custom2, c.custom3, c.description FROM " . $from_str;
} else {
$query = "SELECT c.contact_id, c.first_name, c.last_name, c.phone, c.email FROM " . $from_str;
}
Corelog::log("contact search with $query", Corelog::DEBUG, array('aFilter' => $aFilter));
$result = DB::query('contact', $query);
while ($data = mysqli_fetch_assoc($result)) {
$aContact[] = $data;
}
// if no contact found, check for special contacts
if (empty($aContact) && isset($aFilter['contact_id']) && $aFilter['contact_id'] == Contact::COMPANY) {
$oContact = new Contact($aFilter['contact_id']);
$singleContact['contact_id'] = $oContact->contact_id;
$singleContact['first_name'] = $oContact->first_name;
$singleContact['last_name'] = $oContact->last_name;
$singleContact['phone'] = $oContact->phone;
$singleContact['email'] = $oContact->email;
if ($full) {
$singleContact['address'] = '';
$singleContact['custom1'] = '';
$singleContact['custom2'] = '';
$singleContact['custom3'] = '';
$singleContact['descritpion'] = '';
}
$aContact = $singleContact;
}
return $aContact;
}
private function load()
{
$query = "SELECT * FROM " . self::$table . " WHERE contact_id='%contact_id%' ";
$result = DB::query(self::$table, $query, array('contact_id' => $this->contact_id));
$data = mysqli_fetch_assoc($result);
if ($data) {
$this->contact_id = $data['contact_id'];
$this->first_name = $data['first_name'];
$this->last_name = $data['last_name'];
$this->phone = $data['phone'];
$this->email = $data['email'];
$this->address = $data['address'];
$this->custom1 = $data['custom1'];
$this->custom2 = $data['custom2'];
$this->custom3 = $data['custom3'];
$this->description = $data['description'];
$this->user_id = $data['created_by'];
Corelog::log("Contact loaded name: $this->first_name $this->last_name", Corelog::CRUD);
} else {
throw new CoreException('404', 'Contact not found');
}
}
public function delete()
{
Corelog::log("Contact delete", Corelog::CRUD);
DB::delete(self::$table_link, 'contact_id', $this->contact_id);
return DB::delete(self::$table, 'contact_id', $this->contact_id);
}
public function __isset($field)
{
$method_name = 'isset_' . $field;
if (method_exists($this, $method_name)) {
return $this->$method_name();
} else {
return isset($this->$field);
}
}
public function __get($field)
{
$method_name = 'get_' . $field;
if (method_exists($this, $method_name)) {
return $this->$method_name();
} else if (!empty($field) && isset($this->$field)) {
return $this->$field;
}
return NULL;
}
public function __set($field, $value)
{
$method_name = 'set_' . $field;
if (method_exists($this, $method_name)) {
$this->$method_name($value);
} else if (empty($field) || in_array($field, self::$read_only)) {
return;
} else {
$this->$field = $value;
}
}
public function get_id()
{
return $this->contact_id;
}
public function email_to_phone()
{
$aEmail = imap_rfc822_parse_adrlist($this->email, Conf::get('sendmail:domain', 'localhost'));
$strPhone = $aEmail[0]->mailbox; // we are only interested in 1st (0) part of aEmail list
$this->phone = preg_replace("/[^0-9]/", "", $strPhone); // keep only digits
return $this->phone;
}
public function phone_to_email()
{
$strEmail = $this->phone . '@' . Conf::get('sendmail:domain', 'localhost');
$this->email = $strEmail;
return $this->email;
}
public function save()
{
$data = array(
'contact_id' => $this->contact_id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'phone' => $this->phone,
'email' => $this->email,
'address' => $this->address,
'custom1' => $this->custom1,
'custom2' => $this->custom2,
'custom3' => $this->custom3,
'description' => $this->description
);
if (isset($data['contact_id']) && !empty($data['contact_id'])) {
// update existing record
$result = DB::update(self::$table, $data, 'contact_id');
Corelog::log("Contact updated: $this->contact_id", Corelog::CRUD);
} else {
// add new
$result = DB::update(self::$table, $data, false);
$this->contact_id = $data['contact_id'];
Corelog::log("New Contact created: $this->contact_id", Corelog::CRUD);
}
return $result;
}
public function link($group_id)
{
// add new
$link = array(
'contact_id' => $this->contact_id,
'group_id' => $group_id
);
return DB::update(self::$table_link, $link);
}
public function link_delete($group_id = null)
{
if ($group_id == null) {
$link_delete_query = "DELECT FROM ".self::$table_link." WHERE contact_id=%contact_id%";
} else {
$link_delete_query = "DELETE FROM ".self::$table_link." WHERE contact_id=%contact_id% AND group_id=%group_id%";
}
DB::query(self::$table, $req_query, array('contact_id' => $this->contact_id, 'group_id' => $group_id));
$get_link_count = mysqli_query("SELECT * from contact_link");
$result_add = mysqli_query("DELETE from contact_link where contact_id=".$this->contact_id." AND group_id=".$group_id);
$result = mysqli_num_rows($get_link_count)-1;
//$count_contact = mysql_query("SELECT * from contact_link where group_id=".$group_id." GROUP BY contact_id");
//$cont_result = mysql_num_rows($count_contact);
//$udate_group = mysql_query("UPDATE contact_group set contact_count=".$cont_result." where group_id=".$group_id);
Corelog::log("group contacts Deleted: ", Corelog::CRUD);
return $result ;
}
}