/[drupal]/contributions/modules/install_profile_api/crud.inc
ViewVC logotype

Contents of /contributions/modules/install_profile_api/crud.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.16 - (show annotations) (download) (as text)
Sat Jan 19 07:05:50 2008 UTC (22 months ago) by adrian
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.15: +5 -3 lines
File MIME type: text/x-php
fix the status not being set by install_add_block, and provide some defaults to the function call
1 <?php
2
3 /* $Id: crud.inc,v 1.15 2007/09/25 19:52:18 tatien Exp $ */
4
5 /* --- NODE / CONTENT --- */
6
7 /**
8 * Given a content_type array, enable the new content type
9 */
10 function install_add_content_type($content_type) {
11 $obj = (object)_node_type_set_defaults($content_type);
12 node_type_save($obj);
13 }
14
15 /* --- USER --- */
16
17 /**
18 * Add a user
19 */
20 function install_add_user($username, $password, $email, $roles = array(), $status = 1) {
21 user_save(
22 new stdClass(),
23 array(
24 'name' => $username,
25 'pass' => $password,
26 'mail' => $email,
27 'roles' => $roles,
28 'status' => $status
29 )
30 );
31 }
32
33 /* --- CONTACT --- */
34
35 /**
36 * Add a new contact category, including recipients and so on
37 */
38 function install_contact_add_category($category, $recipients, $reply = '', $weight = 0, $selected = 0) {
39 $cid = db_next_id("{contact}_cid");
40 db_query("INSERT INTO {contact} (cid, category, recipients, reply, weight, selected) VALUES (%d, category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d)", $cid, $category, $recipients, $reply, $weight, $selected);
41 }
42
43 /* --- PROFILE FUNCTIONS --- */
44
45 /**
46 * Given a funky array of profile fields, create them.
47 */
48 function install_profile_field_add($data) {
49 if (!is_array($data) || !isset($data['title']) || !isset($data['name'])) {
50 return false;
51 }
52 $data['fid'] = db_next_id("{profile_fields}_fid");
53 $data['category'] = ($data['category']) ? $data['category'] : '';
54 $data['type'] = ($data['type']) ? $data['type'] : 'textfield';
55 $data['required'] = ($data['required']) ? $data['required'] : '0';
56 $data['register'] = ($data['register']) ? $data['register'] : '0';
57 $data['visibility'] = ($data['visibility']) ? $data['visibility'] : '0';
58 $data['explanation'] = ($data['explanation']) ? $data['explanation'] : '';
59
60 $fields = array_keys($data);
61
62 // Prepare the query:
63 foreach ($data as $key => $value) {
64 if (in_array((string) $key, $fields)) {
65 $k[] = db_escape_string($key);
66 $v[] = $value;
67 $s[] = "'%s'";
68 }
69 }
70 db_query("INSERT INTO {profile_fields} (". implode(", ", $k) .") VALUES (". implode(", ", $s) .")", $v);
71
72 return $data['fid'];
73 }
74
75 /* --- ROLE --- */
76
77 /**
78 * Add a role to the roles table.
79 */
80 function install_add_role($name) {
81 db_query("INSERT INTO {role} (name) VALUES ('%s')", $name);
82 return install_get_rid($name);
83 }
84
85 /**
86 * Get the role id for the role name
87 */
88 function install_get_rid($name) {
89 return db_result(db_query("SELECT rid FROM {role} WHERE name ='%s' LIMIT 1", $name));
90 }
91
92 /**
93 * Set the permission for a certain role
94 */
95 function install_set_permissions($rid, $perms) {
96 db_query('DELETE FROM {permission} WHERE rid = %d', $rid);
97 db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $perms));
98 }
99
100 /* --- MENU --- */
101
102 /**
103 * Create a new top-level menu
104 * @return integer The database ID of the newly created menu
105 */
106 function install_menu_create_menu($title, $weight = 0) {
107 $mid = db_next_id('{menu}_mid');
108 // Check explicitly for mid <= 2. If the database was improperly prefixed,
109 // this would cause a nasty infinite loop or duplicate mid errors.
110 // TODO: have automatic prefixing through an installer to prevent this.
111 while ($mid <= 2) {
112 $mid = db_next_id('{menu}_mid');
113 }
114 db_query("INSERT INTO {menu} (mid, pid, title, weight, type) VALUES (%d, 0, '%s', %d, 115)", $mid, $title, $weight);
115 menu_rebuild(); // not sure if this is needed, but we've seen problems without it
116 return $mid; // this is important to add new items to this menu.
117 }
118
119 /**
120 * Get the menu ID, searching on path
121 * @return integer The database ID of a menu item based on its path
122 */
123 function install_menu_get_mid($path) {
124 menu_rebuild(); // not sure if this is needed, but we've seen problems without it
125 return db_result(db_query("SELECT mid FROM {menu} WHERE path = '%s' LIMIT 1", $path));
126 }
127
128 /**
129 * Get the menu ID of a root menu, based on title, e.g. Secondary links
130 * @return integer The database ID of the root menu that matches the $title
131 */
132 function install_menu_get_root_menu($title) {
133 return db_result(db_query("SELECT mid FROM {menu} WHERE title = '%s' LIMIT 1", $title));
134 }
135
136 /**
137 * Set an existing menu ID to a new parent
138 */
139 function install_menu_set_menu($mid, $pid, $weight = 0, $type = 54) {
140 db_query("UPDATE {menu} SET pid = %d, type = %d, weight = %d WHERE mid = %d", $pid, $type, $weight, $mid);
141 menu_rebuild(); // not sure if this is needed, but we've seen problems without it
142 }
143
144 /**
145 * Create a new menu item
146 * @param $path Path of the new menu item
147 * @param $title Title of the menu item (visible label for menu)
148 * @param $pid Parent ID -- which menu the item is being added to
149 * @param $description Description of the menu item (tooltip)
150 * @param $weight Weight for positioning
151 * @param $type Menu item type; new items are 118 by default
152 * @return integer The database ID of the menu item
153 */
154 function install_menu_create_menu_item($path, $title, $pid, $description = '', $weight = 0, $type = 118) {
155 $menu = array(
156 'path' => $path,
157 'title' => st($title),
158 'pid' => $pid,
159 'description' => st($description),
160 'weight' => $weight,
161 'type' => $type
162 );
163 menu_save_item($menu);
164
165 menu_rebuild(); // not sure if this is needed, but we've seen problems without it
166
167 return install_menu_get_mid($path); // this is important to add new items to this menu.
168 }
169
170 /**
171 * Creates a batch of menu items
172 * @param $items An array containing menu items (as arrays)
173 * @param $pid Parent ID -- which menu the items are being added to
174 */
175 function install_menu_create_menu_items($items, $pid) {
176 foreach ($items as $item) {
177 $mid = install_menu_create_menu_item($item['path'], $item['title'], $pid, $item['description'], $item['weight'], $item['type']);
178 if (isset($items['children'])) {
179 foreach ($item['children'] as $children) {
180 install_menu_create_menu_items($children, $mid);
181 }
182 }
183 }
184 }
185
186 /**
187 * Remove the specified filter from the specified format
188 * @param $mid The ID of the menu item to disable
189 *
190 * NOTE: the module name + the delta is what uniquely identifies a filter
191 */
192 function install_menu_disable_item($mid) {
193 $item = menu_get_item($mid);
194 $type = $item['type'];
195 $type &= ~MENU_VISIBLE_IN_TREE;
196 $type &= ~MENU_VISIBLE_IN_BREADCRUMB;
197 $type |= MENU_MODIFIED_BY_ADMIN;
198 db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $mid);
199 drupal_set_message(t('The menu item has been disabled.'));
200 // No redirection during install
201 //drupal_goto('admin/build/menu');
202 }
203
204 /* --- BLOCKS --- */
205
206 /**
207 * Creates a new block.
208 */
209 function install_add_block($module, $delta, $theme, $status, $weight, $region, $visibility = 0, $pages = '', $custom = 0, $throttle = 0, $title = '') {
210 db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title)
211 VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')",
212 $module, $delta, $theme, $status, $weight, $region, $visibility, $pages, $custom, $throttle, $title);
213 if ($module == 'block') {
214 $box = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid=%d', $delta));
215 db_query("INSERT INTO {boxes} (bid, body, info, format) VALUES (%d, '%s', '%s', '%s')", $box->bid, $box->body, $box->info, $box->format);
216 }
217 }
218
219 /**
220 * Creates a new block role.
221 */
222 function install_add_block_role($module, $delta, $rid) {
223 db_query("INSERT INTO {blocks_roles} (module,delta,rid) VALUES ('%s', '%s', %d)", $module, $delta, $rid);
224 }
225
226 /* --- TINYMCE --- */
227
228 /**
229 * Add roles to an existing TinyMCE profile identified by the profile name
230 */
231 function install_tinymce_add_roles($name, $roles) {
232 foreach ($roles as $role) {
233 db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $name, $role);
234 }
235 }
236
237 /**
238 * Create a new TinyMCE profile and set the settings
239 * @param $name A text string identifying the profile
240 * @param $settings An associative array containing key value pairs
241 */
242 function install_tinymce_create_profile($name, $settings) {
243 db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $name, serialize($settings));
244 }
245
246 /* --- TAXONOMY --- */
247
248 /**
249 * Given the name of a vocabulary, return its Vocab ID
250 * @param $name A text string identifying the vocabulary
251 */
252 function install_get_vid($name) {
253 // not guaranteed to be unique, hence the LIMIT
254 return db_result(db_query("SELECT vid FROM {vocabulary} WHERE name = '%s' LIMIT 1", $name));
255 };
256
257 /* --- FILTER --- */
258
259 /**
260 * Set the roles that can be used with the filter
261 * @param $roles An array of role IDs
262 * @param $format_id An integer of the format ID
263 */
264 function install_format_set_roles($roles, $format_id) {
265 $roles = implode(',',$roles);
266 db_query("UPDATE {filter_formats} SET roles = '%s' WHERE format = %d", $roles, $format_id);
267 }
268
269 /**
270 * Add a new input format
271 * @param $name The human-readable name of the new format
272 * @param $cache If this format is cacheable
273 */
274 function install_add_format($name, $cache = 1) {
275 $format_id = db_next_id('{filter_formats}_fid');
276 // Check explicitly for format_id <= 3. If the database was improperly prefixed,
277 // this would cause a nasty infinite loop or duplicate mid errors.
278 // TODO: have automatic prefixing through an installer to prevent this.
279 while ($format_id <= 3) {
280 $format_id = db_next_id('{filter_formats}_fid');
281 }
282 db_query("INSERT INTO {filter_formats} (`format`, `name`, `roles`, `cache`) VALUES (%d, '%s', '', %d)", $format_id, $name, $cache);
283 return $format_id;
284 }
285
286 /**
287 * Remove the specified filter from the specified format
288 * @param $format_id The ID of the format to remove the filter from
289 * @param $module The module this filter belongs to
290 * @param $delta The delta of this filter
291 *
292 * NOTE: the module name + the delta is what uniquely identifies a filter
293 */
294 function install_remove_filter($format_id, $module, $delta) {
295 db_query("DELETE FROM {filters} WHERE format = %d AND module = '%s' AND delta = %d", $format_id, $module, $delta);
296 }
297
298 /**
299 * Add a filter to an input format
300 * @param $format_id The ID of the format to add the filter to
301 * @param $module The module this filter belongs to
302 * @param $delta The delta of this filter
303 * @param $weight The weight to be applied to this filter
304 */
305 function install_add_filter($format_id, $module, $delta = 0, $weight = 0) {
306 db_query("INSERT INTO {filters} (`format`, `module`, `delta`, `weight`) VALUES (%d, '%s', %d, %d)", $format_id, $module, $delta, $weight);
307 }
308
309 /* --- THEME --- */
310
311 /**
312 * Example usage:
313 * // Set site theme
314 * install_disable_theme("garland");
315 * install_default_theme("mytheme");
316 */
317
318 /**
319 * Enable theme
320 * @param $theme Unique string that is the name of theme
321 */
322 function install_enable_theme($theme) {
323 system_theme_data();
324 db_query("UPDATE {system} SET status = 1 WHERE type = 'theme' and name = '%s'", $theme);
325 system_initialize_theme_blocks($theme);
326 }
327
328 /**
329 * Disable theme
330 * @param $theme Unique string that is the name of theme
331 */
332 function install_disable_theme($theme) {
333 system_theme_data();
334 db_query("UPDATE {system} SET status = 0 WHERE type = 'theme' and name ='%s'", $theme);
335 }
336
337 /**
338 * Set default theme
339 * @param $theme Unique string that is the name of theme
340 */
341 function install_default_theme($theme) {
342 install_enable_theme($theme);
343 variable_set('theme_default', $theme);
344 }
345
346 /**
347 * Set admin theme
348 * @param $theme Unique string that is the name of theme
349 */
350 function install_admin_theme($theme) {
351 variable_set('admin_theme', $theme);
352 }

  ViewVC Help
Powered by ViewVC 1.1.2