Combu Server  3.1.1
PHP API Documentation
AppId.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class AppId extends DataClass {
11 
12  const TABLE_NAME = "AppId";
13 
14  public $Id = 0;
15  public $AppId = "";
16  public $Secret = "";
17  public $DateCreated = "";
18  public $Name = "";
19  public $Description = "";
20  public $Active = 0;
21 
26  public function __construct($src = null, $stripSlashes = false) {
27  global $Database;
28  if (empty($src)) {
29  return;
30  }
31  if (is_array($src)) {
32  // Load by array
33  $this->_loadByRow($src, $stripSlashes);
34  } else if (is_numeric($src)) {
35  // Load by Id
36  $this->_loadFilter(self::GetTableName(__CLASS__), sprintf("Id = %d", $src));
37  } else {
38  // Load by Id
39  $this->_loadFilter(self::GetTableName(__CLASS__), sprintf("AppId = '%s'", $Database->Escape($src)));
40  }
41  }
42 
48  public function Exists() {
49  global $Database;
50  $sql = "SELECT Id FROM " . self::GetTableName(__CLASS__) . " WHERE Name = '" . $Database->Escape($this->Name) . "'";
51  if ($this->Id > 0) {
52  $sql .= " AND Id <> " . $this->Id . "'";
53  }
54  $res = $Database->Query($sql);
55  if ($res) {
56  $row = $Database->FetchAssoc($res);
57  if ($row) {
58  return TRUE;
59  }
60  }
61  return FALSE;
62  }
63 
64  public function IsActive() {
65  return ($this->Active == 1);
66  }
67 
68  public function IsValid() {
69  return ($this->Id > 0 && strlen($this->AppId) > 0 && strcasecmp($this->AppId, "new") != 0);
70  }
71 
79  public static function GetApp($appId, $appSecret) {
80  global $Database;
81  if (!empty($appId) && !empty($appSecret)) {
82  $apps = DataClass::LoadRecords(sprintf("SELECT * FROM %s WHERE AppId = '%s'", self::GetTableName(__CLASS__), $Database->Escape($appId)), __CLASS__);
83  if (count($apps) > 0) {
84  $app = $apps[0];
85  if ($app->IsActive() && \strcmp($app->AppId, $appId) === 0 && \strcmp($app->Secret, $appSecret) === 0) {
86  return $app;
87  }
88  }
89  }
90  return NULL;
91  }
92 
102  public static function Load ($limit = null, $offset = null, &$count = null, $returnArray = false) {
103  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), NULL, "DateCreated DESC", $limit, $offset, $count);
104  }
105 
111  public function Save() {
112  global $Database;
113  if ($this->Id > 0) {
114  $query = sprintf("UPDATE %s SET Active = %d, Name = '%s', Description = '%s' WHERE Id = %d",
115  self::GetTableName(__CLASS__),
116  $this->Active,
117  $Database->Escape($this->Name),
118  $Database->Escape($this->Description),
119  $this->Id);
120  } else {
121  $this->Active = 1;
122  $this->AppId = Utils::NewGUID();
123  $this->Secret = Utils::NewGUID();
124  $this->DateCreated = Utils::GetCurrentDateTimeFormat();
125  $query = sprintf("INSERT INTO %s (Active, DateCreated, AppId, Secret, Name, Description) VALUES (%d, %s, '%s', '%s', '%s', '%s')",
126  self::GetTableName(__CLASS__),
127  $this->Active,
128  $Database->EscapeDate($this->DateCreated),
129  $Database->Escape($this->AppId),
130  $Database->Escape($this->Secret),
131  $Database->Escape($this->Name),
132  $Database->Escape($this->Description));
133  }
134  if ($Database->Query($query)) {
135  if ($this->Id <= 0) {
136  $this->Id = $Database->InsertedId();
137  }
138  return TRUE;
139  }
140  return FALSE;
141  }
142 
148  public function Delete() {
149  if ($this->Id > 0 && $this->_Delete(self::GetTableName(__CLASS__), sprintf("Id = %d", $this->Id))) {
150 
151  $this->_Delete(self::GetTableName(Account_App::class), sprintf("IdApp = %d", $this->Id));
152  $this->_Delete(self::GetTableName(News::class), sprintf("IdApp = %d", $this->Id));
153  $this->_Delete(self::GetTableName(AppCustomData::class), sprintf("IdApp = %d", $this->Id));
154  $this->_Delete(self::GetTableName(Inventory::class), sprintf("IdApp = %d", $this->Id));
155  $this->_Delete(self::GetTableName(SessionToken::class), sprintf("IdApp = %d", $this->Id));
156  $this->_Delete(self::GetTableName(IpBan::class), sprintf("IdApp = %d", $this->Id));
157 
158  $achievements = Achievement::Load($this->Id);
159  foreach ($achievements as $achievement) {
160  if ($achievement->IdApp == $this->Id) {
161  $achievement->Delete();
162  }
163  }
164 
165  $leaderboards = LeaderBoard::Load($this->Id);
166  foreach ($leaderboards as $leaderboard) {
167  if ($leaderboard->IdApp == $this->Id) {
168  $leaderboard->Delete();
169  }
170  }
171 
172  $newsletters = Newsletter::Load($this->Id);
173  foreach ($newsletters as $newsletter) {
174  if ($newsletter->IdApp == $this->Id) {
175  $newsletter->Delete();
176  }
177  }
178 
179  return TRUE;
180  }
181  return FALSE;
182  }
183 }
Exists()
Definition: AppId.php:48
static Load($limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: AppId.php:102
IsActive()
Definition: AppId.php:64
Definition: Account.php:3
IsValid()
Definition: AppId.php:68
static GetApp($appId, $appSecret)
Definition: AppId.php:79
__construct($src=null, $stripSlashes=false)
Definition: AppId.php:26