Completed
Push — master ( 5f0535...74f4b9 )
by Jean-Christophe
01:50
created

CRUDHelper   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 33
lcom 0
cbo 7
dl 0
loc 135
rs 9.3999
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifierFunction() 0 13 3
A search() 0 13 2
C update() 0 55 16
A getPks() 0 4 1
A getMultiWhere() 0 12 3
B getFkIntance() 0 19 5
A getFKIntances() 0 11 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){
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
		return DAO::getAll($model,$condition);
40
	}
41
	
42
	public static function update($instance,$values,$updateManyToOneInForm=true,$updateManyToManyInForm=false) {
43
		$update=false;
44
		$className=\get_class($instance);
45
		$relations=OrmUtils::getManyToOneFields($className);
46
		$fieldTypes=OrmUtils::getFieldTypes($className);
47
		foreach ( $fieldTypes as $property => $type ) {
48
			if ($type == "tinyint(1)") {
49
				if (isset($values[$property])) {
50
					$values[$property]=1;
51
				} else {
52
					$values[$property]=0;
53
				}
54
			}
55
		}
56
		URequest::setValuesToObject($instance, $values);
57
		foreach ( $relations as $member ) {
58
			if ($updateManyToOneInForm) {
59
				$joinColumn=OrmUtils::getAnnotationInfoMember($className, "#joinColumn", $member);
60
				if ($joinColumn) {
61
					$fkClass=$joinColumn["className"];
62
					$fkField=$joinColumn["name"];
63
					if (isset($values[$fkField])) {
64
						$fkObject=DAO::getOne($fkClass, $values["$fkField"]);
65
						Reflexion::setMemberValue($instance, $member, $fkObject);
66
					}
67
				}
68
			}
69
		}
70
		if (isset($instance)) {
71
			if ($instance->_new) {
72
				$update=DAO::insert($instance);
73
			} else {
74
				$update=DAO::update($instance);
75
				if (DbCache::$active) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
76
					// TODO update dbCache
77
				}
78
			}
79
			if ($update) {
80
				if ($updateManyToManyInForm) {
81
					$relations=OrmUtils::getManyToManyFields($className);
82
					foreach ( $relations as $member ) {
83
						if (($annot=OrmUtils::getAnnotationInfoMember($className, "#manyToMany", $member)) !== false) {
84
							$newField=$member . "Ids";
85
							$fkClass=$annot["targetEntity"];
86
							$fkObjects=DAO::getAll($fkClass, self::getMultiWhere($values[$newField], $className));
87
							if (Reflexion::setMemberValue($instance, $member, $fkObjects)) {
88
								DAO::insertOrUpdateManyToMany($instance, $member);
89
							}
90
						}
91
					}
92
				}
93
			}
94
		}
95
		return $update;
96
	}
97
	
98
	private static function getPks($model) {
99
		$instance=new $model();
100
		return OrmUtils::getKeyFields($instance);
101
	}
102
	
103
	private static function getMultiWhere($ids, $class) {
104
		$pk=OrmUtils::getFirstKey($class);
105
		$ids=explode(",", $ids);
106
		if (sizeof($ids) < 1)
107
			return "";
108
		$strs=[ ];
109
		$idCount=\sizeof($ids);
110
		for($i=0; $i < $idCount; $i++) {
111
			$strs[]=$pk . "='" . $ids[$i] . "'";
112
		}
113
		return implode(" OR ", $strs);
114
	}
115
	
116
	public static function getFkIntance($instance,$model,$member){
117
		$result=[];
118
		if (($annot=OrmUtils::getAnnotationInfoMember($model, "#oneToMany", $member)) !== false) {
119
			$objectFK=DAO::getOneToMany($instance, $member);
120
			$fkClass=$annot["className"];
121
		} elseif (($annot=OrmUtils::getAnnotationInfoMember($model, "#manyToMany", $member)) !== false) {
122
			$objectFK=DAO::getManyToMany($instance, $member);
123
			$fkClass=$annot["targetEntity"];
124
		} else {
125
			$objectFK=Reflexion::getMemberValue($instance, $member);
126
			if (isset($objectFK))
127
				$fkClass=\get_class($objectFK);
128
		}
129
		if(isset($fkClass)){
130
			$fkTable=OrmUtils::getTableName($fkClass);
131
			$result[$member]=compact("objectFK","fkClass","fkTable");
132
		}
133
		return $result;
134
	}
135
	
136
	public static function getFKIntances($instance,$model){
137
		$result=[];
138
		$relations=OrmUtils::getFieldsInRelations($model);
139
		foreach ( $relations as $member ) {
140
			$fkInstance=self::getFkIntance($instance, $model, $member);
141
			if(sizeof($fkInstance)>0){
142
				$result=array_merge($result,$fkInstance);
143
			}
144
		}
145
		return $result;
146
	}
147
}
148
149