/[drupal]/contributions/modules/reptag/reptag_helper.inc
ViewVC logotype

Contents of /contributions/modules/reptag/reptag_helper.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Jun 4 12:21:06 2008 UTC (17 months, 3 weeks ago) by profix898
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +365 -0 lines
File MIME type: text/x-php
- task: initial version for Drupal 6
1 <?php
2 // $Id: reptag_helper.inc,v 1.1.2.7 2007/07/25 16:14:31 profix898 Exp $
3
4 define('REPTAG_LOADTAG_DEFAULT', 0);
5 define('REPTAG_LOADTAG_FORMAT', 1);
6 define('REPTAG_LOADTAG_EDIT', 2);
7
8 /**
9 * Function _reptag_language().
10 * (get language for the current page request)
11 */
12 function _reptag_language($lang = NULL) {
13 global $language;
14 return isset($lang) ? $lang : (variable_get('reptag_locale_enable', 0) ? $language->language : language_default('language'));
15 }
16
17 /**
18 * Function _reptag_table_load_tag().
19 * (load tags from table)
20 */
21 function _reptag_table_load_tag($rtid, $cleanlevel = REPTAG_LOADTAG_DEFAULT) {
22 // Load and format tags
23 $reptag = db_fetch_array(db_query('SELECT name, uid FROM {reptag_vars} WHERE rtid = %d', $rtid));
24 $result = db_query('SELECT rtid, name, value, language FROM {reptag_vars} WHERE name = \'%s\' AND uid = %d', $reptag['name'], $reptag['uid']);
25 while ($var = db_fetch_object($result)) {
26 switch ($cleanlevel) {
27 case REPTAG_LOADTAG_FORMAT:
28 $reptag['tag'] = '$'. substr($var->name, 3, -5) .'$';
29 break;
30 case REPTAG_LOADTAG_EDIT:
31 $reptag['tag'] = substr($var->name, 3, -5);
32 break;
33 case REPTAG_LOADTAG_DEFAULT:
34 default:
35 $reptag['tag'] = $var->name;
36 }
37 $reptag['replacements'][$var->language] = array(
38 'rtid' => $var->rtid,
39 'replacement' => $var->value
40 );
41 }
42
43 return count($reptag) ? $reptag : NULL;
44 }
45
46 /**
47 * Function _reptag_table_load().
48 * (load tags from table)
49 */
50 function _reptag_table_load($iid, $cleanlevel = REPTAG_LOADTAG_DEFAULT, $lang = NULL, $limit = NULL) {
51 // Build query
52 $query = 'SELECT rtid, name, value FROM {reptag_vars} WHERE uid = '. intval($iid[0]);
53 for ($i = 1; $i < count($iid); $i++) {
54 $query .= ' OR uid = '. intval($iid[$i]);
55 }
56 $query .= ' AND language = \''. _reptag_language($lang) .'\'';
57 $query .= variable_get('reptag_tableprio', 0) ? ' ORDER BY uid ASC' : ' ORDER BY uid DESC';
58 // Load and format tags
59 $vars = array();
60 $result = isset($limit) ? pager_query($query, $limit) : db_query($query);
61 while ($var = db_fetch_object($result)) {
62 switch ($cleanlevel) {
63 case REPTAG_LOADTAG_FORMAT:
64 $vars['$'. substr($var->name, 3, -5) .'$'] = $var->value;
65 break;
66 case REPTAG_LOADTAG_EDIT:
67 $vars[$var->rtid] = array('tag' => substr($var->name, 3, -5), 'replacement' => $var->value);
68 break;
69 case REPTAG_LOADTAG_DEFAULT:
70 default:
71 $vars[$var->name] = $var->value;
72 }
73 }
74
75 return $vars;
76 }
77
78 /**
79 * Function _reptag_table_add().
80 */
81 function _reptag_table_add($name, $value, $uid, $lang = NULL) {
82 $item = array(
83 'name' => $name,
84 'value' => $value,
85 'uid' => $uid,
86 'language' => _reptag_language($lang)
87 );
88 drupal_write_record('reptag_vars', $item);
89 }
90
91 /**
92 * Function _reptag_table_update().
93 */
94 function _reptag_table_update($rtid, $name, $value, $uid, $lang = NULL) {
95 $item = array(
96 'rtid' => $rtid,
97 'name' => $name,
98 'value' => $value,
99 'uid' => $uid,
100 'language' => _reptag_language($lang)
101 );
102 drupal_write_record('reptag_vars', $item, 'rtid');
103 }
104
105 /**
106 * Function _reptag_table_del_name().
107 */
108 function _reptag_table_del_name($name, $uid, $lang = NULL, $all = FALSE) {
109 $query = 'DELETE FROM {reptag_vars} WHERE name = \'%s\' AND uid = %d';
110 if (!isset($lang) && !$all) {
111 $lang = _reptag_language();
112 }
113 $query .= isset($lang) ? (' AND language = \''. $lang .'\'') : '';
114 db_lock_table('reptag_vars');
115 db_query($query, $name, $uid);
116 db_unlock_tables();
117 }
118
119 /**
120 * Function _reptag_table_del().
121 */
122 function _reptag_table_del($rtid) {
123 db_lock_table('reptag_vars');
124 db_query('DELETE FROM {reptag_vars} WHERE rtid = %d', $rtid);
125 db_unlock_tables();
126 }
127
128 /**
129 * Function _reptag_table_clear().
130 * (remove all tags for a specified user)
131 */
132 function _reptag_table_clear($uid) {
133 $query = 'DELETE FROM {reptag_vars} WHERE uid = %d';
134 db_lock_table('reptag_vars');
135 db_query($query, $uid);
136 db_unlock_tables();
137 }
138
139 /**
140 * Function _reptag_tags_implode().
141 */
142 function _reptag_tags_implode($tags) {
143 $tags = array_filter($tags);
144 $tags = implode('|', $tags);
145 return $tags;
146 }
147
148 /**
149 * Function _reptag_exclude_loadtags().
150 * (load exclude tags from variable)
151 */
152 function _reptag_exclude_loadtags($rid) {
153 $tags = unserialize(variable_get('reptag_exclude_r'. $rid, serialize(array())));
154 return $tags;
155 }
156
157 /**
158 * Function _reptag_exclude_loadtags_string().
159 * (load exclude tags from variable into a string)
160 */
161 function _reptag_exclude_loadtags_string($rid) {
162 $tags = _reptag_exclude_loadtags($rid);
163 $tags = _reptag_tags_implode($tags);
164 return $tags;
165 }
166
167 /**
168 * Function _reptag_exclude_storetags().
169 * (store exclude tags to variable)
170 */
171 function _reptag_exclude_storetags($rid, $tags) {
172 variable_set('reptag_exclude_r'. $rid, serialize($tags));
173 }
174
175 /**
176 * Function _reptag_exclude_storetags_string().
177 * (explode string and store exclude tags to variable)
178 */
179 function _reptag_exclude_storetags_string($rid, $tags) {
180 $tags = explode('|', $tags);
181 _reptag_exclude_storetags($rid, $tags);
182 }
183
184 /**
185 * Function _reptag_user_get_roles().
186 * (get roles for a specified user)
187 */
188 function _reptag_user_get_roles($uid) {
189 $roles = array();
190 switch ($uid) {
191 case 0: // anonymous user
192 $roles[] = DRUPAL_ANONYMOUS_RID;
193 break;
194 case 1: // administrator
195 $roles[] = DRUPAL_AUTHENTICATED_RID;
196 $result = db_query('SELECT DISTINCT rid FROM {users_roles}');
197 while ($rid = db_fetch_object($result)) {
198 $roles[] = $rid->rid;
199 }
200 break;
201 default: // authenticated user
202 $roles[] = DRUPAL_AUTHENTICATED_RID;
203 $result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $uid);
204 while ($rid = db_fetch_object($result)) {
205 $roles[] = $rid->rid;
206 }
207 }
208
209 return $roles;
210 }
211
212 /**
213 * Function _reptag_cache_cid().
214 * (calculate the cache identifier)
215 */
216 function _reptag_cache_cid($node, $fields, $teaser) {
217 return 'n'. $node->nid . ($teaser ? 't:' : ':') .
218 (isset($node->vid) ? 'v'. $node->vid : 'c'. $node->cid) .':'. md5(serialize($fields)) .':'. _reptag_language();
219 }
220
221 /**
222 * Function _reptag_cache_set().
223 * (store node into reptag cache)
224 */
225 function _reptag_cache_set($node, $fields, $teaser = FALSE) {
226 $cache = array();
227 foreach ($fields as $field) {
228 $cache[$field] = _reptag_content_field($node, $field);
229 }
230 if (count($cache)) {
231 $cid = _reptag_cache_cid($node, $fields, $teaser);
232 cache_set($cid, $cache, 'cache_reptag', variable_get('reptag_cache_lifetime', CACHE_PERMANENT));
233 }
234 }
235
236 /**
237 * Function _reptag_cache_get().
238 * (retrieve a node from reptag cache)
239 */
240 function _reptag_cache_get($node, $fields, $teaser = FALSE) {
241 $cid = _reptag_cache_cid($node, $fields, $teaser);
242 if ($cache = cache_get($cid, 'cache_reptag')) {
243 // Does cache contain valid data?
244 if (empty($cache->data)) {
245 return FALSE;
246 }
247 // Unserialize data and override fields
248 $cache = $cache->data;
249 foreach ($fields as $field) {
250 if (isset($cache[$field])) {
251 $field_content = &_reptag_content_field($node, $field);
252 $field_content = $cache[$field];
253 }
254 }
255 return TRUE;
256 }
257
258 return FALSE;
259 }
260
261 /**
262 * Function _reptag_cache_del().
263 * (remove a node from reptag cache)
264 */
265 function _reptag_cache_del($node, $fields) {
266 $node = (object)$node;
267 $cid = _reptag_cache_cid($node, $fields, TRUE);
268 cache_set($cid, '', 'cache_reptag', CACHE_TEMPORARY);
269 $cid = _reptag_cache_cid($node, $fields, FALSE);
270 cache_set($cid, '', 'cache_reptag', CACHE_TEMPORARY);
271 }
272
273 /**
274 * Function _reptag_cache_clear().
275 */
276 function _reptag_cache_clear($msg = NULL) {
277 if (variable_get('reptag_cache', 0)) {
278 cache_clear_all('*', 'cache_reptag', TRUE);
279 drupal_set_message(isset($msg) ? $msg : t('The cache has been flushed.'));
280 }
281 }
282
283 /**
284 * Function _reptag_match_tags().
285 * (filter $tags containing $string)
286 */
287 function _reptag_match_tags($string, $tags) {
288 $matches = array();
289 foreach ($tags as $tag) {
290 if (!(strpos($tag, $string) === false)) {
291 $matches[$tag] = check_plain($tag);
292 }
293 }
294
295 return $matches;
296 }
297
298 /**
299 * Function _reptag_content_field().
300 * (point $text to the $field path of $content)
301 */
302 function &_reptag_content_field(&$content, $field) {
303 $text = &$content;
304 $elements = explode('/', $field);
305 foreach ($elements as $element) {
306 if (is_array($text)) {
307 $text = &$text[$element];
308 }
309 elseif (is_object($text)) {
310 $text = &$text->$element;
311 }
312 else {
313 $text = NULL;
314 }
315 }
316
317 return $text;
318 }
319
320 /**
321 * Function _reptag_debug().
322 */
323 function _reptag_debug($output, $label = NULL, $html = FALSE) {
324 if (variable_get('reptag_debug', 0) && user_access('administer site configuration')) {
325 // Output debug log into a collapsible fieldset
326 $fieldset = array(
327 '#title' => t('Rep[lacement]Tags Debug') . (isset($label) ? (': '. check_plain($label)) : ''),
328 '#collapsible' => TRUE,
329 '#collapsed' => TRUE,
330 '#value' => '<pre>'. ($html ? $output : htmlspecialchars(print_r($output, TRUE))) .'</pre>',
331 );
332 drupal_set_content('content', theme('fieldset', $fieldset));
333 }
334 }
335
336 /**
337 * Function _reptag_array_intersect_key().
338 * (PHP4+5 array_intersect_key()-like replacement)
339 */
340 function _reptag_array_intersect_key($a, $b) {
341 foreach (array_keys($a) as $key) {
342 if (!isset($b[$key])) {
343 unset($a[$key]);
344 }
345 }
346
347 return $a;
348 }
349
350 /**
351 * Function _reptag_array_merge_key().
352 * (PHP4+5 array_merge_key()-like replacement)
353 */
354 function _reptag_array_merge_key($a, $b) {
355 foreach ($b as $key => $value) {
356 if (is_array($value)) {
357 $a[$key] = _reptag_array_merge_key($a[$key], $value);
358 }
359 else {
360 $a[$key] = $value;
361 }
362 }
363
364 return $a;
365 }

  ViewVC Help
Powered by ViewVC 1.1.2