item.factory.class.php 7.19 KB
Newer Older
1
2
3
4
5
6
7
<?php
require_once('./sys/constants.php');
require_once('./models/card.class.php');
require_once('./sys/db.class.php');//useless (called by Card)
class ItemFactory //a quick and dirty class…
{	//…to encapsulate some "complicated" frequent queries
	const ANY = 1;
8
	const ALL_CARDS = 6;
9
	const CARD_NOT_ME = 2;
10
	const CARD_FROM_LEXICON = 5;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	const VALID_RECORDING_NOT_ME = 3;
	const LIMBO_RECORDING_ME_IF_POSSIBLE = 4;
	private $db;
	private $user_id;//(int)for now the items are selected based on the user
	private $lang = self::ANY;//(string iso639-1) language played in
	private $themes = array();//(array(int))the themes sought after
	private $query;
	//we don't use the level and that's a shame…

	/**
	 * Constructors setters and that kind of stuff
	 */
	public function __construct($userId, $langue, $theme = false){
		$this->db = db::getInstance();
		$this->user_id = $userId;
		if(is_string($langue)){
			$this->lang = $langue;
		}
		else{
			throw new Exception($langue." is not a string.");
		}
		if(is_array($theme)){
			$this->themes = $theme;
		}
	}

37
	private function generate_query($queryType, $parameter = NULL, $forOne = true){
38
		$res = true;
39
40
41
42
43
44
		if($forOne){
			$forOne = "LIMIT 1";
		}
		else{
			$forOne="";
		}
45
46
47
48
49
50
51
52
53
		switch ($queryType) {
			case self::CARD_NOT_ME:
				$this->query = "SELECT `cartes`.`idCarte` as `zeId` FROM `cartes` WHERE
						`cartes`.`langue`='$this->lang'
						AND `cartes`.`idDruide` != '$this->user_id' 
						AND `cartes`.`idCarte` NOT IN (
							SELECT `enregistrement`.`carteID` as`cardId` FROM `enregistrement` WHERE `enregistrement`.`idOracle`='$this->user_id'
							UNION SELECT `enregistrement`.`carteID` as`cardId` FROM `arbitrage`,`enregistrement` WHERE `arbitrage`.`enregistrementID` = `enregistrement`.`enregistrementID` AND `arbitrage`.`idDruide`='$this->user_id'
							UNION SELECT `enregistrement`.`carteID` as`cardId` FROM `parties`,`enregistrement` WHERE `parties`.`enregistrementID` = `enregistrement`.`enregistrementID` AND `parties`.`idDevin`='$this->user_id'
54
						) ORDER BY RAND() $forOne;";
55
56
57
58
59
60
61
62
63
64
65
66
67
68
				break;
			case self::LIMBO_RECORDING_ME_IF_POSSIBLE:
				$this->query = "SELECT `enregistrement`.*, IF(
						`cartes`.`idDruide` = '$this->user_id'
						OR `enregistrement`.`idOracle`= '$this->user_id'
						OR `cartes`.`idCarte` IN (
							SELECT `enregistrement`.`carteID` FROM `enregistrement`,`arbitrage` WHERE `enregistrement`.`enregistrementID`=`arbitrage`.`enregistrementID` AND `arbitrage`.`idDruide` = '$this->user_id'
							UNION SELECT `enregistrement`.`carteID` FROM `enregistrement`,`parties` WHERE `enregistrement`.`enregistrementID`=`parties`.`enregistrementID` AND `parties`.`idDevin` = '$this->user_id'
					),1,0) as `poids`
					FROM `enregistrement`,`cartes` WHERE
						`enregistrement`.`idOracle` != '$this->user_id'
						AND `enregistrement`.`validation` = 'limbo'
						AND `enregistrement`.`carteID` = `cartes`.`idCarte`
						AND `cartes`.`langue` = '$this->lang'
69
					ORDER BY `poids` DESC, RAND() $forOne;";
70
71
72
73
74
75
76
77
78
79
80
81

				break;
			case self::VALID_RECORDING_NOT_ME:
				$this->query = "SELECT `enregistrement`.* FROM `enregistrement`,`cartes` WHERE
						`cartes`.`langue`='$this->lang'
						AND `cartes`.`idCarte` = `enregistrement`.`carteID`
						AND `cartes`.`idDruide` != '$this->user_id'
						AND `enregistrement`.`validation` = 'valid'
						AND `enregistrement`.`idOracle` != '$this->user_id' 
						AND `enregistrement`.`carteID` NOT IN (
							SELECT `enregistrement`.`carteID` as`cardId` FROM `arbitrage`,`enregistrement` WHERE `arbitrage`.`enregistrementID` = `enregistrement`.`enregistrementID` AND `arbitrage`.`idDruide`='$this->user_id'
							UNION SELECT `enregistrement`.`carteID` as`cardId` FROM `parties`,`enregistrement` WHERE `parties`.`enregistrementID` = `enregistrement`.`enregistrementID` AND `parties`.`idDevin`='$this->user_id'
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
						) ORDER BY RAND() $forOne;";
				break;
			case self::CARD_FROM_LEXICON: //ICITE tester la requête l'intégrer à innova class pour pouvoir allouer la variable de session et faire l'affichage en fonction de son existence(disabled ou non)  et de celle du dico (pas d'affichage)
				if(isset($parameter)){
					$this->query = "SELECT `cartes`.`idCarte` as `zeId` FROM `cartes` WHERE
						`cartes`.`langue`='$this->lang'
						AND `cartes`.`idDruide` != '$this->user_id' 
						AND `cartes`.`idCarte` NOT IN (
							SELECT `enregistrement`.`carteID` as`cardId` FROM `enregistrement` WHERE `enregistrement`.`idOracle`='$this->user_id'
							UNION SELECT `enregistrement`.`carteID` as`cardId` FROM `arbitrage`,`enregistrement` WHERE `arbitrage`.`enregistrementID` = `enregistrement`.`enregistrementID` AND `arbitrage`.`idDruide`='$this->user_id'
							UNION SELECT `enregistrement`.`carteID` as`cardId` FROM `parties`,`enregistrement` WHERE `parties`.`enregistrementID` = `enregistrement`.`enregistrementID` AND `parties`.`idDevin`='$this->user_id'
						)
						AND (  `cartes`.`mot` IN $parameter
							OR `cartes`.`idCarte` IN (SELECT `idCarte` FROM `mots_interdits` WHERE `mots_interdits`.`mot` IN $parameter)
						) ORDER BY RAND() $forOne;";
				}
				else{
					$res = false;
				}
101
				break;
102
103
104
105
106
			case self::ALL_CARDS:
				//partir sur cette piste ce serait peut être mieux : SELECT `cartes`.*, GROUP_CONCAT(`mots_interdits`.`mot` SEPARATOR '|') as `interdits` FROM `cartes`,`mots_interdits` WHERE `cartes`.`idCarte`=`mots_interdits`.`idCarte` GROUP BY `mots_interdits`.`idCarte` ORDER BY `cartes`.`langue`, `cartes`.`idCarte`
				//mais c'est un peu fatiguant…
				$this->query = "SELECT `idCarte` as `zeId` FROM `cartes` ORDER BY `langue`, `idCarte` $forOne";//le forOne servira pas, pour la cohérence
				break;
107
108
109
110
111
112
113
114
115
			default:
				$res = false;
				break;
		}
		return true;
	}

	//returns a card object if the query yields 1 result and the number
	//of result otherwise, this should only be 0
