| 1 |
|
<?php |
| 2 |
|
|
| 3 |
|
/* $Id: crud.inc,v 1.1.2.1 2007/08/28 23:29:47 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 |
|
* Remove the specified filter from the specified format |
| 171 |
|
* @param $mid The ID of the menu item to disable |
| 172 |
|
* |
| 173 |
|
* NOTE: the module name + the delta is what uniquely identifies a filter |
| 174 |
|
*/ |
| 175 |
|
function install_menu_disable_item($mid) { |
| 176 |
|
$item = menu_get_item($mid); |
| 177 |
|
$type = $item['type']; |
| 178 |
|
$type &= ~MENU_VISIBLE_IN_TREE; |
| 179 |
|
$type &= ~MENU_VISIBLE_IN_BREADCRUMB; |
| 180 |
|
$type |= MENU_MODIFIED_BY_ADMIN; |
| 181 |
|
db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $mid); |
| 182 |
|
drupal_set_message(t('The menu item has been disabled.')); |
| 183 |
|
// No redirection during install |
| 184 |
|
//drupal_goto('admin/build/menu'); |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
/* --- TINYMCE --- */ |
| 188 |
|
|
| 189 |
|
/** |
| 190 |
|
* Add roles to an existing TinyMCE profile identified by the profile name |
| 191 |
|
*/ |
| 192 |
|
function install_tinymce_add_roles($name, $roles) { |
| 193 |
|
foreach ($roles as $role) { |
| 194 |
|
db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $name, $role); |
| 195 |
|
} |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
/** |
| 199 |
|
* Create a new TinyMCE profile and set the settings |
| 200 |
|
* @param $name A text string identifying the profile |
| 201 |
|
* @param $settings An associative array containing key value pairs |
| 202 |
|
*/ |
| 203 |
|
function install_tinymce_create_profile($name, $settings) { |
| 204 |
|
db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $name, serialize($settings)); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
/* --- TAXONOMY --- */ |
| 208 |
|
|
| 209 |
|
/** |
| 210 |
|
* Given the name of a vocabulary, return its Vocab ID |
| 211 |
|
* @param $name A text string identifying the vocabulary |
| 212 |
|
*/ |
| 213 |
|
function install_get_vid($name) { |
| 214 |
|
// not guaranteed to be unique, hence the LIMIT |
| 215 |
|
return db_result(db_query("SELECT vid FROM {vocabulary} WHERE name = '%s' LIMIT 1", $name)); |
| 216 |
|
}; |
| 217 |
|
|
| 218 |
|
/* --- FILTER --- */ |
| 219 |
|
|
| 220 |
|
/** |
| 221 |
|
* Set the roles that can be used with the filter |
| 222 |
|
* @param $roles An array of role IDs |
| 223 |
|
* @param $format_id An integer of the format ID |
| 224 |
|
*/ |
| 225 |
|
function install_format_set_roles($roles, $format_id) { |
| 226 |
|
$roles = implode(',',$roles); |
| 227 |
|
db_query("UPDATE {filter_formats} SET roles = '%s' WHERE format = %d", $roles, $format_id); |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
/** |
| 231 |
|
* Add a new input format |
| 232 |
|
* @param $name The human-readable name of the new format |
| 233 |
|
* @param $cache If this format is cacheable |
| 234 |
|
*/ |
| 235 |
|
function install_add_format($name, $cache = 1) { |
| 236 |
|
$format_id = db_next_id('{filter_formats}_fid'); |
| 237 |
|
// Check explicitly for format_id <= 3. If the database was improperly prefixed, |
| 238 |
|
// this would cause a nasty infinite loop or duplicate mid errors. |
| 239 |
|
// TODO: have automatic prefixing through an installer to prevent this. |
| 240 |
|
while ($format_id <= 3) { |
| 241 |
|
$format_id = db_next_id('{filter_formats}_fid'); |
| 242 |
|
} |
| 243 |
|
db_query("INSERT INTO {filter_formats} (`format`, `name`, `roles`, `cache`) VALUES (%d, '%s', '', %d)", $format_id, $name, $cache); |
| 244 |
|
return $format_id; |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
/** |
| 248 |
|
* Remove the specified filter from the specified format |
| 249 |
|
* @param $format_id The ID of the format to remove the filter from |
| 250 |
|
* @param $module The module this filter belongs to |
| 251 |
|
* @param $delta The delta of this filter |
| 252 |
|
* |
| 253 |
|
* NOTE: the module name + the delta is what uniquely identifies a filter |
| 254 |
|
*/ |
| 255 |
|
function install_remove_filter($format_id, $module, $delta) { |
| 256 |
|
db_query("DELETE FROM {filters} WHERE format = %d AND module = '%s' AND delta = %d", $format_id, $module, $delta); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
/** |
| 260 |
|
* Add a filter to an input format |
| 261 |
|
* @param $format_id The ID of the format to add the filter to |
| 262 |
|
* @param $module The module this filter belongs to |
| 263 |
|
* @param $delta The delta of this filter |
| 264 |
|
* @param $weight The weight to be applied to this filter |
| 265 |
|
*/ |
| 266 |
|
function install_add_filter($format_id, $module, $delta = 0, $weight = 0) { |
| 267 |
|
db_query("INSERT INTO {filters} (`format`, `module`, `delta`, `weight`) VALUES (%d, '%s', %d, %d)", $format_id, $module, $delta, $weight); |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
/* --- THEME --- */ |
| 271 |
|
|
| 272 |
|
/** |
| 273 |
|
* Example usage: |
| 274 |
|
* // Set site theme |
| 275 |
|
* install_disable_theme("garland"); |
| 276 |
|
* install_default_theme("mytheme"); |
| 277 |
|
*/ |
| 278 |
|
|
| 279 |
|
/** |
| 280 |
|
* Enable theme |
| 281 |
|
* @param $theme Unique string that is the name of theme |
| 282 |
|
*/ |
| 283 |
|
function install_enable_theme($theme) { |
| 284 |
|
system_theme_data(); |
| 285 |
|
db_query("UPDATE {system} SET status = 1 WHERE type = 'theme' and name = '%s'", $theme); |
| 286 |
|
system_initialize_theme_blocks($theme); |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
/** |
| 290 |
|
* Disable theme |
| 291 |
|
* @param $theme Unique string that is the name of theme |
| 292 |
|
*/ |
| 293 |
|
function install_disable_theme($theme) { |
| 294 |
|
system_theme_data(); |
| 295 |
|
db_query("UPDATE {system} SET status = 0 WHERE type = 'theme' and name ='%s'", $theme); |
| 296 |
|
} |
| 297 |
|
|
| 298 |
|
/** |
| 299 |
|
* Set default theme |
| 300 |
|
* @param $theme Unique string that is the name of theme |
| 301 |
|
*/ |
| 302 |
|
function install_default_theme($theme) { |
| 303 |
|
install_enable_theme($theme); |
| 304 |
|
variable_set('theme_default', $theme); |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
/** |
| 308 |
|
* Set admin theme |
| 309 |
|
* @param $theme Unique string that is the name of theme |
| 310 |
|
*/ |
| 311 |
|
function install_admin_theme($theme) { |
| 312 |
|
variable_set('admin_theme', $theme); |
| 313 |
|
} |
| 314 |
|
|