-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathom_agman.module
1162 lines (1076 loc) · 40.9 KB
/
om_agman.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// om agplant
/**
* Implements hook_ctools_plugin_directory().
*/
function om_agman_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/' . $plugin_type;
}
if ($owner == 'om' && $plugin_type == 'om_components') {
return 'plugins/' . $plugin_type;
}
if ($owner == 'dh' && $plugin_type == 'dh_variables') {
return 'plugins/' . $plugin_type;
}
if ($owner == 'dh' && $plugin_type == 'dh_components') {
return 'plugins/' . $plugin_type;
}
}
/**
* Implements hook_views_api().
*
* Used for enabling using templates inside my module
*/
function om_agman_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'om_agman'),
'template path' => drupal_get_path('module', 'om_agman') . '/templates',
);
}
/*
function om_agman_theme($existing, $type, $theme, $path) {
return array (
'views-view-table--ipm-planning-grid' => array (
'variables' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
'template' => 'views-view-table--ipm-planning-grid' ,
'base hook' => 'views_view_table',
'path' => drupal_get_path('module', 'om_agman'),
),
);
}
*/
function om_agman_entity_ts_event_types_alter(&$event_types) {
// insert ts events for AR fields/properties ()such as startdate, enddate, dh_link_submittal_feature)
// insert ts events for those linked via adminreg
// system erefs like dh_link_submittal_feature
$event_types['dh_adminreg_feature'] += array(
'agman_event_dh_link_submittal_feature' => array(
'bundle' => 'agchem_app', // FALSE means it happens for all
'entity_type' => 'dh_feature',
'multiplicity' => 'tsvalue_singular', // set adminid as tsvalue, make it unique
'featureid' => array('value_src_type'=> 'eref_targetid', 'value' => 'dh_link_feature_submittal'),
'map' => array(
// object permits sending an integer OR varkey to varid and controller figures it out
'varid' => array('value_src_type'=> 'constant', 'value' => 'event_dh_link_submittal_feature'),
'tstime' => array('value_src_type'=> 'property', 'value' => 'startdate'),
'tsendtime' => array('value_src_type'=> 'property', 'value' => 'enddate'),
'tsvalue' => array('value_src_type' => 'property', 'value' => 'adminid'),
'tscode' => array('value_src_type' => 'constant', 'value' => 'dh_adminreg_feature'),
),
),
'agman_event_adminreg_feature' => array(
'bundle' => 'agchem_app', // FALSE means it happens for all
'entity_type' => 'dh_adminreg_feature',
'multiplicity' => 'singular',
'featureid' => array('value_src_type'=> 'property', 'value' => 'adminid'),
'map' => array(
// object permits sending an integer OR varkey to varid and controller figures it out
'varid' => array('value_src_type'=> 'constant', 'value' => 'agchem_application_event'),
'tstime' => array('value_src_type'=> 'property', 'value' => 'startdate'),
'tsendtime' => array('value_src_type'=> 'property', 'value' => 'enddate'),
'modified' => array('value_src_type'=> 'property', 'value' => 'modified'),
'tsvalue' => array('value_src_type' => 'constant', 'value' => NULL),
'tscode' => array('value_src_type' => 'property', 'value' => 'ftype'),
),
),
);
}
function om_agman_get_block_farmid($blockid) {
return dh_getMpFacilityHydroId($blockid);
}
function om_agman_get_farm_blocks($farmid) {
return array();
}
function om_agman_form_block_select(&$element, $farmid = FALSE) {
$deftable = array(); // need to translate normal select to table select syntax
$def = $element['und']['#default_value'];
$src_format = $element['und']['#type'];
//dpm($element,'block select element');
//dpm($def,'block select #default_value');
foreach ($def as $selkey => $selopt ) {
$deftable[$selopt] = 1;
if (!$farmid or empty($farmid)) {
// if this is a new event, caller must specify farmid
// otherwise try to guess from one of the existing options,
// if src_format is 'select' we expect hydroid to be the value
// otherwise this is already formatted as a check list so we expect hydroid to be the key
$eid = ($src_format == 'select') ? $selopt : $selkey;
$farmid = om_agman_get_block_farmid($eid);
}
}
//dpm($farmid,"farm id");
$element['und']['#empty'] = t('None Selected.');
$element['und']['#type'] = 'tableselect';
$element['und']['#default_value'] = $deftable;
$element['und']['#header'] = array(
'label' => 'Block',
);
$opts = array();
// get vineyard blocks
if ($farmid) {
$blockids = dh_get_facility_mps($farmid, 'landunit');
if (count($blockids) > 0) {
$q = db_query("select hydroid, name from {dh_feature} where hydroid IN (:blocklist) order by name", array(':blocklist' => $blockids));
foreach ($q as $block) {
$id = $block->hydroid;
$opts[$id]['label'] .= $block->name;
}
}
}
$farm = entity_load_single('dh_feature', $farmid);
$element['und']['#prefix'] = "<b>" . t('Blocks to Spray at ') . $farm->name . "</b>";
// dpm($element,'el');
// END - tableselect
$element['und']['#options'] = $opts;
return $element;
}
function om_agman_form_material_select2($entity) {
$criteria = array(
'farm_id' => array('value' => $entity->farmid)
);
$checkboxes = om_agman_material_search_opts('current', $criteria);
//dpm($entity->farmid,"farm id");
$element = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'field-type-entityreference',
'field-name-field-link-to-registered-agchem',
'field-widget-options-buttons'
),
),
'#tree' => TRUE,
'#language' => 'und',
'#access' => TRUE
);
$element['und'] = $checkboxes;
$element['und']['#entity'] = $entity;
$element['und']['#entity_type'] = $entity->entityType();
$element['und']['#bundle'] = $entity->bundle;
$element['und']['#field_name'] = 'dh_link_feature_submittal';
$element['und']['#language'] = 'und';
$element['und']['#multiple'] = TRUE;
$element['und']['#required'] = FALSE;
$element['und']['#delta'] = 0;
$element['und']['#empty'] = t('None Selected.');
$element['und']['#type'] = 'tableselect';
$element['und']['#value_key'] = 'target_id';
$element['und']['#columns'] = array('target_id');
$element['und']['#after_build'] = array(
0 => 'field_form_element_after_build'
);
$element['und']['#element_validate'] = array(
0 => 'options_field_widget_validate'
);
// set the predefined options or NONE if this is a new event
$deftable = array();
$event_chems = $entity->field_link_to_registered_agchem['und'];
$event_chems = array_column($event_chems, 'target_id');
foreach ($event_chems as $selopt ) {
$deftable[$selopt] = 1;
}
$element['und']['#default_value'] = $deftable;
return $element;
}
function om_agman_form_block_select2($entity) {
// selected options are in event_blocks
$event_blocks = $entity->dh_link_feature_submittal['und'];
$event_blocks = array_column($event_blocks, 'target_id');
$element = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
0 => 'field-type-entityreference',
1 => 'field-name-dh-link-feature-submittal',
2 => 'field-widget-entityreference-autocomplete'
),
),
'#weight' => 2,
'#tree' => TRUE,
'#language' => 'und',
'und' => array(),
'#access' => TRUE
);
//dpm($entity,'entity');
//dpm($entity->farmid,"farm id");
$element['und']['#entity'] = $entity;
$element['und']['#entity_type'] = $entity->entityType();
$element['und']['#bundle'] = $entity->bundle;
$element['und']['#field_name'] = 'dh_link_feature_submittal';
$element['und']['#language'] = 'und';
$element['und']['#multiple'] = TRUE;
$element['und']['#required'] = FALSE;
$element['und']['#delta'] = 0;
$element['und']['#empty'] = t('None Selected.');
$element['und']['#type'] = 'tableselect';
$element['und']['#value_key'] = 'target_id';
$element['und']['#columns'] = array('target_id');
$element['und']['#after_build'] = array(
0 => 'field_form_element_after_build'
);
$element['und']['#element_validate'] = array(
0 => 'options_field_widget_validate'
);
$element['und']['#header'] = array(
'label' => 'Block',
);
// Now populate
$deftable = array();
//dpm($element,'block select element');
//dpm($def,'block select #default_value');
foreach ($event_blocks as $selopt ) {
$deftable[$selopt] = 1;
}
$element['und']['#default_value'] = $deftable;
$opts = array();
// get vineyard blocks for all options
$farm = entity_load_single('dh_feature', $entity->farmid);
$element['und']['#prefix'] = "<b>" . t('Blocks to Spray at ') . $farm->name . "</b>";
if ($entity->farmid) {
$all_blockids = dh_get_facility_mps($entity->farmid, 'landunit');
if (count($all_blockids) > 0) {
$q = db_query("select hydroid, name from {dh_feature} where hydroid IN (:blocklist) order by name", array(':blocklist' => $all_blockids));
foreach ($q as $block) {
$id = $block->hydroid;
$opts[$id]['label'] .= $block->name;
}
}
}
// dpm($element,'el');
// END - tableselect
$element['und']['#options'] = $opts;
return $element;
}
// custom field formatters
/**
* Implements hook_field_formatter_info().
*/
function om_agman_field_formatter_info() {
return array(
'entityreference_quick_action' => array(
'label' => t('Add an action link to an entityreference field.'),
// The important bit...
'field types' => array('entityreference'),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function om_agman_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
// @todo: we may simply opt to add this to the standard field link handler as a patch since it
// would be useful and would not require a special handler
if ($display['type'] == 'entityreference_quick_action') {
$element['confirm'] = array(
'#title' => t('Add confirm pop-up'),
'#type' => 'checkbox',
'#default_value' => $settings['entityreference_quick_action'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
* @todo: do we need this?
function om_agman_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
if ($display['type'] == 'entityreference_delete') {
$summary[] = $settings['link'] ? t('Link to the referenced entity') : t('No link');
}
if ($display['type'] == 'entityreference_entity_view') {
$entity_info = entity_get_info($field['settings']['target_type']);
$view_mode_label = $settings['view_mode'] == 'default' ? t('Default') : $settings['view_mode'];
if (isset($entity_info['view modes'][$settings['view_mode']]['label'])) {
$view_mode_label = $entity_info['view modes'][$settings['view_mode']]['label'];
}
$summary[] = t('Rendered as @mode', array('@mode' => $view_mode_label));
$summary[] = !empty($settings['links']) ? t('Display links') : t('Do not display links');
}
return implode('<br />', $summary);
}
*/
/**
* Implements hook_field_formatter_view().
*/
function om_agman_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$result = array();
$settings = $display['settings'];
// Rebuild the items list to contain only those with access.
foreach ($items as $key => $item) {
if (empty($item['access'])) {
unset($items[$key]);
}
}
switch ($display['type']) {
case 'entityreference_quick_action':
$args = arg(); // @todo: should get these from the view so that we can take advantage of any set on the view itself - probably can access from the display object
$base = implode("/", $args);
$query = drupal_get_query_parameters();
$link = l("Bloom x", $base, array(
'attributes' => array(
'onclick' => 'return confirm( "Are you sure you want to delete?" ); '
),
'query' => $query
)
);
$handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
foreach ($items as $delta => $item) {
$label = $handler->getLabel($item['entity']);
// If the link is to be displayed and the entity has a uri, display a link.
// Note the assignment ($url = ) here is intended to be an assignment.
if ($display['settings']['link'] && ($uri = entity_uri($field['settings']['target_type'], $item['entity']))) {
$result[$delta] = array('#markup' => l($label, $uri['path'], $uri['options']));
}
else {
$result[$delta] = array('#markup' => check_plain($label));
}
}
break;
}
return $result;
}
/**
* *****************************************************************
* Modal Form Support Below
* *****************************************************************
*/
/**
* Implements hook_menu().
*/
function om_agman_menu() {
$items = array();
// this creates a page that will house the link
// I *think* this might be optional
$items['om_agman/page'] = array(
'page callback' => 'om_agman_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
// I think this is the definition needed to actually respond to the ajax request
// %ctools_js is a variable, so actually, this will match ANY link in the form:
// om_agman/*
$items['om_agman/%ctools_js'] = array(
'page callback' => 'om_agman_callback',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['om_agman/active_ingredient'] = array(
'page callback' => 'om_active_ingredient_ac',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['om_agman/vitis_varieties'] = array(
'page callback' => 'om_vitis_varieties',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* autocomplete helper
* $string = string for search
*/
function om_vitis_varieties($string) {
watchdog('dh',"Called om_vitis_varieties($string)");
$matches = array();
$aivarid = dh_varkey2varid('agman_plant', TRUE);
/*
$result = db_select('dh_properties', 'c')
->fields('c', array('propcode'))
->condition('propcode', '%' . db_like($string) . '%', 'ILIKE')
->condition('varid', $aivarid, '=')
->execute();
// save the query to matches
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
*/
// @todo - name should be swapped for code?
//$q = " select propcode as key, propcode as val from dh_properties ";
$q = " select propname as key, propname as val from dh_properties ";
$q .= " where varid = $aivarid ";
$q .= " and propname ilike '%$string%' ";
$q .= " group by propname ";
$q .= " order by propname ";
//error_log($q);
$result = db_query($q);
$matches = $result->fetchAllKeyed();
// Return the result to the form in json
drupal_json_output($matches);
}
/**
* autocomplete helper
* $string = string for search
*/
function om_active_ingredient_ac($string) {
$matches = array();
$aivarid = dh_varkey2varid('agchem_ai', TRUE);
/*
$result = db_select('dh_properties', 'c')
->fields('c', array('propcode'))
->condition('propcode', '%' . db_like($string) . '%', 'ILIKE')
->condition('varid', $aivarid, '=')
->execute();
// save the query to matches
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
*/
$q = " select propcode as key, propcode as val from dh_properties ";
$q .= " where varid = $aivarid ";
$q .= " and propcode ilike '%$string%' ";
$q .= " group by propcode ";
$q .= " order by propcode ";
//error_log($q);
$result = db_query($q);
$matches = $result->fetchAllKeyed();
// Return the result to the form in json
drupal_json_output($matches);
}
/**
* Helper function to make a link.
* This IS NOT NEEDED, just part of the demo page, but we do this in views with no trouble
*/
function _om_agman_make_link($link_text = '') {
// Set a default value if no text in supplied.
if (empty($link_text)) {
$link_text = 'Magical Modal';
}
return '<div id="magical-modal-link">' . l($link_text, 'om_agman/nojs', array('attributes' => array('class' => 'ctools-use-modal'))) . '</div>';
}
function om_agman_convertRateUnitsAmount($rate_units) {
// final units
$ra_conv = array(
'oz/acre' => 'oz',
'oz/gal' => 'oz',
'floz/acre' => 'floz',
'lbs/acre' => 'lbs',
'gals/acre' => 'gals',
'pt/acre' => 'pt',
'pt/gal' => 'pt',
'pt/cgal' => 'pt',
'qt/acre' => 'qt',
);
return $ra_conv[$rate_units];
}
function om_agman_get_block_phi($blockid, $varkey, $startdate, $enddate, $debug = FALSE) {
// ex: agchem_application_event
// find the event whose attached phi date prop is the highest
// grab all ts events and their PHI props linked to this block and join to the max enddate of the same list, sort by tsendtime and take the earliest one
$varid = dh_varkey2varid($varkey, TRUE);
$pvarid = dh_varkey2varid('agchem_phi', TRUE);
$q = " select tid from {dh_timeseries} as ts ";
$q .= " left outer join {dh_properties} as phprop ";
$q .= " on ( ";
$q .= " phprop.entity_type = 'dh_timeseries' ";
$q .= " and ts.tid = phprop.featureid ";
$q .= " ) ";
$q .= " left outer join ( ";
$q .= " select max(phprop.enddate) as phi_date from {dh_timeseries} as ts ";
$q .= " left outer join {dh_properties} as phprop ";
$q .= " on ( ";
$q .= " phprop.entity_type = 'dh_timeseries' ";
$q .= " and ts.tid = phprop.featureid ";
$q .= " ) ";
$q .= " left outer join dh_adminreg_feature as arf ";
$q .= " on ( ";
$q .= " ts.featureid = arf.adminid ";
$q .= " and ts.entity_type = 'dh_adminreg_feature' ";
$q .= " ) ";
$q .= " where ts.varid = $varid ";
$q .= " and arf.fstatus <> 'post_harvest' ";
$q .= " and ts.featureid in (select entity_id from field_data_dh_link_feature_submittal where dh_link_feature_submittal_target_id = $blockid ) ";
$q .= " and ts.tstime >= " . dh_handletimestamp($startdate);
$q .= " and ts.tstime <= " . dh_handletimestamp($enddate);
$q .= " ) as max_phi ";
$q .= " on (1 = 1) ";
$q .= " left outer join dh_adminreg_feature as arf ";
$q .= " on (";
$q .= " ts.featureid = arf.adminid";
$q .= " and arf.fstatus <> 'post_harvest' ";
$q .= " ) ";
$q .= " where ts.varid = $varid ";
$q .= " and ts.featureid in (select entity_id from field_data_dh_link_feature_submittal where dh_link_feature_submittal_target_id = $blockid ) ";
$q .= " and ts.entity_type = 'dh_adminreg_feature' ";
$q .= " and tstime >= " . dh_handletimestamp($startdate);
$q .= " and tstime <= " . dh_handletimestamp($enddate);
$q .= " and phprop.enddate = max_phi.phi_date ";
if ($debug) {
dsm($q);
//error_log("$q");
}
$result = db_query($q);
foreach ($result as $record) {
// For whatever reason we need to use this instead of shifting a single result since $result is not an array but works with foreach?
$dh_ts = entity_load_single('dh_timeseries', $record->tid);
return $dh_ts;
}
return FALSE;
}
/**
* An example page.
*/
function om_agman_page() {
// this is our page that contains the link to ajax popup
// is this code needed if we do a view?
// Load the modal library and add the modal javascript.
ctools_include('modal');
ctools_modal_add_js();
return om_agman_application_materials();
//return _om_agman_make_link('Magical modal');
}
/**
* Ajax menu callback.
*/
function om_agman_callback($ajax) {
if ($ajax) {
ctools_include('ajax');
ctools_include('modal');
$form_state = array(
'ajax' => TRUE,
'title' => t('om_agman Modal Form'),
);
// Use ctools to generate ajax instructions for the browser to create
// a form in a modal popup.
$output = ctools_modal_form_wrapper('om_agman_materials_form', $form_state);
//$output = ctools_modal_form_wrapper('om_agman_form', $form_state);
// If the form has been submitted, there may be additional instructions
// such as dismissing the modal popup.
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
// Return the ajax instructions to the browser via ajax_render().
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('om_agman_materials_form');
//return drupal_get_form('om_agman_form');
}
}
/**
* Drupal form to be put in a modal.
*/
function om_agman_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Link text'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Drupal form submit handler.
*/
function om_agman_form_submit(&$form, &$form_state) {
// Generate the new link using the submitted text value.
$link = _om_agman_make_link($form_state['values']['new_link_text']);
// Tell the browser to close the modal.
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
// Tell the browser to replace the old link with the new one.
$form_state['ajax_commands'][] = ajax_command_replace('#magical-modal-link', $link);
}
function om_agman_agchem_materials_select($hydroid, &$form, $fname, $viewname, $displayname, $id_field) {
$deftable = array(); // need to translate normal select to table select syntax
$def = $form[$fname]['und']['#default_value'];
foreach ($def as $selopt) {
$deftable[$selopt] = 1;
}
$dh_farm_feature = entity_load_single('dh_feature', $hydroid);
//dpm($dh_farm_feature->field_link_agchem_material,'agchems');
$chemids = array();
foreach ($dh_farm_feature->field_link_agchem_material['und'] as $link) {
$chemids[] = $link['target_id'];
}
$form[$fname]['und']['#empty'] = t('No content available.');
$form[$fname]['und']['#type'] = 'tableselect';
$form[$fname]['und']['#required'] = TRUE;
$form[$fname]['und']['#default_value'] = $deftable;
$form[$fname]['und']['#header'] = array(
'label' => 'Material',
'REI'=>'REI',
'PHI' => 'PHI',
'FRAC' => 'FRAC',
'PM' => 'PM',
'DM' => 'DM',
'Ph' => 'Ph',
'BR' => 'BR',
'Bot' => 'Bot'
);
// not really icons yet but soon neeedto sort out image paths that are not in themes
// see here: https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_get_path/7.x
$icons = array(
'fungicide' => '(f)',
'herbicide' => '(h)',
'insecticide' => '(i)',
'pesticide' => '(p)',
);
$opts = array();
//dpm($form,'form');
foreach($form[$fname]['und']['#options'] as $id => $result) {
// format the material
if (!in_array($id, $chemids)) {
unset($form[$fname]['und']['#options'][$id]);
continue;
}
$material = entity_load_single('dh_adminreg_feature', $id);
$criteria = array();
$varkeys = array(
'REI'=>'agchem_rei',
'PHI' => 'agchem_phi',
'FRAC' => 'agchem_frac',
'PM' => 'org_powdery_mildew',
'DM' => 'org_downy_mildew',
'Ph' => 'org_phomopsis',
'BR' => 'org_black_rot',
'Bot' => 'org_botrytis'
);
$valkeys = array(
'REI'=>'agchem_rei',
'PHI' => 'agchem_phi',
);
$codekeys = array(
'PM' => 'org_powdery_mildew',
'DM' => 'org_downy_mildew',
'Ph' => 'org_phomopsis',
'BR' => 'org_black_rot',
'Bot' => 'org_botrytis'
);
$vars = dh_vardef_varselect_options(array("varkey in ('" . implode("', '", array_values($varkeys)) . "')"));
$criteria[] = array(
'name' => 'varid',
'op' => 'IN',
'value' => array_keys($vars),
);
$material->loadComponents($criteria);
// BEGIN - tableselect
$opts[$id] = array(
'label' => $material->name . ' ' . $icons[$material->ftype],
);
//dpm($material->dh_properties, 'props');
//dpm($material->prop_varkey_map, 'prop map');
foreach ($valkeys as $label => $key) {
if (isset($material->dh_properties[current($material->prop_varkey_map[$key])])) {
$opts[$id][$label] = $material->dh_properties[current($material->prop_varkey_map[$key])]->propvalue;
} else {
$opts[$id][$label] = 'und';
}
}
foreach ($codekeys as $label => $key) {
if (isset($material->dh_properties[current($material->prop_varkey_map[$key])])) {
$opts[$id][$label] = $material->dh_properties[current($material->prop_varkey_map[$key])]->propcode;
} else {
$opts[$id][$label] = 'und';
}
}
$delim = '';
foreach ($material->prop_varkey_map['agchem_frac'] as $propk) {
//dpm($material->dh_properties[$propk], "material");
$opts[$id]['FRAC'] .= $delim . $material->dh_properties[$propk]->propcode;
$delim = ', ';
}
// END - tableselect
}
$form[$fname]['und']['#options'] = $opts;
//dpm($form[$fname],'select');
}
function om_agman_materials_form($form, &$form_state, $dh_adminreg_feature = null, $op = 'edit') {
if ($dh_adminreg_feature === NULL) {
$props = array(
'bundle' => 'agchem_app',
);
$dh_adminreg_feature = entity_create('dh_adminreg_feature', $props);
$form_state['entity_type'] = 'dh_adminreg_feature';
}
if ($op == 'clone') {
$dh_adminreg_feature->name .= ' (cloned)';
$dh_adminreg_feature->bundle = '';
}
if ($dh_adminreg_feature->adminid > 0) {
$form['adminid'] = array(
'#type' => 'hidden',
'#default_value' => $dh_adminreg_feature->adminid,
);
}
$form['name'] = array(
'#title' => t('Plan Name'),
'#type' => 'textfield',
'#default_value' => empty($dh_adminreg_feature->name) ? 'Planned Spray' : $dh_adminreg_feature->name,
'#description' => t('Name'),
'#required' => TRUE,
'#size' => 30,
);
$form['ftype'] = array(
'#title' => t('FType'),
'#type' => 'hidden',
'#default_value' => 'agchem_app_plan',
'#description' => t('FType'),
'#required' => TRUE,
'#size' => 30,
);
if (trim($dh_adminreg_feature->hydrocode) == '') {
$dh_adminreg_feature->hydrocode = str_replace(' ', '_', strtolower($dh_adminreg_feature->name ));
}
$form['admincode'] = array(
'#title' => t('AdminCode'),
'#type' => 'hidden',
'#default_value' => '',
//'#default_value' => $dh_adminreg_feature->hydrocode,
'#description' => t('The unique identifier used by the originating agency of this dH Feature type.'),
'#required' => FALSE,
'#size' => 30,
);
$form['fstatus'] = array(
'#title' => t('Status'),
'#type' => 'select',
'#options' => array(
'planned' => t('Planned'),
'completed' => t('Completed'),
'cancelled' => t('Cancelled'),
),
'#default_value' => empty($dh_adminreg_feature->fstatus) ? 'planned' : $dh_adminreg_feature->fstatus,
'#required' => TRUE,
'#multiple' => FALSE,
);
$date_format = 'Y-m-d';
// should have code in here to guess based on the phase/or passed in from the URL
$form['startdate'] = array(
'#title' => t('Application Date'),
'#description' => t('Planned date for this spray.'),
'#required' => TRUE,
'#default_value' => empty($dh_adminreg_feature->startdate) ? $dh_adminreg_feature->startdate : date($date_format,$dh_adminreg_feature->startdate),
'#date_format' => $date_format,
'#type' => 'date_select',
'#date_year_range' => '-5:+5',
);
// Machine-readable type name.
$form['bundle'] = array(
'#type' => 'hidden',
'#default_value' => isset($dh_adminreg_feature->bundle) ? $dh_adminreg_feature->bundle : '',
'#maxlength' => 32,
'#attributes' => array('disabled' => 'disabled'),
'#machine_name' => array(
'exists' => 'dh_adminreg_feature_get_types',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'),
);
field_attach_form('dh_adminreg_feature', $dh_adminreg_feature, $form, $form_state);
// now, adjust the block ids form to show only those for this vineyard.
$dh_adminreg_feature->farmid = property_exists($dh_adminreg_feature, 'farmid') ? $dh_adminreg_feature->farmid : FALSE;
//dpm($dh_adminreg_feature,'feature');
om_agman_form_block_select($form['dh_link_feature_submittal'], $dh_adminreg_feature->farmid);
$hiddens = array(
'dh_link_admin_submittal_pr',
//'dh_link_feature_submittal',
'dh_link_admin_timeseries',
);
foreach ($hiddens as $hidethis) {
if (isset($form[$hidethis])) {
$form[$hidethis]['#type'] = 'hidden';
}
}
// add material options for this farm
$fname = 'field_link_to_registered_agchem';
$form[$fname]['und']['#title'] = 'Select Registered Spray Materials to Include for this Farm';
$blockid = $dh_adminreg_feature->dh_link_feature_submittal['und'][0]['target_id'];
om_agman_agchem_materials_select($dh_adminreg_feature->farmid, $form, $fname, 'ipm_block_select_formulary', 'page_1', 'dh_adminreg_feature_field_data_field_link_agchem_material_ad');
$form['data']['#tree'] = TRUE;
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 40,
);
$form['actions']['sprayquan'] = array(
'#type' => 'submit',
'#value' => t('Enter Amounts'),
'#weight' => 40,
'#submit' => array('om_agman_materials_form_sprayquan')
);
switch ($op) {
case 'add':
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#weight' => 45,
'#limit_validation_errors' => array(),
'#submit' => array('om_agman_materials_form_submit_cancel')
);
break;
case 'edit':
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete Plan'),
'#weight' => 45,
'#limit_validation_errors' => array(),
'#submit' => array('om_agman_materials_form_submit_delete')
);
break;
}
return $form;
}
function om_agman_materials_form_submit_cancel($form, &$form_state) {
$parms = drupal_get_query_parameters();
if (isset($parms['finaldest'])) {
$url = $parms['finaldest'];
drupal_goto($url);
//dpm($url);
$form_state['redirect'] = $url;
} else {
$url = $form_state['redirect'] ? $form_state['redirect'] : '';
drupal_goto($url);
}
}
/**
* Form API submit callback for the type form.
*/
function om_agman_materials_form_save(&$form, &$form_state) {
form_load_include($form_state, 'inc', 'entity', 'includes/entity.ui');
form_load_include($form_state, 'inc', 'dh', 'dh.admin');
// load the object
$dh_adminreg_feature = entity_ui_form_submit_build_entity($form, $form_state);
$dh_adminreg_feature->save();
// handle all the attached stuff with the form plugin, using the data array
// load the plugin to handle this aggregate form
$class = ctools_plugin_load_class('om', 'om_components', 'ObjectModelAgmanSprayAppEvent', 'handler');
$src = new $class(array());
$src->dh_adminreg_feature = $dh_adminreg_feature;
// handle all the attached stuff using the SaveDataObjectsAsForm
//dpm("Building form for save");
$src->BuildForm($form, $form_state);
//dpm($src,"Saved src");
$src->SaveDataObjectsAsForm();
return $dh_adminreg_feature;
}
function om_agman_materials_form_submit(&$form, &$form_state) {
$dh_adminreg_feature = om_agman_materials_form_save($form, $form_state);
$blockid = $dh_adminreg_feature->dh_link_feature_submittal['und'][0]['target_id'];
$parms = drupal_get_query_parameters();
if (isset($parms['finaldest'])) {
$url = $parms['finaldest'];
drupal_goto($url);
//dpm($url);
$form_state['redirect'] = $url;
} else {
$url = implode('/', array('ipm-landunit-info', $blockid));
drupal_goto($url);
}
}
function om_agman_materials_form_sprayquan(&$form, &$form_state) {
$blockid = $form_state['values']['dh_link_feature_submittal']['und'][0]['target_id'];
$dh_adminreg_feature = om_agman_materials_form_save($form, $form_state);
$url = implode('/', array('ipm-live-events', $blockid, 'sprayquan', $dh_adminreg_feature->adminid));
$extras = array();
$parms = drupal_get_query_parameters();
if (isset($parms['finaldest'])) {
$extras['query']['finaldest'] = $parms['finaldest'];
}
drupal_goto($url, $extras);
//$form_state['redirect'] = $url;
}
/**
* Form API submit callback for the delete button.
*/
function om_agman_materials_form_submit_delete(&$form, &$form_state) {
$form_state['redirect'] = 'admin/content/dh_adminreg_feature/manage/' . $form_state['dh_adminreg_feature']->adminid . '/delete';
}
function om_agman_application_materials_form () {
global $user;
// Use: q=ipm-live-events/[blockid]/materials/[action or e-adminid]/[vineyardid]/[date]?finaldest=ipm-home/146/all
// Ex: q=ipm-live-events/all/materials/add/146/2017-03-26?finaldest=ipm-home/146/all
// we are given the block featureid as input
// need to fetch the vineyard/facility parent of block
// get the available material list from the parent vineyard
$planid = 'add';
$op = 'add';
$a = arg();
$parms = drupal_get_query_parameters();
//dpm($a);
$startdate = date('Y-m-d');
if (isset($a[1])) {
$blockid = $a[1];
}
if (isset($a[3])) {
$planid = $a[3];
}
if (isset($a[4])) {
$farmid = $a[4];
}
// validate here and require a valid farmid/hydroid
if (!is_numeric($farmid) and !is_numeric($blockid)) {
if (isset($parms['finaldest'])) {
$url = $parms['finaldest'];
} else {
$url = 'ipm-home';
}
drupal_set_message("You must include a valid farm id or block id.", 'error');
drupal_goto($url);
}
$farmid = (!is_numeric($farmid)) ? om_agman_get_block_farmid($blockid) : $farmid;
// if farmid == all and blockid == all, drupal_set_message and go to the finaldest url
// but we should never reach this condition
if (isset($a[5])) {
$startdate = $a[5];
}