We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered BannerY problems

    • 31837
    • 59 Posts
    Hi all, it's been a long since I've visited here, but I'm back right now working on a website.
    I'm trying to figure out what is going on with BannerY.

    I have it displaying banners correctly (so it seems), but I was wondering about 2 things.

    Firstly, I've only 4 positions added, but when I hover over an ad I get a location of
    "http://XXXX.com/bannerclick/5". While the click-through worked, I thought it a little strange.

    The other thing that is really the main concern, and I've submitted a bug report for it, is the tpl. I don't think these are working.

    Has anyone had any dramas with the "tpl" fields? It seems to only load the default template (chunk) of "byAd".

    Cheers,
    Steve

    This question has been answered by andytough. See the first response.

      • 38783
      • 571 Posts
      Hello Steve,

      Could you post the code that you are using on your page for BannerY, along with the tpl chunk you have made for it?
        If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

        email: [email protected] | website: https://andytough.com
        • 31837
        • 59 Posts
        Hi Andy,
        With pleasure. I've attached everything I think that may help you. (and a little more too)



        Snippet: BannerY

        <?php
        /** @var array $scriptProperties */
        /* @var pdoFetch $pdoFetch */
        if (!$modx->getService('pdoFetch')) {return 'You need to install pdoTools to use this snippet!';}
        $pdoFetch = new pdoFetch($modx, $scriptProperties);
        $pdoFetch->addTime('pdoTools loaded');
        $modx->lexicon->load('bannery:default');
        $modx->addPackage('bannery', MODX_CORE_PATH . 'components/bannery/model/');
        
        if (!empty($tplOuter)) {$tplWrapper = $tplOuter;}
        if (empty($outputSeparator)) {$outputSeparator = "\n";}
        $class = 'byAd';
        
        // Adding extra parameters into special place so we can put them in results
        /** @var modSnippet $snippet */
        $additionalPlaceholders = array();
        if ($snippet = $modx->getObject('modSnippet', array('name' => 'BannerY'))) {
        	$properties = unserialize($snippet->properties);
        	foreach ($scriptProperties as $k => $v) {
        		if (!isset($properties[$k])) {
        			$additionalPlaceholders[$k] = $v;
        		}
        	}
        }
        // ---
        
        $date = date('Y-m-d H:i:s');
        $where = array(
        	"({$class}.start IS NULL OR {$class}.start <= '{$date}') AND ({$class}.end IS NULL OR {$class}.end >= '{$date}')"
        );
        if (empty($showInactive)) {
        	$where[$class.'.active'] = 1;
        }
        if (!empty($position)) {
        	$where['byAdPosition.position'] = $position;
        }
        elseif (!empty($positions)) {
        	$where['byAdPosition.position:IN'] = array_map('trim', explode(',', $positions));
        }
        
        if (empty($sortby)) {
        	$sortby = 'RAND()';
        }
        elseif ($sortby == 'idx' || $sortby == 'index') {
        	$sortby = 'byAdPosition.idx';
        }
        else {
        	$sortby = $class.'.'.$sortby;
        }
        
        $innerJoin = array(
        	'byAdPosition' => array(
        		'alias' => 'byAdPosition',
        		'on' => $class.'.id = byAdPosition.ad'
        	)
        );
        
        // Fields to select
        $select = array(
        	$class => 'all',
        	'byAdPosition' => '`byAdPosition`.`id` as `adposition`, `byAdPosition`.`ad`'
        );
        
        // Add custom parameters
        foreach (array('where','innerJoin','select') as $v) {
        	if (!empty($scriptProperties[$v])) {
        		$tmp = $modx->fromJSON($scriptProperties[$v]);
        		if (is_array($tmp)) {
        			$$v = array_merge($$v, $tmp);
        		}
        	}
        	unset($scriptProperties[$v]);
        }
        $pdoFetch->addTime('Conditions prepared');
        
        $default = array(
        	'class' => $class,
        	'innerJoin' => $modx->toJSON($innerJoin),
        	'where' => $modx->toJSON($where),
        	'select' => $modx->toJSON($select),
        	'sortby' => $sortby,
        	'groupby' => $class.'.id',
        	'return' => 'data',
        	'disableConditions' => true,
        );
        
        $pdoFetch->setConfig(array_merge($default, $scriptProperties));
        $rows = $pdoFetch->run();
        
        $output = array();
        $default_source = $modx->getOption('bannery_media_source', null, $modx->getOption('default_media_source'), true);
        $sources = array();
        foreach ($rows as $row) {
        	$source = !empty($row['source'])
        		? $row['source']
        		: $default_source;
        
        	if (!isset($sources[$row['source']])) {
        		/** @var modMediaSource $source */
        		if ($source = $modx->getObject('sources.modMediaSource', $source)) {
        			$source->initialize($modx->context->key);
        		}
        		$sources[$row['source']] = $source;
        	}
        	else {
        		$source = $sources[$row['source']];
        	}
        
        	if (!empty($source) && $source instanceof modMediaSource && !empty($row['image'])) {
        		$row['image'] = $source->getObjectUrl($row['image']);
        	}
        
        	$row['idx'] = $pdoFetch->idx++;
        	$tpl = $pdoFetch->defineChunk($row);
        	if (!empty($additionalPlaceholders)) {
        		$row = array_merge($additionalPlaceholders, $row);
        	}
        
        	$output[] = !empty($tpl)
        		? $pdoFetch->getChunk($tpl, $row, $pdoFetch->config['fastMode'])
        		: '<pre>'.$pdoFetch->getChunk('', $row).'</pre>';
        }
        
        if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) {
        	$output['log'] = '<pre class="pdoUsersLog">' . print_r($pdoFetch->getTime(), 1) . '</pre>';
        }
        
        // Return output
        if (!empty($toSeparatePlaceholders)) {
        	$modx->setPlaceholders($output, $toSeparatePlaceholders);
        }
        else {
        	$output = implode($outputSeparator, $output);
        
        	if (!empty($tplWrapper) && (!empty($wrapIfEmpty) || !empty($output))) {
        		$output = $pdoFetch->getChunk($tplWrapper, array('output' => $output), $pdoFetch->config['fastMode']);
        	}
        
        	if (!empty($toPlaceholder)) {
        		$modx->setPlaceholder($toPlaceholder, $output);
        	}
        	else {
        		return $output;
        	}
        }



        Chunk: sidebyAd

        <a target="_blank" href="[[++bannery_click]]/[[+adposition]]"><img style="width:300px !important;" src="[[+image]]" alt="[[+name]]"/></a>


        Chunk: byAd
        <p><a target="_blank" href="[[++bannery_click]]/[[+adposition]]"><img src="[[+image]]" alt="[[+name]]"/></a></p>




        The HTML Call:

             <div class="main_left_ad">[[BannerY? &position=`3`  &sortby=`RAND()` &limit=`1` tpl=`sidebyAd`]] </div>
             <div class="main_right_ad">[[BannerY? &position=`4`  &sortby=`RAND()` &limit=`1` tpl=`sidebyAd`]] </div>
        





        Additional CSS:
        
        .main_left_ad
        {
          float: left;
          width: 200px;
          height:20px;
          /*border:solid 1px #0F0;*/
          margin-left: -220px; 
        }	
        .main_right_ad
        {
        	margin-right: -310px; 	
        	float: right;
           width: 200px;
           height:20px;
           /*border:solid 1px #00F;*/
          margin-right: -220px; 
        }
        
        div.main_left_ad p a img
        {
        	width: 300px !important;
        }
        
        div.main_right_ad p a img
        {
        	width: 300px !important;
        }
        



        In the end, I had to find a way around this, and used CSS to track down the image so I could change it, however it still wasn't responding to my wishes of changing the 'tpl'.
        • discuss.answer
          • 38783
          • 571 Posts
          I think you have left the ampersand out of your call for the tpl. It should read &tpl=

          <div class="main_left_ad">[[BannerY? &position=`3`  &sortby=`RAND()` &limit=`1` &tpl=`sidebyAd`]] </div>
          <div class="main_right_ad">[[BannerY? &position=`4`  &sortby=`RAND()` &limit=`1` &tpl=`sidebyAd`]] </div>


          I have not looked closely at your other code, so if this does not solve the problem post back here and people can have a proper look at it.
            If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

            email: [email protected] | website: https://andytough.com
            • 31837
            • 59 Posts
            Quote from: andytough at May 21, 2017, 04:21 PM
            I think you have left the ampersand out of your call for the tpl. It should read &tpl=

            <div class="main_left_ad">[[BannerY? &position=`3`  &sortby=`RAND()` &limit=`1` &tpl=`sidebyAd`]] </div>
            <div class="main_right_ad">[[BannerY? &position=`4`  &sortby=`RAND()` &limit=`1` &tpl=`sidebyAd`]] </div>


            I have not looked closely at your other code, so if this does not solve the problem post back here and people can have a proper look at it.

            Oh gosh. I feel dumb. When you said that I thought, surely I can't be that daft?!

            yep I was...

            Thanks mate. I guess I just needed that 2nd pair of eyes. I think I needed the sleep - as at it all day. All fixed now.
              • 38783
              • 571 Posts
              A second pair of eyes would often have saved me from banging my head against a brick wall of my own making. Walking away from things and getting some sleep is the next best thing! Glad to hear that it is working.
                If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                email: [email protected] | website: https://andytough.com
                • 31837
                • 59 Posts
                Yeah, now to continue with the rest of the chores... fix iphone screen. check. wash dishes. check. rebuild macpro... bugger