Page 1 of 1

include / output a index_nested module in a own / other file

Posted: Thu Nov 17, 2022 8:01 am
by loop
Hi all
i have a new file / script, where i want to output / include the same content module as in the index_nested as it uses the same cPath variables ane everything (it's a module that shows a banner)

i tried this in my script:

Code: Select all

 ob_start();
        include $GLOBALS['oscTemplate']->map_to_template(DIR_FS_CATALOG . 'includes/modules/content/index_nested/cm_in_swiper_carousel_banner.php');
        $banner = ob_get_clean();
and made a echo from the $banner but nothing shows, can somebody help me out here? i don't want to copy the full code from the module in the new script as it would be more elegant to include it...
thanks!

Re: include / output a index_nested module in a own / other file

Posted: Thu Nov 17, 2022 11:40 am
by ecartz

Code: Select all

include $GLOBALS['Template']->map(DIR_FS_CATALOG . 'includes/modules/content/index_nested/cm_in_swiper_carousel_banner.php');
should be sufficient. You don't need the ob stuff if you just want to display it immediately. Immediate display is the default behavior.

If that's not working, then the normal debugging steps. E.g.

Code: Select all

echo $GLOBALS['Template']->map(DIR_FS_CATALOG . 'includes/modules/content/index_nested/cm_in_swiper_carousel_banner.php');
Does that path exist? If so, try simplifying the template.

If that doesn't display anything then that code isn't being called.

Re: include / output a index_nested module in a own / other file

Posted: Thu Nov 17, 2022 2:27 pm
by loop
the strange thing is
if i make this:

Code: Select all

        include $GLOBALS['Template']->map(DIR_FS_CATALOG . 'includes/modules/content/index_nested/cm_in_swiper_carousel_banner.php');
the Template (tpl_cm_in_swiper_carousel_banner.php) is included and if i make there a echo it works, but the script part of it cm_in_swiper_carousel_banner.php is not executed (and i need this part, not only the template)
did i miss something that cm_in_swiper_carousel_banner.php is called the function execute() inthere?

Re: include / output a index_nested module in a own / other file

Posted: Thu Nov 17, 2022 6:32 pm
by ecartz
loop wrote: Thu Nov 17, 2022 2:27 pm did i miss something that cm_in_swiper_carousel_banner.php is called the function execute() inthere?
Maybe? Something like

Code: Select all

        $GLOBALS['Template']->add_block($carousel_js . PHP_EOL, 'footer_scripts');
Without seeing cm_in_swiper_carousel_banner.php and tpl_cm_in_swiper_carousel_banner.php, it's hard to say.

Re: include / output a index_nested module in a own / other file

Posted: Sat Nov 19, 2022 11:38 am
by loop
hi ecartz

only to be sure you see my problem:
the modul itself works normal an great (in the index_nested modules, in the normal category pages where the module is located modules/index_nested/...) my only problem is that i cannot include the same module in a external script outside of the index.php ( i need / whant to include it in a sepearate / other script, so it shows the same banners at the top)

And with the include you wrote me, the template itself is there ( ican see the empty template) but it seems the execute of the script is not done, only included the empty template...

here is the cm_in_swiper_carouse_banner.php i try to include:

Code: Select all

<?php
/*
  Copyright (c) 2020, G Burton

  This work is licensed under a
  Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

  You should have received a copy of the license along with this work.
  If not, see <http://creativecommons.org/licenses/by-nc-nd/4.0/>.
*/

  class cm_in_swiper_carousel_banner extends abstract_module {

    const CONFIG_KEY_BASE = 'MODULE_CONTENT_IN_SWIPER_CAROUSEL_BANNER_';

    public function __construct() {
      parent::__construct(__FILE__);
      $this->content_width = MODULE_CONTENT_IN_SWIPER_CAROUSEL_BANNER_CONTENT_WIDTH;
      $this->group = basename(dirname(__FILE__));
    }

    public function execute() {
        global $currencies, $currency, $PHP_SELF;

        $content_width = $this->content_width;
        $card_layout = IS_PRODUCT_PRODUCTS_DISPLAY_ROW;
        
        $swiper_carousel_banner_linkage = adverts::get_grouped_adverts(array(2),$GLOBALS['current_category_id']);
        if (sizeof($swiper_carousel_banner_linkage) > 0) {
          $swiper_div="";
          foreach ($swiper_carousel_banner_linkage as $o => $c) {
            $advert_link = adverts::getLink($c['advert_url'], $c['advert_id']);
            $swiper_div .= '<div class="swiper-slide" id="slide_'.$c['advert_id'].'">';
            $swiper_div .= '<div class="swiper-banner-item swiper-banner-item-div-img" style="cursor: pointer; background-image: url(\''.image_thumbor::get_image('images/'.$c['advert_image'],'900','200').'\');">' . PHP_EOL;
            
            if(!empty($c['advert_image_logo'])){
              $swiper_div .= '<div class="swiper-banner-logo">';
              $swiper_div .= "<img src='".image_thumbor::get_image('images/'.$c['advert_image_logo'],'','30')."'>";
              $swiper_div .= '</div>' . PHP_EOL;
            }

            if(!empty($c['advert_image_badge'])){
              $swiper_div .= '<div class="swiper-banner-badge">';
              $swiper_div .= "<img src='/images/".$c['advert_image_badge']."'>";
              $swiper_div .= '</div>' . PHP_EOL;
            }

            if (tep_not_null($c['advert_html_text'])) {
              $swiper_div .= '<div class="swiper-banner-title-container"><div class="swiper-banner-title">';
                $swiper_div .= $c['advert_html_text'];
              $swiper_div .= '</div>' . PHP_EOL;
              $swiper_div .= '<div class="swiper-banner-subtitle pt-1">';
                $swiper_div .= $c['advert_html_text_2'];
              $swiper_div .= '</div></div>' . PHP_EOL;
              $swiper_div .= '<div class="swiper-banner-button">';
                $swiper_div .= $c['advert_button_text'];
              $swiper_div .= '</div>' . PHP_EOL;
            }
            if($c['advert_url_type'] != "NoLink"){
              $swiper_div .= '<a class="image_darken_hover_overlay" href="' . $advert_link['href'] . '"'.$advert_link['target'].'></a>'. PHP_EOL;
            }
            $swiper_div .= '</div>' . PHP_EOL;
            $swiper_div .= '</div>';
          }

          $swiper_key = "swiper-banner";
          $advert_titel = $c['advert_html_text'];

          $swiper_advert_wrapper_div = <<<swiperHtml
          <div class="swiper swiper-banner advert_{$swiper_key}" id="advert_{$swiper_key}">
              <div class="swiper-wrapper">
              {$swiper_div}
              </div>
              <div class="swiper-pagination swiper-pagination-{$swiper_key}"></div>
              <div class="swiper-button-next swiper-button-next-{$swiper_key} swiper-button-inactive"></div>
              <div class="swiper-button-prev swiper-button-prev-{$swiper_key} swiper-button-inactive"></div>
          </div>
swiperHtml;

          $tpl_data = [ 'group' => $this->group, 'file' => __FILE__ ];
          include 'includes/modules/content/cm_template.php';
        }
      
    }

    protected function get_parameters() {
        return [
          'MODULE_CONTENT_IN_SWIPER_CAROUSEL_BANNER_STATUS' => [
            'title' => 'Enable Best Sellers Module',
            'value' => 'True',
            'desc' => 'Should this module be shown on the index page?',
            'set_func' => "tep_cfg_select_option(['True', 'False'], ",
          ],
          'MODULE_CONTENT_IN_SWIPER_CAROUSEL_BANNER_CONTENT_WIDTH' => [
            'title' => 'Content Width',
            'value' => 'col-sm-12',
            'desc' => 'What width container should the content be shown in?',
          ],
          'MODULE_CONTENT_IN_SWIPER_CAROUSEL_BANNER_MAX_DISPLAY' => [
            'title' => 'Maximum Display',
            'value' => '4',
            'desc' => 'Maximum number of products that should show in this module?'
          ],
          'MODULE_CONTENT_IN_SWIPER_CAROUSEL_BANNER_SORT_ORDER' => [
            'title' => 'Sort Order',
            'value' => '50',
            'desc' => 'Sort order of display. Lowest is displayed first.',
          ],
        ];
      }

  }
and here the template:

Code: Select all

<div class="<?php echo $content_width; ?> i-swiper-carousel">
    <?= $swiper_advert_wrapper_div ?>
</div>
<?php
/*
  $Id$

  CE Phoenix, E-Commerce made Easy
  https://phoenixcart.org

  Copyright (c) 2021 Phoenix Cart

  Released under the GNU General Public License
*/
?>

Re: include / output a index_nested module in a own / other file

Posted: Sat Nov 19, 2022 1:39 pm
by ecartz
I would look for a hook file. E.g. includes/hooks/shop/index/swiper.php or similar. May be under templates.

Re: include / output a index_nested module in a own / other file

Posted: Sun Nov 20, 2022 12:52 pm
by loop
you were right, that i missed the javascript part (the hook) i did that now, but that does not change my first problem. the execute function is somehow not called...
if i replace the function with this:

Code: Select all

 public function execute() {
        global $currencies, $currency, $PHP_SELF;

        echo "yes";
    }
i still don't have this echo in my script, i only have what i do in the template, if i echo somthing directly in the template of the module, i see it, but nothing on the execute function of the module. i miss something elementary here, the include only does not do the execute i think .... hmm

Re: include / output a index_nested module in a own / other file

Posted: Sun Nov 20, 2022 1:58 pm
by ecartz
loop wrote: Sun Nov 20, 2022 12:52 pm the execute function is somehow not called...
Here is the line that calls the execute method for a content module (e.g. index_nested): https://github.com/CE-PhoenixCart/Phoen ... e.php#L106

Here's where the get_content method is called (for index_nested): https://github.com/CE-PhoenixCart/Phoen ... ex.php#L23

The execute method certainly won't get called if nothing actively calls it.

Re: include / output a index_nested module in a own / other file

Posted: Sun Nov 20, 2022 3:00 pm
by loop
ah i see...thank you
but to be honest, i still do not know how i can get a specific index_nested module work in a different / new file

i tried this in my new file, but didn't do what i need..

Code: Select all

$mb = new cm_in_swiper_carousel_banner();
        $mb->execute();
        include $GLOBALS['Template']->map(DIR_FS_CATALOG . 'includes/modules/content/index_nested/cm_in_swiper_carousel_banner.php');
if you could give me a other hint to point me in the right direction, i would be very thankfull