Cheetah
ChWsbFilesDb.php
Go to the documentation of this file.
1 <?php
2 
8 require_once('ChWsbModuleDb.php');
9 
11 {
12  var $_oConfig;
13 
14  var $iViever;
15  // main files table
17  // table of favorites
19 
20  // array of files table's fields
22  // array of favorite files table's fields
24 
25  /*
26  * Constructor.
27  */
28  function __construct (&$oConfig)
29  {
30  parent::__construct($oConfig);
31  $this->_oConfig = &$oConfig;
32  $this->iViewer = getLoggedId();
33  $sPrefix = $this->_oConfig->getMainPrefix();
34  if($sPrefix == 'ch_sounds') {
35  $this->aFileFields = array(
36  'medID' => 'ID',
37  'Categories'=> 'Categories',
38  'medProfId'=> 'Owner',
39  'medTitle' => 'Title',
40  'thumbUrl' => 'ThumbUrl',
41  'medUri' => 'Uri',
42  'medDesc' => 'Description',
43  'medTags' => 'Tags',
44  'medDate' => 'Date',
45  'medViews' => 'Views',
46  'Approved' => 'Status',
47  'Featured' => 'Featured',
48  'Rate' => 'Rate',
49  'RateCount' => 'RateCount',
50  );
51  } else {
52  $this->aFileFields = array(
53  'medID' => 'ID',
54  'Categories'=> 'Categories',
55  'medProfId'=> 'Owner',
56  'medTitle' => 'Title',
57  'medUri' => 'Uri',
58  'medDesc' => 'Description',
59  'medTags' => 'Tags',
60  'medDate' => 'Date',
61  'medViews' => 'Views',
62  'Approved' => 'Status',
63  'Featured' => 'Featured',
64  'Rate' => 'Rate',
65  'RateCount' => 'RateCount',
66  );
67  }
68 
69  $this->aFavoriteFields = array(
70  'fileId' => 'ID',
71  'ownerId' => 'Profile',
72  'favDate' => 'Date'
73  );
74  }
75 
76  function _changeFileCondition ($iFile, $sField, $sValue)
77  {
78  $iFile = (int)$iFile;
79  $sqlQuery = "UPDATE `{$this->sFileTable}` SET `$sField` = '$sValue' WHERE `{$this->aFileFields['medID']}`='$iFile'";
80  return $this->query($sqlQuery);
81  }
82 
83  function approveFile ($iFile)
84  {
85  return $this->_changeFileCondition($iFile, $this->aFileFields['Approved'], 'approved');
86  }
87 
88  function disapproveFile ($iFile)
89  {
90  return $this->_changeFileCondition($iFile, $this->aFileFields['Approved'], 'disapproved');
91  }
92 
93  function makeFeatured ($iFile)
94  {
95  return $this->_changeFileCondition($iFile, $this->aFileFields['Featured'], '1');
96  }
97 
98  function makeUnFeatured ($iFile)
99  {
100  return $this->_changeFileCondition($iFile, $this->aFileFields['Featured'], '0');
101  }
102 
103  function addToFavorites ($iFile)
104  {
105  $iFile = (int)$iFile;
106  $bRes = false;
107  if ($iFile > 0) {
108  $sqlQuery = "INSERT INTO `{$this->sFavoriteTable}` (`{$this->aFavoriteFields['fileId']}`, `{$this->aFavoriteFields['ownerId']}`, `{$this->aFavoriteFields['favDate']}`)
109  VALUES('$iFile', '{$this->iViewer}', '" . time() . "')";
110  $iRes = (int)$this->query($sqlQuery);
111  if ($iRes > 0)
112  $bRes = true;
113  }
114  return $bRes;
115  }
116 
117  function removeFromFavorites ($iFile)
118  {
119  $iFile = (int)$iFile;
120  $bRes = false;
121  if ($iFile > 0) {
122  $sqlQuery = "DELETE FROM `{$this->sFavoriteTable}`
123  WHERE `{$this->aFavoriteFields['fileId']}`='$iFile'
124  AND `{$this->aFavoriteFields['ownerId']}`='{$this->iViewer}'";
125  $iRes = (int)$this->query($sqlQuery);
126  if ($iRes != 1)
127  $bRes = true;
128  }
129  return $bRes;
130  }
131 
132  function checkFavoritesIn ($iFile)
133  {
134  $iFile = (int)$iFile;
135  $sqlCheck = "SELECT COUNT(*) FROM `{$this->sFavoriteTable}`
136  WHERE `{$this->aFavoriteFields['fileId']}`='$iFile'
137  AND `{$this->aFavoriteFields['ownerId']}`='{$this->iViewer}'";
138  $iCheck = (int)$this->getOne($sqlCheck);
139  return $iCheck == 0 ? false : true;
140  }
141 
142  function getFavorites ($iMember, $iFrom = 0, $iPerPage = 10)
143  {
144  $iMember = (int)$iMember;
145  $iFrom = (int)$iFrom;
146  $iPerPage = (int)$iPerPage;
147  $sqlQuery = "SELECT `{$this->aFavoriteFields['fileId']}` FROM `{$this->sFavoriteTable}` WHERE `{$this->aFavoriteFields['ownerId']}`= ? LIMIT $iFrom, $iPerPage";
148  return $this->getAll($sqlQuery, [$iMember]);
149  }
150 
151  function getFavoritesCount ($iFile)
152  {
153  $iFile = (int)$iFile;
154  $sqlQuery = "SELECT COUNT(*) FROM `{$this->sFavoriteTable}` WHERE `{$this->aFavoriteFields['fileId']}`='$iFile'";
155  return (int)$this->getOne($sqlQuery);
156  }
157 
158  function getFilesByMonth ($iYear, $iMonth, $iNextYear, $iNextMonth, $sStatus = 'approved')
159  {
160  $aFields = array('medID', 'Categories', 'medProfId', 'medTitle', 'medUri', 'Rate');
162  foreach ($aFields as $sValue) {
163  if (isset($this->aFileFields[$sValue]))
164  $sqlFields .= "a.`{$this->aFileFields[$sValue]}`, ";
165  }
166  $sqlQuery = "SELECT $sqlFields DAYOFMONTH(FROM_UNIXTIME(a.`{$this->aFileFields['medDate']}`)) AS `Day`, c.*
167  FROM `{$this->sFileTable}` as a
168  LEFT JOIN `sys_albums_objects` as b ON b.`id_object` = a.`{$this->aFileFields['medID']}`
169  INNER JOIN `sys_albums` as c ON c.`ID` = b.`id_album` AND c.`Type` = '{$this->_oConfig->getMainPrefix()}'
170  WHERE a.`{$this->aFileFields['medDate']}` >= UNIX_TIMESTAMP('$iYear-$iMonth-1')
171  AND a.`{$this->aFileFields['medDate']}` < UNIX_TIMESTAMP('$iNextYear-$iNextMonth-1')
172  AND a.`{$this->aFileFields['Approved']}` = '$sStatus'";
173 
174  $sqlQuery .= " AND c.`AllowAlbumView` ";
175  if ($this->iViewer)
176  $sqlQuery .= "<> " . CH_WSB_PG_HIDDEN;
177  else
178  $sqlQuery .= "= " . CH_WSB_PG_ALL;
179  return $this->getAll ($sqlQuery);
180  }
181 
182  function insertData ($aData)
183  {
184  if ($this->_setData($aData))
185  return $this->lastId();
186  }
187 
188  //update db action
189  function updateData ($iFileId, $aData)
190  {
191  if ($this->_setData($aData, $iFileId))
192  return true;
193  else
194  return false;
195  }
196 
197  // update/insert function
198  function _setData ($aData, $iFileId = 0)
199  {
200  $sqlCond = "";
201  $iFileId = (int)$iFileId;
202  if ($iFileId > 0) {
203  $sqlQuery = "UPDATE";
204  $sqlCond = " WHERE `{$this->aFileFields['medID']}`='$iFileId'";
205  } else {
206  $sqlQuery = "INSERT INTO ";
207  // spec key field
208  $aData['Hash'] = md5(microtime());
209  }
210  $sqlQuery .= "`{$this->sFileTable}` SET ";
211  foreach ($aData as $sKey => $sValue) {
212  if (array_key_exists($sKey, $this->aFileFields))
213  $sqlQuery .= "`{$this->aFileFields[$sKey]}`='" . process_db_input($sValue, CH_TAGS_STRIP) . "', ";
214  }
215  return $this->query(trim($sqlQuery, ', ') . $sqlCond);
216  }
217 
218  function deleteData ($iFile)
219  {
220  $iFile = (int)$iFile;
221  //delete from favorites
222  $sqlQuery = "DELETE FROM `{$this->sFavoriteTable}` WHERE `{$this->aFavoriteFields['fileId']}`='$iFile'";
223  $this->query($sqlQuery);
224  //delete from main table
225  $sqlQuery = "DELETE FROM `{$this->sFileTable}` WHERE `{$this->aFileFields['medID']}`='$iFile'";
226  return $this->query($sqlQuery);
227  }
228 
229  function getFilesCountByParams (&$aParams)
230  {
231  $sqlWhere = "1";
232  $sqlJoin = "";
233  foreach ($aParams as $sField => $mixedParam) {
234  $sParam = process_db_input($sParam, CH_TAGS_STRIP);
235  switch ($sField) {
236  case 'albumID':
237  $oAlbum = new ChWsbAlbums($this->_oConfig->getMainPrefix(), $this->iViever);
238  $sqlJoin = "LEFT JOIN `{$oAlbum->sAlbumObjectsTable}` ON `{$oAlbum->sAlbumObjectsTable}`.`id_object`=`{$this->sFileTable}`.`{$this->aFileFields['medID']}`";
239  $sqlWhere .= " AND `{$oAlbum->sAlbumObjectsTable}`.`id_album` = " . (int)$mixedParam;
240  break;
241  case 'albumUri':
242  case 'albumCaption':
243  $oAlbum = new ChWsbAlbums($this->_oConfig->getMainPrefix(), $this->iViever);
244  $sqlJoin = "LEFT JOIN `{$oAlbum->sAlbumObjectsTable}` ON `{$oAlbum->sAlbumObjectsTable}`.`id_object`=`{$this->sFileTable}`.`{$this->aFileFields['medID']}`
245  LEFT JOIN `{$oAlbum->sAlbumTable}` ON `{$oAlbum->sAlbumTable}`.`ID`=`{$oAlbum->sAlbumObjectsTable}`.`id_album`";
246  $sFieldName = str_replace('album', '', $sField);
247  $sqlWhere .= " AND `{$oAlbum->sAlbumTable}`.`Type` = '{$this->_oConfig->getMainPrefix()}' ";
248  if (in_array($sFieldName, $oAlbum->aAlbumFields))
249  $sqlWhere .= "AND `{$oAlbum->sAlbumTable}`.`$sFieldName` = '$mixedParam'";
250  break;
251  default:
252  if (isset($this->aFileFields[$sField])) {
253  $mixedParam = is_array($mixedParam) ? $mixedParam : array($mixedParam);
254  $sqlList = "";
255  foreach ($mixedParam as $sParam)
256  $sqlList .= "'$sParam', ";
257  $sqlList = trim($sqlList, ', ');
258  $sqlWhere .= " AND `{$this->sFileTable}`.`{$this->aFileFields[$sField]}` IN($sqlList)";
259  }
260  break;
261  }
262  }
263  $sqlQuery = "SELECT COUNT(`{$this->sFileTable}`.`{$this->aFileFields['medID']}`) FROM `{$this->sFileTable}` $sqlJoin WHERE $sqlWhere";
264  return $this->getOne($sqlQuery);
265  }
266 
267  function getFilesCountByAuthor ($iProfileId, $sStatus = 'approved')
268  {
269  $iProfileId = (int)$iProfileId;
271  if ($iProfileId > 0) {
272  $sqlQuery = "SELECT COUNT(`{$this->aFileFields['medID']}`) FROM `{$this->sFileTable}` WHERE `{$this->aFileFields['medProfId']}`='$iProfileId' AND `{$this->aFileFields['Approved']}`='$sStatus'";
273  return $this->getOne($sqlQuery);
274  }
275  }
276 
278  {
279  $iProfileId = (int)$iProfileId;
280  if ($iProfileId > 0) {
281  $sqlQuery = "SELECT `{$this->aFileFields['medID']}` FROM `{$this->sFileTable}` WHERE `{$this->aFileFields['medProfId']}`='$iProfileId'";
282  return $this->getPairs($sqlQuery, $this->aFileFields['medID'], $this->aFileFields['medID']);
283  }
284  }
285 
286  function getFileInfo ($aIdent, $bSimple = false, $aFields = array())
287  {
288  // TODO: dynamic pdo bindings
289  if (isset($aIdent['fileUri']))
290  $sqlCondition = "`{$this->sFileTable}`.`{$this->aFileFields['medUri']}`='" . process_db_input($aIdent['fileUri'], CH_TAGS_STRIP) . "'";
291  elseif (isset($aIdent['fileId']))
292  $sqlCondition = "`{$this->sFileTable}`.`{$this->aFileFields['medID']}`='" . (int)$aIdent['fileId'] . "'";
293  else
294  return;
295 
296  if (empty($aFields))
297  $aFields = array_keys($this->aFileFields);
298 
299  $sqlFields = "";
300  foreach ($aFields as $sValue) {
301  if (isset($this->aFileFields[$sValue]))
302  $sqlFields .= "`{$this->sFileTable}`.`{$this->aFileFields[$sValue]}` as `$sValue`, ";
303  }
304 
305  if (!$bSimple) {
306  // album joins
307  $oAlbum = new ChWsbAlbums($this->_oConfig->getMainPrefix());
308  $sqlAlbumJoin = "
309  INNER JOIN `{$oAlbum->sAlbumObjectsTable}` ON `{$oAlbum->sAlbumObjectsTable}`.`id_object`=`{$this->sFileTable}`.`{$this->aFileFields['medID']}`
310  INNER JOIN `{$oAlbum->sAlbumTable}` ON (`{$oAlbum->sAlbumTable}`.`ID`=`{$oAlbum->sAlbumObjectsTable}`.`id_album` AND `{$oAlbum->sAlbumTable}`.`Type`='" . $this->_oConfig->getMainPrefix() . "')
311  ";
312  $sqlAlbumFields = "`{$oAlbum->sAlbumTable}`.`ID` as `albumId`, `{$oAlbum->sAlbumTable}`.`Caption` as `albumCaption`, `{$oAlbum->sAlbumTable}`.`Uri` as `albumUri`, `{$oAlbum->sAlbumTable}`.`AllowAlbumView`, `{$oAlbum->sAlbumObjectsTable}`.`obj_order`";
313 
314  $sqlCount = "COUNT(`share1`.`{$this->aFileFields['medID']}`) as `Count`, ";
315  $sqlCountJoin = "LEFT JOIN `{$this->sFileTable}` as `share1` USING (`{$this->aFileFields['medProfId']}`)";
316  $sqlGroup = "GROUP BY `share1`.`{$this->aFileFields['medProfId']}`";
317  } else
318  $sqlFields = rtrim($sqlFields, ', ');
319  $sqlQuery = "SELECT $sqlFields $sqlCount $sqlAlbumFields
320  FROM `{$this->sFileTable}`
321  $sqlCountJoin
322  $sqlAlbumJoin
323  WHERE $sqlCondition $sqlGroup LIMIT 1";
324  return $this->getRow($sqlQuery);
325  }
326 
328  {
329  }
330 
331  function getMemberList ($sArg)
332  {
333  $sArg = process_db_input($sArg, CH_TAGS_STRIP);
334  $sqlQuery = "SELECT `ID`, `NickName` FROM `Profiles` WHERE `NickName` LIKE ?";
335  return $this->getAll($sqlQuery, ["{$sArg}%"]);
336  }
337 }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
ChWsbFilesDb\updateData
updateData($iFileId, $aData)
Definition: ChWsbFilesDb.php:189
ChWsbFilesDb\getFilesCountByParams
getFilesCountByParams(&$aParams)
Definition: ChWsbFilesDb.php:229
ChWsbFilesDb\makeUnFeatured
makeUnFeatured($iFile)
Definition: ChWsbFilesDb.php:98
ChWsbFilesDb\$sFavoriteTable
$sFavoriteTable
Definition: ChWsbFilesDb.php:18
ChWsbFilesDb\removeFromFavorites
removeFromFavorites($iFile)
Definition: ChWsbFilesDb.php:117
ChWsbFilesDb\getFavorites
getFavorites($iMember, $iFrom=0, $iPerPage=10)
Definition: ChWsbFilesDb.php:142
ChWsbFilesDb\addToFavorites
addToFavorites($iFile)
Definition: ChWsbFilesDb.php:103
php
CH_WSB_PG_HIDDEN
const CH_WSB_PG_HIDDEN
Definition: ChWsbPrivacy.php:17
ChWsbModuleDb
Definition: ChWsbModuleDb.php:12
ChWsbDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:206
ChWsbDb\getPairs
getPairs($sQuery, $sFieldKey, $sFieldValue, $aBindings=[])
Definition: ChWsbDb.php:363
ChWsbFilesDb\approveFile
approveFile($iFile)
Definition: ChWsbFilesDb.php:83
ChWsbFilesDb\getFileInfo
getFileInfo($aIdent, $bSimple=false, $aFields=array())
Definition: ChWsbFilesDb.php:286
ChWsbFilesDb\getFilesByMonth
getFilesByMonth($iYear, $iMonth, $iNextYear, $iNextMonth, $sStatus='approved')
Definition: ChWsbFilesDb.php:158
$iPerPage
else $iPerPage
Definition: browse.php:61
ChWsbFilesDb\_setData
_setData($aData, $iFileId=0)
Definition: ChWsbFilesDb.php:198
ChWsbFilesDb\getFavoritesCount
getFavoritesCount($iFile)
Definition: ChWsbFilesDb.php:151
ChWsbFilesDb\$aFavoriteFields
$aFavoriteFields
Definition: ChWsbFilesDb.php:23
ChWsbFilesDb\disapproveFile
disapproveFile($iFile)
Definition: ChWsbFilesDb.php:88
ChWsbDb\getRow
getRow($sQuery, $aBindings=[], $iFetchStyle=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:225
ChWsbFilesDb\makeFeatured
makeFeatured($iFile)
Definition: ChWsbFilesDb.php:93
$aFields
$aFields
Definition: preValues.php:19
ChWsbFilesDb\$sFileTable
$sFileTable
Definition: ChWsbFilesDb.php:16
getLoggedId
getLoggedId()
Definition: profiles.inc.php:32
ChWsbFilesDb\getMemberList
getMemberList($sArg)
Definition: ChWsbFilesDb.php:331
ChWsbFilesDb\getSettingsCategory
getSettingsCategory()
Definition: ChWsbFilesDb.php:327
ChWsbDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbDb.php:386
ChWsbAlbums
Definition: ChWsbAlbums.php:9
ChWsbFilesDb\$_oConfig
$_oConfig
Definition: ChWsbFilesDb.php:12
ChWsbFilesDb\$aFileFields
$aFileFields
Definition: ChWsbFilesDb.php:21
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
CH_WSB_PG_ALL
const CH_WSB_PG_ALL
Definition: ChWsbPrivacy.php:12
ChWsbDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbDb.php:263
ChWsbFilesDb\getFilesCountByAuthor
getFilesCountByAuthor($iProfileId, $sStatus='approved')
Definition: ChWsbFilesDb.php:267
ChWsbFilesDb\_changeFileCondition
_changeFileCondition($iFile, $sField, $sValue)
Definition: ChWsbFilesDb.php:76
CH_TAGS_STRIP
const CH_TAGS_STRIP
Definition: utils.inc.php:22
ChWsbFilesDb\checkFavoritesIn
checkFavoritesIn($iFile)
Definition: ChWsbFilesDb.php:132
ChWsbFilesDb\$iViever
$iViever
Definition: ChWsbFilesDb.php:14
ChWsbFilesDb\__construct
__construct(&$oConfig)
Definition: ChWsbFilesDb.php:28
$iFileId
$iFileId
Definition: embed.php:12
ChWsbFilesDb\deleteData
deleteData($iFile)
Definition: ChWsbFilesDb.php:218
ChWsbFilesDb\getFilesByAuthor
getFilesByAuthor($iProfileId)
Definition: ChWsbFilesDb.php:277
ChWsbFilesDb
Definition: ChWsbFilesDb.php:11
ChWsbDb\lastId
lastId()
Definition: ChWsbDb.php:449
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$sStatus
$sStatus
Definition: actions.inc.php:11
ChWsbFilesDb\insertData
insertData($aData)
Definition: ChWsbFilesDb.php:182
false
if(!defined("FALSE_VAL")) define("FALSE_VAL" false
Definition: constants.inc.php:9
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29