aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/tags.php
blob: cdcf213c01a012cca651454b43df6c8d9eca29e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
 * Elgg tags
 * Functions for managing tags and tag clouds.
 *
 * @package Elgg
 * @subpackage Core
 * @author Curverider Ltd <info@elgg.com>
 * @link http://elgg.org/
 */


/**
 * The algorithm working out the size of font based on the number of tags.
 * This is quick and dirty.
 */
function calculate_tag_size($min, $max, $number_of_tags, $buckets = 6) {

	$delta =  (($max - $min) / $buckets);
	$thresholds = array();

	for ($n=1; $n <= $buckets; $n++) {
		$thresholds[$n-1] = ($min + $n) * $delta;
	}

	// Correction
	if ($thresholds[$buckets-1]>$max) {
		$thresholds[$buckets-1] = $max;
	}

	$size = 0;
	for ($n = 0; $n < count($thresholds); $n++) {
		if ($number_of_tags >= $thresholds[$n]) {
			$size = $n;
		}
	}

	return $size;
}

/**
 * This function generates an array of tags with a weighting.
 *
 * @param array $tags The array of tags.
 * @return An associated array of tags with a weighting, this can then be mapped to a display class.
 */
function generate_tag_cloud(array $tags, $buckets = 6) {
	$cloud = array();

	$min = 65535;
	$max = 0;

	foreach ($tags as $tag) {
		$cloud[$tag]++;

		if ($cloud[$tag]>$max) {
			$max = $cloud[$tag];
		}

		if ($cloud[$tag]<$min) {
			$min = $cloud[$tag];
		}
	}

	foreach ($cloud as $k => $v) {
		$cloud[$k] = calculate_tag_size($min, $max, $v, $buckets);
	}

	return $cloud;
}

/**
 * Get an array of tags with weights for use with the output/tagcloud view.
 *
 * @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)
 * @param int $limit Number of tags to return
 * @param string $metadata_name Optionally, the name of the field you want to grab for
 * @param string $entity_type Optionally, the entity type ('object' etc)
 * @param string $entity_subtype The entity subtype, optionally
 * @param int $owner_guid The GUID of the tags owner, optionally
 * @param int $site_guid Optionally, the site to restrict to (default is the current site)
 * @param int $start_ts Optionally specify a start timestamp for tags used to generate cloud.
 * @param int $ent_ts Optionally specify an end timestamp for tags used to generate cloud.
 * @return array|false Array of objects with ->tag and ->total values, or false on failure
 */

function get_tags($threshold = 1, $limit = 10, $metadata_name = "", $entity_type = "object", $entity_subtype = "", $owner_guid = "", $site_guid = -1, $start_ts = "", $end_ts = "") {
	global $CONFIG;

	$threshold = (int) $threshold;
	$limit = (int) $limit;

	if (!empty($metadata_name)) {
		$metadata_name = (int) get_metastring_id($metadata_name);
	} else {
		$metadata_name = 0;
	}
	$entity_subtype = get_subtype_id($entity_type, $entity_subtype);
	$entity_type = sanitise_string($entity_type);

	if ($owner_guid != "") {
		if (is_array($owner_guid)) {
			foreach($owner_guid as $key => $val) {
				$owner_guid[$key] = (int) $val;
			}
		} else {
			$owner_guid = (int) $owner_guid;
		}
	}

	if ($site_guid < 0) {
		$site_guid = $CONFIG->site_id;
	}

	//$access = get_access_list();

	$query = "SELECT msvalue.string as tag, count(msvalue.id) as total ";
	$query .= "FROM {$CONFIG->dbprefix}entities e join {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid ";
	if ($entity_subtype > 0) {
		$query .= " join {$CONFIG->dbprefix}entity_subtypes subtype on subtype.id = e.subtype ";
	}
	$query .= " join {$CONFIG->dbprefix}metastrings msvalue on msvalue.id = md.value_id ";

	$query .= " where msvalue.string != '' ";

	if ($metadata_name > 0) {
		$query .= " and md.name_id = {$metadata_name} ";
	}
	if ($site_guid > 0) {
		$query .= " and e.site_guid = {$site_guid} ";
	}
	if ($entity_subtype > 0) {
		$query .= " and e.subtype = {$entity_subtype} ";
	}
	if ($entity_type != "") {
		$query .= " and e.type = '{$entity_type}' ";
	}
	if (is_array($owner_guid)) {
		$query .= " and e.container_guid in (".implode(",",$owner_guid).")";
	} else if (is_int($owner_guid)) {
		$query .= " and e.container_guid = {$owner_guid} ";
	}
	if ($start_ts) {
		$start_ts = (int)$start_ts;
		$query .= " and e.time_created>=$start_ts";
	}

	if ($end_ts) {
		$end_ts = (int)$end_ts;
		$query .= " and e.time_created<=$end_ts";
	}

	//$userid = get_loggedin_userid();
	//$query .= " and (e.access_id in {$access} or (e.access_id = " . ACCESS_PRIVATE . " and e.owner_guid = {$userid}))";
	$query .= ' and ' . get_access_sql_suffix("e"); // Add access controls

	$query .= " group by msvalue.string having total > {$threshold} order by total desc limit {$limit} ";

	return get_data($query);
}

/**
 * Loads and displays a tagcloud given particular criteria.
 *
 * @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)
 * @param int $limit Number of tags to return
 * @param string $metadata_name Optionally, the name of the field you want to grab for
 * @param string $entity_type Optionally, the entity type ('object' etc)
 * @param string $entity_subtype The entity subtype, optionally
 * @param int $owner_guid The GUID of the tags owner, optionally
 * @param int $site_guid Optionally, the site to restrict to (default is the current site)
 * @return string THe HTML (or other, depending on view type) of the tagcloud.
 */

function display_tagcloud($threshold = 1, $limit = 10, $metadata_name = "", $entity_type = "object", $entity_subtype = "", $owner_guid = "", $site_guid = -1) {
	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));
}



/**
 * Return default results for searches on groups.
 *
 * @param unknown_type $hook
 * @param unknown_type $type
 * @param unknown_type $value
 * @param unknown_type $params
 * @return unknown_type
 */
function tags_search_hook($hook, $type, $value, $params) {
	global $CONFIG;

	$query = $params['query'];

	$params['metadata_name_value_pair'] = array ('name' => 'tags', 'value' => $query, 'case_sensitive' => FALSE);

	$entities = elgg_get_entities_from_metadata($params);
	$params['count'] = TRUE;
	$count = elgg_get_entities_from_metadata($params);

	// no need to continue if nothing here.
	if (!$count) {
		return array('entities' => array(), 'count' => $count);
	}

	// add the volatile data for why these entities have been returned.
	foreach ($entities as $entity) {
		$tags = implode(',', $entity->tags);
		$tags_str = search_get_relevant_substring($tags, $query, '<strong class="searchMatch">', '</strong>');
		$entity->setVolatileData('search_matched_tags', $tags_str);
	}

	return array(
		'entities' => $entities,
		'count' => $count,
	);
}

/**
 * Register tags as a custom search type.
 *
 * @param unknown_type $hook
 * @param unknown_type $type
 * @param unknown_type $value
 * @param unknown_type $params
 * @return unknown_type
 */
function tags_search_custom_types_hook($hook, $type, $value, $params) {
	$value[] = 'tags';
	return $value;
}


register_plugin_hook('search', 'tags', 'tags_search_hook');
register_plugin_hook('search_types', 'get_types', 'tags_search_custom_types_hook');