We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8548
    • 104 Posts
    Hello all,

    I start by saying sorry for the code that might not be optimal.

    I'm trying to make my custom snippet work with getPage.
    how the snippet works on the front end:

    1. When page loads, it gets all the events available
    2. The user can search by date and event type (reloads the page whith the result that is passed by query sting)

    What I can't figure out is how to make pagination by using getPage with my custom snippet, The only result returne when using getPage is (no events found) but when I call it direcly I get the result expected with out the pagination.

    I've found this post and whent trought the tutorial in it : https://forums.modx.com/thread/100349/using-getpage-with-complex-custom-snippet but still can't figure it out sad

    Here's what I have.

    The snippent
    <?php
    //setlocale(LC_ALL, 'fr_FR.UTF-8');
    /**
     * eCalendrier
     *
     * DESCRIPTION
     *
     * This Snippet is an event calendar based on resources identified as events
     * by giving the id of the parent resource with a start event date and end event date.
     *
     * PROPERTIES:
     *
     * &tpl name of the chunk to use as template. Default: eCalendrier.tpl.php
     * &parentID integer required - tell the snippet what is the id of the parent resources for the events
     * &sortDir sort direction of result. Default: ASC
     *
     * USAGE:
     *
     * [[!eCalendrier? &tpl=`MyChunkName` &parentID=`7` &sortDir=`ASC`]]
     *
     */
    
    // fonction to put mounth in french
    function moisFR($mois) {
        switch ($mois)
        {
            case (01) : $moisFR = "janvier";   break;
            case (02) : $moisFR = "février";   break;
            case (03) : $moisFR = "mars";      break;
            case (04) : $moisFR = "avril";     break;
            case (05) : $moisFR = "mai";       break;
            case (06) : $moisFR = "juin";      break;
            case (07) : $moisFR = "juillet";   break;
            case (08) : $moisFR = "août";      break;
            case (09) : $moisFR = "septembre"; break;
            case (10) : $moisFR = "octobre";   break;
            case (11) : $moisFR = "novembre";  break;
            case (12) : $moisFR = "décembre";  break;
        }
        return $moisFR;
    }
    
    /* ============  fonction to create timestamps ============= */
    function makeTS( $dateTo ){
      // dateConvert
      // SMF birthday to timestamp
      // input: $SMFday, date in format DD-MM-YYYY
      // output: Unix timestamp
    
      $dateElements = explode('-', $dateTo);
       
      // arguments: hour, minute, second, month, day, year
      // NB: hour set to 1 to avoid problem with DST
      $newDate = mktime( 0, 0, 01, $dateElements[1], $dateElements[0], $dateElements[2] );
      return $newDate;
    }
    
    /* ============= variables ================ */
    $dturl = isset($_GET['c']) ? $_GET['c'] : ''; // passe date début dans URL
    $dfturl = isset($_GET['c']) ? $_GET['f'] : ''; // passe date fin dans URL
    $ec_l = isset($_GET['c']) ? $_GET['ec_l'] : ''; // passe catégorie dans URL
    $ec_errormsg = 'Aucune entrée trouvé avec vos critères de recherche';
    
    
    $tpl = $modx->getOption('tpl',$scriptProperties,'eCalendrier_TPL');
    $sortDir = $modx->getOption('sortDir',$scriptProperties,'ASC');
    $parentID = (int) $modx->getOption('parentID',$scriptProperties,0);
    $rowCls = $modx->getOption('rowCls',$scriptProperties,'row');
    $currentDate = strtotime( date("Y-m-d") );
    
    $output = '';
    
    /* ===== get the resources as xPDOObjects ===== */
    
    /* first, build the query */
    $c = $modx->newQuery('modResource');
    /* we join the custom tv for sortby */
    $c->innerJoin('modTemplateVarResource','TemplateVarResources');
    $c->innerJoin('modTemplateVar','TemplateVar','`TemplateVar`.`id` = `TemplateVarResources`.`tmplvarid` AND `TemplateVar`.`name` = "eCalendrierDateTV"');
    
    $c->where(array(
       'parent:IN' => array($parentID),
       'deleted' => 0,
       'hidemenu' => 1,
       'published' => 1,
    ));
    
    
    // === pour la pagination avec getPage
    
    $total = $modx->getCount('modResource',$c);
    $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
    $modx->setPlaceholder($totalVar,$total);
     
    $limit = $modx->getOption('limit',$scriptProperties,0); // limite le nombre de résultats 0 = illimité
    $offset = $modx->getOption('offset',$scriptProperties,0);
    $c->limit($limit,$offset);
    
    // === fin de la pagination avec getPage
    
    $c->sortby('`TemplateVarResources`.`value`',$sortDir);
    //$collection = $modx->getCollection('modResource',$c);
    
    
    foreach ($resources as $resource) {
    
    	/* ===== we get the TV values ===== */
    
    	$eDateDebut = $resource->getTVValue('eCalendrierDateTV');
    	$eDateFin = $resource->getTVValue('eCalendrierDateFinTV');
    	$eLieu = $resource->getTVValue('eCalendrierLieuTV');
    	$eLieuUrl = $resource->getTVValue('eCalendrierLieuGmapTV');
    	$eTypeEvent = $resource->getTVValue('eCalendrierTypeActiviteTV');
    
    
    
    
    	$eBtnTxt = $resource->getTVValue('eCalendrierBtnTxtTV');
    	$eBtnLnk = $resource->getTVValue('eCalendrierBtnLinkTV');
    	$eImage = $resource->getTVValue('eCalendrierImageTV');
    	
    	/* ====== date manipulation ===== */
    	// split date debut
    	  $Djour = date( "j", strtotime( $eDateDebut ) );
    	  $Dmois = date( "m", strtotime( $eDateDebut ) );
    		$Dmois = moisFR($Dmois);
    	  $Dannee = date( "Y", strtotime( $eDateDebut ) );
    	  $Dheure = date( "G", strtotime( $eDateDebut ) );
    	  $Dminute = date( "i", strtotime( $eDateDebut ) );
    	// if minutes of start time = 00 we remove them
    		if( $Dminute != '00'){ $Dminute = $Dminute; }else{ $Dminute = ''; }
    	// split for date fin
    	  $Fjour = date( "j", strtotime( $eDateFin ) );
    	  $Fmois = date( "m", strtotime( $eDateFin ) );
    		  $Fmois = moisFR($Fmois);
    	  $Fannee = date( "Y", strtotime( $eDateFin ) );
    	  $Fheure = date( "G", strtotime( $eDateFin ) );
    	  $Fminute = date( "i", strtotime( $eDateFin ) );
    	// if minutes of end time = 00 we remove them
    		if( $Fminute != '00'){ $Fminute = $Fminute; }else{ $Fminute = ''; }
    	// format end time
    		$eFormatTimeFin = $Fheure .' h ' . $Fminute;
    	// format date output
    		$eFormatDebut = $Djour .' '. $Dmois .' '. $Dannee; 
    		$eFormatTimeDebut = $Dheure .' h ' . $Dminute;
    	// if end date empty or year before start date
    		if( $Fannee >= $Dannee){
    			$eFormatFin = $Fjour .' '. $Fmois .' '. $Fannee;
    		}else{
    			$eFormatFin = '';
    		}
    	
    // on affiche seulement si la date du jour = aujourd'hui ou un jour après aujourd'hui.
    
    
    
    
    $curdate = makeTS( date("j-m-Y", $currentDate) ); // date actuel
    $ddate = makeTS( date("j-m-Y", strtotime( $eDateDebut )) ); // date début événement
    $edate = makeTS( date("j-m-Y", strtotime( $eDateFin )) ); // date fin événement
    $demain = makeTS( date("j-m-Y", strtotime("tomorrow") ) ) ; // date demain
    $dixjour = makeTS( date('j-m-Y', strtotime("+10 days") ) ); // aujourd'hui + 10 jours
    $moinsdixjour = makeTS( date('j-m-Y', strtotime("-10 days") ) ); // aujourd'hui - 10 jours
    
    if( $dturl != ''){  $dturlT = makeTS( date("j-m-Y", strtotime( $dturl) ) ); } // si une date de debut est clické ou dans champ rechreche date DE
    if( $dfturl != ''){  $dfturlT = makeTS( date("j-m-Y", strtotime( $dfturl) ) ); } // si une date de fin dans champ de recherche date À
    
    /* ===== output section formating ===== */
    	  	$resourceArray = $resource->toArray();
    		$resourceArray['rowCls'] = $rowCls;
    		$resourceArray['eImage'] = $eImage;
    		$resourceArray['eFormatDebut'] = $eFormatDebut;
    		$resourceArray['eFormatTimeDebut'] = $eFormatTimeDebut;
    		$resourceArray['eFormatTimeFin'] = $eFormatTimeFin;
    		$resourceArray['bouton'] = $bouton;
    		$resourceArray['eFormatFin'] = $eFormatFin;
    		$resourceArray['eLieuUrl'] = $eLieuUrl;
    		$resourceArray['eLieu'] = $eLieu;
    		$resourceArray['eTypeEvent'] = $eTypeEvent;
    		$resourceArray['eBtnLnk'] = $eBtnLnk;
    		$resourceArray['eBtnTxt'] = $eBtnTxt;
    		$resourceArray['ddate'] = $ddate;
    		$resourceArray['edate'] = $edate;
    
    /* ===== END output section formating ===== */
    
    
    
    	if( $ec_l != '' AND  $dturl != '' AND $dfturl != ''){ 							// si les 3 option ne sont pas vide
    			if ( strstr($eTypeEvent, $ec_l) AND $ddate >= $dturlT AND $edate <= $dfturlT ) {
    				$output .= $modx->getChunk($tpl,$resourceArray);
    			}
    	}else if( $ec_l != '' AND  $dturl != '' AND $dfturl == '' ){ 					// si les type et date debut ne sont pas vide
    			if ( strstr($eTypeEvent, $ec_l) AND $ddate >= $dturlT ) {
    				$output .= $modx->getChunk($tpl,$resourceArray);
    			}
    	}else if( $ec_l != '' AND  $dturl == '' AND $dfturl != ''){						// si les type et date de fin ne sont pas vide
    			if ( strstr($eTypeEvent, $ec_l) AND $edate <= $dfturlT ) {
    				$output .= $modx->getChunk($tpl,$resourceArray);
    			}
    	}else if( $ec_l != '' AND  $dturl == '' AND $dfturl == ''){						// si seulement le type n'est pas vide, on va chercher ceux qui sont aujourd'hui ou apres.
    			if( strstr($eTypeEvent, $ec_l)  AND $ddate >= $curdate ){ 		
    						$output .= $modx->getChunk($tpl,$resourceArray);
    			  }
    	}else if( $ec_l == '' AND  $dturl != '' AND $dfturl != ''){						// si les date debut et de fin ne sont pas vide (debute entre ses 2 dates)
    				if ( ($ddate >= $dturlT && $ddate <= $dfturlT) && $edate <= $dfturlT  ) {
    					$output .= $modx->getChunk($tpl,$resourceArray);
    				}
    	}else if( $ec_l == '' AND  $dturl != '' AND $dfturl == ''){						// si les date debut n'est pas vide
    				if ( $ddate >= $dturlT ) {
    					$output .= $modx->getChunk($tpl,$resourceArray);
    				}
    	}else if( $ec_l == '' AND  $dturl == '' AND $dfturl != ''){						// si les date de fin n'est pas vide
    				if ( $ddate >= $curdate && $ddate <= $dfturlT ) {
    					$output .= $modx->getChunk($tpl,$resourceArray);
    				}
    	}else{																// si non on affiche tout à partir de la dte du jour OU si la date de fin est après aujourd'hui
    				if ( $ddate >= $curdate AND $date <= $dixjour OR $edate >= $curdate AND $date <= $dixjour ) {
    					$output .= $modx->getChunk($tpl,$resourceArray);
    				}
    	}
    
    }
    
    if($output == ''){ $output = $ec_errormsg; } // si aucun resultats!
    
    return $output;


    the snippet call that give result without the pagination:
    [[!dev_eCalendrier?  &parentID=`1491` &tpl=`eCalendrier_TPL` &rowCls=`row` &limit=`0` &c=`[[!time]]` ]]


    and the getPage call that only returns the no event found (ec_errormsg)
    [[!getPage@templateGetPage?
      &elementClass=`modSnippet`
      &element=`dev_eCalendrier`
      &showHidden=`1`
       &depth=`1`
      &tpl=`dev_eCalendrier_TPL`
      &processTVs=`1`
      &parents=`1491`
      &hideContainers=`1`
       &pageVarKey=`page`
       &pageLimit=`5`
       &includeTVs=`1`
       &includeContent=`1`
       &pageFirstTpl=`<li class="control"><a[[+classes]][[+title]] href="[[+href]]">Début</a></li>`
       &pageLastTpl=`<li class="control"><a[[+classes]][[+title]] href="[[+href]]">Fin</a></li>` 
    
       &rowCls=`row`
       &c=`[[!time]]`
    ]]
    


    hope someone can help me figure this out.

    thank you.

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

    [ed. note: Egam last edited this post 7 years, 8 months ago.]
      • 8548
      • 104 Posts
      No one has any clue on how I could do that?

      :(
      • discuss.answer
        • 3749
        • 24,544 Posts
        Put these in your getPage tag. They will be passed through to your snippet:

        &parentID=`1491` 
        &tpl=`eCalendrier_TPL` 
        &rowCls=`row` &limit=`0` 
        &c=`[[!time]]`
        



        @templateGetPage Is a reference to a property set. Have you created that property set? If you pass the properties in the getPage tag as suggested, you may not need it.

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 8548
          • 104 Posts
          Quote from: BobRay at Aug 31, 2016, 03:04 PM
          Put these in your getPage tag. They will be passed through to your snippet:

          &parentID=`1491` 
          &tpl=`eCalendrier_TPL` 
          &rowCls=`row` &limit=`0` 
          &c=`[[!time]]`
          



          @templateGetPage Is a reference to a property set. Have you created that property set? If you pass the properties in the getPage tag as suggested, you may not need it.


          Thanks BobRay, I will try very soon (add to let this project beside for a few days this morning for an other more urgent).
          Knowing you, I'm positive that it will work smiley
            • 8548
            • 104 Posts
            Thanks BobRay,

            all working now... only have to fix my snippet to make filtering work with custom tv's.
            Quote from: BobRay at Aug 31, 2016, 03:04 PM
            Put these in your getPage tag. They will be passed through to your snippet:

            &parentID=`1491` 
            &tpl=`eCalendrier_TPL` 
            &rowCls=`row` &limit=`0` 
            &c=`[[!time]]`
            



            @templateGetPage Is a reference to a property set. Have you created that property set? If you pass the properties in the getPage tag as suggested, you may not need it.