Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCHR-3544: Hide Fields for Contact Summary #2703

Conversation

ajesamson
Copy link
Contributor

@ajesamson ajesamson commented Jun 21, 2018

Overview

Some contact summary fields are visible by default. This PR changes the default for Suffix, Instant messenger and Social Accounts to un-check so they are hidden from users by default

Before

contact_before

After

contact_after

Technical Details

Before un-checking the fields, it was ensured that no record previously existed relating to them.

private function up1022_getUnusedContactEditOptionNames() {
    $names = [];
    
    foreach (['Contact', 'IM', 'Website'] as $name) {
      $params = ['return' => ['id']];
      if ($name === 'Contact') {
        $params['suffix_id'] = ['IS NOT NULL' => 1];
      }
      
      $result = civicrm_api3($name, 'get', $params);
      if ($result['count'] == 0) {
        array_push($names, $name === 'Contact' ? 'Suffix' : $name);
      }
    }
    
    return $names;
}

They are also compared against previously activated fields. This implies that they are ignored if not previously checked.

public function upgrade_1022() {
    $valuesRemoved = $this->up1022_getContactEditOptionValues();
    $optionValues = $this->up1022_getActiveContactEditOptionValues();
    
    $activeOptionValues = array_diff($optionValues, $valuesRemoved);
    civicrm_api3('Setting', 'create', [
      'contact_edit_options' => $activeOptionValues,
    ]);
    
    return TRUE;
  }

Copy link
Contributor

@mickadoo mickadoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, some minor changes

*/
public function upgrade_1022() {
$valuesRemoved = $this->up1022_getContactEditOptionValues();
$optionValues = $this->up1022_getActiveContactEditOptionValues();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's quite confusing with OptionValue.value but maybe we could make the naming a bit clearer here?

In the end we want to do a diff of "values to remove" and "active values" and only remove those that we want to that are not active, right? $valuesRemoved sounds like they have already been removed, and $optionValues is a bit vague about which option values they are.

What do you think about renaming $valuesRemoved to $valuesToRemove and up1022_getContactEditOptionValues to up1022_getContactEditOptionValuesToRemove?

And then renaming $optionValues to $activeOptionValues. We could then keep the variable $valuesToRemove and reassign it with the result of the diff, because really these are the "values we will remove"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted thanks.

'return' => ['contact_edit_options'],
]);

return $result['values'][0]['contact_edit_options'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a personal preference, but I like removing sequential and using array_shift on the result instead, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth exploring. My main idea behind sequential is using it whenever i do not care about iterating over entity id. By not using sequential, results will be indexed their ids and not array index.

trait CRM_HRCore_Upgrader_Steps_1022 {

/**
* Hide Fields For Contact Summary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed thanks

$names = $this->up1022_getUnusedContactEditOptionNames();

$params = [
'sequential' => 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is sequential required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using sequential, i did not have to worry myself with the key in my foreach

foreach ($optionValues['values'] as $optionValue) {

Like you said, i think its just preference.


foreach (['Contact', 'IM', 'Website'] as $name) {
$params = ['return' => ['id']];
if ($name == 'Contact') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use === here


$result = civicrm_api3($name, 'get', $params);
if ($result['count'] == 0) {
array_push($names, $name == 'Contact' ? 'Suffix' : $name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== should be fine here too

/**
* Hide Fields For Contact Summary
*/
public function upgrade_1022() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a general question, if this upgrader is run twice will the result be the same? That's what we aim for with upgraders. I think this one looks fine, but just asking

Copy link
Contributor Author

@ajesamson ajesamson Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a benefit of doubt, i tested with running the upgrade twice by changing the schema_version on civicrm_extension for HR Core backward and running drush cvapi extension.upgrade. The results remains the same. This was made possible by $activeOptionValues = array_diff($optionValues, $valuesRemoved); which will always return same result after upgrade.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks a lot for checking

Copy link
Contributor

@mickadoo mickadoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good ⚡

/**
* Hide Fields For Contact Summary
*/
public function upgrade_1022() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks a lot for checking

'return' => ['contact_edit_options'],
]);

return $result['values'][0]['contact_edit_options'];
return $result['values'][$result['id']]['contact_edit_options'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that's a good approach too

@ajesamson ajesamson force-pushed the PCHR-3544-hide-fields-for-contact-summary branch 2 times, most recently from 8384336 to f3b4a6f Compare June 21, 2018 12:07
@ajesamson ajesamson merged commit ebede58 into PCHR-3536-Display-Preferences Jun 21, 2018
@ajesamson ajesamson deleted the PCHR-3544-hide-fields-for-contact-summary branch June 21, 2018 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants