PageRenderTime 94ms CodeModel.GetById 68ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/assets/snippets/listindexer/snippet.listindexer.php

https://github.com/good-web-master/modx.evo.custom
PHP | 487 lines | 184 code | 84 blank | 219 comment | 62 complexity | 4b98a44b715fc1d52e5c3f4b49534e40 MD5 | raw file
  1<?php
  2if(!defined('MODX_BASE_PATH')){die('What are you doing? Get out of here!');}
  3// added in 1.0.1: hidePrivate (hide items from unauthorized users)
  4//
  5// Derived from ListIndex 0.6j by jaredc@honeydewdesign.com
  6// Now supports Show In Menu
  7//
  8// This snippet was designed to be a VERY flexible way
  9// of showing what has been recently added. You can use
 10// this snippet to show news from one folder, or what has
 11// been recently added site wide. You can even show what's
 12// new in a 'section' (everything under a defined folder)!
 13
 14// Configuration Settings
 15
 16   // Set the following snippet defaults the way you would normally
 17   // use this snippet, then use snippet variables in other situations
 18   // to override the default behaviors.
 19
 20   // GENERAL OPTIONS
 21
 22   // $rootFolder [ NULL | string (comma separated page id's) ]
 23   // Decide which folder to pull recent documents from.
 24   // If you want to specify a few folders, comma separate them.
 25   // The default NULL will set current page as root. Using 0
 26   // would search site wide. Can be set with snippet parameter
 27   // $LIn_root like:
 28   // [[ListIndexer?LIn_root=3,6,88]] 
 29   $rootFolder = NULL;
 30
 31   // $descendentDepth [ int ]
 32   // How many levels below the root folder(s) do you want to
 33   // include? Can be overridden in snippet call with LIn_depth:
 34   // [[ListIndexer?LIn_depth=2]]
 35   // Uncomment one of these common two or create your own:
 36   // $descendentDepth = 1; // just children of root folder(s)
 37   $descendentDepth = 0; // all decendents of root folder(s)
 38
 39   // $seeThruUnpub [ true | false ]
 40   // When using descendents, do you want to consider files below
 41   // an unpublished (hidden) folder? Usually no. But you decide.
 42   // Snippet parameter: LIn_seeThru
 43   // [[ListIndexer?LIn_seeThru=1]]
 44   $seeThruUnpub = false;
 45
 46   // $seeShowInMenu [ true | false ]
 47   // When using descendents, do you want to consider files flagged
 48   // to be hidden from the menus? Usually no. But you decide.
 49   // Snippet parameter: LIn_seeShowInMenu
 50   // [[ListIndexer?LIn_seeShowInMenu=1]]
 51   $seeShowInMenu = false;
 52   
 53   // $hidePrivate [ true | false ]
 54   // Hide items from users that don't have appropriate
 55   // rights to view. Usually true. But you decide.
 56   // Snippet parameter: LIn_hidePrivate
 57   // [[ListIndexer?LIn_hidePrivate=0]]
 58   $hidePrivate = true;
 59
 60   // $mode [ 'short' | 'full' ]
 61   // Defines whether this list should be a full, paged
 62   // list of all documents, or a short list of the most
 63   // recent few (how many will be defined next). Can be
 64   // overridden in snippet call with $LIn_mode:
 65   // [[ListIndexer?LIn_mode=full]]
 66   $mode = 'short';
 67   
 68   // $sortBy [ 'alpha' | 'date' | 'menuindex' ]
 69   // The default date will sort by most recent items first, but
 70   // by using the 'alpha' option, and using full mode, you could
 71   // use this to create an index, or directory.
 72   // Settable with snippet call $LIn_sort:
 73   // [[ListIndexer?LIn_sort=alpha]]
 74   $sortBy = 'date';
 75   
 76   // $sortDir [ 'ASC' | 'DESC' ]
 77   // Sort direction ascending or descending. Is applied to whatever $sortBy
 78   // field you have chosen above. If left blank, menuindex and alpha will sort
 79   // ASC and date DESC.
 80   // $LIn_dir in snippet call:
 81   // [[ListIndexer?LIn_dir=ASC&LIn_sortBy=menuindex]]
 82   $sortDir = '';
 83
 84   // WHAT TO DISPLAY
 85
 86   // $defaultTitle [ string ]
 87   // If you want a default title for your list
 88   // you can declare it here. Or use an empty
 89   // string to leave this off. This can be overridden
 90   // in the snippet call with the variable $LIn_title:
 91   // [[ListIndexer?LIn_title=Some new title]]
 92   $defaultTitle = '';
 93
 94   // $shortDesc [ true | false ]
 95   // Show the description on the short list, or not. Snippet
 96   // parameter $LIn_sDesc:
 97   // [[ListIndexer?LIn_sDesc=0]]
 98   $shortDesc = true;
 99
