? usability_improv.patch
Index: user_relationship_node_access.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_relationships/user_relationship_node_access/Attic/user_relationship_node_access.module,v
retrieving revision 1.1.4.4
diff -r1.1.4.4 user_relationship_node_access.module
11,12c11,13
<   $actions = array('view', 'update', 'delete');
<   foreach ($actions as $key => $value) {
---
>   $all_actions = array('view', 'update', 'delete');
>   $actions = array();//allowed actions that will appear on the form
>   foreach ($all_actions as $key => $value) {
14a16
>       $actions[] = $value;
93a96,100
> 	// verify we can set permissions on this node
> 	if (!_user_relationship_node_access_node_eligible($form['#node'])) {
> 	  return;
> 	}
> 
97,98c104,105
< 	    $types["{$rtid}_yt"] = t('@type (you to them)', array('@type' => $type->plural_name));
< 	    $types["{$rtid}_ty"] = t('@type (them to you)', array('@type' => $type->plural_name));
---
> 	    $types["{$rtid}_yt"] = t('Post to @type (you to them)', array('@type' => $type->plural_name));
> 	    $types["{$rtid}_ty"] = t('Post to @type (them to you)', array('@type' => $type->plural_name));
101c108
< 	    $types[$rtid] = $type->plural_name;
---
> 	    $types[$rtid] = t('Post to @type', array('@type' => $type->plural_name));
115c122,134
< 
---
>   
>   // get permissions that can be set - from node author not current user
>   $author_allowed_perms = _user_relationship_node_access_get_allowed_grants(user_load($form['#node']->uid));
>   
>   if (!count($author_allowed_perms)) {
>     return;
>   }
>   
>   // use advanced form with a table if more than one permission, otherwise just a simple 'post to related users' checkbox
>   $use_advanced_form = count($author_allowed_perms) > 1;
>   // different labels for group posts (that have the group audience fieldset)
>   $is_group_post = module_exists('og') && og_is_group_post_type($form['type']['#value']);
>   
118,119c137,140
<     '#title'        => t('User Relationships Node Access'),
<     '#description'  => t('Node access based on your relationships to other users'),
---
>     '#title'        => t('Post to Social Network'),
>     '#description'  => $is_group_post ? 
>            t('Node access based on your relationships to other users, even if they are not in selected groups') : 
>            t('Node access based on your relationships to other users'),
121c142
<     '#collapsed'    => TRUE,
---
>     '#collapsed'    => $use_advanced_form,
123d143
<     '#theme'        => 'user_relationship_node_access_form',
125c145,149
<   foreach (array('view', 'update', 'delete') as $action) {
---
>   //theme as a table if more than one permission is given
>   if ($use_advanced_form) {
>     $form['user_relationship_node_access']['#theme'] = 'user_relationship_node_access_form';
>   }
>   foreach ($author_allowed_perms as $action) {
131d154
<   		'#title'          => t(ucfirst($action)),
133c156,158
<   		'#description'    => t('If no box is ticked, then anyone can @action.', array('@action' => $action))
---
>   		'#description'    => $is_group_post ? 
>   	         t('@group_post_ur_node_access_description', array('@group_post_ur_node_access_description' => '')) : 
>   	         t('If no box is ticked, then anyone can @action.', array('@action' => $action))
134a160,162
>   	if ($use_advanced_form) {
>   	  $form['user_relationship_node_access'][$action]['#title'] = t(ucfirst($action));
>   	}
135a164
> 
137a167,207
> /**
>  * Implementation of hook_perm().
>  */
> function user_relationship_node_access_perm() {
>   $results = array();
>   foreach(array('view', 'update', 'delete') as $type) {
>     $results[] = t('grant !type permission to related users', array('!type' => $type));
>   }
>   return $results;
> }
> 
> /**
>  * Sitewide settings 
>  */
> function user_relationship_node_access_admin_settings() {
>   $form['user_relationship_node_access_allowed_types'] = array(
>     '#type' => 'checkboxes',
>     '#default_value' => variable_get('user_relationship_node_access_allowed_types', array()),
>     '#options' => node_get_types('names'),//$options,
>     '#multiple' => TRUE
>   );
>   return system_settings_form($form);
> }
> 
> /**
>  * Implementation of hook_menu().
>  */
> function user_relationship_node_access_menu() {
>   $items = array();
>   $items['admin/content/user_relationship_node_access'] = array(
>     'title' => 'Posting to social network',
>     'description' => 'Set up sharing content based on user relationships',
>     'page callback' => 'drupal_get_form',
>     'page arguments' => array('user_relationship_node_access_admin_settings'),
>     'access callback' => 'user_access',
>     'access arguments' => array('administer nodes'),
>     'type' => MENU_NORMAL_ITEM,
>   );
> 
>   return $items;
> }
151,159c221,235
< 	  // reformat the array and optimize
< 	  $user_relationship_node_access = array();
< 		foreach($node->user_relationship_node_access as $action => $permissions) {
< 		  foreach ($permissions as $key => $permission) {
<   			if($permission) {
<   			  $user_relationship_node_access[$key][$action] = TRUE;
<   			}
< 		  }
< 		}
---
> 	  // if user is not allowed to effect perms, do not change access settings
> 	  $allowed_grants = _user_relationship_node_access_get_allowed_grants($user);
> 	  if (!count($allowed_grants)) {
> 	    break;
> 	  }
> 	  // if no content type isn't included, do not change access settings
> 	  if (!_user_relationship_node_access_node_eligible($node)) {
> 	    break;
> 	  }
> 	  
> 	  // clear old settings, this will actually clear even ones that user is not allowed to set.
>     db_query(
>       "DELETE FROM {user_relationship_node_access} WHERE nid = %d",
>       $node->nid
>     );
161,168c237,254
< 	  db_query(
< 	    "DELETE FROM {user_relationship_node_access} WHERE nid = %d",
< 	    $node->nid
< 	  );
< 		db_query(
< 		  "INSERT INTO {user_relationship_node_access} (nid, permissions) VALUES (%d, '%s')",
< 			$node->nid, serialize($user_relationship_node_access)
< 		);
---
> 	  $user_relationship_node_access = array();
> 	  if (is_array($node->user_relationship_node_access)) {
>       // reformat the array and optimize
> 	    foreach($node->user_relationship_node_access as $action => $permissions) {
>   		  foreach ($permissions as $key => $permission) {
>   		    //make sure user is allowed to set this permission
>     			if($allowed_grants[$action] && $permission) {
>     			  $user_relationship_node_access[$key][$action] = TRUE;
>     			}
>   		  }
>   		}
> 
>       // save permissions if any are set
>   		db_query(
>         "INSERT INTO {user_relationship_node_access} (nid, permissions) VALUES (%d, '%s')",
>         $node->nid, serialize($user_relationship_node_access)
>       );
> 	  }
250a337,364
> 
> /**
>  * Check if we are allowed to effect permissions on a node
>  *
>  * @param $node node to check
>  * @return TRUE iff permissions on the node may be set for related users
>  */
> function _user_relationship_node_access_node_eligible($node) {
>   $allowed_types = variable_get('user_relationship_node_access_allowed_types', array());
>   return isset($node->type) && $allowed_types[$node->type] === $node->type;
> }
> 
> /**
>  * Find the list of permissions that a user is allowed to grant
>  *
>  * @param $user user to check, if not the current user
>  * @return array of zero or more of ('view', 'update', 'delete')
>  */
> function _user_relationship_node_access_get_allowed_grants($user = NULL) {
>   $allowed_grants = array();
>   foreach(array('view', 'update', 'delete') as $type) {
>     $perm = t('grant !type permission to related users', array('!type' => $type));
>     if (user_access($perm, $user)) {
>       $allowed_grants[$type] = $type;
>     }
>   }
>   return $allowed_grants;
> }
