-
Notifications
You must be signed in to change notification settings - Fork 0
/
patternentity.install
176 lines (165 loc) · 4.87 KB
/
patternentity.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
<?php
/**
* @file
* Implements pattern entity.
*/
/**
* Implements hook_install().
* create the public://patternentity folder.
*/
function patternentity_install() {
//create public://patternentity folder.
$public_dir_path = variable_get('file_public_path', conf_path() . '/files');
if (!file_exists($public_dir_path . '/patternentity')) {
if(!drupal_mkdir($public_dir_path . '/patternentity')) {
drupal_set_message(t('cannot create folder in public directory, this should be caused by permission of public directory. e.g. "sites/default/files".'),'error');
}
}
// Check if role patterns_server_user exists
$result = db_select('role', 'r')
->fields('r', array('name'))
->condition('name', 'patterns_server_user', '=')
->execute()
->fetchAssoc();
if (!empty($result['name'])) {
return;
}
// Otherwise creates the role
$role = new stdClass();
$role->name = 'patterns_server_user';
user_role_save($role);
$role = user_role_load_by_name('patterns_server_user');
$role_rid = $role->rid;
// Define our 'editor' role permissions
$role_permissions = array(
'administer patternentity' => TRUE,
'view patternentity' => TRUE,
'upload patternentity' => TRUE,
);
// Grant permissions to our 'editor' role
user_role_change_permissions($role_rid, $role_permissions);
}
/**
* Implements hook_schema().
*/
function patternentity_schema() {
$schema['patternentity'] = array(
'description' => 'Pattern entity for Patterns module',
'fields' => array(
'pid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique pattern id',
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Human readable name.',
),
'description' => array(
'type' => 'text',
'description' => 'Description of the pattern.',
),
'category' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Category of the pattern.',
),
'pattern' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'A serialized array containing pattern code.',
),
'author' => array(
'type' => 'varchar',
'length' => 50,
'description' => 'pattern\'s author',
),
'uploader' => array(
'type' => 'int',
'length' => 50,
'default' => 0,
'nut null' => TRUE,
'description' => 'pattern\'s uploader',
),
'file_name' => array(
'type' => 'varchar',
'length' => 155,
'not null' => TRUE,
'description' => 'Machine readable name of this pattern.',
),
'file_format' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'description' => 'Format of the patterns (XML, YAML, etc.).',
),
'file_path' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Path of the pattern file relative to Drupal root.',
),
'file_downloads' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Download Number.',
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
'description' => 'The Unix timestamp indicating when the pattern file was created.',
),
'uuuid' => array(
'type' => 'varchar',
'length' => 50,
'default' => '',
'description' => 'Identify patterns and their parent',
),
),
'primary key' => array('pid'),
'unique keys' => array('file_name' => array('file_name')),
);
return $schema;
}
/**
* Implements hook_uninstall()
* 1. clean file_managed table.
* 2. clean votingapi_vote table.
* 3. clean all attached field.
*/
function patternentity_uninstall() {
//delete uploaded file, clean file_managed table.
$query = db_select('file_managed', 'fm')
->fields('fm', array('fid'))
->condition('uri', '%' . db_like('patternentity') . '%', 'LIKE')
->execute()
->fetchAll();
$file_fids = array();
foreach ($query as $fid) {
$file_fids[] = $fid->fid;
}
$files = file_load_multiple($file_fids);
foreach ($files as $file) {
file_delete($file);
}
//clean votingapi_vote table.
$num_deleted = db_delete('votingapi_vote')
->condition('entity_type', 'patternentity')
->execute();
$num_deleted = db_delete('votingapi_cache')
->condition('entity_type', 'patternentity')
->execute();
//clean all attached field.
field_attach_delete_bundle('patternentity', 'patternentity');
//delete the role.
user_role_delete('patterns_server_user');
}