Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a60fc5

Browse files
Add files via upload
1 parent d783464 commit 5a60fc5

File tree

1 file changed

+75
-11
lines changed

1 file changed

+75
-11
lines changed

‎db.class.php

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,42 @@
22

33
/**
44
* PHP DB Class
5+
* 2021年01月07日 -> Class created
6+
* 2021年03月12日 -> Added rel="canoical|prev|next" to <a> pagination tags
7+
* 2021年03月12日 -> Added a lot of comments
8+
*
9+
* MUST FIX: delete(), $rules inside insert, define $functions on update and insert
10+
*
11+
* MUST CREATE: search method, wordlist method, similarwords method, searchSimilarWord method
512
*/
613

714
class db
815
{
16+
/** This stores the current $connectionName */
917
protected static $connectionName = null;
18+
19+
/** This array stores all $connections added so far on execution */
1020
protected static $connections = array();
21+
22+
/** This will receive the last inserted row $id */
1123
protected static $id = null;
24+
25+
/** This is an array with the objects keys. */
1226
protected static $object = array();
27+
28+
/** It is the $pagedObject. It is set by pagedQuery method */
1329
protected static $pageObject = null;
30+
31+
/** It will receive a $friendURL instance to work with friendly URLs */
1432
protected static $friendlyURL = false;
33+
34+
/** It will receive the time() when the class has first executed anything */
1535
private static $dbInit = null;
36+
37+
/** This is the default class $language */
1638
protected static $language = "en";
39+
40+
/** These are the words that pagination may have */
1741
protected static $defaultPaginationWords = array(
1842
"en" => array(
1943
"class" => "pagination",
@@ -29,6 +53,7 @@ class db
2953
)
3054
);
3155

56+
/** Will return a database instance. It also sets manny vars about the connection, if isn't yet defined */
3257
public static function getInstance()
3358
{
3459
if (self::$dbInit == null) {
@@ -62,6 +87,7 @@ public static function getInstance()
6287
}
6388
}
6489

90+
/** Adds a database connection */
6591
public static function addConnection($connectionName, $connectionCredentials)
6692
{
6793
if (!array_key_exists($connectionName, self::$connections)) {
@@ -70,6 +96,7 @@ public static function addConnection($connectionName, $connectionCredentials)
7096
}
7197
}
7298

99+
/** Defines which connection the class will use */
73100
public static function useConnection($connectionName)
74101
{
75102
if (array_key_exists($connectionName, self::$connections)) {
@@ -78,6 +105,7 @@ public static function useConnection($connectionName)
78105
}
79106
}
80107

108+
/** Sets total amount of requests to +1 */
81109
private static function updateTotalRequests()
82110
{
83111
if (!isset(self::$connections[self::$connectionName]['totalRequests'])) {
@@ -86,16 +114,19 @@ private static function updateTotalRequests()
86114
self::$connections[self::$connectionName]['totalRequests']++;
87115
}
88116

117+
/** Returns the total amount of requests that the class did so far */
89118
public static function getTotalRequests()
90119
{
91120
return (isset(self::$connections[self::$connectionName]['totalRequests']) ? self::$connections[self::$connectionName]['totalRequests'] : 0);
92121
}
93122

123+
/** Checks the server speed */
94124
public static function performance()
95125
{
96126
echo "<!-- Time ellapsed: " . ((int) microtime() - (int) self::$dbInit) . " -->";
97127
}
98128

129+
/** Creates a new DB Object with the given PDO instance */
99130
private static function encapsulate($mixed)
100131
{
101132
if (is_string($mixed)) {
@@ -116,6 +147,7 @@ private static function encapsulate($mixed)
116147
}
117148
}
118149

150+
/** Returns a single row */
119151
public static function fetch($mixed, $simple = false)
120152
{
121153
if (is_string($mixed)) {
@@ -139,6 +171,7 @@ public static function fetch($mixed, $simple = false)
139171
}
140172
}
141173

174+
/** Returns all fetched data */
142175
public static function fetchAll($mixed)
143176
{
144177
$mixed = self::encapsulate($mixed);
@@ -154,12 +187,14 @@ public static function fetchAll($mixed)
154187
}
155188
}
156189

190+
/** It counts the total rows that $mixed have */
157191
public static function count($mixed)
158192
{
159193
$object = self::encapsulate($mixed);
160194
return $object->extra['totalEntries'];
161195
}
162196

197+
/** Checks if the given $query returns null. If it returns null or 0, the function return true (is empty) */
163198
public static function empty($query)
164199
{
165200
if (is_string($query)) {
@@ -171,11 +206,13 @@ public static function empty($query)
171206
}
172207
}
173208

209+
/** Simple query */
174210
public static function query($mixed)
175211
{
176212
return self::encapsulate($mixed);
177213
}
178214

215+
/** This is a paged query */
179216
public static function pagedQuery($mixed, $limit, $page = false, $words = false)
180217
{
181218
if (is_string($mixed)) {
@@ -218,12 +255,14 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
218255
}
219256
}
220257

258+
/** It sets the class $language */
221259
public static function setLanguage($language)
222260
{
223261
self::$language = $language;
224262
return new static();
225263
}
226264

265+
/** It defines the words that the pagination HTML will have */
227266
public static function setPaginationWords($object, $words = false)
228267
{
229268
try {
@@ -243,6 +282,7 @@ public static function setPaginationWords($object, $words = false)
243282
}
244283
}
245284

285+
/** It retrieves the current page */
246286
private static function getPageNow($object = false)
247287
{
248288
if (!$object) {
@@ -274,6 +314,7 @@ private static function getPageNow($object = false)
274314
return $pageNow;
275315
}
276316

317+
/** It creates the pagination HTML */
277318
public static function page($echo = true, $class = "")
278319
{
279320
$totalRows = self::$pageObject->extra['totalEntries'];
@@ -307,15 +348,15 @@ public static function page($echo = true, $class = "")
307348
}
308349
}
309350
$htmlWrapper = "<ul class='pagination {additional}'>{buttons}</ul>";
310-
$htmlButton = "<li {disabled}><a . $words['class'] . " {active}' href='{target}'>{text|number}</a></li>";
351+
$htmlButton = "<li {disabled}><a {rel} class='" . $words['class'] . " {active}' href='{target}'>{text|number}</a></li>";
311352
$buttons = array();
312353
if ($pageNow > 1) {
313354
if (self::$friendlyURL) {
314355
$parts[$keyPart] = self::$friendlyURL->gerarLink($words['url'], $pageNow - 1);
315-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "/" . implode("/", $parts), $words["prev"], '', ''), $htmlButton);
356+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("rel='prev'", $url . "/" . implode("/", $parts), $words["prev"], '', ''), $htmlButton);
316357
} else {
317358
$_GET[$words['url']] = $pageNow - 1;
318-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "?" . http_build_query($_GET), $words["prev"], '', ''), $htmlButton);
359+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("rel='prev'", $url . "?" . http_build_query($_GET), $words["prev"], '', ''), $htmlButton);
319360
}
320361
}
321362
$pageCount = 0;
@@ -324,39 +365,39 @@ public static function page($echo = true, $class = "")
324365
if ($pageCount == $pageNow) {
325366
if (self::$friendlyURL) {
326367
$parts[$keyPart] = self::$friendlyURL->gerarLink($words['url'], $pageNow);
327-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array("javascript:void(0)", $pageNow, 'active', 'disabled'), $htmlButton);
368+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", "javascript:void(0)", $pageNow, 'active', 'disabled'), $htmlButton);
328369
} else {
329370
$_GET[$words['url']] = $pageNow;
330-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array("javascript:void(0)", $pageNow, 'active', 'disabled'), $htmlButton);
371+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", "javascript:void(0)", $pageNow, 'active', 'disabled'), $htmlButton);
331372
}
332373
} else {
333374
if ($pageCount <= $pageNow && ($pageCount >= $pageNow - 4 && ($pageNow == $totalPages || $pageNow + 1 == $totalPages || $pageNow + 2 == $totalPages) || ($pageCount >= $pageNow - 2)) && $pageCount > 0 && count($buttons) < 5 && ($pageCount == $pageNow - 1 || $pageCount == $pageNow - 2 || count($buttons) <= 5)) {
334375
if (self::$friendlyURL) {
335376
$parts[$keyPart] = self::$friendlyURL->gerarLink($words['url'], $pageCount);
336-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "/" . implode("/", $parts), $pageCount, '', ''), $htmlButton);
377+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", $url . "/" . implode("/", $parts), $pageCount, '', ''), $htmlButton);
337378
} else {
338379
$_GET[$words['url']] = $pageCount;
339-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "?" . http_build_query($_GET), $pageCount, '', ''), $htmlButton);
380+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", $url . "?" . http_build_query($_GET), $pageCount, '', ''), $htmlButton);
340381
}
341382
}
342383
if ($pageCount >= $pageNow && $pageCount <= $totalPages && count($buttons) <= 5 && ($pageCount == $pageNow + 1 || $pageCount == $pageNow + 2 || count($buttons) <= 5)) {
343384
if (self::$friendlyURL) {
344385
$parts[$keyPart] = self::$friendlyURL->gerarLink($words['url'], $pageCount);
345-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "/" . implode("/", $parts), $pageCount, '', ''), $htmlButton);
386+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", $url . "/" . implode("/", $parts), $pageCount, '', ''), $htmlButton);
346387
} else {
347388
$_GET[$words['url']] = $pageCount;
348-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "?" . http_build_query($_GET), $pageCount, '', ''), $htmlButton);
389+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", $url . "?" . http_build_query($_GET), $pageCount, '', ''), $htmlButton);
349390
}
350391
}
351392
}
352393
}
353394
if ($pageNow < $totalPages) {
354395
if (self::$friendlyURL) {
355396
$parts[$keyPart] = self::$friendlyURL->gerarLink($words['url'], $pageNow + 1);
356-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "/" . implode("/", $parts), $words["next"], '', ''), $htmlButton);
397+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("rel='next'", $url . "/" . implode("/", $parts), $words["next"], '', ''), $htmlButton);
357398
} else {
358399
$_GET[$words['url']] = $pageNow + 1;
359-
$buttons[] = str_replace(array("{target}", '{text|number}', '{active}', '{disabled}'), array($url . "?" . http_build_query($_GET), $words["next"], '', ''), $htmlButton);
400+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("rel='next'", $url . "?" . http_build_query($_GET), $words["next"], '', ''), $htmlButton);
360401
}
361402
}
362403
if ($echo) {
@@ -367,16 +408,19 @@ public static function page($echo = true, $class = "")
367408
}
368409
}
369410

