This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
farm_grazing.admin.inc
231 lines (188 loc) · 6.7 KB
/
farm_grazing.admin.inc
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
<?php
/**
* @file
* Grazing configuration forms.
*/
/**
* Grazing types config form.
*/
function farm_grazing_config_animal_types_form($form, &$form_state) {
// Load the animal types.
$animal_types = farm_grazing_animal_types();
// Treat the form as a tree.
$form['#tree'] = TRUE;
// Help text.
$form['help'] = array(
'#type' => 'markup',
'#markup' => t('This form allows you to edit the animal type options used in the grazing planning process. The DMI (dry matter intake) factor is used to compute the amount of dry matter intake an animal requires (DMI = animal weight x DMI factor / 100).'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
// Save the array of animal types for comparison in validate/submit.
$form['animal_types'] = array(
'#type' => 'value',
'#value' => $animal_types,
);
// Iterate through the animal types and add fields for each.
foreach ($animal_types as $id => $type) {
// Animal type ID (hidden value).
$form['types'][$id]['type_id'] = array(
'#type' => 'value',
'#value' => $type['type_id'],
);
// Animal type name.
$form['types'][$id]['name'] = array(
'#title' => t('Animal type name'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#default_value' => $type['name'],
'#required' => TRUE,
'#maxlength' => 255,
);
// DMI factor.
$form['types'][$id]['dmi_factor'] = array(
'#title' => t('DMI factor'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#default_value' => $type['dmi_factor'],
'#required' => TRUE,
);
// Remove checkbox.
$form['types'][$id]['remove'] = array(
'#type' => 'checkbox',
);
}
// Fieldset for adding a new type.
$form['add'] = array(
'#type' => 'fieldset',
'#title' => t('Add animal type'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['add']['name'] = array(
'#type' => 'textfield',
'#title' => t('Animal type'),
'#maxlength' => 255,
);
$form['add']['dmi_factor'] = array(
'#type' => 'textfield',
'#title' => t('DMI factor'),
'#description' => t('What is the dry matter intake factor for this animal type?'),
'#element_validate' => array('element_validate_number'),
);
// Submit button.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Validate handler for the animal type configuration form.
*/
function farm_grazing_config_animal_types_form_validate($form, &$form_state) {
// If we are adding a type, validate the fields.
if (!empty($form_state['values']['add']['name']) || !empty($form_state['values']['add']['dmi_factor'])) {
// Make sure both a name and DMI are set.
if (empty($form_state['values']['add']['name'])) {
form_set_error('add][name', t('Animal type name is required.'));
}
elseif (empty($form_state['values']['add']['dmi_factor'])) {
form_set_error('add][dmi_factor', t('DMI factor is required.'));
}
// Make sure name is unique.
if (!empty($form_state['values']['add']['name'])) {
$name = $form_state['values']['add']['name'];
$exists = db_query('SELECT COUNT(*) FROM {farm_grazing_animal_types} WHERE name = :name', array(':name' => $name))->fetchField();
if (!empty($exists)) {
form_set_error('add][name', t('An animal type named "%name" already exists.', array('%name' => $name)));
}
}
}
// Iterate through the animal types.
foreach ($form_state['values']['types'] as $type_id => $type) {
// If the animal type is being removed, check to see if it is in use.
if (!empty($type['remove'])) {
// Load usage information from the {farm_asset_property} table.
$usage = db_query("SELECT COUNT(id) FROM {farm_asset_property} WHERE name = 'farm_grazing_animal_type' AND value = :type_id", array(':type_id' => $type_id))->fetchField();
// Don't allow deleting types that are in use.
if (!empty($usage)) {
form_set_error('types][' . $type_id . '][delete', t('The animal type "%animal_type" cannot be deleted because it is in use on animals already.', array('%animal_type' => $form_state['values']['animal_types'][$type_id]['name'])));
}
}
}
}
/**
* Submit handler for the animal type configuration form.
*/
function farm_grazing_config_animal_types_form_submit($form, &$form_state) {
// Get the saved animal types.
$animal_types = $form_state['values']['animal_types'];
// Iterate through the types.
foreach ($form_state['values']['types'] as $type_id => $type) {
// Compare the name and dmi_factor to the saved values to see if we should
// update it.
$update = FALSE;
if (($type['name'] != $animal_types[$type_id]['name']) || ($type['dmi_factor'] != $animal_types[$type_id]['dmi_factor'])) {
$update = TRUE;
}
// If the type is marked for removal, delete it.
if (!empty($type['remove'])) {
// Delete the record.
db_query('DELETE FROM {farm_grazing_animal_types} WHERE type_id = :type_id', array(':type_id' => $type_id));
// Do not update it, because it doesn't exist anymore!
$update = FALSE;
}
// If we should update, then do so.
if (!empty($update)) {
drupal_write_record('farm_grazing_animal_types', $type, array('type_id'));
}
}
// If a new type is being added, create it.
if (!empty($form_state['values']['add']['name']) && !empty($form_state['values']['add']['dmi_factor'])) {
$record = array(
'name' => $form_state['values']['add']['name'],
'dmi_factor' => $form_state['values']['add']['dmi_factor'],
);
drupal_write_record('farm_grazing_animal_types', $record);
}
// Display a generic message.
drupal_set_message('Animal types were updated.');
}
/**
* Theme function for the animal type configuration form.
*/
function theme_farm_grazing_config_animal_types_form(&$vars) {
// Start an empty output string.
$output = '';
// Get the form array.
$form = &$vars['form'];
// Render the help text.
$output .= drupal_render($form['help']);
// Assemble the table rows.
$rows = array();
foreach (element_children($form['types']) as $type_id) {
// Get the form item.
$item = &$form['types'][$type_id];
// Assemble the row.
$rows[] = array(
drupal_render($item['name']),
drupal_render($item['dmi_factor']),
drupal_render($item['remove']),
);
}
// Define the table header.
$header = array(
t('Animal type'),
t('DMI factor'),
t('Remove'),
);
// Render the table into the the output.
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
));
// Render any remaining form elements.
$output .= drupal_render_children($form);
return $output;
}