PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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