BxAlbumsDb.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Albums Albums
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxAlbumsDb extends BxBaseModTextDb
  15. {
  16. function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function associateFileWithContent($iContentId, $iFileId, $iProfileId, $sTitle, $sData = '', $sExif = '')
  21. {
  22. $sQuery = $this->prepare ("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE `content_id` = ?", $iContentId);
  23. $iOrder = 1 + (int)$this->getOne($sQuery);
  24. $sQuery = $this->prepare ("INSERT INTO `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` SET `content_id` = ?, `file_id` = ?, `author` = ?, `title` = ?, `data` = ?, `exif` = ?, `order` = ? ON DUPLICATE KEY UPDATE `title` = ?, `data` = ?, `exif` = ?", $iContentId, $iFileId, $iProfileId, $sTitle, $sData, $sExif, $iOrder, $sTitle, $sData, $sExif);
  25. return $this->res($sQuery);
  26. }
  27. public function deassociateFileWithContent($iContentId, $iFileId)
  28. {
  29. $aBindings = array();
  30. $sWhere = '';
  31. if ($iContentId) {
  32. $aBindings['content_id'] = $iContentId;
  33. $sWhere .= " AND `content_id` = :content_id";
  34. }
  35. if ($iFileId) {
  36. $aBindings['file_id'] = $iFileId;
  37. $sWhere .= " AND `file_id` = :file_id";
  38. }
  39. $sQuery = "DELETE FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE 1 " . $sWhere;
  40. return $this->query($sQuery, $aBindings);
  41. }
  42. public function getFileTitle($iFileId)
  43. {
  44. $sQuery = $this->prepare ("SELECT `title` FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE `file_id` = ?", $iFileId);
  45. return $this->getOne($sQuery);
  46. }
  47. public function getMediaInfoById($iMediaId)
  48. {
  49. $CNF = &$this->_oConfig->CNF;
  50. $sSelectClause = "`f2e`.*, `f`.`added`";
  51. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_FILES'] . "` AS `f` ON (`f`.`id` = `f2e`.`file_id`) INNER JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `e` ON (`e`.`id` = `f2e`.`content_id`)";
  52. if($CNF['PARAM_ORDER_BY_GHOSTS']) {
  53. $sSelectClause .= ", `g`.`order` as `gorder`";
  54. $sJoinClause .= $this->prepareAsString("INNER JOIN `sys_storage_ghosts` AS `g` ON `g`.`id`=`f2e`.`file_id` AND `g`.`content_id`=`f2e`.`content_id` AND `g`.`object`=?", $CNF['OBJECT_STORAGE']);
  55. }
  56. $sQuery = $this->prepare("SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_FILES2ENTRIES'] . "` AS `f2e` " . $sJoinClause . " WHERE `f2e`.`id` = ?", $iMediaId);
  57. return $this->getRow($sQuery);
  58. }
  59. public function getMediaInfoSimpleByFileId($iFileId)
  60. {
  61. $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE `file_id` = ?", $iFileId);
  62. return $this->getRow($sQuery);
  63. }
  64. public function getMediaCountByContentId($iContentId)
  65. {
  66. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE `content_id` = ? LIMIT 1", $iContentId);
  67. return $this->getOne($sQuery);
  68. }
  69. public function getMediaListByContentId($iContentId, $iLimit = false)
  70. {
  71. $sLimitQuery = '';
  72. $aBindings = array('id' => $iContentId);
  73. if ((int)$iLimit) {
  74. $sLimitQuery = ' LIMIT :limit';
  75. $aBindings = array_merge($aBindings, array('limit' => (int)$iLimit));
  76. }
  77. $sQuery = "SELECT * FROM `" . $this->_oConfig->CNF['TABLE_FILES2ENTRIES'] . "` WHERE `content_id` = :id ORDER BY `order`" . $sLimitQuery;
  78. return $this->getAll($sQuery, $aBindings);
  79. }
  80. function getMediaBy($aParams = array())
  81. {
  82. $CNF = &$this->_oConfig->CNF;
  83. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  84. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  85. $sSelectClause = "`{$CNF['TABLE_FILES2ENTRIES']}`.*";
  86. switch($aParams['type']) {
  87. case 'id':
  88. $aMethod['name'] = 'getRow';
  89. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  90. $sWhereClause .= " AND `{$CNF['TABLE_FILES2ENTRIES']}`.`id` = :id";
  91. break;
  92. case 'search_ids':
  93. $aMethod['name'] = 'getColumn';
  94. $sSelectClause = "`{$CNF['TABLE_FILES2ENTRIES']}`.`id`";
  95. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  96. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  97. elseif (!empty($aParams['per_page']))
  98. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  99. BxDolSearchExtended::processParams($aParams['search_params'], [
  100. 'table' => $CNF['TABLE_FILES2ENTRIES'],
  101. 'bindings' => &$aMethod['params'][1],
  102. 'where_clause' => &$sWhereClause,
  103. 'bindings_ref' => &$aMethod['params'][1],
  104. 'where_clause_ref' => &$sWhereClause
  105. ]);
  106. $sOrderClause .= "`{$CNF['TABLE_FILES2ENTRIES']}`.`id` ASC";
  107. break;
  108. case 'all_ids':
  109. $aMethod['name'] = 'getColumn';
  110. $sSelectClause = "`{$CNF['TABLE_FILES2ENTRIES']}`.`id`";
  111. $sOrderClause .= "`{$CNF['TABLE_FILES2ENTRIES']}`.`id` ASC";
  112. break;
  113. case 'all':
  114. $sOrderClause .= "`{$CNF['TABLE_FILES2ENTRIES']}`.`id` ASC";
  115. break;
  116. }
  117. if(!empty($sOrderClause))
  118. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  119. if(!empty($sLimitClause))
  120. $sLimitClause = 'LIMIT ' . $sLimitClause;
  121. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$CNF['TABLE_FILES2ENTRIES']}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  122. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  123. }
  124. public function updateMedia($aParamsSet, $aParamsWhere)
  125. {
  126. $CNF = &$this->_oConfig->CNF;
  127. if(empty($aParamsSet) || empty($aParamsWhere))
  128. return false;
  129. $sSql = "UPDATE `" . $CNF['TABLE_FILES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  130. return $this->query($sSql);
  131. }
  132. }
  133. /** @} */