12 const TABLE_NAME =
"Match";
15 public $IdTournament = 0;
17 public $DateCreation = NULL;
18 public $DateExpire = NULL;
25 public function __construct($src = null, $stripSlashes =
false) {
31 $this->_loadByRow($src, $stripSlashes);
32 }
else if (is_numeric($src) && intval($src) > 0) {
34 $this->_loadFilter(self::GetTableName(__CLASS__),
"Id = " . intval($src));
52 public static function Load($idTournament = 0, $idUser = 0, $activeOnly = FALSE, $title =
"", $customData = array(), $order = NULL, $limit = NULL, $offset = NULL, $count = NULL, $returnArray = FALSE) {
55 if ($idTournament > 0) {
56 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(IdTournament = %d)", $idTournament);
57 }
else if ($idTournament == -1) {
58 $where .= ($where ==
"" ?
"" :
" AND ") .
"(IdTournament = 0)";
62 $where .= ($where ==
"" ?
"" :
" AND ") .
"(DateExpire IS NULL OR DateExpire < CURRENT_DATE())" .
63 sprintf(
" AND (Finished = 0 AND Id IN (SELECT IdMatch FROM %s WHERE IdAccount = %d))", self::GetTableName(GameMatch_Account::class), $idUser);
65 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(Finished = 1 AND Id IN (SELECT IdMatch FROM %s WHERE IdAccount = %d))", self::GetTableName(GameMatch_Account::class), $idUser);
69 $where .= sprintf(
"(Title REGEXP '%s')", $Database->Escape($title));
71 if ($customData && is_array($customData)) {
73 foreach ($customData as $search_row) {
74 if (!is_array($search_row)) {
77 $key = $search_row[
"key"];
78 $op = $search_row[
"op"];
79 $value = $search_row[
"value"];
80 $whereCustom .= ($whereCustom ==
"" ?
"" :
" AND ") . sprintf(
"(DataKey = '%s' AND DataValue %s '%s')", $Database->Escape($key), $op, $Database->Escape($value));
83 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(Id IN (SELECT IdMatch FROM %s WHERE %s))", self::GetTableName(GameMatch_CustomData::class), $whereCustom);
89 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where, $order, $limit, $offset, $count);
100 $query = sprintf(
"UPDATE %s SET Title = '%s', Rounds = %d, DateExpire = %s, Finished = %d WHERE Id = %d",
101 self::GetTableName(__CLASS__),
102 $Database->Escape($this->Title),
104 $Database->EscapeDate($this->DateExpire),
108 $this->DateCreation = date(
"Y-m-d H:i:s");
109 $query = sprintf(
"INSERT INTO %s (IdTournament, Title, Rounds, DateCreation, DateExpire, Finished) VALUES (%d, '%s', %d, %s, %s, 0)",
110 self::GetTableName(__CLASS__),
112 $Database->Escape($this->Title),
114 $Database->EscapeDate($this->DateCreation),
115 $Database->EscapeDate($this->DateExpire));
117 if ($Database->Query($query)) {
119 $this->Id = $Database->InsertedId();
132 if ($this->_Delete(self::GetTableName(__CLASS__),
"Id = " . $this->Id)) {
133 $this->_Delete(self::GetTableName(GameMatch_CustomData::class),
"IdMatch = " . $this->Id);
135 $accounts = GameMatch_Account::Load($this->Id);
136 foreach ($accounts as $account) {
150 self::TruncateClass(__CLASS__);
151 self::TruncateClass(GameMatch_Account::class);
152 self::TruncateClass(GameMatch_CustomData::class);
153 self::TruncateClass(GameMatch_Round::class);
161 $accounts = GameMatch_Account::Load($this->Id);
162 foreach ($accounts as $a) {
163 $rounds = GameMatch_Round::Load($a->Id);
164 foreach ($rounds as $r) {
165 if (!$r->DateScore) {
175 $tournament =
new Tournament($this->IdTournament);
176 if ($tournament->Id > 0)
177 $tournament->CheckFinished();
183 $array = Utils::ObjectToArray($this);
185 $array[
"CustomData"] = array();
186 $customData = GameMatch_CustomData::Load($this->Id);
187 foreach ($customData as $data) {
188 $array[
"CustomData"][$data->DataKey] = $data->DataValue;
191 $array[
"Users"] = array();
192 $accounts = GameMatch_Account::Load($this->Id);
193 foreach ($accounts as $i) {
194 $array[
"Users"][] = $i->ToArray();
201 $array = $this->ToArray();
202 return json_encode($array);
static Load($idTournament=0, $idUser=0, $activeOnly=FALSE, $title="", $customData=array(), $order=NULL, $limit=NULL, $offset=NULL, $count=NULL, $returnArray=FALSE)
__construct($src=null, $stripSlashes=false)