forked from lem-doghouse/govCMS-CKAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgovcms_ckan.install
More file actions
196 lines (177 loc) · 5.04 KB
/
Copy pathgovcms_ckan.install
File metadata and controls
196 lines (177 loc) · 5.04 KB
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
<?php
/**
* @file
* Code for the govCMS CKAN module install/uninstall/schema hooks.
*/
/**
* Implements hook_field_schema().
*/
function govcms_ckan_field_schema($field) {
$schema = array();
switch ($field['type']) {
case 'ckan_data':
$schema['columns'] = array(
'dataset_id' => array(
'type' => 'varchar',
'description' => 'The unique identifier of the dataset.',
'length' => '255',
'not null' => FALSE,
),
'dataset_title' => array(
'type' => 'varchar',
'description' => 'The title of the dataset.',
'length' => '255',
'not null' => FALSE,
),
);
break;
case 'ckan_graph_type':
$schema['columns'] = array(
'type' => array(
'type' => 'varchar',
'description' => 'The type of graph',
'length' => '255',
'not null' => FALSE,
),
'orientation' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'xaxis_label' => array(
'type' => 'varchar',
'description' => 'The x-axis graph label',
'length' => '255',
'not null' => FALSE,
'default' => '',
),
'yaxis_label' => array(
'type' => 'varchar',
'description' => 'The y-axis graph label',
'length' => '255',
'not null' => FALSE,
'default' => '',
),
);
break;
}
return $schema;
}
/**
* Implements hook_install().
*/
function govcms_ckan_install() {
module_load_include('inc', 'node', 'content_types');
db_update('system')
->condition('name', 'govcms_ckan')
->condition('type', 'module')
->fields(array('weight' => -1))
->execute();
// Create the graph type.
$graph_type = array(
'type' => 'ckan_graph',
'name' => st('CKAN Graph'),
'base' => 'node_content',
'description' => st('Create a new graph that can be attach to a node or wysiwyg.'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);
$graph_type = node_type_set_defaults($graph_type);
node_type_save($graph_type);
// We enable the CKAN graph by default.
variable_set('govcms_ckan_graph_node_ckan_graph', TRUE);
govcms_ckan_create_graph_data_field('node', 'ckan_graph');
govcms_ckan_create_graph_type_field('node', 'ckan_graph');
}
/**
* Creates the CKAN data field and attaches it to a bundle.
*
* @param string $entity_type
* The entity type name.
* @param string $bundle
* The bundle name.
*/
function govcms_ckan_create_graph_data_field($entity_type, $bundle) {
$field_name = 'ckan_graph_data';
$field_exists = (field_info_field($field_name) !== NULL);
$field_instance_exists = (field_info_instance($entity_type, $field_name, $bundle) !== NULL);
if (!$field_exists) {
$field = array(
'field_name' => $field_name,
'type' => 'ckan_data',
'cardinality' => '-1',
);
$field = field_create_field($field);
}
if (!empty($field) && !$field_instance_exists) {
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'description' => 'A field for storing a reference to a CKAN dataset',
'label' => 'CKAN Data',
'required' => 1,
'active' => 1,
'widget' => array(
'type' => 'ckan_data',
),
);
field_create_instance($instance);
}
}
/**
* Creates the CKAN graph type field and attaches it to a bundle.
*
* @param string $entity_type
* The entity type name.
* @param string $bundle
* The bundle name.
*/
function govcms_ckan_create_graph_type_field($entity_type, $bundle) {
$field_name = 'ckan_graph_type';
$field_exists = (field_info_field($field_name) !== NULL);
$field_instance_exists = (field_info_instance($entity_type, $field_name, $bundle) !== NULL);
if (!$field_exists) {
$field = array(
'field_name' => $field_name,
'type' => 'ckan_graph_type',
);
$field = field_create_field($field);
}
if (!empty($field) && !$field_instance_exists) {
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'description' => 'A field for storing CKAN graph type',
'label' => 'CKAN Graph type',
'required' => 1,
'active' => 1,
'widget' => array(
'type' => 'ckan_graph_type',
),
);
field_create_instance($instance);
}
}
/**
* Implements hook_uninstall().
*/
function govcms_ckan_uninstall() {
variable_del('govcms_ckan_endpoint_url');
variable_del('govcms_ckan_api_key');
foreach (node_type_get_names() as $type => $name) {
variable_del('govcms_ckan_graph_node_' . $type);
}
// We delete the CKAN graph content type if there is no existing nodes.
$query = new EntityFieldQuery();
$results = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'ckan_graph')
->range(0, 1)
->execute();
if (!$results) {
node_type_delete('ckan_graph');
drupal_flush_all_caches();
}
}