1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xhelp; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* You may not change or alter any portion of this comment or credits |
7
|
|
|
* of supporting developers from this source code or any supporting source code |
8
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
17
|
|
|
* @license {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
18
|
|
|
* @author Brian Wahoff <[email protected]> |
19
|
|
|
* @author Eric Juden <[email protected]> |
20
|
|
|
* @author XOOPS Development Team |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* class BaseObject |
25
|
|
|
*/ |
26
|
|
|
class BaseObject extends \XoopsObject |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* create a new object |
30
|
|
|
* @return BaseObject {@link BaseObject} |
31
|
|
|
*/ |
32
|
|
|
public function &create(): BaseObject |
33
|
|
|
{ |
34
|
|
|
return new $this->classname(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* retrieve an object from the database, based on. use in child classes |
39
|
|
|
* @param int $id ID |
40
|
|
|
* @return DepartmentMailBox|bool |
41
|
|
|
*/ |
42
|
|
|
public function get(int $id) |
43
|
|
|
{ |
44
|
|
|
$id = $id; |
45
|
|
|
if ($id > 0) { |
46
|
|
|
$sql = $this->selectQuery(new \Criteria('id', (string)$id)); |
47
|
|
|
if (!$result = $this->db->query($sql)) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
$numrows = $this->db->getRowsNum($result); |
51
|
|
|
if (1 == $numrows) { |
52
|
|
|
$obj = new $this->classname($this->db->fetchArray($result)); |
53
|
|
|
|
54
|
|
|
return $obj; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a "select" SQL query |
63
|
|
|
* @param \CriteriaElement|null $criteria {@link CriteriaElement} to match |
64
|
|
|
* @return string SQL query |
65
|
|
|
*/ |
66
|
|
|
public function selectQuery(\CriteriaElement $criteria = null): string |
67
|
|
|
{ |
68
|
|
|
$sql = \sprintf('SELECT * FROM `%s`', $this->db->prefix($this->dbtable)); |
69
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
70
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
71
|
|
|
if ('' != $criteria->getSort()) { |
72
|
|
|
$sql .= ' ORDER BY ' . $criteria->getSort() . ' |
73
|
|
|
' . $criteria->getOrder(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $sql; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* count objects matching a criteria |
82
|
|
|
* |
83
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} to match |
84
|
|
|
* @return int count of objects |
85
|
|
|
*/ |
86
|
|
|
public function getCount($criteria = null): int |
87
|
|
|
{ |
88
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($this->dbtable); |
89
|
|
|
if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
90
|
|
|
$sql .= ' ' . $criteria->renderWhere(); |
91
|
|
|
} |
92
|
|
|
if (!$result = $this->db->query($sql)) { |
93
|
|
|
return 0; |
94
|
|
|
} |
95
|
|
|
[$count] = $this->db->fetchRow($result); |
96
|
|
|
|
97
|
|
|
return $count; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* delete object based on id |
102
|
|
|
* |
103
|
|
|
* @param \XoopsObject $obj |
104
|
|
|
* @param bool $force |
105
|
|
|
* @return bool true/false on deleting objects |
106
|
|
|
* @internal param CriteriaElement $criteria to match |
107
|
|
|
*/ |
108
|
|
|
public function delete(\XoopsObject $obj, bool $force = false): bool |
109
|
|
|
{ |
110
|
|
|
if (0 != \strcasecmp($this->classname, \get_class($obj))) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), $obj->getVar('id')); |
|
|
|
|
115
|
|
|
if ($force) { |
116
|
|
|
$result = $this->db->queryF($sql); |
117
|
|
|
} else { |
118
|
|
|
$result = $this->db->query($sql); |
119
|
|
|
} |
120
|
|
|
if (!$result) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|