We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21056
    • 327 Posts
    Don’t know about you, but I often find it confusing when the link list just lists all the pages of the site in one big alphabetical list. It’s hard to pinpoint pages within one section.

    I’ve changed the linklist file (tinymce.linklist.php) to return the list of links grouped by what section they are in. They also have the breakcrumb trail so you can quickly identify the section. There are a couple of options at the top of the file, so you can choose to ignore certain templates (for example if you have a template used for data, or to ignore blank template) and also choose to hide the page IDs from the link list (many non-techy users don’t need them). ideally they would be plugin module options in the GUI, but at the moment I don’t know how to incorporate them. If someone would like to incorporate this, it would be most welcome!

    (Hope the coding style is OK; it’s actually based on an older version of the plugin as I did it quite a few months ago).

    <?php
    
    // Sorted Link List for TinyMCE
    // v1.0.3
    // By NCrossland
    //
    // Changelog:
    // 1.0: First release
    // 1.0.1: Update to fix broken accented characters (thanks davidm)
    // 1.0.2: Added choice of breadcrumbs or tree-style display (thanks raum). Setting for charset (thanks mmjaeger)
    // 1.0.3: Added a choice of tree indent styles (thanks raum). Added ability to sort by menuindex and improved avoiding showing unpublished documents. 
    //
    // To do:
    // * Based on Modx DB API, rather than raw SQL
    // * If can't do the above, use less database queries
    // * Make interface prettier -- proper icons for documents and file paths. Maybe a select element isn't best for this?!
    // * Make options available in manager
    // * integrate into TinyMCE plugin release
    //
    // Installation:
    // Replace the contents of tinymce.linklist.php with this file. Done!
    
    // Config options
    $templates_to_ignore = array();	// Template IDs to ignore from the link list
    $include_page_ids = false;
    $charset = 'UTF-8';
    $mode = 'tree'; // breadcrumbs or tree
    $tree_style = '1'; // What style should the tree use? Choose 1,2,3 or 4
    $sortby = 'menuindex'; // Could be menuindex or menutitle
    $path_to_modx_config = '../../../manager/includes/config.inc.php';
    
    
    /* That's it to config! */
    $tree_styles = array("|--", "┖─ ", "▹  ", "L  ");
    
    include_once($path_to_modx_config);
    
    $allpages = getAllPages();
    if (!is_array($allpages) ) {
    	die();
    }
    
    $list = array();
    
    foreach($allpages as $page){
    	if (!in_array($page['template'], $templates_to_ignore) )   {
    			$caption = '';
    			$page['parents'] = array_reverse($page['parents']);
    			$breadcrumbs = array();
    			$sortcrumbs = array();
    			$published = $page['published'];
    			foreach ($page['parents'] as $parent) {
    				$p = getPage($parent);
    				
    				// Assemble what will be displayed
    				$breadcrumbs[] = ($p['menutitle'])?htmlentities($p['menutitle'],ENT_QUOTES,$charset):htmlentities($p['pagetitle'],ENT_QUOTES,$charset);
    				
    				// How will it be sorted?
    				if ($sortby == 'menuindex') {
    					$more_sortby_types = array("menutitle","pagetitle");
    					foreach ($more_sortby_types as $backup_sort_type) {
    							if ( $page[$backup_sort_type] != '') {
    							$sortcrumbs[] = sprintf("%010d", $p[$sortby]);
    							break;
    							}
    					}
    				} else {
    					$sortcrumbs[] = $p[$sortby];
    				}
    				
    				if ($p['published'] != '1') {
    					$published = 0;
    				}
    				
    			}
    			if ($mode=='tree') {	// tree mode
    				$bc_count = count($breadcrumbs);
    				if ($bc_count>1) {
    					$caption = str_repeat(' ', ($bc_count-1)*3);
    					$caption .= $tree_styles[$tree_style-1];
    					$caption .= $breadcrumbs[$bc_count-1];
    				} else {
    					$caption = $breadcrumbs[0];
    				}
    				
    			} else {	// breadcrumb mode
    				$caption = implode(': ', $breadcrumbs);
    			}
    			
    			$keyname = implode('-', $sortcrumbs);
    			
    			// Check for duplicates
    			while (isset($list[$keyname])) {
    				$sortcrumbs[count($sortcrumbs)-1] += 1000000000;
    				$keyname = implode('-', $sortcrumbs);
    			}
    			
    			//$caption = $keyname;
    			$output = "[\"".$caption;
    			if ($include_page_ids) {
    				$output .= " (".$page['id'].")";
    			}
    			$output .= "\", \"[\"+\"~".$page['id']."~\"+\"]\"]";
    			
    			if ($published == '1') {
    				$list[$keyname] = $output;
    			}
    			
    	}
    }
    
    // Sort the list by it's keys
    ksort($list);
    
    // Output the array separated by commas
    $list_output = implode(", \n", $list);
    
    // Output as javascript
    $output = "var tinyMCELinkList = new Array(\n". $list_output .");";
    
    echo $output;
    
    
    
    
    function getAllPages($id=0, $sort='parent', $dir='ASC', $fields='pagetitle, id, menutitle, parent, template, menuindex, published') {
    
    	global $dbase;
    	global $table_prefix;	
    
        $tblsc = $dbase.".".$table_prefix."site_content";
        $tbldg = $dbase.".".$table_prefix."document_groups";
    
        // modify field names to use sc. table reference
        $fields = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$fields)));
        $sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$sort)));
    
        $sql = "SELECT DISTINCT $fields FROM $tblsc sc
          LEFT JOIN $tbldg dg on dg.document = sc.id
          WHERE sc.published=1 AND sc.deleted=0
          ORDER BY $sort $dir;";
    	  
    	$resourceArray = doSql($sql);
        for($i=0;$i<@count($resourceArray);$i++)  {
    		$p = getAllParents($resourceArray[$i]['id']);
    		$resourceArray[$i]['parents'] = $p;
        }
    
        return $resourceArray;
    }
    
    
    function getAllParents($doc_id) {
    	$return_array = array($doc_id);
    	while (getParent($doc_id) != 0) {
    		$doc_id = getParent($doc_id);
    		$return_array[] = $doc_id;
    	} 
    	return $return_array;
    }
    
    function getParent($doc_id) {
    	$r = getPage($doc_id);
    	return $r['parent'];
    }
    
    function getPage($doc_id) {
    	global $dbase;
    	global $table_prefix;	
    	
    	global $page_cache;
    	
    	// If already cached, return this instead of doing another MySQL query
    	if (isset($page_cache[$doc_id])) {
    		return $page_cache[$doc_id];
    	}
    	
    
        $tblsc = $dbase.".".$table_prefix."site_content";
        $tbldg = $dbase.".".$table_prefix."document_groups";
    
        // modify field names to use sc. table reference
        $fields = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$fields)));
        $sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$sort)));
    
        $sql = "SELECT sc.parent, sc.menutitle, sc.pagetitle, sc.menuindex, sc.published FROM $tblsc sc
          LEFT JOIN $tbldg dg on dg.document = sc.id
          WHERE sc.published=1 AND sc.deleted=0 AND sc.id=$doc_id;";
    	  
    	$resourceArray = doSql($sql);
    	
    	// If we have got this far, it must not have been cached already, so lets do it now.
    	$page_cache[$doc_id] = $resourceArray[0];
    
        return $resourceArray[0];
    }
    
    
    function doSql($sql) {
        global $database_type;
        global $database_server;
        global $database_user;
        global $database_password;    
    	global $dbase;
    	global $table_prefix;	
    
    	// Connecting, selecting database
    	$link = mysql_connect($database_server, $database_user, $database_password) or die('Could not connect: ' . mysql_error());
    	$dbase = str_replace('`', '', $dbase);
    	mysql_select_db($dbase) or die('Could not select database');
    
        $result = mysql_query($sql) or die('Query failed: ' . mysql_error() . ' / '. $sql);
        $resourceArray = array();
        for($i=0;$i<@mysql_num_rows($result);$i++)  {
    	  $par = mysql_fetch_assoc($result);
          array_push($resourceArray, $par);
        }
    	// Free resultset
    	mysql_free_result($result);
    	
    	// Closing connection
    	mysql_close($link);
    	
        return $resourceArray;
    
    }
    
    ?>
      Author: ManagerManager plugin - customise your ModX manager interface

      Rckt - web development, Sheffield, UK
      • 4018
      • 1,131 Posts
      I’ll give it a whirl and, if it all looks good, I will certainly consider adding it in a permanent addition (give you credit of course!). Will keep you posted! smiley

      Jeff
        Jeff Whitfield

        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
        • 24530
        • 100 Posts
        Hi ncrossland!
        I like this little it! It is a good start on changing the linklist ...
        [edit]I wantet to write: "linklist" and not "little it"! sorry!!!
          my newest webpage [url=http://gitarren.zucali.at]Meisterwerkstatt f
          • 6726
          • 7,075 Posts
          Wow great idea it will improve end user experience, I’ll make sure to try that out !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id
            • 6726
            • 7,075 Posts
            Just installed and tested it, works fine except it screws up accented character in french tongue

            You can solve this by switching from htmlspecialchars to htmlentities, line 23 :

            	$breadcrumbs[] = ($p['menutitle'])?htmlentities($p['menutitle'],ENT_QUOTES):htmlentities($p['pagetitle'],ENT_QUOTES);


            Then everything works fine for accented characters smiley

            Thanks for this, my clients will love it !
              .: COO - Commerce Guys - Community Driven Innovation :.


              MODx est l&#39;outil id
              • 24530
              • 100 Posts
              Now the script results in:

              Menu1
              Menu1 : Sub1
              Menu1 : Sub2
              Menu1 : Sub2 : Subsub1
              Menu2
              Menu2 : Sub1


              I tried to change it to:

              Menu1
              |-- Sub1
              |-- Sub2
              |--SubSub1
              Menu2
              |-- Sub1

              ... but the list gets sorted alpabetically afterwards ...
                my newest webpage [url=http://gitarren.zucali.at]Meisterwerkstatt f
                • 21056
                • 327 Posts
                I’ll have a look and see if I can get something similar to that output...
                  Author: ManagerManager plugin - customise your ModX manager interface

                  Rckt - web development, Sheffield, UK
                  • 19889
                  • 616 Posts
                  Quote from: ncrossland at Jul 13, 2007, 04:21 AM

                  I’ll have a look and see if I can get something similar to that output...

                  exactly what I was looking for - great stuff.

                  nevertheless, I had to do the following changes to make it work with german umlauts:

                  $charset= ’UTF-8’;
                  $breadcrumbs[] = ($p[’menutitle’])?htmlentities($p[’menutitle’],ENT_QUOTES, $charset):htmlentities($p[’pagetitle’],ENT_QUOTES, $charset);

                  is this correct or what’s the best way to do this - I guess it would be better if the $charset is not hardcoded but I don’t know how to do it.
                    • 21056
                    • 327 Posts
                    I’m not too familiar with the issues facing international character sets -- would we want to get the charset used by the manager, or by the site’s content??
                      Author: ManagerManager plugin - customise your ModX manager interface

                      Rckt - web development, Sheffield, UK
                      • 19889
                      • 616 Posts
                      well I think we should get the same info as it is here:

                      <meta http-equiv="Content-Type" content="text/html; charset=[(modx_charset)]" />
                      I guess that’s the value set in the site configuration: Character encoding:...., isn’t it?