1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Implementation of hook_install(). |
---|
6 | */ |
---|
7 | function userreference_install() { |
---|
8 | drupal_load('module', 'content'); |
---|
9 | content_notify('install', 'userreference'); |
---|
10 | } |
---|
11 | |
---|
12 | /** |
---|
13 | * Implementation of hook_uninstall(). |
---|
14 | */ |
---|
15 | function userreference_uninstall() { |
---|
16 | drupal_load('module', 'content'); |
---|
17 | content_notify('uninstall', 'userreference'); |
---|
18 | } |
---|
19 | |
---|
20 | /** |
---|
21 | * Implementation of hook_enable(). |
---|
22 | * |
---|
23 | * Notify content module when this module is enabled. |
---|
24 | */ |
---|
25 | function userreference_enable() { |
---|
26 | drupal_load('module', 'content'); |
---|
27 | content_notify('enable', 'userreference'); |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Implementation of hook_disable(). |
---|
32 | * |
---|
33 | * Notify content module when this module is disabled. |
---|
34 | */ |
---|
35 | function userreference_disable() { |
---|
36 | drupal_load('module', 'content'); |
---|
37 | content_notify('disable', 'userreference'); |
---|
38 | } |
---|
39 | |
---|
40 | function userreference_update_last_removed() { |
---|
41 | return 4; |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Placeholder update to set newly installed versions to the latest update number. |
---|
46 | */ |
---|
47 | function userreference_update_6000() { |
---|
48 | if ($abort = content_check_update('userreference')) { |
---|
49 | return $abort; |
---|
50 | } |
---|
51 | |
---|
52 | $ret = array(); |
---|
53 | return $ret; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Create an index by user reference column for all fields. |
---|
58 | */ |
---|
59 | function userreference_update_6001(&$sandbox) { |
---|
60 | include_once('./'. drupal_get_path('module', 'content') .'/content.install'); |
---|
61 | drupal_load('module', 'content'); |
---|
62 | |
---|
63 | $ret = array(); |
---|
64 | |
---|
65 | if (!isset($sandbox['progress'])) { |
---|
66 | if ($abort = content_check_update('userreference')) { |
---|
67 | return $abort; |
---|
68 | } |
---|
69 | |
---|
70 | // Get the latest cache values and schema. |
---|
71 | content_clear_type_cache(TRUE, TRUE); |
---|
72 | $types = content_types_install(); |
---|
73 | |
---|
74 | if (empty($types)) { |
---|
75 | return $ret; |
---|
76 | } |
---|
77 | |
---|
78 | $sandbox['fields'] = array(); |
---|
79 | foreach ($types as $type_name => $fields) { |
---|
80 | foreach ($fields as $field) { |
---|
81 | if ($field['type'] == 'userreference') { |
---|
82 | $sandbox['fields'][] = $field; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if (empty($sandbox['fields'])) { |
---|
88 | return $ret; |
---|
89 | } |
---|
90 | |
---|
91 | $sandbox['progress'] = 0; |
---|
92 | $sandbox['visited'] = array(); |
---|
93 | } |
---|
94 | |
---|
95 | $field = $sandbox['fields'][$sandbox['progress']]; |
---|
96 | |
---|
97 | // We only want to process a field once -- if we hit it a second time, |
---|
98 | // that means it's its own table and it should have already been updated. |
---|
99 | if (!in_array($field['field_name'], $sandbox['visited'])) { |
---|
100 | $db_info = content_database_info($field); |
---|
101 | $table = $db_info['table']; |
---|
102 | $attributes = $db_info['columns']['uid']; |
---|
103 | $column = $attributes['column']; |
---|
104 | if (!content_db_index_exists($table, $column)) { |
---|
105 | db_add_index($ret, $table, $column, array($column)); |
---|
106 | } |
---|
107 | $sandbox['visited'][] = $field['field_name']; |
---|
108 | } |
---|
109 | |
---|
110 | $sandbox['progress']++; |
---|
111 | $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']); |
---|
112 | |
---|
113 | return $ret; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Convert 'referenceable_status' option from array to integer to match the |
---|
118 | * change in the field settings form where the element has been changed from |
---|
119 | * a checkboxes element (array) to a radios element (integer). |
---|
120 | * |
---|
121 | * Reference: @link http://drupal.org/node/416134 @endlink |
---|
122 | */ |
---|
123 | function userreference_update_6002() { |
---|
124 | $ret = array(); |
---|
125 | |
---|
126 | drupal_load('module', 'content'); |
---|
127 | |
---|
128 | $result = db_query("SELECT field_name, global_settings FROM {". content_field_tablename() ."} WHERE type = 'userreference'"); |
---|
129 | while ($userreference = db_fetch_object($result)) { |
---|
130 | $global_settings = unserialize($userreference->global_settings); |
---|
131 | |
---|
132 | if (isset($global_settings['referenceable_status']) && is_array($global_settings['referenceable_status'])) { |
---|
133 | $referenceable_status = array_filter($global_settings['referenceable_status']); |
---|
134 | $global_settings['referenceable_status'] = (!empty($referenceable_status) ? 1 : ''); |
---|
135 | |
---|
136 | // We can't use update_sql() here because of curly braces in serialized |
---|
137 | // array. |
---|
138 | db_query("UPDATE {". content_field_tablename() ."} SET global_settings = '%s' WHERE field_name = '%s'", serialize($global_settings), $userreference->field_name); |
---|
139 | $ret[] = array( |
---|
140 | 'success' => TRUE, |
---|
141 | 'query' => t("The 'referenceable_status' option for %field has been fixed.", array('%field' => $userreference->field_name)), |
---|
142 | ); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | // Rebuild content caches only if necessary. |
---|
147 | if (!empty($ret)) { |
---|
148 | content_clear_type_cache(); |
---|
149 | } |
---|
150 | |
---|
151 | return $ret; |
---|
152 | } |
---|