0) {
$tile = $existing_tile_ref;
$rebuild_order = false;
} else {
ps_query(
"INSERT INTO dash_tile
(url,
link,
title,
reload_interval_secs,
all_users,
default_order_by,
resource_count,
allow_delete,txt)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
's', $url,
's', $link,
's', $title,
'i', $reload_interval,
'i', $all_users,
'i', $default_order_by,
'i', $resource_count,
'i', $delete,
's', $text
]
);
$tile = sql_insert_id();
foreach ($specific_user_groups as $user_group_id) {
add_usergroup_dash_tile($user_group_id, $tile, $default_order_by);
build_usergroup_dash($user_group_id, 0, $tile);
}
}
# If tile already existed then this no reorder
if ($rebuild_order) {
reorder_default_dash();
}
if ($all_users == 1 && empty($specific_user_groups)) {
ps_query("DELETE FROM user_dash_tile WHERE dash_tile= ?", ['i', $tile]);
ps_query("INSERT user_dash_tile (user,dash_tile,order_by) SELECT user.ref,?,5 FROM user", ['i', $tile]);
}
hook('after_create_dash_tile', '', array($tile));
return $tile;
}
/*
* Update Dash tile based upon ref
* This updates the record in the dash_tile table
* If the all_user flag is being changed it will only get pushed out to users not removed. That action is specifically upon delete not edit as this is a flag
*/
function update_dash_tile($tile, $url, $link, $title, $reload_interval, $all_users, $tile_audience, $current_specific_user_groups, $specific_user_groups, $default_order_by, $resource_count, $text = "", $delete = 1)
{
global $userref;
if (!is_array($tile)) {
$tile = get_tile($tile);
}
#Sensible Defaults for insertion to Database
if (empty($reload_interval) || !is_numeric($reload_interval)) {
$reload_interval = 0;
}
$delete = $delete ? 1 : 0;
$all_users = $all_users ? 1 : 0;
if (!is_numeric($default_order_by)) {
$default_order_by = $tile["default_order_by"];
}
$resource_count = $resource_count ? 1 : 0;
ps_query(
"UPDATE dash_tile SET url= ?, link= ?, title= ?, reload_interval_secs= ?, all_users= ?, default_order_by= ?, resource_count= ?, allow_delete= ?, txt= ? WHERE ref= ?",
[
's', $url,
's', $link,
's', $title,
'i', $reload_interval,
'i', $all_users,
'i', $default_order_by,
'i', $resource_count,
'i', $delete,
's', $text,
'i', $tile['ref']
]
);
if ($tile_audience == 'true') { // All users tile
// Check if this was a specific usergroup tile
if (count($current_specific_user_groups) > 0 || $tile["all_users"] == 0) {
#Delete the users existing record to ensure they don't get a duplicate.
ps_query("DELETE FROM user_dash_tile WHERE dash_tile= ?", ['i', $tile['ref']]);
ps_query("INSERT user_dash_tile (user,dash_tile,order_by) SELECT user.ref, ?,5 FROM user", ['i', $tile['ref']]);
}
// This is an all users dash tile, delete any existing usergroup entries
ps_query("DELETE FROM usergroup_dash_tile WHERE dash_tile = ?", ['i', $tile['ref']]);
} elseif ($tile_audience == 'specific_user_groups') { // Specific usergroups tile
// This is a usergroup specific dash tile
// As is not meant for a specific user group, remove it from the users immediately
if (count($current_specific_user_groups) == 0) {
// This was an all users/usergroup dash tile, delete any existing user entries
ps_query("DELETE FROM user_dash_tile WHERE dash_tile = ?", ['i', $tile['ref']]);
}
// Remove tile from old user groups
foreach (array_diff($current_specific_user_groups, $specific_user_groups) as $remove_group) {
delete_usergroup_dash_tile($tile['ref'], $remove_group);
}
// Newly selected user groups.
foreach (array_diff($specific_user_groups, $current_specific_user_groups) as $add_group) {
add_usergroup_dash_tile($add_group, $tile['ref'], $default_order_by);
build_usergroup_dash($add_group, 0, $tile['ref']);
}
} else // Tile is now just for the current user
{
// This was an all users/usergroup dash tile, delete any existing user entries and add just for this user
ps_query("DELETE FROM usergroup_dash_tile WHERE dash_tile = ?", ['i', $tile['ref']]);
ps_query("DELETE FROM user_dash_tile WHERE dash_tile = ?", ['i', $tile['ref']]);
add_user_dash_tile($userref, $tile['ref'], $default_order_by);
}
hook('after_update_dash_tile');
}
/*
* Delete a dash tile
* @$tile, the dash_tile.ref number of the tile to be deleted
* @$cascade, whether this delete should remove the tile from all users.
*/
function delete_dash_tile($tile, $cascade = true, $force = false)
{
#Force Delete ignores the allow_delete flag (This allows removal of config tiles)
$allow_delete = $force ? "" : "AND allow_delete=1";
ps_query("DELETE FROM dash_tile WHERE ref= ? " . $allow_delete, ['i', $tile]);
if ($cascade) {
ps_query("DELETE FROM user_dash_tile WHERE dash_tile= ?", ['i', $tile]);
ps_query("DELETE FROM usergroup_dash_tile WHERE dash_tile = ?", ['i', $tile]);
}
hook('after_delete_dash_tile', '', array($tile, $cascade , $force));
}
/*
* Turn off push to all users "all_users" flag and cascade delete any existing entries users might have
* @$tile, the dash_tile.ref number of the tile to be hidden from all users
*/
function revoke_all_users_flag_cascade_delete($tile)
{
ps_query("UPDATE dash_tile SET `all_users`=0 WHERE `ref`= ?", ['i', $tile]);
ps_query("DELETE FROM `user_dash_tile` WHERE `dash_tile`= ?", ['i', $tile]);
}
/*
* Returns the position to append a tile to the default dash order
*/
function append_default_position()
{
$last_tile = ps_query("SELECT default_order_by from dash_tile order by default_order_by DESC LIMIT 1");
return isset($last_tile[0]["default_order_by"]) ? $last_tile[0]["default_order_by"] + 10 : 10;
}
/*
* Reorders the default dash,
* this is useful when you have just inserted a new tile or moved a tile and need to reorder them with the proper 10 gaps
* Tiles should be ordered with values 10,20,30,40,50,60,70 for easy insertion
*/
function reorder_default_dash()
{
$tiles = ps_query("SELECT ref FROM dash_tile WHERE all_users=1 ORDER BY default_order_by");
$order_by = 10 * count($tiles);
for ($i = count($tiles) - 1; $i >= 0; $i--) {
update_default_dash_tile_order($tiles[$i]["ref"], $order_by);
$order_by -= 10;
}
}
/*
* Simple updates a particular dash_tile with the new order_by.
* this does NOT apply to a users dash, that must done with the user_dash functions.
*/
function update_default_dash_tile_order($tile, $order_by)
{
return ps_query("UPDATE dash_tile SET default_order_by= ? WHERE ref= ?", ['i', $order_by, 'i', $tile]);
}
/*
* Gets the full content from a tile record row
*
*/
function get_tile($tile)
{
$result = ps_query("SELECT ref, title, txt, all_users, default_order_by, url, link, reload_interval_secs, resource_count, allow_delete FROM dash_tile WHERE ref= ?", ['i', $tile]);
return isset($result[0]) ? $result[0] : false;
}
/*
* Checks if an all_user tile is currently in use and therefore active for all_users
* Pass the dash_tile.ref of tile to check
*/
function all_user_dash_tile_active($tile)
{
return ps_query(
"SELECT
dash_tile.ref AS 'tile',
dash_tile.title,
dash_tile.url,
dash_tile.reload_interval_secs,
dash_tile.link,
dash_tile.default_order_by as 'order_by',
dash_tile.allow_delete
FROM dash_tile
WHERE
dash_tile.all_users=1
AND
dash_tile.ref= ?
AND
(
dash_tile.allow_delete=1
OR
(
dash_tile.allow_delete=0
AND
dash_tile.ref IN (SELECT DISTINCT user_dash_tile.dash_tile FROM user_dash_tile)
)
) ORDER BY default_order_by
",
['i', $tile]
);
}
/*
* Checks if a tile already exists.
* This is based upon a complete set of values so unless all values match exactly it will return false.
*
*/
function existing_tile($title, $all_users, $url, $link, $reload_interval, $resource_count, $text = "")
{
$existing = ps_query(
"SELECT ref FROM dash_tile WHERE url= ? AND link= ? AND title= ? AND reload_interval_secs= ? AND all_users= ? AND resource_count= ? AND txt= ?",
[
's', $url,
's', $link,
's', $title,
'i', $reload_interval,
'i', $all_users,
'i', $resource_count,
's', $text
]
);
return isset($existing[0]["ref"]);
}
/*
* Cleanup Duplicate and Loose Tiles
* This removes all unused tiles that are flagged as:
* "allowed to delete"
* AND not "all users"
*/
function cleanup_dash_tiles()
{
global $lang;
$tiles = ps_query(
"SELECT ref, title, txt, all_users, default_order_by, url, link, reload_interval_secs, resource_count, allow_delete FROM dash_tile
WHERE allow_delete = 1
AND all_users = 0
AND ref NOT IN (SELECT DISTINCT dash_tile FROM user_dash_tile)
AND ref NOT IN (SELECT DISTINCT dash_tile FROM usergroup_dash_tile)"
);
ps_query(
"DELETE FROM dash_tile
WHERE allow_delete = 1
AND all_users = 0
AND ref NOT IN (SELECT DISTINCT dash_tile FROM user_dash_tile)
AND ref NOT IN (SELECT DISTINCT dash_tile FROM usergroup_dash_tile)"
);
foreach ($tiles as $tile) {
log_activity($lang['manage_all_dash'], LOG_CODE_DELETED, $tile["title"], 'dash_tile', null, $tile["ref"]);
}
}
/*
* Checks if this tiles config is still active
* @param: $tile = tile record
* @param: $tilestyle = extracted tilestyle of this config tile
*/
function checkTileConfig($tile, $tile_style)
{
#Returns whether the config is still on for these tiles
switch ($tile_style) {
case "thmsl":
global $home_themeheaders;
return $home_themeheaders;
case "custm":
global $custom_home_panels;
return isset($custom_home_panels) ? checkConfigCustomHomePanels($tile, $tile_style) : false;
}
}
/*
* Checks the configuration for each custom tile.
* If the config for the tile is still there then return true
*/
function checkConfigCustomHomePanels($tile, $tile_style)
{
global $custom_home_panels;
$tile_config_set = false;
for ($n = 0; $n < count($custom_home_panels); $n++) {
if (existing_tile($tile["title"], $tile["all_users"], $tile["url"], $tile["link"], $tile["reload_interval_secs"], $tile["resource_count"], $tile["txt"])) {
$tile_config_set = true;
}
}
return $tile_config_set;
}
/*
* All dash tiles available to all_users
* If you provide a dash_tile ref it will check if this tile exists within the list of available tiles
*
*/
function get_alluser_available_tiles($tile = "null")
{
if (is_numeric($tile)) {
$tilecheck = 'AND ref = ?';
$params = ['i', $tile];
} else {
$tilecheck = '';
$params = [];
}
return ps_query(
"SELECT
dash_tile.ref,
dash_tile.ref as 'tile',
dash_tile.title,
dash_tile.txt,
dash_tile.link,
dash_tile.url,
dash_tile.reload_interval_secs,
dash_tile.resource_count,
dash_tile.all_users,
dash_tile.allow_delete,
dash_tile.default_order_by,
dash_tile.default_order_by AS `order_by`, # needed for get_default_dash()
(IF(ref IN (select distinct dash_tile FROM user_dash_tile),1,0)) as 'dash_tile'
FROM
dash_tile
WHERE
dash_tile.all_users=1
" . $tilecheck . "
AND ref NOT IN (SELECT dash_tile FROM usergroup_dash_tile)
ORDER BY
dash_tile,
default_order_by
",
$params
);
}
/*
* Retrieves the default dash which only display all_user tiles.
* This should only be accessible to thos with Dash Tile Admin permissions
*/
function get_default_dash($user_group_id = null, $edit_mode = false)
{
global $baseurl,$baseurl_short,$lang,$anonymous_login,$username;
#Build Tile Templates
$tiles = ps_query(
"SELECT
dash_tile.ref AS 'tile',
dash_tile.title,
dash_tile.url,
dash_tile.reload_interval_secs,
dash_tile.link,
dash_tile.default_order_by AS 'order_by',
dash_tile.allow_delete
FROM dash_tile
WHERE dash_tile.all_users = 1
AND dash_tile.ref NOT IN (SELECT dash_tile FROM usergroup_dash_tile)
AND (dash_tile.allow_delete=1 OR (dash_tile.allow_delete=0 AND dash_tile.ref IN (SELECT DISTINCT user_dash_tile.dash_tile FROM user_dash_tile)))
ORDER BY default_order_by"
);
// In edit_mode, as a super admin, we want to see all user dash tiles otherwise re-ordering will be broken
// due to tiles that are not visible but still being taken into account
$hidden_tiles = array();
$hidden_tile_class = '';
if ($edit_mode) {
$managed_tiles = $tiles;
$tiles = get_alluser_available_tiles();
$hidden_tile_class = ' HiddenTile';
foreach ($tiles as $all_user_available_tile) {
if (false === array_search($all_user_available_tile['ref'], array_column($managed_tiles, 'tile'))) {
$hidden_tiles[] = $all_user_available_tile['ref'];
}
}
}
if (!is_null($user_group_id)) {
$tiles = get_usergroup_available_tiles($user_group_id);
}
$order = 10;
if (count($tiles) == 0) {
echo escape($lang["nodashtilefound"]);
exit;
}
foreach ($tiles as $tile) {
$contents_tile_class = '';
if (($order != $tile["order_by"] || ($tile["order_by"] % 10) > 0) && is_null($user_group_id)) {
update_default_dash_tile_order($tile["tile"], $order);
} elseif ((!isset($tile['default_order_by']) || $order != $tile['default_order_by'] || ($tile['default_order_by'] % 10) > 0) && !is_null($user_group_id)) {
update_usergroup_dash_tile_order($user_group_id, $tile['tile'], $order);
}
$order += 10;
$tile_custom_style = '';
$buildstring = explode('?', $tile['url']);
parse_str(str_replace('&', '&', ($buildstring[1] ?? "")), $buildstring);
if (isset($buildstring['tltype']) && allow_tile_colour_change($buildstring['tltype']) && isset($buildstring['tlstylecolour'])) {
$tile_custom_style .= get_tile_custom_style($buildstring);
}
if (in_array($tile['tile'], $hidden_tiles)) {
$contents_tile_class .= $hidden_tile_class;
}
?>
href=""
onclick="if(dragging){dragging=false;return false;}"
class="HomePanel DashTile DashTileDraggable "
id="tile"
>
= 0; $i--) {
update_usergroup_dash_tile_order($usergroup, $usergroup_tiles[$i]['dash_tile'], $order_by);
$order_by -= 10;
}
}
/**
* Update the display order of a specific dashboard tile for a user group.
*
* @param int $usergroup The user group ID to which the dashboard tile belongs.
* @param int $tile The ID of the dashboard tile to update.
* @param int $default_order_by The new default order position for the tile within the user group's dashboard.
* @return void
*/
function update_usergroup_dash_tile_order($usergroup, $tile, $default_order_by)
{
ps_query(
"UPDATE usergroup_dash_tile
SET default_order_by = ?
WHERE usergroup = ?
AND dash_tile = ?",
['i', $default_order_by, 'i', $usergroup, 'i', $tile]
);
}
/**
* build_usergroup_dash - rebuild the usergroup tiles for either a specific user or all users.
* If a specific tile is passed e.g. if called from create_dash_tile then we just add it to the end
*
* @param integer $user_group ID of group to add tile(s) to
* @param integer $user_id ID of individual user to add tile(s) to
* @param integer $newtileid ID of a single tile to add on the end
*
* @return void
*/
function build_usergroup_dash($user_group, $user_id = 0, $newtileid = "")
{
if ($newtileid != "" && is_numeric($newtileid)) {
$user_group_tiles = array($newtileid);
} else {
$user_group_tiles = ps_array(
"SELECT
dash_tile.ref AS `value`
FROM
usergroup_dash_tile
JOIN
dash_tile ON usergroup_dash_tile.dash_tile = dash_tile.ref
WHERE
usergroup_dash_tile.usergroup = ?
AND dash_tile.all_users = 1
AND (dash_tile.allow_delete = 1
OR (dash_tile.allow_delete = 0
AND dash_tile.ref IN (SELECT DISTINCT
user_dash_tile.dash_tile
FROM
user_dash_tile)))
ORDER BY usergroup_dash_tile.default_order_by;",
array("i",$user_group)
);
}
// If client code has specified a user ID, then just add the tiles for it
if (is_numeric($user_id) && 0 < $user_id) {
$starting_order = 99999;
foreach ($user_group_tiles as $tile) {
add_user_dash_tile($user_id, $tile, $starting_order, false); // No need to reorder as we have already set the position
$starting_order += 10;
}
return;
}
$user_list = ps_array("SELECT ref AS `value` FROM user WHERE usergroup = ?", array("i",$user_group));
foreach ($user_list as $user) {
$starting_order = 99999;
foreach ($user_group_tiles as $tile) {
add_user_dash_tile($user, $tile, $starting_order, false); // No need to reorder as we have already set the position
$starting_order += 10;
}
}
}
/**
* Retrieve user group IDs associated with a specific dashboard tile.
*
* This function fetches the IDs of all user groups that have access to a given dashboard tile.
* Each user group ID is returned as a value in the resulting array.
*
* @param int $tile_id The ID of the dashboard tile for which to retrieve associated user groups.
* @return array An array of user group IDs that are linked to the specified dashboard tile.
*/
function get_tile_user_groups($tile_id)
{
return ps_array("SELECT usergroup AS `value` FROM usergroup_dash_tile WHERE dash_tile = ?", array("i",$tile_id));
}
/**
* Retrieve dashboard tiles available to a specific user group.
*
* This function returns the tiles that are accessible by a particular user group, optionally filtered by a specific tile ID.
* The function ensures that the user group ID is numeric and fetches the tiles that are either available to all users
* or specifically assigned to the provided user group.
*
* @param int $user_group_id The ID of the user group for which to retrieve available tiles.
* @param int|string $tile (optional) Specific tile ID to filter by; if omitted, all available tiles for the user group are returned.
* @return array An array of associative arrays, each representing a dashboard tile with keys:
* - 'ref' (int): Unique reference ID of the tile.
* - 'tile' (int): Same as 'ref', included for compatibility.
* - 'title' (string): The title of the tile.
* - 'txt' (string): Text content of the tile.
* - 'link' (string): Link associated with the tile.
* - 'url' (string): URL of an external resource, if any.
* - 'reload_interval_secs' (int): Time interval for reloading the tile content in seconds.
* - 'resource_count' (int): Resource count, if applicable.
* - 'all_users' (int): Flag indicating if the tile is available to all users.
* - 'allow_delete' (int): Flag indicating if the tile can be deleted.
* - 'default_order_by' (int): Default order for the tile.
* - 'order_by' (int|null): Order for the tile within the user group; may be null if not explicitly set.
* - 'dash_tile' (int): Always set to 1, indicating it is a dashboard tile.
*
* @throws \Exception If $user_group_id is not a numeric value.
*/
function get_usergroup_available_tiles($user_group_id, $tile = '')
{
if (!is_numeric($user_group_id)) {
trigger_error('$user_group_id has to be a number');
}
$tile_sql = '';
$params = [];
if ('' != $tile) {
$tile_sql = "AND dt.ref = ?";
$params = ['i', $tile];
}
$params[] = 'i';
$params[] = $user_group_id;
return ps_query(
"SELECT
dt.ref,
dt.ref AS `tile`,
dt.title,
dt.txt,
dt.link,
dt.url,
dt.reload_interval_secs,
dt.resource_count,
dt.all_users,
dt.allow_delete,
dt.default_order_by,
udt.order_by,
1 AS 'dash_tile'
FROM dash_tile AS dt
LEFT JOIN usergroup_dash_tile AS udt ON dt.ref = udt.dash_tile
WHERE dt.all_users = 1
AND udt.usergroup = ? {$tile_sql}
ORDER BY udt.default_order_by ASC",
$params
);
}
/**
* Get usergroup_dash_tile record
*
* @param integer $tile_id
* @param integer $user_group_id
*
* @return array
*/
function get_usergroup_tile($tile_id, $user_group_id)
{
$return = ps_query(
"SELECT ref, usergroup, dash_tile, default_order_by, order_by
FROM usergroup_dash_tile
WHERE dash_tile = ?
AND usergroup = ?",
['i', $tile_id, 'i', $user_group_id]
);
if (0 < count($return)) {
return $return[0];
}
return array();
}
/*
* User Dash Functions
*/
/*
* Add a tile to a users dash
* Affects the user_dash_tile table, tile must be the ref of a record from dash_tile
*
*/
function add_user_dash_tile($user, $tile, $order_by, $reorder = true)
{
if (!is_numeric($user) || !is_numeric($tile)) {
return false;
}
if (!is_numeric($order_by)) {
$order_by = append_user_position($user);
$reorder = false;
}
ps_query(
"INSERT INTO user_dash_tile (user,dash_tile,order_by)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE order_by= ?",
['i', $user, 'i', $tile, 'i', $order_by, 'i', $order_by]
);
if ($reorder) {
reorder_user_dash($user);
}
return true;
}
/*
* Get user_dash_tile record,
* Provide the user_dash_tile ref as the $tile
* this a place holder which links a dash_tile template with the user and the order that that tile should appear on THIS users dash
*
*/
function get_user_tile($usertile, $user)
{
$result = ps_query(
"SELECT ref, user, dash_tile, order_by
FROM user_dash_tile
WHERE ref= ?
AND user= ?",
['i', $usertile, 'i', $user]
);
return isset($result[0]) ? $result[0] : false;
}
/*
* Builds a users dash, this is a quick way of adding all_user tiles back to a users dash.
* The Add_user_dash_tile function used checks for an existing match so that it won't duplicate tiles on a users dash
*
*/
function create_new_user_dash($user)
{
$tiles = ps_query(
"SELECT
dash_tile.ref AS 'tile',
dash_tile.title,
dash_tile.url,
dash_tile.reload_interval_secs,
dash_tile.link,
dash_tile.default_order_by AS 'order'
FROM dash_tile
WHERE dash_tile.all_users = 1
AND ref NOT IN (SELECT dash_tile FROM usergroup_dash_tile)
AND (
dash_tile.allow_delete=1 OR (
dash_tile.allow_delete=0 AND dash_tile.ref IN (
SELECT DISTINCT user_dash_tile.dash_tile FROM user_dash_tile
)
)
)
ORDER BY default_order_by"
);
foreach ($tiles as $tile) {
add_user_dash_tile($user, $tile["tile"], $tile["order"], false);
}
}
/*
* Updates a user_dash_tile record for a specific tile on a users dash with an order.
*
*/
function update_user_dash_tile_order($user, $tile, $order_by)
{
return ps_query("UPDATE user_dash_tile SET order_by= ? WHERE user= ? and ref= ?", ['i', $order_by, 'i', $user, 'i', $tile]);
}
/*
* Delete a tile from a user dash
* this will only remove the tile from this users dash.
* It must be the ref of the row in the user_dash_tile
* this also performs cleanup to ensure that there are no unused templates in the dash_tile table
*
*/
function delete_user_dash_tile($usertile, $user)
{
global $lang;
if (!is_numeric($usertile) || !is_numeric($user)) {
return false;
}
$row = get_user_tile($usertile, $user);
ps_query("DELETE FROM user_dash_tile WHERE ref= ? and user= ?", ['i', $usertile, 'i', $user]);
if (!isset($row["dash_tile"]) || !is_numeric($row["dash_tile"])) {
return false;
}
$existing = ps_query("SELECT count(*) as 'count' FROM user_dash_tile WHERE dash_tile= ?", ['i', $row['dash_tile']]);
if ($existing[0]["count"] < 1) {
$tile = get_tile($row["dash_tile"]);
delete_dash_tile($row["dash_tile"]);
log_activity($lang['manage_all_dash'], LOG_CODE_DELETED, ($tile["title"] ?? ""), 'dash_tile', null, $row["dash_tile"]);
}
}
/*
* Remove all tiles from a users dash
* Purge option does the cleanup in dash_tile removing any unused tiles
* Turn purge off if you are just doing a quick rebuild of the tiles.
*/
function empty_user_dash($user, $purge = true)
{
global $lang;
$usertiles = ps_query(
"SELECT udt.dash_tile,dt.title
FROM user_dash_tile udt
JOIN dash_tile dt ON udt.dash_tile=dt.ref
WHERE udt.user= ?",
['i', $user]
);
ps_query("DELETE FROM user_dash_tile WHERE user= ?", ['i', $user]);
if ($purge) {
foreach ($usertiles as $tile) {
$existing = ps_query("SELECT count(*) as 'count' FROM user_dash_tile WHERE dash_tile= ?", ['i', $tile['dash_tile']]);
if ($existing[0]["count"] < 1) {
delete_dash_tile($tile["dash_tile"]);
log_activity($lang['manage_all_dash'], LOG_CODE_DELETED, $tile["title"], 'dash_tile', null, $tile["dash_tile"]);
}
}
}
}
/*
* Reorders the users dash,
* this is useful when you have just inserted a new tile or moved a tile and need to reorder them with the proper 10 gaps
* Tiles should be ordered with values 10,20,30,40,50,60,70 for easy insertion
*/
function reorder_user_dash($user)
{
$user_tiles = ps_query(
"SELECT user_dash_tile.ref
FROM user_dash_tile
LEFT JOIN dash_tile ON user_dash_tile.dash_tile = dash_tile.ref
WHERE user_dash_tile.user= ?
ORDER BY user_dash_tile.order_by",
['i', $user]
);
if (count($user_tiles) < 2) {
return;
}
$order_by = (10 * count($user_tiles)) + 10; # Begin ordering at 10 for first position, not 0.
$sql = "UPDATE user_dash_tile SET order_by = (CASE ";
$params = [];
for ($i = count($user_tiles) - 1; $i >= 0; $i--) {
$sql .= " WHEN ref= ? THEN ? ";
$order_by -= 10;
$params = array_merge($params, ['i', $user_tiles[$i]["ref"], 'i', $order_by]);
}
$sql .= " END) WHERE user='" . $user . "'";
ps_query($sql, $params);
}
/*
* Returns the position for a tile at the end of existing tiles
*
*/
function append_user_position($user)
{
$last_tile = ps_query("SELECT order_by FROM user_dash_tile WHERE user= ? ORDER BY order_by DESC LIMIT 1", ['i', $user]);
return isset($last_tile[0]["order_by"]) ? $last_tile[0]["order_by"] + 10 : 10;
}
/*
* All dash tiles available to the supplied userref
* If you provide a dash_tile ref it will check if this tile exists within the list of available tiles to the user
*
*/
function get_user_available_tiles($user, $tile = "null")
{
$tilecheck = '';
$params = [];
if (is_numeric($tile)) {
$tilecheck = 'WHERE ref = ?';
$params = ['i', $tile];
}
return ps_query(
"SELECT
result.*
FROM
( (
SELECT
dash_tile.ref,
'' as 'dash_tile',
'' as 'usertile',
'' as 'user',
'' as 'order_by',
dash_tile.ref as 'tile',
dash_tile.title,
dash_tile.txt,
dash_tile.link,
dash_tile.url,
dash_tile.resource_count,
dash_tile.all_users,
dash_tile.allow_delete,
dash_tile.default_order_by
FROM
dash_tile
WHERE
dash_tile.all_users = 1
AND
ref
NOT IN
(
SELECT
dash_tile.ref
FROM
user_dash_tile
RIGHT OUTER JOIN
dash_tile
ON
user_dash_tile.dash_tile = dash_tile.ref
WHERE
user_dash_tile.user = ?
)
AND ref NOT IN (SELECT dash_tile FROM usergroup_dash_tile)
)
UNION
(
SELECT
dash_tile.ref,
user_dash_tile.dash_tile,
user_dash_tile.ref as 'usertile',
user_dash_tile.user,
user_dash_tile.order_by,
dash_tile.ref as 'tile',
dash_tile.title,
dash_tile.txt,
dash_tile.link,
dash_tile.url,
dash_tile.resource_count,
dash_tile.all_users,
dash_tile.allow_delete,
dash_tile.default_order_by
FROM
user_dash_tile
RIGHT OUTER JOIN
dash_tile
ON
user_dash_tile.dash_tile = dash_tile.ref
WHERE
user_dash_tile.user = ?
)
) result
" . $tilecheck . "
ORDER BY result.order_by,result.default_order_by
",
array_merge(['i', $user, 'i', $user], $params)
);
}
/*
* Returns a users dash along with all necessary scripts and tools for manipulation
* checks for the permissions which allow for deletions and manipulation of all_user tiles from the dash
*
*/
function get_user_dash($user)
{
global $baseurl,$baseurl_short,$lang,$help_modal;
#Build User Dash and recalculate order numbers on display
$user_tiles = ps_query(
"SELECT
dash_tile.ref AS 'tile',
dash_tile.title,
dash_tile.all_users,
dash_tile.url,
dash_tile.reload_interval_secs,
dash_tile.link,
user_dash_tile.ref AS 'user_tile',
user_dash_tile.order_by
FROM user_dash_tile
JOIN dash_tile ON user_dash_tile.dash_tile = dash_tile.ref
WHERE user_dash_tile.user = ?
ORDER BY user_dash_tile.order_by ASC, dash_tile.ref DESC",
array("i", $user)
);
$order = 10;
foreach ($user_tiles as $tile) {
if ($order != $tile["order_by"] || ($tile["order_by"] % 10) > 0) {
update_user_dash_tile_order($user, $tile["user_tile"], $order);
}
$order += 10;
$tile_custom_style = '';
$buildstring = explode('?', $tile['url']);
list($url_page, $buildstring) = $buildstring;
parse_str(str_replace('&', '&', $buildstring), $buildstring);
if ($tile['all_users'] == 1 && strpos($tile['link'], 'team_analytics_edit.php') !== false) {
// Dash tile is a graph from an analytics report. Clicking this tile should only link to the analytics report for the report owner.
// The tile won't do anything for other users as they don't have access to view the report.
if (!isset($user_analytics_reports)) {
$user_analytics_reports = ps_array('SELECT ref AS `value` FROM user_report WHERE user = ?', array('i', $user));
}
$analytics_report_id = (int) substr(strrchr($tile['link'], '='), 1);
if (!in_array($analytics_report_id, $user_analytics_reports)) {
$tile['link'] = '';
}
}
$tlsize = (isset($buildstring['tlsize']) ? $buildstring['tlsize'] : '');
if (isset($buildstring['tltype']) && allow_tile_colour_change($buildstring['tltype']) && isset($buildstring['tlstylecolour'])) {
$tile_custom_style .= get_tile_custom_style($buildstring);
}
?>
href=""
onClick="if(dragging){dragging=false;return false;}
"
class="HomePanel DashTile DashTileDraggable "
tile=""
id="user_tile"
>
$resource_data['ref'],
"field{$view_title_field}" => get_data_by_field($resource_data['ref'], $view_title_field),
"resource_type" => $resource_data['resource_type'],
"file_extension" => $resource_data['file_extension']);
}
return $resources;
}
/**
* Validate the type of dash tile and check that the style provided is valid for it.
*
* @param string $type Tile type name.
* @param string $style Tile style name.
*
* @return string Will return the style value provided if correct, the first defined style or blank if no styles defined.
*/
function validate_tile_style(string $type, string $style)
{
global $tile_styles;
if (isset($tile_styles) && array_key_exists($type, $tile_styles)) {
if (count($tile_styles[$type]) === 0) {
return '';
}
if (in_array($style, $tile_styles[$type])) {
return $style;
} else {
return $tile_styles[$type][0];
}
} else {
return '';
}
}
/**
* Sanitise the url provided when saving a dash tile. This function will take the value obtained by the form and pass it through if valid.
* If the url supplied is invalid, a blank value will be returned allowing the default standard tile type to be used.
*
* @param string $buildurl url supplied when dash tile is edited, containing a number of optional parameters.
*
* @return string A valid url or empty string if invalid.
*/
function validate_build_url($buildurl)
{
global $tile_styles;
if ($buildurl != "") {
# Sanitise the url provided.
$build_url_parts = explode('?', $buildurl);
$valid_tile_urls = array();
$valid_tile_urls[] = 'pages/ajax/dash_tile.php';
$valid_tile_urls[] = 'pages/team/ajax/graph.php';
if (!in_array($build_url_parts[0], $valid_tile_urls)) {
// Url is invalid
$buildurl = "";
} else {
parse_str(($build_url_parts[1] ?? ""), $build_url_parts_param);
foreach ($build_url_parts_param as $param => $value) {
switch ($param) {
case 'tltype':
# type checks
if (!array_key_exists($value, $tile_styles)) {
$buildurl = "";
}
break;
case 'tlsize':
# size checks
if (!in_array($value, array('single','double',''))) {
$buildurl = "";
}
break;
case 'tlstyle':
# style checks
$all_tile_styles = array();
foreach ($tile_styles as $tile_type_style) {
$all_tile_styles = array_merge($all_tile_styles, $tile_type_style);
}
if (!in_array($value, $all_tile_styles)) {
$buildurl = "";
}
break;
case 'promimg':
# img checks
if (!is_int_loose($value) && !is_bool($build_url_parts_param[1])) {
$buildurl = "";
}
break;
}
}
}
}
return $buildurl;
}
/**
* Generate client side logic for doing expensive computation async for retrieving the tile background and total results count.
*
* @param array $tile Tile information {@see pages/ajax/dash_tile.php}
* @param string $tile_id HTML ID for the container div
* @param int $tile_width Tile width {@see pages/ajax/dash_tile.php}
* @param int $tile_height Tile height {@see pages/ajax/dash_tile.php}
* @param int $promoted_image ID of the promoted resource (for background)
*/
function tltype_srch_generate_js_for_background_and_count(array $tile, string $tile_id, int $tile_width, int $tile_height, int $promoted_image)
{
// Prevent function from running for the wrong tile type and style
parse_str(parse_url($tile['url'] ?? '', PHP_URL_QUERY), $tile_meta);
if (
!(
isset($tile_meta['tltype'], $tile_meta['tlstyle'])
&& $tile_meta['tltype'] === 'srch'
&& in_array($tile_meta['tlstyle'], $GLOBALS['tile_styles']['srch'])
)
) {
return;
}
$tile_style = $tile_meta['tlstyle'];
?>