summaryrefslogtreecommitdiffstats
path: root/apk_browser.install
blob: a5cd42e68f0509949ac381f21a61e754e48982ac (plain)
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
<?php

function apk_browser_install() {
    /*
     * Alpine packages are stored as nodes.
     * To make sure we have content type we
     * create it with hook install so its a
     * system content type which cannot be
     * deleted.
     */
    // use get_t() to get the name of our localization function for translation
    // during install, when t() is not available.
    $t = get_t();
    //basic new node settings
    $node_fields = array(
        'type' => 'apk', //machine name
        'name' => 'Alpine package', // human name
        'base' => 'node_content', //api callback
        'description' => $t('Content type to store Alpine Linux packages'),
        'title_label' => $t('Package'),
    );
    //set missing defaults and save the node type
    $content_type = node_type_set_defaults($node_fields);
    node_type_save($content_type);

    /*
     * Create all the fields and instances we are adding to our content type.
     */
    foreach (_apk_browser_field_vars() as $name => $fvars) {
        if (!field_info_field($name)) {
            $field = array(
                'field_name' => $name,
                'cardinality' => (isset($fvars['cdl'])) ? $fvars['cdl'] : '1',
                'type' => (isset($fvars['type'])) ? $fvars['type'] : 'text',
                'settings' => array(
                    'max_length' => (isset($fvars['length'])) ? $fvars['length'] : '255',
                ),
            );
            field_create_field($field);
        }
        // now all instances
        if (!field_info_instance('node', $name, 'apk')) {
            $instance = array(
                'field_name' => $name,
                'label' => $fvars['label'],
                'type' => 'text',
                'entity_type' => 'node',
                'bundle' => 'apk',
                'widget' => array(
                    'type' => (isset($fvars['widget'])) ? $fvars['widget'] : 'text_textfield',
                ),
                'display' => array(
                    'default' => array(
                        'label' => 'inline'
                    )
                )
            );
            field_create_instance($instance);
        }
    }

    /*
     * Create taxonamy vocab
     * create table and asign it
     */
    $vnames = array(
        'repo' => $t('Repository'),
        'arch' => $t('Architecture')
    );
    foreach ($vnames as $vname => $desc) {
        $fieldname = 'taxonomy_apk_' . $vname;
        $machinename = 'apk_' . $vname;
        $vedit = array(
            'name' => $desc,
            'description' => $t('@desc taxonomy', array('@desc' => $desc)),
            'machine_name' => $machinename
        );
        if (taxonomy_vocabulary_save((object) $vedit)) {
            if (!field_info_field($fieldname)) {
                $field = array(
                    'field_name' => $fieldname,
                    'type' => 'taxonomy_term_reference',
                    //sets the number of terms which can be selected
                    'cardinality' => '1',
                    'settings' => array(
                        'allowed_values' => array(
                            array(
                                'vocabulary' => $machinename,
                                'parent' => 0,
                            ),
                        ),
                    ),
                );
                field_create_field($field);
            }
            if (!field_info_instance('node', $fieldname, 'taxonomy_term_reference')) {
                $instance = array(
                    'field_name' => $fieldname,
                    'entity_type' => 'node',
                    'label' => $desc,
                    'bundle' => 'apk',
                    'required' => TRUE,
                    'widget' => array(
                        'type' => 'options_select',
                    )
                );
                field_create_instance($instance);
            }
        }
    }
}

//cleanup database, removing all apk entries
function apk_browser_uninstall() {
    $nfields = array_keys(_apk_browser_field_vars());
    //delete the content type
    node_type_delete('apk');
    //delete the apk node fields
    foreach ($nfields as $nfield) {
        field_delete_field($nfield);
    }
    //delete all apk related instaces
    $instances = field_info_instances('node', 'apk');
    foreach ($instances as $instance) {
        field_delete_instance($instance, TRUE);
    }
    field_delete_field('taxonomy_apk_repo');
    field_delete_field('taxonomy_apk_arch');
    //delete taxonomy
    $vocabulary = taxonomy_vocabulary_machine_name_load('apk_arch');
    if ($vocabulary) {
        taxonomy_vocabulary_delete($vocabulary->vid);
    }
    $vocabulary = taxonomy_vocabulary_machine_name_load('apk_repo');
    if ($vocabulary) {
        taxonomy_vocabulary_delete($vocabulary->vid);
    }
    db_delete('variable')->condition('name', 'apk_%%', 'LIKE')->execute();
}

/*
 * functions which return apk browser
 * structure and variables will be used
 * for install and uninstall
 */

function _apk_browser_field_vars() {
    $t = get_t();
    //return array of fields variables
    return array(
        'apk_checksum' => array(
            'label' => $t('Checksum')
        ),
        'apk_version' => array(
            'label' => $t('Version')
        ),
        'apk_size' => array(
            'label' => $t('Size')
        ),
        'apk_isize' => array(
            'label' => $t('Installed size')
        ),
        'apk_description' => array(
            'label' => $t('Description'),
            'length' => '1000'
        ),
        'apk_url' => array(
            'label' => $t('URL'),
        ),
        'apk_license' => array(
            'label' => $t('License')
        ),
        'apk_depends' => array(
            'label' => $t('Dependencies'),
            'cdl' => '-1'
        ),
        'apk_maintainer' => array(
            'label' => $t('Maintainer'),
            'type' => 'user_reference',
            'widget' => 'options_select'
        ),
        'apk_contributor' => array(
            'label' => $t('Contributor'),
            'type' => 'user_reference',
            'widget' => 'user_reference_autocomplete',
            'cdl' => '-1'
        )
    );
}