[go: up one dir, main page]

Menu

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

Download this file

456 lines (408 with data), 14.0 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?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 Account
{
/** @const */
const USER_DEFAULT = -1;
const COMPANY = -2;
const ANONYMOUS = -3;
protected static $table = 'account';
protected static $primary_key = 'account_id';
protected static $fields = array(
'account_id',
'type',
'username',
'passwd',
'passwd_pin',
'first_name',
'last_name',
'phone',
'email',
'linkdid_id',
'address',
'settings',
'active',
'user_id'
);
protected static $read_only = array(
'account_id',
'type',
'user_id'
);
/**
* @property-read integer $account_id
* @var integer
*/
public $account_id = NULL;
/**
* @property-read string $type
* @var string
*/
protected $type = 'account';
/**
* @property string $username
* @see void function Account::set_username()
* @var string
*/
public $username = NULL;
/** @var string */
public $passwd = NULL;
/** @var string */
public $passwd_pin = NULL;
/** @var string */
public $first_name = NULL;
/** @var string */
public $last_name = NULL;
/** @var string */
public $phone = NULL;
/** @var string */
public $email = NULL;
/** @var integer */
public $linkdid_id = NULL;
/** @var string */
public $address = NULL;
/** @var array */
public $settings = array();
/** @var integer */
public $active = 0;
/**
* @property-read integer $user_id
* @see void function Account::associate()
* @var integer
*/
public $user_id = NULL;
public function capabilities()
{
return (Transmission::INBOUND | Transmission::OUTBOUND);
}
public function __construct($account_id = NULL)
{
if (!empty($account_id)) {
$this->account_id = $account_id;
if (Account::ANONYMOUS == $account_id) {
Corelog::log("Anonymous account: creating instance", Corelog::CRUD);
$this->account_id = $account_id;
$this->first_name = 'Anonymous';
$this->last_name = 'User';
$this->email = 'anonymous@unknown.com';
$this->phone = '0000000000';
$this->address = 'Unknown';
return $account_id; // don't proceed further
} else if (Account::COMPANY == $account_id) {
Corelog::log("Company account: creating instance", Corelog::CRUD);
$this->account_id = $account_id;
$title = Conf::get('company:name', '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');
return $account_id; // don't proceed further
} else if (Account::USER_DEFAULT == $account_id) {
Corelog::log("Default account: creating instance", Corelog::CRUD);
$oSession = Session::get_instance();
$query = "SELECT account_id FROM " . self::$table . " WHERE active=1 AND created_by=%user_id%
ORDER BY account_id DESC LIMIT 1";
$result = DB::query(self::$table, $query, array('user_id' => $oSession->user->user_id));
$data = mysqli_fetch_assoc($result);
$this->account_id = $data['account_id'];
}
$this->_load();
}
}
public static function construct_from_array($aAccount)
{
$oAccount = new Account();
foreach ($aAccount as $field => $value) {
$oAccount->$field = $value;
}
return $oAccount;
}
public static function locate($search_value, $contactField = 'phone')
{
// locate an existing account
$accountFilter = array($contactField => $search_value);
$listAccount = static::search($accountFilter);
if ($listAccount) {
$aAccount = array_shift($listAccount);
return Account::load($aAccount['account_id']);
}
return false; // no account found
}
public static function search($aFilter = array())
{
$aAccount = array();
$from_str = self::$table;
$aWhere = array();
foreach ($aFilter as $search_field => $search_value) {
switch ($search_field) {
case 'account_id':
case 'linkdid_id':
$aWhere[] = "$search_field = $search_value";
break;
case 'type':
case 'username':
case 'phone':
case 'email':
case 'passwd':
case 'passwd_pin':
case 'first_name':
case 'last_name':
$aWhere[] = "$search_field LIKE '%$search_value%'";
break;
case 'user_id':
case 'created_by':
$aWhere[] = "created_by = '$search_value'";
break;
case 'before':
$aWhere[] = "date_created <= $search_value";
break;
case 'after':
$aWhere[] = "date_created >= $search_value";
break;
}
}
if (!empty($aWhere)) {
$from_str .= ' WHERE ' . implode(' AND ', $aWhere);
}
$query = "SELECT account_id, type, username, first_name, last_name, phone, email, created_by FROM " . $from_str;
Corelog::log("account search with $query", Corelog::DEBUG, array('aFilter' => $aFilter));
$result = DB::query('account', $query);
while ($data = mysqli_fetch_assoc($result)) {
$aAccount[] = $data;
}
// if no account found, check for special accounts
$special_accounts = array(Account::USER_DEFAULT, Account::COMPANY, Account::ANONYMOUS);
if (empty($aAccount) && isset($aFilter['account_id']) && in_array($aFilter['account_id'], $special_accounts)) {
$oAccount = new Account($aFilter['account_id']);
$aAccount[$oAccount->account_id] = array(
'account_id' => $oAccount->account_id,
'type' => 'account',
'username' => $oAccount->username,
'first_name' => $oAccount->first_name,
'last_name' => $oAccount->last_name,
'phone' => $oAccount->phone,
'email' => $oAccount->email
);
}
return $aAccount;
}
public static function getClass(&$account_id, $namespace = 'ICT\\Core\\Account')
{
if (ctype_digit(trim($account_id))) {
$query = "SELECT type FROM " . self::$table . " WHERE account_id='%account_id%' ";
$result = DB::query(self::$table, $query, array('account_id' => $account_id));
if($row = $result->fetch_row()) {
$account_type = $row[0];
}
} else {
$account_type = $account_id;
$account_id = null;
}
$class_name = ucfirst(strtolower(trim($account_type)));
if (!empty($namespace)) {
$class_name = $namespace . '\\' . $class_name;
}
if (class_exists($class_name, true)) {
return $class_name;
} else {
return false;
}
}
public static function load($account_id)
{
if ($account_id < 0) {
return new self($account_id);
}
$class_name = self::getClass($account_id);
if ($class_name) {
Corelog::log("Creating instance of : $class_name for account: $account_id", Corelog::CRUD);
return new $class_name($account_id);
} else {
Corelog::log("$class_name class not found, Creating instance of : Account", Corelog::CRUD);
return new self($account_id);
}
}
protected function _load()
{
Corelog::log("Loading account: $this->account_id", Corelog::CRUD);
$query = "SELECT * FROM " . self::$table . " WHERE account_id='%account_id%' ";
$result = DB::query(self::$table, $query, array('account_id' => $this->account_id));
$data = mysqli_fetch_assoc($result);
if ($data) {
$this->account_id = $data['account_id'];
$this->type = $data['type'];
$this->username = $data['username'];
$this->passwd = $data['passwd'];
$this->passwd_pin = $data['passwd_pin'];
$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->settings = json_decode($data['settings'], true);
$this->active = $data['active'];
$this->user_id = $data['created_by'];
if (!is_array($this->settings)) {
$this->settings = array();
}
} else {
throw new CoreException('404', 'Account not found');
}
}
public function delete()
{
Corelog::log("Deleting account: $this->account_id", Corelog::CRUD);
// also delete all installed program
$this->remove_program('all');
// now delete account
return DB::delete(self::$table, 'account_id', $this->account_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->account_id;
}
protected function set_username($username)
{
if (empty($this->username)) {
$this->username = $username;
}
}
public function save()
{
$data = array(
'account_id' => $this->account_id,
'type' => $this->type,
'username' => $this->username,
'passwd' => $this->passwd,
'passwd_pin' => $this->passwd_pin,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'phone' => $this->phone,
'email' => $this->email,
'linkdid_id' => $this->linkdid_id,
'address' => $this->address,
'settings' => json_encode($this->settings, JSON_NUMERIC_CHECK),
'active' => $this->active
// Note: user_id or created_by field can't be updated here, instead use associate method
);
if (isset($data['account_id']) && !empty($data['account_id'])) {
// update existing record
$result = DB::update(self::$table, $data, 'account_id');
Corelog::log("Account updated: $this->account_id", Corelog::CRUD);
} else {
// add new
$result = DB::update(self::$table, $data, false);
$this->account_id = $data['account_id'];
Corelog::log("New account created: $this->account_id", Corelog::CRUD);
}
return $result;
}
/**
* Associate / assign current account to some user
*/
public function associate($user_id, $aUser = array())
{
Corelog::log("Changing account owner for: $this->account_id from: $this->user_id to: $user_id", Corelog::CRUD);
// we can change created_by field only via direct query
$this->user_id = $user_id; // also update the internal class variable
$query = "UPDATE " . self::$table . " SET created_by=%user_id% WHERE account_id=%account_id%";
$result = DB::query(self::$table, $query, array('user_id' => $user_id, 'account_id' => $this->account_id));
if ($result && !empty($aUser) && is_array($aUser)) {
foreach ($aUser as $field => $value) {
$this->__set($field, $value); // set function will do necessary validation
}
$this->save();
}
return true;
}
public function dissociate()
{
// first remove all associated programs
$this->remove_program('all');
$this->associate('NULL');
return true;
}
/**
* Deploy given program with current account
* ( only if given program support it )
* @param \ICT\Core\Program $oProgram
* @return int $program_id
*/
public function install_program(Program $oProgram)
{
Corelog::log("Program installation for: $this->account_id Program: $oProgram->name", Corelog::CRUD);
$oToken = new Token();
$oToken->add('account', $this);
$aParameter = $oProgram->parameter_save();
foreach ($aParameter as $parameter_name => $parameter_value) {
$oProgram->{$parameter_name} = $oToken->render_variable($parameter_value, Token::KEEP_ORIGNAL);
}
$oProgram->save();
$oProgram->deploy();
return $oProgram->program_id;
}
public function remove_program($program_name = 'all')
{
Corelog::log("Removing program from: $this->account_id Program: $program_name", Corelog::CRUD);
$listProgram = Program::resource_search(array($this->type, 'account'), $this->account_id);
if (!empty($listProgram)) { // no error / false
foreach ($listProgram as $aProgram) {
$program_id = $aProgram['program_id'];
if (ctype_digit($program_name) && $program_name == $program_id) {
$oProgram = Program::load($program_id);
$oProgram->delete();
} else {
$oProgram = Program::load($program_id);
if (empty($program_name) || 'all' == strtolower($program_name) || strtolower($program_name) == $oProgram->name) {
$oProgram->delete();
}
}
}
}
return true;
}
public function setting_read($name, $default = null) {
if (isset($this->settings[$name])) {
return $this->settings[$name];
}
return $default;
}
}