| 1 |
<?php
|
| 2 |
// $Id $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allow users to preselect a drop down given a large database and a free text "key"
|
| 7 |
*
|
| 8 |
* @author Mark Burton (aka markfoodyburton)
|
| 9 |
*/
|
| 10 |
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_widget_info.
|
| 14 |
* Specifies the label and that it is a widget for the text field type
|
| 15 |
*/
|
| 16 |
|
| 17 |
function fixedDataDropdown_widget_info()
|
| 18 |
{
|
| 19 |
return array(
|
| 20 |
'fixedDataDropdown_select' => array(
|
| 21 |
'label' => 'Fixed Data (dependent dropdown)',
|
| 22 |
'field types' => array('text'),
|
| 23 |
),
|
| 24 |
);
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* set up the taxonomy to be used
|
| 29 |
*/
|
| 30 |
function fixedDataDropdown_widget_settings($op, $field) {
|
| 31 |
switch ($op) {
|
| 32 |
case 'form':
|
| 33 |
$form = array();
|
| 34 |
|
| 35 |
$form['upload'] = array(
|
| 36 |
'#title' => t('Data File (csv of value pairs (i.e. "zip code", "town name")'),
|
| 37 |
'#type' => 'file',
|
| 38 |
'#default_value' => '',
|
| 39 |
'#attributes' => array('enctype' => "multipart/form-data"),
|
| 40 |
'#required' => FALSE,
|
| 41 |
);
|
| 42 |
$form['replace']=array(
|
| 43 |
'#title' => t('Remove old contents'),
|
| 44 |
'#type' => 'select',
|
| 45 |
'#options' => array('no'=>'no','yes'=>'yes'),
|
| 46 |
'#required' => TRUE,
|
| 47 |
);
|
| 48 |
$form['lineregexp']=array(
|
| 49 |
'#title' => t('Regular expression'),
|
| 50 |
'#type' => 'textfield',
|
| 51 |
'#default_value' => '/\"?(?P<key>[^,\"]+)\"?,\"?(?P<value>[^,\"]+)\"?/',
|
| 52 |
'#attributes' => array('enctype' => "multipart/form-data"),
|
| 53 |
'#required' => FALSE,
|
| 54 |
'#description' => t('Regular expression with which to split up file lines (must include "key" and "value" sub expressions). <p>Dont forget to check "multiple values"(below) if you want the key and the value remembered on this field'),
|
| 55 |
);
|
| 56 |
|
| 57 |
return $form;
|
| 58 |
break;
|
| 59 |
|
| 60 |
case 'validate':
|
| 61 |
$file = file_check_upload();
|
| 62 |
|
| 63 |
$re=$field['lineregexp'];
|
| 64 |
|
| 65 |
if ($fd = fopen($file->filepath, 'rb')) {
|
| 66 |
$dbt="fdd_".$field['field_name']."_data";
|
| 67 |
if ($field['replace']=='yes') {
|
| 68 |
drupal_set_message("Clearing old data from database");
|
| 69 |
db_query("DROP TABLE {". $dbt."}");
|
| 70 |
}
|
| 71 |
if (!db_table_exists($dbt)) {
|
| 72 |
db_query("CREATE TABLE {". $dbt."} (
|
| 73 |
fdd_key text,
|
| 74 |
fdd_value text, PRIMARY KEY(fdd_value(255)))");
|
| 75 |
}
|
| 76 |
$mess="DB values set (frst 10, then .'s)\n<br>\n";
|
| 77 |
$no=0;
|
| 78 |
while (!feof($fd)) {
|
| 79 |
preg_match($re, fgets($fd), $matches);
|
| 80 |
if ($no++<10) {
|
| 81 |
$mess.= $matches['key']." => ".$matches['value']."<br>\n";
|
| 82 |
} else {
|
| 83 |
$mess.=".";
|
| 84 |
}
|
| 85 |
|
| 86 |
db_query("UPDATE {%s} SET fdd_value=\"%s\" WHERE fdd_key = \"%s\"",$dbt,$matches['value'],$matches['key']);
|
| 87 |
if (!db_affected_rows()) {
|
| 88 |
@db_query("INSERT INTO {%s} (fdd_key, fdd_value) VALUES (\"%s\",\"%s\")",$dbt,$matches['key'],$matches['value']);
|
| 89 |
}
|
| 90 |
// db_query("INSERT INTO {%s} (fdd_key, fdd_value) VALUES (\"%s\",\"%s\") ON DUPLICATE KEY UPDATE fdd_key=VALUES(fdd_key)",$dbt,$matches['key'],$matches['value']);
|
| 91 |
}
|
| 92 |
fclose($fd);
|
| 93 |
drupal_set_message ($mess);
|
| 94 |
}
|
| 95 |
|
| 96 |
// return array('fdd_data');
|
| 97 |
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
|
| 102 |
/**
|
| 103 |
* the behavior of fixedDataDropdown
|
| 104 |
*/
|
| 105 |
|
| 106 |
function fixedDataDropdown_widget($op, &$node, $field, &$items) {
|
| 107 |
/* include_once(drupal_get_path('module', 'content').'/nodereference.module');*/
|
| 108 |
/* include_once(drupal_get_path('module', 'content_taxonomy').'/content_taxonomy.module');*/
|
| 109 |
|
| 110 |
switch ($op) {
|
| 111 |
case 'prepare form values':
|
| 112 |
$items['default value'] = array($items['value']);
|
| 113 |
break;
|
| 114 |
|
| 115 |
case 'form':
|
| 116 |
drupal_add_js(drupal_get_path('module', 'FixedDataDropdown') . '/FixedDataDropdown.js');
|
| 117 |
drupal_add_js(drupal_get_path('module', 'FixedDataDropdown') . '/jquery-select.js');
|
| 118 |
$form = array();
|
| 119 |
|
| 120 |
$fieldname=$field['field_name'];
|
| 121 |
|
| 122 |
$form[$fieldname] = array(
|
| 123 |
'#tree' => TRUE,
|
| 124 |
'#type' => 'fieldset',
|
| 125 |
'#title' => t($field['widget']['label']),
|
| 126 |
'#collapsible' => TRUE,
|
| 127 |
);
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
$form[$fieldname]['fdd_key'] = array(
|
| 132 |
'#type' => 'textfield',
|
| 133 |
'#title' => t($field['widget']['label'])." Selector ",
|
| 134 |
'#default_value' => $items[1]['value'],
|
| 135 |
'#required' => FALSE,
|
| 136 |
'#description' => $field['widget']['description'],
|
| 137 |
'#id' => "$fieldname",
|
| 138 |
'#autocomplete_path' => 'fixedDataDropdown/autocompleteKeys/'.$fieldname,
|
| 139 |
'#attributes' => array(
|
| 140 |
'class' => "fixedDataDropdown_selector",
|
| 141 |
),
|
| 142 |
);
|
| 143 |
|
| 144 |
/* pre-load options with values for default fdd key ? */
|
| 145 |
$options = array();
|
| 146 |
if ($items[0]['value']) {
|
| 147 |
$options[]=array($items[0]['value']=>$items[0]['value']);
|
| 148 |
} else {
|
| 149 |
$options = array(0 => t('<fill in selector first>'));
|
| 150 |
}
|
| 151 |
|
| 152 |
|
| 153 |
$form[$fieldname]['value'] = array(
|
| 154 |
'#type' => 'select',
|
| 155 |
'#title' => t($field['widget']['label']),
|
| 156 |
'#options' => $options,
|
| 157 |
'#required' => $field['required'],
|
| 158 |
'#description' => $field['widget']['description'],
|
| 159 |
'#id' => "$fieldname",
|
| 160 |
'#attributes' => array(
|
| 161 |
'class' => "fixedDataDropdown_value",
|
| 162 |
),
|
| 163 |
'#DANGEROUS_SKIP_CHECK'=>TRUE,
|
| 164 |
);
|
| 165 |
return $form;
|
| 166 |
|
| 167 |
case 'process form values':
|
| 168 |
$items[0]['value'] = $items['value'];
|
| 169 |
if ($field['multiple']) {
|
| 170 |
$items[1]['value'] = $items['fdd_key'];
|
| 171 |
}
|
| 172 |
// Remove the widget's data representation so it isn't saved.
|
| 173 |
unset($items['fdd_key']);
|
| 174 |
unset($items['value']);
|
| 175 |
|
| 176 |
break;
|
| 177 |
}
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Implementation of hook_menu().
|
| 182 |
*/
|
| 183 |
function fixedDataDropdown_menu($may_cache) {
|
| 184 |
|
| 185 |
$items = array();
|
| 186 |
if ($may_cache) {
|
| 187 |
$items[] = array(
|
| 188 |
'path' => 'fixedDataDropdown/autocomplete',
|
| 189 |
'title' => 'autocomplete for taxonomy',
|
| 190 |
'type' => MENU_CALLBACK,
|
| 191 |
'callback' => 'fixedDataDropdown_autocomplete',
|
| 192 |
'access' => user_access('access content'),
|
| 193 |
);
|
| 194 |
$items[] = array(
|
| 195 |
'path' => 'fixedDataDropdown/autocompleteKeys',
|
| 196 |
'title' => 'autocomplete for taxonomy',
|
| 197 |
'type' => MENU_CALLBACK,
|
| 198 |
'callback' => 'fixedDataDropdown_autocomplete_keys',
|
| 199 |
'access' => user_access('access content'),
|
| 200 |
);
|
| 201 |
}
|
| 202 |
return $items;
|
| 203 |
|
| 204 |
}
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
/**
|
| 209 |
* return back nodes that are tagged with $string.
|
| 210 |
*/
|
| 211 |
function fixedDataDropdown_autocomplete($field_name, $string = '') {
|
| 212 |
$fields = content_fields();
|
| 213 |
$field = $fields[$field_name];
|
| 214 |
$matches = array();
|
| 215 |
if ($string != '') {
|
| 216 |
$dbt="fdd_".$field_name."_data";
|
| 217 |
$result = db_query("SELECT fdd_value FROM {".$dbt."} WHERE fdd_key =\"%s\";",$string);
|
| 218 |
$row = 0;
|
| 219 |
while ($val = db_result($result, $row++)) {
|
| 220 |
$matches[$val]=$val;
|
| 221 |
}
|
| 222 |
if ($row==1) {
|
| 223 |
$result = db_query("SELECT fdd_value FROM {".$dbt."} WHERE instr(fdd_key, \"%s\") LIMIT 10;",$string);
|
| 224 |
$row = 0;
|
| 225 |
while ($val = db_result($result, $row++)) {
|
| 226 |
$matches[$val]=$val;
|
| 227 |
}
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
|
| 232 |
print drupal_to_js($matches);
|
| 233 |
|
| 234 |
|
| 235 |
exit();
|
| 236 |
}
|
| 237 |
|
| 238 |
function fixedDataDropdown_autocomplete_keys($field_name, $string = '') {
|
| 239 |
$fields = content_fields();
|
| 240 |
$field = $fields[$field_name];
|
| 241 |
$matches = array();
|
| 242 |
|
| 243 |
if ($string != '') {
|
| 244 |
$dbt="fdd_".$field_name."_data";
|
| 245 |
$result = db_query("SELECT fdd_key FROM {".$dbt."} WHERE instr(fdd_key,\"%s\") LIMIT 10;",$string);
|
| 246 |
$row = 0;
|
| 247 |
while ($val = db_result($result, $row++)) {
|
| 248 |
$matches[$val]=$val;
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
print drupal_to_js($matches);
|
| 253 |
|
| 254 |
|
| 255 |
exit();
|
| 256 |
}
|
| 257 |
|
| 258 |
?>
|