Combu Server  3.1.1
PHP API Documentation
Tournament.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class Tournament extends DataClass {
11 
12  const TABLE_NAME = "Tournament";
13 
14  public $Id = 0;
15  public $IdOwner = 0;
16  public $Title = "";
17  public $DateCreation = NULL;
18  public $DateFinished = NULL;
19  public $CustomData = "";
20  public $Finished = 0;
21 
25  public function __construct($src = null, $stripSlashes = false) {
26  global $Database;
27  if ($src == null)
28  return;
29  if (is_array($src)) {
30  // Load by array
31  $this->_loadByRow($src, $stripSlashes);
32  } else if (is_numeric($src) && intval($src) > 0) {
33  // Load by Id
34  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
35  }
36  }
37 
51  public static function Load($finished = FALSE, $title = NULL, $customData = array(), $order = NULL, $limit = NULL, $offset = NULL, $count = NULL, $returnArray = FALSE) {
52  global $Database;
53  $where = "";
54  if (!$finished) {
55  $where .= ($where ? " AND " : "") . "(DateFinished IS NULL)";
56  }
57  if ($title) {
58  $where .= sprintf(" AND (Title REGEXP '%s')", $Database->Escape($title));
59  }
60  if ($customData && is_array($customData)) {
61  $whereCustom = "";
62  foreach ($customData as $search_row) {
63  if (!is_array($search_row)) {
64  continue;
65  }
66  $key = $search_row["key"];
67  $op = $search_row["op"];
68  $value = $search_row["value"];
69  $whereCustom .= ($whereCustom == "" ? "" : " AND ") . sprintf("(DataKey = '%s' AND DataValue %s '%s')", $Database->Escape($key), $op, $Database->Escape($value));
70  }
71  if ($whereCustom) {
72  $where .= ($where == "" ? "" : " AND ") . sprintf("(Id IN (SELECT IdTournament FROM %s WHERE %s))", self::GetTableName(Tournament_CustomData::class), $whereCustom);
73  }
74  }
75  if (!$order) {
76  $order = "Id DESC";
77  }
78  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, $order, $limit, $offset, $count);
79  }
80 
86  public function Save() {
87  global $Database;
88  if ($this->Id > 0) {
89  $query = sprintf("UPDATE %s SET Title = '%s', DateFinished = %s, Finished = %d WHERE Id = %d",
90  self::GetTableName(__CLASS__),
91  $Database->Escape($this->Title),
92  $Database->EscapeDate($this->DateFinished),
93  $this->Finished,
94  $this->Id);
95  } else {
96  $this->DateCreation = date("Y-m-d H:i:s");
97  $this->DateFinished = NULL;
98  $query = sprintf("INSERT INTO %s (IdOwner, Title, DateCreation, DateFinished, Finished) VALUES (%d, '%s', %s, NULL, 0)",
99  self::GetTableName(__CLASS__),
100  $this->IdOwner,
101  $Database->Escape($this->Title),
102  $Database->EscapeDate($this->DateCreation));
103  }
104  if ($Database->Query($query)) {
105  if ($this->Id < 1) {
106  $this->Id = $Database->InsertedId();
107  }
108  return TRUE;
109  }
110  return FALSE;
111  }
112 
117  public function Delete() {
118  if ($this->Id > 0) {
119  if ($this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id)) {
120  // Delete Tournament Custom Data and all Matches
121  $this->_Delete(self::GetTableName(Tournament_CustomData::class), "IdTournament = " . $this->Id);
122  $matches = GameMatch::Load($this->Id);
123  foreach ($matches as $match) {
124  $match->Delete();
125  }
126  return TRUE;
127  }
128  }
129  return FALSE;
130  }
131 
136  public static function Prune() {
137  self::TruncateClass(__CLASS__);
138  self::TruncateClass(Tournament_CustomData::class);
139  GameMatch::Prune();
140  }
141 
145  public function CheckFinished() {
146  $allMatches = TRUE;
147  $matches = GameMatch::Load($this->Id);
148  foreach ($matches as $m) {
149  if ($m->Finished != 1) {
150  $allMatches = FALSE;
151  break;
152  }
153  }
154  if ($allMatches) {
155  $this->Finished = 1;
156  $this->Save();
157  }
158  }
159 
160  public function ToArray() {
161  $array = Utils::ObjectToArray($this);
162  // Add the Tournament Custom Data
163  $array["CustomData"] = array();
164  $customData = Tournament_CustomData::Load($this->Id);
165  foreach ($customData as $data) {
166  $array["CustomData"][$data->DataKey] = $data->DataValue;
167  }
168  // Add the Owner
169  $owner = new Account($this->IdOwner);
170  if ($owner->Id > 0) {
171  $array["Owner"] = $owner->ToJson();
172  }
173  // Add the Matches
174  $array["Matches"] = array();
175  $matches = GameMatch::Load($this->Id);
176  foreach ($matches as $i) {
177  $array["Matches"][] = $i->ToArray();
178  }
179  // Return the full array
180  return $array;
181  }
182 
183  public function ToJson() {
184  $array = $this->ToArray();
185  return json_encode($array);
186  }
187 }
__construct($src=null, $stripSlashes=false)
Definition: Tournament.php:25
Definition: Account.php:3
static Load($finished=FALSE, $title=NULL, $customData=array(), $order=NULL, $limit=NULL, $offset=NULL, $count=NULL, $returnArray=FALSE)
Definition: Tournament.php:51