Combu Server  3.1.1
PHP API Documentation
Account_Platform.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class Account_Platform extends DataClass {
11 
12  const TABLE_NAME = "Account_Platform";
13 
14  public $IdAccount = 0;
15  public $PlatformKey = "";
16  public $PlatformId = "";
17 
21  public function __construct($src = null, $stripSlashes = false) {
22  if (!$src)
23  return;
24  if (is_array($src)) {
25  // Load by array
26  $this->_loadByRow($src, $stripSlashes);
27  }
28  }
29 
36  public static function Load($idAccount) {
37  global $Database;
38  $where = sprintf("IdAccount = %d", $idAccount);
39  return self::_load(self::GetTableName(__CLASS__), __CLASS__, $where);
40  }
41 
42  public static function LoadAccount ($platformKey, $platformId, &$accountPlatform = NULL) {
43  global $Database;
44  $account = NULL;
45  $accountPlatform = NULL;
46  $query = sprintf("SELECT IdAccount FROM %s WHERE PlatformKey = '%s' AND PlatformId = '%s'",
47  self::GetTableName(__CLASS__),
48  $Database->Escape($platformKey),
49  $Database->Escape($platformId));
50  $rows = self::LoadRecords($query, __CLASS__);
51  if (count($rows) > 0) {
52  $accountPlatform = $rows[0];
53  // We have an account
54  $account = new Account($accountPlatform->IdAccount);
55  if ($account->Id < 1) {
56  $account = NULL;
57 
58  }
59  }
60  return $account;
61  }
62 
72  public static function LoadOrCreateAccount ($platformKey, $platformId, &$accountPlatform = NULL, &$justCreated = FALSE) {
73  global $Database;
74  $justCreated = FALSE;
75  $accountPlatform = NULL;
76  $account = self::LoadAccount($platformKey, $platformId, $accountPlatform);
77  if ($account == NULL) {
78  if ($accountPlatform) {
79  // Delete this record because the account is invalid
80  $accountPlatform->Delete();
81  $accountPlatform = NULL;
82  } else {
83  // Create new account
84  $account = new Account();
85  $account->Username = $platformKey . "_" . $platformId;
86  $account->Enabled = 1;
87  if ($account->Save()) {
88  $accountPlatform = new Account_Platform();
89  $accountPlatform->PlatformKey = $platformKey;
90  $accountPlatform->PlatformId = $platformId;
91  $accountPlatform->IdAccount = $account->Id;
92  if ($accountPlatform->Save()) {
93  $justCreated = TRUE;
94  } else {
95  $accountPlatform = NULL;
96  // Delete this record because it didn't save the Account-Platform object
97  $account->Delete();
98  $account = NULL;
99  }
100  } else {
101  $account = NULL;
102  }
103  }
104  }
105  return $account;
106  }
107 
113  public function Save() {
114  global $Database;
115  // Data integrity check
116  if ($this->IdAccount < 1 || !$this->PlatformKey || !$this->PlatformId) {
117  return FALSE;
118  }
119  $query = sprintf("REPLACE INTO %s (IdAccount, PlatformKey, PlatformId) VALUES (%d, '%s', '%s')",
120  self::GetTableName(__CLASS__),
121  $this->IdAccount,
122  $Database->Escape($this->PlatformKey),
123  $Database->Escape($this->PlatformId));
124  return $Database->Query($query);
125  }
126 
132  public function Delete() {
133  global $Database;
134  $where = sprintf("IdAccount = %d AND PlatformKey = '%s' AND PlatformId = '%s'",
135  $this->IdAccount,
136  $Database->Escape($this->PlatformKey),
137  $Database->Escape($this->PlatformId));
138  return $this->_Delete(self::GetTableName(__CLASS__), $where);
139  }
140 }
__construct($src=null, $stripSlashes=false)
static LoadOrCreateAccount($platformKey, $platformId, &$accountPlatform=NULL, &$justCreated=FALSE)
static LoadAccount($platformKey, $platformId, &$accountPlatform=NULL)
Definition: Account.php:3
static Load($idAccount)