Skip to content

Commit 2160f62

Browse files
committed
View System 2
1 parent ec4b490 commit 2160f62

File tree

14 files changed

+2199
-199
lines changed

14 files changed

+2199
-199
lines changed

app/controllers/p404Controller.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace AFN\App\Controllers;
1111

1212
use AFN\App\Core\Controller;
13-
use AFN\App\Models\Localization;
1413

1514
/**
1615
* 404 Not Found Page
@@ -19,8 +18,6 @@
1918
*/
2019
class p404Controller extends Controller {
2120
public function show_404() {
22-
$layout = $this->get_layout('404.php');
23-
$loc = new Localization();
24-
echo $loc->translate($layout);
21+
2522
}
2623
}

app/core/Model.php

Lines changed: 164 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
/*
3+
/**
44
* This file was created by AFN.
55
* If you think that there is a notifiable issue
66
* affecting the file, please contact AFN.
@@ -18,31 +18,87 @@
1818
*/
1919
class Model {
2020

21-
private $db;
21+
/**
22+
* Stores database id from config file
23+
* @var integer
24+
*/
25+
private $dbno;
26+
27+
/**
28+
* Stores database connection
29+
* @var object
30+
*/
2231
private $conn;
32+
33+
/**
34+
* Stores number of records after an operation
35+
* @var integer
36+
*/
2337
public $recordcount;
38+
39+
/**
40+
* Stores the name of table that will be called
41+
* @var string
42+
*/
2443
public $table;
2544

45+
/**
46+
* Makes a database connection by using the credential in the config file
47+
* The credential is chosen according to database id
48+
* @param integer $dbno database id
49+
*/
2650
public function __construct($dbno = 1) {
2751
try {
52+
// Check if ROOT_DIR is defined then use it or add it manually for the config file path
2853
if(!defined(ROOT_DIR)) {
2954
$db_dir = realpath(__DIR__ . '/../..') . '/config/database.php';
3055
} else {
3156
$db_dir = ROOT_DIR . '/config/database.php';
3257
}
3358

59+
// Include the database config file
3460
$database = require_once $db_dir;
3561

62+
// Run database connection with PDO and pass it to the variable
3663
$this->conn = new PDO($database[$dbno]["DSN"], $database[$dbno]["USERNAME"], $database[$dbno]["PASSWORD"], [
3764
PDO::ATTR_EMULATE_PREPARES => false,
3865
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
3966
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="NO_BACKSLASH_ESCAPES"'
4067
]);
4168
} catch(PDOException $e) {
69+
// If the process fails, throw an error
4270
echo $e->getMessage();
4371
}
4472
}
4573

74+
/**
75+
* Passes the given value to the given property
76+
* @param string $property
77+
* @param mixed $value
78+
*/
79+
public function __set($property, $value) {
80+
$this->$property = $value;
81+
}
82+
83+
/**
84+
* Returns value of the property if it is defined
85+
* @param string $property
86+
* @return mixed
87+
*/
88+
public function __get($property) {
89+
if(isset($this->$property)) {
90+
return $this->$property;
91+
}
92+
}
93+
94+
/**
95+
* Executes the given query with its parameters as an array
96+
* Besides, it calculates record count of the query and pass it to a public variable
97+
* @param string $query The query that will be executed
98+
* @param array $params The Parameters that will be inserted to the query
99+
* @param boolean $count The parameter that will decide as to whether or not to calculate record count
100+
* @return boolean
101+
*/
46102
public function execute_on_query($query, array $params = [], $count = FALSE) {
47103
$stmt = $this->conn->prepare($query);
48104
try {
@@ -59,10 +115,19 @@ public function execute_on_query($query, array $params = [], $count = FALSE) {
59115
}
60116
}
61117

118+
/**
119+
* @param object $stmt
120+
* @return array
121+
*/
62122
public function get_num_rows($stmt) {
63123
return (is_resource($stmt) ? $stmt->fetchColumn() : 0);
64124
}
65125

126+
/**
127+
* Fetches all column names of the given table as an array
128+
* @param string $tableName
129+
* @return boolean or array
130+
*/
66131
public function get_columns_name(string $tableName = "") {
67132
if(!empty($this->table) || !empty($tableName)) {
68133
$tname = isset($tableName) ? $tableName : $this->table;
@@ -77,6 +142,11 @@ public function get_columns_name(string $tableName = "") {
77142
}
78143
}
79144

145+
/**
146+
* Prepares data according to JSON format
147+
* @param array $array
148+
* @return string
149+
*/
80150
public function json_turkish(array $array) {
81151
foreach($array as $record) {
82152
foreach($record as $key => $og) {
@@ -90,6 +160,11 @@ public function json_turkish(array $array) {
90160
return $result;
91161
}
92162

163+
/**
164+
* Fetches data with given id from the database and passes it to properties
165+
* @param integer $ID The row id of the expected content in the database
166+
* @return string The Error
167+
*/
93168
public function refresh($ID) {
94169
if(is_numeric($ID)) {
95170
$query = "SELECT * FROM " . $this->table . " WHERE " . key($this) . " = :id";
@@ -101,7 +176,7 @@ public function refresh($ID) {
101176
return 'ERROR: ' . $error[2];
102177
} else {
103178
$result = $stmt->fetch(PDO::FETCH_BOTH);
104-
if($result) {
179+
if(isset($result)) {
105180
foreach($result as $name => $value) {
106181
$this->$name = $value;
107182
}
@@ -110,6 +185,12 @@ public function refresh($ID) {
110185
}
111186
}
112187

188+
/**
189+
* Executes given query to fetch data and then passes data to properties
190+
* @param string $query The query that will be executed
191+
* @param array $params The Parameters that will be inserted to the query
192+
* @return string The error or nothing
193+
*/
113194
public function refresh_procedure($query, array $params = []) {
114195
if($query != "") {
115196
$stmt = $this->conn->prepare($query);
@@ -127,8 +208,11 @@ public function refresh_procedure($query, array $params = []) {
127208
}
128209
}
129210

211+
/**
212+
* Returns property names and property values of child classes as JSON format
213+
* @return string The property names and property values as JSON format
214+
*/
130215
public function to_json() {
131-
132216
$classItems = get_object_vars($this);
133217
$json = "{";
134218
foreach($classItems as $key => $val) {
@@ -141,19 +225,33 @@ public function to_json() {
141225
return $json;
142226
}
143227

228+
/**
229+
* The save function
230+
* The function deals two different situation. If the row id not exists,the function insert
231+
* the given data by adding new row to the table. However, if it exists, the function updates the row
232+
* by using new data.
233+
* @return int
234+
*/
144235
public function save() {
236+
// Get class properties and pass them
145237
$classitems = get_object_vars($this);
238+
239+
// Get column names
146240
$columns = $this->get_columns_name();
147241

242+
// Create needed arrays
148243
$data_fields = [];
149244
$data_names = [];
150245
$params = [];
151246

247+
// Push names and values of properties to arrays if property names does not match column names
152248
foreach($classitems as $claskey => $clasval) {
153249
foreach($columns as $colkey => $colval) {
154250
if($claskey == $colval) {
155251
$data_fields[] = $colval;
156252
$data_names[] = ":" . $colval;
253+
254+
// Determine the form of NULL according to numericalness of $classval
157255
if(is_numeric($clasval)) {
158256
$params[":" . $colval] = str_replace("'on'", "True", str_replace("'NULL'", "NULL", is_null($clasval) ? "NULL" : $clasval));
159257
} else {
@@ -163,8 +261,10 @@ public function save() {
163261
}
164262
}
165263

264+
// Begin the transaction
166265
$this->conn->beginTransaction();
167266

267+
// If the id exists, the process is updating. However, if not, the process is inserting.
168268
if(!is_numeric($this->ID) || $this->ID == 0) {
169269
$sql = "INSERT INTO " . $this->table . " (" . implode(",", $data_fields) . ") VALUES " . implode(',', $data_names);
170270
} else {
@@ -179,36 +279,45 @@ public function save() {
179279
$sql .= " WHERE ID=:ID";
180280
}
181281

282+
// Prepare a statement for above execution
182283
$stmt = $this->conn->prepare($sql);
183284

184285
try {
185286

186287
$stmt->execute($params);
187288

289+
// Get last inserted id after inserting process or get the id of updated row
188290
if(substr($sql, 0, 6) == "INSERT") {
189291
$operation = 1;
190-
$last_insert = $this->conn->lastInsertId();
292+
$last_insert_id = $this->conn->lastInsertId();
191293
} else {
192294
$operation = 2;
193-
$last_insert = $this->ID;
295+
$last_insert_id = $this->ID;
194296
}
195297

196-
// history operation
298+
// History operation will be here soon
197299
// $this->new_history($lastInsert, $operation);
198300

199-
return $last_insert;
301+
return $last_insert_id;
200302
} catch(PDOException $e) {
303+
// If the execution fails, throw an error
201304
echo $e->getMessage();
202305
return 0;
203306
}
307+
308+
// Send all to database
204309
$this->conn->commit();
205310
}
206311

312+
/**
313+
* Generate a globally unique identifier
314+
* @return string
315+
*/
207316
public function custom_create_guid() {
208317
if(function_exists('com_create_guid')) {
209318
return com_create_guid();
210319
} else {
211-
mt_srand((double) microtime() * 10000); // optional for php 4.2.0 and up.
320+
mt_srand((double) microtime() * 10000); // Optional for php 4.2.0 and up.
212321
$charid = strtoupper(md5(uniqid(rand(), true)));
213322
$hyphen = chr(45); // "-"
214323
$uuid = ""// "{"
@@ -222,27 +331,43 @@ public function custom_create_guid() {
222331
}
223332
}
224333

334+
/**
335+
* Deletes a record in the database according to the given id
336+
* @param type $force 0 permanent deleting, 1 recoverable deleting
337+
* @return boolean The result of operation
338+
*/
225339
public function delete($force = 0) {
340+
// Check requested operation and run it
226341
if($force == 1) {
342+
// Permanent deleting
227343
$sql = "DELETE FROM " . $this->table . " WHERE " . key($this) . "=:ID";
228344
} else {
345+
// Recoverable deleting
229346
$sql = "UPDATE " . $this->table . " SET isDeleted=1 WHERE " . key($this) . "=:ID";
230347
}
231348

349+
// Prepare a statement for execution
232350
$stmt = $this->conn->prepare($sql);
351+
352+
// Send id of record that will be deleted
233353
$stmt->bindParam(':ID', $this->ID, PDO::PARAM_INT);
234354

235355
try {
236356
$stmt->execute();
237-
// history operation
357+
// History operation will be here soon
238358
// $this->new_history($this->ID, 3);
239359
return TRUE;
240360
} catch(PDOException $e) {
361+
// If the execution fails, throw an error
241362
echo $e->getMessage();
242363
return FALSE;
243364
}
244365
}
245366

367+
/**
368+
* Deletes all records in the given table
369+
* @return boolean
370+
*/
246371
public function delete_all() {
247372
$sql = "DELETE FROM " . $this->table;
248373
$stmt = $this->conn->prepare($sql);
@@ -252,28 +377,16 @@ public function delete_all() {
252377
// $this->new_history($this->ID, 3);
253378
return TRUE;
254379
} catch(PDOException $e) {
380+
// If the execution fails, throw an error
255381
echo $e->getMessage();
256382
return FALSE;
257383
}
258384
}
259385

260-
public function new_history($ID, $operation, $userID = 0) {
261-
//operation 1-New 2-Update 3-Delete
262-
$sql = "INSER INTO History (tableName,tableID,userID,operation,updated) VALUES (':table',:ID,:userID,:operation,NOW())";
263-
$stmt = $this->conn->prepare($sql);
264-
$stmt->bindParam(':table', $this->table, PDO::PARAM_STR);
265-
$stmt->bindParam(':ID', $ID, PDO::PARAM_INT);
266-
$stmt->bindParam(':userID', $userID, PDO::PARAM_INT);
267-
$stmt->bindParam(':operation', $operation, PDO::PARAM_INT);
268-
try {
269-
$stmt->execute();
270-
return TRUE;
271-
} catch(PDOException $e) {
272-
echo $e->getMessage();
273-
return FALSE;
274-
}
275-
}
276-
386+
/**
387+
* Returns the count of records in the given table
388+
* @return integer The count of records
389+
*/
277390
public function table_record_count() {
278391
$query = "SELECT COUNT(*) FROM " . $this->table;
279392
$result = $this->conn->query($query)->fetchColumn();
@@ -338,4 +451,28 @@ public function fetch_column($query, array $params = [], $count = FALSE) {
338451
}
339452
}
340453

454+
/*
455+
* History operation will be in use soon
456+
* @param type $ID
457+
* @param type $operation
458+
* @param type $userID
459+
* @return boolean
460+
461+
public function new_history($ID, $operation, $userID = 0) {
462+
//operation 1-New 2-Update 3-Delete
463+
$sql = "INSER INTO History (tableName,tableID,userID,operation,updated) VALUES (':table',:ID,:userID,:operation,NOW())";
464+
$stmt = $this->conn->prepare($sql);
465+
$stmt->bindParam(':table', $this->table, PDO::PARAM_STR);
466+
$stmt->bindParam(':ID', $ID, PDO::PARAM_INT);
467+
$stmt->bindParam(':userID', $userID, PDO::PARAM_INT);
468+
$stmt->bindParam(':operation', $operation, PDO::PARAM_INT);
469+
try {
470+
$stmt->execute();
471+
return TRUE;
472+
} catch(PDOException $e) {
473+
echo $e->getMessage();
474+
return FALSE;
475+
}
476+
}
477+
*/
341478
}

0 commit comments

Comments
 (0)