| 1 |
|
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* Defines a field type for referencing a knowledgtree document from a drupal node |
| 7 |
|
*/ |
| 8 |
|
|
| 9 |
|
/** |
| 10 |
|
* Implementation of hook_menu(). |
| 11 |
|
* |
| 12 |
|
* @TODO: Provide some relevant permissions and use here |
| 13 |
|
*/ |
| 14 |
|
function kt_docreference_menu() { |
| 15 |
|
$items = array(); |
| 16 |
|
$items['kt_docreference/autocomplete'] = array( |
| 17 |
|
'title' => 'kt_docreference autocomplete', |
| 18 |
|
'page callback' => 'kt_docreference_autocomplete', |
| 19 |
|
'access arguments' => array('access content'), |
| 20 |
|
'type' => MENU_CALLBACK |
| 21 |
|
); |
| 22 |
|
return $items; |
| 23 |
|
} |
| 24 |
|
|
| 25 |
|
/** |
| 26 |
|
* Implementation of hook_field_info(). |
| 27 |
|
*/ |
| 28 |
|
function kt_docreference_field_info() { |
| 29 |
|
return array( |
| 30 |
|
'kt_docreference' => array( |
| 31 |
|
'label' => t('Knowledgetree doc reference'), |
| 32 |
|
'description' => t('Store the ID of a knowledgetree document as an integer value.'), |
| 33 |
|
'content_icon' => 'icon_content_ktdocref.png', |
| 34 |
|
), |
| 35 |
|
); |
| 36 |
|
} |
| 37 |
|
|
| 38 |
|
function kt_docreference_field_settings($op, $field) { |
| 39 |
|
switch ($op) { |
| 40 |
|
|
| 41 |
|
case 'database columns': |
| 42 |
|
$columns = array( |
| 43 |
|
'value' => array('type' => 'int', 'size'=>'big', 'not null' => FALSE, 'sortable' => FALSE), |
| 44 |
|
); |
| 45 |
|
return $columns; |
| 46 |
|
} |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
function kt_docreference_field($op, &$node, $field, &$items, $teaser, $page) { |
| 50 |
|
switch ($op) { |
| 51 |
|
case 'validate': |
| 52 |
|
return $items; |
| 53 |
|
} |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
function kt_docreference_widget_info() { |
| 57 |
|
return array( |
| 58 |
|
'kt_docreference_autocomplete' => array( |
| 59 |
|
'label' => t('Autocomplete text field'), |
| 60 |
|
'field types' => array('kt_docreference'), |
| 61 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 62 |
|
'callbacks' => array( |
| 63 |
|
'default value' => CONTENT_CALLBACK_DEFAULT, |
| 64 |
|
), |
| 65 |
|
), |
| 66 |
|
); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
function kt_docreference_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
| 70 |
|
switch ($field['widget']['type']) { |
| 71 |
|
case 'kt_docreference_autocomplete': |
| 72 |
|
$element = array( |
| 73 |
|
'#type' => 'kt_docreference_autocomplete', |
| 74 |
|
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '', |
| 75 |
|
'#value_callback' => 'kt_docreference_autocomplete_value', |
| 76 |
|
); |
| 77 |
|
break; |
| 78 |
|
} |
| 79 |
|
return $element; |
| 80 |
|
|
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
function kt_docreference_content_is_empty($item, $field) { |
| 84 |
|
if (empty($item['value']) && (string)$item['value'] !== '0') { |
| 85 |
|
return TRUE; |
| 86 |
|
} |
| 87 |
|
return FALSE; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
function kt_docreference_field_formatter_info() { |
| 91 |
|
return array( |
| 92 |
|
'plain' => array( |
| 93 |
|
'label' => t('Document Name (no link)'), |
| 94 |
|
'field types' => array('kt_docreference'), |
| 95 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 96 |
|
), |
| 97 |
|
'default' => array( |
| 98 |
|
'label' => t('Document Name (link to KT detail)'), |
| 99 |
|
'field types' => array('kt_docreference'), |
| 100 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 101 |
|
), |
| 102 |
|
'download' => array( |
| 103 |
|
'label' => t('Document Name (link to download)'), |
| 104 |
|
'field types' => array('kt_docreference'), |
| 105 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 106 |
|
), |
| 107 |
|
'multi' => array( |
| 108 |
|
'label' => t('Document Name (links to view and download)'), |
| 109 |
|
'field types' => array('kt_docreference'), |
| 110 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 111 |
|
), |
| 112 |
|
|
| 113 |
|
); |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
/** |
| 117 |
|
* Implementation of FAPI hook_elements(). |
| 118 |
|
* |
| 119 |
|
* Any FAPI callbacks needed for individual widgets can be declared here, |
| 120 |
|
* and the element will be passed to those callbacks for processing. |
| 121 |
|
* |
| 122 |
|
* Drupal will automatically theme the element using a theme with |
| 123 |
|
* the same name as the hook_elements key. |
| 124 |
|
* |
| 125 |
|
* Autocomplete_path is not used by text_widget but other widgets can use it |
| 126 |
|
* (see nodereference and userreference). |
| 127 |
|
*/ |
| 128 |
|
function kt_docreference_elements() { |
| 129 |
|
return array( |
| 130 |
|
'kt_docreference_autocomplete' => array( |
| 131 |
|
'#input' => TRUE, |
| 132 |
|
'#columns' => array('name'), '#delta' => 0, |
| 133 |
|
'#process' => array('kt_docreference_autocomplete_process'), |
| 134 |
|
'#autocomplete_path' => FALSE, |
| 135 |
|
), |
| 136 |
|
); |
| 137 |
|
} |
| 138 |
|
function kt_docreference_theme() { |
| 139 |
|
return array( |
| 140 |
|
'kt_docreference_autocomplete' => array( |
| 141 |
|
'arguments' => array('element'), |
| 142 |
|
), |
| 143 |
|
'kt_docreference_formatter_default' => array( |
| 144 |
|
'arguments' => array('element'), |
| 145 |
|
'function' => 'theme_kt_docreference_formatter_default', |
| 146 |
|
), |
| 147 |
|
'kt_docreference_formatter_plain' => array( |
| 148 |
|
'arguments' => array('element'), |
| 149 |
|
'function' => 'theme_kt_docreference_formatter_plain', |
| 150 |
|
), |
| 151 |
|
'kt_docreference_formatter_download' => array( |
| 152 |
|
'arguments' => array('element'), |
| 153 |
|
'function' => 'theme_kt_docreference_formatter_download', |
| 154 |
|
), |
| 155 |
|
'kt_docreference_formatter_multi' => array( |
| 156 |
|
'arguments' => array('element'), |
| 157 |
|
'function' => 'theme_kt_docreference_formatter_multi', |
| 158 |
|
), |
| 159 |
|
); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
function theme_kt_docreference_formatter_plain($element) { |
| 163 |
|
$output = ''; |
| 164 |
|
if (!empty($element['#item']['value']) && is_numeric($element['#item']['value']) && ($title = _kt_docreference_document_title($element['#item']['value']))) { |
| 165 |
|
$output = $title; |
| 166 |
|
} |
| 167 |
|
return $output; |
| 168 |
|
} |
| 169 |
|
|
| 170 |
|
function theme_kt_docreference_formatter_default($element) { |
| 171 |
|
$output = ''; |
| 172 |
|
if (!empty($element['#item']['value']) && is_numeric($element['#item']['value']) && ($title = _kt_docreference_document_title($element['#item']['value']))) { |
| 173 |
|
$output = _kt_docreference_details_link($title, $element['#item']['value']); |
| 174 |
|
} |
| 175 |
|
return $output; |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
function theme_kt_docreference_formatter_download($element) { |
| 179 |
|
$output = ''; |
| 180 |
|
if (!empty($element['#item']['value']) && is_numeric($element['#item']['value']) && ($title = _kt_docreference_document_title($element['#item']['value']))) { |
| 181 |
|
$output = _kt_docreference_download_link($title, $element['#item']['value']); |
| 182 |
|
} |
| 183 |
|
return $output; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
function theme_kt_docreference_formatter_multi($element) { |
| 187 |
|
$output = ''; |
| 188 |
|
if (!empty($element['#item']['value']) && is_numeric($element['#item']['value']) && ($title = _kt_docreference_document_title($element['#item']['value']))) { |
| 189 |
|
$output = $title .' ('. _kt_docreference_details_link('Details', $element['#item']['value']) .'|'. _kt_docreference_download_link('Download', $element['#item']['value']) .')'; |
| 190 |
|
} |
| 191 |
|
return $output; |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
function _kt_docreference_document_title($document_id) { |
| 195 |
|
|
| 196 |
|
$document = array(); |
| 197 |
|
|
| 198 |
|
$kt = new Drupal_KT_REST; |
| 199 |
|
$kt->login(); |
| 200 |
|
if ($document = $kt->get_document_detail($document_id, "")) { |
| 201 |
|
$document = _kt_xmlobj2array($document); |
| 202 |
|
} |
| 203 |
|
if (isset($document['title']) && !empty($document['title'])) { |
| 204 |
|
return $document['title']; |
| 205 |
|
} |
| 206 |
|
else { |
| 207 |
|
return 'Untitled Document #'. $document_id; |
| 208 |
|
} |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
function _kt_docreference_details_link($title, $document_id) { |
| 212 |
|
if (($kt_url = variable_get('kt_root_url', '')) == '') { |
| 213 |
|
watchdog("kt_docreference", "KT REST root URL is not configured, so document links cannot be displayed", WATCHDOG_WARNING); |
| 214 |
|
return $title; |
| 215 |
|
} |
| 216 |
|
else { |
| 217 |
|
return l($title, $kt_url .'view.php?fDocumentId='. $document_id, array('attributes' => array('target' => '_blank'))); |
| 218 |
|
} |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
function _kt_docreference_download_link($title, $document_id) { |
| 222 |
|
if (($kt_url = variable_get('kt_root_url', '')) == '') { |
| 223 |
|
watchdog("kt_docreference", "KT REST root URL is not configured, so document links cannot be displayed", WATCHDOG_WARNING); |
| 224 |
|
return $title; |
| 225 |
|
} |
| 226 |
|
else { |
| 227 |
|
return l($title, $kt_url .'action.php?kt_path_info=ktcore.actions.document.view&fDocumentId='. $document_id, array('attributes' => array('target' => '_blank'))); |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
/** |
| 233 |
|
* Value for a kt_docreference autocomplete element. |
| 234 |
|
* |
| 235 |
|
* Substitute in the node title for the node nid. |
| 236 |
|
*/ |
| 237 |
|
function kt_docreference_autocomplete_value($element, $edit = FALSE) { |
| 238 |
|
$field_key = $element['#columns'][0]; |
| 239 |
|
if (!empty($element['#default_value'])) { |
| 240 |
|
$document_id = $element['#default_value']; |
| 241 |
|
$value = _kt_docreference_document_title($document_id); |
| 242 |
|
$value .= ' [DocumentID:'. $document_id .']'; |
| 243 |
|
return array($field_key => $value); |
| 244 |
|
} |
| 245 |
|
return array($field_key => NULL); |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
/** |
| 249 |
|
* Process an individual element. |
| 250 |
|
* |
| 251 |
|
* Build the form element. When creating a form using FAPI #process, |
| 252 |
|
* note that $element['#value'] is already set. |
| 253 |
|
* |
| 254 |
|
*/ |
| 255 |
|
function kt_docreference_autocomplete_process($element, $edit, $form_state, $form) { |
| 256 |
|
|
| 257 |
|
// The kt_docreference autocomplete widget doesn't need to create its own |
| 258 |
|
// element, it can wrap around the text_textfield element and add an autocomplete |
| 259 |
|
// path and some extra processing to it. |
| 260 |
|
// Add a validation step where the value can be unwrapped. |
| 261 |
|
$field_key = $element['#columns'][0]; |
| 262 |
|
|
| 263 |
|
$element[$field_key] = array( |
| 264 |
|
'#type' => 'text_textfield', |
| 265 |
|
'#default_value' => isset($element['#value']) ? $element['#value'] : '', |
| 266 |
|
'#autocomplete_path' => 'kt_docreference/autocomplete/'. $element['#field_name'], |
| 267 |
|
// The following values were set by the content module and need |
| 268 |
|
// to be passed down to the nested element. |
| 269 |
|
'#title' => $element['#title'], |
| 270 |
|
'#required' => $element['#required'], |
| 271 |
|
'#description' => $element['#description'], |
| 272 |
|
'#field_name' => $element['#field_name'], |
| 273 |
|
'#type_name' => $element['#type_name'], |
| 274 |
|
'#delta' => $element['#delta'], |
| 275 |
|
'#columns' => $element['#columns'], |
| 276 |
|
); |
| 277 |
|
if (empty($element[$field_key]['#element_validate'])) { |
| 278 |
|
$element[$field_key]['#element_validate'] = array(); |
| 279 |
|
} |
| 280 |
|
array_unshift($element[$field_key]['#element_validate'], 'kt_docreference_autocomplete_validate'); |
| 281 |
|
|
| 282 |
|
// Used so that hook_field('validate') knows where to flag an error. |
| 283 |
|
$element['_error_element'] = array( |
| 284 |
|
'#type' => 'value', |
| 285 |
|
// Wrapping the element around a text_textfield element creates a |
| 286 |
|
// nested element, so the final id will look like 'field-name-0-nid-nid'. |
| 287 |
|
'#value' => implode('][', array_merge($element['#parents'], array($field_key, $field_key))), |
| 288 |
|
); |
| 289 |
|
return $element; |
| 290 |
|
} |
| 291 |
|
|
| 292 |
|
/** |
| 293 |
|
* Validate an autocomplete element. |
| 294 |
|
* |
| 295 |
|
* Remove the wrapper layer and set the right element's value. |
| 296 |
|
* This will move the nested value at 'field-name-0-nid-nid' |
| 297 |
|
* back to its original location, 'field-name-0-nid'. |
| 298 |
|
*/ |
| 299 |
|
function kt_docreference_autocomplete_validate($element, &$form_state) { |
| 300 |
|
$field_name = $element['#field_name']; |
| 301 |
|
$type_name = $element['#type_name']; |
| 302 |
|
$field = content_fields($field_name, $type_name); |
| 303 |
|
$field_key = $element['#columns'][0]; |
| 304 |
|
$delta = $element['#delta']; |
| 305 |
|
$value = $element['#value'][$field_key]; |
| 306 |
|
$document_id = NULL; |
| 307 |
|
if (!empty($value)) { |
| 308 |
|
preg_match('/^(?:\s*|(.*) )?\[DocumentID:(\d+)\]$/', $value, $matches); |
| 309 |
|
if (!empty($matches)) { |
| 310 |
|
// Explicit [DocumentID:n]. |
| 311 |
|
list(, $title, $document_id) = $matches; |
| 312 |
|
if (!empty($title) && ($title != _kt_docreference_document_title($document_id))) { |
| 313 |
|
form_error($element[$field_key], t('%name: title mismatch. Please check your selection.', array('%name' => t($field['widget']['label'])))); |
| 314 |
|
} |
| 315 |
|
} |
| 316 |
|
else { |
| 317 |
|
// No explicit DocumentID. |
| 318 |
|
$reference = _kt_docreference_autocomplete($field, $value); |
| 319 |
|
if (empty($reference)) { |
| 320 |
|
form_error($element[$field_key], t('%name: found no valid document with that title.', array('%name' => t($field['widget']['label'])))); |
| 321 |
|
} |
| 322 |
|
else { |
| 323 |
|
// TODO: |
| 324 |
|
// the best thing would be to present the user with an additional form, |
| 325 |
|
// allowing the user to choose between valid candidates with the same title |
| 326 |
|
// ATM, we pick the first matching candidate... |
| 327 |
|
$document_id = key($reference); |
| 328 |
|
} |
| 329 |
|
} |
| 330 |
|
} |
| 331 |
|
form_set_value($element, $document_id, $form_state); |
| 332 |
|
} |
| 333 |
|
/** |
| 334 |
|
* Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing users |
| 335 |
|
*/ |
| 336 |
|
function kt_docreference_autocomplete($field_name, $string = '') { |
| 337 |
|
|
| 338 |
|
if ($string == '') { |
| 339 |
|
return; |
| 340 |
|
} |
| 341 |
|
|
| 342 |
|
$kt = new Drupal_KT_REST; |
| 343 |
|
$kt->login(); |
| 344 |
|
|
| 345 |
|
$search_query = '(Filename CONTAINS "'. $string .'") OR (Title CONTAINS "'. $string .'")'; |
| 346 |
|
|
| 347 |
|
$results = array(); |
| 348 |
|
$results = _kt_xmlobj2array($kt->search($search_query, "optionsargdoesntseemtodoanything")); |
| 349 |
|
|
| 350 |
|
if (!is_array($results) || empty($results)) { // We got nothing! |
| 351 |
|
drupal_json(array()); // return empty array |
| 352 |
|
return; |
| 353 |
|
} |
| 354 |
|
else if (is_array($results['item'][0])) { // We got multiple results = array(item=>(0=>first_doc,1=>second_doc...)) |
| 355 |
|
$results = $results['item']; // |
| 356 |
|
} |
| 357 |
|
else { // We got only one document - results = array(item=>first_doc) with no index. |
| 358 |
|
$results[0] = $results['item']; // Make it consistent with multiple results structure, only one doc on index 0 |
| 359 |
|
} |
| 360 |
|
|
| 361 |
|
foreach ($results as $result) { |
| 362 |
|
// Add a class wrapper for a few required CSS overrides. |
| 363 |
|
$title = $result['title']; |
| 364 |
|
$id = $result['document_id']; |
| 365 |
|
$matches[$title ." [DocumentID:$id]"] = '<div class="reference-autocomplete">'. $title .'</div>'; |
| 366 |
|
} |
| 367 |
|
|
| 368 |
|
drupal_json($matches); |
| 369 |
|
} |
| 370 |
|
|
| 371 |
|
function theme_kt_docreference_autocomplete($element) { |
| 372 |
|
return $element['#children']; |
| 373 |
|
} |