| Server IP : 188.114.97.2 / Your IP : 104.23.243.200 Web Server : Apache/2.4.59 (Debian) System : Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64 User : edlftp ( 1002) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/nicolasj/www/sms.formationlangues.be/application/models/ |
Upload File : |
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Address_model extends CI_Model
{
protected $locationType = 'id';
public function get_entry(int $id)
{
if ($id <= 0) {
return null;
}
$result = $this->db->get_where('address', ['address_id' => $id])->result();
return isset($result[0]) ? $result[0] : null;
}
public function update_entry(int $id, array $fields): bool
{
if ($id <= 0 || empty($fields)) {
return false;
}
return $this->db->update('address', $fields, ['address_id' => $id]);
}
public function locationType($type)
{
$this->locationType = $type;
return $this;
}
public function fullfilled(int $address_id) : bool {
$address = $this->get_entry($address_id);
if (empty($address)) {
return false;
}
$valid = !empty($address->line_1) && strlen($address->line_1) > 3;
$valid = $valid && !empty($address->postcode) && strlen($address->postcode) > 3;
$valid = $valid && !empty($address->country) && strlen($address->country) > 3;
return $valid;
}
public function get($addressID)
{
$addressID = (int) $addressID;
switch ($this->locationType) {
case 'text':
$data = $this->db->get_where('address', array('address_id' => $addressID), 1)->row_array();
break;
default:
$data = $this->db->select('a.*,l.location_id,s.state_id,c.country_id')
->join('location AS l', 'l.location_id=a.location_id', 'inner')
->join('state AS s', 's.state_id=l.state_id', 'inner')
->join('country AS c', 'c.country_id=s.country_id', 'inner')
->get_where('address AS a', array('a.address_id' => $addressID), 1)->row_array();
if ($data) {
$data['countries'] = $this->db->get('country')->result_array();
$data['states'] = $this->db->get_where('state', array('country_id' => $data['country_id']))->result_array();
$data['locations'] = $this->db->get_where('location', array('state_id' => $data['state_id']))->result_array();
}
break;
}
return $data;
}
public function save($data, $required = false)
{
$CI = & get_instance();
$addressData = $CI->input->_fetch_from_array($data, null, true);
$addressID = $addressData['address_id'] * 1;
$dataSet = array();
$errors = array();
switch ($this->locationType) {
case 'text':
$fields = array('line_1','country','location','postcode');
break;
default:
$fields = array('line_1','location_id','postcode');
break;
}
foreach($fields as $f) {
if (empty($addressData[$f])) {
$errors[] = $CI->translate($addressData['type'].'_'.$f.'_should_be_set');
} else {
$dataSet[$f] = $addressData[$f];
}
}
if (!empty($addressData['line_2'])) {
$dataSet['line_2'] = $addressData['line_2'];
}
if (!empty($addressData['state'])) {
$dataSet['state'] = $addressData['state'];
}
if ($required || $dataSet !== array()) {
if ($errors !== array()) {
foreach($errors as $e) {
$CI->error($e);
}
}
}
if (!$CI->hasErrors() && $dataSet !== array()) {
if ($addressID == 0) {
$this->db->insert('address', $dataSet);
return $this->db->insert_id();
} else {
$this->db->where('address_id', $addressID)->update('address', $dataSet);
return $addressID;
}
}
return false;
}
}