Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_hit_count_listing.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/cms/listing_engine/listing_engine.inc';
18 
31 {
32 
33 
40  function __construct($assetid=0)
41  {
42  parent::__construct($assetid);
43 
44  }//end constructor
45 
46 
63  function getAssetList()
64  {
65  $assets_to_list = Array();
66 
67  $min_hitcount = $this->attr('min_hitcount');
68  $has_min_hitcount = $min_hitcount >= 0 ? TRUE : FALSE;
69  $max_hitcount = $this->attr('max_hitcount');
70  $has_max_hitcount = $max_hitcount >= 0 ? TRUE : FALSE;;
71 
72  // get the root nodes
73  $root_asset_ids = $this->getRootNodes();
74  $group_by = $this->attr('group_by');
75  $wanted_types = $this->attr('types');
76  $sort_info = $this->getSortInfo();
77 
78  $letter_sort_by = NULL;
79  // if we are showing an A-Z listing but the user has not selected a proper sort field,
80  // we will use the 'name' attribute of the asset to work out the listing
81  if ($group_by == 'letter') {
82  if (empty($sort_info) || !($sort_info['type'] == 'field') || ($sort_info['params']['field'] != 'short_name')) {
83  $letter_sort_by = 'name';
84  } else {
85  $letter_sort_by = $sort_info['params']['field'];
86  }
87  }
88 
89  // check the sorting order
90  $sql_sort = ' desc ';
91  $sort_by_hit_count = ((!empty($sort_info)) && isset($sort_info['params']) && isset($sort_info['params']['field']) && ($sort_info['params']['field'] == 'hitcount'));
92  if ($sort_by_hit_count) {
93  if (!$this->isDescending()) $sql_sort = ' asc ';
94  }
95 
96  $show_zero_hitcount = $this->attr('display_all_assets');
97  // we are overwriting $show_zero_hitcount if the minimum specified was zero
98  if ($min_hitcount == 0) $show_zero_hitcount = TRUE;
99 
100  $side_of_link = ($this->attr('direction') == 'down') ? 'minor' : 'major';
101 
102  // control the depth of the search
103  $min_depth = ($this->attr('min_depth') === '') ? NULL : $this->attr('min_depth');
104  $max_depth = ($this->attr('max_depth') === '') ? NULL : $this->attr('max_depth');
105  // if we display subs only we limit the min and max depth to 1
106  if ($this->attr('subs_only')) {
107  $min_depth = 1;
108  $max_depth = 1;
109  }
110 
111  $assets_to_list = Array();
112 
113  // will contain all the link info
114  $links = Array();
115 
116  // build the full query that get every children/parents of the root nodes with an hit count value
117  $full_query = '';
118  $full_bind_vars = Array();
119 
120  foreach ($root_asset_ids as $index => $root_asset_id) {
121  $root_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($root_asset_id);
122  $no_hit_query = '';
123 
124  // if the direction is down we are using the table with the alias a for the asset
125  $table_alias = 'a';
126  if ($this->attr('direction') == 'down') {
127  // if we are listing children
128  $bind_prefix = 'gc_'.$root_asset_id.'_';
129  $ret_val = $GLOBALS['SQ_SYSTEM']->am->generateGetChildrenQuery($root_asset, array_keys($wanted_types), FALSE, NULL, NULL, NULL, NULL, TRUE, $min_depth, $max_depth, FALSE, NULL, $bind_prefix);
130  $query = $ret_val['sql_array'];
131  $bind_vars = $ret_val['bind_vars'];
132 
133  // if we need to show assets with not hitcount value
134  // get the $query and add in the where clause not in (select assetid from sq_hit_count)
135  if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
136  $zero_bind_prefix = 'gc_z_'.$root_asset_id.'_';
137 
138  $no_hit_query = $query;
139 
140  $zero_bind_vars = Array();
141  foreach (array_keys($no_hit_query) as $key) {
142  $no_hit_query[$key] = str_replace(':'.$bind_prefix, ':'.$zero_bind_prefix, $no_hit_query[$key]);
143  }
144 
145  $bind_keys = array_keys($bind_vars);
146  foreach ($bind_keys as $key) {
147  $zeroed_key = str_replace($bind_prefix, $zero_bind_prefix, $key);
148  $bind_vars[$zeroed_key] = $bind_vars[$key];
149  }
150 
151  $no_hit_query['select'] .= ', 0 as hitcount ';
152  $no_hit_query['where'] .= ' and a.assetid not in (select assetid from sq_hit_count) ';
153  $no_hit_query['union_select'] .= ', 0 as hitcount ';
154  $no_hit_query['union_where'] .= ' and sl.minorid not in (select assetid from sq_hit_count) ';
155  }
156  $query['select'] .= ', hc.hitcount';
157  $query['from'] .= ' INNER JOIN sq_hit_count hc on hc.assetid = '.$table_alias.'.assetid ';
158  $query['union_select'] .= ', hc.hitcount';
159  $query['union_from'] .= ' INNER JOIN sq_hit_count hc on hc.assetid = sl.minorid ';
160 
161  } else {
162  // if we are listing parents
163 
164  // if direction id down we are using the table with the alias ll for the asset
165  $table_alias = 'll';
166  $bind_prefix = 'gp_'.$root_asset_id.'_';
167  $ret_val = $GLOBALS['SQ_SYSTEM']->am->generateGetParentsQuery($root_asset_id, array_keys($wanted_types), FALSE, NULL, NULL, NULL, $min_depth, $max_depth, NULL, $bind_prefix);
168  $query = $ret_val['sql_array'];
169  $bind_vars = $ret_val['bind_vars'];
170 
171  // if we need to show assets with not hitcount value
172  // get the $query and add in the where clause not in (select assetid from sq_hit_count)
173  if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
174  $zero_bind_prefix = 'gp_z_'.$root_asset_id.'_';
175 
176  // we need to replace the bind prefix with another one so we don't duplicate
177  // them in the full query.
178  $no_hit_query = $query;
179  $zero_bind_vars = Array();
180  foreach (array_keys($no_hit_query) as $key) {
181  $no_hit_query[$key] = str_replace(':'.$bind_prefix, ':'.$zero_bind_prefix, $no_hit_query[$key]);
182  }
183 
184  $bind_keys = array_keys($bind_vars);
185  foreach ($bind_keys as $key) {
186  $zeroed_key = str_replace($bind_prefix, $zero_bind_prefix, $key);
187  $bind_vars[$zeroed_key] = $bind_vars[$key];
188  }
189 
190  $no_hit_query['select'] .= ', 0 as hitcount ';
191  $no_hit_query['where'] .= ' where ll.majorid not in (select assetid from sq_hit_count) ';
192  $no_hit_query['union_select'] .= ', 0 as hitcount ';
193  $no_hit_query['union_where'] .= ' and sl.majorid not in (select assetid from sq_hit_count) ';
194  $no_hit_query['order_by'] = ') ';
195  }
196 
197  // the following SQL query parts are added to the full query so it get only assets that have a hit count value
198  $query['select'] .= ', hc.hitcount';
199  $query['where'] = ' inner join sq_hit_count hc on hc.assetid = ll.majorid ';
200  $query['union_select'] .= ', hc.hitcount';
201  $query['union_from'] .= ' inner join sq_hit_count hc on hc.assetid = sl.majorid ';
202  $query['order_by'] = ') ';
203  }
204 
205  if ($has_min_hitcount) {
206  $query['where'] .= ' and hc.hitcount >= :hc_'.$root_asset_id.'_min ';
207  $query['union_where'] .= ' and hc.hitcount >= :hc_'.$root_asset_id.'_min_union ';
208 
209  $bind_vars['hc_'.$root_asset_id.'_min'] = $min_hitcount;
210  $bind_vars['hc_'.$root_asset_id.'_min_union'] = $min_hitcount;
211  }
212 
213  if ($has_max_hitcount) {
214  $query['where'] .= ' and hc.hitcount <= :hc_'.$root_asset_id.'_max ';
215  $query['union_where'] .= ' and hc.hitcount <= :hc_'.$root_asset_id.'_max_union ';
216 
217  $bind_vars['hc_'.$root_asset_id.'_max'] = $max_hitcount;
218  $bind_vars['hc_'.$root_asset_id.'_max_union'] = $max_hitcount;
219  }
220 
221  // if we are sorting A-Z
222  if ($group_by == 'letter') {
223  $query['select'] .= ', SUBSTR('.$table_alias.'.'.$letter_sort_by.', 1, 1) AS first_letter';
224  $query['union_select'] .= ', null AS first_letter';
225  if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
226  $no_hit_query['select'] .= ', SUBSTR('.$table_alias.'.'.$letter_sort_by.', 1, 1) AS first_letter';
227  $no_hit_query['union_select'] .= ', null AS first_letter';
228  }
229  }
230 
231  if (empty($query)) return Array();
232 
233  $full_query .= implode(' ', $query);
234 
235  // if we need to add zero hitcount asset
236  if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
237  $full_query .= 'UNION ALL '.implode(' ', $no_hit_query);
238  }
239 
240  // if we are not the last index of $root_asset_ids we are doing an union with next query
241  if ($index < (count($root_asset_ids) - 1)) {
242  $full_query .= ' UNION ALL ';
243  }
244 
245  // Add bind variables to the overall tally
246  $full_bind_vars += $bind_vars;
247 
248  }//end foreach
249 
250  // sort them by hit count
251  $full_query .= ' ORDER BY hitcount '.$sql_sort;
252 
253  try {
254  $pdo_query = MatrixDAL::preparePdoQuery($full_query);
255  foreach($full_bind_vars as $bind_var => $bind_value) {
256  MatrixDAL::bindValueToPdo($pdo_query, $bind_var, $bind_value);
257  }
258  $links = MatrixDAL::executePdoAll($pdo_query);
259  } catch (Exception $e) {
260  throw new Exception('Unable to get hit counts due to database error: '.$e->getMessage());
261  }
262 
263  foreach ($links as $link_info) {
264  if ($group_by == 'letter') {
265  $assets_to_list[$link_info[$side_of_link.'id']] = Array( Array(
266  'type_code' => $link_info['type_code'],
267  'first_letter' => $link_info['first_letter'],
268  ));
269  } else {
270  $assets_to_list[$link_info[$side_of_link.'id']] = Array( Array('type_code' => $link_info['type_code']));
271  }
272  }
273 
274  // we add the following code to fix a problem where the assets are sorted when no sorting is selected in standard and group by listing mode
275  if ($group_by == 'number') {
276  $assets_to_list = $this->sortAssetList($assets_to_list, $sort_info);
277  }
278  return $assets_to_list;
279 
280  }//end getAssetList()
281 
282 
291  {
292 
293  $keywords = parent::_getGenericSingleAssetFormatKeywords();
294  $keywords['asset_hitcount'] = 'Hit count value of the asset';
295  return $keywords;
296 
297  }//end _getGenericSingleAssetFormatKeywords()
298 
299 
310  function _printAsset($assetid, $list_position)
311  {
312  ob_start();
313  parent::_printAsset($assetid, $list_position);
314  $element_contents = ob_get_contents();
315  ob_end_clean();
316 
317  // get the hit count value
318  try {
319  $bind_vars = Array('assetid' => $assetid);
320  $_tmp_hitcount = MatrixDAL::executeAll('data_package', 'getHitCount', $bind_vars);
321  } catch (Exception $e) {
322  throw new Exception('Unable to get hit count value due to database error: '.$e->getMessage());
323  }
324 
325  if (empty($_tmp_hitcount)) {
326  $hitcount = 0;
327  } else {
328  $hitcount = $_tmp_hitcount[0]['hitcount'];
329  }
330 
331  $element_replacements = Array(
332  'asset_hitcount' => $hitcount,
333  );
334  $element_contents = replace_keywords($element_contents, $element_replacements);
335 
336  echo $element_contents;
337 
338  }//end _printAsset()
339 
340 
348  {
349  return Array('asset_hitcount' => '%asset_hitcount%');
350 
351  }//end getExtendedAssetKeywordReplacements()
352 
353 
363  function sortAssetList($asset_list, $sort_info)
364  {
365  $sort_by_hit_count = (!empty($sort_info) && isset($sort_info['params']) && isset($sort_info['params']['field']) && ($sort_info['params']['field'] == 'hitcount'));
366 
367  if (!$sort_by_hit_count) {
368  return parent::sortAssetList($asset_list, $sort_info);
369  }
370 
371  return $asset_list;
372 
373  }//end sortAssetList()
374 
375 
385  function filterAssetList(&$todo)
386  {
387  $sort_info = $this->getSortInfo();
388  $sort_by_hit_count = (!empty($sort_info) && isset($sort_info['params']) && isset($sort_info['params']['field']) && ($sort_info['params']['field'] == 'hitcount'));
389 
390  if (!$sort_by_hit_count) {
391  parent::filterAssetList($todo);
392  } else {
393 
394  // filter the asset types
395  if ($this->attr('group_by') == 'letter') {
396  // if we sort by letter
397  $filtered_todo = Array();
398  for (reset($todo); NULL !== ($todo_id = key($todo)); next($todo)) {
399  $filtered_todo[$todo_id] = $todo[$todo_id]['type_code'];
400  }
401 
402  $this->filterAssetTypes($filtered_todo);
403  foreach (array_keys($todo) as $todo_id) {
404  if (!isset($filtered_todo[$todo_id])) {
405  unset($todo[$todo_id]);
406  }
407  }
408  unset($filtered_todo);
409  $todo = $this->sortAssetList($todo, $this->getSortInfo());
410  } else {
411  // if we are not sorting by letter
412  $this->filterAssetTypes($todo);
413  }
414  }
415 
416  }//end filterAssetList()
417 
418 
419 }//end class
420 ?>