BxDolCategories.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolTags');
  9. define("CATEGORIES_DIVIDER", ';');
  10. class BxDolCategories extends BxDolTags {
  11. var $sAutoApprovePrefix;
  12. function BxDolCategories ($iPossOwner = 0) {
  13. parent::BxDolTags();
  14. $this->iViewer = (int)$iPossOwner > 0 ? (int)$iPossOwner : $this->iViewer;
  15. $this->sCacheFile = 'sys_objects_categories';
  16. $this->sNonParseParams = 'tags_non_parsable';
  17. $this->sCacheTable = 'sys_objects_categories';
  18. $this->sTagTable = 'sys_categories';
  19. $this->aTagFields = array(
  20. 'id' => 'ID',
  21. 'type' => 'Type',
  22. 'tag' => 'Category',
  23. 'owner' => 'Owner',
  24. 'status' => 'Status',
  25. 'date' => 'Date'
  26. );
  27. $this->sAutoApprovePrefix = 'category_auto_app_';
  28. $this->bToLower = false;
  29. }
  30. function getGroupChooser ($sType, $iOwnerId = 0, $bForm = false, $sCustomValues = '') {
  31. $a = $this->getCategoriesList ($sType, $iOwnerId, $bForm);
  32. $a = array('' => _t('_Please_Select_')) + (is_array($a) ? $a : array());
  33. $aCustomValues = $this->explodeTags($sCustomValues);
  34. foreach($aCustomValues as $iIndex => $sValue)
  35. $a[$sValue] = $sValue;
  36. return array(
  37. 'type' => 'select_box',
  38. 'name' => 'Categories',
  39. 'caption' => _t('_Categories'),
  40. 'values' => $a,
  41. 'required' => true,
  42. 'checker' => array (
  43. 'func' => 'avail',
  44. 'error' => _t ('_sys_err_categories'),
  45. ),
  46. 'db' => array (
  47. 'pass' => 'Categories',
  48. ),
  49. );
  50. }
  51. function getCategoriesList ($sType, $iOwnerId = 0, $bForm = false) {
  52. $oDb = BxDolDb::getInstance();
  53. $this->getTagObjectConfig();
  54. $sType = array_key_exists($sType, $this->aTagObjects) === true ? $sType : 'bx_photos';
  55. $iOwnerId = (int)$iOwnerId;
  56. $sqlQuery = $oDb->prepare("SELECT `cat`.`{$this->aTagFields['tag']}`
  57. FROM `{$this->sTagTable}` `cat`
  58. WHERE (`cat`.`{$this->aTagFields['owner']}` = 0 OR `cat`.`{$this->aTagFields['owner']}` = ?)
  59. AND `cat`.`{$this->aTagFields['type']}` = ? AND `cat`.`{$this->aTagFields['status']}` = 'active'
  60. __sqlAdd__
  61. GROUP BY `cat`.`{$this->aTagFields['tag']}`", $iOwnerId, $sType);
  62. $aAddSql = array();
  63. if (getParam($this->sAutoApprovePrefix . $sType) != 'on')
  64. $aAddSql[] = " AND `cat`.`{$this->aTagFields['status']}` = 'active'";
  65. $sqlAdd = '';
  66. foreach ($aAddSql as $sValue)
  67. $sqlAdd .= $sValue;
  68. $sqlQuery = str_replace('__sqlAdd__', $sqlAdd, $sqlQuery);
  69. $a = $oDb->getAll($sqlQuery);
  70. foreach ($a as $aList) {
  71. if ($bForm)
  72. $aCatList[$aList[$this->aTagFields['tag']]] = $aList[$this->aTagFields['tag']];
  73. else
  74. $aCatList[] = $aList[$this->aTagFields['tag']];
  75. }
  76. return $aCatList;
  77. }
  78. function getTagList($aParam) {
  79. $oDb = BxDolDb::getInstance();
  80. $sLimit = '';
  81. $aTotalTags = array();
  82. $sGroupBy = "GROUP BY `{$this->aTagFields['tag']}`";
  83. if (isset($aParam['limit']))
  84. {
  85. $sLimit = 'LIMIT ';
  86. if (isset($aParam['start']))
  87. $sLimit .= (int)$aParam['start'] . ', ';
  88. $sLimit .= (int)$aParam['limit'];
  89. }
  90. $sCondition = $this->_getSelectCondition($aParam);
  91. if (isset($aParam['orderby']))
  92. {
  93. if ($aParam['orderby'] == 'popular')
  94. $sGroupBy .= " ORDER BY `count` DESC, `{$this->aTagFields['tag']}` ASC";
  95. else if ($aParam['orderby'] == 'recent')
  96. $sGroupBy .= " ORDER BY `{$this->aTagFields['date']}` DESC, `{$this->aTagFields['tag']}` ASC";
  97. }
  98. $sDiffCount = '';
  99. $sqlQuery = "SELECT
  100. `tgs`.`{$this->aTagFields['tag']}` as `{$this->aTagFields['tag']}`,
  101. `tgs`.`{$this->aTagFields['date']}` as `{$this->aTagFields['date']}`,
  102. COUNT(`tgs`.`{$this->aTagFields['id']}`) AS `count`
  103. FROM `{$this->sTagTable}` `tgs` $sCondition $sGroupBy $sLimit";
  104. $aTags = $oDb->getAll($sqlQuery);
  105. foreach ($aTags as $aTag) {
  106. if ((int)$aTag['count'] > 0)
  107. $aTotalTags[$aTag[$this->aTagFields['tag']]] = (int)$aTag['count'];
  108. }
  109. return $aTotalTags;
  110. }
  111. function getTagsCount($aParam) {
  112. $sCondition = $this->_getSelectCondition($aParam);
  113. $sqlQuery = "SELECT count(DISTINCT `tgs`.`{$this->aTagFields['tag']}`) AS `count` FROM
  114. `{$this->sTagTable}` `tgs` {$sCondition}";
  115. $oDb = BxDolDb::getInstance();
  116. return $oDb->getOne($sqlQuery);
  117. }
  118. function _getSelectCondition($aParam)
  119. {
  120. $sCondition = "WHERE `tgs`.`{$this->aTagFields['owner']}` != 0";
  121. if (!$aParam)
  122. return $sCondition;
  123. $oDb = BxDolDb::getInstance();
  124. if (isset($aParam['common'])) {
  125. $aUnitsCommon = $this->_getCommonCategories($aParam['type']);
  126. $sCondition .= " AND `tgs`.`{$this->aTagFields['tag']}` " . (!$aParam['common'] ? 'NOT' : '') .
  127. " IN (" . $oDb->implode_escape($aUnitsCommon) . ")";
  128. }
  129. if (isset($aParam['type']) && $aParam['type'])
  130. $sCondition .= $oDb->prepare(" AND `tgs`.`{$this->aTagFields['type']}` = ?", $aParam['type']);
  131. if (isset($aParam['status']) && $aParam['status'])
  132. $sCondition .= $oDb->prepare(" AND tgs.`{$this->aTagFields['status']}` = ?", $aParam['status']);
  133. else
  134. $sCondition .= " AND tgs.`{$this->aTagFields['status']}` = 'active'";
  135. if (isset($aParam['filter']) && $aParam['filter'])
  136. $sCondition .= " AND `tgs`.`{$this->aTagFields['tag']}` LIKE '%" . $oDb->escape($aParam['filter']) . "%'";
  137. if (isset($aParam['date']) && $aParam['date'])
  138. $sCondition .= $oDb->prepare(" AND DATE(`tgs`.`{$this->aTagFields['date']}`) = DATE(?)", $aParam['date']['year'] . '-' . $aParam['date']['month'] . '-' . $aParam['date']['day']);
  139. return $sCondition;
  140. }
  141. function _getCommonCategories($sModule = '') {
  142. $oDb = BxDolDb::getInstance();
  143. $sCondModule = $sModule ? $oDb->prepare(" AND `Type` = ?", $sModule) : '';
  144. $aCtegories = $oDb->getAll("SELECT `Category` FROM `sys_categories` WHERE `Owner` = 0 $sCondModule");
  145. $aResult = array();
  146. foreach ($aCtegories as $aCategory)
  147. $aResult[] = $aCategory['Category'];
  148. return $aResult;
  149. }
  150. function _insertTags ($aTagsSet) {
  151. $oDb = BxDolDb::getInstance();
  152. $aTags = $this->explodeTags($aTagsSet['tagString']);
  153. if( !$aTags )
  154. return;
  155. $sFields = '';
  156. foreach ($this->aTagFields as $sKey => $sValue)
  157. $sFields .= $sValue .', ';
  158. $aCommonCat = $this->_getCommonCategories($aTagsSet['type']);
  159. $bAutoApprove = getParam($this->sAutoApprovePrefix . $aTagsSet['type']) == 'on';
  160. $aTagsSet['owner'] = $this->iViewer;
  161. $sFields = trim($sFields, ', ');
  162. $sValues = '';
  163. foreach( $aTags as $sTag )
  164. {
  165. $aTagsSet['tag'] = addslashes( $sTag );
  166. $aTagsSet['status'] = $bAutoApprove || in_array($aTagsSet['tag'], $aCommonCat) ? 'active' : 'passive';
  167. $sValues .= $oDb->prepare("(?, ?, ?, ?, ?, CURRENT_TIMESTAMP), ", $aTagsSet['id'], $aTagsSet['type'], $aTagsSet['tag'], $aTagsSet['owner'], $aTagsSet['status']);
  168. }
  169. $sValues = trim($sValues, ', ');
  170. $sqlQuery = "INSERT INTO `{$this->sTagTable}` ($sFields) VALUES $sValues";
  171. $oDb->res($sqlQuery);
  172. }
  173. }