Skip to content

Commit

Permalink
attempt not not count tags assigned to items that are not viewable
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylian committed Nov 16, 2014
1 parent 86c19ac commit 81e5edc
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 16 deletions.
120 changes: 105 additions & 15 deletions zp-core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,19 +1093,80 @@ function getAllTagsUnique() {
* @return array
*/
function getAllTagsCount() {
global $_zp_count_tags;
if (!is_null($_zp_count_tags))
return $_zp_count_tags;
$_zp_count_tags = array();
$sql = "SELECT DISTINCT tags.name, tags.id, (SELECT COUNT(*) FROM " . prefix('obj_to_tag') . " as object WHERE object.tagid = tags.id) AS count FROM " . prefix('tags') . " as tags ORDER BY `name`";
$tagresult = query($sql);
if ($tagresult) {
while ($tag = db_fetch_assoc($tagresult)) {
$_zp_count_tags[$tag['name']] = $tag['count'];
}
db_free_result($tagresult);
}
return $_zp_count_tags;
global $_zp_count_tags, $_zp_object_to_tags;
if (!is_null($_zp_count_tags))
return $_zp_count_tags;
$_zp_count_tags = array();
$sql = "SELECT DISTINCT tags.name, tags.id, (SELECT COUNT(*) FROM " . prefix('obj_to_tag') . " as object WHERE object.tagid = tags.id) AS count FROM " . prefix('tags') . " as tags ORDER BY `name`";
$tagresult = query($sql);
if ($tagresult) {
$tags = array();
while ($tag = db_fetch_assoc($tagresult)) {
$_zp_count_tags[$tag['name']] = getTagCountByAccess($tag);
}
db_free_result($tagresult);
}
return $_zp_count_tags;
}

/**
* Checks if the items a tag is assigned can be viewed by the current users and returns the corrected count if not
* Helper function for getAllTagsCount()
*
* @global obj $_zp_zenpage
* @param array $tag Array representing a tag containing its name and id at least
* @return int
*/
function getTagCountByAccess($tag) {
global $_zp_zenpage,$_zp_object_to_tags;
if($tag['count'] == 0) {
return $tag['count'];
}
if (is_null($_zp_object_to_tags)) {
$sql = "SELECT tagid, type, objectid FROM " . prefix('obj_to_tag') . " ORDER BY tagid";
$_zp_object_to_tags = query_full_array($sql);
}
$hidealbums = getNotViewableAlbums();
$hideimages = getNotViewableImages();
if (extensionEnabled('Zenpage')) {
$hidenews = $_zp_zenpage->getNotViewableNews();
$hidepages = $_zp_zenpage->getNotViewablePages();
}
$count = '';
foreach($_zp_object_to_tags as $tagcheck) {
if ($tagcheck['tagid'] == $tag['id']) {
switch ($tagcheck['type']) {
case 'albums':
if (!in_array($tagcheck['objectid'], $hidealbums)) {
$count++;
}
break;
case 'images':
if (!in_array($tagcheck['objectid'], $hideimages)) {
$count++;
}
break;
case 'news':
if (extensionEnabled('Zenpage') && ZP_NEWS_ENABLED) {
if (!in_array($tagcheck['objectid'], $hidenews)) {
$count++;
}
}
break;
case 'pages':
if (extensionEnabled('Zenpage') && ZP_PAGES_ENABLED) {
if (!in_array($tagcheck['objectid'], $hidepages)) {
$count++;
}
}
break;
}
}
}
if(empty($count)) {
$count = 0;
}
return $count;
}

/**
Expand Down Expand Up @@ -1385,10 +1446,9 @@ function sortMultiArray($array, $index, $descending = false, $natsort = true, $c
* @return array
*/
function getNotViewableAlbums() {
global $_zp_not_viewable_album_list, $_zp_gallery;
global $_zp_not_viewable_album_list;
if (zp_loggedin(ADMIN_RIGHTS | MANAGE_ALL_ALBUM_RIGHTS))
return array(); //admins can see all
$hint = '';
if (is_null($_zp_not_viewable_album_list)) {
$sql = 'SELECT `folder`, `id`, `password`, `show` FROM ' . prefix('albums') . ' WHERE `show`=0 OR `password`!=""';
$result = query($sql);
Expand All @@ -1411,6 +1471,36 @@ function getNotViewableAlbums() {
}

/**
* Returns a list of image IDs that the current viewer is not allowed to see
*
* @return array
*/
function getNotViewableImages() {
global $_zp_not_viewable_image_list;
if (zp_loggedin(ADMIN_RIGHTS | MANAGE_ALL_ALBUM_RIGHTS)) {
return array(); //admins can see all
}
$hidealbums = getNotViewableAlbums();
$where = '';
if (!is_null($hidealbums)) {
foreach ($hidealbums as $id) {
$where .= ' AND `albumid` = ' . $id;
}
}
if (is_null($_zp_not_viewable_image_list)) {
$sql = 'SELECT `id` FROM ' . prefix('images') . ' WHERE `show`= 0' . $where;
$result = query($sql);
if ($result) {
$_zp_not_viewable_image_list = array();
while ($row = db_fetch_assoc($result)) {
$_zp_not_viewable_image_list[] = $row['id'];
}
}
}
return $_zp_not_viewable_image_list;
}

/**
* Checks to see if a URL is valid
*
* @param string $url the URL being checked
Expand Down
56 changes: 55 additions & 1 deletion zp-core/zp-extensions/zenpage/zenpage-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,33 @@ function getPages($published = NULL, $toplevel = false, $number = NULL, $sorttyp
return $all_pages;
}

/**
* Returns a list of Zenpage page IDs that the current viewer is not allowed to see
* Helper function to be used with getAllTagsUnique() and getAllTagsCount()
* Note if the Zenpage plugin is not enabled but items exists this returns no IDs so you need an extra check afterwards!
*
* @return array
*/
function getNotViewablePages() {
global $_zp_not_viewable_pages_list;
if (zp_loggedin(ADMIN_RIGHTS | ALL_PAGES_RIGHTS)) {
return array(); //admins can see all
}
if (is_null($_zp_not_viewable_pages_list)) {
$items = $this->getPages(true, false, NULL, NULL, NULL);
if (!is_null($items)) {
$_zp_not_viewable_pages_list = array();
foreach ($items as $item) {
$obj = new ZenpageNews($item['titlelink']);
if (!$obj->isProtected()) {
$_zp_not_viewable_pages_list[] = $obj->getID();
}
}
}
}
return $_zp_not_viewable_pages_list;
}

/* * ********************************* */
/* general news article functions */
/* * ********************************* */
Expand Down Expand Up @@ -404,8 +431,35 @@ function getArticles($articles_per_page = 0, $published = NULL, $ignorepaginatio
}
return $result;
}

/**
* Returns a list of Zenpage news article IDs that the current viewer is not allowed to see
* Helper function to be used with getAllTagsUnique() and getAllTagsCount() or db queries only
* Note if the Zenpage plugin is not enabled but items exists this returns no IDs so you need an extra check afterwards!
*
* @return array
*/
function getNotViewableNews() {
global $_zp_not_viewable_news_list;
if (zp_loggedin(ADMIN_RIGHTS | ALL_NEWS_RIGHTS)) {
return array(); //admins can see all
}
if (is_null($_zp_not_viewable_news_list)) {
$items = $this->getArticles(0, 'published', true, NULL, NULL, NULL, NULL);
if (!is_null($items)) {
$_zp_not_viewable_news_list = array();
foreach ($items as $item) {
$obj = new ZenpageNews($item['titlelink']);
if ($obj->isProtected()) {
$_zp_not_viewable_news_list[] = $obj->getID();
}
}
}
}
return $_zp_not_viewable_news_list;
}

/**
/**
* Returns an article from the album based on the index passed.
*
* @param int $index
Expand Down

0 comments on commit 81e5edc

Please sign in to comment.