From 60220453865696e60dac25cd6f7287ff1f21c28b Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 14:57:52 +0100 Subject: [PATCH 1/7] PCHR-4281: Remove image style from Image URL field The display handling of the thumbnail image style is not compatible with images uploaded via webform so this field needs to be reverted to default style --- .../views/views_export/views_mydetails_personal.inc | 1 - 1 file changed, 1 deletion(-) diff --git a/civihr_employee_portal/views/views_export/views_mydetails_personal.inc b/civihr_employee_portal/views/views_export/views_mydetails_personal.inc index dfdaaa06..d0a74eb2 100644 --- a/civihr_employee_portal/views/views_export/views_mydetails_personal.inc +++ b/civihr_employee_portal/views/views_export/views_mydetails_personal.inc @@ -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'; From f3d645ad9588f1159f2a325e566e59780fe33721 Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 16:22:55 +0100 Subject: [PATCH 2/7] PCHR-4281: Add function for removing single query value from URL --- .../src/Helpers/UrlHelper.php | 40 ++++++++++++++ .../tests/Helpers/UrlHelperTest.php | 52 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/civihr_employee_portal/src/Helpers/UrlHelper.php b/civihr_employee_portal/src/Helpers/UrlHelper.php index 35c5c61d..9145e042 100644 --- a/civihr_employee_portal/src/Helpers/UrlHelper.php +++ b/civihr_employee_portal/src/Helpers/UrlHelper.php @@ -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; + } + } diff --git a/civihr_employee_portal/tests/Helpers/UrlHelperTest.php b/civihr_employee_portal/tests/Helpers/UrlHelperTest.php index 76d7aa57..136a0ce3 100644 --- a/civihr_employee_portal/tests/Helpers/UrlHelperTest.php +++ b/civihr_employee_portal/tests/Helpers/UrlHelperTest.php @@ -14,6 +14,58 @@ 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', + ], + ]; + } + /** * @return array */ From 87e1bdf33dc1dff5adb033d8f25dfdbdbc8e4520 Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 16:23:41 +0100 Subject: [PATCH 3/7] PCHR-4281: Remove hacky fix for image URL The warning doesn't appear in newer versions of CiviCRM so this is not required --- .../src/Forms/OnboardingWebForm.php | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/civihr_employee_portal/src/Forms/OnboardingWebForm.php b/civihr_employee_portal/src/Forms/OnboardingWebForm.php index 72794165..60dac397 100644 --- a/civihr_employee_portal/src/Forms/OnboardingWebForm.php +++ b/civihr_employee_portal/src/Forms/OnboardingWebForm.php @@ -31,7 +31,6 @@ public function onSubmit($node, $values) { } $this->setWorkEmailAsPrimary($node, $values, $contactID); - $this->hackyFixForImageURL($contactID); } /** @@ -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. * From ce09df4ecaedba36552f90d9498f116ab22726c8 Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 16:25:06 +0100 Subject: [PATCH 4/7] PCHR-4281: (unrelated) Remove unused code --- .../src/Forms/OnboardingWebForm.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/civihr_employee_portal/src/Forms/OnboardingWebForm.php b/civihr_employee_portal/src/Forms/OnboardingWebForm.php index 60dac397..6dc2126d 100644 --- a/civihr_employee_portal/src/Forms/OnboardingWebForm.php +++ b/civihr_employee_portal/src/Forms/OnboardingWebForm.php @@ -204,18 +204,4 @@ private function getSkipButtonMarkup() { return sprintf('%s', $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; - } - } From b8e429b0409845282de5abe949cc0c342ce7e36b Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 16:53:54 +0100 Subject: [PATCH 5/7] PCHR-4281: Add test --- civihr_employee_portal/tests/Helpers/UrlHelperTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/civihr_employee_portal/tests/Helpers/UrlHelperTest.php b/civihr_employee_portal/tests/Helpers/UrlHelperTest.php index 136a0ce3..f6c85a7f 100644 --- a/civihr_employee_portal/tests/Helpers/UrlHelperTest.php +++ b/civihr_employee_portal/tests/Helpers/UrlHelperTest.php @@ -63,6 +63,11 @@ public function urlWithQueryProvider() { 'photo', 'http://civihr.local/sites/default/files/webform/500.jpg', ], + [ + 'http://example.com?a=1#foo', + 'a', + 'http://example.com#foo', + ], ]; } From bf4f22387558a9c6b0b9acab3ec304741e8908d9 Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 16:59:15 +0100 Subject: [PATCH 6/7] PCHR-4281: Autoload all new upgraders in updates directory --- .../civihr_employee_portal.install | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/civihr_employee_portal/civihr_employee_portal.install b/civihr_employee_portal/civihr_employee_portal.install index 9db733b9..b6750c82 100644 --- a/civihr_employee_portal/civihr_employee_portal.install +++ b/civihr_employee_portal/civihr_employee_portal.install @@ -1,10 +1,16 @@ ['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. * From 0cc10429437e4a3b446c4250d986fe6a1206c6da Mon Sep 17 00:00:00 2001 From: Michael Devery Date: Mon, 8 Oct 2018 16:59:30 +0100 Subject: [PATCH 7/7] PCHR-4281: Add upgrader to fix existing image URLs --- civihr_employee_portal/updates/7042.php | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 civihr_employee_portal/updates/7042.php diff --git a/civihr_employee_portal/updates/7042.php b/civihr_employee_portal/updates/7042.php new file mode 100644 index 00000000..459cbda7 --- /dev/null +++ b/civihr_employee_portal/updates/7042.php @@ -0,0 +1,34 @@ + $contact['id'], + 'image_URL' => $newImageUrl, + ]); + } +}