<?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright https://yawik.org/COPYRIGHT.php
 * @license   MIT
 */
/**
 * generates the list of organizations.
 */
?>
<?php
$columns = [
    [
        'label' => $this->translate('Organization'),
        'sort'  => '_organizationName',
    ],
    [
        'label' => $this->translate('City'),
        'sort' => 'contact.city',
    ],
    [
        'label' => $this->translate('Street'),
        'sort' => 'contact.street',
    ],
];

$sort = $this->params()->fromQuery('sort', 'name');
if (false !== strpos($sort, '-')) {
    $sortBy  = substr($sort, 1);
    $sortDir = false;
} else {
    $sortBy  = $sort;
    $sortDir = true;
}
?>
<table class="table table-striped table-bordered table-hover" id="cam-organization-list">
    <thead>
    <tr>
        <?php foreach ($columns as $spec):?>
            <th<?php echo isset($spec['class']) ? ' class="' . $spec['class'] . '"' : '' ?>>
                <?php if (! isset($spec['sort'])): echo $spec['label'];
                else:
                    if ($spec['sort'] == $sortBy):
                        $icon = '<span style="margin-top: 5px;" class="pull-right text-right yk-icon yk-icon-sort' . ($sortDir ? '-desc' : '-asc') . '"></span>';
                        $dir = !$sortDir;
                    else:
                        $dir = true;
                        $icon = '<span style="margin-top: 5px" class="pull-right yk-icon yk-icon-sort"></span>';
                    endif;
                    $sortParam = array('sort' => ($dir ? '' : '-') . $spec['sort']);
                    $url = $this->url(
                                'lang/organizations/profile', array(),
                                array('query' => array_merge($this->params()->fromQuery(), $sortParam)),
                                true
                    );
                    ?>
                    <a style="width: 100%; display: inline-block;" href="<?php echo $url?>"><?php echo $spec['label'].$icon ?></a>
                <?php endif; ?>
            </th>
        <?php endforeach; ?>
    </tr>
    </thead>
<?php foreach ($this->organizations as $organization): /* @var $organization \Organizations\Entity\Organization */?>
 <tr>
  <td>
      <?php if ($organization->getOrganizationName() && !is_null($image=$organization->getImages()->get('thumbnail'))): ?>
          <div class="yk-logo-list">
              <img class="yk-logo-sm" src="<?php echo $this->basePath($this->organizationImageCache->getUri($image)) ?>">
          </div>
      <?php endif ?>
      <a href="<?php echo $this->url('lang/organizations/profileDetail', array('id' => $organization->getId())); ?>?clear=1">
    <?php
        $nameEntity = $organization->getOrganizationName();
        echo isset($nameEntity)?$nameEntity->getName():'**** DRAFT ****';
    ?>
      </a>
      <?php if ($organization->isHiringOrganization()): $parent = $organization->getParent(); ?>
          <br><small><?php echo sprintf(
                $this->translate('Hiring organization of <strong>%s</strong>'),
                '<a href="' . $this->url('lang/organizations/profileDetail',
                                         array('id' => $parent->getId())) . '?clear=1">' . $parent->getOrganizationName()->getName() . '</a>'
          );?></small>

        <?php endif ?>
  </td>
  <td>
      <?php echo $organization->getContact()->getPostalcode();?>
      <?php echo $organization->getContact()->getCity();?>
  </td>
  <td>
      <?php echo $organization->getContact()->getStreet();?>
      <?php echo $organization->getContact()->getHouseNumber();?>
  </td>
 </tr>
<?php endforeach?>
</table>

<?php echo $this->paginationControl($this->organizations, 'Sliding', 'pagination-control',
                                    ['lang' => $this->params('lang'), 'route' => 'lang/organizations/profile']
);?>
