| 1 |
<?php
|
| 2 |
// $Id: currency_cck.module,v 1.1.2.1 2008/01/02 11:01:31 melsawy Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Defines a field type for currency.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function currency_cck_help($section) {
|
| 13 |
switch ($section) {
|
| 14 |
case 'admin/modules#description':
|
| 15 |
return t('<strong>CCK:</strong> Defines a field type for currency. <em>Note: Requires content.module and currency_api.</em>');
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_field_info().
|
| 21 |
*/
|
| 22 |
function currency_cck_field_info() {
|
| 23 |
return array(
|
| 24 |
'currency_cck' => array('label' => 'Currency'),
|
| 25 |
);
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_field_settings().
|
| 30 |
*/
|
| 31 |
function currency_cck_field_settings($op, $field) {
|
| 32 |
switch ($op) {
|
| 33 |
case 'database columns':
|
| 34 |
$columns = array(
|
| 35 |
'currency' => array('type' => 'varchar', 'length' => 5, 'not null' => TRUE, 'default' => "''"),
|
| 36 |
);
|
| 37 |
return $columns;
|
| 38 |
|
| 39 |
case 'filters':
|
| 40 |
return array(
|
| 41 |
'default' => array(
|
| 42 |
'name' => t('Default'),
|
| 43 |
'operator' => 'views_handler_operator_or',
|
| 44 |
),
|
| 45 |
);
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Implementation of hook_field().
|
| 51 |
*/
|
| 52 |
function currency_cck_field($op, &$node, $field, &$items, $teaser, $page) {
|
| 53 |
switch ($op) {
|
| 54 |
case 'view':
|
| 55 |
foreach ($items as $delta => $item) {
|
| 56 |
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
|
| 57 |
}
|
| 58 |
return theme('field', $node, $field, $items, $teaser, $page);
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Implementation of hook_field_formatter_info().
|
| 64 |
*/
|
| 65 |
function currency_cck_field_formatter_info() {
|
| 66 |
return array(
|
| 67 |
'default' => array(
|
| 68 |
'label' => 'Default',
|
| 69 |
'field types' => array('currency_cck'),
|
| 70 |
),
|
| 71 |
'plain' => array(
|
| 72 |
'label' => 'Plain text',
|
| 73 |
'field types' => array('currency_cck'),
|
| 74 |
),
|
| 75 |
);
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Implementation of hook_field_formatter().
|
| 80 |
*/
|
| 81 |
function currency_cck_field_formatter($field, $item, $formatter, $node) {
|
| 82 |
$text = '';
|
| 83 |
if (isset($item['currency'])) {
|
| 84 |
$currency_desc = currency_api_get_desc($item['currency']);
|
| 85 |
if ($currency_desc !== FALSE) {
|
| 86 |
$text = $currency_desc;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
switch ($formatter) {
|
| 90 |
case 'plain':
|
| 91 |
return strip_tags($text);
|
| 92 |
|
| 93 |
default:
|
| 94 |
return $text;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* Implementation of hook_widget_info().
|
| 100 |
*/
|
| 101 |
function currency_cck_widget_info() {
|
| 102 |
return array(
|
| 103 |
'currency_cck_select' => array(
|
| 104 |
'label' => 'Select List',
|
| 105 |
'field types' => array('currency_cck'),
|
| 106 |
),
|
| 107 |
);
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Implementation of hook_widget().
|
| 112 |
*/
|
| 113 |
function currency_cck_widget($op, &$node, $field, &$items) {
|
| 114 |
switch ($op) {
|
| 115 |
case 'prepare form values':
|
| 116 |
$items_transposed = content_transpose_array_rows_cols($items);
|
| 117 |
$items['default currency'] = $items_transposed['currency'];
|
| 118 |
break;
|
| 119 |
case 'form':
|
| 120 |
$form = array();
|
| 121 |
$options = currency_api_get_list();
|
| 122 |
if (!$field['required']) {
|
| 123 |
$options = array('none' => t('<none>')) + $options;
|
| 124 |
}
|
| 125 |
if (empty($items['default currency'])) {
|
| 126 |
$items['default currency'][] = 'none';
|
| 127 |
}
|
| 128 |
$form[$field['field_name']] = array('#tree' => TRUE);
|
| 129 |
$form[$field['field_name']]['currency'] = array(
|
| 130 |
'#type' => 'select',
|
| 131 |
'#title' => t($field['widget']['label']),
|
| 132 |
'#default_value' => $items['default currency'],
|
| 133 |
'#multiple' => $field['multiple'],
|
| 134 |
'#size' => $field['multiple'] ? min(count($options), 6) : 0,
|
| 135 |
'#options' => $options,
|
| 136 |
'#required' => $field['required'],
|
| 137 |
'#description' => t($field['widget']['description']),
|
| 138 |
);
|
| 139 |
return $form;
|
| 140 |
|
| 141 |
case 'process form values':
|
| 142 |
if ($field['multiple']) {
|
| 143 |
// drop the 'none' option
|
| 144 |
unset($items['currency']['none']);
|
| 145 |
if (!empty($items['currency'])) {
|
| 146 |
$items = array_values(content_transpose_array_rows_cols(array('currency' => $items['currency'])));
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
$items[0]['currency'] = '';
|
| 150 |
}
|
| 151 |
}
|
| 152 |
else {
|
| 153 |
$items[0]['currency'] = ($items['currency'] != 'none') ? $items['currency'] : '';
|
| 154 |
}
|
| 155 |
// Remove the widget's data representation so it isn't saved.
|
| 156 |
unset($items['currency']);
|
| 157 |
foreach ($items as $delta => $item) {
|
| 158 |
$items[$delta]['error_field'] = $field['field_name'] .'][currency';
|
| 159 |
}
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|