Cheetah
ChWsbCategories.php
Go to the documentation of this file.
1 <?php
2 
8 require_once(CH_DIRECTORY_PATH_CLASSES . 'ChWsbTags.php');
9 
10 define("CATEGORIES_DIVIDER", ';');
11 
13 {
15 
16  function __construct($iPossOwner = 0)
17  {
18  parent::__construct();
19  $this->iViewer = (int)$iPossOwner > 0 ? (int)$iPossOwner : $this->iViewer;
20  $this->sCacheFile = 'sys_objects_categories';
21  $this->sNonParseParams = 'tags_non_parsable';
22  $this->sCacheTable = 'sys_objects_categories';
23  $this->sTagTable = 'sys_categories';
24  $this->aTagFields = array(
25  'id' => 'ID',
26  'type' => 'Type',
27  'tag' => 'Category',
28  'owner' => 'Owner',
29  'status' => 'Status',
30  'date' => 'Date'
31  );
32  $this->sAutoApprovePrefix = 'category_auto_app_';
33  $this->bToLower = false;
34  }
35 
36  function getGroupChooser ($sType, $iOwnerId = 0, $bForm = false, $sCustomValues = '')
37  {
38  $a = $this->getCategoriesList ($sType, $iOwnerId, $bForm);
39  $a = array('' => _t('_Please_Select_')) + (is_array($a) ? $a : array());
40 
41  $aCustomValues = $this->explodeTags($sCustomValues);
42  foreach($aCustomValues as $iIndex => $sValue)
43  $a[$sValue] = $sValue;
44 
45  return array(
46  'type' => 'select_box',
47  'name' => 'Categories',
48  'caption' => _t('_Categories'),
49  'values' => $a,
50  'required' => true,
51  'checker' => array (
52  'func' => 'avail',
53  'error' => _t ('_sys_err_categories'),
54  ),
55  'db' => array (
56  'pass' => 'Categories',
57  ),
58  );
59  }
60 
61  function getCategoriesList ($sType, $iOwnerId = 0, $bForm = false)
62  {
63  $this->getTagObjectConfig();
64  $sType = array_key_exists($sType, $this->aTagObjects) === true ? $sType : 'ch_photos';
65  $iOwnerId = (int)$iOwnerId;
66  $sqlQuery = "SELECT `cat`.`{$this->aTagFields['tag']}`
67  FROM `{$this->sTagTable}` `cat`
68  WHERE (`cat`.`{$this->aTagFields['owner']}` = 0 OR `cat`.`{$this->aTagFields['owner']}` = $iOwnerId)
69  AND `cat`.`{$this->aTagFields['type']}` = '$sType' AND `cat`.`{$this->aTagFields['status']}` = 'active'
70  __sqlAdd__
71  GROUP BY `cat`.`{$this->aTagFields['tag']}`";
72 
73  $aAddSql = array();
74 
75  // Deano notice. This added query is above as well. May be a bug in dolphin code. I'll wait for
76  // bug report on auto approve.
77  if (getParam($this->sAutoApprovePrefix . $sType) != 'on')
78  $aAddSql[] = " AND `cat`.`{$this->aTagFields['status']}` = 'active'";
79 
80  $sqlAdd = '';
81  foreach ($aAddSql as $sValue)
82  $sqlAdd .= $sValue;
83 
84  $sqlQuery = str_replace('__sqlAdd__', $sqlAdd, $sqlQuery);
85  return $bForm ? $this->oDb->getPairs($sqlQuery, $this->aTagFields['tag'], $this->aTagFields['tag']) : $this->oDb->getColumn($sqlQuery);
86  }
87 
88  function getTagList($aParam)
89  {
90  // TODO: pdo dynamic bindings
91  $sLimit = '';
92  $aTotalTags = array();
93  $sGroupBy = "GROUP BY `{$this->aTagFields['tag']}`";
94 
95  if (isset($aParam['limit'])) {
96  $sLimit = 'LIMIT ';
97  if (isset($aParam['start']))
98  $sLimit .= (int)$aParam['start'] . ', ';
99  $sLimit .= (int)$aParam['limit'];
100  }
101 
102  $sCondition = $this->_getSelectCondition($aParam);
103 
104  if (isset($aParam['orderby'])) {
105  if ($aParam['orderby'] == 'popular')
106  $sGroupBy .= " ORDER BY `count` DESC, `{$this->aTagFields['tag']}` ASC";
107  else if ($aParam['orderby'] == 'recent')
108  $sGroupBy .= " ORDER BY `{$this->aTagFields['date']}` DESC, `{$this->aTagFields['tag']}` ASC";
109  }
110 
111  $sDiffCount = '';
112 
113  $sqlQuery = "SELECT
114  `tgs`.`{$this->aTagFields['tag']}` as `{$this->aTagFields['tag']}`,
115  `tgs`.`{$this->aTagFields['date']}` as `{$this->aTagFields['date']}`,
116  COUNT(`tgs`.`{$this->aTagFields['id']}`) AS `count`
117  FROM `{$this->sTagTable}` `tgs` $sCondition $sGroupBy $sLimit";
118 
119  $aTags = $this->oDb->getAll($sqlQuery);
120  if (count($aTags) > 0) {
121  foreach($aTags as $aTag) {
122  if ((int)$aTag['count'] > 0)
123  $aTotalTags[$aTag[$this->aTagFields['tag']]] = $aTag['count'];
124  }
125  }
126  return $aTotalTags;
127  }
128 
129  function getTagsCount($aParam)
130  {
131  $sCondition = $this->_getSelectCondition($aParam);
132  $sqlQuery = "SELECT count(DISTINCT `tgs`.`{$this->aTagFields['tag']}`) AS `count` FROM
133  `{$this->sTagTable}` `tgs` {$sCondition}";
134 
135  return (int)$this->oDb->getOne($sqlQuery);
136  }
137 
138  function _getSelectCondition($aParam)
139  {
140  $sCondition = "WHERE `tgs`.`{$this->aTagFields['owner']}` != 0";
141 
142  if (!$aParam)
143  return $sCondition;
144 
145  if (isset($aParam['common'])) {
146  $aUnitsCommon = $this->_getCommonCategories($aParam['type']);
147  $sCatsList = "";
148  if (is_array($aUnitsCommon)) {
149  foreach ($aUnitsCommon as $sUnit)
150  $sCatsList .= process_db_input($sUnit, CH_TAGS_NO_ACTION, CH_SLASHES_NO_ACTION) . "','";
151  $sCatsList = substr($sCatsList, 0, -3);
152  }
153  $sCondition .= " AND `tgs`.`{$this->aTagFields['tag']}` " . (!$aParam['common'] ? 'NOT' : '') .
154  " IN ('" . $sCatsList . "')";
155  }
156 
157  if (isset($aParam['type']) && $aParam['type'])
158  $sCondition .= " AND `tgs`.`{$this->aTagFields['type']}` = '{$aParam['type']}'";
159 
160  if (isset($aParam['status']) && $aParam['status'])
161  $sCondition .= " AND tgs.`{$this->aTagFields['status']}` = '{$aParam['status']}'";
162  else
163  $sCondition .= " AND tgs.`{$this->aTagFields['status']}` = 'active'";
164 
165  if (isset($aParam['filter']) && $aParam['filter'])
166  $sCondition .= " AND `tgs`.`{$this->aTagFields['tag']}` LIKE '%{$aParam['filter']}%'";
167 
168  if (isset($aParam['date']) && $aParam['date'])
169  $sCondition .= " AND DATE(`tgs`.`{$this->aTagFields['date']}`) = DATE('{$aParam['date']['year']}-{$aParam['date']['month']}-{$aParam['date']['day']}')";
170 
171  return $sCondition;
172  }
173 
175  {
176  $sCondModule = strlen($sModule) > 0 ? "AND `Type` = '$sModule'" : '';
177  return $this->oDb->getColumn("SELECT `Category` FROM `sys_categories` WHERE `Owner` = 0 $sCondModule");
178  }
179 
180  function _insertTags ($aTagsSet)
181  {
182  $aTags = $this->explodeTags($aTagsSet['tagString']);
183  if( !$aTags )
184  return;
185  $sFields = '';
186  foreach ($this->aTagFields as $sKey => $sValue)
187  $sFields .= $sValue .', ';
188 
189  $aCommonCat = $this->_getCommonCategories($aTagsSet['type']);
190  $bAutoApprove = getParam($this->sAutoApprovePrefix . $aTagsSet['type']) == 'on';
191  $aTagsSet['owner'] = $this->iViewer;
192  $sFields = trim($sFields, ', ');
193  $sValues = '';
194  foreach( $aTags as $sTag ) {
195  $aTagsSet['tag'] = process_db_input($sTag, CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
196  $aTagsSet['status'] = $bAutoApprove || in_array($aTagsSet['tag'], $aCommonCat) ? 'active' : 'passive';
197  $sValues .= "('{$aTagsSet['id']}', '{$aTagsSet['type']}', '{$aTagsSet['tag']}', '{$aTagsSet['owner']}', '{$aTagsSet['status']}', CURRENT_TIMESTAMP), ";
198  }
199  $sValues = trim($sValues, ', ');
200 
201  $sqlQuery = "INSERT INTO `{$this->sTagTable}` ($sFields) VALUES $sValues";
202  $this->oDb->query($sqlQuery);
203  }
204 }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
ChWsbTags\$iViewer
$iViewer
Definition: ChWsbTags.php:18
ChWsbCategories\getTagsCount
getTagsCount($aParam)
Definition: ChWsbCategories.php:129
ChWsbCategories\_getSelectCondition
_getSelectCondition($aParam)
Definition: ChWsbCategories.php:138
php
ChWsbTags
Definition: ChWsbTags.php:15
$sModule
if(!file_exists($sRayHeaderPath)) $sModule
Definition: index.php:14
ChWsbCategories\getCategoriesList
getCategoriesList($sType, $iOwnerId=0, $bForm=false)
Definition: ChWsbCategories.php:61
ChWsbCategories\$sAutoApprovePrefix
$sAutoApprovePrefix
Definition: ChWsbCategories.php:14
ChWsbCategories
Definition: ChWsbCategories.php:13
ChWsbCategories\_getCommonCategories
_getCommonCategories($sModule='')
Definition: ChWsbCategories.php:174
CH_SLASHES_NO_ACTION
const CH_SLASHES_NO_ACTION
Definition: utils.inc.php:30
ChWsbTags\explodeTags
explodeTags($sText)
Definition: ChWsbTags.php:83
$sType
$sType
Definition: actions.inc.php:11
$iIndex
$iIndex
Definition: bottom_menu_compose.php:142
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
CH_TAGS_NO_ACTION
const CH_TAGS_NO_ACTION
Definition: utils.inc.php:21
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
ChWsbCategories\getGroupChooser
getGroupChooser($sType, $iOwnerId=0, $bForm=false, $sCustomValues='')
Definition: ChWsbCategories.php:36
CH_TAGS_STRIP
const CH_TAGS_STRIP
Definition: utils.inc.php:22
ChWsbCategories\getTagList
getTagList($aParam)
Definition: ChWsbCategories.php:88
ChWsbCategories\_insertTags
_insertTags($aTagsSet)
Definition: ChWsbCategories.php:180
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbTags\getTagObjectConfig
getTagObjectConfig($aParam=array())
Definition: ChWsbTags.php:62
ChWsbCategories\__construct
__construct($iPossOwner=0)
Definition: ChWsbCategories.php:16