Banner Module

Open to all! Ask other shopowners for help.
Post Reply
User avatar
tessthepup
Certified Developer
Posts: 382
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Banner Module

Post by tessthepup »

Hi Guys,

I am using Raiwa's code to display a banner in the stores header but changed it to an index module.

I have also changed the template code to display 2 banners side by side. The bit I can not seem to figure out is what to change/modify so that I can select 2 unique banners as at the moment it shows the same banner twice.

Code: Select all

  class cm_i_index_banners extends abstract_executable_module {

    const CONFIG_KEY_BASE = 'MODULE_CONTENT_INDEX_BANNERS_';

    public function __construct() {
      parent::__construct(__FILE__);
    }

    function execute() {
      $content_width = MODULE_CONTENT_INDEX_BANNERS_CONTENT_WIDTH;

      if (tep_not_null(MODULE_CONTENT_INDEX_BANNERS_GROUPS)) {
        $banner_query = tep_db_query("SELECT a.*, ai.*
                                      FROM
                                        (SELECT *
                                         FROM advert
                                         WHERE status = '1'
                                         AND advert_group IN (" . MODULE_CONTENT_INDEX_BANNERS_GROUPS . ")
                                         ORDER BY rand()) a
                                         LEFT JOIN advert_info ai ON a.advert_id = ai.advert_id AND ai.languages_id = " . (int)$_SESSION['languages_id']
                                      . " GROUP BY advert_group");

        if (tep_db_num_rows($banner_query) > 0) {

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

    protected function get_parameters() {
      return [
        'MODULE_CONTENT_INDEX_BANNERS_STATUS' => [
          'title' => 'Enable Static Index Banners',
          'value' => 'True',
          'desc' => 'Do you want to enable the Static Index Banner content module?',
          'set_func' => "tep_cfg_select_option(['True', 'False'], ",
        ],
        'MODULE_CONTENT_INDEX_BANNERS_CONTENT_WIDTH' => [
          'title' => 'Content Width',
          'value' => '12',
          'desc' => 'What width container should the content be shown in?',
          'set_func' => "tep_cfg_select_option(['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], ",
        ],
        'MODULE_CONTENT_INDEX_BANNERS_BANNER_WIDTH' => [
          'title' => 'Banner Width',
          'value' => '6',
          'desc' => 'What width container should each banner be shown in?',
          'set_func' => "tep_cfg_select_option(['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], ",
        ],
        'MODULE_CONTENT_INDEX_BANNERS_GROUPS' => [
          'title' => 'Banner Groups',
          'value' => '',
          'desc' => 'Check the Banner Groups to show in this module.',
          'use_func' => 'cm_i_index_banners::show_modules',
          'set_func' => 'cm_i_index_banners::edit_modules(',
        ],
        'MODULE_CONTENT_INDEX_BANNERS_SORT_ORDER' => [
          'title' => 'Sort Order',
          'value' => '0',
          'desc' => 'Sort order of display. Lowest is displayed first.',
        ],


      ];
    }

    public static function readonly($value) {
      return $value;
    }

    public static function show_modules($text) {
      return nl2br(implode("\n", explode(',', str_replace('\'', '', $text))));
    }

    public static function edit_modules($values, $key) {
      global $PHP_SELF;

      $group_query = tep_db_query("SELECT DISTINCT advert_group FROM advert WHERE status = '1'");

      $values_array = explode(',', $values);

      $output = '';
      while ($group = tep_db_fetch_array($group_query)) {
        $output .= tep_draw_checkbox_field($key . 'st_group_module[]', '\'' . $group['advert_group'] . '\'', in_array('\'' . $group['advert_group'] . '\'', $values_array), null, True) . '&nbsp;' . tep_output_string($group['advert_group']) . '<br />';
      }

      if (!empty($output)) {
        $output = '<br>' . substr($output, 0, -6);
      }

      $output .= tep_draw_hidden_field('configuration[' . $key . ']', '', 'id="' . $key . 'htrn_group_modules"');

      $output .= '<script>
                  function ' . $key . 'htrn_group_update_cfg_value() {
                    var ' . $key . 'htrn_group_selected_modules = \'\';

                    if ($(\'input[name="' . $key . 'st_group_module[]"]\').length > 0) {
                      $(\'input[name="' . $key . 'st_group_module[]"]:checked\').each(function() {
                        ' . $key . 'htrn_group_selected_modules += $(this).attr(\'value\') + \',\';
                      });

                      if (' . $key . 'htrn_group_selected_modules.length > 0) {
                        ' . $key . 'htrn_group_selected_modules = ' . $key . 'htrn_group_selected_modules.substring(0, ' . $key . 'htrn_group_selected_modules.length - 1);
                      }
                    }

                    $(\'#' . $key . 'htrn_group_modules\').val(' . $key . 'htrn_group_selected_modules);
                  }

                  $(function() {
                    ' . $key . 'htrn_group_update_cfg_value();

                    if ($(\'input[name="' . $key . 'st_group_module[]"]\').length > 0) {
                      $(\'input[name="' . $key . 'st_group_module[]"]\').change(function() {
                        ' . $key . 'htrn_group_update_cfg_value();
                      });
                    }
                  });
                  </script>';

      return $output;
    }

  }

Code: Select all

<div class="row col-sm-<?= $content_width ?> cm-i-index-banners m-0">
 <div class="col-sm-6 w-100 h-auto p-1 align-center">
  <?php
    while ($banner_values = tep_db_fetch_array($banner_query)) {
      echo '<div class="cm-i-index-banners col-sm-' . MODULE_CONTENT_INDEX_BANNERS_BANNER_WIDTH . ' w-100 h-auto m-0 p-0">';
      if (tep_not_null($banner_values['advert_html_text'])) {
        echo $banner_values['advert_html_text'];
      } else {
        if (tep_not_null($banner_values['advert_url'])) {
          echo '<a href="' . tep_href_link($banner_values['advert_url'], $banner_values['advert_fragment']) . '" target="_blank" rel="noopener">' . tep_image('images/' . $banner_values['advert_image'], htmlspecialchars($banner_values['advert_title'])) . '</a>';
        } else {
          echo tep_image('images/' . $banner_values['advert_image'], htmlspecialchars($banner_values['advert_title']));
        }
      echo '</div>';
    }
  ?>
 </div>

 <div class="col-sm-6 w-100 h-auto m-0 p-1 align-center">
  <?php
      echo '<div class="cm-i-index-banners col-sm-' . MODULE_CONTENT_INDEX_BANNERS_BANNER_WIDTH . ' w-100 h-auto m-0 p-0">';
      if (tep_not_null($banner_values['advert_html_text'])) {
        echo $banner_values['advert_html_text'];
      } else {
        if (tep_not_null($banner_values['advert_url'])) {
          echo '<a href="' . tep_href_link($banner_values['advert_url'], $banner_values['advert_fragment']) . '" target="_blank" rel="noopener">' . tep_image('images/' . $banner_values['advert_image'], htmlspecialchars($banner_values['advert_title'])) . '</a>';
        } else {
          echo tep_image('images/' . $banner_values['advert_image'], htmlspecialchars($banner_values['advert_title']));
        }
      }
      echo '</div>';
    }
  ?>
 </div>
</div>
You do not have the required permissions to view the files attached to this post.


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Banner Module

Post by ecartz »

Change

Code: Select all

    while ($banner_values = tep_db_fetch_array($banner_query)) {
to

Code: Select all

    if ($banner_values = $banner_query->fetch_assoc()) {
and wrap the other one in its own if (same line to start, plus you need a closing } ). That will at least make it stop displaying twice.
User avatar
tessthepup
Certified Developer
Posts: 382
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Banner Module

Post by tessthepup »

ecartz wrote: Sun Feb 27, 2022 4:17 pm Change

Code: Select all

    while ($banner_values = tep_db_fetch_array($banner_query)) {
to

Code: Select all

    if ($banner_values = $banner_query->fetch_assoc()) {
and wrap the other one in its own if (same line to start, plus you need a closing } ). That will at least make it stop displaying twice.
Thank you very much.

Would I be right in thinking I would need a new function to select the second banner or would that be overkill :?:
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Banner Module

Post by ecartz »

Code: Select all

SELECT a.*, ai.*
                                      FROM
                                        (SELECT *
                                         FROM advert
                                         WHERE status = '1'
                                         AND advert_group IN (" . MODULE_CONTENT_INDEX_BANNERS_GROUPS . ")
                                         ORDER BY rand()) a
                                         LEFT JOIN advert_info ai ON a.advert_id = ai.advert_id AND ai.languages_id = " . (int)$_SESSION['languages_id']
                                      . " GROUP BY advert_group LIMIT 2
will return more than two banners if more than one banner is in that group. Note that the only change that I made was to add the LIMIT. And you don't actually need that; it just reduces the load on the database slightly.

I.e. put two banners with advert_group in MODULE_CONTENT_INDEX_BANNERS_GROUPS. Right now there's apparently only one banner meeting that criterion.
User avatar
tessthepup
Certified Developer
Posts: 382
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Banner Module

Post by tessthepup »

ecartz wrote: Sun Feb 27, 2022 5:44 pm

Code: Select all

SELECT a.*, ai.*
                                      FROM
                                        (SELECT *
                                         FROM advert
                                         WHERE status = '1'
                                         AND advert_group IN (" . MODULE_CONTENT_INDEX_BANNERS_GROUPS . ")
                                         ORDER BY rand()) a
                                         LEFT JOIN advert_info ai ON a.advert_id = ai.advert_id AND ai.languages_id = " . (int)$_SESSION['languages_id']
                                      . " GROUP BY advert_group LIMIT 2
will return more than two banners if more than one banner is in that group. Note that the only change that I made was to add the LIMIT. And you don't actually need that; it just reduces the load on the database slightly.

I.e. put two banners with advert_group in MODULE_CONTENT_INDEX_BANNERS_GROUPS. Right now there's apparently only one banner meeting that criterion.
Simple when you know ;)
User avatar
tessthepup
Certified Developer
Posts: 382
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Banner Module

Post by tessthepup »

Maybe I am getting a little to cocky now lol

I have copied and renamed the banner module to index_products and all works as it should (see first pic)

However because I have the Category/Manufacturer Description module set to a width of 5 on smaller screens it does not span the entire page obviously because of the hidden banner (see second pic)

I have tried d-flex etc but cannot get the Category/Manufacturer Description to span the entire 12 columns when the banner is hidden. Is this even possible???
You do not have the required permissions to view the files attached to this post.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Banner Module

Post by ecartz »

https://stackoverflow.com/a/133064/6660678

Change from visibility: hidden to display: none. Note: you may be applying a Bootstrap class to make it "hidden" -- if so, you either need to override the class or switch to something else.
User avatar
tessthepup
Certified Developer
Posts: 382
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Banner Module

Post by tessthepup »

ecartz wrote: Mon Feb 28, 2022 8:58 pm https://stackoverflow.com/a/133064/6660678

Change from visibility: hidden to display: none. Note: you may be applying a Bootstrap class to make it "hidden" -- if so, you either need to override the class or switch to something else.
Thank you. I understand the principle but not sure how to implement it without either hiding it completely or not hiding it at all.
User avatar
tessthepup
Certified Developer
Posts: 382
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Banner Module

Post by tessthepup »

tessthepup wrote: Tue Mar 01, 2022 6:45 pm
ecartz wrote: Mon Feb 28, 2022 8:58 pm https://stackoverflow.com/a/133064/6660678

Change from visibility: hidden to display: none. Note: you may be applying a Bootstrap class to make it "hidden" -- if so, you either need to override the class or switch to something else.
Thank you. I understand the principle but not sure how to implement it without either hiding it completely or not hiding it at all.
Finally got it working the way I want it to after a lot of chopping and changing and the only way I could get it to work correctly was by by setting the columns widths to auto for the two modules on small screens etc for different size screens.

Code: Select all

<div class="row col-sm-auto col-md-6 col-lg-6 cm-ip-banner mx-auto mt-auto mb-3 d-md-none d-lg-block">

Code: Select all

<div class="col-sm-auto col-md-auto col-lg-6 cm_ip_category_title_manufacturer_description">


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply