| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file community_tags.pages.inc
|
| 6 |
*
|
| 7 |
* Page handlers of Community Tags.
|
| 8 |
*
|
| 9 |
* @defgroup community_tags_pages Community Tags page handlers.
|
| 10 |
* @{
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Quick tag form
|
| 15 |
*/
|
| 16 |
function community_tags_form($form_state, $edit, $title = NULL) {
|
| 17 |
$form['cloud'] = array(
|
| 18 |
'#type' => 'markup',
|
| 19 |
'#title' => t('All tags'),
|
| 20 |
'#value' => $edit['cloud'],
|
| 21 |
);
|
| 22 |
|
| 23 |
$access = user_access('tag content');
|
| 24 |
|
| 25 |
$form['tags'] = array(
|
| 26 |
'#type' => 'textfield',
|
| 27 |
'#title' => t('My tags'),
|
| 28 |
'#maxlength' => 100,
|
| 29 |
'#default_value' => $edit['tags'],
|
| 30 |
'#required' => FALSE,
|
| 31 |
'#autocomplete_path' => 'taxonomy/autocomplete/'. $edit['vid'],
|
| 32 |
'#attributes' => array('class' => 'form-tags'),
|
| 33 |
'#access' => $access,
|
| 34 |
);
|
| 35 |
if ($edit['inline']) {
|
| 36 |
$form['tags']['#size'] = 20;
|
| 37 |
}
|
| 38 |
|
| 39 |
if (!$access) {
|
| 40 |
$destination = drupal_get_destination();
|
| 41 |
$form['login'] = array(
|
| 42 |
'#type' => 'markup',
|
| 43 |
'#value' => '<div>'. t('<a href="@login">Login</a> or <a href="@register">register</a> to tag items', array('@login' => url('user/login', $destination), '@register' => url('user/register', $destination))) .'</div>',
|
| 44 |
);
|
| 45 |
}
|
| 46 |
|
| 47 |
$form['submit'] = array(
|
| 48 |
'#type' => 'submit',
|
| 49 |
'#value' => t('Save'),
|
| 50 |
'#access' => $access,
|
| 51 |
);
|
| 52 |
|
| 53 |
$form['node'] = array(
|
| 54 |
'#type' => 'value',
|
| 55 |
'#value' => $edit['node'],
|
| 56 |
);
|
| 57 |
|
| 58 |
$form['nid'] = array(
|
| 59 |
'#type' => 'value',
|
| 60 |
'#value' => $edit['nid'],
|
| 61 |
);
|
| 62 |
|
| 63 |
$form['vid'] = array(
|
| 64 |
'#type' => 'value',
|
| 65 |
'#value' => $edit['vid'],
|
| 66 |
);
|
| 67 |
|
| 68 |
return $form;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Validate the quick tag form.
|
| 73 |
*/
|
| 74 |
function community_tags_form_validate($form, &$form_state) {
|
| 75 |
if ($form_state['values']['tags'] == '') {
|
| 76 |
form_set_error('tags', t('You must enter at least one tag.'));
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Submit callback for quick tag form.
|
| 82 |
*/
|
| 83 |
function community_tags_form_submit($form, &$form_state) {
|
| 84 |
global $user;
|
| 85 |
$form_values = $form_state['values'];
|
| 86 |
community_tags_taxonomy_node_save($form_values['node'], array('tags' => array($form_values['vid'] => $form_values['tags'])), FALSE, $user->uid);
|
| 87 |
|
| 88 |
$form_state['redirect'] = 'node/'. $form_values['nid'];
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Theme the quick tag form.
|
| 93 |
* @ingroup themeable
|
| 94 |
*/
|
| 95 |
function theme_community_tags_form($form) {
|
| 96 |
$output = theme('form_element', array('#title' => t('All tags')), drupal_render($form['cloud']));
|
| 97 |
|
| 98 |
$output .= drupal_render($form);
|
| 99 |
|
| 100 |
// We add the JS file this late, to ensure it comes after autocomplete.js.
|
| 101 |
drupal_add_css(drupal_get_path('module', 'community_tags') .'/community_tags.css', 'module');
|
| 102 |
drupal_add_js(drupal_get_path('module', 'community_tags') .'/community_tags.js');
|
| 103 |
|
| 104 |
return $output;
|
| 105 |
}
|
| 106 |
|
| 107 |
/**
|
| 108 |
* Menu callback:
|
| 109 |
*/
|
| 110 |
function community_tags_mypage($uid = NULL) {
|
| 111 |
$uid = isset($uid) ? $uid : $GLOBALS['uid'];
|
| 112 |
|
| 113 |
_community_tags_get_tag_result('user', 100, $uid);
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Form builder; Builds the settings form.
|
| 118 |
*
|
| 119 |
* @ingroup forms
|
| 120 |
*/
|
| 121 |
function community_tags_settings() {
|
| 122 |
$form = array();
|
| 123 |
|
| 124 |
community_tags_rehash();
|
| 125 |
|
| 126 |
// Build list of available free-tagging vocabularies
|
| 127 |
$options = array();
|
| 128 |
$vocabs = db_query('SELECT v.vid, v.name FROM {vocabulary} v WHERE v.tags = 1 ORDER BY v.weight, v.name');
|
| 129 |
while ($vocabulary = db_fetch_object($vocabs)) {
|
| 130 |
$options[$vocabulary->vid] = $vocabulary->name;
|
| 131 |
}
|
| 132 |
if ($options) {
|
| 133 |
$form['community_tags_vocabularies'] = array(
|
| 134 |
'#type' => 'select',
|
| 135 |
'#multiple' => TRUE,
|
| 136 |
'#title' => t('Community vocabularies'),
|
| 137 |
'#default_value' => variable_get('community_tags_vocabularies', array()),
|
| 138 |
'#options' => $options,
|
| 139 |
'#description' => t('Which vocabularies should community tagging use? Note: only one community tagged vocabulary per node type is supported.'),
|
| 140 |
);
|
| 141 |
}
|
| 142 |
|
| 143 |
$form = system_settings_form($form);
|
| 144 |
|
| 145 |
return $form;
|
| 146 |
}
|
| 147 |
|
| 148 |
// Patch for Drupal 5.x. Remove for 6.x.
|
| 149 |
if (!function_exists('taxonomy_explode_tags')) {
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Explode a string of given tags into an array.
|
| 153 |
*/
|
| 154 |
function taxonomy_explode_tags($tags) {
|
| 155 |
// This regexp allows the following types of user input:
|
| 156 |
// this, "somecompany, llc", "and ""this"" w,o.rks", foo bar
|
| 157 |
$regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
|
| 158 |
preg_match_all($regexp, $tags, $matches);
|
| 159 |
$typed_tags = array_unique($matches[1]);
|
| 160 |
|
| 161 |
$tags = array();
|
| 162 |
foreach ($typed_tags as $tag) {
|
| 163 |
// If a user has escaped a term (to demonstrate that it is a group,
|
| 164 |
// or includes a comma or quote character), we remove the escape
|
| 165 |
// formatting so to save the term into the database as the user intends.
|
| 166 |
$tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $tag)));
|
| 167 |
if ($tag != "") {
|
| 168 |
$tags[] = $tag;
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
return $tags;
|
| 173 |
}
|
| 174 |
}
|