Combu Server  3.1.1
PHP API Documentation
Inventory.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class Inventory extends DataClass {
11 
12  const TABLE_NAME = "Inventory";
13 
14  public $Id = 0;
15  public $IdAccount = 0;
16  public $IdApp = 0;
17  public $Name = "";
18  public $Quantity = 0;
19  public $CustomData = "";
20 
24  public function __construct($src = null, $stripSlashes = false) {
25  global $Database;
26  if ($src == null)
27  return;
28  if (is_array($src)) {
29  // Load by array
30  $this->_loadByRow($src, $stripSlashes);
31  } else if (is_numeric($src) && intval($src) > 0) {
32  // Load by Id
33  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
34  } else {
35  // Load by Name
36  $this->_loadFilter(self::GetTableName(__CLASS__), "Name = '" . $Database->Escape($src) . "' ORDER BY Id");
37  }
38  }
39 
48  public static function Load ($idAccount, $returnArray = false) {
49  global $AppId;
50  $where = sprintf("IdAccount = %d", $idAccount);
51  if ($AppId->IsValid()) {
52  $where .= sprintf(" AND IdApp = %d", $AppId->Id);
53  }
54  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where);
55  }
56 
66  public static function LoadOrCreate ($idAccount, $itemName, $returnArray = false) {
67  global $Database, $AppId;
68  // Load the existing record
69  $where = sprintf("IdAccount = %d AND Name = '%s'", $idAccount, $Database->Escape($itemName));
70  if ($AppId->IsValid()) {
71  $where .= sprintf(" AND IdApp = %d", $AppId->Id);
72  }
73  $recs = self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where);
74  if (count($recs) > 0) {
75  return $recs[0];
76  }
77  // If no record was found, then create new instance
78  $rec = new self();
79  $rec->IdAccount = $idAccount;
80  $rec->Name = $itemName;
81  return $rec;
82  }
83 
90  public function Save() {
91  global $Database, $AppId;
92  if ($this->Id > 0) {
93  $query = sprintf("UPDATE %s SET Quantity = %d, CustomData = '%s' WHERE Id = %d",
94  self::GetTableName(__CLASS__),
95  $this->Quantity,
96  $Database->Escape($this->CustomData),
97  $this->Id);
98  } else {
99  $query = sprintf("INSERT INTO %s (IdAccount, IdApp, Name, Quantity, CustomData) VALUES (%d, %d, '%s', %d, '%s')",
100  self::GetTableName(__CLASS__),
101  $this->IdAccount,
102  $AppId->Id,
103  $Database->Escape($this->Name),
104  $this->Quantity,
105  $Database->Escape($this->CustomData));
106  }
107  if ($Database->Query($query)) {
108  if ($this->Id < 1) {
109  $this->Id = $Database->InsertedId();
110  }
111  return TRUE;
112  }
113  return FALSE;
114  }
115 
121  public function Delete() {
122  if ($this->Id > 0) {
123  return $this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id);
124  }
125  return FALSE;
126  }
127 
131  public static function Prune() {
132  self::TruncateClass(__CLASS__);
133  }
134 }
static Prune()
Definition: Inventory.php:131
__construct($src=null, $stripSlashes=false)
Definition: Inventory.php:24
static LoadOrCreate($idAccount, $itemName, $returnArray=false)
Definition: Inventory.php:66
Definition: Account.php:3
static Load($idAccount, $returnArray=false)
Definition: Inventory.php:48