-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet.install
209 lines (190 loc) · 5.48 KB
/
snippet.install
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
<?php
/**
* @file
* Snippets Schema
*
*/
/**
* Implementation of hook_schema().
*/
function snippet_schema() {
$schema['snippet'] = array(
'description' => t('Text-container exportable definitions.'),
'export' => array(
'key' => 'name',
'identifier' => 'snippet', // Exports will be defined as $snippet
'default hook' => 'default_snippet', // Function hook name.
'api' => array(
'owner' => 'snippet',
'api' => 'default_snippet', // Base name for api include files.
'minimum_version' => 1,
'current_version' => 1,
),
),
'fields' => array(
'sid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
'no export' => TRUE, // Do not export database-only keys.
),
'admin_title' => array(
'type' => 'varchar',
'length' => '255',
'description' => 'Unique ID for text-container exportible. Used to identify them programmatically.',
),
'name' => array(
'type' => 'varchar',
'length' => '255',
'description' => 'Unique ID for text-container exportible. Used to identify them programmatically.',
),
'title' => array(
'type' => 'varchar',
'length' => '255',
'description' => 'Title for snippet.',
),
),
'primary key' => array('sid'),
'foreign keys' => array(
'snippet_revision' => array(
'table' => 'snippet_revision',
'columns' => array('rid' => 'rid'),
),
),
'unique keys' => array(
'name' => array('name'),
),
);
$schema['snippet_revision'] = array(
'description' => t('Stores information about each snippet version.'),
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => '255',
'description' => 'Relationship key.',
),
'rid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary ID for this snippet version.',
),
'title' => array(
'type' => 'varchar',
'length' => '255',
'description' => 'Title for snippet.',
),
'content' => array(
'type' => 'text',
'size' => 'big',
'description' => 'Exportable configuration data.',
),
'content_format' => array(
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'timestamp' => array(
'description' => 'A Unix timestamp indicating when this version was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'is_current' => array(
'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('rid'),
'foreign keys' => array(
'versioned_snippet' => array(
'table' => 'snippet',
'columns' => array('name' => 'name'),
),
),
);
return $schema;
}
/**
* Implementation of hook_install().
*/
function snippet_uninstall() {
drupal_uninstall_schema('snippet');
}
function snippet_update_7001(&$sandbox) {
$content_format = array(
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
);
db_add_field('snippet_revision', 'content_format', $content_format);
}
/**
* Adds the machine_name as name in revision table to hold the revisions
*/
function snippet_update_7002(&$sandbox) {
$snippets_machine_name = array(
'type' => 'varchar',
'length' => '255',
);
//db_add_field('snippet_revision', 'name', $snippets_machine_name);
db_drop_field('snippet_revision', 'sid');
}
/**
* Add name column to revision table
*
*/
function snippet_update_7003(&$sandbox) {
$content_format = array(
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
);
db_add_field('snippet_revision', 'name', $content_format);
}
/**
* Add name column to revision table and Assign permissions to roles
*
*/
function snippet_update_7004(&$sandbox) {
// add field to revision table to have a revision on Title field
$content_format = array(
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
);
db_add_field('snippet_revision', 'title', $content_format);
// update if any snippet has a reviosion
ctools_include('export');
$options = ctools_export_crud_load_all('snippet');
foreach ($options as $key => $value) {
$num_updated = db_update('snippet_revision')
->fields(array(
'title' => $value->title,
))
->condition('name', $key)
->condition('is_current', 1)
->execute();
}
// assign the user permissions
$permissions = user_role_permissions(user_roles());
foreach ($permissions as $rid => $permission) {
$user = user_role_load($rid);
$permission = array_keys($permission);
$check_count = 0;
if (in_array('access snippet list', $permission)) {
$check_count++;
user_role_revoke_permissions($rid, array('access snippet list'));
}
if (in_array('edit snippet content', $permission)) {
$check_count++;
user_role_revoke_permissions($rid, array('edit snippet content'));
}
if ($check_count) {
user_role_grant_permissions($rid, array('manage snippet'));
}
}
}