<?php
/**
 * @package     Joomla.Site
 * @subpackage  com_content
 *
 * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\Registry\Registry;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Language\Associations;

HTMLHelper::addIncludePath(JPATH_COMPONENT . '/helpers/html');
HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('behavior.framework');

// Create a shortcut for params.
$params = $this->item->params;
$images  = json_decode($this->item->images);
$canEdit = $this->item->params->get('access-edit');
$info    = $params->get('info_block_position', 0);

$aInfo1 = ($params->get('show_publish_date') || $params->get('show_category') || $params->get('show_parent_category') || $params->get('show_author'));
$aInfo2 = ($params->get('show_create_date') || $params->get('show_modify_date') || $params->get('show_hits'));
$topInfo = ($aInfo1 && $info != 1) || ($aInfo2 && $info == 0);
$botInfo = ($aInfo1 && $info == 1) || ($aInfo2 && $info != 0);
$icons = $params->get('access-edit') || $params->get('show_print_icon') || $params->get('show_email_icon');

// Check if associations are implemented. If they are, define the parameter.
$assocParam = (Associations::isEnabled() && $params->get('show_associations'));

// update catslug if not exists - compatible with 2.5
if (empty ($this->item->catslug)) {
  $this->item->catslug = $this->item->category_alias ? ($this->item->catid.':'.$this->item->category_alias) : $this->item->catid;
}

// Get Extrafields
$extrafields = new Registry($this->item->attribs);

$pjType = $extrafields->get('project-type');
$pjLocation = $extrafields->get('location');
$pjInfo = $extrafields->get('other-info');

$timePublishDown = $this->item->publish_down != null ? $this->item->publish_down : '';
$timePublishUp = $this->item->publish_up != null ? $this->item->publish_up : '';
?>

<?php if ($this->item->state == 0 || strtotime($timePublishUp) > strtotime(Factory::getDate())
|| ((strtotime($timePublishDown) < strtotime(Factory::getDate())) && !in_array($this->item->publish_down,array('',Factory::getDbo()->getNullDate())) )) : ?>
<div class="system-unpublished">
<?php endif; ?>

	<!-- Article -->
	<article <?php if($pjType) echo 'class="view-project"' ;?>>
    <div class="image-wrap">
      <?php echo LayoutHelper::render('joomla.content.intro_image', $this->item); ?>
    </div>

    <div class="intro-wrap">
      <?php if ($params->get('show_title')) : ?>
  			<?php echo LayoutHelper::render('joomla.content.item_title', array('item' => $this->item, 'params' => $params, 'title-tag'=>'h2')); ?>
      <?php endif; ?>
  	
      <?php if($pjType) :?>
        <span class="project-type"><?php echo $pjType ;?></span>
      <?php endif ;?>

      <?php if (!$params->get('show_intro')) : ?>
        <?php // Content is generated by content plugin event "onContentAfterTitle" ?>
        <?php echo $this->item->event->afterDisplayTitle; ?>
      <?php endif; ?>

      <!-- Aside -->
      <?php if ($topInfo || $icons) : ?>
      <aside class="article-aside clearfix">
        <?php if ($topInfo): ?>
        <?php echo LayoutHelper::render('joomla.content.info_block.block', array('item' => $this->item, 'params' => $params, 'position' => 'above')); ?>
        <?php endif; ?>
        
        <?php if ($icons): ?>
        <?php echo LayoutHelper::render('joomla.content.icons', array('item' => $this->item, 'params' => $params)); ?>
        <?php endif; ?>
      </aside>  
      <?php endif; ?>
      <!-- //Aside -->

      <?php if ($params->get('show_intro')) : ?>
  		<section class="article-intro clearfix">
        <?php // Content is generated by content plugin event "onContentBeforeDisplay" ?>
  			<?php echo $this->item->event->beforeDisplayContent; ?>

  			<?php echo $this->item->introtext; ?>
  		</section>
      <?php endif; ?>

      <!-- footer -->
      <?php if ($botInfo) : ?>
      <footer class="article-footer clearfix">
        <?php // Todo: for Joomla4 joomla.content.info_block.block can be changed to joomla.content.info_block ?>
        <?php echo LayoutHelper::render('joomla.content.info_block.block', array('item' => $this->item, 'params' => $params, 'position' => 'below')); ?>
      </footer>
      <?php endif; ?>
      <!-- //footer -->


  		<?php if ($params->get('show_readmore') && $this->item->readmore) :
  			if ($params->get('access-view')) :
  				$link = Route::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid, $this->item->language));
  			else :
  				$menu      = Factory::getApplication()->getMenu();
  				$active    = $menu->getActive();
  				$itemId    = $active->id;
          $link = new Uri(Route::_('index.php?option=com_users&view=login&Itemid=' . $itemId, false));
          $link->setVar('return', base64_encode(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid, $this->item->language)));
  			endif; ?>

        <?php echo LayoutHelper::render('joomla.content.readmore', array('item' => $this->item, 'params' => $params, 'link' => $link)); ?>
        
  		<?php endif; ?>
    </div>
	</article>
	<!-- //Article -->

  <?php if ($this->item->state == 0 || strtotime($timePublishUp) > strtotime(Factory::getDate())
|| ((strtotime($timePublishDown) < strtotime(Factory::getDate())) && !in_array($this->item->publish_down,array('',Factory::getDbo()->getNullDate())) )) : ?>
</div>
<?php endif; ?>

<?php // Content is generated by content plugin event "onContentAfterDisplay" ?>
<?php echo $this->item->event->afterDisplayContent; ?> 
