1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter CRUD Model
*
* A basic and resulable CRUD model for working with databases in CodeIgniter
*
* @package CodeIgniter
* @subpackage Models
* @category Models
* @author Chris Kacerguis
* @license MIT
* @link https://github.com/chriskacerguis/ci-crud
* @version 1.0.0
*/
class CRUD_model extends CI_Model
{
/**
* @var string
*/
protected $table;
/**
* @var string
*/
protected $cols;
/**
* @var string
*/
protected $created;
/**
* @var string
*/
protected $db_key;
/**
* constructor
*/
public function __construct()
{
parent::__construct();
// We need the DB library, so I'll load it here, just in case
// it hasn't been loaded already.
$this->load->library('database');
$this->cols = $this->db->list_fields($this->table);
$this->created = $this->config->item('crud_db_created');
$this->db_key = $this->config->item('crud_db_key');
$this->db_encrypt = $this->config->item('crud_db_encrypted');
// Unset the colunms we don't want people to change
unset($this->cols[$this->config->item('crud_db_key')]);
unset($this->cols[$this->config->item('crud_db_created')]);
unset($this->cols[$this->config->item('crud_db_modified')]);
// If encryption is enabled, then load the encrypt lib
if ($this->db_encrypt === true) {
$this->load->library('encrypt');
}
}
/**
* decrypts a table row, ignoring the columns that aren't encrypted:
* crud_db_key, crud_db_created, crud_db_modified
* @param array $data [description]
* @return array $data array with decrypted elements
*/
protected function decrypt_array(array $data)
{
foreach ($this->cols as $col) {
if (isset($data[$col])) {
$data[$col] = $this->encrypt->deode($data[$col]);
} else {
$data[$col] = $data[$col];
}
}
return $data;
}
/**
* set the table for our CRUD model
* @param string $table
* @return [type]
*/
public function table(string $table)
{
$this->table = $table;
return $this;
}
/**
* creates a record in our table
* @param array $data
* @return bool
*/
public function create(array $data)
{
$payload = null;
foreach ($this->cols as $item) {
if (isset($data[$item])) {
if ($this->db_encrypt === true) {
$payload[$item] = $this->encrypt->encode($data[$item]);
} else {
$payload[$item] = $data[$item];
}
}
}
if ($payload === null) {
return false;
}
$payload[$this->created] = date('Y-m-d H:i:s');
return $this->db->insert($this->table, $payload);
}
/**
* returns the record with the given id from our table
* @param int $id
* @return array
*/
public function get(string $id = null)
{
if ($id === null) {
$result = $this->db->select()->get($this->table)->result_array();
if ($this->db_encrypt === true) {
for ($i = 0; $i <= count($result); $i++) {
$result[$i] = $this->decrypt_array($result[$i]);
}
return $result;
}
return $result;
}
$result = $this->db->select()->where($this->db_key, $id)->get($this->table)->row_array();
if ($this->db_encrypt === true) {
return $this->decrypt_array($result);
}
return $result;
}
/**
* updates the record in the database with the id provided.
* @param int $id
* @param array $data
* @return bool
*/
public function update(string $id, array $data)
{
$payload = null;
foreach ($this->cols as $item) {
if (isset($data[$item])) {
if ($this->db_encrypt === true) {
$payload[$item] = $this->encrypt->encode($data[$item]);
}
$payload[$item] = $data[$item];
}
}
if ($payload === null) {
return false;
}
return $this->db->update($this->table, $payload, array($this->db_key => $id));
}
/**
* deleted the reocrd whos id is provided
* @param int $id
* @return bool
*/
public function delete(string $id)
{
return $this->db->delete($this->table, array($this->db_key => $id));
}
}
/* End of file crud_model.php */
/* Location: ./application/models/crud_model.php */
|