We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19889
    • 616 Posts
    Hello
    I hope somebody could help me on the following - this is what I basically like to come up with:
    array( 
                array('title'=>'Home', 'URL'=>'/'), 
                array('title'=>'About Us','URL'=>'/about/', 'children'=>array( 
                    array('title'=>'Company History','URL'=>'/about/history.php'), 
                    array('title'=>'Franchise Information','URL'=>'/about/franchise.php'), 
                    array('title'=>'Shareholders Information','URL'=>'/about/shareholders.php'), 
                )), 
                array('title'=>'Services', 'URL'=>'/services/'), 
                array('title'=>'Contact Us', 'URL'=>'/contact/'), 
            ); 
    


    What I was wondering is whether the MODx Api could be used in an external php file and how - I guess this would be the easiest way to get all documents in the site.

    As an alternative, I guess I could use tinymce.linklist.php as a basis - however, I don’t see how I could get the required format above from that script.

    Hope somebody can help.
    Thank you in advance for your input.
      • 3749
      • 24,544 Posts
      It is possible to have the MODx API available in an external file, but is there some reason you can’t just put the code in a snippet? The API is automatically available in a snippet with no extra work.
        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
        • 19889
        • 616 Posts
        I need to get the list externally in order to feed a WYSIWYG editor with the links to the documents
          • 3749
          • 24,544 Posts
          I know how to do it in MODx Revolution, but it’s been quite a while for Evolution.

          Try this (assuming your php file is in the MODx root):

          include_once(dirname(__FILE__).'/manager/includes/config.inc.php');
          include_once(MODX_MANAGER_PATH.'/includes/document.parser.class.inc.php');
          $modx = new DocumentParser;

            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
            • 19889
            • 616 Posts
            Thank you for your help so far - hope somebody can help me to optimize the following code - based on my first tests, most of it is working but I’m sure the code can be simplified and made work so it would also list not only first level folders - I’m just not very good at those iterations.

            there is one error left and I’ve no idea how to fix it: Notice: Undefined variable: charset in /modxEvo/manager/includes/extenders/dbapi.mysql.class.inc.php on line 86

            <?php 
            if (!defined('IN_WPRO')) exit; 
            
            include_once($modx->config['base_path'].'manager/includes/document.parser.class.inc.php');
            
            class wproPlugin_modxLinks { 
                
                function onBeforeGetLinks(&$editor) { 
                    // Add some links that you can choose from when inserting a hyperlink: 
                    /*$editor->links = array( 
                        array('title'=>'Home', 'URL'=>'/'), 
                        array('title'=>'About Us','URL'=>'/about/', 'children'=>array( 
                            array('title'=>'Company History','URL'=>'/about/history.php'), 
                            array('title'=>'Franchise Information','URL'=>'/about/franchise.php'), 
                            array('title'=>'Shareholders Information','URL'=>'/about/shareholders.php'), 
                        )), 
                        array('title'=>'Services', 'URL'=>'/services/'), 
                        array('title'=>'Contact Us', 'URL'=>'/contact/'), 
                    );*/ 
                    
                    $editor->links = getSiteMap(0);
                } 
                
            }
            
            
            
            $modx = new DocumentParser;
            $site_root = 0;
            
            $pages = array();
            $i = 0;
            function getSiteMap($parentId) {
            	global $modx;
            	global $pages;
            	global $i;
            	$items = $modx->getAllChildren($parentId,'menuindex ASC, pagetitle','ASC','id,isfolder,pagetitle,description,parent,alias,longtitle,published,deleted,hidemenu');
            	
            	foreach($items as $item) {
            		if ( $item['isfolder'] == 0 ) {
            			$pages[$i] = array(
            				'title'		=> "'" . $item['pagetitle']."'",
            				'URL' 		=> $modx->makeUrl($item['id']));
            		} else {
            			$pages[$i] = array(
            				'title' 	=> "'" . $item['pagetitle']."'",
            				'URL' 		=> $modx->makeUrl($item['id']));
            				$children = $modx->getAllChildren($item['id'],'menuindex ASC, pagetitle','ASC','id,isfolder,pagetitle,description,parent,alias,longtitle,published,deleted,hidemenu');
            					
            				foreach($children as $child) {
            					$pages[$i]['children'][] = array(
            						'title'		=> "'" . $child['pagetitle']."'",
            						'URL' 		=> $modx->makeUrl($child['id']));
            
            				}
            		}
            		$i++;
            		
            	}
            	
            	return $pages;
            
            };
             
            ?>


            Thank you in advance for your help
              • 3749
              • 24,544 Posts
              Try adding this at the top. That should set the charset and the other DB info.

              require_once("manager/includes/config.inc.php");
                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
                • 19889
                • 616 Posts
                Thanks for your input - I certainly give it a try. Since I was able to refer to the config setting when loading
                include_once($modx->config['base_path'].'manager/includes/document.parser.class.inc.php');
                I though the config file was already included somewhere earlier in the WP 3 plugin?!

                What I’m really struggling with however is the recursive function to create that multi-dimensional array to get the document tree - any ideas how this could be done?

                Thanks again and sorry for being such a pain.

                Edit: tried to add the config file but doesn’t make any difference as to the charset error: Notice: Undefined variable: charset in /manager/includes/extenders/dbapi.mysql.class.inc.php on line 86
                  • 4172
                  • 5,888 Posts
                  for generating your recursiv link-array you can try something like this:

                  <?php
                  $start = array (array ('id'=>0));
                  $map = getSiteMap($start);
                  //print_r($map);
                  
                  function getSiteMap($items)
                  {
                      global $modx;
                      $pages = array ();
                      foreach ($items as $item)
                      {
                          $page = array ();
                          $page['title'] = $item['pagetitle'];
                          $page['URL'] = $modx->makeUrl($item['id']);
                          $children = $modx->getAllChildren($item['id'], 'menuindex ASC, pagetitle', 'ASC', 'id,isfolder,pagetitle,description,parent,alias,longtitle,published,deleted,hidemenu');
                          if (count($children) > 0)
                          {
                              $children = getSiteMap($children);
                              $page['children'] = $children;
                          }
                          $pages[] = $page;
                      }
                  
                      return $pages;
                  }
                  ?>
                  

                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 19889
                    • 616 Posts
                    thank you so much - I’ll give it a try and let you know how it goes
                      • 19889
                      • 616 Posts
                      Finally - with some help of you guys, I got the tree working - here is the final code:
                      <?php
                       
                      	if (!defined('IN_WPRO')) exit;
                      	 
                      	include_once($modx->config['base_path'].'manager/includes/document.parser.class.inc.php');
                      	$modx 	= new DocumentParser;
                      	$fields = 'id,pagetitle,longtitle,description,parent,alias,isfolder,published,deleted,hidemenu';
                      	
                      	class wproPlugin_modxLinks {     
                      	    function onBeforeGetLinks(&$editor) {
                      	    		global $modx; 
                      	    		$start = array(
                      	    			array (
                      	    				'id' 		=> 0,
                      	    				'pagetitle' => $modx->config['site_name']
                      	    			)
                      	    		);
                      				$editor->links = getMODxLinks( $start );
                      	    } 
                      	}
                      	
                      	function getMODxLinks($items) {
                      	    global $modx, $fields;
                      	    $pages = array ();
                      	    
                      	    foreach ($items as $item) {
                      	        $page = array();
                      	        $page['title'] 	= $item['pagetitle'];
                      	        $page['URL'] = ($item['id'] > 0) ? $modx->makeUrl($item['id']) : '';
                      	        
                      	        $children = $modx->getAllChildren($item['id'], 'menuindex ASC, pagetitle ASC', $fields);
                      	        
                      	        if (count($children) > 0) {
                      	            $children = getMODxLinks($children);
                      	            $page['children'] = $children;
                      	        }
                      	        
                      	        $pages[] = $page;
                      	    }
                      	
                      	    return $pages;
                      	};
                      
                      ?>