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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 
<?php
/* --------------------------------------------------------------
 ContentNavigationTrait.inc.php 2018-01-15
 Gambio GmbH
 http://www.gambio.de
 Copyright (c) 2018 Gambio GmbH
 Released under the GNU General Public License (Version 2)
 [http://www.gnu.org/licenses/gpl-2.0.html]
 --------------------------------------------------------------
 */

trait ContentManagerContentNavigationTrait
{
    /**
     * @var array
     */
    protected $contentTypeFileFlagMap = [
        'pages_main'      => 'topmenu',
        'pages_secondary' => 'topmenu_corner',
        'pages_info'      => 'content',
        'pages_info_box'  => 'information',
        'elements_start'  => 'extraboxes',
        'elements_header' => 'extraboxes',
        'elements_footer' => 'extraboxes',
        'elements_boxes'  => 'extraboxes',
        'elements_others' => 'extraboxes',
        'elements_withdrawal' => 'withdrawal',
    ];
    
    /**
     * @var array
     */
    protected $fileTypMap = [
        'information',
        'content',
        'topmenu_corner',
        'topmenu',
        'extraboxes',
        'withdrawal'
    ];

    /**
     * @var bool
     */
    protected $isExpertMode = false;
    
    
    /**
     * Creates the content navigation object for the content manager templates.
     *
     * @param \LanguageTextManager $languageTextManager Text manager instance to fetch texts.
     * @param string|null          $current             (Optional) Whether "pages", "elements" or "productContents" to
     *                                                  set nav item active.
     *
     * @return \ContentNavigationCollection
     */
    protected function _createContentNavigation(LanguageTextManager $languageTextManager, $current = null)
    {
        $pagesTitle = new StringType($languageTextManager->get_text('PAGE_TITLE_PAGES'));
        $pagesUrl   = new StringType('admin.php?do=ContentManagerPages');
        
        $elementsTitle = new StringType($languageTextManager->get_text('PAGE_TITLE_ELEMENTS'));
        $elementsUrl   = new StringType('admin.php?do=ContentManagerElements');
        
        $productContentsTitle = new StringType($languageTextManager->get_text('PAGE_TITLE_PRODUCT_CONTENTS'));
        $productContentsUrl   = new StringType('admin.php?do=ContentManagerProductContents');
        
        $contentNavigation = MainFactory::create('ContentNavigationCollection', []);
        
        $true  = new BoolType(true);
        $false = new BoolType(false);
        
        $contentNavigation->add($pagesTitle, $pagesUrl, $current === 'pages' ? $true : $false);
        $contentNavigation->add($elementsTitle, $elementsUrl, $current === 'elements' ? $true : $false);
        $contentNavigation->add($productContentsTitle, $productContentsUrl,
                                $current === 'productContents' ? $true : $false);
        
        return $contentNavigation;
    }
    
    
    /**
     * Creates a new content group id.
     *
     * @param \CI_DB_query_builder $queryBuilder Query builder instance to access the database.
     *
     * @return int New content manager group id.
     */
    protected function _createNewContentGroupId(CI_DB_query_builder $queryBuilder)
    {
        return (int)$queryBuilder->select('content_group')
                                 ->from('content_manager')
                                 ->where('`content_group` < 3889891')
                                 ->order_by('content_group', 'DESC')
                                 ->limit(1)
                                 ->get()
                                 ->row_array()['content_group'] + 1;
    }
    
    
    /**
     * Whether redirects to the last overview or update pages.
     *
     * @param string $contentManagerType Name of content manager controller class.
     * @param int    $contentGroupId     Content id of last edited content.
     *
     * @return \RedirectHttpControllerResponse
     */
    protected function _getUpdateResponse($contentManagerType, $contentGroupId, $editMethod = 'edit')
    {
        $expertModeQueryParameter = $this->isExpertMode ? '&expert' : '';

        if(isset($this->_getPostData('content_manager')['content_group_id'])
           && $this->_getPostData('content_manager')['content_group_id'] > 0
        )
        {
            $contentGroupId = $this->_getPostData('content_manager')['content_group_id'];
        }
        
        $update = (int)$this->_getQueryParameter('update') === 1 ? true : false;
        if($update)
        {
            $selectedLanguage = $this->_getPostData('content_manager')['selected_language'];
            if(!empty($selectedLanguage))
            {
                $_SESSION['content_manager_selected_language'] = $selectedLanguage;
            }
            
            return MainFactory::create('RedirectHttpControllerResponse',
                                       'admin.php?do=' . $contentManagerType . '/' . $editMethod . '&id='
                                       . $contentGroupId . $expertModeQueryParameter);
        }
        
        return MainFactory::create('RedirectHttpControllerResponse',
                                   'admin.php?do=' . $contentManagerType . $expertModeQueryParameter);
    }
    
    
    /**
     * Inserts the given content data in the database.
     *
     * @param \CI_DB_query_builder $queryBuilder Query builder instance to access the database.
     * @param array                $contentData  Content data array.
     *
     * @return $this|\ContentManagerPagesController Same instance for chained method calls.
     */
    protected function _insertContentData(CI_DB_query_builder $queryBuilder, array $contentData)
    {
        foreach($contentData as $contentDataSet)
        {
            $queryBuilder->insert('content_manager', $contentDataSet);
            $queryBuilder->replace('content_manager_history', $contentDataSet);
        }
        
        return $this;
    }
    
    
    /**
     * Returns the assets for the content manager editing and creation pages.
     *
     * @return \AssetCollection
     */
    protected function _getAssets()
    {
        $assets = MainFactory::create('AssetCollection');
        $assets->add(MainFactory::create('Asset', 'admin_buttons.lang.inc.php'));
        $assets->add(MainFactory::create('Asset', 'content_manager.lang.inc.php'));
        $assets->add(MainFactory::create('Asset', 'shipping_and_payment_matrix.lang.inc.php'));
        $assets->add(MainFactory::create('Asset', 'includes/ckeditor/ckeditor.js'));
        
        return $assets;
    }
    
    
    /**
     * Returns an existing file object with the path to a content manager template file.
     * Take a look on the template files which are located in html/content/content_manager/$type directory
     * to know possible values for the $name argument.
     *
     * @param string $type Content manager type, whether "pages", "elements" or "product_contents".
     * @param string $name Name of template file.
     *
     * @return \ExistingFile
     */
    protected function _getTemplate($type, $name)
    {
        return new ExistingFile(new NonEmptyStringType(DIR_FS_ADMIN . '/html/content/content_manager/' . $type . '/'
                                                       . $name . '.html'));
    }
    
    
    /**
     * Updates the given content data in the database.
     *
     * @param \CI_DB_query_builder $queryBuilder   Query builder instance to access the database.
     * @param array                $contentData    Content data array.
     * @param int                  $contentGroupId Content group id.
     *
     * @return $this|\ContentManagerPagesController Same instance for chained method calls.
     */
    protected function _updateContentData(CI_DB_query_builder $queryBuilder, array $contentData, $contentGroupId)
    {
        foreach($contentData as $contentDataSet)
        {
            $queryBuilder->update('content_manager', $contentDataSet, [
                'content_group' => $contentGroupId,
                'languages_id'  => $contentDataSet['languages_id']
            ]);
            $queryBuilder->replace('content_manager_history', $contentDataSet);
        }
        
        return $this;
    }
    
    
    /**
     * Returns an array with allowed script files for content data.
     *
     * @return array List with allowed script files.
     */
    protected function _getScriptPageFiles()
    {
        $contentFileDirectory = DIR_FS_CATALOG . 'media/content/';
        $scriptPageFiles      = [];
        $ignoredScripts       = ['.', '..', 'index.html'];
        
        $iterator = new IteratorIterator(new DirectoryIterator($contentFileDirectory));
        
        $scriptPageFiles[''] = $this->languageTextManager->get_text('TEXT_NO_SELECTION', 'admin_general');
        foreach($iterator as $scriptFile)
        {
            /** @var \DirectoryIterator $scriptFile */
            if(!in_array($scriptFile->getFilename(), $ignoredScripts))
            {
                $scriptPageFiles[$scriptFile->getFilename()] = $scriptFile->getFilename();
            }
        }
        
        return $scriptPageFiles;
    }
    
    
    /**
     * Returns an array with allowed script files for content data.
     *
     * @return array List with allowed script files.
     */
    protected function _getProductsContentFiles()
    {
        $contentFileDirectory = DIR_FS_CATALOG . 'media/products/';
        $scriptPageFiles      = [];
        $ignoredScripts       = ['.', '..', 'index.html'];
        
        $iterator = new IteratorIterator(new DirectoryIterator($contentFileDirectory));
        
        $scriptPageFiles[''] = $this->languageTextManager->get_text('TEXT_SELECT', 'admin_general');
        foreach($iterator as $scriptFile)
        {
            /** @var \DirectoryIterator $scriptFile */
            if(!in_array($scriptFile->getFilename(), $ignoredScripts))
            {
                $scriptPageFiles[$scriptFile->getFilename()] = $scriptFile->getFilename();
            }
        }
        
        return $scriptPageFiles;
    }
    
    
    /**
     * Returns true if the "Responsive File Manager" is installed an false otherwise.
     *
     * @return bool
     */
    protected function _isFilemanagerAvailable()
    {
        $fileManagerConfiguration = MainFactory::create('ResponsiveFileManagerConfigurationStorage');
        return $fileManagerConfiguration->isInstalled() && $fileManagerConfiguration->get('use_in_content_manager_pages');
    }

    /**
     * Sets the expert mode, if the query parameter has been passed.
     */
    protected function _setExpertMode()
    {
        $this->isExpertMode = $this->_getQueryParameter('expert') !== null;
    }
    
    
    /**
     * Prepares $_POST data for the content_manager's 'group_ids' column.
     *
     * @return string
     */
    protected function _prepareContentManagerGroupCheckData()
    {
        $groupCheckData = $this->_getPostData('content_manager')['group_check'];
        
        return $groupCheckData ? implode(',', array_map(function ($element) {
                return 'c_' . $element . '_group';
            }, $groupCheckData)) . ',' : '';
    }
    
    
    /**
     * Returns the content type of the given query result.
     *
     * @param array $queryResult Data sets of query for content_manager table.
     *
     * @return string Whether "content", "file" or "link".
     */
    protected function _getContentType(array $queryResult)
    {
        foreach($queryResult as $result)
        {
            return $result['content_type'];
        }
        
        return 'content';
    }
}