17 require_once SQ_PACKAGES_PATH.
'/cms/listing_engine/listing_engine.inc';
42 parent::__construct($assetid);
65 $assets_to_list = Array();
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;;
74 $group_by = $this->
attr(
'group_by');
75 $wanted_types = $this->
attr(
'types');
78 $letter_sort_by = NULL;
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';
85 $letter_sort_by = $sort_info[
'params'][
'field'];
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) {
96 $show_zero_hitcount = $this->
attr(
'display_all_assets');
98 if ($min_hitcount == 0) $show_zero_hitcount = TRUE;
100 $side_of_link = ($this->
attr(
'direction') ==
'down') ?
'minor' :
'major';
103 $min_depth = ($this->
attr(
'min_depth') ===
'') ? NULL : $this->
attr(
'min_depth');
104 $max_depth = ($this->
attr(
'max_depth') ===
'') ? NULL : $this->
attr(
'max_depth');
106 if ($this->
attr(
'subs_only')) {
111 $assets_to_list = Array();
118 $full_bind_vars = Array();
120 foreach ($root_asset_ids as $index => $root_asset_id) {
121 $root_asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($root_asset_id);
126 if ($this->
attr(
'direction') ==
'down') {
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'];
135 if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
136 $zero_bind_prefix =
'gc_z_'.$root_asset_id.
'_';
138 $no_hit_query = $query;
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]);
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];
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) ';
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 ';
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'];
173 if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
174 $zero_bind_prefix =
'gp_z_'.$root_asset_id.
'_';
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]);
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];
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'] =
') ';
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'] =
') ';
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 ';
209 $bind_vars[
'hc_'.$root_asset_id.
'_min'] = $min_hitcount;
210 $bind_vars[
'hc_'.$root_asset_id.
'_min_union'] = $min_hitcount;
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 ';
217 $bind_vars[
'hc_'.$root_asset_id.
'_max'] = $max_hitcount;
218 $bind_vars[
'hc_'.$root_asset_id.
'_max_union'] = $max_hitcount;
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';
231 if (empty($query))
return Array();
233 $full_query .= implode(
' ', $query);
236 if ($show_zero_hitcount && ((!$has_min_hitcount) || ($has_min_hitcount && ($min_hitcount == 0))) ) {
237 $full_query .=
'UNION ALL '.implode(
' ', $no_hit_query);
241 if ($index < (count($root_asset_ids) - 1)) {
242 $full_query .=
' UNION ALL ';
246 $full_bind_vars += $bind_vars;
251 $full_query .=
' ORDER BY hitcount '.$sql_sort;
255 foreach($full_bind_vars as $bind_var => $bind_value) {
259 }
catch (Exception $e) {
260 throw new Exception(
'Unable to get hit counts due to database error: '.$e->getMessage());
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'],
270 $assets_to_list[$link_info[$side_of_link.
'id']] = Array( Array(
'type_code' => $link_info[
'type_code']));
275 if ($group_by ==
'number') {
276 $assets_to_list = $this->
sortAssetList($assets_to_list, $sort_info);
278 return $assets_to_list;
293 $keywords = parent::_getGenericSingleAssetFormatKeywords();
294 $keywords[
'asset_hitcount'] =
'Hit count value of the asset';
313 parent::_printAsset($assetid, $list_position);
314 $element_contents = ob_get_contents();
319 $bind_vars = Array(
'assetid' => $assetid);
321 }
catch (Exception $e) {
322 throw new Exception(
'Unable to get hit count value due to database error: '.$e->getMessage());
325 if (empty($_tmp_hitcount)) {
328 $hitcount = $_tmp_hitcount[0][
'hitcount'];
331 $element_replacements = Array(
332 'asset_hitcount' => $hitcount,
334 $element_contents = replace_keywords($element_contents, $element_replacements);
336 echo $element_contents;
349 return Array(
'asset_hitcount' =>
'%asset_hitcount%');
365 $sort_by_hit_count = (!empty($sort_info) && isset($sort_info[
'params']) && isset($sort_info[
'params'][
'field']) && ($sort_info[
'params'][
'field'] ==
'hitcount'));
367 if (!$sort_by_hit_count) {
368 return parent::sortAssetList($asset_list, $sort_info);
388 $sort_by_hit_count = (!empty($sort_info) && isset($sort_info[
'params']) && isset($sort_info[
'params'][
'field']) && ($sort_info[
'params'][
'field'] ==
'hitcount'));
390 if (!$sort_by_hit_count) {
391 parent::filterAssetList($todo);
395 if ($this->
attr(
'group_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'];
403 foreach (array_keys($todo) as $todo_id) {
404 if (!isset($filtered_todo[$todo_id])) {
405 unset($todo[$todo_id]);
408 unset($filtered_todo);