daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Sorted Link List for TinyMCE
  3. // v1.0.3
  4. // By NCrossland
  5. //
  6. // Changelog:
  7. // 1.0: First release
  8. // 1.0.1: Update to fix broken accented characters (thanks davidm)
  9. // 1.0.2: Added choice of breadcrumbs or tree-style display (thanks raum). Setting for charset (thanks mmjaeger)
  10. // 1.0.3: Added a choice of tree indent styles (thanks raum). Added ability to sort by menuindex and improved avoiding showing unpublished documents.
  11. // 1.0.4a: Updated this file with new security + including of more files. $_SESSION is now reconized!
  12. // 1.0.4b: Removed default start session by MODx and changed if statement if loged in
  13. //
  14. // To do:
  15. // * Based on Modx DB API, rather than raw SQL
  16. // * If can't do the above, use less database queries
  17. // * Make interface prettier -- proper icons for documents and file paths. Maybe a select element isn't best for this?!
  18. // * Make options available in manager
  19. // * integrate into TinyMCE plugin release
  20. //
  21. // Installation:
  22. // Replace the contents of tinymce.linklist.php with this file. Done!
  23.  
  24. $manage_path = '../../../../manager/';
  25. include_once $manage_path."includes/config.inc.php";
  26. session_name($site_sessionname);
  27. session_start();
  28.  
  29. include_once $manage_path."includes/document.parser.class.inc.php";
  30. $modx = new DocumentParser;
  31. $modx->loadExtension("ManagerAPI");
  32. $modx->getSettings();
  33. $etomite = &$modx; // for backward compatibility
  34.  
  35. // Config options
  36. $templates_to_ignore = array(); // Template IDs to ignore from the link list
  37. $include_page_ids = false;
  38. $charset = 'UTF-8';
  39. $mode = 'tree'; // breadcrumbs or tree
  40. $tree_style = '1'; // What style should the tree use? Choose 1,2,3 or 4
  41. $sortby = 'menuindex'; // Could be menuindex or menutitle
  42.  
  43. /* That's it to config! */
  44. $tree_styles = array("|--", "&#x2516;&#x2500;&nbsp;", "&#x25B9;&nbsp;&nbsp;", "L&nbsp;&nbsp;");
  45. /* only display if manager user is logged in */
  46. if (empty($_SESSION["mgrValidated"]) || $_SESSION["mgrValidated"] != 1 || session_id() != $_COOKIE[$site_sessionname]) {
  47.     // Make output a real JavaScript file!
  48.     header('Content-type: text/javascript'); // browser will now recognize the file as a valid JS file
  49.    
  50.     // prevent browser from caching
  51.     header('pragma: no-cache');
  52.     header('expires: 0'); // i.e. contents have already expired
  53.    
  54.     echo "var tinyMCELinkList = new Array();";
  55.     exit();
  56. }
  57.  
  58. $allpages = getAllPages();
  59. if (!is_array($allpages) ) {die();}
  60.  
  61. $list = array();
  62.  
  63. foreach($allpages as $page)
  64. {
  65. if (!in_array($page['template'], $templates_to_ignore) )
  66. {
  67. $caption = '';
  68. $page['parents'] = array_reverse($page['parents']);
  69. $breadcrumbs = array();
  70. $sortcrumbs = array();
  71. $published = $page['published'];
  72. foreach ($page['parents'] as $parent)
  73. {
  74. $p = getPage($parent);
  75.  
  76. // Assemble what will be displayed
  77. $breadcrumbs[] = ($p['menutitle'])?htmlentities($p['menutitle'],ENT_QUOTES,$charset):htmlentities($p['pagetitle'],ENT_QUOTES,$charset);
  78.  
  79. // How will it be sorted?
  80. if ($sortby == 'menuindex')
  81. {
  82. $more_sortby_types = array("menutitle","pagetitle");
  83. foreach ($more_sortby_types as $backup_sort_type)
  84. {
  85. if ( $page[$backup_sort_type] != '')
  86. {
  87. $sortcrumbs[] = sprintf("%010d", $p[$sortby]);
  88. break;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. $sortcrumbs[] = $p[$sortby];
  95. }
  96.  
  97. if ($p['published'] != '1')
  98. {
  99. $published = 0;
  100. }
  101. }
  102. if ($mode=='tree')
  103. { // tree mode
  104. $bc_count = count($breadcrumbs);
  105. if ($bc_count>1)
  106. {
  107. $caption = str_repeat('&nbsp;', ($bc_count-1)*3);
  108. $caption .= $tree_styles[$tree_style-1];
  109. $caption .= $breadcrumbs[$bc_count-1];
  110. }
  111. else
  112. {
  113. $caption = $breadcrumbs[0];
  114. }
  115. }
  116. else
  117. { // breadcrumb mode
  118. $caption = implode(': ', $breadcrumbs);
  119. }
  120.  
  121. $keyname = implode('-', $sortcrumbs);
  122.  
  123. // Check for duplicates
  124. while (isset($list[$keyname]))
  125. {
  126. $sortcrumbs[count($sortcrumbs)-1] += 1000000000;
  127. $keyname = implode('-', $sortcrumbs);
  128. }
  129.  
  130. //$caption = $keyname;
  131.  
  132. if (function_exists('mb_encode_numericentity'))
  133. {
  134. $convmap = array(0x0080, 0xffff, 0, 0xffff);
  135. $encoding = $GLOBALS['database_connection_charset'];
  136. $caption = mb_encode_numericentity($caption, $convmap, $encoding);
  137. }
  138. $output = '["' .$caption;
  139. if ($include_page_ids)
  140. {
  141. $output .= ' (' . $page['id'] . ')';
  142. }
  143. $output .= '", "[~' . $page['id'] . '~]"]';
  144.  
  145. if ($published == '1')
  146. {
  147. $list[$keyname] = $output;
  148. }
  149. }
  150. }
  151.  
  152. // Sort the list by it's keys
  153. ksort($list);
  154.  
  155. // Output the array separated by commas
  156. $list_output = implode(", \n", $list);
  157.  
  158. // Output as javascript
  159. $output = "var tinyMCELinkList = new Array(\n". $list_output .");";
  160.  
  161. // Make output a real JavaScript file!
  162. header('Content-type: text/javascript'); // browser will now recognize the file as a valid JS file
  163.  
  164. // prevent browser from caching
  165. header('pragma: no-cache');
  166. header('expires: 0'); // i.e. contents have already expired
  167.  
  168. echo $output;
  169.  
  170.  
  171. function getAllPages($id=0, $sort='parent', $dir='ASC', $fields='pagetitle, id, menutitle, parent, template, menuindex, published')
  172. {
  173. global $dbase;
  174. global $table_prefix;
  175.  
  176.     $tblsc = $dbase.".`".$table_prefix."site_content`";
  177.     $tbldg = $dbase.".`".$table_prefix."document_groups`";
  178.  
  179.     // modify field names to use sc. table reference
  180.     $fields = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$fields)));
  181.     $sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$sort)));
  182.  
  183.     $sql = "SELECT DISTINCT $fields FROM $tblsc sc
  184. LEFT JOIN $tbldg dg on dg.document = sc.id
  185. WHERE sc.published=1 AND sc.deleted=0
  186. ORDER BY $sort $dir;";
  187.  
  188. $resourceArray = doSql($sql);
  189.     for($i=0;$i<@count($resourceArray);$i++) {
  190. $p = getAllParents($resourceArray[$i]['id']);
  191. $resourceArray[$i]['parents'] = $p;
  192.     }
  193.  
  194.     return $resourceArray;
  195. }
  196.  
  197.  
  198. function getAllParents($doc_id) {
  199. $return_array = array($doc_id);
  200. while (getParent($doc_id) != 0) {
  201. $doc_id = getParent($doc_id);
  202. $return_array[] = $doc_id;
  203. }
  204. return $return_array;
  205. }
  206.  
  207. function getParent($doc_id) {
  208. $r = getPage($doc_id);
  209. return $r['parent'];
  210. }
  211.  
  212. function getPage($doc_id)
  213. {
  214. global $dbase;
  215. global $table_prefix;
  216.  
  217. global $page_cache;
  218.  
  219. // If already cached, return this instead of doing another MySQL query
  220. if (isset($page_cache[$doc_id]))
  221. {
  222. return $page_cache[$doc_id];
  223. }
  224.  
  225.  
  226.     $tblsc = $dbase.".".$table_prefix."site_content";
  227.     $tbldg = $dbase.".".$table_prefix."document_groups";
  228.  
  229.     // modify field names to use sc. table reference
  230.     $fields = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$fields)));
  231.     $sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$sort)));
  232.  
  233.     $sql = "SELECT sc.parent, sc.menutitle, sc.pagetitle, sc.menuindex, sc.published FROM $tblsc sc
  234. LEFT JOIN $tbldg dg on dg.document = sc.id
  235. WHERE sc.published=1 AND sc.deleted=0 AND sc.id=$doc_id;";
  236.  
  237. $resourceArray = doSql($sql);
  238.  
  239. // If we have got this far, it must not have been cached already, so lets do it now.
  240. $page_cache[$doc_id] = $resourceArray[0];
  241.  
  242.     return $resourceArray[0];
  243. }
  244.  
  245.  
  246. function doSql($sql)
  247. {
  248. global $modx;
  249. // Connecting, selecting database
  250.  
  251.     $result = $modx->db->query($sql);
  252.     $resourceArray = array();
  253.     for($i=0; $i < $modx->db->getRecordCount($result); $i++)
  254.     {
  255. $par = $modx->db->getRow($result, 'assoc');
  256.       array_push($resourceArray, $par);
  257.     }
  258.  
  259.     return $resourceArray;
  260. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top