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. * diff --git a/civihr_employee_portal/src/Forms/OnboardingWebForm.php b/civihr_employee_portal/src/Forms/OnboardingWebForm.php index 72794165..6dc2126d 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. * @@ -238,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; - } - } 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..f6c85a7f 100644 --- a/civihr_employee_portal/tests/Helpers/UrlHelperTest.php +++ b/civihr_employee_portal/tests/Helpers/UrlHelperTest.php @@ -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 */ 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, + ]); + } +} 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';