iflrandevu/application/models/Providers_model.php

717 lines
22 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>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
/**
2021-10-27 11:04:21 +03:00
* Providers model
*
2021-10-27 11:04:21 +03:00
* Handles all the database operations of the provider resource.
*
* @package Models
*/
class Providers_model extends EA_Model {
/**
2021-10-27 11:04:21 +03:00
* Providers_model constructor.
*/
public function __construct()
{
parent::__construct();
2021-10-27 11:04:21 +03:00
$this->load->helper('password');
$this->load->helper('validation');
}
/**
2021-10-27 11:04:21 +03:00
* Save (insert or update) a provider.
*
2021-10-27 11:04:21 +03:00
* @param array $provider Associative array with the provider data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @return int Returns the provider ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:21 +03:00
public function save(array $provider): int
{
$this->validate($provider);
2021-10-27 11:04:21 +03:00
if (empty($provider['id']))
{
2021-10-27 11:04:21 +03:00
return $this->insert($provider);
2018-01-23 12:08:37 +03:00
}
else
{
2021-10-27 11:04:21 +03:00
return $this->update($provider);
}
}
/**
2021-10-27 11:04:21 +03:00
* Validate the provider data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @param array $provider Associative array with the provider data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function validate(array $provider)
{
2021-10-27 11:04:21 +03:00
// If a provider ID is provided then check whether the record really exists in the database.
if ( ! empty($provider['id']))
{
2021-10-27 11:04:21 +03:00
$count = $this->db->get_where('users', ['id' => $provider['id']])->num_rows();
if ( ! $count)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided provider ID does not exist in the database: ' . $provider['id']);
}
}
2021-10-27 11:04:21 +03:00
// Make sure all required fields are provided.
if (
empty($provider['first_name'])
|| empty($provider['last_name'])
|| empty($provider['email'])
|| empty($provider['phone_number'])
)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($provider, TRUE));
}
2021-10-27 11:04:21 +03:00
// Validate the email address.
if ( ! filter_var($provider['email'], FILTER_VALIDATE_EMAIL))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('Invalid email address provided: ' . $provider['email']);
}
// Validate provider services.
2021-10-27 11:04:21 +03:00
if (empty($provider['services']) || ! is_array($provider['services']))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided provider services are invalid: ' . print_r($provider, TRUE));
}
else
2020-12-05 12:38:57 +03:00
{
2021-10-27 11:04:21 +03:00
// Make sure the provided service entries are numeric values.
foreach ($provider['services'] as $service_id)
{
if ( ! is_numeric($service_id))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided provider services are invalid: ' . print_r($provider, TRUE));
}
}
}
2021-10-27 11:04:21 +03:00
// Make sure the username is unique.
if ( ! empty($provider['settings']['username']))
{
2021-10-27 11:04:21 +03:00
$provider_id = $provider['id'] ?? NULL;
2021-10-27 11:04:21 +03:00
if ( ! $this->validate_username($provider['settings']['username'], $provider_id))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided username is already in use, please use a different one.');
}
}
2021-10-27 11:04:21 +03:00
// Validate the password.
if ( ! empty($provider['settings']['password']))
{
if (strlen($provider['settings']['password']) < MIN_PASSWORD_LENGTH)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provider password must be at least ' . MIN_PASSWORD_LENGTH . ' characters long.');
}
}
2021-10-27 11:04:21 +03:00
// New users must always have a password value set.
if (empty($provider['id']) && empty($provider['settings']['password']))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provider password cannot be empty when inserting a new record.');
}
2021-10-27 11:04:21 +03:00
// Validate calendar view type value.
if (
! empty($provider['settings']['calendar_view'])
&& ! in_array($provider['settings']['calendar_view'], [CALENDAR_VIEW_DEFAULT, CALENDAR_VIEW_TABLE])
)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided calendar view is invalid: ' . $provider['settings']['calendar_view']);
}
2021-10-27 11:04:21 +03:00
// Make sure the email address is unique.
$provider_id = $provider['id'] ?? NULL;
2021-10-27 11:04:21 +03:00
$count = $this
->db
->select()
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_PROVIDER)
->where('users.email', $provider['email'])
2020-12-05 12:38:57 +03:00
->where('users.id !=', $provider_id)
->get()
->num_rows();
2021-10-27 11:04:21 +03:00
if ($count > 0)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided email address is already in use, please use a different one.');
}
}
/**
2021-10-27 11:04:21 +03:00
* Validate the provider username.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @param string $username Provider username.
* @param int|null $provider_id Provider ID.
2017-09-08 15:03:45 +03:00
*
* @return bool Returns the validation result.
*/
2021-10-27 11:04:21 +03:00
public function validate_username(string $username, int $provider_id = NULL): bool
{
2021-10-27 11:04:21 +03:00
if ( ! empty($provider_id))
{
2021-10-27 11:04:21 +03:00
$this->db->where('id_users !=', $provider_id);
}
2021-10-27 11:04:21 +03:00
return $this->db->get_where('user_settings', ['username' => $username])->num_rows() === 0;
}
/**
2021-10-27 11:04:21 +03:00
* Insert a new provider into the database.
*
2021-10-27 11:04:21 +03:00
* @param array $provider Associative array with the provider data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @return int Returns the provider ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:21 +03:00
protected function insert(array $provider): int
{
2021-10-27 11:04:21 +03:00
$provider['id_roles'] = $this->get_provider_role_id();
2021-10-27 11:04:21 +03:00
$service_ids = $provider['services'];
unset($provider['services']);
2021-10-27 11:04:21 +03:00
$settings = $provider['settings'];
unset($provider['settings']);
2021-10-27 11:04:21 +03:00
if ( ! $this->db->insert('users', $provider))
{
2021-10-27 11:04:21 +03:00
throw new RuntimeException('Could not insert provider.');
}
2021-10-27 11:04:21 +03:00
$provider['id'] = $this->db->insert_id();
$settings['salt'] = generate_salt();
$settings['password'] = hash_password($settings['salt'], $settings['password']);
2021-10-27 11:04:21 +03:00
$this->save_settings($provider['id'], $settings);
$this->save_service_ids($provider['id'], $service_ids);
2021-10-27 11:04:21 +03:00
return $provider['id'];
}
/**
2021-10-27 11:04:21 +03:00
* Update an existing provider.
*
2021-10-27 11:04:21 +03:00
* @param array $provider Associative array with the provider data.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @return int Returns the provider ID.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @throws RuntimeException
*/
2021-10-27 11:04:21 +03:00
protected function update(array $provider): int
{
2021-10-27 11:04:21 +03:00
$service_ids = $provider['services'];
unset($provider['services']);
2021-10-27 11:04:21 +03:00
$settings = $provider['settings'];
unset($provider['settings']);
2021-10-27 11:04:21 +03:00
if (isset($settings['password']))
{
2021-10-27 11:04:21 +03:00
$existing_settings = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
if (empty($existing_settings))
{
throw new RuntimeException('No settings record found for provider with ID: ' . $provider['id']);
}
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
}
2021-10-27 11:04:21 +03:00
if ( ! $this->db->update('users', $provider, ['id' => $provider['id']]))
{
throw new RuntimeException('Could not update provider.');
}
2021-10-27 11:04:21 +03:00
$this->save_settings($provider['id'], $settings);
$this->save_service_ids($provider['id'], $service_ids);
2021-10-27 11:04:21 +03:00
return $provider['id'];
}
/**
2021-10-27 11:04:21 +03:00
* Remove an existing provider from the database.
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id Provider ID.
*
* @throws RuntimeException
*/
2021-10-27 11:49:42 +03:00
public function delete(int $provider_id)
{
2021-10-27 11:04:21 +03:00
if ( ! $this->db->delete('users', ['id' => $provider_id]))
{
throw new RuntimeException('Could not delete provider.');
}
}
/**
2021-10-27 11:04:21 +03:00
* Get a specific provider from the database.
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id The ID of the record to be returned.
*
2021-10-27 11:04:21 +03:00
* @return array Returns an array with the provider data.
*
* @throws InvalidArgumentException
*/
2021-10-27 11:04:21 +03:00
public function find(int $provider_id): array
{
2021-10-27 11:04:21 +03:00
if ( ! $this->db->get_where('users', ['id' => $provider_id])->num_rows())
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided provider ID was not found in the database: ' . $provider_id);
}
2021-10-27 11:04:21 +03:00
$provider = $this->db->get_where('users', ['id' => $provider_id])->row_array();
2021-10-27 11:04:21 +03:00
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
2021-10-27 11:04:21 +03:00
unset($provider['settings']['id_users']);
2021-10-27 11:04:21 +03:00
$service_provider_connections = $this->db->get_where('services_providers', ['id_users' => $provider_id])->result_array();
2021-10-27 11:04:21 +03:00
$provider['services'] = [];
2021-10-27 11:04:21 +03:00
foreach ($service_provider_connections as $service_provider_connection)
{
$provider['services'][] = (int)$service_provider_connection['id_services'];
}
2021-10-27 11:04:21 +03:00
return $provider;
}
/**
2021-10-27 11:04:21 +03:00
* Get a specific field value from the database.
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id Provider ID.
* @param string $field Name of the value to be returned.
*
2021-10-27 11:04:21 +03:00
* @return string Returns the selected provider value from the database.
2020-11-16 11:41:04 +03:00
*
2021-10-27 11:04:21 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:04:21 +03:00
public function value(int $provider_id, string $field): string
{
2021-10-27 11:04:21 +03:00
if (empty($field))
{
throw new InvalidArgumentException('The field argument is cannot be empty.');
}
2021-10-27 11:04:21 +03:00
if (empty($provider_id))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provider ID argument cannot be empty.');
}
2021-10-27 11:04:21 +03:00
// Check whether the provider exists.
$query = $this->db->get_where('users', ['id' => $provider_id]);
if ( ! $query->num_rows())
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The provided provider ID was not found in the database: ' . $provider_id);
}
2021-10-27 11:04:21 +03:00
// Check if the required field is part of the provider data.
$provider = $query->row_array();
2020-12-05 12:38:57 +03:00
2021-10-27 11:04:21 +03:00
if ( ! array_key_exists($field, $provider))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The requested field was not found in the provider data: ' . $field);
}
2021-10-27 11:04:21 +03:00
return $provider[$field];
}
/**
2021-10-27 11:04:21 +03:00
* Get all providers that match the provided criteria.
*
2021-10-27 11:04:21 +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.
*
2021-10-27 11:04:21 +03:00
* @return array Returns an array of providers.
*/
2021-10-27 11:04:21 +03:00
public function get($where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
2021-10-27 11:04:21 +03:00
$role_id = $this->get_provider_role_id();
2021-10-27 11:04:21 +03:00
if ($where !== NULL)
{
2021-10-27 11:04:21 +03:00
$this->db->where($where);
}
2021-10-27 11:04:21 +03:00
if ($order_by !== NULL)
{
2021-10-27 11:04:21 +03:00
$this->db->order_by($order_by);
}
2021-10-27 11:04:21 +03:00
$providers = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
2021-10-27 11:04:21 +03:00
foreach ($providers as &$provider)
{
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
unset(
$provider['settings']['id_users'],
$provider['settings']['password'],
$provider['settings']['salt']
);
$provider['services'] = [];
$service_provider_connections = $this->db->get_where('services_providers', ['id_users' => $provider['id']])->result_array();
foreach ($service_provider_connections as $service_provider_connection)
{
$provider['services'][] = (int)$service_provider_connection['id_services'];
}
}
return $providers;
}
2021-10-27 11:04:21 +03:00
/**
2021-10-27 11:04:21 +03:00
* Get the provider role ID.
*
2021-10-27 11:04:21 +03:00
* @return int Returns the role ID.
*/
2021-10-27 11:04:21 +03:00
public function get_provider_role_id(): int
{
2021-10-27 11:04:21 +03:00
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row_array();
2021-10-27 11:04:21 +03:00
if (empty($role))
{
2021-10-27 11:04:21 +03:00
throw new RuntimeException('The provider role was not found in the database.');
}
2021-10-27 11:04:21 +03:00
return $role['id'];
}
/**
2021-10-27 11:04:21 +03:00
* Save the provider settings.
2017-09-08 15:03:45 +03:00
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id Provider ID.
* @param array $settings Associative array with the settings data.
*
2021-10-27 11:04:21 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
protected function save_settings(int $provider_id, array $settings)
{
2021-10-27 11:04:21 +03:00
if (empty($settings))
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('The settings argument cannot be empty.');
}
2021-10-27 11:04:21 +03:00
// Make sure the settings record exists in the database.
$count = $this->db->get_where('user_settings', ['id_users' => $provider_id])->num_rows();
if ( ! $count)
{
2021-10-27 11:04:21 +03:00
$this->db->insert('user_settings', ['id_users' => $provider_id]);
}
2021-10-27 11:04:21 +03:00
foreach ($settings as $name => $value)
{
2021-10-27 11:04:21 +03:00
// Sort working plans exceptions in descending order that they are easier to modify later on.
if ($name === 'working_plan_exceptions')
{
$value = json_decode($value, TRUE);
2021-10-27 11:04:21 +03:00
if ( ! $value)
{
$value = [];
}
2020-12-05 12:38:57 +03:00
2021-10-27 11:04:21 +03:00
krsort($value);
2021-10-27 11:04:21 +03:00
$value = json_encode($value);
}
$this->set_setting($provider_id, $name, $value);
}
}
/**
2021-10-27 11:04:21 +03:00
* Set the value of a provider setting.
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id Provider ID.
* @param string $name Setting name.
* @param string $value Setting value.
*/
2021-10-27 11:49:42 +03:00
public function set_setting(int $provider_id, string $name, string $value)
{
2021-10-27 11:04:21 +03:00
if ( ! $this->db->update('user_settings', [$name => $value], ['id_users' => $provider_id]))
{
2021-10-27 11:04:21 +03:00
throw new RuntimeException('Could not set the new provider setting value: ' . $name);
}
2021-10-27 11:04:21 +03:00
}
2021-10-27 11:04:21 +03:00
/**
* Get the value of a provider setting.
*
* @param int $provider_id Provider ID.
* @param string $name Setting name.
*
* @return string Returns the value of the requested user setting.
*/
public function get_setting(int $provider_id, string $name): string
{
$settings = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
2021-10-27 11:04:21 +03:00
if (empty($settings[$name]))
{
2021-10-27 11:04:21 +03:00
throw new RuntimeException('The requested setting value was not found: ' . $provider_id);
}
2021-10-27 11:04:21 +03:00
return $settings[$name];
}
/**
2021-10-27 11:04:21 +03:00
* Save the provider service IDs.
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id Provider ID.
* @param array $service_ids Service IDs.
*/
2021-10-27 11:49:42 +03:00
protected function save_service_ids(int $provider_id, array $service_ids)
{
2021-10-27 11:04:21 +03:00
// Re-insert the provider-service connections.
$this->db->delete('services_providers', ['id_users' => $provider_id]);
2021-10-27 11:04:21 +03:00
foreach ($service_ids as $service_id)
{
2021-10-27 11:04:21 +03:00
$service_provider_connection = [
'id_users' => $provider_id,
'id_services' => $service_id
];
2021-10-27 11:04:21 +03:00
$this->db->insert('services_providers', $service_provider_connection);
}
}
/**
2021-10-27 11:04:21 +03:00
* Save a new or existing working plan exception.
*
2021-10-27 11:04:21 +03:00
* @param int $provider_id Provider ID.
* @param string $date Working plan exception date (in YYYY-MM-DD format).
* @param array $working_plan_exception Associative array with the working plan exception data.
*
2021-10-27 11:04:21 +03:00
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function save_working_plan_exception(int $provider_id, string $date, array $working_plan_exception)
{
// Validate the working plan exception data.
$start = date('H:i', strtotime($working_plan_exception['start']));
2021-10-27 11:04:21 +03:00
$end = date('H:i', strtotime($working_plan_exception['end']));
if ($start > $end)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('Working plan exception start date must be before the end date.');
}
// Make sure the provider record exists.
2021-10-27 11:04:21 +03:00
$where = [
'id' => $provider_id,
'id_roles' => $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row()->id
];
2021-10-27 11:04:21 +03:00
if ($this->db->get_where('users', $where)->num_rows() === 0)
{
2021-10-27 11:04:21 +03:00
throw new InvalidArgumentException('Provider ID was not found in the database: ' . $provider_id);
}
2021-10-27 11:04:21 +03:00
// Store the working plan exception.
$working_plan_exceptions = json_decode($this->get_setting('working_plan_exceptions', $provider_id), TRUE);
2020-12-05 12:38:57 +03:00
if ( ! isset($working_plan_exception['breaks']))
{
$working_plan_exception['breaks'] = [];
}
$working_plan_exceptions[$date] = $working_plan_exception;
2021-10-27 11:04:21 +03:00
$this->set_setting(
'working_plan_exceptions',
json_encode($working_plan_exceptions),
$provider_id
);
}
/**
* Delete a provider working plan exception.
*
* @param string $date The working plan exception date (in YYYY-MM-DD format).
* @param int $provider_id The selected provider record id.
*
* @throws Exception If $provider_id argument is invalid.
*/
2021-10-27 11:49:42 +03:00
public function delete_working_plan_exception(int $provider_id, string $date)
{
2021-10-27 11:04:21 +03:00
$provider = $this->find($provider_id);
$working_plan_exceptions = json_decode($provider['settings']['working_plan_exceptions'], TRUE);
if ( ! isset($working_plan_exceptions[$date]))
{
2021-10-27 11:04:21 +03:00
return; // The selected date does not exist in provider's settings.
}
unset($working_plan_exceptions[$date]);
2021-10-27 11:04:21 +03:00
$this->set_setting(
'working_plan_exceptions',
json_encode(empty($working_plan_exceptions) ? new stdClass() : $working_plan_exceptions),
$provider_id
);
}
2020-12-05 12:38:57 +03:00
/**
2021-10-27 11:04:21 +03:00
* Get all the provider records that are assigned to at least one service.
2020-12-05 12:38:57 +03:00
*
2021-10-27 11:04:21 +03:00
* @return array Returns an array of providers.
2020-12-05 12:38:57 +03:00
*/
2021-10-27 11:04:21 +03:00
public function get_available_providers(): array
2020-12-05 12:38:57 +03:00
{
2021-10-27 11:04:21 +03:00
$providers = $this
->db
->select('users.*')
->from('users')
->join('roles', 'roles.id = users.id_roles', 'inner')
->where('roles.slug', DB_SLUG_PROVIDER)
->order_by('first_name ASC, last_name ASC, email ASC')
->get()
->result_array();
2020-12-05 12:38:57 +03:00
2021-10-27 11:04:21 +03:00
foreach ($providers as &$provider)
2020-12-05 12:38:57 +03:00
{
2021-10-27 11:04:21 +03:00
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
unset(
$provider['settings']['id_users'],
$provider['settings']['username'],
$provider['settings']['password'],
$provider['settings']['salt']
);
$provider['services'] = [];
$service_provider_connections = $this->db->get_where('services_providers', ['id_users' => $provider['id']])->result_array();
foreach ($service_provider_connections as $service_provider_connection)
{
$provider['services'][] = (int)$service_provider_connection['id_services'];
}
2020-12-05 12:38:57 +03:00
}
2021-10-27 11:04:21 +03:00
return $providers;
}
2020-12-05 12:38:57 +03:00
2021-10-27 11:04:21 +03:00
/**
* Get the query builder interface, configured for use with the users (provider-filtered) table.
*
* @return CI_DB_query_builder
*/
public function query(): CI_DB_query_builder
{
$role_id = $this->get_provider_role_id();
return $this->db->from('users')->where('id_roles', $role_id);
}
/**
* Search providers 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 providers.
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
$role_id = $this->get_provider_role_id();
return $this
->db
->select()
->from('users')
->where('id_roles', $role_id)
->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)
->limit($limit)
->offset($offset)
->order_by($order_by)
->get()
->result_array();
}
/**
* Attach related resources to a provider.
*
* @param array $provider Associative array with the provider data.
* @param array $resources Resource names to be attached ("services" supported).
*
* @throws InvalidArgumentException
*/
2021-10-27 11:49:42 +03:00
public function attach(array &$provider, array $resources)
2021-10-27 11:04:21 +03:00
{
if (empty($provider) || empty($resources))
2020-12-05 12:38:57 +03:00
{
2021-10-27 11:04:21 +03:00
return;
2020-12-05 12:38:57 +03:00
}
2021-10-27 11:04:21 +03:00
foreach ($resources as $resource)
{
switch ($resource)
{
case 'services':
$provider['services'] = $this
->db
->select('services.*')
->from('services')
->join('services_providers', 'services_providers.id_services = services.id', 'inner')
->where('id_users', $provider['id'])
->get()
->result_array();
break;
default:
throw new InvalidArgumentException('The requested provider relation is not supported: ' . $resource);
}
}
2020-12-05 12:38:57 +03:00
}
}