Combu Server  3.1.1
PHP API Documentation
UserFile.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
5 use Combu\Utils;
6 
12 class UserFile extends DataClass {
13 
14  const TABLE_NAME = "UserFile";
15 
16  public $Id = 0;
17  public $IdAccount = 0;
18  public $Name = "";
19  public $Url = "";
20  public $ShareType = SHARETYPE_EVERYBODY;
21  public $Likes = 0;
22  public $Views = 0;
23  public $CustomData = "";
24 
28  public function __construct($src = null, $stripSlashes = false) {
29  if (empty($src)) {
30  return;
31  }
32  if (is_array($src)) {
33  // Load by array
34  $this->_loadByRow($src, $stripSlashes);
35  } else if (is_numeric($src) && intval($src) > 0) {
36  // Load by Id
37  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
38  }
39  }
40 
52  public static function Load ($idAccount, $includeShared = false, $returnArray = false, $limit = NULL, $offset = NULL, &$count = NULL) {
53  // Load the files from account's storage, files shared by others and files shared by friends
54  $where = "IdAccount = " . $idAccount;
55  if ($includeShared) {
56  $where .= " OR ShareType = " . SHARETYPE_EVERYBODY .
57  " OR (ShareType = " . SHARETYPE_FRIENDS . " AND IdAccount IN (SELECT IdAccount FROM " . self::GetTableName(Friend::class) . " WHERE IdFriend = " . $idAccount . " AND State = " . FRIEND_STATE_ACCEPTED . "))";
58  }
59  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "Id", $limit, $offset, $count);
60  }
61 
69  public static function LoadByUrl($url) {
70  global $Database;
71  $where = sprintf("Url = '%s'", $Database->Escape($url));
72  $records = self::_load(self::GetTableName(__CLASS__), __CLASS__, $where, NULL, 1);
73  return (count($records) > 0 ? $records[0] : NULL);
74  }
75 
81  public function Save() {
82  global $Database;
83  if ($this->Id > 0) {
84  $query = sprintf("UPDATE %s SET Name = '%s', Url = '%s', ShareType = %d, CustomData = '%s' WHERE Id = %d",
85  self::GetTableName(__CLASS__),
86  $Database->Escape($this->Name),
87  $Database->Escape($this->Url),
88  $this->ShareType,
89  $Database->Escape($this->CustomData),
90  $this->Id);
91  } else {
92  $query = sprintf("INSERT INTO %s (IdAccount, Name, Url, ShareType, Likes, Views, CustomData) VALUES (%d, '%s', '%s', %d, 0, 0, '%s')",
93  self::GetTableName(__CLASS__),
94  $this->IdAccount,
95  $Database->Escape($this->Name),
96  $Database->Escape($this->Url),
97  $this->ShareType,
98  $Database->Escape($this->CustomData));
99  }
100  if ($Database->Query($query)) {
101  if ($this->Id < 1) {
102  $this->Id = $Database->InsertedId();
103  }
104  return TRUE;
105  }
106  return FALSE;
107  }
108 
114  public function Delete() {
115  if ($this->Id > 0) {
116  if ($this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id)) {
117  // Delete file activities
118  $this->_Delete(self::GetTableName(UserFileActivity::class), "IdFile = " . $this->Id);
119  // Try to delete the file uploaded
120  $filepath = UPLOAD_FOLDER . $this->Url;
121  if (file_exists($filepath)) {
122  try {
123  unlink($filepath);
124  } catch (Exception $ex) {
125  AppLog::Error("Could not delete UserFile uploaded: " . $ex->getMessage() . " (file: " . $filepath . ")");
126  }
127  }
128  return TRUE;
129  }
130  }
131  return FALSE;
132  }
133 
138  public static function Prune() {
139  // Load all files and call Delete() to clean the upload folder
140  $files = self::_load(self::GetTableName(__CLASS__), __CLASS__);
141  foreach ($files as $f) {
142  $f->Delete();
143  }
144  }
145 
152  public function AddLike () {
153  global $LoggedAccount, $Database;
154  if ($this->Id > 0) {
155  // Try to save the new count
156  $this->Likes++;
157  $query = sprintf("UPDATE %s SET Likes = %d WHERE Id = %d",
158  self::GetTableName(__CLASS__),
159  $this->Likes,
160  $this->Id);
161  if ($Database->Query($query)) {
162  // Add an activity log of the logged user
163  if ($LoggedAccount->IsLogged()) {
164  $activity = new UserFileActivity();
165  $activity->IdFile = $this->Id;
166  $activity->IdAccount = $LoggedAccount->Id;
167  $activity->Likes = 1;
168  $activity->Save();
169  }
170  return TRUE;
171  }
172  // If the SQL statement failed then bring back the old count
173  $this->Likes--;
174  }
175  return FALSE;
176  }
177 
184  public function AddView () {
185  global $LoggedAccount, $Database;
186  if ($this->Id > 0) {
187  // Try to save the new count
188  $this->Views++;
189  $query = sprintf("UPDATE %s SET Views = %d WHERE Id = %d",
190  self::GetTableName(__CLASS__),
191  $this->Views,
192  $this->Id);
193  if ($Database->Query($query)) {
194  // Add an activity log of the logged user
195  if ($LoggedAccount->IsLogged()) {
196  $activities = UserFileActivity::Load($this->Id, $LoggedAccount->Id);
197  if (count($activities) == 0) {
198  $activity = new UserFileActivity();
199  $activity->IdFile = $this->Id;
200  $activity->IdAccount = $LoggedAccount->Id;
201  } else {
202  $activity = $activities[0];
203  }
204  $activity->Views++;
205  $activity->Save();
206  }
207  return TRUE;
208  }
209  // If the SQL statement failed then bring back the old count
210  $this->Views--;
211  }
212  return FALSE;
213  }
214 
215  public function ToArray() {
216  $array = parent::ToArray();
217 
218  $array["FullUrl"] = Utils::GetUploadUrl($this->Url);
219 
220  $loggedFileData = UserFileActivity::Load($userFiles[$i]->Id, $LoggedAccount->Id);
221  $array["UserLikes"] = (count($loggedFileData) == 0 ? 0 : $loggedFileData[0]->Likes);
222  $array["UserViews"] = (count($loggedFileData) == 0 ? 0 : $loggedFileData[0]->Views);
223  $array["UserLastActivity"] = (count($loggedFileData) == 0 ? 0 : $loggedFileData[0]->LastActivity);
224 
225  return $array;
226  }
227 
228  public function ToArrayClient() {
229  $array = $this->ToArray();
230  $array["Url"] = $array["FullUrl"];
231  return $array;
232  }
233 
234  public function ToJsonClient() {
235  return json_encode($this->ToArrayClient());
236  }
237 }
static LoadByUrl($url)
Definition: UserFile.php:69
__construct($src=null, $stripSlashes=false)
Definition: UserFile.php:28
static Load($idAccount, $includeShared=false, $returnArray=false, $limit=NULL, $offset=NULL, &$count=NULL)
Definition: UserFile.php:52
Definition: Account.php:3
static Prune()
Definition: UserFile.php:138