X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Simple CMS module and custom page code (https://forum.x-cart.com/showthread.php?t=73346)

Scott Godin 01-18-2016 11:03 AM

Re: Simple CMS module and custom page code
 
after knocking my head on this for nearly a week, I cannot for the life of me figure out what I'm doing wrong, which class I need to extend (and/or decorate), whether I am to use view, model, or controller .. there doesn't seem to be any detailed information anywhere of anyone else trying this for me to build my ideas on, and I don't yet have enough basic knowledge of x-cart 5's functionality despite all the tutorials due to their un-fleshed-out nature.

I created classes/XLite/Module/MY-ID/MY-MODULE/View/Page.php and added the following

Code:

<?php
// vim: set ts=4 sw=4 sts=4 et ft=php.html:


namespace XLite\Module\MHG\TemplateMods\View;

class Page extends \Xlite\View\AView implements \XLite\Base\IDecorator
{
        public static function getAllowedTargets()
        {
                return array_merge(parent::getAllowedTargets(), array('page'));
        }

        public function getCSSFiles()
        {
                $css = parent::getCSSFiles();
                // find page id by its name
                // $cnd is an object containing the condition for "page name is equal to ..." and if this search
                // condition does not exist in the Repo class you have to create it as well for this to work
                $cnd = new \Xlite\Core\CommonCell();
                $cnd->enabled = true;
                $cnd->name = "Photo Gallery";

                $current_id = \XLite\Core\Request::getInstance()->id;
                $target_id = \XLite\Core\Database::getRepo('XLite\Module\CDev\SimpleCMS\Model\Page')->search($cnd);

                \XLite\Logger::logCustom('MHG', "currentID: {$current_id} targetID: {$target_id}\n", true);
                //error_log("currentID: {$current_id} targetID: {$target_id}\n");
 
               
                if ( $target_id == $current_id )
                {
                        $css = array_merge(
                                $css,
                                array(
                                        'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.3.0/css/nanogallery.min.css",
                                        'no_minify' => true,
                                ),
                                array(
                                        'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.3.0/css/themes/light/nanogallery_light.min.css",
                                        'no_minify' => true,
                                ),
                                        // see also https://cdnjs.cloudflare.com/ajax/libs/nanogallery/5.3.0/jquery.nanogallery.min.js for javascript related to this
                                )
                        );
                }
                return $css;
        }

        protected function getDefaultTemplate()
        {
                return 'modules/CDev/SimpleCMS/page/body.tpl';
        }

}


even if $cnd is wrong, AND there is no search functionality in this for the Repo, I should STILL get a log of the current page ID and don't get that either.

where am I going so totally wrong?

cflsystems 01-18-2016 11:54 AM

Re: Simple CMS module and custom page code
 
You have to extend the Page class from the CMS module not the AView class

Scott Godin 01-18-2016 02:36 PM

Re: Simple CMS module and custom page code
 
Quote:

Originally Posted by cflsystems
You have to extend the Page class from the CMS module not the AView class


ahhhhhhhhhhhh. I was coming to suspect that, but after so much flailing, rather than yet another TIAS, I figured it was smarter to just ask.

Thanks, now let's see what's what.

Scott Godin 01-18-2016 03:25 PM

Re: Simple CMS module and custom page code
 
there was also a typo in my earlier code:

Code:

class Page extends \Xlite\View\AView implements \XLite\Base\IDecorator

see it?

strangely, that generated no error.

and after fixing Xlite to XLite, NOW I get the error about the extra parenthesis on line 45. not before.

Scott Godin 01-18-2016 03:45 PM

Re: Simple CMS module and custom page code
 
still nothing.

Code:

namespace XLite\Module\MHG\TemplateMods\View;                                                                                                                       
                                                                                                                                                                     
