Completed
Push — master ( 997533...5f070c )
by Jean-Christophe
01:37
created

CRUDHelper::getFKIntances()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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