14 const TABLE_NAME =
"UserFile";
17 public $IdAccount = 0;
20 public $ShareType = SHARETYPE_EVERYBODY;
23 public $CustomData =
"";
28 public function __construct($src = null, $stripSlashes =
false) {
34 $this->_loadByRow($src, $stripSlashes);
35 }
else if (is_numeric($src) && intval($src) > 0) {
37 $this->_loadFilter(self::GetTableName(__CLASS__),
"Id = " . intval($src));
52 public static function Load ($idAccount, $includeShared =
false, $returnArray =
false, $limit = NULL, $offset = NULL, &$count = NULL) {
54 $where =
"IdAccount = " . $idAccount;
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 .
"))";
59 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"Id", $limit, $offset, $count);
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);
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),
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__),
95 $Database->Escape($this->Name),
96 $Database->Escape($this->Url),
100 if ($Database->Query($query)) {
102 $this->Id = $Database->InsertedId();
116 if ($this->_Delete(self::GetTableName(__CLASS__),
"Id = " . $this->Id)) {
118 $this->_Delete(self::GetTableName(UserFileActivity::class),
"IdFile = " . $this->Id);
120 $filepath = UPLOAD_FOLDER . $this->Url;
121 if (file_exists($filepath)) {
124 }
catch (Exception $ex) {
125 AppLog::Error(
"Could not delete UserFile uploaded: " . $ex->getMessage() .
" (file: " . $filepath .
")");
140 $files = self::_load(self::GetTableName(__CLASS__), __CLASS__);
141 foreach ($files as $f) {
153 global $LoggedAccount, $Database;
157 $query = sprintf(
"UPDATE %s SET Likes = %d WHERE Id = %d",
158 self::GetTableName(__CLASS__),
161 if ($Database->Query($query)) {
163 if ($LoggedAccount->IsLogged()) {
165 $activity->IdFile = $this->Id;
166 $activity->IdAccount = $LoggedAccount->Id;
167 $activity->Likes = 1;
185 global $LoggedAccount, $Database;
189 $query = sprintf(
"UPDATE %s SET Views = %d WHERE Id = %d",
190 self::GetTableName(__CLASS__),
193 if ($Database->Query($query)) {
195 if ($LoggedAccount->IsLogged()) {
196 $activities = UserFileActivity::Load($this->Id, $LoggedAccount->Id);
197 if (count($activities) == 0) {
199 $activity->IdFile = $this->Id;
200 $activity->IdAccount = $LoggedAccount->Id;
202 $activity = $activities[0];
216 $array = parent::ToArray();
218 $array[
"FullUrl"] = Utils::GetUploadUrl($this->Url);
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);
229 $array = $this->ToArray();
230 $array[
"Url"] = $array[
"FullUrl"];
235 return json_encode($this->ToArrayClient());
__construct($src=null, $stripSlashes=false)
static Load($idAccount, $includeShared=false, $returnArray=false, $limit=NULL, $offset=NULL, &$count=NULL)