100   // $fullDesc [ true | false ]
101   // Show the description on the full list, or not. Snippet
102   // parameter $LIn_fDesc:
103   // [[ListIndexer?LIn_fDesc=0]]
104   $fullDesc = true;
105
106   // $linkToIndex [ int ]
107   // If you have a page set up as an 'index' for all the 
108   // documents in this list, you can link to it by specifying 
109   // its id- can also be set in snippet call with LIn_link:
110   // [[ListIndexer?LIn_link=8]]
111   // The default 0 will eliminate this link
112   $linkToIndex = 0;
113
114   // $indexText [ string ]
115   // If you want a link to an index (probably a page running this
116   // snippet in "full" mode), you can declare what you want that
117   // link to say here. Or in the snippet call with LIn_indexText:
118   // [[ListIndexer?LIn_indexText=Financial News Index]]
119   $indexText = 'Index';
120
121   // $showCreationDate [ true | false ]
122   // Decide to include date of creation in output or not. From
123   // snippet call $LIn_showDate use 1 (true) or 0 (false)
124   // [[ListIndexer?LIn_showDate=1]]
125   $showCreationDate = true;
126
127   // $dateFormat [ string ]
128   // Used to define how date will be displayed (if using date)
129   // Y = 4 digit year     y = 2 digit year
130   // M = Jan - Dec        m = 01 - 12
131   // D = Sun - Sat        d = 01 -31
132   // Other standard PHP characters may be used
133   $dateFormat = 'Y.m.d';
134
135   // $shortQty [ int ]
136   // Number of entries to list in the short list. Can be
137   // overridden in snippet call with $LIn_sQty:
138   //[[ListIndexer?LIn_sQty=3]]
139   $shortQty = 3;
140
141   // PAGING
142
143   // $fullQty [ int ]
144   // Number of entries PER PAGE to list in the full list
145   // Can be overridden in snippet call with $LIn_fQty:
146   // [[ListIndexer?LIn_fQty=20]]
147   // To show all set to 0 here or in snippet call
148   $fullQty = 10;
149   
150   // $pageSeparator [ string ]
151   // What you want your page number links to be separated by.
152   // You NEED to include spaces if you want them. They are NOT
153   // created automatically to facilitate styling ability.
154   // For instance, " | " will render links like:
155   // 1 | 2 | 3 | 4
156   $pageSeparator = " | ";
157   
158   // $pgPosition [ 'top' | 'bottom' | 'both']
159   // Pick where you want your pagination links to appear.
160   $pgPosition = 'both';
161
162   // PERFORMANCE
163
164   // $useFastUrls [ true | false ]
165   // IMPORTANT- using fast urls will reduce database queries
166   // and improve performance WHEN IN FULL MODE ONLY and 
167   // should NOT be used when multiple instances of this snippet
168   // appear on the same page. With snippet call LIn_fast use 1
169   // (true) or 0 (false)
170   // [[ListIndexer?LIn_fast=0]]
171   $useFastUrls = false;
172
173   // $newLinesForLists [ true | false ]
174   // Depending on how you want to style your list, you may
175   // or may not want your <li>s on new lines. Generally, if you
176   // are displaying then inline (horizontal, you do not want new
177   // lines, but standard vertical block styling you do. This is
178   // for IE, real browsers don't care.
179   $newLinesForLists = true;
180
181// Styles
182//
183// The following are the styles included in this snippet. It is up
184// to you to include these styles in your stylesheet to get them to
185// look the way you want.
186
187   // div.LIn_title {}          List title div
188   // ul.LIn_fullMode {}        UL class
189   // ul.LIn_shortMode {}       UL class
190   // span.LIn_date {}          Span surrounding pub/created date
191   // span.LIn_desc {}          Span surrounding description
192   // div.LIn_pagination        Div surrounding pagination links
193   // span.LIn_currentPage {}   Span surrounding current page of
194   //                           pagination (which wouldn't be css-able
195   //                           by virtue of its <a> tag)
196
197
198// **********************************************************************
199// END CONFIG SETTINGS
200// THE REST SHOULD TAKE CARE OF ITSELF
201// **********************************************************************
202
203// Take care of IE list issue
204$ie = ($newLinesForLists)? "\n" : '' ;
205
206// Use snippet call defined variables if set
207$activeTitle = (isset($LIn_title))? $LIn_title : $defaultTitle ;
208$mode = (isset($LIn_mode))? $LIn_mode : $mode ;
209$descendentDepth = (isset($LIn_depth))? $LIn_depth : $descendentDepth ;
210$seeThruUnpub = (isset($LIn_seeThru))? $LIn_seeThru : $seeThruUnpub ;
211$seeShowInMenu = (isset($LIn_seeShowInMenu))? $LIn_seeShowInMenu : $seeShowInMenu ;
212$hidePrivate = (isset($LIn_hidePrivate))? $LIn_hidePrivate : $hidePrivate;
213$linkToIndex = (isset($LIn_link))? $LIn_link : $linkToIndex ;
214$rootFolder = (isset($LIn_root))? $LIn_root : $rootFolder ;
215$shortQty = (isset($LIn_sQty))? $LIn_sQty : $shortQty ;
216$fullQty = (isset($LIn_fQty))? $LIn_fQty : $fullQty ;
217$showCreationDate = (isset($LIn_showDate))? $LIn_showDate : $showCreationDate ;
218$indexText = (isset($LIn_indexText))? $LIn_indexText : $indexText ;
219$useFastUrls = (isset($LIn_fast))? $LIn_fast : $useFastUrls ;
220$sortBy = (isset($LIn_sort))? $LIn_sort : $sortBy;
221$shortDesc = (isset($LIn_sDesc))? $LIn_sDesc : $shortDesc ;
222$fullDesc = (isset($LIn_fDesc))? $LIn_fDesc : $fullDesc ;
223$sortDir = (isset($LIn_dir))? $LIn_dir : $sortDir ;
224if ($sortDir == '') $sortDir = ($sortBy == 'date')? 'DESC' : 'ASC' ;
225
226
227// Make useful variable shortcut for the content table
228//$tbl = $modx->dbConfig['dbase'] . "." . $modx->dbConfig['table_prefix'] . "site_content";
229$tblsc = $modx->getFullTableName("site_content");
230$tbldg = $modx->getFullTableName("document_groups");
231
232// Initialize output
233$output = '';
234
235// ---------------------------------------------------
236// ---------------------------------------------------
237// Query db for parent folders, or not. First check to
238// see if a querystring cheat has been provided- this
239// should speed things up considerably when using this
240// in full mode. (a.k.a. fastUrls)
241// ---------------------------------------------------
242// ---------------------------------------------------
243$inFolder= isset($_GET['LIn_f'])? $_GET['LIn_f']: 0;
244if ((!$inFolder && $useFastUrls) || !$useFastUrls ){
245  // Only run all the database queries if we don't already
246  // know the folders AND fastUrls are desired.
247
248  // ---------------------------------------------------
249  // Seed list of viable parents
250  // ---------------------------------------------------
251
252  if ($rootFolder == NULL){
253    $rootFolder = $modx->documentIdentifier;
254  }
255  // Set root level parent array
256  $seedArray = explode(',',$rootFolder);
257  $parentsArray = array();
258  foreach($seedArray AS $seed){
259    $parentsArray['level_0'][] = $seed;
260  }
261
262  // ---------------------------------------------------
263  // Make array of all allowed parents
264  // ---------------------------------------------------
265
266  // Process valid parents
267  $levelCounter = 1;
268
269  while (((count($parentsArray) < $descendentDepth) || ($descendentDepth == 0)) && ($levelCounter <= count($parentsArray)) && ($levelCounter < 10)){
270
271    // Find all decendant parents for this level
272    $pLevel = 'level_'.($levelCounter - 1);
273    $tempLevelArray = $parentsArray[$pLevel];
274
275    foreach($tempLevelArray AS $p){
276
277      // Get children who are parents (isfolder = 1)
278      $validParentSql = "";
279      $validParentSql .= "SELECT id FROM $tblsc sc WHERE ";
280      $validParentSql .= "isfolder = 1 AND parent = $p ";
281      $validParentSql .= "AND sc.deleted=0 ";
282      $validParentSql .= ($seeThruUnpub)? ";" : "AND sc.published = 1;";
283
284      // Run statement
285      $rsTempParents = $modx->dbQuery($validParentSql);
286      // Get number of results
287      $countTempParents = $modx->recordCount($rsTempParents);
288
289      // If there are results, put them in an array
290      $tempValidArray = false;
291      if ($countTempParents){
292        for ($i=0;$i<$countTempParents;$i++){
293          $tempId = $modx->fetchRow($rsTempParents);
294          $tempValidArray[] = $tempId['id'];
295        } // end while
296      } // end if
297
298	  // populate next level of array 
299	  if ($tempValidArray){
300	    foreach($tempValidArray AS $kid){
301	      $kidLevel = 'level_'.$levelCounter;
302	      $parentsArray[$kidLevel][] = $kid;
303	    } // end foreach
304
305	  } // end if
306    } // end foreach
307
308    // Do next level
309    $levelCounter++;
310
311  } // end while
312
313  // Finalize list of parents
314  $validParents = '';
315  foreach ($parentsArray AS $level){
316    foreach ($level AS $validP){
317      $validParents .= $validP . ',';
318    }
319  }
320
321  // Remove trailing comma
322  $validParents = substr($validParents,0,strlen($validParents)-1);
323
324} else {
325  $validParents = $_GET['LIn_f'];
326}
327
328// ---------------------------------------------------
329// Make appropriate SQL statement to pull recent items
330// ---------------------------------------------------
331
332// get document groups for current user
333if($docgrp = $modx->getUserDocGroups()) $docgrp = implode(",",$docgrp);
334
335$access = " (".($modx->isFrontend() ? "sc.privateweb=0":"1='".$_SESSION['mgrRole']."' OR sc.privatemgr=0").
336          (!$docgrp ? "":" OR dg.document_group IN ($docgrp)").") AND ";
337
338// Initialize
339$recentSql = "";
340$recentSql .= "SELECT sc.id, pagetitle, description";
341// Include pub_date or createdon date if date is desired
342$recentSql .= ($showCreationDate)? ", IF(pub_date > 0, pub_date, createdon) AS pubDate ": " " ;
343$recentSql .= "FROM $tblsc sc LEFT JOIN $tbldg dg on dg.document = sc.id ";
344$recentSql .= "WHERE ";
345$recentSql .= ($hidePrivate)? $access:"";
346// Look everywhere, or just under valid parents
347$recentSql .= (($rootFolder == 0) && $seeThruUnpub && ($descendentDepth == 0))? "" : "parent IN ($validParents) AND " ;
348// Published
349$recentSql .= "sc.published = 1 ";
350// Show In Menu
351$recentSql .= ($seeShowInMenu)? " " : " AND sc.hidemenu=0 " ;
352// Not deleted
353$recentSql .= "AND sc.deleted=0 ";
354// Choose sort method
355switch ($sortBy){
356  case 'alpha':
357    $recentSql .= "ORDER BY pagetitle ";
358    break;
359  case 'menuindex':
360    $recentSql .= "ORDER BY menuindex ";
361    break;
362  default:
363    $recentSql .= "ORDER BY IF(pub_date>0, pub_date, createdon) ";
364    break;
365}
366// Provide a sort direction
367$recentSql .= $sortDir;
368
369// If this is a short list, just pull a limited number
370$recentSql .= ($mode == 'short')? " LIMIT $shortQty;" : ";" ;
371
372// Run statement
373$rsRecent = $modx->dbQuery($recentSql);
374// Count records
375$recentLimit = $modx->recordCount($rsRecent);
376
377// ---------------------------------------------------
378// Generate pagination string if needed
379// ---------------------------------------------------
380$offsetParam = isset($_GET['LIn_o'])? $_GET['LIn_o']: 0;
381$offset = ($offsetParam && ($mode == 'full'))? $offsetParam : 0 ;
382$pagination = '';
383
384// Don't bother unless there are enough records to justify it
385if ( ($mode == 'full') && ($recentLimit > $fullQty) && ($fullQty) ){
386  $fullUrl = $_SERVER['REQUEST_URI'];
387  $urlPieces = parse_url($fullUrl);
388  $urlPath = $urlPieces['path'];
389  $otherQs = '';
390
391  if ($urlPieces['query']){
392    foreach($_GET AS $qsKey=>$qsValue){
393	  if (($qsKey != 'LIn_o') && ($qsKey != 'LIn_f')){
394	    $otherQs .= '&'.$qsKey.'='.$qsValue;
395	  }
396	}
397  } 
398  
399  $fastUrl = $urlPath.'?LIn_f='.$validParents.$otherQs;
400
401  // Determine number of pages needed to show results
402  $totalPages = ceil($recentLimit/$fullQty);
403  
404  // Make links
405  for ($j = 0 ; $j < $totalPages; $j++){
406    // only include links to OTHER pages, not current page
407    if($offset == $j*$fullQty){
408	  $pagination .= '<span class="LIn_currentPage">'.($j+1) .'</span>';
409	} else {
410      $pagination .= '<a href="'.$fastUrl.'&LIn_o='.($j*$fullQty).'" title="'.($j+1).'">'.($j+1) .'</a>';
411	}
412	if ($j < $totalPages-1){
413	  $pagination .= $pageSeparator;
414	}
415  }
416  
417  // Make final pagination link set in it's own div
418  $pagination = '<div class="LIn_pagination">'."\n".$pagination."\n</div>\n";
419  
420}
421
422
423// ---------------------------------------------------
424// Create title if wanted
425// ---------------------------------------------------
426
427if ($activeTitle){
428  $output .= '<div class="LIn_title">'.$activeTitle.'</div>'."\n";
429}
430
431// ---------------------------------------------------
432// Create list of recent items
433// ---------------------------------------------------
434
435// Include pagination
436$output .= ($pgPosition == 'top' || $pgPosition == 'both')? $pagination : '' ;
437
438$output .= '<ul class="LIn_'.$mode.'Mode">' . $ie;
439
440$recentCounter = $offset;
441if ($mode == 'short') {
442  $recentCounterLimit = min($shortQty,$recentLimit);
443} else {
444  $recentCounterLimit = ($fullQty)? min(($fullQty+$offset),$recentLimit) : $recentLimit ;
445}
446
447while (($recentCounter < $recentCounterLimit) && $rsRecent && ($recentLimit > 0)){
448  mysql_data_seek($rsRecent,$recentCounter);
449  $recentRecord = $modx->fetchRow($rsRecent);
450  $output .= '<li>';
451  // Link to page
452  $output .= '<a href="[~'.$recentRecord['id'].'~]" title="'.strip_tags($recentRecord['pagetitle']).'">'.$recentRecord['pagetitle'].'</a> ';
453  // Date if desired
454  if ($showCreationDate){
455    $output .= '<span class="LIn_date">'.date($dateFormat,$recentRecord['pubDate']).'</span> ';
456  }
457  // Description if desired
458  if ((($mode == 'short') && ($shortDesc)) || (($mode == 'full') && ($fullDesc))){
459   $output .= '<span class="LIn_desc">'.$recentRecord['description'].'</span>';
460  }
461  // wrap it up
462  $output .= '</li>' . $ie;
463  $recentCounter ++;
464}
465
466$output .= '</ul>' . $ie;
467
468$output .= ($pgPosition == 'bottom' || $pgPosition == 'both')? $pagination : '' ;
469
470// ---------------------------------------------------
471// Link to index
472// ---------------------------------------------------
473
474if ($linkToIndex) {
475
476  $output .= '<div class="LIn_index">';
477  $output .= '<a href="[~'.$linkToIndex.'~]" title="'.$indexText.'">'.$indexText.'</a>';
478  $output .= '</div>';
479
480}
481
482// ---------------------------------------------------
483// Send to browser
484// ---------------------------------------------------
485
486return $output;
487?>