411+
/** Returns server date */
370412
public static function date($time = false)
371413
{
372414
return ($time ? date("Y-m-d", $time) : date("Y-m-d"));
373415
}
374416

417+
/** Returns server dateTime */
375418
public static function dateTime($time = false)
376419
{
377420
return ($time ? date("Y-m-d H:s:i", $time) : date("Y-m-d H:s:i"));
378421
}
379422

423+
/** It will change the whole database $collation */
380424
public static function setCollation($collation, $show = false, $execute = false)
381425
{
382426
$db_nome = self::$connections[self::$connectionName]['NAME'];
@@ -403,17 +447,20 @@ public static function setCollation($collation, $show = false, $execute = false)
403447
}
404448
}
405449

450+
/** Will define the $friendlyURL instance on the class */
406451
public static function setFriendlyURL($FriendlyURLInstance)
407452
{
408453
self::$friendlyURL = $FriendlyURLInstance;
409454
return new static();
410455
}
411456

457+
/** It will retrieve the collumns of the given $table */
412458
private static function getTableCollumns($table)
413459
{
414460
return self::fetchAll(self::query("DESCRIBE $table"));
415461
}
416462

463+
/** It will fix invalid $data keys before insert them on the $table. It removes not used keys inside $data, and bind a empty string if a specific $key that exists on the table, doesn't exist on $data */
417464
private static function fixDataCollumns($data, $table, &$newData = array(), $mode = "insert")
418465
{
419466
$collumns = self::getTableCollumns($table);
@@ -465,21 +512,25 @@ private static function fixDataCollumns($data, $table, &$newData = array(), $mod
465512
return $newData;
466513
}
467514

515+
/** It will prepare a given $sql to the given $object */
468516
public static function prepare($object, $sql)
469517
{
470518
return $object->prepare($sql);
471519
}
472520

521+
/** It will set the given $value on the specific $key, inside the $object. Object is a PDO Statement */
473522
public static function set(&$object, $key, $value)
474523
{
475524
$object->bindValue(":" . $key, $value);
476525
}
477526

527+
/** It executes any given $sql */
478528
public static function execute($sql)
479529
{
480530
return self::getInstance()->exec($sql);
481531
}
482532

533+
/** WORK IN PROGRESS - It will insert $data on a specific $table. The $rules are optional */
483534
public static function insert($data, $table, $rules = array())
484535
{
485536
self::fixDataCollumns($data, $table, $newData);
@@ -498,11 +549,13 @@ public static function insert($data, $table, $rules = array())
498549
}
499550
}
500551

