1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 
<?php
/* --------------------------------------------------------------
   ProductMediaContentController.inc.php 2017-09-25
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2017 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('HttpViewController');

/**
 * Class ProductMediaContentController
 *
 * @extends    HttpViewController
 * @category   System
 * @package    HttpViewControllers
 */
class ProductMediaContentController extends HttpViewController
{
    /**
     * Renders a content resource by the query parameter "pcdId".
     *
     * @return \HttpControllerResponse
     */
    public function actionDefault()
    {
        $result   = $this->_getProductContentDescriptionData();
        $isImage  = $this->_isImageFile($result['resource']);
        $filePath = $isImage ? DIR_WS_CATALOG . 'media/products/' . $result['resource'] : DIR_FS_CATALOG
                                                                                          . 'media/products/'
                                                                                          . $result['resource'];
        
        $content     = $isImage ? xtc_image($filePath) : $this->_include($filePath);
        $templateCss = 'templates/' . CURRENT_TEMPLATE . '/stylesheet.css';
        
        $html = <<<HTML
<html>
    <head>
        <title>{$result['title']}</title>
        <link rel="stylesheet" href="$templateCss">
    </head>
    <body>
        $content
    </body>
</html>
HTML;
        
        return MainFactory::create('HttpControllerResponse', $html);
    }
    
    
    /**
     * Redirects to a product content resource (link) and increment the database field "hits".
     *
     * @return \RedirectHttpControllerResponse
     */
    public function actionLink()
    {
        return MainFactory::create('RedirectHttpControllerResponse',
                                   $this->_getProductContentDescriptionData()['resource']);
    }
    
    
    /**
     * Returns the product content description data by the query argument "pcdID".
     * The field product_content_descriptions.hits will be incremented by the value "1".
     *
     * @return array Product content description data array.
     */
    protected function _getProductContentDescriptionData()
    {
        $queryBuilder = StaticGXCoreLoader::getDatabaseQueryBuilder();
        
        $queryResult = $queryBuilder->select()
                                    ->from('product_content_descriptions as pcd')
                                    ->join('product_content_resources as pcr',
                                           'pcd.id = pcr.product_content_description_id')
                                    ->where('pcd.id', $this->_getQueryParameter('pcdId'))
                                    ->get()
                                    ->row_array();
        
        $queryBuilder->update('product_content_descriptions', ['hits' => (int)$queryResult['hits'] + 1],
                              ['id' => $queryResult['id']]);
        
        return $queryResult;
    }
    
    
    /**
     * Checks if the given file has an image file extension.
     * Detected image extensions are "gif", "jpg", "png", "tif" and "bmp".
     *
     * @param string $fileName File to be checked.
     *
     * @return bool Whether true if the given file has an image file extension and false otherwise.
     */
    protected function _isImageFile($fileName)
    {
        $imageExt = [
            'gif',
            'jpg',
            'png',
            'tif',
            'bmp',
        ];
        
        $fileExtension = substr($fileName, strpos($fileName, '.') + 1);
        
        return in_array($fileExtension, $imageExt);
    }
    
    
    /**
     * Includes a file by the given file name and returns their data.
     * If the given file do not exists, and empty string will be returned.
     *
     * @param string $fileName Path to included file.
     *
     * @return string Data of included file or empty string, if the file was not found.
     */
    protected function _include($fileName)
    {
        if(!is_file($fileName))
        {
            return '';
        }
        
        ob_start();
        include $fileName;
        
        return ob_get_clean();
    }
}