class PGPage extends \XLite\Module\CDev\SimpleCMS\View\Page implements \XLite\Base\IDecorator
{


even tried renaming the file from Page.php to PGPage.php and adjusting the class accordingly.

I still get nothing from the Logger, which means that code isn't running when the Simple CMS Page is running.

Scott Godin 01-19-2016 09:41 AM

Re: Simple CMS module and custom page code
 
okay it's not \XLite\Module\CDev\SimpleCMS\View\Page I need to extend, but \XLite\Module\CDev\SimpleCMS\View\CustomerPage

now I have to figure out the right way to make the search work

Scott Godin 01-19-2016 11:24 AM

Re: Simple CMS module and custom page code
 
I've managed to 'fake' my way through the search bit by doing the following, and so long as I have css and js aggregation turned off, this works, but I'm curious why when css+js aggregation is ON, it doesn't pull in the external resources from the CDN this way ... as I AM adding them with url and not file parameter.

I still have to learn how to do the getRepo(...)->search() bit correctly, but I'm far closer than I was. any pointers would be appreciated

Code:

<?php
// vim: set ts=4 sw=4 sts=4 et ft=php.html:


namespace XLite\Module\MHG\TemplateMods\View;

class PGPage extends \XLite\Module\CDev\SimpleCMS\View\CustomerPage implements \XLite\Base\IDecorator
{
        public static function getAllowedTargets()
        {
                return array_merge(parent::getAllowedTargets(), array('page'));
        }

        public function getCSSFiles()
        {
                // find page id by its name
                // $cnd is an object containing the condition for "page name is equal to ..." and if this search
                // condition does not exist in the Repo class you have to create it as well for this to work
                $cnd = new \Xlite\Core\CommonCell();
                $cnd->enabled = true;
                $cnd->name = "Photo Gallery";

                $current_id = \XLite\Core\Request::getInstance()->id;
                $targets = \XLite\Core\Database::getRepo('XLite\Module\CDev\SimpleCMS\Model\Page')->search($cnd);

                /* {{{ Debug
                foreach ($target_id as $k => $v) { $tgt[$k] = array('NAME' => $v->name, 'LABEL_ID' => $v->label_id, 'ID' => $v->id); }
                $tgt = print_r($tgt, true);

                \XLite\Logger::logCustom('MHG', "currentID: {$current_id} tgt: {$tgt}      targetID: {$target_id}", false);
                /* }}} */

                $target_acquired = false;
                foreach ($targets as $tk => $tv)
                {
                        if ( ($tv->name == $cnd->name) && ($tv->id == $current_id) )
                        {
                                $target_acquired = true;
                                break;
                        }
                }                       

                if ( $target_acquired )
                {
                        return array_merge(
                                parent::getCSSFiles(),
                                array(
                                        array(
                                                'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.9.1/css/nanogallery.min.css",
                                                'media' => 'screen',
                                                'no_minify' => true,
                                        ),
                                        array(
                                                'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.9.1/css/themes/light/nanogallery_light.min.css",
                                                'media' => 'screen',
                                                'no_minify' => true,
                                        )
                                )
                        );
                }
                return parent::getCSSFiles();
        }

        public function getJSFiles()
        {
                $cnd = new \XLite\Core\CommonCell();
                $cnd->enabled = true;
                $cnd->name = 'Photo Gallery';

                $current_id = \XLite\Core\Request::getInstance()->id;
                $targets = \XLite\Core\Database::getRepo('XLite\Module\CDev\SimpleCMS\Model\Page')->search($cnd);

                $target_acquired = false;
                foreach ($targets as $tk => $tv)
                {
                        if ( ($tv->name == $cnd->name) && ($tv->id == $current_id) )
                        {
                                $target_acquired = true;
                                break;
                        }
                }

                if ($target_acquired)
                {
                        return array_merge(
                                parent::getJSFiles(),
                                array(
                                        array(
                                                'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.9.1/jquery.nanogallery.min.js",
                                                'no_minify' => true
                                        ),
                                        array(
                                                'file' => 'modules/MHG/TemplateMods/nanogallery_local.js'
                                        )
                                )
                        );
                }
                return parent::getJSFiles();
        }

