We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53484
    • 9 Posts
    Hey,

    I needed a script that would list all contexts (without the mgr) from which I could load particular settings and tho there were lot's topics on the forum 'like this' none of them fitted my purpose, this it what I came up with. The results can be used in e.g. the index.php to return all contexts and initialise the requested.

    <?php
    
    require_once 'config.core.php';
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    $modx = new modX();
    $modx->getService('error','error.modError', '', '');
    
    
    /* script starts here */
    
    $contextKeys = array();
    $query = $modx->newQuery('modContext', array('key:NOT IN' => array('mgr')));
    $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
    
    if ($query->prepare() && $query->stmt->execute()) {
        $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
    }
    
    foreach($contextKeys as $key=>$value)
    {
        $contextInit = $modx->getContext($value);
        $contextKey = $value;
    	$contextSettings = $contextInit->config;
    	
    	echo $contextKey . "</br>";
    	echo $contextSettings['site_url'] . '</br>';
    	echo $contextSettings['site_start'] . '</br><hr>';
    	
    }
    
    ?>


    Result will be something like:

    patbloomcom
    http://patbloom.com/
    154
    ----------------------------
    vamdsnl
    http://vamds.nl/
    21
    ----------------------------
    web
    http://vamds.com/
    1
    ----------------------------


    Still working on getting the 'context name' - suggestions are welcome wink

    Pat
      • 53484
      • 9 Posts
      Hey,

      I modified the script a little so it can be used in the root index.php to load all contexts dynamically and initialise the proper context or website:

      $contextKeys = array();
      $query = $modx->newQuery('modContext', array('key:NOT IN' => array('mgr')));
      $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
      
      if ($query->prepare() && $query->stmt->execute()) {
          $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
      }
      
      $serverHost = $_SERVER['HTTP_HOST'];
      
      foreach($contextKeys as $key=>$value)
      {
      	$contextInit = $modx->getContext($value);
      	$contextKey = $value;
      	$contextSettings = $contextInit->config;
      	$contextHost = $contextSettings['http_host'];
      	
      	if($serverHost === $contextHost) {
      		$modx->initialize($contextKey);
      	}
      
      }

      [ed. note: tezone last edited this post 6 years, 9 months ago.]