View source
<?php
function webform_install() {
$success = TRUE;
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$success = $success && db_query("CREATE TABLE if not exists {webform} (\n nid int(10) unsigned NOT NULL default '0',\n confirmation text,\n redirect_post int(1) unsigned not null default '0',\n submit_limit int(2) not null default '-1',\n submit_interval int(10) not null default '157784630',\n email varchar(255) default NULL,\n email_from_name varchar(255) default NULL,\n email_from_address varchar(255) default NULL,\n email_subject varchar(255) default NULL,\n additional_validate text default NULL,\n additional_submit text default NULL,\n PRIMARY KEY (nid)\n ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */");
$success = $success && db_query("CREATE TABLE if not exists {webform_component} (\n nid int(10) unsigned NOT NULL default '0',\n cid int(10) unsigned NOT NULL default '0',\n pid int(10) unsigned NOT NULL default '0',\n form_key varchar(128) default NULL,\n name varchar(128) default NULL,\n type varchar(16) default NULL,\n value text default NULL,\n extra text,\n mandatory int(2) unsigned default NULL,\n weight int(2) default NULL,\n PRIMARY KEY (nid,cid)\n ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */");
$success = $success && db_query("CREATE TABLE if not exists {webform_submissions} (\n nid int(10) unsigned NOT NULL default '0',\n sid int(10) unsigned NOT NULL default '0',\n uid int(10) unsigned NOT NULL default '0',\n submitted int(11) NOT NULL default '0',\n remote_addr varchar(128),\n PRIMARY KEY (nid,sid),\n KEY sid (sid)\n ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */");
$success = $success && db_query("CREATE TABLE if not exists {webform_submitted_data} (\n nid int(10) unsigned NOT NULL default '0',\n sid int(10) unsigned NOT NULL default '0',\n cid int(10) unsigned NOT NULL default '0',\n no int(10) unsigned NOT NULL default '0',\n data longtext,\n PRIMARY KEY (nid,sid,cid,no),\n KEY sid (sid)\n ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */");
break;
case 'pgsql':
$success = $success && db_query("CREATE TABLE {webform} (\n nid integer NOT NULL default '0',\n confirmation text NOT NULL default '',\n redirect_post smallint NOT NULL default '0',\n submit_limit smallint NOT NULL default '-1',\n submit_interval integer NOT NULL default '157784630',\n email varchar(255) NOT NULL default '',\n email_from_name varchar(255) NOT NULL default '',\n email_from_address varchar(255) NOT NULL default '',\n email_subject varchar(255) NOT NULL default '',\n additional_validate text default NULL,\n additional_submit text default NULL,\n PRIMARY KEY (nid)\n )");
$success = $success && db_query("CREATE TABLE {webform_component} (\n nid integer NOT NULL default '0',\n cid integer NOT NULL default '0',\n pid integer NOT NULL default '0',\n form_key varchar(128) default NULL,\n name varchar(128) NOT NULL default '',\n type varchar(16) NOT NULL default '',\n value text NOT NULL default '',\n extra text NOT NULL default '',\n mandatory smallint NOT NULL default '0',\n weight smallint NOT NULL default '0',\n PRIMARY KEY (nid,cid)\n )");
$success = $success && db_query("CREATE TABLE {webform_submissions} (\n nid integer NOT NULL default '0',\n sid serial UNIQUE,\n uid integer NOT NULL default '0',\n submitted integer NOT NULL default '0',\n remote_addr varchar(128) NOT NULL default '',\n PRIMARY KEY (nid,sid)\n )");
$success = $success && db_query("CREATE INDEX {webform_submissions}_sid_idx ON {webform_submissions}(sid)");
$success = $success && db_query("CREATE TABLE {webform_submitted_data} (\n nid integer NOT NULL default '0',\n sid integer NOT NULL default '0',\n cid integer NOT NULL default '0',\n no integer NOT NULL default '0',\n data text NOT NULL default '',\n PRIMARY KEY (nid,sid,cid,no)\n )");
$success = $success && db_query("CREATE INDEX {webform_submitted_data}_sid_idx ON {webform_submitted_data}(sid)");
break;
}
if ($success) {
drupal_set_message(t('Webform module installed module tables successfully.'));
}
else {
drupal_set_message(t('The installation of webform module was unsuccessful.'), 'error');
}
}
function webform_update_1() {
$ret = array();
$installed_version = variable_get("webform_version", 0);
$current_version = array(
'text' => '4.6.3',
'build' => '463',
);
if ($installed_version > 0) {
$ret = _webform_legacy_update($installed_version, $current_version);
}
$ret[] = update_sql("CREATE TABLE {webform_tmp} ( " . " nid int(10) unsigned NOT NULL default '0', " . " sid int(10) unsigned NOT NULL default '0', " . " cid int(10) unsigned NOT NULL default '0', " . " no int(10) unsigned NOT NULL default '0', " . " data longtext, " . " PRIMARY KEY (nid,sid,cid,no) " . " ) ");
$result = db_query("SELECT ws.nid, ws.sid, wc.cid, ws.name, ws.data " . " FROM {webform_submitted_data} ws, {webform_component} wc " . " WHERE ws.nid = wc.nid " . " AND ws.name = wc.name ");
while ($row = db_fetch_object($result)) {
$data = unserialize($row->data);
if (is_array($data)) {
foreach ($data as $k => $v) {
db_query("INSERT INTO {webform_tmp} (nid, sid, cid, no, data) VALUES (%d, %d, %d, %d, '%s')", $row->nid, $row->sid, $row->cid, $k, $v);
}
}
else {
db_query("INSERT INTO {webform_tmp} (nid, sid, cid, no, data) VALUES (%d, %d, %d, %d, '%s')", $row->nid, $row->sid, $row->cid, 0, $row->data);
}
}
$ret[] = update_sql("DROP TABLE {webform_submitted_data}");
$ret[] = update_sql("ALTER TABLE {webform_tmp} RENAME TO {webform_submitted_data}");
$ret[] = update_sql("CREATE TABLE {webform_submissions} ( " . " nid int(10) unsigned NOT NULL default '0', " . " sid int(10) unsigned NOT NULL default '0', " . " submitted int(11) NOT NULL default '0', " . " PRIMARY KEY (nid, sid) " . ")");
return $ret;
}
function webform_update_2() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {webform} ADD redirect_post int(1) UNSIGNED NOT NULL DEFAULT '0'");
break;
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform} ADD redirect_post int(1) UNSIGNED NOT NULL DEFAULT '0' AFTER confirmation");
break;
}
return $ret;
}
function webform_update_3() {
return _system_update_utf8(array(
'webform',
'webform_component',
'webform_role_node',
'webform_submissions',
'webform_submitted_data',
));
}
function webform_update_4() {
$ret[] = update_sql("DROP TABLE if exists {webform_role_node}");
$ret[] = update_sql("ALTER TABLE {webform_submissions} ADD user varchar(128) AFTER submitted");
$ret[] = update_sql("ALTER TABLE {webform_submissions} ADD remote_addr varchar(128) AFTER user");
$ret[] = update_sql("ALTER TABLE {webform} ADD submit_limit int(2) NOT NULL DEFAULT '-1' AFTER redirect_post");
$ret[] = update_sql("ALTER TABLE {webform} ADD submit_interval int(10) NOT NULL DEFAULT '157784630' AFTER submit_limit");
$result = db_query("SELECT rid,perm FROM {permission}");
while ($row = db_fetch_object($result)) {
if (strpos($row->perm, "maintain webforms") !== false) {
$updated_perm = str_replace("maintain webforms", "edit webforms, access webform results", $row->perm);
db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $updated_perm, $row->rid);
}
}
return $ret;
}
function webform_update_5() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
db_query('LOCK TABLES {sequences} WRITE');
$ret[] = update_sql(sprintf("UPDATE {sequences} SET name = '%s' WHERE name = '%s'", db_prefix_tables('{webform_submissions}_sid'), db_prefix_tables('{webform_submissions}_id')));
db_query('UNLOCK TABLES');
break;
}
return $ret;
}
function webform_update_6() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {webform_component} ADD pid integer NOT NULL DEFAULT '0'");
break;
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform_component} ADD pid int(10) NOT NULL DEFAULT 0 AFTER cid");
break;
}
return $ret;
}
function webform_update_7() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
db_change_column($ret, 'webform_component', 'value', 'value', 'TEXT', array(
'not null' => FALSE,
'default' => "NULL",
));
break;
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform_component} CHANGE value value TEXT NULL DEFAULT NULL");
break;
}
return $ret;
}
function webform_update_8() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {webform} ADD additional_validate text DEFAULT NULL");
$ret[] = update_sql("ALTER TABLE {webform} ADD additional_submit text DEFAULT NULL");
break;
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform} ADD additional_validate text DEFAULT NULL AFTER email_subject");
$ret[] = update_sql("ALTER TABLE {webform} ADD additional_submit text DEFAULT NULL AFTER additional_validate");
break;
}
return $ret;
}
function webform_update_9() {
variable_del('webform_version');
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
db_change_column($ret, 'webform', 'email_from', 'email_from_address', 'varchar(255)', array(
'not null' => FALSE,
'default' => "NULL",
));
$ret[] = update_sql("ALTER TABLE {webform} ADD email_from_name varchar(255) NULL DEFAULT NULL");
break;
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform} CHANGE email_from email_from_address varchar(255) NULL DEFAULT NULL");
$ret[] = update_sql("ALTER TABLE {webform} ADD email_from_name varchar(255) NULL DEFAULT NULL AFTER email");
break;
}
return $ret;
}
function webform_update_10() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {webform_component} ADD form_key varchar(128) NULL DEFAULT NULL");
break;
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform_component} ADD form_key varchar(128) NULL DEFAULT NULL AFTER pid");
break;
}
return $ret;
}
function webform_update_11() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {webform_submissions} ADD INDEX sid (sid)");
$ret[] = update_sql("ALTER TABLE {webform_submitted_data} ADD INDEX sid (sid)");
break;
case 'pgsql':
$ret[] = update_sql("CREATE INDEX {webform_submissions}_sid_idx ON {webform_submissions}(sid)");
$ret[] = update_sql("CREATE INDEX {webform_submitted_data}_sid_idx ON {webform_submitted_data}(sid)");
break;
}
return $ret;
}
function webform_update_12() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {webform_submissions} ADD uid int(10) NOT NULL DEFAULT 0 AFTER sid");
$ret[] = update_sql("UPDATE {webform_submissions} ws set uid = (SELECT uid FROM {users} u WHERE u.name = ws.user)");
$ret[] = update_sql("ALTER TABLE {webform_submissions} DROP user");
break;
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {webform_submissions} ADD uid integer NOT NULL DEFAULT 0");
$ret[] = update_sql("UPDATE {webform_submissions} ws set uid = (SELECT uid FROM {users} u WHERE u.name = ws.user)");
$ret[] = update_sql("ALTER TABLE {webform_submissions} DROP user");
break;
}
return $ret;
}
function _webform_legacy_update($installed_version, $current_version) {
$ret = array();
if ($installed_version['build'] <= 1) {
$ret[] = array(
1 => "<strong>Build 1</strong><br />\n",
2 => "",
);
$ret[] = update_sql("CREATE TABLE {webform_submited_data} " . "( nid int(10) unsigned not null, " . "sid int(10) unsigned not null, " . "name varchar(255) not null, " . "data blob, " . "PRIMARY KEY(nid, sid, name))");
}
if ($installed_version['build'] <= 1) {
$ret[] = update_sql("ALTER TABLE {webform_component} MODIFY extra TEXT");
$ret[] = update_sql("ALTER TABLE {webform_submited_data} MODIFY data LONGTEXT");
}
if ($installed_version['build'] < 460) {
$ret[] = update_sql("ALTER TABLE {webform_submited_data} RENAME TO {webform_submitted_data}");
}
if ($installed_version['build'] < 461) {
$ret[] = update_sql(" ALTER TABLE {webform} MODIFY email varchar(255)");
$ret[] = update_sql(" UPDATE {webform_component} SET type = 'label' WHERE type = 'lable'");
}
if ($installed_version['build'] < 462) {
$ret[] = update_sql(" ALTER TABLE {webform} CHANGE confirm confirmation text");
}
if ($installed_version['build'] < 463) {
$result = db_query("SHOW COLUMNS FROM {webform} LIKE 'email_subject'");
if (db_num_rows($result) == 0) {
$ret[] = update_sql("ALTER TABLE {webform} ADD COLUMN email_subject varchar(255)");
}
$result = db_query("SHOW COLUMNS FROM {webform} LIKE 'email_from'");
if (db_num_rows($result) == 0) {
$ret[] = update_sql("ALTER TABLE {webform} ADD COLUMN email_from varchar(255)");
}
}
variable_set("webform_version", $current_version);
return $ret;
}
function webform_uninstall() {
watchdog("webform", "Removing webform module.");
variable_del('webform_use_cookies');
variable_del('webform_debug');
variable_del('webform_enable_fieldset');
variable_del('webform_default_from_email');
variable_del('webform_default_from_name');
variable_del('webform_default_subject');
if (!isset($component_list)) {
$component_list = array();
$path = drupal_get_path('module', 'webform') . "/components";
$files = file_scan_directory($path, '^.*\\.inc$');
foreach ($files as $filename => $file) {
variable_del('webform_enable_' . $file->name, 1);
}
}
$filepath = file_create_path('webform');
_webform_recursive_delete($filepath);
db_query("DROP TABLE IF EXISTS {webform}");
db_query("DROP TABLE IF EXISTS {webform_component}");
db_query("DROP TABLE IF EXISTS {webform_submissions}");
db_query("DROP TABLE IF EXISTS {webform_submitted_data}");
cache_clear_all(null, 'cache');
cache_clear_all(null, 'cache_filter');
cache_clear_all(null, 'cache_menu');
cache_clear_all(null, 'cache_page');
watchdog("Webform", "Webform module removed.");
}
function _webform_recursive_delete($path) {
if ($path) {
$listing = $path . "/*";
foreach (glob($listing) as $file) {
if (is_file($file) === true) {
@unlink($file);
}
elseif (is_dir($file) === true) {
_webform_recursive_delete($file);
}
}
@rmdir($path);
}
}