12 const TABLE_NAME =
"Tournament";
17 public $DateCreation = NULL;
18 public $DateFinished = NULL;
19 public $CustomData =
"";
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));
51 public static function Load($finished = FALSE, $title = NULL, $customData = array(), $order = NULL, $limit = NULL, $offset = NULL, $count = NULL, $returnArray = FALSE) {
55 $where .= ($where ?
" AND " :
"") .
"(DateFinished IS NULL)";
58 $where .= sprintf(
" AND (Title REGEXP '%s')", $Database->Escape($title));
60 if ($customData && is_array($customData)) {
62 foreach ($customData as $search_row) {
63 if (!is_array($search_row)) {
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));
72 $where .= ($where ==
"" ?
"" :
" AND ") . sprintf(
"(Id IN (SELECT IdTournament FROM %s WHERE %s))", self::GetTableName(Tournament_CustomData::class), $whereCustom);
78 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where, $order, $limit, $offset, $count);
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),
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__),
101 $Database->Escape($this->Title),
102 $Database->EscapeDate($this->DateCreation));
104 if ($Database->Query($query)) {
106 $this->Id = $Database->InsertedId();
119 if ($this->_Delete(self::GetTableName(__CLASS__),
"Id = " . $this->Id)) {
121 $this->_Delete(self::GetTableName(Tournament_CustomData::class),
"IdTournament = " . $this->Id);
122 $matches = GameMatch::Load($this->Id);
123 foreach ($matches as $match) {
137 self::TruncateClass(__CLASS__);
138 self::TruncateClass(Tournament_CustomData::class);
147 $matches = GameMatch::Load($this->Id);
148 foreach ($matches as $m) {
149 if ($m->Finished != 1) {
161 $array = Utils::ObjectToArray($this);
163 $array[
"CustomData"] = array();
164 $customData = Tournament_CustomData::Load($this->Id);
165 foreach ($customData as $data) {
166 $array[
"CustomData"][$data->DataKey] = $data->DataValue;
169 $owner =
new Account($this->IdOwner);
170 if ($owner->Id > 0) {
171 $array[
"Owner"] = $owner->ToJson();
174 $array[
"Matches"] = array();
175 $matches = GameMatch::Load($this->Id);
176 foreach ($matches as $i) {
177 $array[
"Matches"][] = $i->ToArray();
184 $array = $this->ToArray();
185 return json_encode($array);
__construct($src=null, $stripSlashes=false)
static Load($finished=FALSE, $title=NULL, $customData=array(), $order=NULL, $limit=NULL, $offset=NULL, $count=NULL, $returnArray=FALSE)