| 1 |
<?php
|
| 2 |
|
| 3 |
function taxonomy_enhancer_init() {
|
| 4 |
$level = variable_get('taxonomy_enhancer_update_fail', FALSE);
|
| 5 |
if ($level !== FALSE) {
|
| 6 |
db_query('UPDATE {system} SET schema_version = %d WHERE name = "taxonomy_enhancer"', $level);
|
| 7 |
variable_del('taxonomy_enhancer_update_fail');
|
| 8 |
}
|
| 9 |
}
|
| 10 |
|
| 11 |
|
| 12 |
function taxonomy_enhancer_form_alter($form_id, &$form) {
|
| 13 |
switch ($form_id) {
|
| 14 |
case 'taxonomy_form_vocabulary' :
|
| 15 |
_taxonomy_enhancer_form_alter_vocabulary($form);
|
| 16 |
break;
|
| 17 |
|
| 18 |
case 'taxonomy_form_term' :
|
| 19 |
_taxonomy_enhancer_form_alter_term($form);
|
| 20 |
break;
|
| 21 |
}
|
| 22 |
}
|
| 23 |
|
| 24 |
|
| 25 |
function _taxonomy_enhancer_form_alter_term(&$form) {
|
| 26 |
if (isset($form['tid']['#value'])) {
|
| 27 |
$tid = $form['tid']['#value'];
|
| 28 |
$term = taxonomy_get_term($tid);
|
| 29 |
taxonomy_enhancer_load_term($term);
|
| 30 |
}
|
| 31 |
else {
|
| 32 |
$term = null;
|
| 33 |
}
|
| 34 |
|
| 35 |
$vid = $form['vid']['#value'];
|
| 36 |
$fields = taxonomy_enhancer_get_fields_by_vocabulary($vid);
|
| 37 |
if (empty($fields)) {
|
| 38 |
return;
|
| 39 |
}
|
| 40 |
|
| 41 |
$form['fields'] = array(
|
| 42 |
'#type' => 'fieldset',
|
| 43 |
'#title' => t('Extended Fields'),
|
| 44 |
'#tree' => true,
|
| 45 |
);
|
| 46 |
|
| 47 |
foreach ($fields as $field) {
|
| 48 |
$extra = module_invoke($field->module, 'te_api', 'form', 'value', $field, $term);
|
| 49 |
$form['fields'] = array_merge($form['fields'], $extra);
|
| 50 |
}
|
| 51 |
|
| 52 |
|
| 53 |
$form['submit']['#weight'] = $form['delete']['#weight'] = 10;
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
function taxonomy_enhancer_load_term(&$term) {
|
| 59 |
$fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
|
| 60 |
$term->fields = array();
|
| 61 |
|
| 62 |
foreach ($fields as $field) {
|
| 63 |
$term->{$field->fid} = module_invoke($field->module, 'te_api', 'load', 'value', $field, $term);
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
function taxonomy_enhancer_extend_term(&$term) {
|
| 68 |
$fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
|
| 69 |
$term->fields = array();
|
| 70 |
|
| 71 |
foreach ($fields as $field) {
|
| 72 |
$data = module_invoke($field->module, 'te_api', 'view', 'value', $field, $term);
|
| 73 |
$term->{$field->fid} = taxonomy_enhancer_theme('taxonomy_enhanced_field', $field, $data);
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
function _taxonomy_enhancer_form_alter_vocabulary(&$form) {
|
| 80 |
if (isset($form['vid']['#value'])) {
|
| 81 |
$vid = $form['vid']['#value'];
|
| 82 |
}
|
| 83 |
else {
|
| 84 |
return;
|
| 85 |
}
|
| 86 |
|
| 87 |
$form['fields'] = array(
|
| 88 |
'#type' => 'fieldset',
|
| 89 |
'#title' => t('Fields'),
|
| 90 |
'#tree' => true,
|
| 91 |
);
|
| 92 |
|
| 93 |
$existing_fields = taxonomy_enhancer_get_fields_by_vocabulary($vid);
|
| 94 |
|
| 95 |
if (empty($existing_fields)) {
|
| 96 |
$form['fields']['created'] = array(
|
| 97 |
'#type' => 'item',
|
| 98 |
'#value' => t('There are no fields added to this vocabulary.'),
|
| 99 |
);
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
$form['fields']['created'] = array(
|
| 103 |
'#type' => 'fieldset',
|
| 104 |
'#title' => t('Existing Fields'),
|
| 105 |
'#collapsed' => false,
|
| 106 |
'#collapsible' => true,
|
| 107 |
);
|
| 108 |
|
| 109 |
foreach ($existing_fields as $id => $field) {
|
| 110 |
$form['fields']['created'][$id] = array(
|
| 111 |
'#type' => 'fieldset',
|
| 112 |
'#title' => check_plain($field->title),
|
| 113 |
'#collapsed' => true,
|
| 114 |
'#collapsible' => true,
|
| 115 |
);
|
| 116 |
$form['fields']['created'][$id]['fid'] = array(
|
| 117 |
'#type' => 'value',
|
| 118 |
'#value' => $field->fid,
|
| 119 |
);
|
| 120 |
|
| 121 |
$form['fields']['created'][$id]['fid_print'] = array(
|
| 122 |
'#type' => 'item',
|
| 123 |
'#title' => t('Field Name / Field ID'),
|
| 124 |
'#value' => check_plain($field->fid),
|
| 125 |
'#description' => t('The <em>Field Id</em> is used to uniquely identify this field. This cannot be changed.'),
|
| 126 |
);
|
| 127 |
|
| 128 |
$form['fields']['created'][$id]['title'] = array(
|
| 129 |
'#type' => 'textfield',
|
| 130 |
'#title' => t('Field Title'),
|
| 131 |
'#default_value' => $field->title,
|
| 132 |
'#required' => true,
|
| 133 |
'#description' => t('The title is the human-friendly identified of the field.'),
|
| 134 |
);
|
| 135 |
|
| 136 |
$form['fields']['created'][$id]['type'] = array(
|
| 137 |
'#type' => 'select',
|
| 138 |
'#title' => t('Field Type'),
|
| 139 |
'#options' => taxonomy_enhancer_get_field_types(),
|
| 140 |
'#default_value' => $field->type,
|
| 141 |
'#description' => t('The type is used to define the data that this field represents, eg text, date, etc.'),
|
| 142 |
);
|
| 143 |
$form['fields']['created'][$id]['weight'] = array(
|
| 144 |
'#type' => 'weight',
|
| 145 |
'#title' => t('Field Weight'),
|
| 146 |
'#description' => t('The weight controls where this field appears in the <em>Extended Terms</em> form.'),
|
| 147 |
);
|
| 148 |
|
| 149 |
$field = module_invoke($field->module, 'te_api', 'load', 'field', $field);
|
| 150 |
|
| 151 |
$module_form = module_invoke($field->module, 'te_api', 'form', 'field', $field);
|
| 152 |
if (is_array($module_form)) {
|
| 153 |
$form['fields']['created'][$id] = array_merge($form['fields']['created'][$id], $module_form);
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
$form['fields']['new'] = array(
|
| 159 |
'#type' => 'fieldset',
|
| 160 |
'#title' => t('Add New Field'),
|
| 161 |
'#validate' => array('_taxonomy_enhancer_validate_new' => array()),
|
| 162 |
'#collapsed' => true,
|
| 163 |
'#collapsible' => true,
|
| 164 |
);
|
| 165 |
$form['fields']['new']['fid'] = array(
|
| 166 |
'#type' => 'textfield',
|
| 167 |
'#title' => t('Name/Field ID'),
|
| 168 |
'#description' => t('This field is the computer readable name of the field'),
|
| 169 |
'#size' => 15,
|
| 170 |
);
|
| 171 |
$form['fields']['new']['title'] = array(
|
| 172 |
'#type' => 'textfield',
|
| 173 |
'#title' => t('Title'),
|
| 174 |
'#description' => t('This is the human readable field title'),
|
| 175 |
'#size' => 20,
|
| 176 |
);
|
| 177 |
$form['fields']['new']['type'] = array(
|
| 178 |
'#type' => 'select',
|
| 179 |
'#title' => t('Type'),
|
| 180 |
'#description' => t('Select which type of field you would like to add'),
|
| 181 |
'#options' => taxonomy_enhancer_get_field_types(),
|
| 182 |
);
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
$form['fields']['delete'] = array(
|
| 187 |
'#type' => 'fieldset',
|
| 188 |
'#title' => t('Delete Fields'),
|
| 189 |
'#collapsed' => true,
|
| 190 |
'#collapsible' => true,
|
| 191 |
);
|
| 192 |
|
| 193 |
$delete_fields = array();
|
| 194 |
foreach ($existing_fields as $id => $field) {
|
| 195 |
$delete_fields[$id] = $field->title;
|
| 196 |
}
|
| 197 |
$form['fields']['delete']['fields'] = array(
|
| 198 |
'#type' => 'checkboxes',
|
| 199 |
'#title' => t('Fields'),
|
| 200 |
'#description' => t('Check the fields you would like to delete. <strong>Note: there is no confirmation and no undo!</strong>'),
|
| 201 |
'#options' => $delete_fields,
|
| 202 |
);
|
| 203 |
|
| 204 |
|
| 205 |
$form['submit']['#weight'] = $form['delete']['#weight'] = 10;
|
| 206 |
}
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
function _taxonomy_enhancer_validate_new($form) {
|
| 211 |
if (empty($form['fid']['#value']) && empty($form['title']['#value'])) {
|
| 212 |
return;
|
| 213 |
}
|
| 214 |
|
| 215 |
if (preg_match('|[^a-z0-9_\-]|', $form_values['fid']['#value'])) {
|
| 216 |
form_set_error('fields][new][fid', t('Name/Field ID must be lowercase alphanumeric & underscore only'));
|
| 217 |
}
|
| 218 |
|
| 219 |
if (!empty($form['fid']['#value']) && empty($form['title']['#value'])) {
|
| 220 |
form_set_error('fields][new][title', t('Both field Name and Title must be set when creating a new field'));
|
| 221 |
}
|
| 222 |
|
| 223 |
if (empty($form['fid']['#value']) && !empty($form['title']['#value'])) {
|
| 224 |
form_set_error('fields][new][fid', t('Both field Name and Title must be set when creating a new field'));
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Implementation of hook_taxonomy
|
| 232 |
*
|
| 233 |
* @param unknown_type $op
|
| 234 |
* @param unknown_type $type
|
| 235 |
* @param unknown_type $array
|
| 236 |
*/
|
| 237 |
function taxonomy_enhancer_taxonomy($op, $type, $array = NULL) {
|
| 238 |
switch ($type) {
|
| 239 |
case 'vocabulary' :
|
| 240 |
switch ($op) {
|
| 241 |
case 'update' :
|
| 242 |
if (!empty($array['fields']['new']['fid'])) {
|
| 243 |
//Get the module and field type from the concatendated form type field.
|
| 244 |
list($module, $field_type) = explode('|', $array['fields']['new']['type']);
|
| 245 |
|
| 246 |
//Build a form array for inserting and module_invoking...ing.
|
| 247 |
$field = array(
|
| 248 |
'vid' => $array['vid'],
|
| 249 |
'fid' => $array['fields']['new']['fid'],
|
| 250 |
'title' => $array['fields']['new']['title'],
|
| 251 |
'module' => $module,
|
| 252 |
'type' => $field_type,
|
| 253 |
'settings' => serialize(array()),
|
| 254 |
);
|
| 255 |
|
| 256 |
//Create the entry in the DB
|
| 257 |
db_query('INSERT INTO {taxonomy_enhancer_fields} (vid, fid, title, module, type, settings) VALUES(%d, "%s", "%s", "%s", "%s", "%s")', $field);
|
| 258 |
|
| 259 |
//Invoke the module's hook
|
| 260 |
module_invoke($module, 'te_api', 'insert', 'field', $field);
|
| 261 |
}
|
| 262 |
|
| 263 |
//Handle updating of fields...
|
| 264 |
if (isset($array['fields']['created']) && is_array($array['fields']['created'])) {
|
| 265 |
foreach ($array['fields']['created'] as $fid => $field) {
|
| 266 |
//Get the module and type from the concatenated field (eg, 'taxonomy_enhancer_text|text'
|
| 267 |
list($module, $type) = explode('|', $field['type']);
|
| 268 |
|
| 269 |
//Compare the old field's module and type to the submitted field. If its changed then blank out the settings.
|
| 270 |
$old_field = taxonomy_enhancer_get_field($array['vid'], $field['fid']);
|
| 271 |
if ($old_field->module == $module && $old_field->type = $type) {
|
| 272 |
$settings = module_invoke($module, 'te_api', 'update', 'field', $field);
|
| 273 |
}
|
| 274 |
else {
|
| 275 |
$settings = array();
|
| 276 |
}
|
| 277 |
|
| 278 |
//Write the field.
|
| 279 |
db_query(
|
| 280 |
'UPDATE {taxonomy_enhancer_fields} SET title = "%s", module = "%s", type = "%s", settings = "%s" WHERE vid = %d AND fid = "%s"',
|
| 281 |
$field['title'], $module, $type, serialize($settings), $array['vid'], $field['fid']
|
| 282 |
);
|
| 283 |
}
|
| 284 |
}
|
| 285 |
break;
|
| 286 |
|
| 287 |
|
| 288 |
case 'delete' :
|
| 289 |
$fields = taxonomy_enhancer_get_fields_by_vocabulary($array['vid']);
|
| 290 |
foreach ($fields as $field) {
|
| 291 |
module_invoke($field->module, 'te_api', 'delete', 'field', $field);
|
| 292 |
db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $array['vid'], $field->fid);
|
| 293 |
}
|
| 294 |
break;
|
| 295 |
}
|
| 296 |
break;
|
| 297 |
|
| 298 |
|
| 299 |
case 'term' :
|
| 300 |
$fields = taxonomy_enhancer_get_fields_by_vocabulary($array['vid']);
|
| 301 |
|
| 302 |
switch ($op) {
|
| 303 |
case 'update' :
|
| 304 |
foreach ($fields as $field) {
|
| 305 |
module_invoke($field->module, 'te_api', 'update', 'value', $field, $array);
|
| 306 |
}
|
| 307 |
break;
|
| 308 |
|
| 309 |
case 'insert' :
|
| 310 |
foreach ($fields as $field) {
|
| 311 |
module_invoke($field->module, 'te_api', 'insert', 'value', $field, $array);
|
| 312 |
}
|
| 313 |
break;
|
| 314 |
|
| 315 |
case 'delete' :
|
| 316 |
foreach ($fields as $field) {
|
| 317 |
module_invoke($field->module, 'te_api', 'delete', 'value', $field, $array);
|
| 318 |
}
|
| 319 |
break;
|
| 320 |
}
|
| 321 |
break;
|
| 322 |
}
|
| 323 |
}
|
| 324 |
|
| 325 |
|
| 326 |
/**
|
| 327 |
* Get an individual field identified by Vocabulary ID and Field ID
|
| 328 |
*
|
| 329 |
* @param int $vid
|
| 330 |
* Vocabulary ID
|
| 331 |
* @param string $fid
|
| 332 |
* Field ID
|
| 333 |
* @return array
|
| 334 |
* Array of field objects
|
| 335 |
*/
|
| 336 |
function taxonomy_enhancer_get_field($vid, $fid) {
|
| 337 |
static $field_cache;
|
| 338 |
|
| 339 |
if (!isset($field_cache[$vid][$fid])) {
|
| 340 |
$field_cache[$vid][$fid] = db_fetch_object(db_query('SELECT * FROM {taxonomy_enhancer_fields} f WHERE vid = %d AND fid = "%s"', $vid, $fid));
|
| 341 |
$field_cache[$vid][$fid] = drupal_unpack($field_cache[$vid][$fid], 'settings');
|
| 342 |
unset($field_cache[$vid][$fid]->setttings);
|
| 343 |
}
|
| 344 |
|
| 345 |
return $field_cache[$vid][$fid];
|
| 346 |
}
|
| 347 |
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Get a list of the fields in any given vocabulary, sorted by weight and then title
|
| 351 |
*
|
| 352 |
* @param int $vid
|
| 353 |
* Vocabulary ID
|
| 354 |
* @return array
|
| 355 |
* Array of Fields
|
| 356 |
*/
|
| 357 |
function taxonomy_enhancer_get_fields_by_vocabulary($vid) {
|
| 358 |
static $fields = array();
|
| 359 |
|
| 360 |
if (!isset($fields[$vid])) {
|
| 361 |
$result = db_query('SELECT * FROM {taxonomy_enhancer_fields} f WHERE vid = %d ORDER BY weight', $vid);
|
| 362 |
|
| 363 |
$temp = array();
|
| 364 |
while ($row = db_fetch_object($result)) {
|
| 365 |
$temp[$row->fid] = $row;
|
| 366 |
$temp[$row->fid] = drupal_unpack($temp[$row->fid], 'settings');
|
| 367 |
unset($temp[$row->fid]->settings);
|
| 368 |
}
|
| 369 |
usort($temp, '_taxonomy_enhancer_field_sort');
|
| 370 |
|
| 371 |
$fields[$vid] = $temp;
|
| 372 |
}
|
| 373 |
|
| 374 |
|
| 375 |
return $fields[$vid];
|
| 376 |
}
|
| 377 |
|
| 378 |
|
| 379 |
/**
|
| 380 |
* Comparitor function for sorting fields.
|
| 381 |
* - Returns -1 (negative one) if 'A' is less than 'B'
|
| 382 |
* - Returns 1 (positive one) if 'A' is more than 'B'
|
| 383 |
* - Returns 0 (zero) if 'A' and 'B' are identical
|
| 384 |
*
|
| 385 |
* @param object $a
|
| 386 |
* Field Object A
|
| 387 |
* @param object $b
|
| 388 |
* Field Object B
|
| 389 |
* @return int
|
| 390 |
* Comparison result
|
| 391 |
*/
|
| 392 |
function _taxonomy_enhancer_field_sort($a, $b) {
|
| 393 |
if ($a->weight < $b->weight) {
|
| 394 |
return -1;
|
| 395 |
}
|
| 396 |
elseif ($aweight > $bweight) {
|
| 397 |
return 1;
|
| 398 |
}
|
| 399 |
elseif (isset($a->title) && isset($b->title)) {
|
| 400 |
return strnatcasecmp($a->title, $b->title);
|
| 401 |
}
|
| 402 |
else {
|
| 403 |
return 1;
|
| 404 |
}
|
| 405 |
}
|
| 406 |
|
| 407 |
|
| 408 |
/**
|
| 409 |
* Returns an array of field types. The key is in the form 'module|type', for example 'taxonomy_term_text|text'
|
| 410 |
*
|
| 411 |
* @return array
|
| 412 |
* Array of field types
|
| 413 |
*/
|
| 414 |
function taxonomy_enhancer_get_field_types() {
|
| 415 |
$items = array();
|
| 416 |
|
| 417 |
|
| 418 |
foreach (module_implements('te_api') as $module) {
|
| 419 |
$result = module_invoke($module, 'te_api', 'list', 'field');
|
| 420 |
|
| 421 |
if (is_array($result)) {
|
| 422 |
foreach ($result as $field => $title) {
|
| 423 |
$items[$module . '|' . $field] = $title;
|
| 424 |
}
|
| 425 |
}
|
| 426 |
}
|
| 427 |
|
| 428 |
return $items;
|
| 429 |
}
|
| 430 |
|
| 431 |
|
| 432 |
/**
|
| 433 |
* Theme function inspired by/leeched from Views (views_theme)
|
| 434 |
*
|
| 435 |
* Easily theme any item to an enhanced term field.
|
| 436 |
* @param $function
|
| 437 |
* The name of the function to call.
|
| 438 |
* @param $field
|
| 439 |
* The field being themed.
|
| 440 |
* @param $data
|
| 441 |
* The data being outputted
|
| 442 |
*/
|
| 443 |
function taxonomy_enhancer_theme() {
|
| 444 |
$args = func_get_args();
|
| 445 |
$function = array_shift($args);
|
| 446 |
$field = $args[0];
|
| 447 |
|
| 448 |
if (!($func = theme_get_function($function . "_" . $field->fid))) {
|
| 449 |
$func = theme_get_function($function);
|
| 450 |
}
|
| 451 |
|
| 452 |
if ($func) {
|
| 453 |
return call_user_func_array($func, $args);
|
| 454 |
}
|
| 455 |
}
|
| 456 |
|
| 457 |
|
| 458 |
/**
|
| 459 |
* Default Theme function for a generic field.
|
| 460 |
*
|
| 461 |
* @param object $field
|
| 462 |
* Field object
|
| 463 |
* @param string $data
|
| 464 |
* String to be outputted
|
| 465 |
* @return string
|
| 466 |
* Rendered Output
|
| 467 |
*/
|
| 468 |
function theme_taxonomy_enhanced_field($field, $data) {
|
| 469 |
return '<div id="field_' . $field->fid . '" class="taxonomy_enhanced_field ' . $field->type . '">' . $data . '</div>';
|
| 470 |
}
|
| 471 |
|