116
117
	public function get_card($queryType, $parameter = NULL){
		if($this->generate_query($queryType, $parameter)){
118
119
120
121
122
123
124
125
126
127
128
129
			$this->db->query($this->query);
			$res = $this->db->affected_rows();
			if($res == 1){
				$res = new Card($this->db->fetch_object()->zeId);
			}
		}
		else{
			throw new Exception("Not a proper query type '$queryType'.");
		}
		return $res;
	}

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	public function get_card_id($queryType, $parameter = NULL){
		if($this->generate_query($queryType, $parameter)){
			$this->db->query($this->query);
			if($this->db->affected_rows() == 1){
				$res = $this->db->fetch_object()->zeId;
			}
			else{
				$res = -1;
			}
		}
		else{
			throw new Exception("Not a proper query type '$queryType'.");
		}
		return $res;
	}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
	public function get_card_ids($queryType, $parameter = NULL){
		if($this->generate_query($queryType, $parameter, false)){
			$this->db->query($this->query);
			$res = $this->db->affected_rows();
			if($res >= 1){
				$res = array();
				while($id = $this->db->fetch_object()){
					array_push($res, $id->zeId);
				}
			}
		}
		else{
			throw new Exception("Not a proper query type '$queryType'.");
		}
		return $res;
	}

163
164
	public function get_recording($queryType, $parameter = NULL){
		if($this->generate_query($queryType,$parameter)){
165
166
167
168
169
170
171
172
173
174
175
176
177
			$this->db->query($this->query);
			$res = $this->db->affected_rows();
			if($res == 1){
				$res = $this->db->fetch_object();
			}
		}
		else{
			throw new Exception("Not a proper query type '$queryType'.");
		}
		return $res;
	}
}
?>