-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #499: Contact link viewhelper
* pr-499: Issue #496
- Loading branch information
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* YAWIK | ||
* | ||
* @filesource | ||
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) | ||
* @license MIT | ||
*/ | ||
|
||
/** Core view helpers */ | ||
namespace Core\View\Helper; | ||
|
||
use Zend\View\Helper\AbstractHelper; | ||
use Auth\Entity\Info; | ||
|
||
/** | ||
* This helper generates HTML email link markup from a user info entity. | ||
* | ||
* <code> | ||
* | ||
* $this->contactLink($user->getInfo()); | ||
* // Outputs: <a href="mailto:[email protected]">Fancy Name</a> | ||
* | ||
* $this->contactLink($user->getInfo()); | ||
* // Outputs: <a href="mailto:[email protected]">[email protected]</a> | ||
* | ||
* $this->contactLink($user->getInfo()); | ||
* // Outputs: Fancy Name | ||
* </code> | ||
* | ||
*/ | ||
class ContactLink extends AbstractHelper | ||
{ | ||
|
||
/** | ||
* generates an email link from a user info entity | ||
* | ||
* @param \Auth\Entity\Info $userInfo | ||
* @param array $attributes | ||
* @return string | ||
*/ | ||
public function __invoke(Info $userInfo, array $attributes = array()) | ||
{ | ||
$email = $userInfo->getEmail(); | ||
$displayName = $userInfo->getDisplayName(false); | ||
|
||
if ($email) { | ||
$label = $displayName | ||
? $displayName | ||
: $email; | ||
|
||
$attributesStr = $attributes | ||
? $this->createAttributesString($attributes) | ||
: ''; | ||
|
||
return sprintf('<a %s href="mailto:%s">%s</a>', $attributesStr, $email, $label); | ||
} else { | ||
return $displayName | ||
? $displayName | ||
: ''; | ||
} | ||
} | ||
|
||
protected function createAttributesString(array $attributes) | ||
{ | ||
$renderer = $this->getView(); | ||
$escape = $renderer->plugin('escapehtml'); | ||
$escapeAttr = $renderer->plugin('escapehtmlattr'); | ||
$attr = array(); | ||
|
||
foreach ($attributes as $name => $value) { | ||
$attr[] = $escape($name) . (strlen($value) ? ('="' . $escapeAttr($value) . '"') : ''); | ||
} | ||
|
||
return implode(' ', $attr); | ||
} | ||
} |