-
Notifications
You must be signed in to change notification settings - Fork 0
/
budgetsys.module
1333 lines (1262 loc) · 43.9 KB
/
budgetsys.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
/**
* @file
* The main module file for the Budget System.
*/
/******************************************************************
**********************Drupal Hooks*********************************
*******************************************************************/
/**
* Implements hook_help().
*/
function budgetsys_help($path, $arg) {
switch ($path) {
case 'admin/help#budgetsys':
// Return a line-break version of the module README.txt
return check_markup(file_get_contents( dirname(__FILE__) . "/README.txt") );
}
}
/**
* Implements hook_entity_info().
*/
function budgetsys_entity_info() {
$return = array(
'budgetsys_line' => array(
'label' => t('Budget System Line Item'),
'plural label' => t('Budget System Line Items'),
'description' => t('An entity type for line items which group budget values.'),
'entity class' => 'BudgetsysLineClass',
'controller class' => 'BudgetsysLineClassController',
'base table' => 'budgetsys_line',
'fieldable' => TRUE,
'module' => 'budgetsys_line',
'load hook' => 'budgetsys_line_load',
'entity keys' => array(
'id' => 'lid',
'label' => 'title',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'static cache' => TRUE,
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'access callback' => 'budgetsys_line_access',
),
);
$return['budgetsys_line_type'] = array(
'label' => t('Line Item Type'),
'entity class' => 'LineItemType',
'controller class' => 'LineItemTypeController',
'base table' => 'budgetsys_line_type',
'fieldable' => FALSE,
'bundle of' => 'budgetsys_line',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'ltid',
'name' => 'type',
'label' => 'label',
),
'module' => 'budgetsys_line',
'access callback' => 'budgetsys_line_type_access',
);
$return['budgetsys_org'] = array(
'label' => t('Budget System Organization'),
'plural label' => t('Budget System Organizations'),
'description' => t('An entity type for organizations which group line items.'),
'entity class' => 'BudgetsysOrgClass',
'controller class' => 'BudgetsysOrgClassController',
'base table' => 'budgetsys_org',
'access callback' => 'budgetsys_org_access',
'fieldable' => TRUE,
'bundles' => array(),
'entity keys' => array(
'id' => 'oid',
),
// Make use the class' label() and uri() implementation by default.
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'budgetsys_org',
);
$return['budgetsys_value'] = array(
'label' => t('Budget Value'),
'plural label' => t('Budget Values'),
'description' => t('An entity type for budget values used in values.'),
'entity class' => 'BudgetsysValueClass',
'controller class' => 'BudgetsysValueController',
'base table' => 'budgetsys_value',
'revision table' => 'budgetsys_value_revision',
'fieldable' => TRUE,
'module' => 'budgetsys_value',
'load hook' => 'budgetsys_value_load',
'entity keys' => array(
'id' => 'bvid',
'revision' => 'vid',
'label' => 'title',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'static cache' => TRUE,
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'access callback' => 'budgetsys_value_access',
);
$return['budgetsys_value_type'] = array(
'label' => t('Budget Value Type'),
'entity class' => 'BudgetValueType',
'controller class' => 'BudgetValueTypeController',
'base table' => 'budgetsys_value_type',
'fieldable' => FALSE,
'bundle of' => 'budgetsys_value',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'vtid',
'name' => 'type',
'label' => 'label',
),
'module' => 'budgetsys_value',
'access callback' => 'budgetsys_value_type_access',
);
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function budgetsys_entity_info_alter(&$entity_info) {
foreach (budgetsys_line_item_types() as $type => $info) {
$entity_info['budgetsys_line']['bundles'][$type] = array(
'label' => $info->label,
);
}
}
/**
* Implements hook_entity_property_info_alter().
*/
function budgetsys_entity_property_info_alter(&$info) {
$properties = &$info['budgetsys_line']['properties'];
$properties['created'] = array(
'label' => t("Date created"),
'type' => 'date',
'description' => t("The date the budget line was posted."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget line items',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t("Date changed"),
'type' => 'date',
'schema field' => 'changed',
'description' => t("The date the budget like was most recently updated."),
);
$properties['oid'] = array(
'label' => t("Organization"),
'type' => 'user',
'description' => t("The organization of the budget line."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget line items',
'required' => TRUE,
'schema field' => 'oid',
);
$properties['account_number'] = array(
'label' => t("Account Number"),
'type' => 'varchar',
'description' => t("The account number of the budget line."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget line items',
'required' => TRUE,
'schema field' => 'account_number',
);
$properties['uid'] = array(
'label' => t("Author"),
'type' => 'user',
'description' => t("The author of the budget line."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget line items',
'required' => TRUE,
'schema field' => 'uid',
);
$properties = &$info['budgetsys_org']['properties'];
$properties['created'] = array(
'label' => t("Date created"),
'type' => 'date',
'description' => t("The date the budget line was posted."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget organizations',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t("Date changed"),
'type' => 'date',
'schema field' => 'changed',
'description' => t("The date the budget like was most recently updated."),
);
$properties['active'] = array(
'label' => t("Active"),
'type' => 'integer',
'description' => t("The status of the organization."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget organizations',
'required' => TRUE,
'schema field' => 'active',
);
$properties['budget_category'] = array(
'label' => t("Budget Category"),
'type' => 'text',
'description' => t("The category this budget is in."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget organizations',
'required' => TRUE,
'schema field' => 'budget_category',
);
$properties['title'] = array(
'label' => t("Title"),
'type' => 'text',
'description' => t("The title of this organization."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget organizations',
'schema field' => 'title',
);
$properties = &$info['budgetsys_value']['properties'];
$properties['created'] = array(
'label' => t("Date created"),
'type' => 'date',
'description' => t("The date the budget value was posted."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget values',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t("Date changed"),
'type' => 'date',
'schema field' => 'changed',
'description' => t("The date the budget value was most recently updated."),
);
$properties['oid'] = array(
'label' => t("Organization"),
'type' => 'user',
'description' => t("The organization of the budget value."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget values',
'required' => TRUE,
'schema field' => 'oid',
);
$properties['account_number'] = array(
'label' => t("Account Number"),
'type' => 'text',
'description' => t("The account number of the budget value."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget values',
'required' => TRUE,
'schema field' => 'account_number',
);
$properties['year'] = array(
'label' => t("Fiscal Year"),
'type' => 'text',
'description' => t("The fiscal year of the budget value."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget values',
'required' => TRUE,
'schema field' => 'year',
);
$properties['uid'] = array(
'label' => t("Author"),
'type' => 'user',
'description' => t("The author of the budget value."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget values',
'required' => TRUE,
'schema field' => 'uid',
);
$properties['value'] = array(
'label' => t("Value"),
'type' => 'decimal',
'description' => t("The value of this budget value."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer budget values',
'required' => TRUE,
'schema field' => 'value',
);
}
/**
* Implements hook_views_data_alter().
*/
function budgetsys_views_data_alter(&$data) {
$data['budgetsys_value']['organization_id'] = array(
'title' => t('Organization Relationship'),
'help' => t('Adds a relationship to an organization'),
'relationship' => array(
'base' => 'budgetsys_org', // Table we're joining to.
'base field' => 'oid', // Field on the joined table.
'field' => 'oid', // Real field name on the 'foo' table.
'handler' => 'views_handler_relationship',
'label' => t('Organization'),
'title' => t('Organization'),
'help' => t('The organization linked to this budget value.'),
),
);
$data['budgetsys_line']['organization_id'] = array(
'title' => t('Organization Relationship'),
'help' => t('Adds a relationship to an organization'),
'relationship' => array(
'base' => 'budgetsys_org', // Table we're joining to.
'base field' => 'oid', // Field on the joined table.
'field' => 'oid', // Real field name on the 'foo' table.
'handler' => 'views_handler_relationship',
'label' => t('Organization'),
'title' => t('Organization'),
'help' => t('The organization linked to this budget line item.'),
),
);
}
/**
* Implements hook_menu().
*/
function budgetsys_menu() {
$items['budget/full'] = array(
'title' => 'Budget',
'description' => 'The entire budget displayed by budget category',
'page callback' => 'budgetsys_view_budget',
'page arguments' => array(2,3),
'access arguments' => array('view budget'),
'file' => 'budgetsys.pages.inc',
);
$items['budget/full/all-lines'] = array(
'title' => 'All Lines',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access arguments' => array('view budget'),
);
$items['admin/config/system/budget_system'] = array(
'title' => t('Budget System Configuration'),
'description' => 'Configuration for the Budget System module',
'page callback' => 'drupal_get_form',
'page arguments' =>array('budget_value_configuration_form'),
'access arguments' => array('administer budget system'),
'type' => MENU_NORMAL_ITEM,
'file' => 'budgetsys.settings.inc',
);
$items['admin/budget'] = array(
'title' => 'Budget',
'description' => 'Administer Budget System',
'position' => 'right',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer budget system'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
return $items;
}
/**
* Adds the theme specifications to the Theme Registry
*/
function budgetsys_theme($existing, $type, $theme, $path) {
return array(
'budgetsys_full_budget' => array(
'variables' => array('element' => null),
'template' => 'budgetsys_full_budget',
'path' => drupal_get_path('module','budgetsys') . '/templates',
),
);
}
/**
* Implements hook_permission().
*/
function budgetsys_permission() {
$permissions = array(
'administer budget line items' => array(
'title' => t('Administer budget line items'),
'description' => t('Allows users to configure budget line item types and their fields.'),
'restrict access' => TRUE,
),
'create budget line items' => array(
'title' => t('Create budget line items'),
'description' => t('Allows users to create budget line items.'),
'restrict access' => TRUE,
),
'view budget line items' => array(
'title' => t('View budget line items'),
'description' => t('Allows users to view budget line items.'),
),
'edit any budget line items' => array(
'title' => t('Edit any budget line items'),
'description' => t('Allows users to edit any budget line items.'),
'restrict access' => TRUE,
),
'edit own budget line items' => array(
'title' => t('Edit own budget line items'),
'description' => t('Allows users to edit own budget line items.'),
'restrict access' => TRUE,
),
'administer budget organizations' => array(
'title' => t('Administer budget organizations'),
'description' => t('Allows users to configure budget line item types and their fields.'),
'restrict access' => TRUE,
),
'create budget organizations' => array(
'title' => t('Create budget organizations'),
'description' => t('Allows users to create budget organizations.'),
'restrict access' => TRUE,
),
'view budget organizations' => array(
'title' => t('View budget organizations'),
'description' => t('Allows users to view budget organizations.'),
),
'edit any budget organizations' => array(
'title' => t('Edit any budget organizations'),
'description' => t('Allows users to edit any budget organizations.'),
'restrict access' => TRUE,
),
'edit own budget organizations' => array(
'title' => t('Edit own budget organizations'),
'description' => t('Allows users to edit own budget organizations.'),
'restrict access' => TRUE,
),
'administer budget values' => array(
'title' => t('Administer budget values'),
'description' => t('Allows users to configure budget value types and their fields.'),
'restrict access' => TRUE,
),
'create budget values' => array(
'title' => t('Create budget values'),
'description' => t('Allows users to create budget values.'),
'restrict access' => TRUE,
),
'view budget values' => array(
'title' => t('View budget values'),
'description' => t('Allows users to view budget values.'),
),
'edit any budget values' => array(
'title' => t('Edit any budget values'),
'description' => t('Allows users to edit any budget values.'),
'restrict access' => TRUE,
),
'edit own budget values' => array(
'title' => t('Edit own budget values'),
'description' => t('Allows users to edit own budget values.'),
'restrict access' => TRUE,
),
'view budget' => array(
'title' => t('View Budget'),
'description' => t('Allow users to view bdget.'),
),
);
return $permissions;
}
/**
* Creates a new object from the specified class type.
*
* @param $class_type
* The Budget System API class you wish to call.
*
* @return
* An object of that class.
*/
function budgetsys_load_class($class_type) {
$loaded_classes = &drupal_static(__FUNCTION__, array());
if (!isset($loaded_classes[$class_type])) {
$class_name = 'BudgetSystem';
$class_name .= ucfirst($class_type);
$class_name .= 'API';
$loaded_classes[$class_type] = new $class_name();
}
return $loaded_classes[$class_type];
}
/******************************************************************
**********************Settings API*********************************
*******************************************************************/
/**
* Creates an array of budget_categories from the variable.
*
* @return $budget_allowed_years
* An array of allowed years from the budgetsys_allowed_fiscal_years
* variable.
*/
function budgetsys_load_years() {
return budgetsys_load_class('settings')->loadYears();
}
/**
* Generates a list of allowed years. Both the index
* and the values are the translated allowed years.
*
* @return $list
* An array of allowed years, keyed by the allowed years.
*
*/
function budgetsys_load_years_list() {
return budgetsys_load_class('settings')->loadYears(TRUE);
}
/**
* Generates an array of fiscal years.
*
* @param $current_year
* The current year, formatted as either a 2 year 9 digit definition or a 1 year 4 digit definition
*
* @param $num_years
* The number of years to return
*
* @return
* An array consisting of the number of years specified starting from, but not including,
* $current_year
*/
function budgetsys_generate_prior_years($current_year = NULL, $num_years = NULL, $reversed = NULL) {
return budgetsys_load_class('settings')->generatePriorYears($current_year, $num_years, $reversed);
}
/**
*
*/
function budgetsys_get_years() {
return budgetsys_load_class('settings')->getYears();
}
/**
* Loads a taxonomy tree for the Budget System Fiscal Years
*/
function budgetsys_fiscal_year_taxonomy_tree() {
return budgetsys_load_class('settings')->fiscalYearTaxonomyTree();
}
/**
* Creates an associative array keyed by term IDs with values of
* the names of those terms.
*
* returns
* An array keyed by TIDs with values of term names.
*/
function budgetsys_current_fiscal_year_taxonomy() {
return budgetsys_load_class('settings')->currentFiscalYear();
}
/**
* Extracts the term id from a vocabulary term
*
* @param $term
* A taxonomy term object
*
* returns
* The tid of the term
*/
function budgetsys_taxonomy_get_tid($term) {
return budgetsys_load_class('settings')->taxonomyGetTID($term);
}
/**
* Extracts the name from a vocabulary term
*
* @param $term
* A taxonomy term object
*
* returns
* The name of the term
*/
function budgetsys_taxonomy_get_name($term) {
return budgetsys_load_class('settings')->taxonomyGetName($term);
}
/******************************************************************
**********************Formatting API*******************************
*******************************************************************/
/**
* A simple function to properly format currency.
*
* @param $value
* The value to be formatted as currency.
*
* @return
* The formatted currency.
*/
function budget_value_format_currency($value) {
return budgetsys_load_class('formatting')->formatCurrency($value);
}
/**
* function to create a link render item. Returns the render array.
*
* @param $text
* The text of the link
*
* @param $path
* The path the link leads to
*
* @return
* see above
*/
function budgetsys_create_link($text, $path) {
return budgetsys_load_class('formatting')->createLink($text, $path);
}
/******************************************************************
**********************Line Item API********************************
*******************************************************************/
/**
* Loads the LIDs of all the budget lines in the database
*
* @return
* An array of all the LIDs in the system
*/
function budgetsys_line_load_all_lids() {
return budgetsys_load_class('line')->loadAllLineIDs();
}
/*******************************************************************************
****************************** Budget Line API ******************************
******************************************************************************/
/**
* Access callback for Budget Line.
*/
function budgetsys_line_access($op, $budget_line, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer budget line items', $account)
|| user_access('create budget line items', $account);
case 'view':
return user_access('administer budget line items', $account)
|| user_access('view budget line items', $account);
case 'edit':
return user_access('administer budget line items')
|| user_access('edit any budget line items')
|| (user_access('edit own budget line items') && ($budget_line->uid == $account->uid));
}
}
/**
* Load a budget line item.
*/
function budgetsys_line_load($lid, $reset = FALSE) {
$budget_line = budgetsys_line_load_multiple(array($lid), array(), $reset);
return reset($budget_line);
}
/**
* Load multiple line items based on certain conditions.
*/
function budgetsys_line_load_multiple($lids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('budgetsys_line', $lids, $conditions, $reset);
}
/**
* Save budget line.
*/
function budgetsys_line_save($line_item) {
entity_save('budgetsys_line', $line_item);
}
/**
* Delete single budget line.
*/
function budgetsys_line_delete($line_item) {
entity_delete('budgetsys_line', entity_id('budgetsys_line' ,$line_item));
}
/**
* Delete multiple budget lines.
*/
function budgetsys_line_delete_multiple($line_ids) {
entity_delete_multiple('budgetsys_line', $line_ids);
}
/**
* Query the database for the value_type of a specific account.
*/
function budgetsys_line_value_type($account) {
return entity_get_controller('budgetsys_line')->queryValueType($account);
}
/*******************************************************************************
****************************** Budget Line Type API *************************
******************************************************************************/
/**
* Access callback for Line Type.
*/
function budgetsys_line_type_access($op, $entity = NULL) {
return user_access('administer budget line items');
}
/**
* Load Line Type.
*/
function budgetsys_line_type_load($line_item_type) {
return budgetsys_line_item_types($line_item_type);
}
/**
* List of budget line types.
*/
function budgetsys_line_item_types($line_item_name = NULL) {
$types = entity_load_multiple_by_name('budgetsys_line_type', isset($line_item_name) ? array($line_item_name) : FALSE);
return isset($line_item_name) ? reset($types) : $types;
}
/**
* Save budget line type entity.
*/
function budgetsys_line_type_save($line_item_type) {
entity_save('budgetsys_line_type', $line_item_type);
}
/**
* Delete single budget line type.
*/
function budgetsys_line_type_delete($line_item_type) {
entity_delete('budgetsys_line_type', entity_id('budgetsys_line_type' ,$line_item_type));
}
/**
* Delete multiple line types.
*/
function budgetsys_line_type_delete_multiple($line_item_type_ids) {
entity_delete_multiple('budgetsys_line_type', $line_item_type_ids);
}
/*******************************************************************************
****************************** Budget Value API ******************************
******************************************************************************/
/**
* Access callback for Budget Value.
*/
function budgetsys_value_access($op, $budget_value, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer budget value items', $account)
|| user_access('create budget value items', $account);
case 'view':
return user_access('administer budget value items', $account)
|| user_access('view budget value items', $account);
case 'edit':
return user_access('administer budget value items')
|| user_access('edit any budget value items')
|| (user_access('edit own budget value items') && ($budget_value->uid == $account->uid));
}
}
/**
* Load a budget value.
*/
function budgetsys_value_load($lid, $reset = FALSE) {
$budget_value = budgetsys_value_load_multiple(array($lid), array(), $reset);
return reset($budget_value);
}
/**
* Load multiple values based on certain conditions.
*/
function budgetsys_value_load_multiple($lids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('budgetsys_value', $lids, $conditions, $reset);
}
/**
* Save budget value.
*/
function budgetsys_value_save($value) {
entity_save('budgetsys_value', $value);
module_invoke_all('budgetsys_value_update', $value);
}
/**
* Delete single budget value.
*/
function budgetsys_value_delete($value) {
entity_delete('budgetsys_value', entity_id('budgetsys_value' ,$value));
}
/**
* Delete multiple budget values.
*/
function budgetsys_value_delete_multiple($value_ids) {
entity_delete_multiple('budgetsys_value', $value_ids);
}
/**
* Query the database for a specific budget value
*/
function budgetsys_value_query_values($account, $type, $year) {
return entity_get_controller('budgetsys_value')->queryValue($account, $type, $year);
}
function budgetsys_value_load_budget_value($account, $year, $type, $taxonomy = NULL) {
return entity_get_controller('budgetsys_value')->loadBudgetValue($account, $year, $type, $taxonomy);
}
function budgetsys_value_load_budget_line_item($account, $main_type = NULL, $types = NULL ,$years = NULL, $taxonomy = NULL) {
if($taxonomy) {
return entity_get_controller('budgetsys_value')->loadBudgetLineItemTaxonomy($account, $main_type, $types, $years);
}
return entity_get_controller('budgetsys_value')->loadBudgetLineItem($account, $main_type, $types, $years);
}
function budgetsys_value_item_formatter($account, $year, $type, $taxonomy) {
return entity_get_controller('budgetsys_value')->budgetLineItemFormatter($account, $year, $type, $taxonomy);
}
/**
* Calculates the total for a series of accounts.
* If calculating a total for an entire organization use the budgetsys_org_calculate_total function instead
*
* @param $accounts
* An array of accounts that should be added together
*
* @param $type
* The type of budget values that should be used in this calculation
*
* @param $year
* The year for the budget values that should be used in this calculation
*/
function budgetsys_value_calculate_total($accounts, $type, $year, $taxonomy = NULL) {
$total = 0;
foreach($accounts as $account) {
$budget_value = budgetsys_value_load_budget_value($account, $year, $type, $taxonomy);
$value = $budget_value['value'];
$total += $value;
}
return $total;
}
/**
* Add Account Year field to a Budget Value Type.
*
* @param $type
* A budget valye type object.
* @param $label
* The label for the body instance.
*
* @return
* Account Year field instance.
*/
function budgetsys_add_account_year_field($type, $label = 'Account Year') {
// Add or remove the body field, as needed.
$vocab = variable_get('budgetsys_fiscal_years_vid');
$field = field_info_field('budgetsys_account_year');
$instance = field_info_instance('budgetsys_value', 'budgetsys_account_year', $type->type);
if (empty($field)) {
$field = array(
'field_name' => 'budgetsys_account_year',
'type' => 'taxonomy_term_reference',
'cardinality' => '1',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocab->machine_name,
'parent' => 0,
),
),
),
'entity_types' => array('budgetsys_value'),
);
$field = field_create_field($field);
}
if (empty($instance)) {
$instance = array(
'field_name' => 'budgetsys_account_year',
'entity_type' => 'budgetsys_value',
'bundle' => $type->type,
'label' => $label,
'widget' => array('type' => 'taxonomy_term_reference'),
'settings' => array('display_summary' => TRUE),
'required' => TRUE,
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'plain_text',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'plain_text',
),
),
);
$instance = field_create_instance($instance);
}
return $instance;
}
/*******************************************************************************
****************************** Budget Value Type API *************************
******************************************************************************/
/**
* Access callback for value Type.
*/
function budgetsys_value_type_access($op, $entity = NULL) {
return user_access('administer budget value items');
}
/**
* Load value Type.
*/
function budgetsys_value_type_load($value_type) {
return budgetsys_value_types($value_type);
}
/**
* List of budget value types.
*/
function budgetsys_value_types($value_name = NULL) {
$types = entity_load_multiple_by_name('budgetsys_value_type', isset($value_name) ? array($value_name) : FALSE);
return isset($value_name) ? reset($types) : $types;
}
/**
* array of budget value types keyed by the value type.
*/
function budgetsys_value_types_array() {
$types = budgetsys_value_types();
$return = array();
foreach($types as $type) {
$return[$type->type] = $type->label;
}
return $return;
}
/**
* Returns an array of the budget value types without the main value type.
*/
function budgetsys_value_types_extra() {
$types = array();
foreach(budgetsys_value_types() as $type) {
$types[] = $type->label;
$types_type[] = $type->type;
}
$main_type_key = array_search(variable_get('budgetsys_value_final_type'), $types_type);
unset($types[$main_type_key]);
return array_values($types); // Adds the types array into the content array, but reindexes using array_values first
}
/**
* Save budget value type entity.
*/
function budgetsys_value_type_save($value_type) {
entity_save('budgetsys_value_type', $value_type);
}
/**
* Delete single budget value type.
*/
function budgetsys_value_type_delete($value_type) {
entity_delete('budgetsys_value_type', entity_id('budgetsys_value_type' ,$value_type));
}
/**
* Delete multiple value types.
*/
function budgetsys_value_type_delete_multiple($value_type_ids) {
entity_delete_multiple('budgetsys_value_type', $value_type_ids);
}
/******************************************************************
**********************Organization API*****************************
*******************************************************************/
/**
* Access callback for Budget Line.
*/
function budgetsys_org_access($op, $budget_line, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer budget organizations', $account)
|| user_access('create budget organizations', $account);
case 'view':
return user_access('administer budget organizations', $account)
|| user_access('view budget organizations', $account);
case 'edit':
return user_access('administer budget organizations')
|| user_access('edit any budget organizations')
|| (user_access('edit own budget organizations') && ($budget_line->uid == $account->uid));
}
}
/**
* @return $budget_categories
* An array of budget categories from the budgetsys_org_budget_categories
* variable.
*
* Creates an array of budget_categories from the variable.
*/
function budgetsys_org_load_budget_categories() {
return budgetsys_load_class('org')->loadBudgetCategories();
}
/**
* @return $list
* An array of budget categories, keyed by the category name
*
* Generates a list of budget categories. Both the index