Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
CTable.php
1 <?php
2 
3 /**
4  * CTable.php - Table ancestor class implementation.
5  *
6  * @package SellerDeck Extensions
7  *
8  * @author Péter Erdődi
9  * @copyright © SellerDeck Ltd 2015. All rights reserved.
10  */
11 
12 namespace SDExtension\DB\Table;
13 
14 class CTable
15  {
16 
17  /**
18  * @var object DataBase Object
19  */
20  protected $m_oDB = null;
21 
22  /**
23  * @var string Table name
24  */
25  protected $m_sTable = 'table';
26 
27  /**
28  * @var string Table ID Column
29  * @todo Does not support compound keys
30  */
31  protected $m_sIdColumn = 'id';
32 
33  /**
34  * @var bool Is ID autogenerated?
35  */
36  protected $m_bAutoID = true;
37 
38  /**
39  * @var array Record Default values
40  */
41  protected $m_aDefaultValues = [];
42 
43  /**
44  * @var array Record Actual values
45  */
46  protected $m_aActualValues = [];
47 
48  /**
49  * @var array Record validation rules
50  */
51  protected $m_aRules = [];
52 
53  /**
54  * @var bool Is New Record?
55  */
56  protected $m_bNewRecord;
57 
58  /**
59  * @var bool Is Modified?
60  */
61  protected $m_bModified;
62 
63  /**
64  * __construct - Object constructor method
65  *
66  * @access public
67  * @param CDataBase $oDB Database Object
68  * @return void
69  */
70  public function __construct(\SDExtension\DB\CDataBase $oDB)
71  {
72  $this->m_oDB = $oDB;
73  $this->Reset();
74  }
75 
76  /**
77  * GetDB - DB
78  *
79  * @access public
80  * @return object DB
81  */
82  public function GetDB()
83  {
84  return $this->m_oDB->GetDB();
85  }
86 
87  /**
88  * Reset - Resets class variables
89  *
90  * @access public
91  * @return void
92  */
93  public function Reset()
94  {
95  $this->m_bNewRecord = true;
96  $this->m_bModified = false;
97  $this->m_aActualValues = $this->m_aDefaultValues;
98  }
99 
100  /**
101  * Save - Saves (inserts or updates) actual record to DB
102  *
103  * @access public
104  * @param array|null $aActualValues Actual Values
105  * @return void
106  */
107  public function Save($aActualValues = null)
108  {
109  if ($aActualValues == null)
110  {
111  $aActualValues = $this->m_aActualValues;
112  }
113  elseif (is_array($aActualValues))
114  {
115  $this->m_aActualValues = $aActualValues;
116  }
117  $sID = arr_get($aActualValues, $this->m_sIdColumn, '');
118  if ($this->m_bModified)
119  {
120  if ($this->m_bNewRecord)
121  {
122  if (empty($sID))
123  {
124  unset($aActualValues[$this->m_sIdColumn]);
125  }
126  $sID = $this->m_oDB->Insert($this->m_sTable, $aActualValues, $this->m_bAutoID, $this->m_sIdColumn);
127  }
128  else
129  {
130  unset($aActualValues[$this->m_sIdColumn]);
131  $this->m_oDB->Update($this->m_sTable, $aActualValues, $this->GenerateWhereID($sID));
132  }
133  $this->m_bModified = false;
134  }
135  return $sID;
136  }
137 
138  /**
139  * UpdateSave - Updates record and saves it to the DB
140  *
141  * @access public
142  * @param array $aActualValues Values to set
143  * @param bool $bModified Is modified?
144  * @param bool $bAutoId Get id from Actual Record
145  * @return array Actual record values
146  */
147  public function UpdateSave($aActualValues = [], $bModified = true, $bAutoId = false)
148  {
149  $vID = arr_get($bAutoId ? $this->m_aActualValues : $aActualValues, $this->m_sIdColumn, '');
150  unset($aActualValues[$this->m_sIdColumn]);
151  $this->m_oDB->Update($this->m_sTable, $aActualValues, $this->GenerateWhereID($vID));
152  $aActualValues[$this->m_sIdColumn] = $vID;
153  $this->Set($aActualValues, $bModified);
154  }
155 
156  /**
157  * Set - Sets Active record values
158  *
159  * @access public
160  * @param array $aActualValues Values to set
161  * @param bool $bModified Is modified?
162  * @return array Actual record values
163  */
164  public function Set($aActualValues = [], $bModified = true)
165  {
166  $this->m_bModified = $bModified;
167  $this->m_aActualValues = array_merge($this->m_aActualValues, $aActualValues);
168  return $this->m_aActualValues;
169  }
170 
171  /**
172  * Get - Gets Active record values
173  *
174  * @access public
175  * @return array Actual record values
176  */
177  public function Get()
178  {
179  return $this->m_aActualValues;
180  }
181 
182  /**
183  * GetRecords - Gets all records from the DB
184  *
185  * @access public
186  * @return GeneratorObject
187  */
188  public function GetRecords()
189  {
190  foreach ($this->m_oDB->Select($this->m_sTable) as $row)
191  {
192  yield $row;
193  }
194  }
195 
196  /**
197  * Load - Loads one record from the DB (the first record according to the condition)
198  *
199  * @access public
200  * @param array $aWhere Conditions
201  * @param int $nReturnType Return Type (associative array by default)
202  * @return array|object Record
203  */
204  public function Load($aWhere = [], $nReturnType = \PDO::FETCH_ASSOC)
205  {
206  $oQueryResult = $this->m_oDB->Select($this->m_sTable, "*", $aWhere);
207  if ($oQueryResult === false)
208  {
209  return [];
210  }
211  $this->m_bNewRecord = false;
212  $vRecord = $oQueryResult->fetch($nReturnType);
213  if ($vRecord === false)
214  {
215  return [];
216  }
217  return $this->Set($vRecord, false);
218  }
219 
220  /**
221  * LoadByValue - Loads one record by condition: Column = Value
222  *
223  * @access public
224  * @param string $sColumn Column name
225  * @param string $sValue Column value
226  * @return array Record
227  */
228  public function LoadByValue($sColumn, $sValue)
229  {
230  $aWhere = $this->GenerateWhereCondition($sColumn, $sValue);
231  return $this->Load($aWhere);
232  }
233 
234  /**
235  * LoadByID - Loads one record by given ID value
236  *
237  * @access public
238  * @param string $sValue Column value
239  * @return array Record
240  */
241  public function LoadByID($sValue)
242  {
243  $aWhere = $this->GenerateWhereID($sValue);
244  return $this->Load($aWhere);
245  }
246 
247  /**
248  * Reload - Reloads Actual record, or loads a new one by given ID value
249  *
250  * @access public
251  * @param string $sValue Column value
252  * @return array Record
253  */
254  public function Reload($sID = null)
255  {
256  if ($sID == null)
257  {
258  $sID = $this->m_aActualValues[$this->m_sIdColumn];
259  }
260  return $this->LoadByID($sID);
261  }
262 
263  /**
264  * GenerateWhereCondition - Generates WHERE Condition
265  *
266  * @access private
267  * @param string $sColumn Column name
268  * @param string $sValue Column value
269  * @return array Where condition array
270  */
271  private function GenerateWhereCondition($sColumn, $sValue)
272  {
273  return [[$sColumn, "=", $sValue]];
274  }
275 
276  /**
277  * GenerateWhereCondition - Generates WHERE Condition with given ID value
278  *
279  * @access private
280  * @param string $sValue ID Column value
281  * @return array Where condition array
282  */
283  private function GenerateWhereID($sValue)
284  {
285  return $this->GenerateWhereCondition($this->m_sIdColumn, $sValue);
286  }
287 
288  /**
289  * CreateOriginalCopies - Creates Original Copies
290  *
291  * @access public
292  * @return void
293  */
294  public function CreateOriginalCopies()
295  {
296  foreach ($this->m_aActualValues as $sKey => $sValue)
297  {
298  foreach (array("Original ", "dOriginal", "sOriginal", "sOrig") as $sOriginalPrefix)
299  {
300  $sAltKey = substr($sKey, 1);
301  if (isset($this->m_aDefaultValues["$sOriginalPrefix$sKey"]))
302  {
303  $this->m_aActualValues["$sOriginalPrefix$sKey"] = $sValue;
304  }
305  elseif (isset($this->m_aDefaultValues["$sOriginalPrefix$sAltKey"]))
306  {
307  $this->m_aActualValues["$sOriginalPrefix$sAltKey"] = $sValue;
308  }
309  }
310  }
311  }
312 
313  /**
314  * Validate - Validates record
315  *
316  * @access public
317  * @param array $aActualValues Column value
318  * @return mixed true | Array of errors
319  */
320  public function Validate($aActualValues = [])
321  {
322  $oValidationResult = \SimpleValidator\Validator::validate($aActualValues, $this->GetRules());
323  $oValidationResult->customErrors(load_config(CONFIG_APP_ROOT . "validator/en.php"));
324  if ($oValidationResult->isSuccess() == true)
325  {
326  return true;
327  }
328  else
329  {
330  return $oValidationResult->getErrors();
331  }
332  }
333 
334  /**
335  * GetRules - Get Validator rules
336  *
337  * @access protected
338  * @return array Rules
339  */
340  protected function GetRules()
341  {
342  return $this->m_aRules;
343  }
344 
345  }
__construct(\SDExtension\DB\CDataBase $oDB)
Definition: CTable.php:70
UpdateSave($aActualValues=[], $bModified=true, $bAutoId=false)
Definition: CTable.php:147
LoadByValue($sColumn, $sValue)
Definition: CTable.php:228
Set($aActualValues=[], $bModified=true)
Definition: CTable.php:164
Load($aWhere=[], $nReturnType=\PDO::FETCH_ASSOC)
Definition: CTable.php:204
Validate($aActualValues=[])
Definition: CTable.php:320
Save($aActualValues=null)
Definition: CTable.php:107