MaketRandevu/application/models/Customers_model.php

520 lines
15 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @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
* @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.
*
* @package Models
*/
class Customers_model extends EA_Model {
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'id_roles' => 'integer',
];
/**
* @var array
*/
protected $api_resource = [
'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',
'notes' => 'notes',
];
/**
2021-10-27 11:04:15 +03:00
* Save (insert or update) a customer.
*
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.
*
2021-10-27 11:04:15 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:15 +03:00
public function save(array $customer): int
{
$this->validate($customer);
2021-10-27 11:04:15 +03:00
if ($this->exists($customer) && empty($customer['id']))
{
$customer['id'] = $this->find_record_id($customer);
}
2021-10-27 11:04:15 +03:00
if (empty($customer['id']))
{
2021-10-27 11:04:15 +03:00
return $this->insert($customer);
2018-01-23 12:08:37 +03:00
}
else
{
2021-10-27 11:04:15 +03:00
return $this->update($customer);
}
}
/**
2021-10-27 11:04:15 +03:00
* Validate the customer data.
*
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
*/
2021-10-27 11:49:42 +03:00
public function validate(array $customer)
{
2021-10-27 11:04:15 +03:00
// If a customer ID is provided then check whether the record really exists in the database.
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
2021-10-27 11:04:15 +03:00
if ( ! $count)
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('The provided customer ID does not exist in the database: ' . $customer['id']);
}
}
2021-10-27 11:04:15 +03:00
// Make sure all required fields are provided.
$phone_number_required = filter_var(setting('require_phone_number'), FILTER_VALIDATE_BOOLEAN);
2021-10-27 11:04:15 +03:00
if (
empty($customer['first_name'])
|| empty($customer['last_name'])
|| empty($customer['email'])
|| (empty($customer['phone_number']) && $phone_number_required)
)
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($customer, TRUE));
}
2021-10-27 11:04:15 +03:00
// Validate the email address.
if ( ! filter_var($customer['email'], FILTER_VALIDATE_EMAIL))
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('Invalid email address provided: ' . $customer['email']);
}
2021-10-27 11:04:15 +03:00
// Make sure the email address is unique.
$customer_id = $customer['id'] ?? NULL;
2021-10-27 11:04:15 +03:00
$count = $this
->db
->select()
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_CUSTOMER)
->where('users.email', $customer['email'])
2020-12-05 12:38:57 +03:00
->where('users.id !=', $customer_id)
->get()
->num_rows();
2021-10-27 11:04:15 +03:00
if ($count > 0)
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('The provided email address is already in use, please use a different one.');
}
}
/**
2021-10-27 11:04:15 +03:00
* Insert a new customer into the database.
*
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
*/
2021-10-27 11:04:15 +03:00
protected function insert(array $customer): int
{
2021-10-27 11:04:15 +03:00
$customer['id_roles'] = $this->get_customer_role_id();
if ( ! $this->db->insert('users', $customer))
{
2021-10-27 11:04:15 +03:00
throw new RuntimeException('Could not insert customer.');
}
2021-10-27 11:04:15 +03:00
return $this->db->insert_id();
}
/**
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.
*
2021-10-27 11:04:15 +03:00
* @return int Returns the customer ID.
*
2021-10-27 11:04:15 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:15 +03:00
protected function update(array $customer): int
{
2021-10-27 11:04:15 +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.');
}
2021-10-27 11:04:15 +03:00
return $customer['id'];
}
2021-10-27 11:04:15 +03:00
/**
* Remove an existing customer from the database.
*
* @param int $customer_id Customer ID.
*
* @throws RuntimeException
*/
2021-10-27 11:49:42 +03:00
public function delete(int $customer_id)
2021-10-27 11:04:15 +03:00
{
if ( ! $this->db->delete('users', ['id' => $customer_id]))
{
2021-10-27 11:04:15 +03:00
throw new RuntimeException('Could not delete customer.');
}
}
/**
2021-10-27 11:04:15 +03:00
* Get a specific customer from the database.
*
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.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:15 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:15 +03:00
public function find(int $customer_id): array
{
2021-10-27 11:04:15 +03:00
if ( ! $this->db->get_where('users', ['id' => $customer_id])->num_rows())
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('The provided customer ID was not found in the database: ' . $customer_id);
}
$customer = $this->db->get_where('users', ['id' => $customer_id])->row_array();
$this->cast($customer);
return $customer;
}
/**
2021-10-27 11:04:15 +03:00
* Get a specific field value from the database.
*
2021-10-27 11:04:15 +03:00
* @param int $customer_id Customer ID.
* @param string $field Name of the value to be returned.
*
2021-10-27 11:04:15 +03:00
* @return string Returns the selected customer value from the database.
*
2021-10-27 11:04:15 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:15 +03:00
public function value(int $customer_id, string $field): string
{
2021-10-27 11:04:15 +03:00
if (empty($field))
{
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
if (empty($customer_id))
{
throw new InvalidArgumentException('The customer ID argument cannot be empty.');
}
// Check whether the customer exists.
$query = $this->db->get_where('users', ['id' => $customer_id]);
if ( ! $query->num_rows())
{
throw new InvalidArgumentException('The provided customer ID was not found in the database: ' . $customer_id);
}
// Check if the required field is part of the customer data.
$customer = $query->row_array();
$this->cast($customer);
2021-10-27 11:04:15 +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);
}
2021-10-27 11:04:15 +03:00
return $customer[$field];
}
/**
2021-10-27 11:04:15 +03:00
* Get all customers that match the provided criteria.
*
2021-10-27 11:04:15 +03:00
* @param array|string $where Where conditions.
* @param int|null $limit Record limit.
* @param int|null $offset Record offset.
* @param string|null $order_by Order by.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:15 +03:00
* @return array Returns an array of customers.
*/
2021-10-27 11:04:15 +03:00
public function get($where = NULL, 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();
if ($where !== NULL)
{
2021-10-27 11:04:15 +03:00
$this->db->where($where);
}
2021-10-27 11:04:15 +03:00
if ($order_by !== NULL)
{
2021-10-27 11:04:15 +03:00
$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;
}
/**
2021-10-27 11:04:15 +03:00
* Get the customer role ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:15 +03:00
* @return int Returns the role ID.
*/
2021-10-27 11:04:15 +03:00
public function get_customer_role_id(): int
{
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_CUSTOMER])->row_array();
2021-10-27 11:04:15 +03:00
if (empty($role))
{
throw new RuntimeException('The customer role was not found in the database.');
}
2021-10-27 11:04:15 +03:00
return $role['id'];
}
/**
2021-10-27 11:04:15 +03:00
* Check if a particular customer record already exists in the database.
*
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 bool Returns whether there is a record matching the provided one or not.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:15 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:15 +03:00
public function exists(array $customer): bool
{
2021-10-27 11:04:15 +03:00
if (empty($customer['email']))
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('The customer email was not provided: ' . print_r($customer, TRUE));
}
2021-10-27 11:04:15 +03:00
$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();
2021-10-27 11:04:15 +03:00
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']))
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('The customer email was not provided: ' . print_r($customer, TRUE));
}
2021-10-27 11:04:15 +03:00
$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();
2020-12-05 12:38:57 +03:00
2021-10-27 11:04:15 +03:00
if (empty($customer))
{
2021-10-27 11:04:15 +03:00
throw new InvalidArgumentException('Could not find customer record id.');
}
2021-10-27 11:04:15 +03:00
return $customer['id'];
}
/**
2021-10-27 11:04:15 +03:00
* Get the query builder interface, configured for use with the users (customer-filtered) table.
*
2021-10-27 11:04:15 +03:00
* @return CI_DB_query_builder
*/
2021-10-27 11:04:15 +03:00
public function query(): CI_DB_query_builder
{
2021-10-27 11:04:15 +03:00
$role_id = $this->get_customer_role_id();
2021-10-27 11:04:15 +03:00
return $this->db->from('users')->where('id_roles', $role_id);
}
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.
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
$role_id = $this->get_customer_role_id();
$customers = $this
2021-10-27 11:04:15 +03:00
->db
->select()
->from('users')
->where('id_roles', $role_id)
->group_start()
2021-10-27 11:04:15 +03:00
->like('first_name', $keyword)
->or_like('last_name', $keyword)
->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)
->group_end()
2021-10-27 11:04:15 +03:00
->limit($limit)
->offset($offset)
->order_by($order_by)
->get()
->result_array();
foreach ($customers as $customer)
{
$this->cast($customer);
}
return $customers;
}
/**
* 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.
*
2021-10-27 11:04:15 +03:00
* @throws InvalidArgumentException
*/
public function load(array &$customer, array $resources)
{
2021-10-27 11:04:15 +03:00
// Customers do not currently have any related resources.
}
/**
* Convert the database customer record to the equivalent API resource.
*
* @param array $customer Customer data.
*/
public function api_encode(array &$customer)
{
$encoded_resource = [
'id' => array_key_exists('id', $customer) ? (int)$customer['id'] : NULL,
'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'],
'notes' => $customer['notes']
];
$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).
*/
public function api_decode(array &$customer, array $base = NULL)
{
$decoded_resource = $base ?: [];
if (array_key_exists('id', $customer))
{
$decoded_resource['id'] = $customer['id'];
}
if (array_key_exists('firstName', $customer))
{
$decoded_resource['first_name'] = $customer['firstName'];
}
if (array_key_exists('lastName', $customer))
{
$decoded_resource['last_name'] = $customer['lastName'];
}
if (array_key_exists('email', $customer))
{
$decoded_resource['email'] = $customer['email'];
}
if (array_key_exists('phone', $customer))
{
$decoded_resource['phone_number'] = $customer['phone'];
}
if (array_key_exists('address', $customer))
{
$decoded_resource['address'] = $customer['address'];
}
if (array_key_exists('city', $customer))
{
$decoded_resource['city'] = $customer['city'];
}
if (array_key_exists('zip', $customer))
{
$decoded_resource['zip_code'] = $customer['zip'];
}
if (array_key_exists('notes', $customer))
{
$decoded_resource['notes'] = $customer['notes'];
}
$customer = $decoded_resource;
}
}