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

    I’m currently working on an SMF theme that needs info from the MODx API.

    Currently it uses the MODx API to get a ’header’ chunk which is basically the site name in a h1 tag, and it also gets a ’navigation’ chunk generated by Wayfinder - i.e. the main site navigation appears on the same webpage as the forums.

    Is there any existing documentation on doing this?

    I’ve looked around, but all of what I see appears to be either about getting SMF to believe it has a user logged in (straightforward enough c/o the SMF connector module - thanks Raymond!) or using the SSIs in MODx pages. I’m working at the other end - getting the SMF templating system to ask MODx for stuff.

    -- Tim.
      • 30023
      • 172 Posts
      Just as a follow up to the above, I have the following (slightly tested wink ) code.

      Curiously the SMF SSIs include the SMF template file index.template.php, with no apparent effect other than crashing; as such I test for an SSI running, and bail out if so. No adverse effects noted so far!

      Another oddity is that I need to check if this is the first include of index.template.php within the current browser session. Otherwise on first going to the forums, the system crashes (the include in my code of MODx’s index.php fails when it tries to include /manager/includes/config.inc.php as that for some reason fails.) Refreshing the browser page resulted in success, so I am basically doing that automatically within my PHP, but it seems a bit of a hack. The crash only occured on the very first visit i.e. logging out, then logging in as a different user worked fine within the same session.

      <?php
      /*
       *  MREW Simple Machines Forum Templating. 
       *
       * Customised from the default SMF theme by Tim Spencer May/June 2009
       */
      
      
      // MREW site config for linking to MODx ---------------------------
      
      define('MODX_SITE_DOMAIN', 'http://dev.mountain.rescue.org.uk');
      
      define('MODX_SITE_BASE_URL', '/modx/');
      
      define('MODX_MANAGER_PATH', '/var/www/mrew/modx/modx-0.9.6.3/manager');
      
      define('MODX_DOCUMENT_IDENTIFIER', 69); // The MODx document we are going to pretend to be (the docid of the weblink to the forums ???)
      
      define('MODX_DOCID_REDIRECT_GUEST', 48); // If defined, where to redirect users who are not logged in to SMF (guests). If guests are allowed, comment out this line.
      
      
      
      
      
      // MODx integration -----------------------------------------------
      
      global $modx, $database_type, $database_server, $database_user, $database_password, $database_connection_charset, $database_connection_method, $dbase, $table_prefix;
      
      if (SMF == 'SSI') return; // SSIs cause MODx config.inc.php to fail. Furthermore the call to here appears to have no effect. <<<< Why?
      
      // First call of this template also appears superfluous and also causes config.inc.php to fail
      if (!isset($_SESSION['MREW']))
      	{
      	$_SESSION['MREW'] = true;
      	header('Location: '.$_SERVER['PHP_SELF']); // <<<< A blatant hack???
      	}
      
      define('MODX_API_MODE', true); // Tells MODx index.php to not run $modx->executeParser
      
      define('MODX_SITE_URL', MODX_SITE_DOMAIN.MODX_SITE_BASE_URL); // Used by both this code and the MODx API
      
      // Run the MODx config and get site settings
      require(MODX_MANAGER_PATH.'/../index.php');
      $modx->getSettings();
      
      //  Set the docid - most sensibly to the MODx weblink to this forum
      $modx->documentIdentifier = MODX_DOCUMENT_IDENTIFIER;
      
      // Set the base URL for Wayfinder etc
      $modx->config['base_url'] = MODX_SITE_BASE_URL;
      
      // MODx db table names
      $modx_web_user_attributes = $modx->getFullTableName('web_user_attributes');
      $modx_web_users = $modx->getFullTableName('web_users');
      $modx_web_groups = $modx->getFullTableName('web_groups');
      $modx_webgroup_names = $modx->getFullTableName('webgroup_names');
      $modx_document_groups = $modx->getFullTableName('document_groups');
      $modx_webgroup_access = $modx->getFullTableName('webgroup_access');
      
      // Check that user is logged in to SMF
      if ($context['user']['is_logged'])
      	{
      	// and build up $_SESSION for user by the MODx API with webusers. 
      	// Note that fullname and email are gathered from the MODx db, not SMF
      	$modx->db->connect();
      	$result = mysql_query("SELECT {$modx_web_users}.id, {$modx_web_user_attributes}.fullname,  {$modx_web_user_attributes}.failedlogincount,  {$modx_web_user_attributes}.lastlogin,  {$modx_web_user_attributes}.logincount
      					FROM {$modx_web_users}, {$modx_web_user_attributes}
      					WHERE  {$modx_web_users}.id = {$modx_web_user_attributes}.internalKey
      					AND {$modx_web_users}.username = '{$context['user']['name']}'");
      	if ($result && $row = mysql_fetch_assoc($result))
      		{
      		$_SESSION['webShortname'] = $context['user']['name']; 
      		$_SESSION['webFullname'] = $row['fullname'];
      		$_SESSION['webEmail'] = $row['email']; 
      		$_SESSION['webValidated'] = 1; 
      		$_SESSION['webInternalKey'] = $row['id'];
      		$_SESSION['webUser'] = base64_encode($context['user']['name']); 
      		$_SESSION['webFailedlogins'] = $row['failedlogincount'];
      		$_SESSION['webLastlogin'] = $row['lastlogin'];
      		$_SESSION['webnrlogins'] = $row['logincount'];
      		// WARNING! $_SESSION['webValid'] not set!!!
      
      		// Get web groups // <<<< EFFECTIVELY UNTESTED AFAIK 25-05-2009
      		$_SESSION['webUserGroupNames'] = array();
      		$result = mysql_query("SELECT name FROM {$modx_web_users}, {$modx_web_groups}, {$modx_webgroup_names} WHERE {$modx_web_users}.id = {$modx_web_groups}.webuser AND {$modx_web_groups}.webgroup = {$modx_webgroup_names}.id AND {$modx_web_users}.id = {$_SESSION['webInternalKey']}");
      		if ($result) while ($row = mysql_fetch_assoc($result)) $_SESSION['webUserGroupNames'][] = $row['name'];
      	
      		// Get allowed document groups
      		$_SESSION['webDocgroups'] = array();
      		$result = mysql_query("SELECT documentgroup FROM {$modx_web_groups}, {$modx_webgroup_access} WHERE {$modx_web_groups}.webgroup = {$modx_webgroup_access}.webgroup AND {$modx_web_groups}.webuser = {$_SESSION['webInternalKey']}");
      		if ($result) while ($row = mysql_fetch_assoc($result)) $_SESSION['webDocgroups'][] = $row['documentgroup'];
      		}
      	}
      
      elseif (defined('MODX_DOCID_REDIRECT_GUEST'))
      	{
      	header('Location: '.MODX_SITE_URL); // This throws out non-logged in SMF users!
      	}
      
      [...rest of template here including calls such as ($modx->parseDocumentSource(str_replace('[!', '[[', str_replace('!]', ']]', $modx->getChunk('MREWnavMain'))))) ...]
      
      
      
      
        • 9130
        • 171 Posts
        Might be a bit late but take a look at the MODxAPI Library its much easier to use then coding direcly to the MODx API like you seem to be doing now. I using it to call MODx stuff from within a wordpress template.
          • 30023
          • 172 Posts

          Sounds useful - will take a look.

          There’s also the issue with this particular job that I need to make an SMF user look like a MODx user (or rather, by virtue of SMF connector, I’m making sure that a MODx user that is has been copied into an SMF user look again like that MODx user) so some bespoke code is needed anyway.

          thanks.
          Tim.