Skip to content

Commit

Permalink
Merge pull request #561 from compucorp/PCHR-4281-image-display-fixes
Browse files Browse the repository at this point in the history
PCHR-4281: Image Display Fixes
  • Loading branch information
mickadoo authored Oct 9, 2018
2 parents da7a01a + 0cc1042 commit 8f955f7
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 50 deletions.
17 changes: 16 additions & 1 deletion civihr_employee_portal/civihr_employee_portal.install
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php

use Drupal\civihr_employee_portal\Helpers\WebformHelper;
use Drupal\civihr_employee_portal\Forms\OnboardingWebForm;
use Drupal\civihr_employee_portal\Helpers\UrlHelper;
use Drupal\civihr_employee_portal\Helpers\TaxonomyHelper;

/**
* Include all the update files
*/
foreach (glob(__DIR__ . '/updates/*.php') as $updateFile) {
require_once $updateFile;
}

/**
* Create Report Pages and Report Age Settings menu links.
*/
Expand Down Expand Up @@ -511,6 +517,15 @@ function civihr_employee_portal_update_7041() {
features_revert(['civihr_default_permissions' => ['user_permission']]);
}

/**
*
* Hey, don't add any new upgrader functions here. Put any new upgrader
* functions in a separate file in the /updates directory.
*
* @see https://compucorp.atlassian.net/wiki/spaces/PCHR/pages/676823043/Add+a+Drupal+Upgrader
*
*/

/**
* Function to determine whether menu link exists or not.
*
Expand Down
48 changes: 0 additions & 48 deletions civihr_employee_portal/src/Forms/OnboardingWebForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function onSubmit($node, $values) {
}

$this->setWorkEmailAsPrimary($node, $values, $contactID);
$this->hackyFixForImageURL($contactID);
}

/**
Expand Down Expand Up @@ -177,39 +176,6 @@ private function addHelpText(&$form) {
];
}

/**
* Unfortunately for us the contact profile page will expect an image URL
* using the civicrm/file?photo=foo.jpg style. Since our contact images are
* created using webform they won't match this so to avoid the warnings about
* 'Undefined index: photo' we append photo=0 here.
*
* @see CRM_Utils_File::getImageURL
*
* @param int $contactID
*/
private function hackyFixForImageURL($contactID) {
$params = ['return' => 'image_URL', 'id' => $contactID];
/** @var string $current */
$current = civicrm_api3('Contact', 'getvalue', $params);

if (empty($current)) {
return;
}

// don't append to url if photo is already set
parse_str(parse_url($current, PHP_URL_QUERY), $queryParts);
if (isset($queryParts['photo'])) {
return;
}

$operator = FALSE === strpos($current, '?') ? '?' : '&';
$current .= $operator . 'photo=0';

unset($params['return']);
$params['image_URL'] = $current;
civicrm_api3('Contact', 'create', $params);
}

/**
* Gets the help text to show the user at the beginning of the onboarding form.
*
Expand Down Expand Up @@ -238,18 +204,4 @@ private function getSkipButtonMarkup() {
return sprintf('<a href="%s">%s</a>', $link, $buttonMarkup);
}

/**
* Checks if the current logged in user was created after the onboarding
* feature was released.
*
* @return bool
*/
private function userCreatedAfterOnboardingReleased() {
global $user;
$onboardingForm = WebformHelper::findOneByTitle(self::NAME);
$onboardingRelease = $onboardingForm->created;

return $user->created > $onboardingRelease;
}

}
40 changes: 40 additions & 0 deletions civihr_employee_portal/src/Helpers/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,44 @@ public static function dedupeUrlQueryParams($url) {
return str_replace($origQueryString, $newQueryString, $url);
}

