This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
lnapp/classes/lnApp/Model/Account.php

148 lines
3.5 KiB
PHP
Raw Normal View History

2014-09-29 12:06:38 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This Model manages both the accounts that users use to login to the system, as well as the account where services are owned.
*
* @package lnApp
* @category Models
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Model_Account extends Model_Auth_UserDefault {
// Relationships
protected $_has_many = array(
'email_log'=>array('far_key'=>'id'),
'group'=>array('through'=>'account_group'),
);
2016-08-31 11:51:58 +00:00
protected $_belongs_to = array(
'country'=>array(),
'currency'=>array(),
'language'=>array(),
2014-09-29 12:06:38 +00:00
);
protected $_display_filters = array(
2016-08-31 11:51:58 +00:00
'active'=>array(
array('StaticList_YesNo::get',array(':value',TRUE)),
),
2014-09-29 12:06:38 +00:00
'date_orig'=>array(
array('Site::Date',array(':value')),
),
'date_last'=>array(
array('Site::Date',array(':value')),
),
);
protected $_form = array('id'=>'id','value'=>'name()');
2014-09-29 12:06:38 +00:00
protected $_save_message = TRUE;
/** REQUIRED ABSTRACT METHODS **/
/** LOCAL METHODS **/
/**
* Our account number format
*/
2014-09-29 12:06:38 +00:00
public function activate_code() {
return md5(sprintf('%s-%s-%s-%s',$this->refnum(),$this->date_orig,$this->date_last,$this->email));
2014-09-29 12:06:38 +00:00
}
public function activated() {
2014-10-02 05:33:07 +00:00
return $this->verified;
2014-09-29 12:06:38 +00:00
}
/**
* Get the groups that an account belongs to
*/
public function groups() {
$result = array();
foreach ($this->group->where_active()->find_all() as $go)
foreach ($go->list_parentgrps(TRUE) as $cgo)
if (empty($result[$cgo->id]))
$result[$cgo->id] = $cgo;
return $result;
}
public function log($message) {
2014-10-02 05:33:07 +00:00
if (! class_exists('Model_Account_Log'))
return TRUE;
2014-09-29 12:06:38 +00:00
// Log a message for this account
$alo = ORM::factory('Account_Log');
$alo->account_id = $this->id;
$alo->ip = Request::$client_ip;
$alo->details = $message;
$alo->save();
return $alo->saved();
}
public function isAdmin() {
return FALSE;
}
/**
2016-08-31 11:51:58 +00:00
* Return an account name
2014-09-29 12:06:38 +00:00
*/
2016-08-31 11:51:58 +00:00
public function name($variable=NULL) {
return trim(sprintf('%s %s',$this->first_name,$this->last_name));
}
2014-09-29 12:06:38 +00:00
2016-08-31 11:51:58 +00:00
public function refnum($short=FALSE) {
return ($short ? '' : sprintf('%02s-',Site::id())).sprintf('%04s',$this->id);
2014-09-29 12:06:38 +00:00
}
2014-10-02 05:33:07 +00:00
/**
* Return a token valid for this user
*/
public function token($token_expire,$module,$method,$uses) {
return $this->id.':'.md5(sprintf('%s-%s',$this->refnum(),$this->date_last));
2014-10-02 05:33:07 +00:00
}
2014-09-29 12:06:38 +00:00
/**
* Search for accounts matching a term
*/
2016-08-02 05:40:23 +00:00
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=array()) {
if (! isset($options['parentbypass'])) {
$ao = Auth::instance()->get_user();
$this->clear();
$this->where_active();
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
list($fn,$ln) = explode(' ',$term,2);
$this->where_open()
->where('first_name','like','%'.$fn.'%')
->and_where('last_name','like','%'.$ln.'%')
->where_close();
} elseif (is_numeric($term)) {
$this->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$this->where('email','like','%'.$term.'%');
} else {
$this->where_open()
->or_where('first_name','like','%'.$term.'%')
->or_where('last_name','like','%'.$term.'%')
->or_where('email','like','%'.$term.'%')
->where_close();
}
// Restrict results to authorised accounts
// @todo
2014-09-29 12:06:38 +00:00
}
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
}
}
?>