Skip to content

Commit

Permalink
Merge a1cdf3b into f9e15d8
Browse files Browse the repository at this point in the history
  • Loading branch information
TiSiE committed Jun 4, 2020
2 parents f9e15d8 + a1cdf3b commit 3b88983
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
2 changes: 2 additions & 0 deletions module/Core/config/module.config.php
Expand Up @@ -554,6 +554,7 @@
\Core\View\Helper\Snippet::class => \Core\Factory\View\Helper\SnippetFactory::class,
\Core\View\Helper\Proxy::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
\Core\View\Helper\ModuleVersion::class => \Core\View\Helper\ModuleVersionFactory::class,
\Core\View\Helper\FormatLocation::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
),
'initializers' => array(
// '\Core\View\Helper\Service\HeadScriptInitializer',
Expand All @@ -564,6 +565,7 @@
'proxy' => \Core\View\Helper\Proxy::class,
'form_element' => 'formElement',
'moduleVersion' => \Core\View\Helper\ModuleVersion::class,
'formatLocation' => \Core\View\Helper\FormatLocation::class,
],
),

Expand Down
119 changes: 119 additions & 0 deletions module/Core/src/View/Helper/FormatLocation.php
@@ -0,0 +1,119 @@
<?php

/**
* Aviation
*
*/

declare(strict_types=1);

namespace Core\View\Helper;

use Core\Entity\LocationInterface;
use Doctrine\Common\Collections\Collection;
use Laminas\View\Helper\AbstractHelper;

/**
* TODO: description
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* TODO: write tests
*/
class FormatLocation extends AbstractHelper
{
protected $defaultFormat = '%S%Z%r%C';

public function __invoke($location = null, ?string $format = null, $separator = '<br>')
{
if ($location === null) {
return $this;
}

if ($location instanceof Collection) {
return $this->formatCollection($location, $format, $separator);
}

return $this->format($location, $format);
}

public function format(LocationInterface $location, ?string $format = null)
{
if ($format == null) {
$format = $this->defaultFormat;
}

$placeholders = [];
preg_match_all('~%[cCzZrsSn](?::[^\s%]*)?~s', $format, $matches);

$variables = [
'%c' => "getCity",
'%C' => "getCountry",
'%z' => "getPostalCode",
'%Z' => ["getPostalCode", "getCity"],
'%r' => "getRegion",
'%s' => "getStreetname",
'%S' => ["getStreetname", "getStreetnumber"],
'%n' => "getStreetnumber",
];

$str = str_replace('%%', ':percent:', $format);

foreach ($matches[0] as $var) {
if (strpos($var, ':') !== false) {
[$ph, $sep] = explode(':', $var, 2);
} else {
$ph = $var;
$sep = null;
}

$method = $variables[$ph];
if (is_array($method)) {
$val = [];
foreach ($method as $m) {
$val[] = $location->$m();
}
$val = join(' ', $val);
} else {
$val = $location->$method();
}

$val = trim((string) $val);

if (!$val) {
$str = str_replace($var, '', $str);
continue;
}

$placeholders[] = [$var, $val, $sep];
}

while ($placeholder = array_shift($placeholders)) {
[$var, $val, $sep] = $placeholder;

if (count($placeholders)) {
$val .= $sep === null ? ', ' : $sep;
}

$str = str_replace($var, $val, $str);
}

if (strpos($format, '%lon') !== false || strpos($format, '%lat' !== false)) {
$coords = $location->getCoordinates()->getCoordinates();
$str = str_replace(['%lon', '%lat'], [$coords[0], $coords[1]], $str);
}

$str = str_replace([':percent:', ':s:'], ['%', ' '], $str);
return $str;
}

public function formatCollection($locations, $format, $separator = '<br>')
{
$loc = [];

foreach ($locations as $l) {
$loc[] = $this->format($l, $format);
}

return join($separator, $loc);
}
}
10 changes: 5 additions & 5 deletions module/Jobs/view/jobs/index/index.ajax.phtml
Expand Up @@ -15,7 +15,7 @@ $snapshots = $this->services('repositories.Jobs/JobSnapshot');
<tr>
<th><?php echo $this->translate('Date of receipt')?></th>
<th><?php echo $this->translate('Title of the job')?></th>
<th><?php echo $this->translate('Location')?></th>
<th><?php echo $this->translate('Location')?></th>
<th><?php echo $this->translate('Companyname')?></th>
<th><?php echo $this->translate('Reference')?></th>
<?php if ($isRecruiter):?>
Expand Down Expand Up @@ -46,7 +46,7 @@ $isSnapshot = $job->hasSnapshotDraft();

?>)
<?php endif ?></td>

<td>
<?php if ($isSnapshot): ?><span class="label label-warning"><?=$this->translate('Changed')?></span><?php
elseif ( ($snapshot = $job->getLatestSnapshot()) ):
Expand All @@ -65,7 +65,7 @@ $isSnapshot = $job->hasSnapshotDraft();
<small><?=$job->getClassifications()->getProfessions()?></small> |
<small><?=$job->getClassifications()->getEmploymentTypes()?></small>
</td>
<td><?php echo $job->getLocation()?></td>
<td><?php echo $this->formatLoaction($job->getLocations())?></td>
<td><?php echo $job->getCompany()?></td>
<td><?php echo empty($job->getReference())? $job->getApplyId():$job->getReference() ?></td>

Expand All @@ -77,8 +77,8 @@ $isSnapshot = $job->hasSnapshotDraft();
</a>
<?php endif;?>
<?php if ($job->getUnreadApplications()->count()):?>
/
<a title="<?php echo $this->translate("unread")?>"
/
<a title="<?php echo $this->translate("unread")?>"
href="<?php echo $this->url( 'lang/applications', array(), array('query' => 'job=' . $job->getId() . '&unread=1'), true) ?>">
<span class="badge"> <?php echo $job->getUnreadApplications()->count()?></span>
</a>
Expand Down
2 changes: 1 addition & 1 deletion module/Jobs/view/jobs/jobboard/index.ajax.phtml
Expand Up @@ -100,7 +100,7 @@ $this->proxy('jobsByMailSubscriptionForm')->render($jobs)
<?php endif; ?>
</td>
<td>
<div><?php echo $job->getLocation()?></div>
<div><?php echo $this->formatLocation($job->getLocations())?></div>
<small>
<?php
if ($job->getDatePublishStart()): echo $this->dateFormat($job->getdatePublishStart(), 'short', 'none');
Expand Down

0 comments on commit 3b88983

Please sign in to comment.