552+
/** It will return the id of the last inserted row */
501553
public static function id()
502554
{
503555
return self::$id;
504556
}
505557

558+
/** It will update a row on a specific $table with new $data. The row must attend to the $rules */
506559
public static function update($data, $table, $rules = array())
507560
{
508561
self::fixDataCollumns($data, $table, $newData, "update");
@@ -535,6 +588,7 @@ public static function update($data, $table, $rules = array())
535588
}
536589
}
537590

591+
/** WORK IN PROGRESS - It will delete a record - or all - on a specific $table */
538592
public static function delete($table, $rules = array())
539593
{
540594
if (!empty($rules)) {
@@ -558,6 +612,7 @@ public static function delete($table, $rules = array())
558612
}
559613
}
560614

615+
/** It will normalize a string to be accepted on URL addresses */
561616
public function URLNormalize($string)
562617
{
563618
$string = preg_replace('/[áàãâä]/ui', 'a', $string);
@@ -572,22 +627,31 @@ public function URLNormalize($string)
572627
}
573628
}
574629

630+
/** DB Object class - it will store the result of the request */
575631
class dbObject
576632
{
633+
/** It is the DB instance */
577634
protected static $instance = null;
635+
636+
/** Array with extra info */
578637
public $extra = null;
579638

639+
/** It already sets a number of info on $extra */
580640
public function __construct($instance, $extra = array())
581641
{
582642
$this->setInstance($instance);
583643
$this->extra = $extra;
584644
$this->extra['totalEntries'] = $instance->rowCount();
585645
return $this;
586646
}
647+
648+
/** It will set the $instance */
587649
private static function setInstance($instance)
588650
{
589651
self::$instance = $instance;
590652
}
653+
654+
/** It will retrieve the object's DB $instance */
591655
public static function getInstance()
592656
{
593657
return self::$instance;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /