-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
farm_quantity.module
320 lines (279 loc) · 7.79 KB
/
farm_quantity.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* @file
* Farm quantity module.
*/
// Include Features code.
include_once 'farm_quantity.features.inc';
/**
* Implements hook_farm_ui_entities().
*/
function farm_quantity_farm_ui_entities() {
return array(
'taxonomy_term' => array(
'farm_quantity_units' => array(
'label' => t('Quantity unit'),
'label_plural' => t('Quantity units'),
),
),
);
}
/**
* Implements hook_restws_field_collection_info().
*/
function farm_quantity_restws_field_collection_info() {
return array(
'field_farm_quantity' => array(
'alias' => 'quantity',
'label' => t('Quantity'),
'multiple' => TRUE,
'fields' => array(
'measure' => array(
'field_name' => 'field_farm_quantity_measure',
'field_label' => t('Measure'),
'field_type' => 'text',
'field_value' => 'value',
),
'value' => array(
'field_name' => 'field_farm_quantity_value',
'field_label' => t('Value'),
'field_type' => 'decimal',
'field_value' => 'decimal',
),
'unit' => array(
'field_name' => 'field_farm_quantity_units',
'field_label' => t('Unit'),
'field_type' => 'taxonomy_term',
'field_value' => 'tid',
),
'label' => array(
'field_name' => 'field_farm_quantity_label',
'field_label' => t('Label'),
'field_type' => 'text',
'field_value' => 'value',
),
),
),
);
}
/**
* Implements hook_permission().
*/
function farm_quantity_permission() {
$perms = array(
'administer farm_quantity module' => array(
'title' => t('Administer farm quantity module'),
),
);
return $perms;
}
/**
* Implements hook_farm_access_perms().
*/
function farm_quantity_farm_access_perms($role) {
$perms = array();
// Load the list of farm roles.
$roles = farm_access_roles();
// If this role has 'config' access, grant access to quantity configuration.
if (!empty($roles[$role]['access']['config'])) {
$perms[] = 'administer farm_quantity module';
}
return $perms;
}
/**
* Implements hook_menu().
*/
function farm_quantity_menu() {
// Quantity configuration form.
$items['admin/config/farm/quantity'] = array(
'title' => 'Quantity',
'description' => 'Quantity configuration settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_quantity_settings_form'),
'access arguments' => array('administer farm_quantity module'),
);
return $items;
}
/**
* Quantity settings form.
*/
function farm_quantity_settings_form($form, &$form_state) {
// Metric or US/Imperial.
$form['farm_quantity_unit_system'] = array(
'#type' => 'radios',
'#title' => t('System of measurement'),
'#description' => t('Select the system of measurement you would like to use in farmOS. Changing this setting after data has been entered is not recommended.'),
'#options' => array(
'metric' => t('Metric'),
'us' => t('US/Imperial'),
),
'#default_value' => farm_quantity_system_of_measurement(),
);
// Return it as a system settings form.
return system_settings_form($form);
}
/**
* Helper function for loading the default system of measurement.
*
* @return string
* Returns either 'metric' or 'us' (defaults to 'metric').
*/
function farm_quantity_system_of_measurement() {
return variable_get('farm_quantity_unit_system', 'metric');
}
/**
* Define information about available quantity measures.
*
* @return array
* Returns an array of measure information.
*/
function farm_quantity_measures() {
return array(
'count' => array(
'label' => t('Count'),
),
'length' => array(
'label' => t('Length'),
),
'weight' => array(
'label' => t('Weight'),
),
'area' => array(
'label' => t('Area'),
),
'volume' => array(
'label' => t('Volume'),
),
'time' => array(
'label' => t('Time'),
),
'temperature' => array(
'label' => t('Temperature'),
),
'water_content' => array(
'label' => t('Water content'),
),
'value' => array(
'label' => t('Value'),
),
'rating' => array(
'label' => t('Rating'),
),
'ratio' => array(
'label' => t('Ratio'),
),
'probability' => array(
'label' => t('Probability'),
),
);
}
/**
* Define available options for the Measure field.
*/
function farm_quantity_measure_options() {
// Start an empty options array.
$options = array();
// Load information about measures.
$measures = farm_quantity_measures();
// Iterate through the measures and build a list of options.
foreach ($measures as $measure => $data) {
$options[$measure] = $data['label'];
}
// Return the array of options.
return $options;
}
/**
* Return an array of standard unit names for given measures.
*
* @todo
* This is a first step towards more complete unit management in farmOS. It
* is provided as a central helper function for other modules to use as a
* placeholder until then.
*
* @param string $measure
* An optional measure to filter by.
* See available options in farm_quantity_measures() above.
*
* @return array
* Returns an array of standard unit strings for a given measure. Or an array
* of all unit strings, organized into sub-arrays keyed by measure.
*/
function farm_quantity_units($measure = '') {
// Load the available measures.
$measures = farm_quantity_measures();
// Build an empty array of quantity unit sub-arrays, keyed by measure.
$units = array();
foreach ($measures as $name => $info) {
$units[$name] = array();
}
// Look up the system of measurement.
$system = farm_quantity_system_of_measurement();
// Depending on the system of measurement, add unit options to measures.
if ($system == 'metric') {
// Length units.
$units['length'][] = 'kilometers';
$units['length'][] = 'meters';
$units['length'][] = 'centimeters';
// Weight units.
$units['weight'][] = 'kilograms';
$units['weight'][] = 'grams';
// Area units.
$units['area'][] = 'hectares';
$units['area'][] = 'square meters';
// Volume units.
$units['volume'][] = 'kiloliters';
$units['volume'][] = 'liters';
$units['volume'][] = 'milliliters';
// Temperature units.
$units['temperature'][] = 'C';
}
elseif ($system == 'us') {
// Length units.
$units['length'][] = 'miles';
$units['length'][] = 'feet';
$units['length'][] = 'inches';
// Weight units.
$units['weight'][] = 'tons';
$units['weight'][] = 'lbs';
$units['weight'][] = 'ounces';
// Area units.
$units['area'][] = 'acres';
$units['area'][] = 'square feet';
// Volume units.
$units['volume'][] = 'cubic yards';
$units['volume'][] = 'cubic feet';
$units['volume'][] = 'gallons';
$units['volume'][] = 'quarts';
$units['volume'][] = 'cups';
// Temperature units.
$units['temperature'][] = 'F';
}
// Time units are independent of system.
$units['time'][] = 'years';
$units['time'][] = 'months';
$units['time'][] = 'weeks';
$units['time'][] = 'days';
$units['time'][] = 'hours';
$units['time'][] = 'minutes';
$units['time'][] = 'seconds';
// If a measure is specified, only return units for that measure.
if (!empty($measure)) {
if (!empty($units[$measure])) {
return $units[$measure];
}
else {
return array();
}
}
// Return all quantity units.
return $units;
}
/**
* Implements hook_preprocess_field().
*/
function farm_quantity_preprocess_field(&$vars) {
// Add quantity CSS when a Quantity field is displayed.
if (!empty($vars['element']['#field_name']) && $vars['element']['#field_name'] == 'field_farm_quantity') {
drupal_add_css(drupal_get_path('module', 'farm_quantity') . '/farm_quantity.css');
}
}