diff --git a/modules/hosting/client/hosting_client.install b/modules/hosting/client/hosting_client.install index 0497d82..1cc0f18 100644 --- a/modules/hosting/client/hosting_client.install +++ b/modules/hosting/client/hosting_client.install @@ -19,24 +19,22 @@ function hosting_client_schema() { 'not null' => TRUE, 'default' => 0, ), - 'name' => array( - 'type' => 'text', - 'size' => 'big', - 'not null' => FALSE, - ), - 'organization' => array( - 'type' => 'text', - 'size' => 'big', - 'not null' => FALSE, - ), 'email' => array( 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', ), + 'uname' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Machine-usable name of this client (without shell metacharacters, usable for UNIX groups)', + ), ), 'primary key' => array('vid'), + 'unique keys' => array('uname_unq' => array('uname')), ); $schema['hosting_client_user'] = array( @@ -212,3 +210,45 @@ function hosting_client_update_9() { } return $ret; } + +/** + * Cleanup the hosting_client fields + * + * This drops the client_name and client_organization fields in favor + * of the node title. The email field is deprecated until we link + * properly the users with the clients. + * + * It also populates the new, unique, uname field in the + * hosting_client table. + * + * Note that this request will fail if you have more than 100 clients + * with the same title. + * + * https://drupal.org/node/962330 + * https://drupal.org/node/461840 + */ +function hosting_client_update_6000() { + $ret = array(); + db_drop_field($ret, 'hosting_client', 'name'); + db_drop_field($ret, 'hosting_client', 'organization'); + db_add_field($ret, 'hosting_client', 'uname', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '')); + db_add_unique_key($ret, 'hosting_client', 'uname_unq' => array('uname')); + // populate the unique uname field + $result = db_query("SELECT nid,vid,title FROM {node} WHERE type = 'client'"); + while ($client = db_fetch_object($result)) { + $unique = preg_replace("/[!\W\.\-]/", "", $result->title); + $failed_client = FALSE; + for ($i = 0; $i < 100; $i++) { + if (db_query("UPDATE {hosting_client} SET uname = '%s' WHERE nid = %d AND vid = %d", $unique, $result->nid, $result->nid)) { + $failed_client = $result->title; + break; + } + $unique = preg_replace("/[!\W\.\-]/", "", $result->title) . '_' . $i; + } + if ($failed_client) { + $ret['#abort'] = array('success' => FALSE, 'query' => t('Could not find a unique client name for client @client', array('@client' => $failed_client))); + break; + } + } + return $ret; +} \ No newline at end of file diff --git a/modules/hosting/client/hosting_client.module b/modules/hosting/client/hosting_client.module index cf17351..66b3a8f 100644 --- a/modules/hosting/client/hosting_client.module +++ b/modules/hosting/client/hosting_client.module @@ -10,7 +10,7 @@ function hosting_client_node_info() { $types["client"] = array( "type" => 'client', "name" => 'Client', "module" => 'hosting_client', - "has_title" => TRUE, "title_label" => 'Client', + "has_title" => TRUE, "title_label" => 'Client name', "description" => hosting_node_help("client"), "has_body" => 0, "body_label" => '', "min_word_count" => 0); @@ -110,7 +110,13 @@ function hosting_get_client_by_email($email) { function hosting_client_form(&$node) { $type = node_get_types('type', $node); - $form['title'] = array('#type' => 'hidden', '#value' => $node->title); + $form['title'] = array( + '#type' => 'textfield', + '#required' => TRUE, + '#size' => 40, + '#default_value' => $node->email, + '#maxlength' => 100, + ); $form['email'] = array( '#type' => 'textfield', @@ -131,22 +137,6 @@ function hosting_client_form(&$node) { ); } - $form['client_name'] = array( - '#type' => 'textfield', - '#title' => t('Client name'), - '#size' => 40, - '#default_value' => $node->client_name, - '#maxlength' => 50, - ); - - $form['organization'] = array( - '#type' => 'textfield', - '#title' => t('Organization'), - '#size' => 40, - '#default_value' => $node->organization, - '#maxlength' => 100, - ); - if ($node->nid) { $users = hosting_client_users($node); foreach ($users as $uid => $uname) { @@ -198,6 +188,23 @@ function theme_hosting_client_form($form) { */ function hosting_client_validate(&$node) { + $nid = db_result(db_query("SELECT nid FROM {hosting_client} WHERE LOWER(uname) LIKE LOWER('%s')", preg_replace("/[!\W\.\-]/", "", $node->title))); + if ($nid && $node->nid != $nid) { + $suggestion = FALSE; + for ($i = 0; $i < 10; $i++) { + $nid = db_result(db_query("SELECT nid FROM {hosting_client} WHERE LOWER(uname) LIKE LOWER('%s')", preg_replace("/[!\W\.\-]/", "", $node->title) . $i)); + if ($nid && $node->nid != $nid) { + $suggestion = $node->title . " " . $i; + break; + } + } + if ($suggestion) { + form_set_error('title', t("Client name already in use, try @suggestion.", $suggestion)); + } else { + form_set_error('title', t("Client name already in use.")); + } + } + $nid = db_result(db_query("SELECT nid FROM {hosting_client} WHERE email='%s'", $node->email)); if ($nid && $node->nid != $nid) { form_set_error('email', t("Email address already exists.")); @@ -212,32 +219,12 @@ function hosting_client_validate(&$node) { } } -function hosting_client_set_title(&$node) { - if ($node->organization && $node->client_name) { - $node->title = $node->client_name . ' ('.$node->organization.')'; - } - elseif ($node->client_name) { - $node->title = $node->client_name . ' ('.$node->email.')'; - } - elseif ($node->organization) { - $node->title = $node->organization . ' ('.$node->email.')'; - } - else { - $node->title = $node->email; - } - $node->title = filter_xss($node->title); - db_query("UPDATE {node} SET title='%s' WHERE nid=%d", $node->title, $node->nid); - db_query("UPDATE {node_revisions} SET title='%s' WHERE vid=%d", $node->title, $node->vid); -} - - /** * Implementation of hook_insert(). */ function hosting_client_insert($node) { - db_query("INSERT INTO {hosting_client} (vid, nid, name, organization, email) VALUES (%d, %d, '%s', '%s', '%s' )", - $node->vid, $node->nid, $node->client_name, $node->organization, $node->email); - hosting_client_set_title($node); + db_query("INSERT INTO {hosting_client} (vid, nid, uname, email) VALUES (%d, %d, '%s', '%s' )", + $node->vid, $node->nid, preg_replace("/[!\W\.\-]/", "", $node->title), $node->email); if (variable_get('hosting_client_register_user', FALSE) && !user_load(array('mail' => $node->email))) { $user = hosting_client_register_user($node); @@ -319,8 +306,8 @@ function hosting_client_update($node) { hosting_client_insert($node); } else { - db_query("UPDATE {hosting_client} SET nid=%d, name = '%s', organization = '%s', email='%s' WHERE vid=%d", - $node->nid, $node->client_name, $node->organization, $node->email, $node->vid); + db_query("UPDATE {hosting_client} SET nid=%d, email='%s' WHERE vid=%d", + $node->nid, $node->email, $node->vid); } hosting_client_set_title($node); if ($node->users) { @@ -352,7 +339,7 @@ function hosting_client_delete($node) { * Node object */ function hosting_client_load($node) { - $additions = db_fetch_object(db_query('SELECT name as client_name, organization, email FROM {hosting_client} WHERE vid = %d', $node->vid)); + $additions = db_fetch_object(db_query('SELECT name as email FROM {hosting_client} WHERE vid = %d', $node->vid)); return $additions; } @@ -405,20 +392,6 @@ function hosting_client_users($node) { */ function hosting_client_view($node, $teaser = FALSE, $page = FALSE) { $node->content['info']['#prefix'] = '
'; - if ( $node->organization ) { - $node->content['info']['organization'] = array( - '#type' => 'item', - '#title' => t('Organization'), - '#value' => filter_xss($node->organization), - ); - } - if ( $node->client_name ) { - $node->content['info']['name'] = array( - '#type' => 'item', - '#title' => t('Client name'), - '#value' => filter_xss($node->client_name), - ); - } if ($node->name || $node->email) { $node->content['info']['email'] = array( '#type' => 'item', @@ -471,6 +444,7 @@ function hosting_client_view($node, $teaser = FALSE, $page = FALSE) { * Return an existing client registered for this email address, defaults to FALSE * @return * The nid of the generated client + * @deprecated the $organization parameter will be dropped after 1.x */ function hosting_import_client($email, $name = '', $organization = '') { $client = hosting_get_client_by_email($email); @@ -480,7 +454,7 @@ function hosting_import_client($email, $name = '', $organization = '') { $client->type = 'client'; $client->uid = 1; $client->email = $email; - $client->client_name = trim($name); + $client->title = trim($name); $client->organization = $organization; $client->status = 1; node_save($client); diff --git a/modules/hosting/client/hosting_client.views.inc b/modules/hosting/client/hosting_client.views.inc index 76e124e..cc5d6ca 100644 --- a/modules/hosting/client/hosting_client.views.inc +++ b/modules/hosting/client/hosting_client.views.inc @@ -26,17 +26,6 @@ function hosting_client_views_data() { 'handler' => 'views_handler_sort', ), ), - 'organization' => array( - 'title' => t('Organization'), - 'help' => t('The name of the client organization.'), - 'field' => array( - 'handler' => 'views_handler_field', - 'click sortable' => TRUE, - ), - 'sort' => array( - 'handler' => 'views_handler_sort', - ), - ), 'email' => array( 'title' => t('Email'), 'help' => t('The email contact address for this client.'), diff --git a/modules/hosting/signup/hosting_signup.module b/modules/hosting/signup/hosting_signup.module index 213f541..5852b39 100644 --- a/modules/hosting/signup/hosting_signup.module +++ b/modules/hosting/signup/hosting_signup.module @@ -84,7 +84,6 @@ function hosting_signup_form() { function hosting_signup_form_validate($form, &$form_state) { $client = (object) $form_state['values']; $client->type = 'client'; - $client->title = ''; node_validate($client); $site = (object) $form_state['values']; @@ -101,7 +100,6 @@ function hosting_signup_form_validate($form, &$form_state) { function hosting_signup_form_submit($form, &$form_state) { $client = (object) $form_state['values']; $client->type = 'client'; - $client->title = ''; $client->status = 1; node_save($client, 'submit'); diff --git a/modules/hosting/site/hosting_site.drush.inc b/modules/hosting/site/hosting_site.drush.inc index 7fd5abb..2db28b1 100644 --- a/modules/hosting/site/hosting_site.drush.inc +++ b/modules/hosting/site/hosting_site.drush.inc @@ -14,6 +14,7 @@ function hosting_hosting_site_context_options(&$task) { $client = node_load($task->ref->client); $task->context_options['client_email'] = $client->email; + $task->context_options['client_name'] = preg_replace("/[!\W\.\-]/", "", $client->title; } function hosting_site_drush_context_import($context, &$node) { diff --git a/modules/hosting/site/hosting_site.module b/modules/hosting/site/hosting_site.module index 47e7cb3..4ab17ac 100644 --- a/modules/hosting/site/hosting_site.module +++ b/modules/hosting/site/hosting_site.module @@ -320,7 +320,7 @@ function hosting_import_site($site_id, $data, $platform) { $client = node_load(HOSTING_DEFAULT_CLIENT); if ($data['client_email']) { - $client = hosting_import_client($data['client_email'], $data['client_name'], $data['client_organization']); + $client = hosting_import_client($data['client_email'], $data['client_name']); } $site = node_load($site_id);