-
Notifications
You must be signed in to change notification settings - Fork 2
/
eck.install
240 lines (215 loc) · 7.24 KB
/
eck.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @file
* This install file creates a table for the this module to store information
* about entities. Well, the only information that it needs to store is a name
* and a label for each entity created, the rest of the information is generated
* by the functions.
*
* eck__entity_info
* efacoty__entity_schema
* eck__entity_menu
*
* in eck.module
*/
/**
* TODO: Currently when the module is uninstalled, the tables of Entities that
* were left behind are still around, can we warn the user when they are trying
* to uninstall about the left over entities that are being managed?
*/
/**
* Implements hook_schema().
*
* Create the database table that will store the entities information.
* All that we need for each entity is a name and a label
*/
function eck_schema() {
module_load_include('inc', 'eck', 'eck.entity_type');
$schema = array();
// Schema for the eck table.
$schema['eck'] = array(
'description' => "The base table for entities information",
'fields' => array(
'id' => array(
'description' => "The primary identifier for a(n) entity",
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'name' => array(
'description' => "The machine name of the entity",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
),
'label' => array(
'description' => "The entity's Label",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
)
),
'primary key' => array('id'),
);
$schema['eck_types'] = array(
'description' => "The base table for entities types information",
'fields' => array(
'id' => array(
'description' => "The primary identifier for a(n) entity type",
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'entity' => array(
'description' => "The entity this type belongs to",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
),
'type' => array(
'description' => "The type (bundle) name that will be used",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
),
'label' => array(
'description' => "A human readable name for the bundle (not that the type is not human readable)",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
)
),
'primary key' => array('id'),
'indexes' => array(
'entity_type' => array('entity', 'type'),
),
);
// At the installation stage, eck has not been created, so what is this for?
if (db_table_exists('eck')) {
// When something requests an entity's info, the hook_schema is called to
// get the information about the entity's table, so we need to provide that
// information in the hook.
// Get all the entities that have been create (all the rows in eck table).
$results = db_select('eck', 'e')->fields('e', array('name', 'label'))->execute();
foreach ($results as $result) {
// The function entity_basic_schema defines a standard schema for each
// entity created.
$schema =
array_merge($schema, array("eck_{$result->name}" => eck__entity_type__schema($result->name)));
}
}
return $schema;
}
/**
* Update 7000
*/
function eck_update_7000() {
// To implement bundles (types), I have decided to have one table that will
// hold all of the types information. So for this update we need to create the
// table, and add all of the current entities with their types to the table,
// so we can unify the implementations of bundles in code.
$schema = array(
'description' => "The base table for entities types information",
'fields' => array(
'id' => array(
'description' => "The primary identifier for a(n) entity type",
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'entity' => array(
'description' => "The entity this type belongs to",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
),
'type' => array(
'description' => "The type (bundle) name that will be used",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
),
'label' => array(
'description' => "A human readable name for the bundle (not that the type is not human readable)",
'type' => 'varchar',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE
)
),
'primary key' => array('id'),
'indexes' => array(
'entity_type' => array('entity', 'type'),
),
);
db_create_table('eck_types', $schema);
// Now we add a type (bundle) to the table for each entity in the eck table.
$results = db_select('eck', 't')->fields('t')->execute();
foreach ($results as $record) {
$nid = db_insert('eck_types')
->fields(array(
'entity' => $record->name,
'type' => $record->name,
'label' => $record->label,
))
->execute();
// Also we want to add a field to all the enity tables for the type, and
// populated with the current type.
db_add_field("eck_{$record->name}", 'type', array(
'description' => 'The type of the entity',
'type' => 'varchar',
'default' => '',
'length' => 255,
'not null' => TRUE,
'initial' => $record->name
));
}
}
/**
* Update 7001
*/
function eck_update_7001() {
// For importing and exporting things it is always good to have a uuid, so we
// will add that colunm to the current entity types.
// Now we add a type (bundle) to the table for each entity in the eck table.
$results = db_select('eck', 't')->fields('t')->execute();
foreach ($results as $record) {
// Also we want to add a field to all the enity tables for the type, and
// populated with the current type.
db_add_field("eck_{$record->name}", 'uuid', array(
'type' => 'char',
'length' => 36,
'not null' => TRUE,
'default' => '',
'description' => 'The Universally Unique Identifier.'
));
}
}
function eck_update_7002() {
// The more I think about it, the more it makes sense to replicate the
// "status" property from nodes, this is just a flag that lets the system know
// whether a piece of data should be seen by the public or not. Instead of a
// boolean, I think that a int instead of boolean could be more useful, that
// way more complicated workflows can be implemented. For example if a piece
// of data needs to be reviewed before showing it to the public, and int can
// campture those different states: 0- don't show 1 - show 3 - needs review 4
// - revised by editor, etc. Also instead of status, I think that "state" is a
// more appropiate name.
$results = db_select('eck', 't')->fields('t')->execute();
foreach ($results as $record) {
db_add_field("eck_{$record->name}", 'state', array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The state of the entity'
));
}
}