|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\crud; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
use Ubiquity\utils\http\URequest; |
|
7
|
|
|
use Ubiquity\orm\DAO; |
|
8
|
|
|
use Ubiquity\orm\parser\Reflexion; |
|
9
|
|
|
use Ubiquity\cache\database\DbCache; |
|
10
|
|
|
use Ubiquity\db\SqlUtils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Ubiquity\controllers\crud$CRUDHelper |
|
14
|
|
|
* This class is part of Ubiquity |
|
15
|
|
|
* |
|
16
|
|
|
* @author jc |
|
17
|
|
|
* @version 1.0.1 |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class CRUDHelper { |
|
21
|
|
|
|
|
22
|
7 |
|
public static function getIdentifierFunction($model) { |
|
23
|
7 |
|
$pks = self::getPks ( $model ); |
|
24
|
|
|
return function ($index, $instance) use ($pks) { |
|
25
|
7 |
|
$values = [ ]; |
|
26
|
7 |
|
foreach ( $pks as $pk ) { |
|
27
|
7 |
|
$getter = "get" . ucfirst ( $pk ); |
|
28
|
7 |
|
if (method_exists ( $instance, $getter )) { |
|
29
|
7 |
|
$values [] = $instance->{$getter} (); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
7 |
|
return implode ( "_", $values ); |
|
33
|
7 |
|
}; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public static function search($model, $search, $fields, $initialCondition = "1=1") { |
|
37
|
1 |
|
$words = preg_split ( "@(\s*?(\(|\)|\|\||\&\&)\s*?)@", $search ); |
|
38
|
1 |
|
$params = [ ]; |
|
39
|
1 |
|
$count = \count ( $fields ); |
|
40
|
1 |
|
$db = DAO::getDb ( $model ); |
|
41
|
1 |
|
$like = $db->getSpecificSQL ( 'tostring' ) . ' LIKE '; |
|
42
|
1 |
|
SqlUtils::$quote = $db->quote; |
|
43
|
1 |
|
if ($words !== false) { |
|
44
|
1 |
|
$words = array_filter ( $words, 'strlen' ); |
|
45
|
1 |
|
$condition = "(" . SqlUtils::getSearchWhere ( $like, $fields, '?', '', '' ) . ")"; |
|
46
|
1 |
|
foreach ( $words as $word ) { |
|
47
|
1 |
|
$word = trim ( $word ); |
|
48
|
1 |
|
$params = [ ...$params,...(\array_fill ( 0, $count, "%{$word}%" )) ]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$condition = str_replace ( "||", " OR ", $condition ); |
|
52
|
1 |
|
$condition = str_replace ( "&&", " AND ", $condition ); |
|
53
|
1 |
|
$condition = '(' . $condition . ') AND ' . $initialCondition . ''; |
|
54
|
|
|
} else { |
|
55
|
|
|
$condition = $initialCondition; |
|
56
|
|
|
} |
|
57
|
1 |
|
return DAO::getAll ( $model, $condition, false, $params ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
public static function update($instance, $values, $setValues = true, $updateMany = true) { |
|
61
|
3 |
|
$className = \get_class ( $instance ); |
|
62
|
3 |
|
$fieldsInRelationForUpdate = OrmUtils::getFieldsInRelationsForUpdate_ ( $className ); |
|
63
|
3 |
|
$manyToOneRelations = $fieldsInRelationForUpdate ["manyToOne"]; |
|
64
|
3 |
|
$manyToManyRelations = $fieldsInRelationForUpdate ["manyToMany"]; |
|
65
|
|
|
|
|
66
|
3 |
|
$members = array_keys ( $values ); |
|
67
|
3 |
|
OrmUtils::setFieldToMemberNames ( $members, $fieldsInRelationForUpdate ["relations"] ); |
|
68
|
3 |
|
$update = false; |
|
69
|
|
|
|
|
70
|
3 |
|
$fieldTypes = OrmUtils::getFieldTypes ( $className ); |
|
71
|
3 |
|
foreach ( $fieldTypes as $property => $type ) { |
|
72
|
3 |
|
if ($type == "tinyint(1)") { |
|
73
|
|
|
if (isset ( $values [$property] )) { |
|
74
|
|
|
$values [$property] = 1; |
|
75
|
|
|
} else { |
|
76
|
|
|
$values [$property] = 0; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
3 |
|
if ($setValues) { |
|
81
|
3 |
|
URequest::setValuesToObject ( $instance, $values ); |
|
82
|
|
|
} |
|
83
|
3 |
|
if ($manyToOneRelations) { |
|
84
|
|
|
self::updateManyToOne ( $manyToOneRelations, $members, $className, $instance, $values ); |
|
85
|
|
|
} |
|
86
|
3 |
|
if (isset ( $instance )) { |
|
87
|
3 |
|
if ($instance->_new) { |
|
88
|
1 |
|
$update = DAO::insert ( $instance ); |
|
89
|
|
|
} else { |
|
90
|
2 |
|
$update = DAO::update ( $instance ); |
|
91
|
2 |
|
if (DbCache::$active) { |
|
92
|
|
|
// TODO update dbCache |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
3 |
|
if ($updateMany && $update && $manyToManyRelations) { |
|
96
|
|
|
self::updateManyToMany ( $manyToManyRelations, $members, $className, $instance, $values ); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
3 |
|
return $update; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected static function updateManyToOne($manyToOneRelations, $members, $className, $instance, $values) { |
|
103
|
|
|
foreach ( $manyToOneRelations as $member ) { |
|
104
|
|
|
if (array_search ( $member, $members ) !== false) { |
|
105
|
|
|
$joinColumn = OrmUtils::getAnnotationInfoMember ( $className, "#joinColumn", $member ); |
|
106
|
|
|
if ($joinColumn) { |
|
107
|
|
|
$fkClass = $joinColumn ["className"]; |
|
108
|
|
|
$fkField = $joinColumn ["name"]; |
|
109
|
|
|
if (isset ( $values [$fkField] )) { |
|
110
|
|
|
$fkObject = DAO::getById ( $fkClass, $values ["$fkField"] ); |
|
111
|
|
|
Reflexion::setMemberValue ( $instance, $member, $fkObject ); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected static function updateManyToMany($manyToManyRelations, $members, $className, $instance, $values) { |
|
119
|
|
|
foreach ( $manyToManyRelations as $member ) { |
|
120
|
|
|
if (array_search ( $member, $members ) !== false) { |
|
121
|
|
|
if (($annot = OrmUtils::getAnnotationInfoMember ( $className, "#manyToMany", $member )) !== false) { |
|
122
|
|
|
$newField = $member . "Ids"; |
|
123
|
|
|
$fkClass = $annot ["targetEntity"]; |
|
124
|
|
|
$fkObjects = DAO::getAll ( $fkClass, self::getMultiWhere ( $values [$newField], $className ) ); |
|
125
|
|
|
if (Reflexion::setMemberValue ( $instance, $member, $fkObjects )) { |
|
126
|
|
|
DAO::insertOrUpdateManyToMany ( $instance, $member ); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
7 |
|
private static function getPks($model) { |
|
134
|
7 |
|
$instance = new $model (); |
|
135
|
7 |
|
return OrmUtils::getKeyFields ( $instance ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private static function getMultiWhere($ids, $class) { |
|
139
|
|
|
$pk = OrmUtils::getFirstKey ( $class ); |
|
140
|
|
|
$ids = explode ( ",", $ids ); |
|
141
|
|
|
if (sizeof ( $ids ) < 1) |
|
142
|
|
|
return ""; |
|
143
|
|
|
$strs = [ ]; |
|
144
|
|
|
$idCount = \sizeof ( $ids ); |
|
145
|
|
|
for($i = 0; $i < $idCount; $i ++) { |
|
146
|
|
|
$strs [] = $pk . "='" . $ids [$i] . "'"; |
|
147
|
|
|
} |
|
148
|
|
|
return implode ( " OR ", $strs ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
3 |
|
public static function getFkIntance($instance, $model, $member, $included = false) { |
|
152
|
3 |
|
$result = [ ]; |
|
153
|
3 |
|
if (($annot = OrmUtils::getAnnotationInfoMember ( $model, "#oneToMany", $member )) !== false) { |
|
154
|
3 |
|
$objectFK = DAO::getOneToMany ( $instance, $member, $included ); |
|
155
|
3 |
|
$fkClass = $annot ["className"]; |
|
156
|
|
|
} elseif (($annot = OrmUtils::getAnnotationInfoMember ( $model, "#manyToMany", $member )) !== false) { |
|
157
|
|
|
$objectFK = DAO::getManyToMany ( $instance, $member ); |
|
158
|
|
|
$fkClass = $annot ["targetEntity"]; |
|
159
|
|
|
} else { |
|
160
|
|
|
$objectFK = Reflexion::getMemberValue ( $instance, $member ); |
|
161
|
|
|
if (! is_object ( $objectFK )) { |
|
162
|
|
|
$objectFK = DAO::getManyToOne ( $instance, $member, $included ); |
|
163
|
|
|
} |
|
164
|
|
|
if (isset ( $objectFK )) |
|
165
|
|
|
$fkClass = \get_class ( $objectFK ); |
|
166
|
|
|
} |
|
167
|
3 |
|
if (isset ( $fkClass )) { |
|
168
|
3 |
|
$fkTable = OrmUtils::getTableName ( $fkClass ); |
|
169
|
3 |
|
$result [$member] = compact ( "objectFK", "fkClass", "fkTable" ); |
|
170
|
|
|
} |
|
171
|
3 |
|
return $result; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
3 |
|
public static function getFKIntances($instance, $model, $included = false) { |
|
175
|
3 |
|
$result = [ ]; |
|
176
|
3 |
|
$relations = OrmUtils::getFieldsInRelations ( $model ); |
|
177
|
3 |
|
foreach ( $relations as $member ) { |
|
178
|
3 |
|
$fkInstance = self::getFkIntance ( $instance, $model, $member, $included ); |
|
179
|
3 |
|
if (sizeof ( $fkInstance ) > 0) { |
|
180
|
3 |
|
$result = array_merge ( $result, $fkInstance ); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
3 |
|
return $result; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|