        protected function getDefaultTemplate()
        {
                return 'modules/CDev/SimpleCMS/page/body.tpl';
        }

}


Scott Godin 01-20-2016 01:12 PM

Re: Simple CMS module and custom page code
 
neither http://kb.x-cart.com/display/XDD/Speeding+up+your+store#Speedingupyourstore-Step1.EnableCSS,JSaggregationandwidgetcaching nor http://kb.x-cart.com/display/XDD/Adding+CSS+and+JS+files have any information on why css/js aggregation under Look and Feel > Performance, when enabled, fail to load resources added via URL rather than FILE, from within a Module so I can leverage the awesome https://cdnjs.com/

I tried to do everything I could to find out the right way to include the links short of saving and serving them locally, and when css/js aggregation is OFF, everything works fine. I would *expect* it would add the external resources as separate LINK and SCRIPT tags when aggregation is on, but that does not appear to be happening.

bug? or did I miss something

qualiteam 01-25-2016 04:42 AM

Re: Simple CMS module and custom page code
 
A few notes on your code:
1. You don't need getAllowedTargets() and getDefaultTemplate() methods in your class as they exist in the original widget class.
2. For easier tracking which classes extend what other classes it makes sense to rename your class from PGPage to CustomerPage (as the original class that you modify from your module).
3. Instead of searching a page with the given name, it will be easier to get the current page and check its name.
4. View classes can use public methods declared in the controller used for the current page. In case of SimpleCMS pages the controller class is \XLite\Module\CDev\SimpleCMS\Controller\Customer\P age that already has getPage() public method. It makes sense to use these methods instead of trying to retrieve the current page by using the Repo class.
5. It makes sense to put the code that checks if it is the gallery page into a separate method.
6. Having one return operator per method is a good practice. You should avoid multiple returns per method when possible.

So, I would change your code as follows:
PHP Code:

<?php 
// vim: set ts=4 sw=4 sts=4 et ft=php.html:

namespace XLite\Module\MHG\TemplateMods\View;

/**
 * Decorated widget displaying a SimpleCMS page.
 */
class CustomerPage extends \XLite\Module\CDev\SimpleCMS\View\CustomerPage implements \XLite\Base\IDecorator
{
    public function 
getCSSFiles()
    {
        
$files parent::getCSSFiles();

        if (
$this->isGallerypage()) {
            
$files array_merge(
                
$files,
                array(
                    array(
                        
'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.9.1/css/nanogallery.min.css",
                        
'media' => 'screen',
                        
'no_minify' => true,
                    ),
                    array(
                        
'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.9.1/css/themes/light/nanogallery_light.min.css",
                        
'media' => 'screen',
                        
'no_minify' => true,
                    )
                )
            );
        }

        return 
$files;
    }

    public function 
getJSFiles()
    {
        
$files parent::getJSFiles();

        if (
$this->isGallerypage())
        {
            
$files array_merge(
                
$files,
                array(
                    array(
                        
'url' => "//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.9.1/jquery.nanogallery.min.js",
                        
'no_minify' => true
                    
),
                    array(
                        
'file' => 'modules/MHG/TemplateMods/nanogallery_local.js'
                    
)
                )
            );
        }

        return 
$files;
    }

    protected function 
isGalleryPage()
    {
        
// Use the getPage() method of the controller class to get the current page object
        
$currentName $this->getPage()->getName();

         return 
"Photo Gallery" === $currentName;
    }

}


Quote:

Originally Posted by Scott Godin
why css/js aggregation under Look and Feel > Performance, when enabled, fail to load resources added via URL rather than FILE


If you add a resource with the "url" key, not "file", X-Cart 5 won't aggregate the file. So, this should work as you expect.

Scott Godin 01-25-2016 08:49 AM

Re: Simple CMS module and custom page code
 
Quote:

If you add a resource with the "url" key, not "file", X-Cart 5 won't aggregate the file. So, this should work as you expect.

no I wasn't expecting it to aggregate the file.. what I WAS expecting, however is that it would append separate link/script tags AFTER the aggregation's css/js link. Because right now if I enable aggregation, the external urls are NOT added to the html AS external urls.

What I expected:
HTML Code:


... header...
<link *Plethora of aggregated css files for various media [screen, print, all]* />
<link  href="//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.3.0/css/nanogallery.min.css?1453234303" rel="stylesheet" type="text/css" media="screen" />
<link  href="//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.3.0/css/themes/light/nanogallery_light.min.css?1453234303" rel="stylesheet" type="text/css" media="screen" />
...body...
...footer...
<script  *aggregated JS that DOES contain nanogallery_local.js* ></script>
<script  type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/nanogallery/5.3.0/jquery.nanogallery.min.js?1453234303"></script>


What I got:
HTML Code:


...header...
<link *Plethora of aggregated css files for various media [screen, print, all]* />
...body...
...footer...
<script  *aggregated JS that DOES contain nanogallery_local.js* ></script>


your aggregation codebase currently fails to append the external urls, and does not report the issue in logging.


All times are GMT -8. The time now is 01:56 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.