📜 ⬆️ ⬇️

Making your own banner krutilki

We do functionality that will allow:

1. Store all banners in one place.
2. Display pages on which the banner will be displayed.
3. Observe the display limit
4. Specify the destination address for the banner (URL)

It will work exactly on 0.9.6.3 (on the versions above it doesn’t foretell any problems), the integration should take about half an hour, depending on your knowledge of MODx.
')
Basic skills of working with MODx and understanding of terms are needed: snippet, TV in the context of this CMF.


I am happy to answer all substantive questions.

Let's start:
We divide the whole integration into several steps:

1. Create a Banner Snippet

<?php
if (!function_exists( 'setTemplateVar' )) {
function setTemplateVar($ value , $docID, $tplVarName) {
global $modx;

//-- get tmplvar id
$tplName = $modx->getFullTableName( 'site_tmplvars' );
$tplRS = $modx->db-> select ( 'id' , $tplName, 'name="' . $tplVarName . '"' );
$tplRow = $modx->db->getRow($tplRS);

$tblName = $modx->getFullTableName( 'site_tmplvar_contentvalues' );

$selectQuery = $modx->db-> select ( '*' , $tblName, 'contentid=' . $docID . ' AND tmplvarid=' . $tplRow[ 'id' ]);

$updFields = array (
'value' => $ value
);
$insFields = array (
'tmplvarid' => $tplRow[ 'id' ],
'contentid' => $docID,
'value' => $ value
);

if ($modx->db->getRecordCount($selectQuery) < 1) {
$modx->db->insert($insFields, $tblName);
} else {
$modx->db->update($updFields, $tblName, 'contentid=' . $docID . ' AND tmplvarid=' . $tplRow[ 'id' ]);
}
}
}

/**/

$vault = 234; //ID

$children= $modx->getActiveChildren($vault, 'menuindex' , 'ASC' );
if (!$children === false ) {

$allBanners = $modx->getDocumentChildrenTVarOutput($vault, array( 'pages' , 'fileBanner' , 'counter' , 'linkToURL' ));

foreach ($allBanners as $idBannerPage=>$banner)
{
$pages = explode( ',' ,$banner[ 'pages' ]);
if ((in_array($modx->documentIdentifier,$pages) || in_array( '-1' ,$pages)) && ($banner[ 'counter' ]>0 || $banner[ 'counter' ] == '-1' ))
{
$path_info = pathinfo(MODX_BASE_PATH.$banner[ 'fileBanner' ]);
switch ($path_info[ 'extension' ]){
case 'swf' :
echo '
<div id="productsSecondaryPromo"></div>
<script type="text/javascript">
// <![CDATA[
var so = new SWFObject("/'
.$banner[ 'fileBanner' ]. '?clickTag=' .$banner[ 'linkToURL' ]. '", "company_right", "214", "308", "7", "#FFFFFF");
so.write("productsSecondaryPromo");
// ]]>
</script>'
; break ;
default : echo '<a href="' .$banner[ 'linkToURL' ]. '"><img src="' .$modx->getConfig( 'base_url' ).$banner[ 'fileBanner' ]. '" alt="" border="0"></a>' ;

}
if ($banner[ 'counter' ] !== '-1' && ( int )$banner[ 'counter' ]>0){ setTemplateVar($banner[ 'counter' ]-1, $idBannerPage, 'counter' );}
}
unset($banner);
}
}
?>


* This source code was highlighted with Source Code Highlighter .


2. Create the following TV parameters (everything that I did not mention below - we leave it by default):

counter , in the Input Type drop-down list, select Number . The default value is -1 . Choose a template. Save.

fileBanner , in the "Input Type" drop-down list, select File . Choose a template. Save.

linkToURL , in the "Input Type" drop-down list, select the URL . Choose a template. Save.

Pages , in the drop-down list "Input Type" select Listbox (Multi-Select) . In the field "Possible values" we write: @SELECT pagetitle, CONCAT (id, ',') FROM site_content WHERE id NOT IN (2,3,4,5,6,7,8,9,10,10,11,234) ORDER BY menuindex
The default value is -1 . Choose a template. Save.

3. Creating a banner store

Now go to the document tree and create a new document with the name "BANNERS", set the checkbox on "Container (contains child documents)". Now find the $ vault variable in the snippet code and assign the ID of the newly created document (container) to it.

In my case this is ID 234
image

Then we add a child document - this is the essence of the banner, at the bottom of the document should be visible the new fields that we created earlier.

image

* If in the field “Number of impressions ...” change from -1 to a natural number, the banner will be shown the equivalent of this number. I think everything is clear with the other fields, if not, ask.
* If you remove activity from the document via the MODx context menu, the banner will also no longer be displayed.
* If you do not select the page, the banner will be displayed on all pages of the site.
* Do not forget to load the banner :)

4. Now insert the [[Banner]] snippet into the place where you want to display the banner.

PS: this solution is quite scalable, you can modify it.

Source: https://habr.com/ru/post/66277/


All Articles