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

CRM-20821 & CRM-20938 - fix bugs in managing premium product images #10720

Merged
merged 9 commits into from
Jul 25, 2017

Conversation

seancolsen
Copy link
Contributor

Issues

This PR addresses both

Details steps to reproduce are provided in both tickets.

Summary

Before After
Editing a premium product (without making changes) breaks its image Images are preserved while editing
Uploading a non-square image for a premium product squishes it into a square, distorting the image Aspect ratio is preserved while resizing images
Long functions, deeply nested code, sparse code comments Improved code clarity through refactoring

Notes

The reason the images were getting broken is because of ffc4d2d which added logic to "strip protocol and domain from image URLs" but mistakenly ended up being a bit overzealous with its "strip" logic — it would strip path components off the start of the URL no matter what. I improved this logic in 368073f (and added tests to support the new logic).

@seamuslee001
Copy link
Contributor

Jenkins re test this please

$params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
// Use local URLs for images when possible
$params['image'] = CRM_Utils_String::simplifyURL($params['image'], TRUE);
$params['thumbnail'] = CRM_Utils_String::simplifyURL($params['thumbnail'], TRUE);

Copy link
Member

@monishdeb monishdeb Jul 24, 2017

Choose a reason for hiding this comment

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

@seanmadsen can you further minimize it to

$params = array_merge($params, array(
     'id' => CRM_Utils_Array::value('premium', $ids),
     'image' => CRM_Utils_String::simplifyURL(CRM_Utils_Array::value('image', $params, ''), TRUE),
     'thumbnail' => CRM_Utils_String::simplifyURL(CRM_Utils_Array::value('thumbnail', $params, '', TRUE),
     'is_active' => FALSE,
     'is_deductible' => FALSE,
     'currency' => CRM_Core_Config::singleton()->defaultCurrency,
));

??

- Move resizeImage() from CRM_Contribute_Form_ManagePremiums to
  CRM_Utils_File
- Make it public static
- Don't change anything about how it works
- Clean up CRM_Utils_File::resizeImage()
- No major functionality changes
- Better docblock
- Better error handling
- Better variable names
- Refactor CRM_Contribute_BAO_ManagePremiums::add
- Don't change any functionality
- Improve docblock
- Use a $defaults array
- Refactor CRM_Contribute_Form_ManagePremiums::postProcess()
- No major changes to functionality
- Eliminate deeply nested code (addressing "FIXME" comment)
- Split code into multiple functions
- Add more comments
- Improve CRM_Contribute_Form_ManagePremiums::formRule()
- Require an image file, if "upload" is chosen
- Remove the commented-out code for a rule which required images to be
  within a certain size
- Minor refactoring to improve code clarity
This change is the crux of CRM-20821.

Before this change, editing a premium product would automatically
break the image URL. After this change, premium products can be edited
while retaining the image URL. Functionality is preserved which changes
the URL to a local URL when possible.

This commit adds two new Utils functions (along with tests) to handle
the logic of changing a supplied URL to a local URL.
Make CRM_Utils_File::resizeImage() preserve image aspect ratio by
default.
@monishdeb
Copy link
Member

@colemanw @eileenmcnaughton I have reviewed the PR and updated the PR with some additional fix. Ensured that there is no unintended regression due to refactoring. Can you please merge this PR?

@eileenmcnaughton
Copy link
Contributor

@monishdeb I have read your patch and agree that it adds some tidy ups and the lines all look correct. I understand from your comments that you have reviewed all Sean's code & tested it - so I'm happy that this is good to merge.

@seanmadsen would be great if you can do a final test against master after merge

@colemanw colemanw merged commit 638bab7 into civicrm:master Jul 25, 2017
isset($params['imageOption']) &&
$params['imageOption'] == 'image' &&
empty($files['uploadFile']['name'])
if (CRM_Utils_Array::value('imageOption', $params['imageOption']) == 'image'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code:
CRM_Utils_Array::value('imageOption', $params['imageOption'])
needs to be changed to
CRM_Utils_Array::value('imageOption', $params)

Copy link
Member

Choose a reason for hiding this comment

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

Ahh right typo .. sorry about that .. going to create a new PR for that

) {
$errors['uploadFile'] = ts('A file must be selected');
}

// If choosing to use image URLs, then both URLs must be present
if (isset($params['imageOption']) && $params['imageOption'] == 'thumbnail') {
if (CRM_Utils_Array::value('imageOption', $params['imageOption']) == 'thumbnail') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code:
CRM_Utils_Array::value('imageOption', $params['imageOption'])
needs to be changed to
CRM_Utils_Array::value('imageOption', $params)

@monishdeb
Copy link
Member

@seanmadsen I have created a separate PR #10761 . Is there anything else I need to fix?

seancolsen added a commit to seancolsen/civicrm-core that referenced this pull request Jul 25, 2017
Before this commit, the URL simplification was performed within the
$params array used as the *default* values, so these values got
over-written by the submitted values with array_merge().

This is a follow-up to changes made in civicrm#10720 bb80d2f
@seancolsen
Copy link
Contributor Author

seancolsen commented Jul 25, 2017

@monishdeb PR #10761 is good in that it addresses my comments above. ✅

In further testing I identified another problem with your changes in bb80d2f. ❌ I am proposing a fix in #10762.

I have now completed my testing. Once #10761 and #10762 are merged then I think we can close the book on this little project.

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.

6 participants