From b8e626b1b1fde3a6ed16b55818aa677bd92e07ff Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Sat, 23 Apr 2011 17:59:24 +0200 Subject: [PATCH] Issue #1115956 by Berdir: Added reversed and capitalized relationship type names. --- tests/user_relationships.api.test | 52 +++++++++++++++ user_relationships.admin.inc | 128 ++++++++++++++++++++++-------------- user_relationships.install | 74 +++++++++++++++++++++- user_relationships.module | 73 ++++++++++++++++++--- 4 files changed, 265 insertions(+), 62 deletions(-) diff --git a/tests/user_relationships.api.test b/tests/user_relationships.api.test index 4cf61d5..69f43a8 100644 --- a/tests/user_relationships.api.test +++ b/tests/user_relationships.api.test @@ -278,3 +278,55 @@ class UserRelationshipsTestCase extends DrupalWebTestCase { $this-> assertFalse($rtype->is_reciprocal); } } + +/** + * User Relationship type tests. + */ +class UserRelationshipsTypesTestCase extends DrupalWebTestCase { + + /** + * Implements getInfo(). + */ + function getInfo() { + return array( + 'name' => t('User Relationships Types'), + 'description' => t('Checks various user relationship types functions.'), + 'group' => t('User Relationships'), + ); + } + + /** + * Enable modules. + */ + function setUp() { + parent::setUp('user_relationships'); + } + + function testNames() { + // Create relationship type. + $relationship_type = (object)array( + 'name' => $this->randomName(), + 'plural_name' => $this->randomName(), + 'reverse_name' => $this->randomName(), + 'reverse_plural_name' => $this->randomName(), + 'name_capitalized' => $this->randomName(), + 'plural_name_capitalized' => $this->randomName(), + 'reverse_name_capitalized' => $this->randomName(), + 'reverse_plural_name_capitalized' => $this->randomName(), + 'requires_approval' => FALSE, + 'expires_val' => 0, + ); + + // First, check all names. + $this->assertEqual($relationship_type->name, user_relationships_type_get_name($relationship_type)); + $this->assertEqual($relationship_type->plural_name, user_relationships_type_get_name($relationship_type, TRUE)); + $this->assertEqual($relationship_type->reverse_name, user_relationships_type_get_name($relationship_type, FALSE, TRUE)); + $this->assertEqual($relationship_type->reverse_plural_name, user_relationships_type_get_name($relationship_type, TRUE, TRUE)); + $this->assertEqual($relationship_type->name_capitalized, user_relationships_type_get_name($relationship_type, FALSE, FALSE, TRUE)); + $this->assertEqual($relationship_type->plural_name_capitalized, user_relationships_type_get_name($relationship_type, TRUE, FALSE, TRUE)); + $this->assertEqual($relationship_type->reverse_name_capitalized, user_relationships_type_get_name($relationship_type, FALSE, TRUE, TRUE)); + $this->assertEqual($relationship_type->reverse_plural_name_capitalized, user_relationships_type_get_name($relationship_type, TRUE, TRUE, TRUE)); + + // @todo: Test fallback strategy. + } +} diff --git a/user_relationships.admin.inc b/user_relationships.admin.inc index a031e34..0f70357 100644 --- a/user_relationships.admin.inc +++ b/user_relationships.admin.inc @@ -155,12 +155,6 @@ function user_relationships_admin_settings() { function user_relationships_admin_type_edit($form, &$form_state, $relationship_type = NULL) { if (!$relationship_type) { drupal_set_title(t('Add relationship type')); - $relationship_type = (object) array( - 'name' => '', - 'plural_name' => '', - 'is_oneway' => FALSE, - 'is_reciprocal' => FALSE, - ); } else { drupal_set_title(t('Edit @name relationship type', array('@name' => $relationship_type->name))); @@ -179,15 +173,6 @@ function user_relationships_admin_type_edit($form, &$form_state, $relationship_t '#required' => TRUE, '#weight' => -10, ); - $form['plural_name'] = array( - '#type' => 'textfield', - '#title' => t('Plural name'), - '#maxlength' => 255, - '#default_value' => isset($relationship_type) ? $relationship_type->plural_name : NULL, - '#description' => t("Example: buddies, friends, coworkers, spouses."), - '#required' => TRUE, - '#weight' => -9, - ); $form['requires_approval'] = array( '#type' => 'checkbox', '#title' => t('Requires Approval'), @@ -229,6 +214,82 @@ function user_relationships_admin_type_edit($form, &$form_state, $relationship_t ), ); + $form['branding'] = array( + '#type' => 'fieldset', + '#title' => t('Branding'), + '#group' => 'tabs', + ); + + $form['branding']['reverse_name'] = array( + '#type' => 'textfield', + '#title' => t('Reversed name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type) ? $relationship_type->reverse_name : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 0, + ); + + $form['branding']['plural_name'] = array( + '#type' => 'textfield', + '#title' => t('Plural name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type->plural_name) ? $relationship_type->plural_name : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 2, + ); + + $form['branding']['reverse_plural_name'] = array( + '#type' => 'textfield', + '#title' => t('Plural reversed name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type) ? $relationship_type->reverse_plural_name : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 4, + ); + + $form['branding']['name_capitalized'] = array( + '#type' => 'textfield', + '#title' => t('Capitalized name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type) ? $relationship_type->name_capitalized : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 6, + ); + + $form['branding']['reverse_name_capitalized'] = array( + '#type' => 'textfield', + '#title' => t('Capitalized reversed name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type) ? $relationship_type->reverse_name_capitalized : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 8, + ); + + $form['branding']['plural_name_capitalized'] = array( + '#type' => 'textfield', + '#title' => t('Capitalized plural name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type) ? $relationship_type->plural_name_capitalized : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 10, + ); + + $form['branding']['reverse_plural_name_capitalized'] = array( + '#type' => 'textfield', + '#title' => t('Capitalized lural reversed name'), + '#maxlength' => 255, + '#default_value' => isset($relationship_type) ? $relationship_type->reverse_plural_name_capitalized : NULL, + '#description' => t("Example: buddies, friends, coworkers, spouses."), + '#required' => TRUE, + '#weight' => 12, + ); + $form['rtid'] = array( '#type' => 'value', '#value' => isset($relationship_type->rtid) ? (int)$relationship_type->rtid : NULL, @@ -317,19 +378,10 @@ function user_relationships_setting_validation($element, $validations) { */ function user_relationships_admin_type_edit_submit($form, &$form_state) { $relationship_type = (object)$form_state['values']; - if ($form_state['values']['action'] == 'add') { - //#348025 when editing a type, make sure that two-way relationships are not left as reciprocal, just in case, as the UI allows it. - if (!$relationship_type->is_oneway) { - $relationship_type->is_reciprocal = 0; - } - drupal_write_record('user_relationship_types', $relationship_type); - // Update strings for translation - _user_relationships_update_translations($relationship_type); - - $form_state['values']['rtid'] = $relationship_type->rtid; - module_invoke_all('user_relationships_type_insert', $relationship_type); + user_relationships_type_save($relationship_type); + if ($form_state['values']['action'] == 'add') { drupal_set_message(t('Relationship %name has been added.', array('%name' => $relationship_type->name))); watchdog( 'u_relationship', @@ -339,16 +391,6 @@ function user_relationships_admin_type_edit_submit($form, &$form_state) { ); } else { - //#348025 when editing a type, make sure that two-way relationships are not left as reciprocal, just in case, as the UI allows it. - if (!$relationship_type->is_oneway) { - $relationship_type->is_reciprocal = 0; - } - drupal_write_record('user_relationship_types', $relationship_type, 'rtid'); - - // Update strings for translation - _user_relationships_update_translations($relationship_type); - module_invoke_all('user_relationships_type_update', $relationship_type); - drupal_set_message(t('Relationship %name has been updated.', array('%name' => $relationship_type->name))); watchdog( 'u_relationship', @@ -358,9 +400,6 @@ function user_relationships_admin_type_edit_submit($form, &$form_state) { ); } - user_relationships_types_load(TRUE); - menu_rebuild(); - $form_state['redirect'] = 'admin/config/people/relationships'; } @@ -388,17 +427,6 @@ function user_relationships_admin_type_delete_submit($form, &$form_state) { } /** - * Make relationship type available to translation by i18nstrings.module - */ -function _user_relationships_update_translations(&$relationship_type) { - if (function_exists('i18nstrings_update') && $relationship_type->rtid) { - i18nstrings_update("user_relationships:rtid:$relationship_type->rtid:name", $relationship_type->name); - i18nstrings_update("user_relationships:rtid:$relationship_type->rtid:plural_name", $relationship_type->plural_name); - } -} - - -/** * Relationship Types List */ function user_relationships_admin_types_list_page() { diff --git a/user_relationships.install b/user_relationships.install index 6ce657f..29a12ce 100644 --- a/user_relationships.install +++ b/user_relationships.install @@ -33,11 +33,41 @@ function user_relationships_schema() { 'fields' => array( 'rtid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), 'name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), - 'plural_name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), + 'plural_name' => array('type' => 'varchar', 'length' => 255, 'default' => ''), 'is_oneway' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), 'is_reciprocal' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), 'requires_approval' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'), 'expires_val' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), + 'reverse_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'plural_name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'reverse_name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'reverse_plural_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'reverse_plural_name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), ), 'unique keys' => array( 'name' => array('name'), @@ -93,4 +123,46 @@ function user_relationships_update_7001() { drupal_set_message(('Allowed roles settings for user relationship types must now be set by configuring the corresponding permissions explicitly.')); db_drop_table('user_relationship_type_roles'); db_drop_table('user_relationship_type_roles_receive'); +} + +/** + * Add table columns for reversed and capitalized names. + */ +function user_relationships_update_7002() { + $fields = array( + 'reverse_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'plural_name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'reverse_name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'reverse_plural_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + 'reverse_plural_name_capitalized' => array( + 'type' => 'varchar', + 'length' => 255, + 'default' => '', + ), + ); + + foreach ($fields as $field => $definition) { + db_add_field('user_relationship_types', $field, $definition); + } } \ No newline at end of file diff --git a/user_relationships.module b/user_relationships.module index 1ba8419..cdb63a7 100644 --- a/user_relationships.module +++ b/user_relationships.module @@ -11,6 +11,10 @@ define('UR_OK', 0x0); define('UR_BANNED', 0x1); +define('USER_RELATIONSHIPS_NAME_PLURAL', 1); +define('USER_RELATIONSHIPS_NAME_CAPITAL', 2); +define('USER_RELATIONSHIPS_NAME_REVERSE', 4); + function user_relationships_menu() { $items['admin/config/people/relationships'] = array( 'title' => 'Relationships', @@ -376,28 +380,39 @@ function user_relationships_types_load($reset = FALSE) { * A User Relationship type object */ function user_relationships_type_save($rtype) { + + // #348025 when editing a type, make sure that two-way relationships are not + // left as reciprocal, just in case, as the UI allows it. + if (empty($rtype->is_oneway)) { + $rtype->is_reciprocal = 0; + } + + // Ensure "expires_val" is numeric and not negative. + if (!isset($rtype->expires_val) || !is_numeric($rtype->expires_val) || $rtype->expires_val < 0) { + $rtype->expires_val = 0; + } + module_invoke_all('user_relationships_type_presave', $rtype); + // Update strings for translation. + _user_relationships_update_translations($rtype); + $op = (isset($rtype->rtid) && $rtype->rtid) ? 'update' : 'insert'; - // find a relationship type with the name we're trying to save + // Find a relationship type with the name we're trying to save // if it's an update action check to see that the rtypes match - // otherwise it's just invalid - if ( - ($found_rt = user_relationships_type_load(array('name' => $rtype->name))) && - ($op == 'update' ? $found_rt->rtid != $rtype->rtid : TRUE) - ) { + // otherwise it's just invalid. + $found_rt = user_relationships_type_load(array('name' => $rtype->name)); + if ($found_rt && (($op == 'update' && $found_rt->rtid != $rtype->rtid) || $op == 'insert')) { return FALSE; } - // ensure "expires_val" is numeric and not negative - if (!is_numeric($rtype->expires_val) || $rtype->expires_val < 0) { - $rtype->expires_val = 0; - } - drupal_write_record('user_relationship_types', $rtype, ($op == 'update' ? 'rtid' : array())); module_invoke_all('user_relationships_type_' . $op, $rtype); + + user_relationships_types_load(TRUE); + menu_rebuild(); } @@ -930,3 +945,39 @@ function user_relationships_ui_locale($op = 'groups') { return array('user_relationships' => t('User Relationships')); } } + +/** + * Gets the relationship type name. + */ +function user_relationships_type_get_name($type, $plural = FALSE, $reversed = FALSE, $capitalized = FALSE) { + if (is_string($type)) { + $type = user_relationships_type_load($type); + } + + // Start with just name as the name and then add suffixes and prefixes + // according to the given flags. + $name = 'name'; + if ($plural) { + $name = 'plural_' . $name; + } + + if ($reversed) { + $name = 'reverse_' . $name; + } + + if ($capitalized) { + $name .= '_capitalized'; + } + + return !empty($type->$name) ? $type->$name : $type->name; +} + +/** + * Make relationship type available to translation by i18nstrings.module + */ +function _user_relationships_update_translations(&$relationship_type) { + if (function_exists('i18nstrings_update') && $relationship_type->rtid) { + i18nstrings_update("user_relationships:rtid:$relationship_type->rtid:name", $relationship_type->name); + i18nstrings_update("user_relationships:rtid:$relationship_type->rtid:plural_name", $relationship_type->plural_name); + } +} \ No newline at end of file -- 1.7.4.1