Elgg  Version 1.9
tags.php
Go to the documentation of this file.
1 <?php
19  if (is_string($string)) {
20  $ar = explode(",", $string);
21  $ar = array_map('trim', $ar);
22  $ar = array_filter($ar, 'is_not_null');
23  $ar = array_map('strip_tags', $ar);
24  return $ar;
25  }
26  return false;
27 }
28 
71 function elgg_get_tags(array $options = array()) {
73 
74  $defaults = array(
75  'threshold' => 1,
76  'tag_names' => array(),
77  'limit' => 10,
78 
79  'types' => ELGG_ENTITIES_ANY_VALUE,
80  'subtypes' => ELGG_ENTITIES_ANY_VALUE,
81  'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
82 
83  'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
84  'container_guids' => ELGG_ENTITIES_ANY_VALUE,
85  'site_guids' => $CONFIG->site_guid,
86 
87  'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
88  'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
89  'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
90  'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
91 
92  'joins' => array(),
93  'wheres' => array(),
94  );
95 
96 
97  $options = array_merge($defaults, $options);
98 
99  $singulars = array('type', 'subtype', 'owner_guid', 'container_guid', 'site_guid', 'tag_name');
101 
102  $registered_tags = elgg_get_registered_tag_metadata_names();
103 
104  if (!is_array($options['tag_names'])) {
105  return false;
106  }
107 
108  // empty array so use all registered tag names
109  if (count($options['tag_names']) == 0) {
110  $options['tag_names'] = $registered_tags;
111  }
112 
113  $diff = array_diff($options['tag_names'], $registered_tags);
114  if (count($diff) > 0) {
115  elgg_deprecated_notice('Tag metadata names must be registered by elgg_register_tag_metadata_name()', 1.7);
116  // return false;
117  }
118 
119 
120  $wheres = $options['wheres'];
121 
122  // catch for tags that were spaces
123  $wheres[] = "msv.string != ''";
124 
125  $sanitised_tags = array();
126  foreach ($options['tag_names'] as $tag) {
127  $sanitised_tags[] = '"' . sanitise_string($tag) . '"';
128  }
129  $tags_in = implode(',', $sanitised_tags);
130  $wheres[] = "(msn.string IN ($tags_in))";
131 
132  $wheres[] = _elgg_get_entity_type_subtype_where_sql('e', $options['types'],
133  $options['subtypes'], $options['type_subtype_pairs']);
134  $wheres[] = _elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
135  $wheres[] = _elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
136  $wheres[] = _elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
137  $wheres[] = _elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
138  $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
139 
140  // see if any functions failed
141  // remove empty strings on successful functions
142  foreach ($wheres as $i => $where) {
143  if ($where === false) {
144  return false;
145  } elseif (empty($where)) {
146  unset($wheres[$i]);
147  }
148  }
149 
150  // remove identical where clauses
151  $wheres = array_unique($wheres);
152 
153  $joins = $options['joins'];
154 
155  $joins[] = "JOIN {$CONFIG->dbprefix}metadata md on md.entity_guid = e.guid";
156  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msv on msv.id = md.value_id";
157  $joins[] = "JOIN {$CONFIG->dbprefix}metastrings msn on md.name_id = msn.id";
158 
159  // remove identical join clauses
160  $joins = array_unique($joins);
161 
162  foreach ($joins as $i => $join) {
163  if ($join === false) {
164  return false;
165  } elseif (empty($join)) {
166  unset($joins[$i]);
167  }
168  }
169 
170 
171  $query = "SELECT msv.string as tag, count(msv.id) as total ";
172  $query .= "FROM {$CONFIG->dbprefix}entities e ";
173 
174  // add joins
175  foreach ($joins as $j) {
176  $query .= " $j ";
177  }
178 
179  // add wheres
180  $query .= ' WHERE ';
181 
182  foreach ($wheres as $w) {
183  $query .= " $w AND ";
184  }
185 
186  // Add access controls
187  $query .= _elgg_get_access_where_sql();
188 
189  $threshold = sanitise_int($options['threshold']);
190  $query .= " GROUP BY msv.string HAVING total >= {$threshold} ";
191  $query .= " ORDER BY total DESC ";
192 
193  $limit = sanitise_int($options['limit']);
194  $query .= " LIMIT {$limit} ";
195 
196  return get_data($query);
197 }
198 
210  global $CONFIG;
211 
212  if (!isset($CONFIG->registered_tag_metadata_names)) {
213  $CONFIG->registered_tag_metadata_names = array();
214  }
215 
216  if (!in_array($name, $CONFIG->registered_tag_metadata_names)) {
217  $CONFIG->registered_tag_metadata_names[] = $name;
218  }
219 
220  return true;
221 }
222 
230  global $CONFIG;
231 
232  $names = (isset($CONFIG->registered_tag_metadata_names)) ? $CONFIG->registered_tag_metadata_names : array();
233 
234  return $names;
235 }
236 
246 
247  $title = elgg_echo('tags:site_cloud');
248  $options = array(
249  'threshold' => 0,
250  'limit' => 100,
251  'tag_name' => 'tags',
252  );
254  $content = $tags;
255  $body = elgg_view_layout('one_sidebar', array(
256  'title' => $title,
257  'content' => $content,
258  ));
259 
261  return true;
262 }
263 
267 function _elgg_tags_init() {
268  // register the standard tags metadata name
270 
271  elgg_register_page_handler('tags', '_elgg_tagcloud_page_handler');
272 }
273 
274 elgg_register_event_handler('init', 'system', '_elgg_tags_init');
$tags
Definition: summary.php:41
elgg_get_registered_tag_metadata_names()
Returns an array of valid metadata names for tags.
Definition: tags.php:229
elgg_get_tags(array $options=array())
Get popular tags and their frequencies.
Definition: tags.php:71
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
string_to_tag_array($string)
Takes in a comma-separated string and returns an array of tags which have been trimmed.
Definition: tags.php:18
elgg_view_tagcloud(array $options=array())
Create a tagcloud for viewing.
Definition: views.php:1317
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
Definition: entities.php:1238
_elgg_get_entity_time_where_sql($table, $time_created_upper=null, $time_created_lower=null, $time_updated_upper=null, $time_updated_lower=null)
Returns SQL where clause for entity time limits.
Definition: entities.php:1287
_elgg_tagcloud_page_handler($page)
Page hander for sitewide tag cloud.
Definition: tags.php:245
$title
Definition: save.php:24
$string
$options
Definition: index.php:14
$limit
Definition: userpicker.php:33
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
global $CONFIG
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2134
elgg echo
Translates a string.
Definition: languages.js:43
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_view_layout($layout_name, $vars=array())
Displays a layout with optional parameters.
Definition: views.php:617
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
elgg_register_tag_metadata_name($name)
Registers a metadata name as containing tags for an entity.
Definition: tags.php:209
_elgg_tags_init()
private
Definition: tags.php:267
$content
Set robots.txt action.
Definition: set_robots.php:6
_elgg_get_entity_type_subtype_where_sql($table, $types, $subtypes, $pairs)
Returns SQL where clause for type and subtype on main entity table.
Definition: entities.php:1069
elgg_view_page($title, $body, $page_shell= 'default', $vars=array())
Assembles and outputs a full page.
Definition: views.php:437
sanitise_int($int, $signed=true)
Sanitizes an integer for database use.
Definition: database.php:173
$defaults
Definition: tags.php:18
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1594
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
Definition: access.php:343