| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
* Elgg tags |
|---|
| 4 |
* Functions for managing tags and tag clouds. |
|---|
| 5 |
* |
|---|
| 6 |
* @package Elgg |
|---|
| 7 |
* @subpackage Core |
|---|
| 8 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2 |
|---|
| 9 |
* @author Curverider Ltd <info@elgg.com> |
|---|
| 10 |
* @copyright Curverider Ltd 2008-2009 |
|---|
| 11 |
* @link http://elgg.org/ |
|---|
| 12 |
*/ |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
/** |
|---|
| 16 |
* The algorithm working out the size of font based on the number of tags. |
|---|
| 17 |
* This is quick and dirty. |
|---|
| 18 |
*/ |
|---|
| 19 |
function calculate_tag_size($min, $max, $number_of_tags, $buckets = 6) |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
$delta = (($max - $min) / $buckets); |
|---|
| 23 |
$thresholds = array(); |
|---|
| 24 |
|
|---|
| 25 |
for ($n=1; $n <= $buckets; $n++) { |
|---|
| 26 |
$thresholds[$n-1] = ($min + $n) * $delta; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
if ($thresholds[$buckets-1]>$max) $thresholds[$buckets-1] = $max; |
|---|
| 31 |
|
|---|
| 32 |
$size = 0; |
|---|
| 33 |
for ($n = 0; $n < count($thresholds); $n++) { |
|---|
| 34 |
if ($number_of_tags >= $thresholds[$n]) |
|---|
| 35 |
$size = $n; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
return $size; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
* This function generates an array of tags with a weighting. |
|---|
| 43 |
* |
|---|
| 44 |
* @param array $tags The array of tags. |
|---|
| 45 |
* @return An associated array of tags with a weighting, this can then be mapped to a display class. |
|---|
| 46 |
*/ |
|---|
| 47 |
function generate_tag_cloud(array $tags, $buckets = 6) |
|---|
| 48 |
{ |
|---|
| 49 |
$cloud = array(); |
|---|
| 50 |
|
|---|
| 51 |
$min = 65535; |
|---|
| 52 |
$max = 0; |
|---|
| 53 |
|
|---|
| 54 |
foreach ($tags as $tag) |
|---|
| 55 |
{ |
|---|
| 56 |
$cloud[$tag]++; |
|---|
| 57 |
|
|---|
| 58 |
if ($cloud[$tag]>$max) $max = $cloud[$tag]; |
|---|
| 59 |
if ($cloud[$tag]<$min) $min = $cloud[$tag]; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
foreach ($cloud as $k => $v) |
|---|
| 63 |
$cloud[$k] = calculate_tag_size($min, $max, $v, $buckets); |
|---|
| 64 |
|
|---|
| 65 |
return $cloud; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
* Get an array of tags with weights for use with the output/tagcloud view. |
|---|
| 70 |
* |
|---|
| 71 |
* @param int $threshold Get the threshold of minimum number of each tags to bother with (ie only show tags where there are more than $threshold occurances) |
|---|
| 72 |
* @param int $limit Number of tags to return |
|---|
| 73 |
* @param string $metadata_name Optionally, the name of the field you want to grab for |
|---|
| 74 |
* @param string $entity_type Optionally, the entity type ('object' etc) |
|---|
| 75 |
* @param string $entity_subtype The entity subtype, optionally |
|---|
| 76 |
* @param int $owner_guid The GUID of the tags owner, optionally |
|---|
| 77 |
* @param int $site_guid Optionally, the site to restrict to (default is the current site) |
|---|
| 78 |
* @return array|false Array of objects with ->tag and ->total values, or false on failure |
|---|
| 79 |
*/ |
|---|
| 80 |
|
|---|
| 81 |
function get_tags($threshold = 1, $limit = 10, $metadata_name = "", $entity_type = "object", $entity_subtype = "", $owner_guid = "", $site_guid = -1) { |
|---|
| 82 |
|
|---|
| 83 |
global $CONFIG; |
|---|
| 84 |
|
|---|
| 85 |
$threshold = (int) $threshold; |
|---|
| 86 |
$limit = (int) $limit; |
|---|
| 87 |
|
|---|
| 88 |
if (!empty($metadata_name)) { |
|---|
| 89 |
$metadata_name = (int) get_metastring_id($metadata_name); |
|---|
| 90 |
} else { |
|---|
| 91 |
$metadata_name = 0; |
|---|
| 92 |
} |
|---|
| 93 |
$entity_subtype = get_subtype_id($entity_type, $entity_subtype); |
|---|
| 94 |
$entity_type = sanitise_string($entity_type); |
|---|
| 95 |
|
|---|
| 96 |
if ($owner_guid != "") |
|---|
| 97 |
if (is_array($owner_guid)) { |
|---|
| 98 |
foreach($owner_guid as $key => $val) |
|---|
| 99 |
$owner_guid[$key] = (int) $val; |
|---|
| 100 |
} else { |
|---|
| 101 |
$owner_guid = (int) $owner_guid; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
if ($site_guid < 0) { |
|---|
| 105 |
$site_guid = $CONFIG->site_id; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
$query = "SELECT msvalue.string as tag, count(msvalue.id) as total "; |
|---|
| 111 |
$query .= "FROM {$CONFIG->dbprefix}entities e join {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid "; |
|---|
| 112 |
$query .= " join {$CONFIG->dbprefix}entity_subtypes subtype on subtype.id = e.subtype "; |
|---|
| 113 |
$query .= " join {$CONFIG->dbprefix}metastrings msvalue on msvalue.id = md.value_id "; |
|---|
| 114 |
|
|---|
| 115 |
$query .= " where msvalue.string != '' "; |
|---|
| 116 |
|
|---|
| 117 |
if ($metadata_name > 0) { |
|---|
| 118 |
$query .= " and md.name_id = {$metadata_name} "; |
|---|
| 119 |
} |
|---|
| 120 |
if ($site_guid > 0) { |
|---|
| 121 |
$query .= " and e.site_guid = {$site_guid} "; |
|---|
| 122 |
} |
|---|
| 123 |
if ($entity_subtype > 0) { |
|---|
| 124 |
$query .= " and e.subtype = {$entity_subtype} "; |
|---|
| 125 |
} |
|---|
| 126 |
if ($entity_type != "") { |
|---|
| 127 |
$query .= " and e.type = '{$entity_type}' "; |
|---|
| 128 |
} |
|---|
| 129 |
if (is_array($owner_guid)) { |
|---|
| 130 |
$query .= " and e.container_guid in (".implode(",",$owner_guid).")"; |
|---|
| 131 |
} else if (is_int($owner_guid)) { |
|---|
| 132 |
$query .= " and e.container_guid = {$owner_guid} "; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
//$query .= " and (e.access_id in {$access} or (e.access_id = " . ACCESS_PRIVATE . " and e.owner_guid = {$userid}))"; |
|---|
| 137 |
$query .= ' and ' . get_access_sql_suffix("e"); |
|---|
| 138 |
|
|---|
| 139 |
$query .= " group by msvalue.string having count(msvalue.id) > {$threshold} order by count(msvalue.id) desc limit {$limit} "; |
|---|
| 140 |
|
|---|
| 141 |
return get_data($query); |
|---|
| 142 |
|
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
* Loads and displays a tagcloud given particular criteria. |
|---|
| 147 |
* |
|---|
| 148 |
* @param int $threshold Get the threshold of minimum number of each tags to bother with (ie only show tags where there are more than $threshold occurances) |
|---|
| 149 |
* @param int $limit Number of tags to return |
|---|
| 150 |
* @param string $metadata_name Optionally, the name of the field you want to grab for |
|---|
| 151 |
* @param string $entity_type Optionally, the entity type ('object' etc) |
|---|
| 152 |
* @param string $entity_subtype The entity subtype, optionally |
|---|
| 153 |
* @param int $owner_guid The GUID of the tags owner, optionally |
|---|
| 154 |
* @param int $site_guid Optionally, the site to restrict to (default is the current site) |
|---|
| 155 |
* @return string THe HTML (or other, depending on view type) of the tagcloud. |
|---|
| 156 |
*/ |
|---|
| 157 |
|
|---|
| 158 |
function display_tagcloud($threshold = 1, $limit = 10, $metadata_name = "", $entity_type = "object", $entity_subtype = "", $owner_guid = "", $site_guid = -1) { |
|---|
| 159 |
|
|---|
| 160 |
return elgg_view("output/tagcloud",array('value' => get_tags($threshold, $limit, $metadata_name, $entity_type, $entity_subtype, $owner_guid, $site_guid),'object' => $entity_type, 'subtype' => $entity_subtype)); |
|---|
| 161 |
|
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
?> |
|---|
| 165 |
|
|---|