Fixed #884820 by boran, dragonwize: Fix i18nstrings user translation function.
[project/better_formats.git] / better_formats.install
1 <?php
2
3 /**
4 * @file
5 * Installs the better_formats module.
6 *
7 * Creates a database for use of multi-layered default formats and sets
8 * default settings.
9 */
10
11 /**
12 * Implementation of hook_enable().
13 */
14 function better_formats_enable() {
15 better_formats_check_roles();
16 }
17
18 /**
19 * Implementation of hook_schema().
20 */
21 function better_formats_schema() {
22 $schema['better_formats_defaults'] = array(
23 'fields' => array(
24 'rid' => array(
25 'type' => 'int',
26 'size' => 'normal',
27 'unsigned' => TRUE,
28 'not null' => TRUE,
29 ),
30 'type' => array(
31 'type' => 'varchar',
32 'length' => 255,
33 'not null' => TRUE,
34 ),
35 'format' => array(
36 'type' => 'int',
37 'size' => 'medium',
38 'unsigned' => TRUE,
39 'not null' => TRUE,
40 ),
41 'type_weight' => array(
42 'type' => 'int',
43 'size' => 'tiny',
44 'default' => 0,
45 'unsigned' => TRUE,
46 'not null' => TRUE,
47 ),
48 'weight' => array(
49 'type' => 'int',
50 'size' => 'tiny',
51 'default' => 0,
52 'unsigned' => FALSE,
53 'not null' => TRUE,
54 ),
55 ),
56 'primary key' => array('rid', 'type'),
57 );
58
59 return $schema;
60 }
61
62 /**
63 * Implementation of hook_install().
64 */
65 function better_formats_install() {
66 // Create tables.
67 drupal_install_schema('better_formats');
68
69 // Increase module weight to prevent compatibility issues.
70 $sql = "UPDATE {system}
71 SET weight = 100
72 WHERE name = 'better_formats'";
73 db_query($sql);
74
75 // Insert format defaults.
76 $roles = user_roles();
77 $sql = "INSERT INTO {better_formats_defaults}
78 VALUES (%d, '%s', %d, %d, %d)";
79 foreach ($roles as $rid => $role) {
80 db_query($sql, $rid, 'node', 0, 1, 0);
81 db_query($sql, $rid, 'comment', 0, 1, 0);
82 db_query($sql, $rid, 'block', 0, 1, 25);
83 }
84
85 // Set default perms to be like core defaults.
86 $default_perms = ', show format selection for nodes, show format selection for comments, show format selection for blocks, show format tips, show more format tips link, collapsible format selection, collapse format fieldset by default';
87 // Get current core perms.
88 $sql = "SELECT *
89 FROM {permission}
90 WHERE rid IN (1,2)";
91 $result = db_query($sql);
92 $role_perms = array();
93 while ($row = db_fetch_object($result)) {
94 $role_perms[$row->rid] = $row;
95 }
96 // If a role has no permissions set it will not have a row in the database.
97 // Assume that roles do not need perms set for BF as well if none are set.
98 if (!empty($role_perms)) {
99 // Add perms to core roles (anonymous user, authenticated user).
100 foreach ($role_perms as $perms) {
101 $sql = "UPDATE {permission}
102 SET perm = '%s'
103 WHERE pid = %d";
104 db_query($sql, $perms->perm . $default_perms, $perms->pid);
105 }
106 // Clear the cache.
107 cache_clear_all();
108 }
109 }
110
111 /**
112 * Implementation of hook_uninstall().
113 */
114 function better_formats_uninstall() {
115 // Remove tables.
116 drupal_uninstall_schema('better_formats');
117
118 // Delete settings from varible table.
119 $sql = "DELETE FROM {variable}
120 WHERE name LIKE 'better_formats%'";
121 db_query($sql);
122 }
123
124 /**
125 * Update from 1.0 to 1.1.
126 */
127 function better_formats_update_6110() {
128 $ret = array();
129
130 // Insert block format defaults.
131 $roles = user_roles();
132 $sql = "INSERT INTO {better_formats_defaults}
133 VALUES (%d, '%s', %d, %d, %d)";
134 foreach ($roles as $rid => $role) {
135 $result = db_query($sql, $rid, 'block', 0, 1, 25);
136 $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
137 }
138
139 // Split show format selection permission.
140 // Get permissions by role.
141 $sql = "SELECT *
142 FROM {permission}";
143 $result = db_query($sql);
144 $row_perms = array();
145 while ($row = db_fetch_object($result)) {
146 $role_perms[$row->rid] = $row;
147 }
148 if (!empty($role_perms)) {
149 // Add perms to core roles (anonymous user, authenticated user).
150 foreach ($role_perms as $perms) {
151 // Only enable spit permissions if previous was enabled.
152 $replace = ', show format selection for nodes, show format selection for comments, show format selection for blocks,';
153 $perms->perm = str_replace(', show format selection,', $replace, $perms->perm);
154 $sql = "UPDATE {permission}
155 SET perm = '%s'
156 WHERE pid = %d";
157 $result = db_query($sql, $perms->perm, $perms->pid);
158 $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
159 }
160 // Clear the cache.
161 cache_clear_all();
162 }
163
164 return $ret;
165 }