diff --git a/modules/hosting/alias/hosting_alias.install b/modules/hosting/alias/hosting_alias.install
index 8e92515..4f1bc35 100644
--- a/modules/hosting/alias/hosting_alias.install
+++ b/modules/hosting/alias/hosting_alias.install
@@ -32,10 +32,9 @@ function hosting_alias_schema() {
'default' => 0,
),
'redirection' => array(
- 'type' => 'int',
- 'size' => 'tiny',
+ 'type' => 'varchar',
+ 'length' => 255,
'not null' => TRUE,
- 'default' => 0,
),
),
'indexes' => array(
@@ -81,3 +80,15 @@ function hosting_alias_update_2() {
return $ret;
}
+
+/**
+ * Alter the redirection field to a varchar
+ */
+function hosting_alias_update_3() {
+ $ret = array();
+ $schema = hosting_alias_schema();
+ $spec = $schema['hosting_site_alias']['fields']['redirection'];
+ db_change_field($ret, 'hosting_site_alias', 'redirection', 'redirection', $spec);
+ return $ret;
+}
+
diff --git a/modules/hosting/alias/hosting_alias.module b/modules/hosting/alias/hosting_alias.module
index 599fd40..978450a 100644
--- a/modules/hosting/alias/hosting_alias.module
+++ b/modules/hosting/alias/hosting_alias.module
@@ -28,6 +28,12 @@ function hosting_alias_menu() {
'access arguments' => array('administer hosting aliases'),
'type' => MENU_LOCAL_TASK
);
+ $items['hosting_alias/js'] = array(
+ 'title' => 'Javascript Alias Form',
+ 'page callback' => 'hosting_alias_form_js',
+ 'access arguments' => array('access content'),
+ 'type' => MENU_CALLBACK,
+ );
return $items;
}
@@ -52,9 +58,9 @@ function hosting_alias_help($path, $arg) {
* Add a textbox to site node forms with a newline
* separated list of aliases to the site
*/
-function hosting_alias_form_alter(&$form, $form_state, $form_id) {
+function hosting_alias_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'site_node_form') {
- return hosting_alias_form_data($form);
+ return hosting_alias_form_data($form, $form_state);
}
}
@@ -64,13 +70,13 @@ function hosting_alias_form_alter(&$form, $form_state, $form_id) {
* @param $form
* The form to alter, should come from hook_form_alter().
*/
-function hosting_alias_form_data(&$form) {
+function hosting_alias_form_data(&$form, &$form_state) {
if (user_access('create site aliases')) {
// List the automatic aliasses first.
- $aliases = hosting_alias_get_aliases($form['#node'], HOSTING_ALIAS_AUTOMATIC);
- if (sizeof($aliases)) {
- foreach ($aliases as $link) {
+ $automatic_aliases = hosting_alias_get_aliases($form['#node'], HOSTING_ALIAS_AUTOMATIC);
+ if (sizeof($automatic_aliases)) {
+ foreach ($automatic_aliases as $link) {
$links[] = l($link, "http://$link");
}
$form['aliases_automatic'] = array(
@@ -81,24 +87,135 @@ function hosting_alias_form_data(&$form) {
);
}
- $form['aliases'] = array(
- '#type' => 'textarea',
- '#title' => t('Domain aliases'),
- '#description' => t('The site can also be accessed through these domain names, one per line.'),
- '#default_value' => implode("\n", (array) $form['#node']->aliases),
- '#weight' => 10,
+ // Add a wrapper for the aliases and more button.
+ $form['aliases_wrapper'] = array(
+ '#tree' => FALSE,
+ '#title' => t('Domain Aliases'),
+ '#type' => 'fieldset',
+ '#prefix' => '
',
+ '#suffix' => '
',
);
- $form['redirection'] = array(
- '#type' => 'checkbox',
- '#title' => t('Redirect domain aliases to main domain'),
- '#default_value' => isset($form['#node']->redirection) ? $form['#node']->redirection : variable_get('hosting_alias_redirection', FALSE),
- '#weight' => 11,
+ $form['aliases_wrapper']['aliases'] = array(
+ '#prefix' => '',
+ '#suffix' => '
',
+ '#tree' => TRUE,
+ // '#theme' => 'hosting_alias_form',
+ );
+
+ // Get the list of existing aliases, either from form_state or the node.
+ if (isset($form_state['aliases'])) {
+ $aliases = array_filter($form_state['aliases']);
+ }
+ else {
+ $aliases = array_filter($form['#node']->aliases);
+ }
+
+ // Figure out the alias count
+ if (isset($form_state['alias_count'])) {
+ $alias_count = $form_state['alias_count'] + 1;
+ }
+ else {
+ $alias_count = max(1, empty($aliases) ? 1 : count($aliases) + 1);
+ }
+
+ // Add alias textfields
+ for ($delta = 0; $delta < $alias_count; $delta++) {
+ $form['aliases_wrapper']['aliases'][$delta] = array(
+ '#type' => 'textfield',
+ '#default_value' => $aliases[$delta],
+ );
+ }
+
+ // "Add Alias" button
+ $form['aliases_wrapper']['add_alias'] = array(
+ '#type' => 'submit',
+ '#value' => t('Add an alias'),
+ '#description' => t("Click here to add another alias."),
+ '#weight' => 1,
+ '#submit' => array('hosting_alias_add_alias_submit'), // If no javascript action.
+ '#ahah' => array(
+ 'path' => 'hosting_alias/js',
+ 'wrapper' => 'hosting-aliases-wrapper',
+ 'method' => 'replace',
+ 'effect' => 'fade',
+ ),
+ );
+
+ // Redirection Domain
+ $options = array();
+ $options[0] = t('No redirection');
+ $options[$form['#node']->title] = $form['#node']->title;
+ if (!empty($aliases)) {
+ $options += array_combine($aliases, $aliases);
+ }
+ if (!empty($automatic_aliases)) {
+ $options += array_combine($automatic_aliases, $automatic_aliases);
+ }
+
+ $default = isset($form_state['values']['redirection']) ? $form_state['values']['redirection'] : $form['#node']->redirection;
+
+ $form['aliases_wrapper']['redirection'] = array(
+ '#type' => 'select',
+ '#title' => t('Redirect all domain aliases to'),
+ '#options' => $options,
+ '#default_value' => $default,
+ '#weight' => -1,
);
return $form;
}
}
/**
+ * Callback to handle "Add Alias" button
+ */
+function hosting_alias_add_alias_submit($form, &$form_state) {
+ // Set the form to rebuild and run submit handlers.
+ node_form_submit_build_node($form, $form_state);
+
+ // Make the changes we want to the form state.
+ if ($form_state['values']['add_alias']) {
+ $form_state['values']['aliases'] = array_filter($form_state['values']['aliases']);
+ $form_state['alias_count'] = max(count($form_state['values']['aliases']), 1);
+ }
+}
+
+/**
+ * AHAH callback for additional alias form fields.
+ *
+ * Copied from poll_choice_js()
+ */
+function hosting_alias_form_js() {
+ include_once 'modules/node/node.pages.inc';
+ $form_state = array(
+ 'storage' => NULL,
+ 'submitted' => FALSE,
+ );
+ $form_build_id = $_POST['form_build_id'];
+ // Get the form from the cache.
+ $form = form_get_cache($form_build_id, $form_state);
+ $args = $form['#parameters'];
+ $form_id = array_shift($args);
+ // We will run some of the submit handlers so we need to disable redirecting.
+ $form['#redirect'] = FALSE;
+ // We need to process the form, prepare for that by setting a few internals
+ // variables.
+ $form['#post'] = $_POST;
+ $form['#programmed'] = FALSE;
+ $form_state['post'] = $_POST;
+ // Build, validate and if possible, submit the form.
+ drupal_process_form($form_id, $form, $form_state);
+ // This call recreates the form relying solely on the form_state that the
+ // drupal_process_form set up.
+ $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+ // Render the new output.
+ $alias_form = $form['aliases_wrapper'];
+ unset($alias_form['#prefix'], $alias_form['#suffix']); // Prevent duplicate wrappers.
+ $output = theme('status_messages') . drupal_render($alias_form);
+
+ drupal_json(array('status' => TRUE, 'data' => $output));
+}
+
+/**
* Retrieve a list of aliases for a site.
*
* @param $node
@@ -147,14 +264,14 @@ function hosting_alias_insert($node) {
if (is_array($aliases)) {
foreach ($aliases as $alias) {
if (($alias = trim($alias)) && !in_array($alias, $automatic)) {
- db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM, $node->redirection);
+ db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, '%s')", $node->vid, $node->nid, $alias, HOSTING_ALIAS_CUSTOM, $node->redirection);
}
}
}
if (sizeof($automatic)) {
foreach ($automatic as $alias) {
if (($alias = trim($alias)) && _hosting_valid_fqdn($alias)) {
- db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, %d)", $node->vid, $node->nid, $alias, HOSTING_ALIAS_AUTOMATIC, $node->redirection);
+ db_query("INSERT INTO {hosting_site_alias} (vid, nid, alias, automatic, redirection) VALUES (%d, %d, '%s', %d, '%s')", $node->vid, $node->nid, $alias, HOSTING_ALIAS_AUTOMATIC, $node->redirection);
}
}
}
@@ -218,7 +335,7 @@ function hosting_alias_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
hosting_alias_delete_revision($node);
break;
case 'validate' :
- $aliases = explode("\n", $node->aliases);
+ $aliases = $node->aliases;
foreach ($aliases as $alias) {
if ($alias = trim($alias)) {
if (!hosting_domain_allowed($alias, array('nid' => $node->nid)) || $alias == $node->title) {
@@ -229,6 +346,11 @@ function hosting_alias_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
}
}
}
+
+ // Make sure chosen redirect is still an alias
+ if ($node->redirection != 0 && !in_array($node->redirection, $aliases) && !($node->redirection == $node->title)) {
+ form_set_error('redirection', t('The domain name @alias is not an alias for this site.', array('@alias' => $node->redirection)));
+ }
break;
case 'load':
// XXX: this returns only the first redirection status. it
@@ -256,8 +378,8 @@ function hosting_alias_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$redirection = db_result(db_query("SELECT redirection FROM {hosting_site_alias} WHERE vid=%d", $node->vid));
$node->content['info']['redirection'] = array(
'#type' => 'item',
- '#title' => t('Redirection'),
- '#value' => $redirection['redirection'] ? t('Yes') : t('No'),
+ '#title' => t('Redirection Target'),
+ '#value' => !empty($redirection) ? l($redirection, "http://$redirection") : t('None'),
'#weight' => 10,
);
}