~raulraat/eventum/raul

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
var dynamic_options = new Array();

{foreach from=$fields item=field name=fields}
i = dynamic_options.length;
dynamic_options[i] = new Object();
dynamic_options[i].target_field_id = {$field.fld_id};
dynamic_options[i].fld_type = '{$field.fld_type}';
dynamic_options[i].controlling_field_id = '{$field.controlling_field_id}';
dynamic_options[i].controlling_field_name = '{$field.controlling_field_name}';
dynamic_options[i].hide_when_no_options = '{$field.hide_when_no_options}';
dynamic_options[i].lookup_method = '{$field.lookup_method}';
dynamic_options[i].groups = new Array();
    {foreach from=$field.structured_data key=key item=group}
    j = dynamic_options[i].groups.length;
    dynamic_options[i].groups[j] = new Object();
    dynamic_options[i].groups[j].keys = new Array();
        {foreach from=$group.keys item=key}
        dynamic_options[i].groups[j].keys[dynamic_options[i].groups[j].keys.length] = '{$key}';
        {/foreach}
    dynamic_options[i].groups[j].options = new Array();
        {foreach from=$group.options item=option key=option_value}
        dynamic_options[i].groups[j].options[dynamic_options[i].groups[j].options.length] = new Option('{$option|escape:'javascript'}', '{$option_value|escape:'javascript'}');
        {/foreach}
    {/foreach}
{/foreach}
{literal}
function custom_field_get_details_by_controller(controller_id)
{
    var details = new Array();
    for (var i = 0; i < dynamic_options.length; i++) {
        if (dynamic_options[i].controlling_field_id == controller_id) {
            details[details.length] = dynamic_options[i];
        }
    }
    return details;
}

function custom_field_get_details_by_target(target_id)
{
    for (i = 0; i < dynamic_options.length; i++) {
        if (dynamic_options[i].target_field_id == target_id) {
            return dynamic_options[i];
        }
    }
}


function custom_field_init_dynamic_options(fld_id)
{
    for (var i = 0; i < dynamic_options.length; i++) {
        if (dynamic_options[i].target_field_id == fld_id) {
            // set alert on target field prompting them to choose controlling field first
            target_field = $('#custom_field_' + dynamic_options[i].target_field_id);
            target_field.bind("focus.choose_controller", dynamic_options[i].target_field_id, function(e) {
                target_field = e.target;
                target_id = e.data;
                details = custom_field_get_details_by_target(target_id);

                alert('{/literal}{t escape=js}Please choose{/t} ' + details.controlling_field_name + ' {t}first{/t}{literal}');

                target_field.blur();
                return false;
            });

            // set event handler for controlling field
            controlling_field = $('#' + dynamic_options[i].controlling_field_id);
            controlling_field.bind('change.change_options', dynamic_options[i].controlling_field_id, function(e) {
                custom_field_set_new_options($(e.target), false);
            });
            custom_field_set_new_options(controlling_field, true, fld_id);
            break;
        }
    }
}


function custom_field_set_new_options(controller, keep_target_value, target_fld_id) {
    // get current value of controller field
    value = controller.val();

    // find the object
    if (target_fld_id != undefined) {
        details = new Array();
        details[0] = custom_field_get_details_by_target(target_fld_id);
    } else {
        details = custom_field_get_details_by_controller(controller.attr('id'), target_fld_id);
    }
    for (var i = 0; i < details.length; i++) {
        // get the target/targets
        var targets = new Array();
        targets[0] = target = $('#custom_field_' + details[i].target_field_id);

        for (var targ_num = 0; targ_num < targets.length; targ_num++) {
            wrapped_target = targets[targ_num];
            target = wrapped_target.get(0);
            // see if this value has a set of options for the child field
            if (keep_target_value) {
                // get the current value
                current_value = wrapped_target.val();
            }
            if (target.type != 'text' && target.type != 'textarea' && details[i].fld_type != 'date') {
                target.options.length = 1;
            }
            var show = false;
            if (details[i].lookup_method == 'local') {
                for (var j = 0; j < details[i].groups.length; j++) {
                    for (var k = 0; k < details[i].groups[j].keys.length; k++) {
                        if (((typeof value == 'object') && (value.indexOf(details[i].groups[j].keys[k]) > -1)) || (details[i].groups[j].keys[k] == value)) {
                            show = true;
                            for (var l = 0; l < details[i].groups[j].options.length; l++) {
                                target.options[target.options.length] = details[i].groups[j].options[l];
                            }
                            // unbind "choose a controller" message
                            wrapped_target.unbind("focus.choose_controller");
                            if (keep_target_value) {
                                if (target.type == 'text' || target.type == 'textarea') {
                                    target.value = current_value;
                                } else {
                                    selectOption(target.form, target.name, current_value);
                                }
                            } else {
                                if (target.type == 'text' || target.type == 'textarea') {
                                    target.value = '';
                                } else {
                                    target.selectedIndex = 0;
                                }
                            }
                        }
                    }
                }
            } else if (details[i].lookup_method == 'ajax') {
                // submit form via ajax trying to get data
                $('#report_form').ajaxSubmit({
                    'type':   'GET',
                    'url':  'rpc/get_custom_field_dynamic_options.php',
                    'dataType': 'json',
                    'data': {
                        'fld_id':   details[i].target_field_id
                    },
                    'success': function(options, status) {
                        console.info(options);
                        if (options != null) {
                            target.options.length = 0;
                            $.each(options, function(key, val) {
                                target.options[target.options.length] = new Option(val, key);
                                return true;
                            });
                            $(target).unbind("focus.choose_controller");
                        } else {
                            target.options.length = 0;
                            target.options[0] = new Option('Please choose an option', "");
                        }
                    }
                })
            }

            if (details[i].hide_when_no_options == 1) {
                if (show == false) {
                    target.parentNode.parentNode.style.display = 'none';
                } else {
                    target.parentNode.parentNode.style.display = getDisplayStyle();
                }
            }
        }
    }

}
{/literal}