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
/
Copy pathfarm_grazing.paddocks.recovery.inc
291 lines (245 loc) · 7.87 KB
/
farm_grazing.paddocks.recovery.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
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
<?php
/**
* @file
* Grazing paddock recovery form.
*/
/**
* Paddock recovery page callback.
*/
function farm_grazing_plan_recovery_form($form, &$form_state, $plan_obj) {
// Load utility functions.
module_load_include('inc', 'farm_grazing', 'farm_grazing.utils');
// Set the page title.
drupal_set_title(t('Determine recovery periods'));
// Get the plan id from the plan object.
$plan = $plan_obj->id;
$wrapper = entity_metadata_wrapper('farm_plan', $plan);
// Check to see if this is a growing season plan.
$grazing_growing_season = $wrapper->field_grazing_growing_season->value();
// Load the start and end times for the plan.
$start_ts = $wrapper->field_farm_date_range->value->value();
$end_ts = $wrapper->field_farm_date_range->value2->value();
// Build a table header.
$header = array(
t('Month'),
t('Min. Recovery Days'),
t('Max. Recovery Days'),
);
// Get the start date month and year.
$start_date = new DateTime();
$start_date->setTimestamp($start_ts);
// Get the end date month and year.
$end_date = new DateTime();
$end_date->setTimestamp($end_ts);
// Get the DateInterval between them.
$diff = $start_date->diff($end_date);
// Get the number of paddocks in the plan.
$num_paddocks = farm_grazing_get_paddock_count_for_plan($plan);
// Treat the form as a tree so that nested vales are accessible in submit.
$form['#tree'] = TRUE;
// Start with an assumption that the form elements will not be disabled.
$disabled = FALSE;
// If this is a non-growing season plan...
if (!$grazing_growing_season) {
// Load the necessary variables from the plan.
$days_bulk_feeding = $wrapper->field_days_bulk_feeding->value();
$days_of_drought_reserve = $wrapper->field_days_of_drought_reserve->value();
$expected_rotations = $wrapper->field_expected_rotations->value();
// Calculate the expected days of non-growth.
$expected_days_non_growth = floor(($end_ts - $start_ts) / 3600 / 24);
// Calculate the recovery period for all paddocks.
$recovery_period = ($expected_days_non_growth + $days_of_drought_reserve
- $days_bulk_feeding) / $expected_rotations;
// Disable input fields.
$disabled = TRUE;
// Set the form description.
$form['text'] = array(
'#markup' => t('<p>Recovery period is computed for non-growing season plans.</p>'),
);
}
// Or, if this is a growing season plan...
else {
// Set the form description.
$form['text'] = array(
'#markup' => t('<p>Enter your minimum and maximum recovery periods (in days) for each planned month of the growing season plan.</p>'),
);
}
// Store the plan object in the form values.
$form['plan'] = array(
'#type' => 'value',
'#value' => $plan,
);
// If this is a growing season plan, get the plan recovery information.
// Otherwise, initialize an empty array.
if ($grazing_growing_season) {
$minmax = farm_grazing_get_plan_recovery($plan);
}
else {
$minmax = array();
}
// Create a form element for the fields, themed with
// farm_grazing_paddocks_recovery.
$form['months'] = array(
'#theme' => 'farm_grazing_paddocks_recovery',
'#header' => $header,
);
// Generate input fields for each month between the start and end dates.
$num_months = $diff->y * 12 + $diff->m;
$date = $start_date;
$minmin = 999999999;
$maxmax = -999999999;
for ($i = 0; $i <= $num_months; $i++) {
$key = $date->format('Ym');
if (isset($minmax[$key])) {
$min = $minmax[$key][0];
$max = $minmax[$key][1];
}
else {
if ($grazing_growing_season) {
$min = 0;
$max = 0;
}
else {
$min = $recovery_period;
$max = $recovery_period;
}
}
if ($min > 0 and $min < $minmin) {
$minmin = $min;
}
if ($max > 0 and $maxmax < $max) {
$maxmax = $max;
}
$form['months'][$key]['month'] = array(
'#markup' => $date->format('F Y'),
);
$form['months'][$key]['min'] = array(
'#type' => 'textfield',
'#title' => t('Min'),
'#title_display' => 'invisible',
'#default_value' => $min,
'#size' => 30,
'#maxlength' => 30,
'#disabled' => $disabled,
);
$form['months'][$key]['max'] = array(
'#type' => 'textfield',
'#title' => t('Max'),
'#title_display' => 'invisible',
'#default_value' => $max,
'#size' => 30,
'#maxlength' => 30,
'#disabled' => $disabled,
);
// increment the month
$date->modify('+1 month');
}
// get herd count from database or 1.0
$herd_cnt = farm_grazing_get_herd_count_for_plan($plan);
if ($herd_cnt == 0) {
$herd_cnt = 1;
}
$form['summary']['herd_cnt'] = array(
'#prefix' => '<p>' . t('Number of herds: '),
'#suffix' => t(' (Defaults to one herd if none are defined)') . '</p>',
'#markup' => $herd_cnt,
);
$form['summary']['pad_cnt'] = array(
'#prefix' => '<p>' . t('Number of paddocks: '),
'#markup' => $num_paddocks,
'#suffix' => '</p>',
);
if ($grazing_growing_season) {
$min_grazing = round($minmin / ($num_paddocks - $herd_cnt), 1);
$max_grazing = round($maxmax / ($num_paddocks - $herd_cnt), 1);
}
else {
$min_grazing = round($recovery_period / $num_paddocks, 1);
$max_grazing = $min_grazing;
}
$form['summary']['min_grazing'] = array(
'#prefix' => '<p>' . t('Est. Min. Grazing Period: '),
'#markup' => $min_grazing,
'#suffix' => '</p>',
);
$form['summary']['max_grazing'] = array(
'#prefix' => '<p>' . t('Est. Max. Grazing Period: '),
'#markup' => $max_grazing,
'#suffix' => '</p>',
);
if ($grazing_growing_season) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('farm_grazing_plan_recovery_form_submit'),
'#validate' => array('farm_grazing_plan_recovery_form_validate'),
);
}
// Return markup.
return $form;
}
/**
* farm_grazing_get_plan_recovery($plan)
*/
function farm_grazing_get_plan_recovery($plan) {
$minmax = array();
$results = db_query('select month, min_recovery, max_recovery from {farm_grazing_plan_recovery} where plan_id=:plan', array(':plan' => $plan));
foreach ($results as $record) {
$minmax[$record->month] = array($record->min_recovery, $record->max_recovery);
}
return $minmax;
}
/**
* Validate handler for farm_grazing_paddocks_recovery_form
*/
function farm_grazing_plan_recovery_form_validate($form, &$form_state) {
foreach ($form_state['values']['months'] as $id => $val) {
if (!is_numeric($val['min']) or $val['min'] < 0.0) {
form_set_error('min_recovery', t('Min. Recovery Days must be numeric >= 0'));
}
if (!is_numeric($val['max']) or $val['max'] < 0.0) {
form_set_error('max_recovery', t('Max. Recovery Days must be numeric >= 0'));
}
if ($val['min'] > $val['max']) {
form_set_error('max_recovery', t('Max. Recovery Days must be greater than Min. Recovery Days'));
}
}
}
/**
* Process the paddock recovery form on submit
*/
function farm_grazing_plan_recovery_form_submit($form, &$form_state) {
$plan = $form_state['values']['plan'];
foreach ($form_state['values']['months'] as $key => $val) {
db_merge('farm_grazing_plan_recovery')
->key(array(
'plan_id' => $plan,
'month' => $key,
))
->fields(array(
'min_recovery' => $val['min'],
'max_recovery' => $val['max'],
))
->execute();
}
}
/**
* Implements hook_theme for farm_grazing_paddocks_recovery
*/
function theme_farm_grazing_paddocks_recovery(&$vars) {
$form = $vars['form'];
$rows = array();
foreach (element_children($form) as $key) {
$rows[] = array(
render($form[$key]['month']),
render($form[$key]['min']),
render($form[$key]['max']),
);
}
$header = $form['#header'];
return theme('table', array(
'header' => $header,
'rows' => $rows,
'submit' => render($form['submit']),
));
}