Passed
Push — master ( d36a99...d31551 )
by Jean-Christophe
06:01
created

CRUDHelper::getPks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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