Files
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

77 lines
2.1 KiB
PHP

<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Carpool_friend_model extends CI_Model {
private $read_replica;
public function __construct()
{
parent::__construct();
$this->read_replica = $this->load->database('savvy_replica', TRUE);
}
public function get_carpool_friend_records(
$filters = [],
$limit = null,
$offset = null
) {
$combo_array = [
'proc_status' => 'cf.status',
'status' => 'cf.status'
];
$numeric_array = [
'member_id' => 'cp.member_id',
'carpool_id' => 'cf.carpool_id'
];
$this->read_replica->select([
'(CASE
WHEN cf.status = 1 THEN \'Pending\'
WHEN cf.status = 4 THEN \'Accepted\'
ELSE cf.status::text END
) AS proc_status',
'\'<a href =/member/viewLocateMember?member_id=\'||m.id||\'>\'||m.firstname||\'</a>\' AS Member',
'm.id AS member_id',
'cf.*'
]);
$this->read_replica->from('members_carpool_friends cf');
$this->read_replica->join('members_carpool cp', 'cp.id=cf.carpool_id', 'left');
$this->read_replica->join('members m', 'm.id = cp.member_id', 'left');
foreach($filters as $key => $val) {
if (array_key_exists($key, $combo_array)) {
$this->read_replica->where($combo_array[$key], $val);
} else if (array_key_exists($key, $numeric_array)) {
$this->read_replica->where($key, $val);
} else if (strpos($key, 'from') !== FALSE) {
$this->read_replica->where('DATE(cf.added) >=', $val);
} else if (strpos($key, 'to') !== FALSE) {
$this->read_replica->where('DATE(cf.added) <=', $val);
} else {
$this->read_replica->like('lower(cf.' . $key . ')', strtolower($val));
}
}
if ($limit) {
$this->read_replica->limit($limit, $offset);
}
return $this->read_replica->get()->result_array();
}
}