We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24865
    • 289 Posts
    I’m trying to build a redirect plugin that dynamicly fetches a similar resource (by TV) and which then picks out one good resource and redirects to it, without changing the URL. Thus, I’m using sendForward.
    For the ’main’ idea, think of it as a dynamic SymLink, that only replaced the ID. smiley

    if (!isset($modx->ucs) || $modx->ucs->reseller == null) {
    	return true;
    }
    
    if ($modx->resource->get('class_key') == 'modSymLink' && intval($modx->resource->get('content')) == 9) { 
    	$tvs = $modx->resource->getMany('TemplateVars');
    	
    	foreach($tvs as $tv) {
    		if ($tv->get('name') == 'ucsResourceType') {
    			$similar = $modx->getCollection('modTemplateVarResource', array(
    				'tmplvarid' => $tv->get('id'),
    				'value' => $tv->get('value'),
    				'contentid:!=' => $modx->resource->get('id')
    			));
    			
    			foreach($similar as $tv) {
    				$resource = $modx->getObject('modResource', $tv->get('contentid'));
    
    				if ($resource == null) {
    					unset($resource);
    					continue;
    				}
    				
    				$parameters = $modx->ucs->reseller->get('parameters');
    				
    				if ($resource->get('parent') == $parameters['resourcetree']) {
    					$modx->sendForward($resource->get('id'));
    					return true;
    				}
    			}
    			break;
    		}
    	}
    }


    Now, the script works a bit like a VHost, it fetches a specific ID as a parent resource from the parameters, which are loaded from the user that is linked to a specific domain host (test.ucs.local for example).

    The problem is however, that it only SOMETIMES works. There is something fishy going on here and I can’t seem to find what I’m doing wrong.

    Oh and the script fires OnLoadWebDocument.
      @MarkGHErnst

      Developer at Adwise Internetmarketing, the Netherlands.
      • 24865
      • 289 Posts
      Little bit more updated function...

      <?php
      
      if (!isset($modx->ucs) || $modx->ucs->reseller == null) {
      	return true;
      }
      
      if ($modx->resource->get('class_key') == 'modSymLink' && intval($modx->resource->get('content')) == 9) { 
      	$tvs = $modx->resource->getMany('TemplateVars');
      	
      	foreach($tvs as $tv) {
      		if ($tv->get('name') == 'ucsResourceType') {
      			$similar = $modx->getCollection('modTemplateVarResource', array(
      				'tmplvarid' => $tv->get('id'),
      				'value' => $tv->get('value'),
      				'contentid:!=' => $modx->resource->get('id')
      			));
      			
      			foreach($similar as $tv) {
      				$resource = $modx->getObject('modResource', $tv->get('contentid'));
      				
      				if ($resource == null) {
      					unset($resource);
      					continue;
      				}
      				
      				$parameters = $modx->ucs->reseller->get('parameters');
      				
      				if ($resource->get('parent') == $parameters['resourcetree']) {
      					$modx->sendForward($resource->get('id'), array(
      						'response_code' => 101
      					));
      					return true;
      				}
      			}
      			break;
      		}
      	}
      }


      Note that I changed the sendForward command to a response_code 101... that seemed to fix a couple of glitches...
        @MarkGHErnst

        Developer at Adwise Internetmarketing, the Netherlands.
        • 17422
        • 225 Posts
        My response could be bad, but work for me.

        i use htaccess for redirect to good ressource.

        ex URL :
        http://nomdedomaine.com/page/b/

        RewriteRule ^page/([a-zA-Z]+)/$ /annuaire.html?id=$1 [L]
        
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
        



        and in the ressource annuaire.html, i get the id and in the snippet and use sendForward

        I hope it will help

        Laurent
          • 24865
          • 289 Posts
          Thanks for your response, but that’s isn’t really what I need.

          When I go to a specific, symlinked resource like:

          domain.tld/folder/subfolder/resource.html
          This resource has the type modSymLink with a redirect to ID 9

          I want to dynamicly fetch, via my plugin
          domain.tld/resellers/resellername/resource.html
          This is a simply modDocument resource

          And via a custom component I redirect to the one, right resource (by fetching the chosen resource tree (which is in the DB).

          Here is the new code... which launched on OnWebPageInit

          $modx->resourceIdentifier = intval($modx->resourceIdentifier);
          $resource = $modx->getObject('modResource', $modx->resourceIdentifier);
          
          if ($resource->get('class_key') == 'modSymLink' && intval($resource->get('content')) == 9) { 
          	$tvs = $resource->getMany('TemplateVars');
          	$parameters = $modx->ucs->reseller->get('parameters');
          	
          	foreach($tvs as $tv) {
          		if ($tv->get('name') == 'ucsResourceType') {
          			$similar = $modx->getCollection('modTemplateVarResource', array(
          				'tmplvarid' => $tv->get('id'),
          				'value' => $tv->get('value'),
          				'contentid:!=' => $resource->get('id')
          			));
          			
          			foreach($similar as $tv) {
          				$resource = $modx->getObject('modResource', $tv->get('contentid'));
          				
          				if ($resource == null) {
          					unset($resource);
          					continue;
          				}
          				
          				if ($resource->get('parent') == $parameters['resourcetree']) {
          					/*$modx->sendForward($resource->get('id'), array(
          						'response_code' => 101
          					));*/
          					$modx->resourceIdentifier = $resource->get('id');
          					$modx->resource = $resource;
          					break;
          				}
          			}
          			break;
          		}
          	}
          }
            @MarkGHErnst

            Developer at Adwise Internetmarketing, the Netherlands.
            • 24865
            • 289 Posts
            Going along with the previous post, I broke it down into a more simple structure;

            $resource = $modx->getObject('modResource', $modx->resourceIdentifier);
            
            if ($resource->get('class_key') == 'modSymLink' && intval($resource->get('content')) == 5) {
            	$modx->resource =& $modx->getObject('modResource', 7);
            }


            My resource tree looks like:

            ...
            Catch me (5) (type: document)
            From here (6) (type: symlink, forward: 5)
            I really want this one (7) (type: document)
            ...

            When I call /form-here.html, I am actually getting a SymLink sendForward to "Catch Me (5)", where the OnWebPageInit eventually fires and picks it up. See, since I am calling the symlink with a redirect to ID 5, the Plugin IF statement picks it up and starts processing.
            Where I rewrite it, it works just fine. That’s the strange bit - it flippin’ works! Now why doesn’t it work cross-domain (test.ucs.local and anothertest.ucs.local)? Is it because I am logged in on one?
              @MarkGHErnst

              Developer at Adwise Internetmarketing, the Netherlands.
              • 24865
              • 289 Posts
              I think I got it fixed now... smiley

              $modx->ucs = $modx->getService('ucs', 'ucs', $modx->getOption('core_path', null, MODX_CORE_PATH).'components/ucs/model/ucs/', array( 
              	'context' => $modx->context->get('key')
              ));
              
              $forward = false;
              $resource = $modx->request->getResource('id', intval($modx->resourceIdentifier));
              
              if ($resource->get('class_key') == 'modSymLink' && intval($resource->get('content')) == 9) { 
              	$tvs = $resource->getMany('TemplateVars');
              	$parameters = $modx->ucs->reseller->get('parameters');
              	
              	foreach($tvs as $tv) {
              		if ($tv->get('name') == 'ucsResourceType') {
              			$similar = $modx->getCollection('modTemplateVarResource', array(
              				'tmplvarid' => $tv->get('id'),
              				'value' => $tv->get('value'),
              				'contentid:!=' => $resource->get('id')
              			));
              			
              			foreach($similar as $tv) {
              				$resource = $modx->request->getResource('id', $tv->get('contentid'));
              				
              				if ($resource == null) {
              					unset($resource);
              					continue;
              				}
              				
              				if ($resource->get('parent') == $parameters['resourcetree']) {
              					$resource->set('cacheable', false);
              					$modx->resourceIdentifier = $resource->get('id');
              					$modx->resource =& $resource;
              					$forward = true;
              					break;
              				}
              			}
              			
              			break;
              		}
              	}
              	
              	if ($forward == false) {
              		$modx->resourceIdentifier = 47;
              		$modx->resource =& $modx->request->getResource('id', 47);
              	}
              }


              I’m gonna start developing and see what happens. If I mess up, I’ll post it again.
                @MarkGHErnst

                Developer at Adwise Internetmarketing, the Netherlands.