00001 <?php
00022 function calculate_tag_size($min, $max, $number_of_tags, $buckets = 6) {
00023 $delta = (($max - $min) / $buckets);
00024 $thresholds = array();
00025
00026 for ($n = 1; $n <= $buckets; $n++) {
00027 $thresholds[$n - 1] = ($min + $n) * $delta;
00028 }
00029
00030
00031 if ($thresholds[$buckets - 1] > $max) {
00032 $thresholds[$buckets - 1] = $max;
00033 }
00034
00035 $size = 0;
00036 for ($n = 0; $n < count($thresholds); $n++) {
00037 if ($number_of_tags >= $thresholds[$n]) {
00038 $size = $n;
00039 }
00040 }
00041
00042 return $size;
00043 }
00044
00054 function generate_tag_cloud(array $tags, $buckets = 6) {
00055 $cloud = array();
00056
00057 $min = 65535;
00058 $max = 0;
00059
00060 foreach ($tags as $tag) {
00061 $cloud[$tag]++;
00062
00063 if ($cloud[$tag] > $max) {
00064 $max = $cloud[$tag];
00065 }
00066
00067 if ($cloud[$tag] < $min) {
00068 $min = $cloud[$tag];
00069 }
00070 }
00071
00072 foreach ($cloud as $k => $v) {
00073 $cloud[$k] = calculate_tag_size($min, $max, $v, $buckets);
00074 }
00075
00076 return $cloud;
00077 }
00078
00121 function elgg_get_tags(array $options = array()) {
00122 global $CONFIG;
00123
00124 $defaults = array(
00125 'threshold' => 1,
00126 'tag_names' => array(),
00127 'limit' => 10,
00128
00129 'types' => ELGG_ENTITIES_ANY_VALUE,
00130 'subtypes' => ELGG_ENTITIES_ANY_VALUE,
00131 'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
00132
00133 'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
00134 'container_guids' => ELGG_ENTITIES_ANY_VALUE,
00135 'site_guids' => $CONFIG->site_guid,
00136
00137 'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
00138 'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
00139 'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
00140 'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
00141
00142 'joins' => array(),
00143 'wheres' => array(),
00144 );
00145
00146
00147 $options = array_merge($defaults, $options);
00148
00149 $singulars = array('type', 'subtype', 'owner_guid', 'container_guid', 'site_guid', 'tag_name');
00150 $options = elgg_normalise_plural_options_array($options, $singulars);
00151
00152 $registered_tags = elgg_get_registered_tag_metadata_names();
00153
00154 if (!is_array($options['tag_names'])) {
00155 return false;
00156 }
00157
00158
00159 if (count($options['tag_names']) == 0) {
00160 $options['tag_names'] = $registered_tags;
00161 }
00162
00163 $diff = array_diff($options['tag_names'], $registered_tags);
00164 if (count($diff) > 0) {
00165 elgg_deprecated_notice('Tag metadata names must be registered by elgg_register_tag_metadata_name()', 1.7);
00166
00167 }
00168
00169
00170 $wheres = $options['wheres'];
00171
00172
00173 $wheres[] = "msv.string != ''";
00174
00175 $sanitised_tags = array();
00176 foreach ($options['tag_names'] as $tag) {
00177 $sanitised_tags[] = '"' . sanitise_string($tag) . '"';
00178 }
00179 $tags_in = implode(',', $sanitised_tags);
00180 $wheres[] = "(msn.string IN ($tags_in))";
00181
00182 $wheres[] = elgg_get_entity_type_subtype_where_sql('e', $options['types'],
00183 $options['subtypes'], $options['type_subtype_pairs']);
00184 $wheres[] = elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
00185 $wheres[] = elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
00186 $wheres[] = elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
00187 $wheres[] = elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
00188 $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
00189
00190
00191
00192 foreach ($wheres as $i => $where) {
00193 if ($where === FALSE) {
00194 return FALSE;
00195 } elseif (empty($where)) {
00196 unset($wheres[$i]);
00197 }
00198 }
00199
00200
00201 $wheres = array_unique($wheres);
00202
00203 $joins = $options['joins'];
00204
00205 $joins[] = "JOIN {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid";
00206 $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msv on msv.id = md.value_id";
00207 $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msn on md.name_id = msn.id";
00208
00209
00210 $joins = array_unique($joins);
00211
00212 foreach ($joins as $i => $join) {
00213 if ($join === FALSE) {
00214 return FALSE;
00215 } elseif (empty($join)) {
00216 unset($joins[$i]);
00217 }
00218 }
00219
00220
00221 $query = "SELECT msv.string as tag, count(msv.id) as total ";
00222 $query .= "FROM {$CONFIG->dbprefix}entities e ";
00223
00224
00225 foreach ($joins as $j) {
00226 $query .= " $j ";
00227 }
00228
00229
00230 $query .= ' WHERE ';
00231
00232 foreach ($wheres as $w) {
00233 $query .= " $w AND ";
00234 }
00235
00236
00237 $query .= get_access_sql_suffix('e');
00238
00239 $threshold = sanitise_int($options['threshold']);
00240 $query .= " GROUP BY msv.string HAVING total >= {$threshold} ";
00241 $query .= " ORDER BY total DESC ";
00242
00243 $limit = sanitise_int($options['limit']);
00244 $query .= " LIMIT {$limit} ";
00245
00246 return get_data($query);
00247 }
00248
00263 function elgg_view_tagcloud(array $options = array()) {
00264
00265 $type = $subtype = '';
00266 if (isset($options['type'])) {
00267 $type = $options['type'];
00268 }
00269 if (isset($options['subtype'])) {
00270 $subtype = $options['subtype'];
00271 }
00272
00273 $tag_data = elgg_get_tags($options);
00274 return elgg_view("output/tagcloud", array(
00275 'value' => $tag_data,
00276 'type' => $type,
00277 'subtype' => $subtype,
00278 ));
00279 }
00280
00291 function elgg_register_tag_metadata_name($name) {
00292 global $CONFIG;
00293
00294 if (!isset($CONFIG->registered_tag_metadata_names)) {
00295 $CONFIG->registered_tag_metadata_names = array();
00296 }
00297
00298 if (!in_array($name, $CONFIG->registered_tag_metadata_names)) {
00299 $CONFIG->registered_tag_metadata_names[] = $name;
00300 }
00301
00302 return TRUE;
00303 }
00304
00311 function elgg_get_registered_tag_metadata_names() {
00312 global $CONFIG;
00313
00314 $names = (isset($CONFIG->registered_tag_metadata_names))
00315 ? $CONFIG->registered_tag_metadata_names : array();
00316
00317 return $names;
00318 }
00319
00328 function elgg_tagcloud_page_handler($page) {
00329
00330 $title = elgg_view_title(elgg_echo('tags:site_cloud'));
00331 $options = array(
00332 'threshold' => 0,
00333 'limit' => 100,
00334 'tag_name' => 'tags',
00335 );
00336 $tags = elgg_view_tagcloud($options);
00337 $content = $title . $tags;
00338 $body = elgg_view_layout('one_sidebar', array('content' => $content));
00339
00340 echo elgg_view_page(elgg_echo('tags:site_cloud'), $body);
00341 return true;
00342 }
00343
00347 function elgg_tags_init() {
00348
00349 elgg_register_tag_metadata_name('tags');
00350
00351 elgg_register_page_handler('tags', 'elgg_tagcloud_page_handler');
00352 }
00353
00354 elgg_register_event_handler('init', 'system', 'elgg_tags_init');