Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_tag_listing.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_asset_listing/page_asset_listing.inc';
18 
40 {
41 
48  function __construct($assetid=0)
49  {
50  parent::__construct($assetid);
51 
52  }//end constructor
53 
54 
62  public function create(Array &$link)
63  {
64  $this->setAttrValue('types', Array('thesaurus_term' => '1'));
65  return parent::create($link);
66 
67  }//end constructor
68 
69 
80  public function getAssetList()
81  {
82  $this->_tmp['tag_freq'] = Array();
83 
84  $assets_to_list = Array();
85 
86  // get all the children
87  $min_depth = $this->attr('min_depth');
88  if ($min_depth == '') $min_depth = NULL;
89  $max_depth = $this->attr('max_depth');
90  if ($max_depth == '') $max_depth = NULL;
91 
92  $query_links = '';
93  $bind_vars = Array();
94 
95  $root_asset_ids = $this->getRootNodes();
96 
97  foreach ($root_asset_ids as $root_asset_id) {
98  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_asset_id);
99 
100  // must have read and effective access on the children
101  $query_links = $GLOBALS['SQ_SYSTEM']->am->generateGetChildrenQuery($asset, '', FALSE, NULL, NULL, SQ_PERMISSION_READ, TRUE, TRUE, $min_depth, $max_depth, TRUE, NULL);
102  // ok, confession time: this is not nice, we cover up a bug in generateGetChildrenQuery() which adds a random AND at the end of the weher clause
103  // need to be fixed properly later
104  $query_links['sql_array']['where'] = preg_replace('/AND\s*$/', '', $query_links['sql_array']['where']);
105  $rows = array();
106 
107  $bind_vars = $query_links['bind_vars'];
108 
109  $query_links['sql_array']['select'] = '(SELECT DISTINCT l.minorid ';
110 
111  // remove shadow children from this search as the query would be too complex and slower
112  $query_links['sql_array']['union_select'] = '';
113  $query_links['sql_array']['union_from'] = '';
114  $query_links['sql_array']['union_where'] = '';
115 
116  if (isset($bind_vars['gc_union_majorid'])) {
117  unset($bind_vars['gc_union_majorid']);
118  }
119 
120  $query_links = implode(' ', $query_links['sql_array']);
121 
122  // no support for multiple root nodes; only the first one is processed
123  break;
124  }
125 
126  // thesaurus filtering
127  $thesaurus_filters = $this->_getThesaurusFilters();
128 
129  $thesaurus_filter_clause = '';
130  if (!empty($thesaurus_filters)) {
131  // assume there is only filtering thesaurus
132  $thesaurus_filter_clause = ' AND (te.thesid = :thesaurusid) ';
133  $bind_vars['thesaurusid'] = $thesaurus_filters[0];
134  }
135 
136  // include root node is required
137  $where_root_node = '';
138  if (!$min_depth) {
139  $where_root_node = ' OR (sl2.majorid = :root_nodeid) ';
140  $bind_vars['root_nodeid'] = $root_asset_ids[0];
141  }
142 
143  // build and execute the query
144  $query = '
145  SELECT
146  sl2.minorid,
147  count(sl2.minorid) as score
148  FROM
149  sq_shdw_ast_lnk sl2
150  JOIN sq_thes_term te
151  ON (te.thesid || \':\' || te.termid)= sl2.minorid
152  WHERE
153  (sl2.majorid IN ('.$query_links.')
154  '.$where_root_node.' )
155  '.$thesaurus_filter_clause.'
156  GROUP BY
157  sl2.minorid
158  ORDER BY
159  score DESC
160  ';
161 
162  try {
163  $query = MatrixDAL::preparePdoQuery($query);
164  foreach ($bind_vars as $bind_var => $bind_value) {
165  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
166  }
167  $rows = MatrixDAL::executePdoAll($query);
168  } catch (Exception $e) {
169  throw new Exception('Unable to get children for asset: '.join(', ', $root_asset_ids).' due to database error: '.$e->getMessage());
170  }
171  // scoop the result
172  // TODO: limit the select instead, that would be faster.
173  $query_limit = $this->_getQueryLimit();
174  if ($query_limit != -1) {
175  $rows = array_slice($rows, 0, $query_limit);
176  }
177 
178  // store the scores
179  foreach ($rows as $row) {
180  // save the weight of all the tagids
181  $this->_tmp['tag_freq'][$row['minorid']] = $row['score'];
182  $assets_to_list[$row['minorid']] = Array(Array('type_code' => 'thesaurus_term'));
183  }
184 
185  return $assets_to_list;
186 
187  }//end getAssetList()
188 
189 
197  protected function _getQueryLimit()
198  {
199  $ret = -1;
200 
201  if ($this->attr('first_page_only')) {
202  $si = $this->getSortInfo();
203  if (empty($si['type'])) {
204  $ret = $this->attr('num_per_page');
205  if ($ret == 0) $ret = -1;
206  }
207  }
208 
209  return $ret;
210 
211  }//end _getQueryLimit()
212 
213 
220  protected function _getThesaurusFilters()
221  {
222  $ret = Array();
223 
224  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_NOTICE, 'thesaurus', FALSE);
225 
226  foreach ($links as $link) {
227  $ret[] = $link['minorid'];
228  }
229 
230  return $ret;
231 
232  }//end _getThesaurusFilters()
233 
234 
249  public function _getShadowAssetKeywordReplacements($assetid, $bc_keywords)
250  {
251  $keywords = Array();
252  $shadow_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
253 
254  foreach ($bc_keywords as $key => $keyword) {
255  if ($keyword == 'tag_count') {
256  $keywords[$keyword] = array_get_index($this->_tmp['tag_freq'], $assetid, 1);
257  } else if ($keyword == 'tag_size') {
258  $keywords[$keyword] = ceil(array_get_index($this->_tmp['tag_freq'], $assetid, 1) / $this->_getTagMaxCount() * $this->Attr('tag_max_size'));
259  } else {
260  $keywords[$keyword] = $shadow_asset->getKeywordReplacement($keyword);
261  }
262  }
263  return $keywords;
264 
265  }//end _getShadowAssetKeywordReplacements()
266 
267 
274  protected function _getTagMaxCount()
275  {
276  $ret = 1;
277 
278  if (count($this->_tmp['tag_freq'])) {
279  $ret = array_values($this->_tmp['tag_freq']);
280  $ret = $ret[0];
281  }
282 
283  return $ret;
284 
285  }//end _getTagMaxCount()
286 
287 
298  {
299  $ret = parent::_getAdditionalSingleAssetFormatKeywords();
300 
301  $ret['tag_count'] = translate('page_tag_listing_keyword_tag_count');
302  $ret['tag_size'] = translate('page_tag_listing_keyword_tag_size');
303 
304  return $ret;
305 
306  }//end _getAdditionalSingleAssetFormatKeywords()
307 
308 
315  function _getCacheKey()
316  {
317  $cache_key = parent::_getCacheKey();
318 
319  // add the thesaurusid we use for filtering
320  $cache_key .= ';THES='.join(',', $this->_getThesaurusFilters());
321  // add the maximum tag size
322  $cache_key .= ';MTS='.$this->Attr('tag_max_size');
323  // add the "first page only"
324  $cache_key .= ';FPO='.($this->attr('first_page_only') ? '1' : '0');
325 
326  return $cache_key;
327 
328  }//end _getCacheKey()
329 
330 
331 }//end class
332 
333 ?>