| 1 |
<?php
|
| 2 |
|
| 3 |
/* $Id: crud.inc,v 1.1 2008/07/20 16:47:47 alexb Exp $ */
|
| 4 |
|
| 5 |
// Borrowed from install_profile_api :)
|
| 6 |
|
| 7 |
// Allow crud to run error free if called during a normal bootstrap. Primarily for st()
|
| 8 |
include_once('includes/install.inc');
|
| 9 |
|
| 10 |
/* --- NODE / CONTENT --- */
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Given a content_type array, enable the new content type
|
| 14 |
*/
|
| 15 |
function install_add_content_type($content_type) {
|
| 16 |
install_create_content_type($content_type['type'], $content_type['name'], $content_type);
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Given a content_type name, and an array of custom properties, enable
|
| 21 |
* a new content type. This function is written to be self-documenting,
|
| 22 |
* as it explicitly lists the properties you can set, and shows the defaults
|
| 23 |
* if you don't set them.
|
| 24 |
*
|
| 25 |
* @param $content_type Required. Type id, only alpha-numeric and underscores.
|
| 26 |
* @param $content_name Required. A descriptive name.
|
| 27 |
* @param $properties Optional. An array of properties that describe the new node type.
|
| 28 |
*/
|
| 29 |
function install_create_content_type($content_type, $content_name, $properties = array()) {
|
| 30 |
|
| 31 |
$default = array(
|
| 32 |
'type' => $content_type, // No default, must pass a content type id name
|
| 33 |
'name' => st($content_name), // No default, must pass a content type display name
|
| 34 |
'module' => 'node',
|
| 35 |
'description' => st(''),
|
| 36 |
'custom' => TRUE,
|
| 37 |
'modified' => TRUE,
|
| 38 |
'has_title' => TRUE,
|
| 39 |
'has_body' => TRUE,
|
| 40 |
'title_label' => 'Title',
|
| 41 |
'body_label' => '',
|
| 42 |
'locked' => FALSE,
|
| 43 |
'min_word_count' => '0',
|
| 44 |
'orig_type' => '',
|
| 45 |
'help' => '',
|
| 46 |
);
|
| 47 |
$type = array_merge($default, $properties);
|
| 48 |
|
| 49 |
$type = (object)_node_type_set_defaults($type);
|
| 50 |
node_type_save($type);
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Create a new field for an existing content type.
|
| 55 |
*
|
| 56 |
* @param $content_type Required. Existing content type (text id).
|
| 57 |
* @param $field_name Required. A field name using only alpha-numeric and underscores.
|
| 58 |
* @param $field_widget_type Required. A special name that uniquely identifies both the field type
|
| 59 |
* and widget type. Each option item on the first page of the 'Add Field' form.
|
| 60 |
* @param $properties Optional. An array of additional properties for the field.
|
| 61 |
*
|
| 62 |
* NOTE: Use install add_existing_field() to add an existing field.
|
| 63 |
*
|
| 64 |
* TIP: You can call print_r(install_get_widget_types()); to display a list of available
|
| 65 |
* $field_widget_type options.
|
| 66 |
*/
|
| 67 |
function install_create_field($content_type, $field_name, $field_widget_type, $label, $properties = array()) {
|
| 68 |
|
| 69 |
include_once(drupal_get_path('module', 'content') .'/content_admin.inc');
|
| 70 |
|
| 71 |
// Manage weight, so that fields are ordered as they are created.
|
| 72 |
// Pass 'weight' => ##, on the properties array to reset.
|
| 73 |
static $weight = 1;
|
| 74 |
$weight = isset($properties['weight']) ? $properties['weight'] : $weight + 1;
|
| 75 |
|
| 76 |
$widget_types = install_get_widget_types();
|
| 77 |
if (!array_key_exists($field_widget_type, $widget_types)) {
|
| 78 |
drupal_set_message("'$field_widget_type' is not an available field/widget.");
|
| 79 |
return;
|
| 80 |
}
|
| 81 |
|
| 82 |
// First step of creating the field - create the basic field.
|
| 83 |
// Corresponds to first page of the Add field UI.
|
| 84 |
$default_field_add = array(
|
| 85 |
'type_name' => $content_type,
|
| 86 |
'field_name' => $field_name,
|
| 87 |
'field_widget_type' => $field_widget_type,
|
| 88 |
'label' => $label,
|
| 89 |
'op' => 'Create field',
|
| 90 |
'submit' => 'Create field',
|
| 91 |
'form_id' => '_content_admin_field_add_new',
|
| 92 |
);
|
| 93 |
// Create our new field.
|
| 94 |
_content_admin_field_add_new_submit('_content_admin_field_add_new', $default_field_add);
|
| 95 |
|
| 96 |
// Second step, update the field with our custom properties.
|
| 97 |
// Corresponds to the second page (general config page) of content type creation.
|
| 98 |
|
| 99 |
// We define defaults for a number of different field/widget types (eg. "referenceable_types"
|
| 100 |
// is applicable to nodereference. These are ignored in cck when not applicable.
|
| 101 |
$default_field_edit = array(
|
| 102 |
'type_name' => $content_type,
|
| 103 |
'field_name' => $field_name,
|
| 104 |
'label' => $label,
|
| 105 |
'weight' => $weight,
|
| 106 |
'description' => '',
|
| 107 |
'default_value_php' => '',
|
| 108 |
'group' => 0,
|
| 109 |
'required' => 0,
|
| 110 |
'multiple' => FALSE,
|
| 111 |
'text_processing' => 0,
|
| 112 |
'max_length' => '',
|
| 113 |
'allowed_values' => '',
|
| 114 |
'allowed_values_php' => '',
|
| 115 |
'rows' => 1,
|
| 116 |
'op' => 'Save field settings',
|
| 117 |
'submit' => 'Save field settings',
|
| 118 |
'form_id' => '_content_admin_field',
|
| 119 |
'referenceable_types' => array(), // Used in nodereference fields and userreference
|
| 120 |
);
|
| 121 |
foreach($default_field_edit AS $key => $value) {
|
| 122 |
if (isset($properties[$key])) {
|
| 123 |
$default_field_edit[$key] = $properties[$key];
|
| 124 |
}
|
| 125 |
}
|
| 126 |
if (isset($properties['default_value'])) {
|
| 127 |
$default_field_edit['default_value'][0]['value'] = $properties['default_value'];
|
| 128 |
}
|
| 129 |
|
| 130 |
// Derive the widget and type from the input type.
|
| 131 |
$widget_parts = explode('-', $field_widget_type);
|
| 132 |
$default_field_edit['field_type'] = $widget_parts[0];
|
| 133 |
$default_field_edit['widget_type'] = $widget_parts[1];
|
| 134 |
$default_field_edit['module'] = $widget_parts[0] .', optionwidgets';
|
| 135 |
|
| 136 |
_content_admin_field_submit('_content_admin_field_add_new', $default_field_edit);
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Returns an array of available fieldtype-widgettype arguments to
|
| 141 |
* the $field_widget_type parameter of install_create_field()
|
| 142 |
*/
|
| 143 |
function install_get_widget_types() {
|
| 144 |
$field_types = _content_field_types();
|
| 145 |
$widget_types = _content_widget_types();
|
| 146 |
$field_type_options = array();
|
| 147 |
foreach ($field_types as $field_name => $field_type) {
|
| 148 |
foreach ($widget_types as $widget_name => $widget_type) {
|
| 149 |
if (in_array($field_name, $widget_type['field types'])) {
|
| 150 |
$field_type_options[$field_name .'-'. $widget_name] = $field_type['label'] .': '. $widget_type['label'];
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
return $field_type_options;
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Add an existing field to another content type.
|
| 159 |
*
|
| 160 |
* @param $content_type Required. Existing content type (text id).
|
| 161 |
* @param $field_name Required. Existing field name (text id).
|
| 162 |
*/
|
| 163 |
function install_add_existing_field($content_type, $field_name) {
|
| 164 |
// Defaults for passing to submit function.
|
| 165 |
$form_values = array(
|
| 166 |
'field_name' => $field_name,
|
| 167 |
'type_name' => $content_type,
|
| 168 |
'op' => 'Add field',
|
| 169 |
'submit' => 'Add field',
|
| 170 |
);
|
| 171 |
|
| 172 |
_content_admin_field_add_existing_submit('_content_admin_field_add_existing', $form_values);
|
| 173 |
}
|
| 174 |
|
| 175 |
/**
|
| 176 |
* Create a new field group for an existing content type.
|
| 177 |
*
|
| 178 |
* @param $content_type Required. Existing content type (text id).
|
| 179 |
* @param $group_name An alpha-numeric/underscore group name.
|
| 180 |
* @param $label Required. A nice text name for your group.
|
| 181 |
* @param $settings Optional. An array of settings, see the default array
|
| 182 |
* defined in the function for an example.
|
| 183 |
* @param $weight Optional, a standard weight integer.
|
| 184 |
*
|
| 185 |
*/
|
| 186 |
function install_create_field_group($content_type, $group_name, $label, $settings = array(), $weight = 0) {
|
| 187 |
if (!is_array($settings)) {
|
| 188 |
// Default settings array.
|
| 189 |
$settings = array(
|
| 190 |
'form' => array('style' => 'fieldset', 'description' => ''),
|
| 191 |
'display' => array('description' => '', 'teaser' => NULL, 'label' => NULL),
|
| 192 |
);
|
| 193 |
}
|
| 194 |
db_query("INSERT INTO {node_group} (type_name, group_name, label, settings, weight)
|
| 195 |
VALUES ('%s', '%s', '%s', '%s', %d)",
|
| 196 |
$content_type, $group_name, $label, serialize($settings), $weight);
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Assign fields to a field group.
|
| 201 |
*
|
| 202 |
* @param $content_type Required. Existing content type (text id).
|
| 203 |
* @param $group_name Required. Existing group name (text id).
|
| 204 |
* @param $fields Required. An array of field names.
|
| 205 |
*/
|
| 206 |
function install_fields_in_group($content_type, $group_name, $fields) {
|
| 207 |
if (!is_array($fields)) {
|
| 208 |
$fields = array($fields);
|
| 209 |
}
|
| 210 |
foreach ($fields AS $field_name) {
|
| 211 |
db_query("INSERT INTO {node_group_fields} (type_name, group_name, field_name)
|
| 212 |
VALUES ('%s', '%s', '%s')",
|
| 213 |
$content_type, $group_name, $field_name);
|
| 214 |
}
|
| 215 |
}
|
| 216 |
|
| 217 |
/* --- USER --- */
|
| 218 |
|
| 219 |
/**
|
| 220 |
* Add a user
|
| 221 |
*/
|
| 222 |
function install_add_user($username, $password, $email, $roles = array(), $status = 1) {
|
| 223 |
user_save(
|
| 224 |
new stdClass(),
|
| 225 |
array(
|
| 226 |
'name' => $username,
|
| 227 |
'pass' => $password,
|
| 228 |
'mail' => $email,
|
| 229 |
'roles' => $roles,
|
| 230 |
'status' => $status
|
| 231 |
)
|
| 232 |
);
|
| 233 |
}
|
| 234 |
|
| 235 |
/* --- CONTACT --- */
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Add a new contact category, including recipients and so on
|
| 239 |
*/
|
| 240 |
function install_contact_add_category($category, $recipients, $reply = '', $weight = 0, $selected = 0) {
|
| 241 |
db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $category, $recipients, $reply, $weight, $selected);
|
| 242 |
}
|
| 243 |
|
| 244 |
/* --- PROFILE FUNCTIONS --- */
|
| 245 |
|
| 246 |
/**
|
| 247 |
* Given a funky array of profile fields, create them.
|
| 248 |
*/
|
| 249 |
function install_profile_field_add($data) {
|
| 250 |
if (!is_array($data) || !isset($data['title']) || !isset($data['name'])) {
|
| 251 |
return false;
|
| 252 |
}
|
| 253 |
$data['fid'] = db_next_id("{profile_fields}_fid");
|
| 254 |
$data['category'] = ($data['category']) ? $data['category'] : '';
|
| 255 |
$data['type'] = ($data['type']) ? $data['type'] : 'textfield';
|
| 256 |
$data['required'] = ($data['required']) ? $data['required'] : '0';
|
| 257 |
$data['register'] = ($data['register']) ? $data['register'] : '0';
|
| 258 |
$data['visibility'] = ($data['visibility']) ? $data['visibility'] : '0';
|
| 259 |
$data['explanation'] = ($data['explanation']) ? $data['explanation'] : '';
|
| 260 |
|
| 261 |
$fields = array_keys($data);
|
| 262 |
|
| 263 |
// Prepare the query:
|
| 264 |
foreach ($data as $key => $value) {
|
| 265 |
if (in_array((string) $key, $fields)) {
|
| 266 |
$k[] = db_escape_string($key);
|
| 267 |
$v[] = $value;
|
| 268 |
$s[] = "'%s'";
|
| 269 |
}
|
| 270 |
}
|
| 271 |
db_query("INSERT INTO {profile_fields} (". implode(", ", $k) .") VALUES (". implode(", ", $s) .")", $v);
|
| 272 |
|
| 273 |
return $data['fid'];
|
| 274 |
}
|
| 275 |
|
| 276 |
/* --- ROLE --- */
|
| 277 |
|
| 278 |
/**
|
| 279 |
* Add a role to the roles table.
|
| 280 |
*/
|
| 281 |
function install_add_role($name) {
|
| 282 |
db_query("INSERT INTO {role} (name) VALUES ('%s')", $name);
|
| 283 |
return install_get_rid($name);
|
| 284 |
}
|
| 285 |
|
| 286 |
/**
|
| 287 |
* Get the role id for the role name
|
| 288 |
*/
|
| 289 |
function install_get_rid($name) {
|
| 290 |
return db_result(db_query("SELECT rid FROM {role} WHERE name ='%s' LIMIT 1", $name));
|
| 291 |
}
|
| 292 |
|
| 293 |
/**
|
| 294 |
* Set the permission for a certain role
|
| 295 |
*/
|
| 296 |
function install_set_permissions($rid, $perms) {
|
| 297 |
db_query('DELETE FROM {permission} WHERE rid = %d', $rid);
|
| 298 |
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $perms));
|
| 299 |
}
|
| 300 |
|
| 301 |
/* --- MENU --- */
|
| 302 |
|
| 303 |
/**
|
| 304 |
* Create a new top-level menu
|
| 305 |
*
|
| 306 |
* @return integer The database ID of the newly created menu
|
| 307 |
*/
|
| 308 |
function install_menu_create_menu($title, $weight = 0) {
|
| 309 |
$mid = db_next_id('{menu}_mid');
|
| 310 |
// Check explicitly for mid <= 2. If the database was improperly prefixed,
|
| 311 |
// this would cause a nasty infinite loop or duplicate mid errors.
|
| 312 |
// TODO: have automatic prefixing through an installer to prevent this.
|
| 313 |
while ($mid <= 2) {
|
| 314 |
$mid = db_next_id('{menu}_mid');
|
| 315 |
}
|
| 316 |
db_query("INSERT INTO {menu} (mid, pid, title, weight, type) VALUES (%d, 0, '%s', %d, 115)", $mid, $title, $weight);
|
| 317 |
// not sure if this is needed, but we've seen problems without it.
|
| 318 |
menu_rebuild();
|
| 319 |
// this is important to add new items to this menu.
|
| 320 |
return $mid;
|
| 321 |
}
|
| 322 |
|
| 323 |
/**
|
| 324 |
* Get the menu ID, searching on path
|
| 325 |
*
|
| 326 |
* @return integer The database ID of a menu item based on its path
|
| 327 |
*/
|
| 328 |
function install_menu_get_mid($path) {
|
| 329 |
// not sure if this is needed, but we've seen problems without it.
|
| 330 |
menu_rebuild();
|
| 331 |
return db_result(db_query("SELECT mid FROM {menu} WHERE path = '%s' LIMIT 1", $path));
|
| 332 |
}
|
| 333 |
|
| 334 |
/**
|
| 335 |
* Get the menu ID of a root menu, based on title, e.g. Secondary links
|
| 336 |
*
|
| 337 |
* @return integer The database ID of the root menu that matches the $title
|
| 338 |
*/
|
| 339 |
function install_menu_get_root_menu($title) {
|
| 340 |
return db_result(db_query("SELECT mid FROM {menu} WHERE title = '%s' LIMIT 1", $title));
|
| 341 |
}
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Set an existing menu ID to a new parent
|
| 345 |
*/
|
| 346 |
function install_menu_set_menu($mid, $pid, $weight = 0, $type = 54) {
|
| 347 |
db_query("UPDATE {menu} SET pid = %d, type = %d, weight = %d WHERE mid = %d", $pid, $type, $weight, $mid);
|
| 348 |
menu_rebuild(); // not sure if this is needed, but we've seen problems without it.
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Create a new menu item
|
| 353 |
*
|
| 354 |
* @param $path Path of the new menu item
|
| 355 |
* @param $title Title of the menu item (visible label for menu)
|
| 356 |
* @param $pid Parent ID -- which menu the item is being added to
|
| 357 |
* @param $description Description of the menu item (tooltip)
|
| 358 |
* @param $weight Weight for positioning
|
| 359 |
* @param $type Menu item type; new items are 118 by default
|
| 360 |
* @return integer The database ID of the menu item
|
| 361 |
*/
|
| 362 |
function install_menu_create_menu_item($path, $title, $pid, $description = '', $weight = 0, $type = 118) {
|
| 363 |
$menu = array(
|
| 364 |
'path' => $path,
|
| 365 |
'title' => st($title),
|
| 366 |
'pid' => $pid,
|
| 367 |
'description' => st($description),
|
| 368 |
'weight' => $weight,
|
| 369 |
'type' => $type
|
| 370 |
);
|
| 371 |
menu_save_item($menu);
|
| 372 |
|
| 373 |
menu_rebuild(); // not sure if this is needed, but we've seen problems without it
|
| 374 |
|
| 375 |
return $menu['mid']; // this is important to add new items to this menu.
|
| 376 |
}
|
| 377 |
|
| 378 |
/**
|
| 379 |
* Creates a batch of menu items
|
| 380 |
* @param $items An array containing menu items (as arrays)
|
| 381 |
* @param $pid Parent ID -- which menu the items are being added to
|
| 382 |
*/
|
| 383 |
function install_menu_create_menu_items($items, $pid) {
|
| 384 |
foreach ($items as $item) {
|
| 385 |
$mid = install_menu_create_menu_item($item['path'], $item['title'], $pid, $item['description'], $item['weight'], $item['type']);
|
| 386 |
if (isset($item['children'])) {
|
| 387 |
install_menu_create_menu_items($item['children'], $mid);
|
| 388 |
}
|
| 389 |
}
|
| 390 |
}
|
| 391 |
|
| 392 |
/**
|
| 393 |
* Update a menu item.
|
| 394 |
*
|
| 395 |
* @param $mid Menu item id
|
| 396 |
* @param $properties A keyed array of only the values you want to change.
|
| 397 |
*
|
| 398 |
* @return FALSE if menu does not exist, otherwise return the status returned
|
| 399 |
* by menu_save_item() in menu.module
|
| 400 |
*/
|
| 401 |
function install_menu_update_menu_item($mid, $properties) {
|
| 402 |
$existing_item = menu_get_item($mid);
|
| 403 |
if (empty($existing_item)) {
|
| 404 |
drupal_set_message(t('Menu item '. $mid .' did not exist in install_menu_update_menu().'));
|
| 405 |
return FALSE;
|
| 406 |
}
|
| 407 |
$edit = array_merge($existing_item, $properties);
|
| 408 |
$edit['mid'] = $mid;
|
| 409 |
// Tell menu this is an updated item.
|
| 410 |
$edit['type'] = 54;
|
| 411 |
|
| 412 |
$status = menu_save_item($edit);
|
| 413 |
menu_rebuild();
|
| 414 |
return $status;
|
| 415 |
}
|
| 416 |
|
| 417 |
/**
|
| 418 |
* Remove the specified filter from the specified format
|
| 419 |
* @param $mid The ID of the menu item to disable
|
| 420 |
*
|
| 421 |
* NOTE: the module name + the delta is what uniquely identifies a filter
|
| 422 |
*/
|
| 423 |
function install_menu_disable_item($mid) {
|
| 424 |
$item = menu_get_item($mid);
|
| 425 |
$type = $item['type'];
|
| 426 |
$type &= ~MENU_VISIBLE_IN_TREE;
|
| 427 |
$type &= ~MENU_VISIBLE_IN_BREADCRUMB;
|
| 428 |
$type |= MENU_MODIFIED_BY_ADMIN;
|
| 429 |
db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $mid);
|
| 430 |
drupal_set_message(t('The menu item has been disabled.'));
|
| 431 |
// No redirection during install
|
| 432 |
//drupal_goto('admin/build/menu');
|
| 433 |
}
|
| 434 |
|
| 435 |
/* --- BLOCKS --- */
|
| 436 |
|
| 437 |
/**
|
| 438 |
* Creates a new block.
|
| 439 |
*/
|
| 440 |
function install_add_block($module, $delta, $theme, $status, $weight, $region, $visibility, $pages, $custom, $throttle, $title) {
|
| 441 |
db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $module, $delta, $theme, $status, $weight, $region, $visibility, $pages, $custom, $throttle, $title);
|
| 442 |
if ($module == 'block') {
|
| 443 |
$box = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid=%d', $delta));
|
| 444 |
db_query("INSERT INTO {boxes} (bid, body, info, format) VALUES (%d, '%s', '%s', '%s')", $box->bid, $box->body, $box->info, $box->format);
|
| 445 |
}
|
| 446 |
}
|
| 447 |
|
| 448 |
/**
|
| 449 |
* Position an existing block inside a region of a theme.
|
| 450 |
*
|
| 451 |
* @param $theme A theme name, eg. 'garland'
|
| 452 |
* @param $region Available region: usually one of 'header', 'footer', 'left', 'right', 'content'
|
| 453 |
* @param $module The name of the module that provides the block
|
| 454 |
* @param $delta The block id.
|
| 455 |
* @param $weight Block order within the region.
|
| 456 |
*
|
| 457 |
* TIP: To identify the $module and $delta, go to an existing site
|
| 458 |
* and visit the /admin/build/block page. Hover over the 'configure'
|
| 459 |
* links - the module and delta are the last two parts of the
|
| 460 |
* target url.
|
| 461 |
*/
|
| 462 |
function install_set_block($theme, $region, $module, $delta, $weight = 0) {
|
| 463 |
block_admin_display_submit('block_admin_display', array(array('theme' => $theme, 'region' => $region, 'module' => $module, 'delta' => $delta, 'weight' => $weight)));
|
| 464 |
}
|
| 465 |
|
| 466 |
/**
|
| 467 |
* Creates a new block role.
|
| 468 |
*/
|
| 469 |
function install_add_block_role($module, $delta, $rid) {
|
| 470 |
db_query("INSERT INTO {blocks_roles} (module,delta,rid) VALUES ('%s', '%s', %d)", $module, $delta, $rid);
|
| 471 |
}
|
| 472 |
|
| 473 |
/* --- TINYMCE --- */
|
| 474 |
|
| 475 |
/**
|
| 476 |
* Add roles to an existing TinyMCE profile identified by the profile name
|
| 477 |
*/
|
| 478 |
function install_tinymce_add_roles($name, $roles) {
|
| 479 |
foreach ($roles as $role) {
|
| 480 |
db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $name, $role);
|
| 481 |
}
|
| 482 |
}
|
| 483 |
|
| 484 |
/**
|
| 485 |
* Create a new TinyMCE profile and set the settings
|
| 486 |
*
|
| 487 |
* @param $name A text string identifying the profile
|
| 488 |
* @param $settings An associative array containing key value pairs
|
| 489 |
*/
|
| 490 |
function install_tinymce_create_profile($name, $settings) {
|
| 491 |
db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $name, serialize($settings));
|
| 492 |
}
|
| 493 |
|
| 494 |
/* --- TAXONOMY --- */
|
| 495 |
|
| 496 |
/**
|
| 497 |
* Given the name of a vocabulary, return its Vocab ID
|
| 498 |
*
|
| 499 |
* @param $name A text string identifying the vocabulary
|
| 500 |
*/
|
| 501 |
function install_get_vid($name) {
|
| 502 |
// not guaranteed to be unique, hence the LIMIT
|
| 503 |
return db_result(db_query("SELECT vid FROM {vocabulary} WHERE name = '%s' LIMIT 1", $name));
|
| 504 |
};
|
| 505 |
|
| 506 |
/**
|
| 507 |
* Create a new taxonomy vocabulary
|
| 508 |
*
|
| 509 |
* @param $vocab_name The vocabulary name
|
| 510 |
* @param $properties Optional. An array of additional properties.
|
| 511 |
* @param $node_types Optional. An array of content types.
|
| 512 |
* @return integer The database ID of the created vocabulary.
|
| 513 |
*/
|
| 514 |
function install_add_vocabulary($vocab_name, $properties = array(), $content_types = array()) {
|
| 515 |
// Copy values to respective keys, as per taxonomy module.
|
| 516 |
$content_types = array_flip($content_types);
|
| 517 |
foreach ($content_types AS $type => $ignore_key) {
|
| 518 |
$content_types[$type] = $type;
|
| 519 |
}
|
| 520 |
// Default properties so you don't have to pass anything.
|
| 521 |
$default = array(
|
| 522 |
'name' => $vocab_name,
|
| 523 |
'description' => '',
|
| 524 |
'help' => '',
|
| 525 |
'hierarchy' => 0, // 0 = disabled; 1 = single; 2 = multiple
|
| 526 |
'relations' => FALSE,
|
| 527 |
'tags' => FALSE,
|
| 528 |
'required' => FALSE,
|
| 529 |
'weight' => 0,
|
| 530 |
'op' => 'Submit',
|
| 531 |
'submit' => 'Submit',
|
| 532 |
);
|
| 533 |
|
| 534 |
$form_values = array_merge($default, $properties);
|
| 535 |
$form_values['nodes'] = $content_types;
|
| 536 |
taxonomy_save_vocabulary($form_values);
|
| 537 |
return $form_values['vid'];
|
| 538 |
}
|
| 539 |
|
| 540 |
/**
|
| 541 |
* Create a new taxonomy term
|
| 542 |
* @param $vocab The vocabulary ID
|
| 543 |
* @param $name The text name of the new term
|
| 544 |
* @param $description Term description
|
| 545 |
* @return integer The database ID of the created term
|
| 546 |
*/
|
| 547 |
function install_add_term($vocab, $name, $parent, $description) {
|
| 548 |
$form_values = array(
|
| 549 |
'name' => $name,
|
| 550 |
'description' => $description,
|
| 551 |
'parent' => array($parent),
|
| 552 |
'vid' => $vocab,
|
| 553 |
'synonyms' => '',
|
| 554 |
'op' => 'Submit',
|
| 555 |
'weight' => 0,
|
| 556 |
);
|
| 557 |
taxonomy_save_term($form_values);
|
| 558 |
return $form_values['tid'];
|
| 559 |
}
|
| 560 |
|
| 561 |
/**
|
| 562 |
* Assign a term to a node.
|
| 563 |
* @param $vocab The vocabulary ID
|
| 564 |
* @param $name The text name of the new term
|
| 565 |
*
|
| 566 |
* NOTE: does not check whether the assignment is valid.
|
| 567 |
*/
|
| 568 |
function install_assign_nid_tid($nid, $tid) {
|
| 569 |
if (!$tid || !$nid) {
|
| 570 |
return;
|
| 571 |
}
|
| 572 |
db_query('DELETE FROM {term_node} WHERE nid = %d AND tid = %d', $nid, $tid);
|
| 573 |
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
|
| 574 |
}
|
| 575 |
|
| 576 |
/* --- FILTER --- */
|
| 577 |
|
| 578 |
/**
|
| 579 |
* Set the roles that can be used with the filter
|
| 580 |
* @param $roles An array of role IDs
|
| 581 |
* @param $format_id An integer of the format ID
|
| 582 |
*/
|
| 583 |
function install_format_set_roles($roles, $format_id) {
|
| 584 |
$roles = implode(',',$roles);
|
| 585 |
db_query("UPDATE {filter_formats} SET roles = '%s' WHERE format = %d", $roles, $format_id);
|
| 586 |
}
|
| 587 |
|
| 588 |
/**
|
| 589 |
* Add a new input format
|
| 590 |
* @param $name The human-readable name of the new format
|
| 591 |
* @param $cache If this format is cacheable
|
| 592 |
*/
|
| 593 |
function install_add_format($name, $cache = 1) {
|
| 594 |
$format_id = db_next_id('{filter_formats}_fid');
|
| 595 |
// Check explicitly for format_id <= 3. If the database was improperly prefixed,
|
| 596 |
// this would cause a nasty infinite loop or duplicate mid errors.
|
| 597 |
// TODO: have automatic prefixing through an installer to prevent this.
|
| 598 |
while ($format_id <= 3) {
|
| 599 |
$format_id = db_next_id('{filter_formats}_fid');
|
| 600 |
}
|
| 601 |
db_query("INSERT INTO {filter_formats} (`format`, `name`, `roles`, `cache`) VALUES (%d, '%s', '', %d)", $format_id, $name, $cache);
|
| 602 |
return $format_id;
|
| 603 |
}
|
| 604 |
|
| 605 |
/**
|
| 606 |
* Remove the specified filter from the specified format
|
| 607 |
*
|
| 608 |
* @param $format_id The ID of the format to remove the filter from
|
| 609 |
* @param $module The module this filter belongs to
|
| 610 |
* @param $delta The delta of this filter
|
| 611 |
*
|
| 612 |
* NOTE: the module name + the delta is what uniquely identifies a filter
|
| 613 |
*/
|
| 614 |
function install_remove_filter($format_id, $module, $delta) {
|
| 615 |
db_query("DELETE FROM {filters} WHERE format = %d AND module = '%s' AND delta = %d", $format_id, $module, $delta);
|
| 616 |
}
|
| 617 |
|
| 618 |
/**
|
| 619 |
* Add a filter to an input format
|
| 620 |
*
|
| 621 |
* @param $format_id The ID of the format to add the filter to
|
| 622 |
* @param $module The module this filter belongs to
|
| 623 |
* @param $delta The delta of this filter
|
| 624 |
* @param $weight The weight to be applied to this filter
|
| 625 |
*/
|
| 626 |
function install_add_filter($format_id, $module, $delta = 0, $weight = 0) {
|
| 627 |
db_query("INSERT INTO {filters} (`format`, `module`, `delta`, `weight`) VALUES (%d, '%s', %d, %d)", $format_id, $module, $delta, $weight);
|
| 628 |
}
|
| 629 |
|
| 630 |
/* --- THEME --- */
|
| 631 |
|
| 632 |
/**
|
| 633 |
* Example usage:
|
| 634 |
* // Set site theme
|
| 635 |
* install_disable_theme("garland");
|
| 636 |
* install_default_theme("mytheme");
|
| 637 |
*/
|
| 638 |
|
| 639 |
/**
|
| 640 |
* Enable theme
|
| 641 |
*
|
| 642 |
* @param $theme Unique string that is the name of theme
|
| 643 |
*/
|
| 644 |
function install_enable_theme($theme) {
|
| 645 |
system_theme_data();
|
| 646 |
db_query("UPDATE {system} SET status = 1 WHERE type = 'theme' and name = '%s'", $theme);
|
| 647 |
system_initialize_theme_blocks($theme);
|
| 648 |
}
|
| 649 |
|
| 650 |
/**
|
| 651 |
* Disable theme
|
| 652 |
*
|
| 653 |
* @param $theme Unique string that is the name of theme
|
| 654 |
*/
|
| 655 |
function install_disable_theme($theme) {
|
| 656 |
system_theme_data();
|
| 657 |
db_query("UPDATE {system} SET status = 0 WHERE type = 'theme' and name ='%s'", $theme);
|
| 658 |
}
|
| 659 |
|
| 660 |
/**
|
| 661 |
* Set default theme
|
| 662 |
*
|
| 663 |
* @param $theme Unique string that is the name of theme
|
| 664 |
*/
|
| 665 |
function install_default_theme($theme) {
|
| 666 |
install_enable_theme($theme);
|
| 667 |
variable_set('theme_default', $theme);
|
| 668 |
}
|
| 669 |
|
| 670 |
/**
|
| 671 |
* Set admin theme
|
| 672 |
*
|
| 673 |
* @param $theme Unique string that is the name of theme
|
| 674 |
*/
|
| 675 |
function install_admin_theme($theme) {
|
| 676 |
variable_set('admin_theme', $theme);
|
| 677 |
}
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
/* --- WORKFLOW AND ACTIONS --- */
|
| 682 |
|
| 683 |
/*
|
| 684 |
* NOTE: It's been found that workflow implements very clean CRUD style
|
| 685 |
* functions. The wrappers implemented here should serve as pointers to
|
| 686 |
* the existence of these functions, sort of an education. Since these are
|
| 687 |
* just wrappers, keep the space used to a minimum, please review the
|
| 688 |
* workflow and actions modules for documentation of their functions.
|
| 689 |
*
|
| 690 |
* Might be worth more discussion: remove these wrappers? add docs?.
|
| 691 |
*/
|
| 692 |
|
| 693 |
/**
|
| 694 |
* Create a workflow.
|
| 695 |
*
|
| 696 |
* @param $name Display name for workflow.
|
| 697 |
* @param $roles An array of role IDs
|
| 698 |
*/
|
| 699 |
function install_workflow_create($name, $roles) {
|
| 700 |
$wid = workflow_create($name);
|
| 701 |
workflow_update($wid, $name, $roles);
|
| 702 |
return $wid;
|
| 703 |
}
|
| 704 |
|
| 705 |
/**
|
| 706 |
* Create workflow-to-content-type mappings.
|
| 707 |
*
|
| 708 |
* @param $content_to_workflows
|
| 709 |
* Keyed array: 'content_type' => 'workflow_id'
|
| 710 |
*
|
| 711 |
*/
|
| 712 |
function install_workflow_type_map_create($content_type, $workflow_id) {
|
| 713 |
// Prevent workflow module destroying existing associations. Oh well.
|
| 714 |
$type_map = array();
|
| 715 |
$result = db_query("SELECT wid, type FROM {workflow_type_map}");
|
| 716 |
while ($data = db_fetch_object($result)) {
|
| 717 |
$type_map[$data->type] = $data->wid;
|
| 718 |
}
|
| 719 |
$type_map[$content_type] = $workflow_id;
|
| 720 |
workflow_types_save($type_map);
|
| 721 |
}
|
| 722 |
|
| 723 |
/**
|
| 724 |
* Create a workflow state
|
| 725 |
*/
|
| 726 |
function install_workflow_create_state($wid, $name, $weight = 0, $sysid = 0) {
|
| 727 |
return workflow_state_save(array('wid' => $wid, 'state' => $name, 'sysid' => $sysid, 'weight' => $weight));
|
| 728 |
}
|
| 729 |
|
| 730 |
/**
|
| 731 |
* Create a workflow transition
|
| 732 |
*/
|
| 733 |
function install_workflow_add_transition_role($from, $to, $role) {
|
| 734 |
workflow_transition_add_role($from, $to, $role);
|
| 735 |
}
|
| 736 |
|
| 737 |
/**
|
| 738 |
* Create an configured action.
|
| 739 |
*
|
| 740 |
* @param $function The function name that implements an action, eg. 'action_send_email'
|
| 741 |
* @param $description The description for this instance of an action
|
| 742 |
* @param $params A keyed array of configuration parameters.
|
| 743 |
*
|
| 744 |
* NOTE: Non-configurable actions (eg. 'action_node_publish') do not need to be created.
|
| 745 |
*/
|
| 746 |
function install_action_create_action($function, $description, $params) {
|
| 747 |
// $type is not configurable in the admin, so hide it from the install api and assign dynamically.
|
| 748 |
$actions = actions_list();
|
| 749 |
$type = $actions[$function]['type'];
|
| 750 |
return actions_save($function, $type, $params, $description);
|
| 751 |
}
|
| 752 |
|
| 753 |
/**
|
| 754 |
* Register an action to a transition.
|
| 755 |
*/
|
| 756 |
function install_workflow_add_transition_action($transition_id, $action_id) {
|
| 757 |
workflow_actions_save($transition_id, $action_id);
|
| 758 |
}
|
| 759 |
|