forked from farmOS/farm_eggs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarm_eggs.farm_quick.inc
222 lines (194 loc) · 6.18 KB
/
farm_eggs.farm_quick.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
<?php
/**
* @file
* Farm eggs quick forms.
*/
/**
* Implements hook_farm_quick_forms().
*/
function farm_eggs_farm_quick_forms() {
return array(
'eggs' => array(
'label' => t('Eggs'),
'permission' => 'create farm_harvest log entities',
'form' => 'farm_eggs_form',
'file' => 'farm_eggs.farm_quick.inc',
),
);
}
// defaults the form value to zero if empty, or validates value as integer
function validate_and_default_zero($element, &$form_state) {
$value = $element['#value'];
if ($value == ''){
form_set_value($element, 0, $form_state);
}
elseif (!is_numeric($value) || intval($value) != $value) {
form_error($element, t('%name must be an integer.', array(
'%name' => $element['#title'],
)));
}
}
/**
* Form for adding egg harvest logs.
*/
function farm_eggs_form($form, &$form_state) {
// Wrapper fieldset.
$form['eggs'] = array(
'#type' => 'fieldset',
'#title' => t('Record an egg harvest'),
'#description' => t('Use this form to record an egg harvest. A harvest log will be created with standard details filled in.'),
);
// Load active assets with the "farm_eggs" asset property.
$asset_options = array();
$result = db_query("SELECT a.id FROM {farm_asset_property} p LEFT JOIN {farm_asset} a ON p.id = a.id WHERE p.name = 'farm_eggs' AND a.archived = 0");
foreach ($result as $row) {
if (!empty($row->id)) {
// Load the asset.
$asset = farm_asset_load($row->id);
// If the asset didn't load, skip it.
if (empty($asset)) {
continue;
}
// Add an option (linked to asset).
$entity_label = entity_label('farm_asset', $asset);
$entity_uri = entity_uri('farm_asset', $asset);
$asset_options[$asset->id] = l($entity_label, $entity_uri['path']);
}
}
// If there are asset options, add checkboxes.
if (!empty($asset_options)) {
$form['eggs']['assets'] = array(
'#type' => 'radios',
'#title' => t('Group/animal'),
'#description' => t('Select the group/animal that these eggs came from. To add groups/animals to this list, edit their record and check the "Produces eggs" checkbox.'),
'#options' => $asset_options,
'#required' => TRUE,
);
// If there is only one option, select it by default.
if (count($asset_options) == 1) {
$form['eggs']['assets']['#default_value'] = array_keys($asset_options)[0];
}
}
// Otherwise, show some text about adding groups/animals.
else {
$form['eggs']['assets'] = array(
'#type' => 'markup',
'#markup' => t('If you would like to associate this egg harvest log with a group/animal asset, edit their record and check the "Produces eggs" checkbox. Then you will be able to select them here.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
//return $form;
}
// Quantity S.
$form['eggs']['quantityS'] = array(
'#type' => 'textfield',
'#title' => t('Quantity ( class A size S )'),
'#required' => FALSE,
'#element_validate' => array('validate_and_default_zero'),
);
// Quantity M.
$form['eggs']['quantityM'] = array(
'#type' => 'textfield',
'#title' => t('Quantity ( class A size M )'),
'#required' => FALSE,
'#element_validate' => array('validate_and_default_zero'),
);
// Quantity L.
$form['eggs']['quantityL'] = array(
'#type' => 'textfield',
'#title' => t('Quantity ( class A size L )'),
'#required' => FALSE,
'#element_validate' => array('validate_and_default_zero'),
);
// Quantity XL.
$form['eggs']['quantityXL'] = array(
'#type' => 'textfield',
'#title' => t('Quantity ( class A size XL )'),
'#required' => FALSE,
'#element_validate' => array('validate_and_default_zero'),
);
// Quantity class B.
$form['eggs']['quantityB'] = array(
'#type' => 'textfield',
'#title' => t('Quantity ( class B )'),
'#required' => FALSE,
'#element_validate' => array('validate_and_default_zero'),
);
// Quantity class C.
$form['eggs']['quantityC'] = array(
'#type' => 'textfield',
'#title' => t('Quantity ( class C )'),
'#required' => FALSE,
'#element_validate' => array('validate_and_default_zero'),
);
// Submit button.
$form['eggs']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save log'),
);
// Return the form.
return $form;
}
/**
* Submit function for eggs quick form.
*/
function farm_eggs_form_submit($form, &$form_state) {
// The log type will be a harvest.
$log_type = 'farm_harvest';
// radio buttons only provide one asset.
$assets = array(
farm_asset_load($form_state['values']['assets'])
);
// Assemble an array of measurements.
$measurements = array(
array(
'measure' => 'count',
'value' => $form_state['values']['quantityS'],
'units' => t('egg(s)'),
'label' => 'S',
),
array(
'measure' => 'count',
'value' => $form_state['values']['quantityM'],
'units' => t('egg(s)'),
'label' => 'M',
),
array(
'measure' => 'count',
'value' => $form_state['values']['quantityL'],
'units' => t('egg(s)'),
'label' => 'L',
),
array(
'measure' => 'count',
'value' => $form_state['values']['quantityXL'],
'units' => t('egg(s)'),
'label' => 'XL',
),
array(
'measure' => 'count',
'value' => $form_state['values']['quantityB'],
'units' => t('egg(s)'),
'label' => 'ClassB',
),
array(
'measure' => 'count',
'value' => $form_state['values']['quantityC'],
'units' => t('egg(s)'),
'label' => 'ClassC',
),
);
// calculate total quantity
$totalqty = 0;
foreach ( $measurements as $measurement) {
$totalqty += $measurement['value'];
}
// setting category
$categories = array( "Eggs" );
// Set log name.
$log_name = t('Collected @qty egg(s) from the @flk', array('@qty' => $totalqty, '@flk' => $assets[0]->name));
// Create a new farm quantity log.
$log = farm_quantity_log_create($log_type, $log_name, REQUEST_TIME, TRUE, $assets, $measurements, '', $categories);
// Link the log to the quick form.
farm_quick_entity_link('farm_eggs_form', 'log', $log);
}