/**
* Removes the provided key from the URL query parameters
*
* @param string $url
* The URL to edit, e.g. http://example.com?foo=bar&bar=zoo
* @param string $targetKey
* The key you want to remove e.g. bar
*
* @return string
* Original URL without the target query, e.g. http://example.com?foo=bar
*/
public static function removeQueryValueFromUrl($url, $targetKey) {
$origQueryString = parse_url($url, PHP_URL_QUERY);
$queryParts = explode('&', $origQueryString);

$newQueryParts = [];
foreach ($queryParts as $index => $queryPart) {
list($key) = explode('=', $queryPart);

$keyToCompare = $targetKey;
if (substr($key, -2) === '[]') {
$keyToCompare = $targetKey . '[]';
}

if ($key !== $keyToCompare) {
$newQueryParts[$key] = $queryPart;
}
}

$newQueryString = implode('&', $newQueryParts);
$newUrl = str_replace($origQueryString, $newQueryString, $url);

if (!$newQueryString) {
// remove ? if no query string remains
$newUrl = str_replace('?', '', $newUrl);
}

return $newUrl;
}

}
57 changes: 57 additions & 0 deletions civihr_employee_portal/tests/Helpers/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,63 @@ public function testDedupeWillReturnExpectedResults($url, $expected) {
$this->assertEquals($expected, UrlHelper::dedupeUrlQueryParams($url));
}

/**
* @dataProvider urlWithQueryProvider
*
* @param string $url
* @param string $key
* @param string $expected
*/
public function testExpectedUrlValuesWillBeRemoved($url, $key, $expected) {
$result = UrlHelper::removeQueryValueFromUrl($url, $key);
$this->assertEquals($expected, $result);
}

public function urlWithQueryProvider() {
return [
[
'http://example.com?foo=bar',
'',
'http://example.com?foo=bar',
],
[
'http://example.com?foo=bar',
'foo',
'http://example.com',
],
[
'http://example.com?a=1&b=2',
'a',
'http://example.com?b=2',
],
[
'http://example.com?a=1&b=2',
'b',
'http://example.com?a=1',
],
[
'http://example.com?a[]=1&a[]=2&b=3',
'a',
'http://example.com?b=3',
],
[
'http://example.com?a=1&b[]=2&b[]=3',
'b',
'http://example.com?a=1',
],
[
'http://civihr.local/sites/default/files/webform/500.jpg?photo=0',
'photo',
'http://civihr.local/sites/default/files/webform/500.jpg',
],
[
'http://example.com?a=1#foo',
'a',
'http://example.com#foo',
],
];
}

/**
* @return array
*/
Expand Down
34 changes: 34 additions & 0 deletions civihr_employee_portal/updates/7042.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Drupal\civihr_employee_portal\Helpers\UrlHelper;

/**
* Remove photo=0 from all contact image URLs
*
* It was added to fix a warning in CiviCRM, but this warning is no longer
* generated and the addition of photo=0 causes problems for image display
*/
function civihr_employee_portal_update_7042() {
civicrm_initialize();
$contacts = civicrm_api3('Contact', 'get');
$contacts = CRM_Utils_Array::value('values', $contacts, []);

foreach ($contacts as $contact) {
$imageUrl = CRM_Utils_Array::value('image_URL', $contact);

if (!$imageUrl) {
continue;
}

if (strpos($imageUrl, 'photo=0') === FALSE) {
continue;
}

$newImageUrl = UrlHelper::removeQueryValueFromUrl($imageUrl, 'photo');

civicrm_api3('Contact', 'create', [
'id' => $contact['id'],
'image_URL' => $newImageUrl,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ $handler->display->display_options['fields']['image_URL']['empty'] = 'no image';
$handler->display->display_options['fields']['image_URL']['empty_zero'] = TRUE;
$handler->display->display_options['fields']['image_URL']['hide_alter_empty'] = FALSE;
$handler->display->display_options['fields']['image_URL']['url_only'] = 0;
$handler->display->display_options['fields']['image_URL']['image_style'] = 'thumbnail';
/* Field: CiviCRM Contacts: Contact ID */
$handler->display->display_options['fields']['id']['id'] = 'id';
$handler->display->display_options['fields']['id']['table'] = 'civicrm_contact';
Expand Down

0 comments on commit 8f955f7

Please sign in to comment.