2020-04-22 22:48:56 +03:00
|
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2023-12-31 21:46:27 +03:00
|
|
|
|
* IFLRandevu - İzmir Fen Lisesi Randevu Portalı
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
|
|
|
|
* @package EasyAppointments
|
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
|
* @link https://easyappointments.org
|
2016-01-09 23:29:28 +02:00
|
|
|
|
* @since v1.0.0
|
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
|
* Customers model.
|
2021-10-27 11:04:15 +03:00
|
|
|
|
*
|
|
|
|
|
* Handles all the database operations of the customer resource.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
|
|
|
|
* @package Models
|
|
|
|
|
*/
|
2023-12-11 11:54:29 +03:00
|
|
|
|
class Customers_model extends EA_Model
|
|
|
|
|
{
|
2021-10-29 13:38:56 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $casts = [
|
2021-10-29 13:38:56 +03:00
|
|
|
|
'id' => 'integer',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'id_roles' => 'integer',
|
2021-10-29 13:38:56 +03:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-06 17:02:40 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $api_resource = [
|
2021-11-06 17:02:40 +03:00
|
|
|
|
'id' => 'id',
|
|
|
|
|
'firstName' => 'first_name',
|
|
|
|
|
'lastName' => 'last_name',
|
|
|
|
|
'email' => 'email',
|
|
|
|
|
'phone' => 'phone_number',
|
|
|
|
|
'address' => 'address',
|
|
|
|
|
'city' => 'city',
|
|
|
|
|
'state' => 'state',
|
|
|
|
|
'zip' => 'zip_code',
|
|
|
|
|
'timezone' => 'timezone',
|
|
|
|
|
'language' => 'language',
|
2023-12-11 11:54:29 +03:00
|
|
|
|
'customField1' => 'custom_field_1',
|
|
|
|
|
'customField2' => 'custom_field_2',
|
|
|
|
|
'customField3' => 'custom_field_3',
|
|
|
|
|
'customField4' => 'custom_field_4',
|
|
|
|
|
'customField5' => 'custom_field_5',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'notes' => 'notes',
|
2021-11-06 17:02:40 +03:00
|
|
|
|
];
|
|
|
|
|
|
2020-11-12 15:51:10 +03:00
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Save (insert or update) a customer.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @param array $customer Associative array with the customer data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @return int Returns the customer ID.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:15 +03:00
|
|
|
|
public function save(array $customer): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
|
$this->validate($customer);
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if ($this->exists($customer) && empty($customer['id'])) {
|
2017-09-15 14:36:37 +03:00
|
|
|
|
$customer['id'] = $this->find_record_id($customer);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (empty($customer['id'])) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
return $this->insert($customer);
|
2023-12-11 11:54:29 +03:00
|
|
|
|
} else {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
return $this->update($customer);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Validate the customer data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @param array $customer Associative array with the customer data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
|
public function validate(array $customer)
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:04:15 +03:00
|
|
|
|
// If a customer ID is provided then check whether the record really exists in the database.
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!empty($customer['id'])) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
$count = $this->db->get_where('users', ['id' => $customer['id']])->num_rows();
|
2020-12-05 12:38:57 +03:00
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!$count) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided customer ID does not exist in the database: ' . $customer['id'],
|
2023-12-11 11:54:29 +03:00
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
// Make sure all required fields are provided.
|
2021-12-20 11:38:51 +03:00
|
|
|
|
$require_first_name = filter_var(setting('require_phone_number'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
$require_last_name = filter_var(setting('require_last'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
$require_email = filter_var(setting('require_email'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
$require_phone_number = filter_var(setting('require_phone_number'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
$require_address = filter_var(setting('require_address'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
$require_city = filter_var(setting('require_city'), FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
$require_zip_code = filter_var(setting('require_zip_code'), FILTER_VALIDATE_BOOLEAN);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
if (
|
2023-12-11 11:54:29 +03:00
|
|
|
|
(empty($customer['first_name']) && $require_first_name) ||
|
|
|
|
|
(empty($customer['last_name']) && $require_last_name) ||
|
|
|
|
|
(empty($customer['email']) && $require_email) ||
|
|
|
|
|
(empty($customer['phone_number']) && $require_phone_number) ||
|
|
|
|
|
(empty($customer['address']) && $require_address) ||
|
|
|
|
|
(empty($customer['city']) && $require_city) ||
|
|
|
|
|
(empty($customer['zip_code']) && $require_zip_code)
|
|
|
|
|
) {
|
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($customer, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($customer['email'])) {
|
2021-12-20 11:38:51 +03:00
|
|
|
|
// Validate the email address.
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) {
|
2021-12-20 11:38:51 +03:00
|
|
|
|
throw new InvalidArgumentException('Invalid email address provided: ' . $customer['email']);
|
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2021-12-20 11:38:51 +03:00
|
|
|
|
// Make sure the email address is unique.
|
2023-12-11 11:54:29 +03:00
|
|
|
|
$customer_id = $customer['id'] ?? null;
|
2021-12-20 11:38:51 +03:00
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
$count = $this->db
|
2021-12-20 11:38:51 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('users')
|
|
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
|
|
|
|
->where('roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
|
->where('users.email', $customer['email'])
|
|
|
|
|
->where('users.id !=', $customer_id)
|
|
|
|
|
->get()
|
|
|
|
|
->num_rows();
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if ($count > 0) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided email address is already in use, please use a different one.',
|
2023-12-11 11:54:29 +03:00
|
|
|
|
);
|
2021-12-20 11:38:51 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get all customers that match the provided criteria.
|
|
|
|
|
*
|
|
|
|
|
* @param array|string|null $where Where conditions.
|
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array of customers.
|
|
|
|
|
*/
|
|
|
|
|
public function get(
|
|
|
|
|
array|string $where = null,
|
|
|
|
|
int $limit = null,
|
|
|
|
|
int $offset = null,
|
2023-12-22 13:35:41 +03:00
|
|
|
|
string $order_by = null,
|
2023-12-11 11:54:29 +03:00
|
|
|
|
): array {
|
|
|
|
|
$role_id = $this->get_customer_role_id();
|
|
|
|
|
|
|
|
|
|
if ($where !== null) {
|
|
|
|
|
$this->db->where($where);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customers = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
|
|
foreach ($customers as &$customer) {
|
|
|
|
|
$this->cast($customer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $customers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the customer role ID.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the role ID.
|
|
|
|
|
*/
|
|
|
|
|
public function get_customer_role_id(): int
|
|
|
|
|
{
|
|
|
|
|
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_CUSTOMER])->row_array();
|
|
|
|
|
|
|
|
|
|
if (empty($role)) {
|
|
|
|
|
throw new RuntimeException('The customer role was not found in the database.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $role['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a particular customer record already exists in the database.
|
|
|
|
|
*
|
|
|
|
|
* @param array $customer Associative array with the customer data.
|
|
|
|
|
*
|
|
|
|
|
* @return bool Returns whether there is a record matching the provided one or not.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public function exists(array $customer): bool
|
|
|
|
|
{
|
|
|
|
|
if (empty($customer['email'])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$count = $this->db
|
|
|
|
|
->select()
|
|
|
|
|
->from('users')
|
|
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
|
|
|
|
->where('users.email', $customer['email'])
|
|
|
|
|
->where('roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
|
->get()
|
|
|
|
|
->num_rows();
|
|
|
|
|
|
|
|
|
|
return $count > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find the record ID of a customer.
|
|
|
|
|
*
|
|
|
|
|
* @param array $customer Associative array with the customer data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the ID of the record that matches the provided argument.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public function find_record_id(array $customer): int
|
|
|
|
|
{
|
|
|
|
|
if (empty($customer['email'])) {
|
|
|
|
|
throw new InvalidArgumentException('The customer email was not provided: ' . print_r($customer, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customer = $this->db
|
|
|
|
|
->select('users.id')
|
|
|
|
|
->from('users')
|
|
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
|
|
|
|
->where('users.email', $customer['email'])
|
|
|
|
|
->where('roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
|
->get()
|
|
|
|
|
->row_array();
|
|
|
|
|
|
|
|
|
|
if (empty($customer)) {
|
|
|
|
|
throw new InvalidArgumentException('Could not find customer record id.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (int) $customer['id'];
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Insert a new customer into the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @param array $customer Associative array with the customer data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @return int Returns the customer ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @throws RuntimeException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:15 +03:00
|
|
|
|
protected function insert(array $customer): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$customer['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
$customer['update_datetime'] = date('Y-m-d H:i:s');
|
2021-10-27 11:04:15 +03:00
|
|
|
|
$customer['id_roles'] = $this->get_customer_role_id();
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!$this->db->insert('users', $customer)) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
throw new RuntimeException('Could not insert customer.');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
return $this->db->insert_id();
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Update an existing customer.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @param array $customer Associative array with the customer data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @return int Returns the customer ID.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @throws RuntimeException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:15 +03:00
|
|
|
|
protected function update(array $customer): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$customer['update_datetime'] = date('Y-m-d H:i:s');
|
2022-05-10 15:28:51 +03:00
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!$this->db->update('users', $customer, ['id' => $customer['id']])) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
throw new RuntimeException('Could not update customer.');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
return $customer['id'];
|
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Remove an existing customer from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $customer_id Customer ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function delete(int $customer_id): void
|
2021-10-27 11:04:15 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
$this->db->delete('users', ['id' => $customer_id]);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Get a specific customer from the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @param int $customer_id The ID of the record to be returned.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @return array Returns an array with the customer data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function find(int $customer_id): array
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-29 13:38:56 +03:00
|
|
|
|
$customer = $this->db->get_where('users', ['id' => $customer_id])->row_array();
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!$customer) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided customer ID was not found in the database: ' . $customer_id,
|
2023-12-11 11:54:29 +03:00
|
|
|
|
);
|
2022-01-25 01:32:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 13:38:56 +03:00
|
|
|
|
$this->cast($customer);
|
|
|
|
|
|
|
|
|
|
return $customer;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Get a specific field value from the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @param int $customer_id Customer ID.
|
|
|
|
|
* @param string $field Name of the value to be returned.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
|
* @return mixed Returns the selected customer value from the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2020-04-22 22:48:56 +03:00
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
|
public function value(int $customer_id, string $field): mixed
|
2020-04-22 22:48:56 +03:00
|
|
|
|
{
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (empty($field)) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (empty($customer_id)) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
throw new InvalidArgumentException('The customer ID argument cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether the customer exists.
|
|
|
|
|
$query = $this->db->get_where('users', ['id' => $customer_id]);
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!$query->num_rows()) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided customer ID was not found in the database: ' . $customer_id,
|
2023-12-11 11:54:29 +03:00
|
|
|
|
);
|
2021-10-27 11:04:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the required field is part of the customer data.
|
|
|
|
|
$customer = $query->row_array();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
2021-10-29 13:38:56 +03:00
|
|
|
|
$this->cast($customer);
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (!array_key_exists($field, $customer)) {
|
2021-10-27 11:04:15 +03:00
|
|
|
|
throw new InvalidArgumentException('The requested field was not found in the customer data: ' . $field);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
return $customer[$field];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* Get the query builder interface, configured for use with the users (customer-filtered) table.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @return CI_DB_query_builder
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:15 +03:00
|
|
|
|
public function query(): CI_DB_query_builder
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:04:15 +03:00
|
|
|
|
$role_id = $this->get_customer_role_id();
|
2020-04-06 21:34:32 +03:00
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
return $this->db->from('users')->where('id_roles', $role_id);
|
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2021-10-27 11:04:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Search customers by the provided keyword.
|
|
|
|
|
*
|
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array of customers.
|
|
|
|
|
*/
|
2023-12-11 11:54:29 +03:00
|
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2021-10-27 11:04:15 +03:00
|
|
|
|
{
|
|
|
|
|
$role_id = $this->get_customer_role_id();
|
2021-10-29 13:38:56 +03:00
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
$customers = $this->db
|
2021-10-27 11:04:15 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('users')
|
|
|
|
|
->where('id_roles', $role_id)
|
2021-11-06 17:02:40 +03:00
|
|
|
|
->group_start()
|
2021-10-27 11:04:15 +03:00
|
|
|
|
->like('first_name', $keyword)
|
|
|
|
|
->or_like('last_name', $keyword)
|
2022-12-12 11:15:55 +03:00
|
|
|
|
->or_like('CONCAT_WS(" ", first_name, last_name)', $keyword)
|
2021-10-27 11:04:15 +03:00
|
|
|
|
->or_like('email', $keyword)
|
|
|
|
|
->or_like('phone_number', $keyword)
|
|
|
|
|
->or_like('mobile_number', $keyword)
|
|
|
|
|
->or_like('address', $keyword)
|
|
|
|
|
->or_like('city', $keyword)
|
|
|
|
|
->or_like('state', $keyword)
|
|
|
|
|
->or_like('zip_code', $keyword)
|
|
|
|
|
->or_like('notes', $keyword)
|
2021-11-06 17:02:40 +03:00
|
|
|
|
->group_end()
|
2021-10-27 11:04:15 +03:00
|
|
|
|
->limit($limit)
|
|
|
|
|
->offset($offset)
|
|
|
|
|
->order_by($order_by)
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
2021-10-29 13:38:56 +03:00
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
foreach ($customers as &$customer) {
|
2021-10-29 13:38:56 +03:00
|
|
|
|
$this->cast($customer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $customers;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
|
* Load related resources to a customer.
|
2021-10-27 11:04:15 +03:00
|
|
|
|
*
|
|
|
|
|
* @param array $customer Associative array with the customer data.
|
|
|
|
|
* @param array $resources Resource names to be attached.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:15 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
|
public function load(array &$customer, array $resources)
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Customers do not currently have any related resources.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
2021-11-05 11:32:34 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the database customer record to the equivalent API resource.
|
|
|
|
|
*
|
|
|
|
|
* @param array $customer Customer data.
|
|
|
|
|
*/
|
|
|
|
|
public function api_encode(array &$customer)
|
|
|
|
|
{
|
|
|
|
|
$encoded_resource = [
|
2023-12-11 11:54:29 +03:00
|
|
|
|
'id' => array_key_exists('id', $customer) ? (int) $customer['id'] : null,
|
2021-11-05 11:32:34 +03:00
|
|
|
|
'firstName' => $customer['first_name'],
|
|
|
|
|
'lastName' => $customer['last_name'],
|
|
|
|
|
'email' => $customer['email'],
|
|
|
|
|
'phone' => $customer['phone_number'],
|
|
|
|
|
'address' => $customer['address'],
|
|
|
|
|
'city' => $customer['city'],
|
|
|
|
|
'zip' => $customer['zip_code'],
|
2023-09-15 16:40:02 +03:00
|
|
|
|
'notes' => $customer['notes'],
|
|
|
|
|
'timezone' => $customer['timezone'],
|
2023-12-11 11:54:29 +03:00
|
|
|
|
'customField1' => $customer['custom_field_1'],
|
|
|
|
|
'customField2' => $customer['custom_field_2'],
|
|
|
|
|
'customField3' => $customer['custom_field_3'],
|
|
|
|
|
'customField4' => $customer['custom_field_4'],
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'customField5' => $customer['custom_field_5'],
|
2021-11-05 11:32:34 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$customer = $encoded_resource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the API resource to the equivalent database admin record.
|
|
|
|
|
*
|
|
|
|
|
* @param array $customer API resource.
|
|
|
|
|
* @param array|null $base Base customer data to be overwritten with the provided values (useful for updates).
|
|
|
|
|
*/
|
2023-12-11 11:54:29 +03:00
|
|
|
|
public function api_decode(array &$customer, array $base = null)
|
2021-11-05 11:32:34 +03:00
|
|
|
|
{
|
|
|
|
|
$decoded_resource = $base ?: [];
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('id', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['id'] = $customer['id'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('firstName', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['first_name'] = $customer['firstName'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('lastName', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['last_name'] = $customer['lastName'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('email', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['email'] = $customer['email'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('phone', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['phone_number'] = $customer['phone'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('address', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['address'] = $customer['address'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('city', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['city'] = $customer['city'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('zip', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['zip_code'] = $customer['zip'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 11:54:29 +03:00
|
|
|
|
if (array_key_exists('language', $customer)) {
|
|
|
|
|
$decoded_resource['language'] = $customer['language'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('customField1', $customer)) {
|
|
|
|
|
$decoded_resource['custom_field_1'] = $customer['customField1'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('customField2', $customer)) {
|
|
|
|
|
$decoded_resource['custom_field_2'] = $customer['customField2'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('customField3', $customer)) {
|
|
|
|
|
$decoded_resource['custom_field_3'] = $customer['customField3'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('customField4', $customer)) {
|
|
|
|
|
$decoded_resource['custom_field_4'] = $customer['customField4'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('customField5', $customer)) {
|
|
|
|
|
$decoded_resource['custom_field_5'] = $customer['customField5'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('notes', $customer)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
|
$decoded_resource['notes'] = $customer['notes'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customer = $decoded_resource;
|
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|