18 require_once SQ_PACKAGES_PATH.
'/cms/listing_engine/listing_engine.inc';
44 parent::__construct($assetid);
67 $group_by = $this->
attr(
'group_by');
69 $logic = $this->
attr(
'all_root_nodes') ?
'AND' :
'OR';
75 $wanted_types = $this->
attr(
'types');
76 if (empty($wanted_types)) {
77 trigger_localised_error(
'CMS0019', E_USER_NOTICE);
88 $link_value_wanted = NULL;
89 if ($this->
attr(
'link_value_filter_enabled')) {
91 $link_value_wanted = htmlentities($this->
attr(
'link_value_filter'));
94 $assets_to_list = $this->
_getAssetList($group_by, $logic, $wanted_types, $root_asset_ids, $sort_info, $link_value_wanted);
96 return $assets_to_list;
124 function _getAssetList($group_by, $logic, $wanted_types, $root_asset_ids, $sort_info, $link_value_wanted=NULL)
126 $assets_to_list = NULL;
129 $list_permission = $this->
attr(
'list_permission');
130 $list_effective = $this->
attr(
'list_effective');
133 $raw_assets_to_list = $parameter_map->getParameterValue(
'asset_selection');
134 if (!empty($raw_assets_to_list)) {
135 $assets_to_list = Array();
137 if (!is_array($raw_assets_to_list)) {
140 preg_match_all(
'|[0-9]+(:([0-9a-z_\-\s\(\)\\\]+=[0-9a-z_\-\s\(\)\\\]+,)*[0-9a-z_\-\s\(\)\\\]+=[0-9a-z_\-\s\(\)\\\]+)?|i', $raw_assets_to_list, $matches);
141 $list_keys = $matches[0];
142 $raw_assets_to_list = Array();
143 foreach ($list_keys as $raw_assetid) {
145 $raw_assets_to_list[$raw_assetid] = 1;
150 foreach ($raw_assets_to_list as $raw_asset => $enabled) {
151 $list_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($raw_asset);
152 if (is_null($list_asset))
continue;
156 switch ($list_permission) {
158 case SQ_PERMISSION_READ :
159 $allowed = $list_asset->readAccess();
162 case SQ_PERMISSION_WRITE :
163 $allowed = $list_asset->writeAccess();
166 case SQ_PERMISSION_ADMIN :
167 $allowed = $list_asset->adminAccess();
172 if (!$allowed)
continue;
175 $parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($list_asset->id);
178 foreach ($parents as $parent_id => $parent_type) {
179 if (in_array($parent_id, $root_asset_ids)) {
184 if (!$allowed)
continue;
186 $assets_to_list[$list_asset->id][0][
'type_code'] = $list_asset->type();
190 return $assets_to_list;
193 $dynamic_asset_selection = $parameter_map->getParameters();
194 if (in_array(
'asset_selection', $dynamic_asset_selection) && $this->
attr(
'dynamic_asset_selection_option') ==
'empty_result') {
201 if ($group_by ==
'letter') {
202 if (empty($sort_info) || !($sort_info[
'type'] ==
'field') || ($sort_info[
'params'][
'field'] !=
'short_name')) {
203 $letter_sort_by =
'name';
205 $letter_sort_by = $sort_info[
'params'][
'field'];
209 if (!is_null($link_value_wanted)) {
210 $link_value_wanted = Array(
211 'link_value' => Array($link_value_wanted),
212 'equal' => $this->
attr(
'link_value_filter_logic'),
216 if ($this->
attr(
'subs_only')) {
218 $side_of_link = ($this->
attr(
'direction') ==
'down') ?
'major' :
'minor';
219 $link_type = ($side_of_link ==
'major') ?
'minor' :
'major';
225 foreach ($root_asset_ids as $root_asset_id) {
226 $links_query = $GLOBALS[
'SQ_SYSTEM']->am->generateGetLinksQuery($root_asset_id, $this->
attr(
'link_types'), array_keys($wanted_types), FALSE, $side_of_link, $link_value_wanted, NULL, NULL, $letter_sort_by, $list_permission, $list_effective);
227 if (empty($links_query))
return Array();
230 $links_query[
'sql_array'][
'select'] .=
', SUBSTR(l.'.$letter_sort_by.
', 1, 1) AS first_letter';
237 foreach ($links_query[
'bind_vars'] as $bind_var => $bind_value) {
242 }
catch (Exception $e) {
243 throw new Exception($e->getMessage().
' (in '.$e->getFile().
' on line '.$e->getLine().
')');
246 $new_assets_to_list = Array();
247 foreach ($links as $link_info) {
248 $new_assets_to_list[$link_info[$link_type.
'id']] = Array(
250 'type_code' => $link_info[$link_type.
'_type_code'],
251 'first_letter' => $link_info[
'first_letter'],
256 $assets_to_list = $this->
_combineAssets($logic, $new_assets_to_list, $assets_to_list);
264 foreach ($root_asset_ids as $root_asset_id) {
265 $links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($root_asset_id, $this->
attr(
'link_types'), array_keys($wanted_types), FALSE, $side_of_link, $link_value_wanted, NULL, NULL, NULL, $list_permission, $list_effective);
266 $new_assets_to_list = Array();
267 foreach ($links as $link_info) {
268 $new_assets_to_list[$link_info[$link_type.
'id']] = Array( 0 => Array(
'type_code' => $link_info[$link_type.
'_type_code']));
271 $assets_to_list = $this->
_combineAssets($logic, $new_assets_to_list, $assets_to_list);
279 $min_depth = ($this->
attr(
'min_depth') ===
'') ? NULL : $this->
attr(
'min_depth');
280 $max_depth = ($this->
attr(
'max_depth') ===
'') ? NULL : $this->
attr(
'max_depth');
283 if ($min_depth ===
'0' && $max_depth ===
'0') {
284 $assets_to_list = $this->
_combineRootNodes(Array(), $root_asset_ids, $wanted_types);
285 return empty($assets_to_list) ? Array() : $assets_to_list;
290 foreach ($root_asset_ids as $root_asset_id) {
291 if ($this->
attr(
'direction') ==
'down') {
292 $root_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($root_asset_id);
293 $ret_val = $GLOBALS[
'SQ_SYSTEM']->am->generateGetChildrenQuery($root_asset, array_keys($wanted_types), FALSE, NULL, $letter_sort_by, $list_permission, $list_effective, TRUE, $min_depth, $max_depth, TRUE, $link_value_wanted);
294 $sql_array = $ret_val[
'sql_array'];
295 $bind_vars = $ret_val[
'bind_vars'];
296 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($root_asset);
300 $ret_val = $GLOBALS[
'SQ_SYSTEM']->am->generateGetParentsQuery($root_asset_id, array_keys($wanted_types), FALSE, $letter_sort_by, $list_permission, $list_effective, $min_depth, $max_depth, $link_value_wanted);
301 $sql_array = $ret_val[
'sql_array'];
302 $bind_vars = $ret_val[
'bind_vars'];
304 if (empty($ret_val)) $new_assets_to_list = Array();
306 $sql_array[
'select'] .=
', SUBSTR('.$table_alias.
'.'.$letter_sort_by.
', 1, 1) AS first_letter';
307 if (strpos($sql_array[
'group_by'], ($table_alias.
'.'.$letter_sort_by)) === FALSE) {
308 $sql_array[
'group_by'] .=
', '.$table_alias.
'.'.$letter_sort_by;
312 $sql_array[
'union_select'] .=
', null AS first_letter';
314 $new_assets_to_list = Array();
317 foreach ($bind_vars as $bind_var => $bind_value) {
321 }
catch (Exception $e) {
322 throw new Exception($e->getMessage());
326 $assets_to_list = $this->
_combineAssets($logic, $new_assets_to_list, $assets_to_list);
337 $grouping_attr = $this->
attr(
'asset_grouping');
339 if ((count($grouping_attr) == 1) && ($grouping_attr[0][
'group_type'] ==
'parent_asset') && array_get_index($grouping_attr[0],
'direct_parent_only', FALSE)) {
340 $group = $grouping_attr[0];
342 $links_sql =
'SELECT DISTINCT
345 '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk
346 WHERE minorid IN (%s)';
351 SELECT assetid FROM sq_ast WHERE %s
355 if (!empty($group[
'restrict_types'][
'type_code'])) {
356 $types_sub_sql = Array();
358 $inherited_types = Array();
359 $non_inherited_types = Array();
363 foreach ($group[
'restrict_types'][
'type_code'] as $key => $type) {
364 if ($group[
'restrict_types'][
'inherit'][$key]) {
371 if (!empty($uninherited_types)) {
372 $types_sub_sql[] =
'(type_code IN ('.implode(
', ', $uninherited_types).
'))';
375 if (!empty($inherited_types)) {
376 $types_sub_sql[] =
'(type_code IN
381 '.SQ_TABLE_RUNNING_PREFIX.
'ast_typ_inhd
382 WHERE inhd_type_code IN ('.implode(
', ', $inherited_types).
')))';
385 $types_sub_sql = implode(
' OR ', $types_sub_sql);
386 $types_sql = sprintf($types_sql, $types_sub_sql);
388 $links_sql .= $types_sql;
393 $bind_vars = Array();
394 foreach ($root_asset_ids as $root_asset_id) {
395 $root_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($root_asset_id);
397 $bind_prefix =
'gc_'.$root_asset_id.
'_';
398 $ret_val = $GLOBALS[
'SQ_SYSTEM']->am->generateGetChildrenQuery($root_asset, array_keys($wanted_types), FALSE, NULL, NULL, $list_permission, $list_effective, TRUE, $min_depth, $max_depth, TRUE, $link_value_wanted, $bind_prefix);
399 $sql = $ret_val[
'sql_array'];
400 $bind_vars = array_merge($bind_vars, $ret_val[
'bind_vars']);
402 $sql[
'select'] = str_replace(
', a.type_code',
'', $sql[
'select']);
403 $sql[
'union_select'] = str_replace(
', null',
'', $sql[
'union_select']);
405 $sql = implode(
' ', $sql);
406 $sqls[] =
'('.sprintf($links_sql, $sql).
')';
410 $sql = implode(
' UNION ', $sqls);
415 foreach ($bind_vars as $bind_var => $bind_value) {
419 }
catch (Exception $e) {
420 throw new Exception($e->getMessage());
423 $childids = array_keys($links);
424 $direct_parents = Array();
425 $restrict_types = array_get_index($group,
'restrict_types', Array());
426 $direct_parent_only = array_get_index($group,
'direct_parent_only', FALSE);
427 foreach ($childids as $childid) {
428 foreach ($links[$childid] as $key => $direct_parent) {
429 if (count($links[$childid]) > 1) {
435 $direct_parents[$childid][] = $direct_parent[0];
440 $this->_tmp[
'direct_parent_grouping'] = $direct_parents;
447 if ($this->
attr(
'direction') ==
'down') {
448 foreach ($root_asset_ids as $root_asset_id) {
449 $asset_list = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($root_asset_id, array_keys($wanted_types), FALSE, NULL, NULL, $list_permission, $list_effective, $min_depth, $max_depth, TRUE, $link_value_wanted);
451 $assets_to_list = $this->
_combineAssets($logic, $asset_list, $assets_to_list);
454 foreach ($root_asset_ids as $root_asset_id) {
458 $asset_list = Array();
459 $new_asset_list = $GLOBALS[
'SQ_SYSTEM']->am->getParents($root_asset_id, array_keys($wanted_types), FALSE, NULL, $list_permission, $list_effective, $min_depth, $max_depth, TRUE, $link_value_wanted);
460 foreach ($new_asset_list as $k => $v) {
461 $asset_list[$k] = Array( 0 => Array(
'type_code' => $v));
463 $assets_to_list = $this->
_combineAssets($logic, $asset_list, $assets_to_list);
471 if ($min_depth ===
'0' && $max_depth !==
'0') {
472 $assets_to_list = $this->
_combineRootNodes($assets_to_list, $root_asset_ids, $wanted_types);
477 if (empty($assets_to_list))
return Array();
478 return $assets_to_list;
495 return parent::sortAssetList($assets_to_list, $sort_info);
519 if (is_null($existing))
return $new_assets;
521 if ($logic !==
'AND') {
523 $existing += $new_assets;
527 foreach ($existing as $assetid => $data) {
528 if (!isset($new_assets[$assetid])) {
529 unset($existing[$assetid]);
551 foreach ($root_nodes as $root_node) {
552 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($root_node);
553 if (!is_null($asset)) {
554 foreach ($types as $wanted_type => $include) {
555 if ($asset instanceof $wanted_type) {
556 $assets[$root_node] = Array(
558 'type_code' => $asset->type(),
559 'sort_value'=> $asset->short_name,
560 'first_letter'=> substr($asset->short_name, 0 , 1),
565 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);