| 21 |
$form['inventory_items_const'] = array( |
$form['inventory_items_const'] = array( |
| 22 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 23 |
'#title' => t('Allowed inventory item list'), |
'#title' => t('Allowed inventory item list'), |
| 24 |
'#description' => t('The possible values this field can contain. Enter one value per line.'), |
'#description' => t('The possible values this field can contain. Enter one value per line. Must follow the format KEY|DESC.'), |
| 25 |
'#default_value' => $field['inventory_items_const'], |
'#default_value' => $field['inventory_items_const'], |
| 26 |
); |
); |
| 27 |
$form['advanced_options'] = array( |
$form['inventory_items_php'] = array( |
| 28 |
'#type' => 'fieldset', |
'#title' => t('PHP Code'), |
|
'#title' => t('Php code'), |
|
|
'#collapsible' => TRUE, |
|
|
'#collapsed' => empty($field['inventory_items_php']), |
|
|
); |
|
|
$form['advanced_options']['inventory_items_php'] = array( |
|
| 29 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 30 |
'#title' => t('Code'), |
'#description' => t('Advanced Usage Only: PHP code that returns a hash of allowed inventory items. Should not include <?php ?> delimiters.'), |
|
'#description' => t('Advanced Usage Only: PHP code that returns a keyed array of allowed inventory items. Should not include <?php ?> delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.'), |
|
| 31 |
'#default_value' => $field['inventory_items_php'], |
'#default_value' => $field['inventory_items_php'], |
| 32 |
); |
); |
| 33 |
$form['inventory_item_min'] = array( |
$form['inventory_items_query'] = array( |
| 34 |
|
'#type' => 'textarea', |
| 35 |
|
'#title' => t('SQL Query'), |
| 36 |
|
'#description' => t('Advanced Usage Only: SQL query that returns a list of 2 field rows, the first being used as key and the second as the item name. Beware that this is potentially dangerous if you do not know what you\'re doing.'), |
| 37 |
|
'#default_value' => $field['inventory_items_query'], |
| 38 |
|
); |
| 39 |
|
$form['inventory_items_content_types'] = array( |
| 40 |
|
'#type' => 'checkboxes', |
| 41 |
|
'#title' => t('Content Types'), |
| 42 |
|
'#description' => t('Content types to be used to build the inventory.'), |
| 43 |
|
'#default_value' => $field['inventory_items_content_types'], |
| 44 |
|
'#options' => node_get_types('names'), |
| 45 |
|
'#multiple' => TRUE, |
| 46 |
|
); |
| 47 |
|
$form['inventory_range'] = array( |
| 48 |
|
'#type' => 'fieldset', |
| 49 |
|
'#title' => t('Range Options'), |
| 50 |
|
'#collapsible' => FALSE, |
| 51 |
|
); |
| 52 |
|
$form['inventory_range']['inventory_item_min'] = array( |
| 53 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 54 |
'#title' => t('Minimum number of an item'), |
'#title' => t('Minimum number of an item'), |
| 55 |
'#description' => t('Please enter a non-negative integer'), |
'#description' => t('Please enter a non-negative integer'), |
| 56 |
'#default_value' => empty($field['inventory_items_min']) ? 0 : $field['inventory_items_min'], |
'#default_value' => empty($field['inventory_items_min']) ? 0 : $field['inventory_items_min'], |
| 57 |
); |
); |
| 58 |
$form['inventory_item_max'] = array( |
$form['inventory_range']['inventory_item_max'] = array( |
| 59 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 60 |
'#title' => t('Maximum number of an item'), |
'#title' => t('Maximum number of an item'), |
| 61 |
'#description' => t('Please enter a positive integer that is not lower than the minimum number.'), |
'#description' => t('Please enter a positive integer that is not lower than the minimum number.'), |
| 63 |
); |
); |
| 64 |
return $form; |
return $form; |
| 65 |
case 'save': |
case 'save': |
| 66 |
return array('inventory_items_const', 'inventory_items_php', 'inventory_item_min', 'inventory_item_max'); |
return array('inventory_items_const', 'inventory_items_php', 'inventory_item_min', 'inventory_item_max','inventory_items_query','inventory_items_content_types'); |
| 67 |
case 'validate': |
case 'validate': |
| 68 |
if (empty($field['inventory_items_const']) && empty($form['inventory_items_php'])) { |
// Couldn't find out why those values get into the checkboxes form element return :( |
| 69 |
form_set_error('inventory_items_const', t('You have to provide a list of available values either by typing the items in manually or giving a PHP code that will return them.')); |
unset($field['inventory_items_content_types']['0']); |
| 70 |
|
unset($field['inventory_items_content_types']['1']); |
| 71 |
|
// Fix up values from checkboxes control (isn't there an specific op for this?) |
| 72 |
|
$cts = array(); |
| 73 |
|
foreach($field['inventory_items_content_types'] as $type) { |
| 74 |
|
if($field['inventory_items_content_types'][$type]) { |
| 75 |
|
$cts[] = $type; |
| 76 |
|
} |
| 77 |
} |
} |
| 78 |
elseif (empty($field['inventory_items_const'])) { |
$field['inventory_items_content_types'] = $cts; |
| 79 |
|
// Just a funny way to say "one and only one" |
| 80 |
|
$n = (empty($field['inventory_items_const'])?0:1) + (empty($field['inventory_items_php'])?0:1) |
| 81 |
|
+ (empty($field['inventory_items_query'])?0:1) + (empty($field['inventory_items_content_types'])?0:1); |
| 82 |
|
if( ($n == 0) || ($n > 1) ) { |
| 83 |
|
form_set_error('inventory_items_const',t('You have to provide a list of available values either by typing the items in manually, giving an SQL query, the PHP code that will return them, or a list of node content types.')); |
| 84 |
|
} |
| 85 |
|
if (!empty($field['inventory_items_php'])) { |
| 86 |
$array = drupal_eval($field['inventory_items_php']); |
$array = drupal_eval($field['inventory_items_php']); |
| 87 |
if (!is_array($array) || empty($array)) { |
if (!is_array($array) || empty($array)) { |
| 88 |
form_set_error('inventory_items_const', t('The PHP snippet you have provided is not returning an array or the array itself is empty.')); |
form_set_error('inventory_items_php', t('The PHP snippet you have provided is not returning an array or the array itself is empty.')); |
| 89 |
} |
} |
| 90 |
} |
} |
| 91 |
if (!is_numeric($field['inventory_item_max']) || intval($field['inventory_item_max']) != $field['inventory_item_max'] || $field['inventory_item_max'] < 1) { |
if (!is_numeric($field['inventory_item_max']) || intval($field['inventory_item_max']) != $field['inventory_item_max'] || $field['inventory_item_max'] < 1) { |
| 110 |
function inventory_field($op, &$node, $field, &$node_field, $teaser, $page) { |
function inventory_field($op, &$node, $field, &$node_field, $teaser, $page) { |
| 111 |
switch ($op) { |
switch ($op) { |
| 112 |
case 'view': |
case 'view': |
| 113 |
$fieldset = _inventory_get_fields($field); |
$context = $teaser ? 'teaser' : 'full'; |
| 114 |
$rs = db_query("SELECT name, count FROM {inventory_items} WHERE vid = %d AND fieldset = '%s' ORDER BY name ASC", $node->vid, $field['field_name']); |
$formatter = isset($field['display_settings'][$context]['format']) ? |
| 115 |
while ($row = db_fetch_object($rs)) { |
$field['display_settings'][$context]['format'] : 'default'; |
| 116 |
if (in_array($row->name, $fieldset)) { |
$rs = db_query("SELECT itemkey, count FROM {inventory_items} " |
| 117 |
$node_field[] = array($row->name => $row->count); |
."WHERE vid = %d AND fieldset = '%s' ORDER BY itemkey ASC", $node->vid, $field['field_name']); |
| 118 |
|
//Since we don't store our data in any CCK DB column, we might have this populated or not (e.g: preview) |
| 119 |
|
if(empty($node_field)) { |
| 120 |
|
while ($row = db_fetch_object($rs)) { |
| 121 |
|
$node_field[$row->itemkey] = $row->count; |
| 122 |
} |
} |
| 123 |
} |
} |
| 124 |
|
$captions = _inventory_get_items($field); |
| 125 |
foreach ($node_field as $delta => $item) { |
foreach ($node_field as $delta => $item) { |
| 126 |
$node_field[$delta]['view'] = content_format($field, $item, 'default', $node); |
$node_field[$delta] = array ( |
| 127 |
|
//TODO $delta => $item, |
| 128 |
|
'view' => content_format($field, array($captions[$delta],$item), $formatter, $node) |
| 129 |
|
); |
| 130 |
} |
} |
| 131 |
return theme('field', $node, $field, $node_field, $teaser, $page); |
return theme('field', $node, $field, $node_field, $teaser, $page); |
| 132 |
case 'insert': case 'update': |
case 'insert': case 'update': |
| 133 |
// deleting because if the fields are determined by the PHP snippet and the snippet is |
// Entire cleanup here because we don't want to keep lost itemkey references |
| 134 |
// now giving a different fieldset than before, the deprecated fields must get ridden of |
db_query("DELETE FROM {inventory_items} WHERE vid = %d AND fieldset = '%s'", |
| 135 |
db_query("DELETE FROM {inventory_items} WHERE vid = %d AND fieldset = '%s'", $node->vid, $field['field_name']); |
$node->vid, $field['field_name']); |
| 136 |
foreach ($node_field as $item => $count) { |
foreach ($node_field as $itemkey => $count) { |
| 137 |
db_query("INSERT INTO {inventory_items} (nid, vid, fieldset, name, count) VALUES(%d, %d, '%s', '%s', %d)", $node->nid, $node->vid, $field['field_name'], $item, $count); |
db_query("INSERT INTO {inventory_items} (nid, vid, fieldset, itemkey, count) VALUES " |
| 138 |
|
."(%d, %d, '%s', '%s', %d)", $node->nid, $node->vid, $field['field_name'], $itemkey, $count); |
| 139 |
} |
} |
| 140 |
break; |
break; |
| 141 |
case 'delete': |
case 'delete': |
| 153 |
'label' => t('Default'), |
'label' => t('Default'), |
| 154 |
'field types' => array('inventory_field'), |
'field types' => array('inventory_field'), |
| 155 |
), |
), |
| 156 |
|
'skipempty' => array( |
| 157 |
|
'label' => t('Skip Empty Fields'), |
| 158 |
|
'field types' => array('inventory_field'), |
| 159 |
|
), |
| 160 |
); |
); |
| 161 |
return $formatters; |
return $formatters; |
| 162 |
} |
} |
| 168 |
// switch formatter in case we implement multiple formatters. |
// switch formatter in case we implement multiple formatters. |
| 169 |
switch ($formatter) { |
switch ($formatter) { |
| 170 |
case 'default': return theme('inventory_formatter_default', $item); |
case 'default': return theme('inventory_formatter_default', $item); |
| 171 |
|
case 'skipempty': return theme('inventory_formatter_skipempty', $item); |
| 172 |
} |
} |
| 173 |
} |
} |
| 174 |
|
|
| 178 |
function theme_inventory_formatter_default($item) { |
function theme_inventory_formatter_default($item) { |
| 179 |
$output = ''; |
$output = ''; |
| 180 |
if (is_array($item)) { |
if (is_array($item)) { |
| 181 |
$keys = array_keys($item); |
$output .= $item[0] .": ". ($item[1] == 0 ? t('None') : $item[1]); |
| 182 |
$output .= $keys[0] .": ". ($item[$keys[0]] == 0 ? t('None') : $item[$keys[0]]); |
} |
| 183 |
|
return $output; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
function theme_inventory_formatter_skipempty($item) { |
| 187 |
|
$output = ''; |
| 188 |
|
if (is_array($item)) { |
| 189 |
|
if( $item[1] != 0 ) { |
| 190 |
|
$output .= $item[0].": ".$item[1]; |
| 191 |
|
} |
| 192 |
} |
} |
| 193 |
return $output; |
return $output; |
| 194 |
} |
} |
| 198 |
*/ |
*/ |
| 199 |
function inventory_widget_info() { |
function inventory_widget_info() { |
| 200 |
return array( |
return array( |
| 201 |
'inventory_manual' => array( |
'inventory_select' => array( |
| 202 |
'label' => t('Manual Input'), |
'label' => t('Select List'), |
| 203 |
|
'field types' => array('inventory_field'), |
| 204 |
|
), |
| 205 |
|
'inventory_textfield' => array( |
| 206 |
|
'label' => t('Text Field'), |
| 207 |
'field types' => array('inventory_field'), |
'field types' => array('inventory_field'), |
| 208 |
), |
), |
| 209 |
); |
); |
| 216 |
$fieldname = $field['field_name']; |
$fieldname = $field['field_name']; |
| 217 |
switch ($op) { |
switch ($op) { |
| 218 |
case 'prepare form values': |
case 'prepare form values': |
| 219 |
$fieldset = _inventory_get_fields($field); |
$fieldset = _inventory_get_items($field); |
| 220 |
$rs = db_query("SELECT name, count FROM {inventory_items} WHERE vid = %d AND fieldset = '%s' ORDER BY name ASC", $node->vid, $field['field_name']); |
$rs = db_query("SELECT itemkey, count FROM {inventory_items} WHERE vid = %d AND fieldset = '%s' ORDER BY itemkey ASC", $node->vid, $field['field_name']); |
| 221 |
while ($row = db_fetch_object($rs)) { |
while ($row = db_fetch_object($rs)) { |
| 222 |
if (in_array($row->name, $fieldset)) { |
if (isset($fieldset[$row->itemkey])) { |
| 223 |
$node_field[$row->name] = $row->count; |
$node_field[$row->itemkey] = $row->count; |
| 224 |
} |
} |
| 225 |
} |
} |
| 226 |
break; |
break; |
| 227 |
|
|
| 228 |
case 'form': |
case 'form': |
| 229 |
|
// This is a bit redundant, might be cached |
| 230 |
|
$fieldset = _inventory_get_items($field); |
| 231 |
|
if (!($fieldset)) { |
| 232 |
|
return array(); |
| 233 |
|
} |
| 234 |
$form = array(); |
$form = array(); |
| 235 |
$form[$fieldname] = array( |
$form[$fieldname] = array( |
| 236 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 239 |
'#collapsible' => TRUE, |
'#collapsible' => TRUE, |
| 240 |
'#collapsed' => FALSE, |
'#collapsed' => FALSE, |
| 241 |
'#tree' => TRUE, |
'#tree' => TRUE, |
| 242 |
|
'#description' => t("Values in this fieldset must be in the range from %min to %max.",array( '%min' => $field['inventory_item_min'], '%max' => $field['inventory_item_max'])), |
| 243 |
); |
); |
| 244 |
$options = range($field['inventory_item_min'], $field['inventory_item_max']); |
switch($field['widget']['type']) { |
| 245 |
if (empty($options[0])) { |
default: |
| 246 |
$options[0] = t('None'); |
case 'inventory_textfield': |
| 247 |
|
foreach (array_keys($fieldset) as $itemkey) { |
| 248 |
|
$form[$fieldname][$itemkey] = array( |
| 249 |
|
'#type' => 'textfield', |
| 250 |
|
'#title' => $fieldset[$itemkey], |
| 251 |
|
'#size' => 6, |
| 252 |
|
'#maxlength' => 6, |
| 253 |
|
'#default_value' => $node_field[$itemkey], |
| 254 |
|
); |
| 255 |
|
}; |
| 256 |
|
break; |
| 257 |
|
|
| 258 |
|
case 'inventory_select': |
| 259 |
|
$options = range($field['inventory_item_min'], $field['inventory_item_max']); |
| 260 |
|
if (empty($options[0])) { |
| 261 |
|
$options[0] = t('None'); |
| 262 |
|
} |
| 263 |
|
foreach (array_keys($fieldset) as $itemkey) { |
| 264 |
|
$form[$fieldname][$itemkey] = array( |
| 265 |
|
'#type' => 'select', |
| 266 |
|
'#title' => $fieldset[$itemkey], |
| 267 |
|
'#options' => $options, |
| 268 |
|
'#default_value' => $node_field[$itemkey], |
| 269 |
|
); |
| 270 |
|
}; |
| 271 |
|
break; |
| 272 |
|
|
| 273 |
} |
} |
| 274 |
|
return $form; |
| 275 |
|
|
| 276 |
if ($fields = _inventory_get_fields($field)) { |
case 'validate': |
| 277 |
foreach ($fields as $item) { |
foreach(array_keys($node_field) as $itemkey) { |
| 278 |
$form[$fieldname][$item] = array( |
if( $node_field[$itemkey] && (($node_field[$itemkey] < $field['inventory_item_min']) || ($node_field[$itemkey] > $field['inventory_item_max'])) ) { |
| 279 |
'#type' => 'select', |
form_set_error($fieldname."][".$itemkey,t("The fields in this set must be in the range from %min to %max.",array('%min'=>$field['inventory_item_min'],'%max' => $field['inventory_item_max']))); |
|
'#title' => $item, |
|
|
'#options' => $options, |
|
|
'#default_value' => $node_field[$item], |
|
|
); |
|
| 280 |
} |
} |
|
return $form; |
|
| 281 |
} |
} |
| 282 |
return array(); |
break; |
| 283 |
|
|
| 284 |
} |
} |
| 285 |
} |
} |
| 286 |
|
|
| 287 |
/** |
/** |
| 288 |
* Collects the available fields and returns it in an array |
* Collects the available fields and returns them in an array |
| 289 |
*/ |
*/ |
| 290 |
function _inventory_get_fields($field) { |
function _inventory_get_items($field, $string = '') { |
| 291 |
static $field_set; |
$field_set = array(); |
| 292 |
|
if (!empty($field['inventory_items_query'])) { |
| 293 |
if (isset($field_set[$field['field_name']])) { |
$result = db_query($field['inventory_items_query']); |
| 294 |
return $field_set[$field['field_name']]; |
while($row=db_fetch_array($result)) { |
| 295 |
} |
$keys = array_keys($row); |
| 296 |
|
if(count($row)<2) { |
| 297 |
$field_set[$field['field_name']] = array(); |
$field_set[$row[$keys[0]]] = $row[$keys[0]]; |
| 298 |
if (!empty($field['inventory_items_php'])) { |
} else { |
| 299 |
|
$field_set[$row[$keys[0]]] = $row[$keys[1]]; |
| 300 |
|
} |
| 301 |
|
} |
| 302 |
|
} elseif (!empty($field['inventory_items_php'])) { // Items from PHP Code |
| 303 |
$result = eval($field['inventory_items_php']); |
$result = eval($field['inventory_items_php']); |
| 304 |
if (is_array($result)) { |
if (is_array($result)) { |
| 305 |
$field_set[$field['field_name']] = $result; |
$field_set = $result; |
| 306 |
} |
} |
| 307 |
} |
} elseif (!empty($field['inventory_items_content_types'])) { // Items from content type (autocomplete-ready) |
| 308 |
if (empty($field_set[$field['field_name']])) { |
$placeholders = array_fill(0, count($field['inventory_items_content_types']), "'%s'"); |
| 309 |
|
$sql = 'SELECT n.nid, n.title FROM {node} n WHERE n.type in ('. implode(',', $placeholders) .') AND n.status = 1 '; |
| 310 |
|
$args = $field['inventory_items_content_types']; |
| 311 |
|
if($string != '') { |
| 312 |
|
$sql .= " AND n.title LIKE '%%%s%'"; |
| 313 |
|
$args[] = $string; |
| 314 |
|
} |
| 315 |
|
$sql .= 'ORDER BY n.created DESC'; |
| 316 |
|
$sql = db_rewrite_sql($sql); |
| 317 |
|
$result = db_query($sql,$args); |
| 318 |
|
while($row=db_fetch_array($result)) { |
| 319 |
|
$keys = array_keys($row); |
| 320 |
|
if(count($row)<2) { break; } |
| 321 |
|
$field_set[$row[$keys[0]]] = $row[$keys[1]]; |
| 322 |
|
} |
| 323 |
|
} elseif (empty($field_set)) { // Items from plain textarea |
| 324 |
$list = explode("\n", $field['inventory_items_const']); |
$list = explode("\n", $field['inventory_items_const']); |
| 325 |
$list = array_map('trim', $list); |
$list = array_map('trim', $list); |
| 326 |
$list = array_filter($list, 'strlen'); |
$list = array_filter($list, 'strlen'); |
| 327 |
foreach ($list as $item) { |
foreach ($list as $item) { |
| 328 |
$field_set[$field['field_name']][] = $item; |
if(strpos($item,"|")) { |
| 329 |
|
$ii = explode("|",$item,2); |
| 330 |
|
if(count($ii)==2) { |
| 331 |
|
$field_set[$ii[0]] = $ii[1]; |
| 332 |
|
} |
| 333 |
|
} else { // To ease old behaviour - item keys same as names |
| 334 |
|
$field_set[$item] = $item; |
| 335 |
|
} |
| 336 |
} |
} |
| 337 |
} |
} |
| 338 |
return $field_set[$field['field_name']]; |
return $field_set; |
| 339 |
} |
} |
| 340 |
|
|