<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$fp = new Permissions($f);
$c = Page::getCurrentPage();
$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
$ih = $app->make('helper/image');
$nh = $app->make('helper/navigation');
$cID = $c->cID;



//if you have some content entries print out the html
if(count($rows) > 0) : ?>

<div class="content-list--default">

	<?php if($contentListTitle != '' && isset($contentListTitle)): ?>

		<h3 class="content-list__main-title"><?= h($contentListTitle); ?></h3>

	<?php endif; ?>
    
    <div class="row">
        <div class="small-12 medium-7 medium-centered large-12 columns">
        	<div class="row small-up-1 large-up-3">
            
            <?php foreach($rows as $row) :
        		//reset variables;
                $title = $subtitle = $description = $button = false;
        		$image = '';
        		$buttonUrl = '';
        		$buttonText = '';
        		$buttonTitleAttr = '';
        		$target = '';
                $trackingLink = false;

        		if(isset($row['title'])){ $title = $row['title']; }
                if(isset($row['subtitle'])){ $subtitle = $row['subtitle']; }
                if(isset($row['description'])){ $description = $row['description']; }

        		/**************************************************
        		*
        		* image
        		*
        		**************************************************/
        		//create image tag
        		if($row['image']) {
                    $image = $row['image']->getThumbnailURL('small');
                    if(!$image->src){
            			$image = $ih->getThumbnail($row['image'], 432, 300, true);
                    }
        		}


        		if ($row['buttonText']):
        			/**************************************************
        			*
        			* button & link
        			*
        			**************************************************/

        	        if (isset($row['buttonTitleAttr'])) {
        	            $buttonTitleAttr = $row['buttonTitleAttr'];
        	        } elseif (isset($row['buttonText'])) {
        	            $buttonTitleAttr = $row['buttonText'];
        	        } else {
        	            $buttonTitleAttr = 'Button';
        	        }

        	        if (isset($row['buttonText'])) {
                        $buttonText = $row['buttonText'];
                        $button = true;
                    } elseif (isset($row['buttonTitleAttr'])) {
                        $buttonText = $row['buttonTitleAttr'];
                    } else {
                        $buttonText = 'Button';
                    }

        			//check the button type
        			//1 = internal link
        			//2 = external link
        			//3 = file download

        	        //if button type 3 and there is a file
        			if (($row['linkType']) == 3 &&  $row['file']) {
                        $file = $controller->getFileObject($row['fileID']);
                        $fp = new Permissions($file);

                        if ($fp->canViewFile()) {

                            $buttonUrl = ($row['forceDownload'])?$file->getForceDownloadURL():$file->getURL();
                            $button = true;
                            $trackingLink = " onclick=\"trackOutboundLink('".$file->getURL()."'); return false;\" rel=\"nofollow\"";
                        }

        	        } else {

        		        //show the button if buttonText has been entered
        		        //some clients like to add the button in so they know it is there and add a url when it is ready
        		        $target = '';

        		        //check if the link is external or internal
        		        if ($row['linkType'] == 2) {
        		            $buttonUrl = $row['linkURL'];
        		            $target = ' target="_blank"';
        		            $button = true;
        		        } else {
        		            $buttonUrl = (empty($row['linkPage']) || $row['linkPage']->error) ? '' : $nh->getLinkToCollection($row['linkPage']);
        		            $button = true;
        		        }
        		    }

        		endif; ?>

        		<div class="column content-list__item">
                    <!-- image -->
                    <?php if($button) : ?>
        			    <a href="<?= $buttonUrl; ?>"<?=$trackingLink?> title="<?= h($row['title']); ?>" class="content-list__img"<?= $target; ?>>
                    <?php endif; ?><img src="<?= $image->src ?>" height="<?= $image->height ?>" width="<?= $image->width ?>" alt="#"><?php if($button) : ?>
                        </a>
                    <?php endif; ?>
        			<!-- image -->

        			<!-- title -->
                    <?php if($title): ?>
                        <h3 class="content-list__title"><?= h($title); ?></h3>
                    <?php endif; ?>
                    <!-- end title -->

                    <!-- subtitle -->
                    <?php if($subtitle): ?>
                        <p class="content-list__subtitle"><?= h($subtitle); ?></p>
                    <?php endif; ?>
                    <!-- end subtitle -->

                    <!-- description -->
                    <?php if($description): ?>
                        <div class="content-list__description">
                            <?= $description; ?>
                        </div>
                    <?php endif; ?>
                    <!-- end description -->

        			<!-- button -->
        			<?php if($button) : ?>
        			<a href="<?= $buttonUrl; ?>"<?=$trackingLink?> class="medium button button--arrow-right content-list__button" title="<?= $buttonTitleAttr; ?>"<?= $target; ?>><?= h($buttonText);?></a>
        			<?php endif; ?>
        			<!-- end button -->
        		</div>
        	<?php endforeach; ?>
        	</div>
        </div>
    </div>
</div>
<?php endif; ?>

