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

Answered Redirector help

    • 8168
    • 1,118 Posts
    Hi guys,

    I want to apply a snippet to a template which jumps the user up X levels in the page heirachy (so up 2 levels, so if they land on http://www.example.com/test/example/hello/this.html then they should be redirected to http://www.example.com/test/

    I'd like to be able to set the number of levels to jump up in the redirect call by TV if possible?

    Additionally, I'd like to be able to add a "#[[+id]]" element to the url the user is redirected to. (note if not possible to call the pageid - then it could be set in a TV as well)

    So back in the example, if the user lands on http://www.example.com/test/example/hello/this.html (with this.html page being page id 24), and the number of levels to jump up being set to 2 - they should be redirected to http://www.example.com/test/#24

    Is this possible? and if so how???

    Cheers


    dubbs.

    This question has been answered by dubbs. See the first response.

      • 3749
      • 24,544 Posts
      This is a kind of hacky way to do it, but I think it will work:


      $thisId = $modx->resource->get('id');
      $ids = array_reverse($modx->getParentIds($thisId));
      $ids[] = $thisId;
      
      $i = 1;
      $level = $this->resource->getTVValue('levelsUp');
      
      foreach ($ids as $id => $value) {
          $docIds[$i] = $value;
          if ($value == $docId) {
              $target = $docIds[$i - $level];
              if (!empty($target)) {
                  $url = $modx->makeUrl($docIds[$i - $level], "", "", "full");
                  if (!empty($url)) {
                      $modx->sendRedirect($url); 
                  }
                   
              }
          }
          $i++;
      }
      return '';


      I should mention that unless you put some kind of conditional statement in the snippet to prevent it from executing in some cases, no one will ever see the pages with the snippet tag again unless the TV is modified.
      [ed. note: BobRay last edited this post 9 years, 4 months ago.]
        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
        • 8168
        • 1,118 Posts
        Thanks Bob wink Have pasted that in as a snippet and called it from the page template of the child page that needs the redirection and created the levelsup TV but nothing happens when I view the child page - e.g. redirection does not function?

        Any ideas?
          • 8168
          • 1,118 Posts
          Quote from: BobRay at Dec 15, 2014, 02:44 AM
          This is a kind of hacky way to do it, but I think it will work:


          $thisId = $modx->resource->get('id');
          $ids = array_reverse($modx->getParentIds($thisId));
          $ids[] = $thisId;
          
          $i = 1;
          $level = $this->resource->getTVValue('levelsUp');
          
          foreach ($ids as $id => $value) {
              $docIds[$i] = $value;
              if ($value == $docId) {
                  $target = $docIds[$i - $level];
                  if (!empty($target)) {
                      $url = $modx->makeUrl($docIds[$i - $level], "", "", "full");
                      if (!empty($url)) {
                          $modx->sendRedirect($url); 
                      }
                       
                  }
              }
              $i++;
          }
          return '';


          I should mention that unless you put some kind of conditional statement in the snippet to prevent it from executing in some cases, no one will ever see the pages with the snippet tag again unless the TV is modified.

          bob - I can't get this to work - how should one call the snippet? passing the levelsUp TV with it?

          Note - I have called this snippet "redirectoParent"

          [[redirectoParent? &levelsUp=`[[*levelsUp]]`]]
          [ed. note: dubbs last edited this post 9 years, 4 months ago.]
            • 3749
            • 24,544 Posts
            No, the snippet gets the TV value directly from the TV so you don't need to include it as a property.

            I'm not sure what could be wrong. I did test the snippet and it worked for me, but I had to change a few things before posting it since I was testing it in my editor and had no $modx->resource.

            Try this and see what you get:

            $thisId = $modx->resource->get('id');
            $ids = array_reverse($modx->getParentIds($thisId));
            $ids[] = $thisId;
             
            $i = 1;
            $level = $this->resource->getTVValue('levelsUp');
            $output = '<p>In snippet id = ' . $thisId . '</p>'; 
            foreach ($ids as $id => $value) {
                $docIds[$i] = $value;
                if ($value == $docId) {
                    $target = $docIds[$i - $level];
                    if (!empty($target)) {
                        $url = $modx->makeUrl($target, "", "", "full");
                        if (!empty($url)) {
                            $output .= '<p>URL: ' . $url;
                            // $modx->sendRedirect($url);
                        } else {
                            $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                        }
                          
                    } else {
                            $output .= return '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                    }
                }
                $i++;
            }
            
            $output .= '<br><pre>' . print_r($ids, true) . '</pre>';
            return $output;
            

              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
              • 8168
              • 1,118 Posts
              Thanks Bob - afraid, I still get no response - e.g. nothing happens... Calling the snippet via

              [[!redirectoParent]] with the snippet titled "redirectoParent" with the code you have sent above, with the TV "levelsUp" set to 2....

              Odd that nothing is happening???

              Is there a way to debug with snippets? My understanding of PHP is very poor so I can't spot anything a miss...

              Looking in Firebug I am getting a server 500 error when using this code. seems something is not right in the middle element of the code - for example if i debug to return $thisId; like below the snippet fires and doesnt create a 500 error...

              $thisId = $modx->resource->get('id');
              $ids = array_reverse($modx->getParentIds($thisId));
              $ids[] = $thisId;
                
              
              $output .= '<br><pre>' . print_r($ids, true) . '</pre>';
              return $output;
              
              return $thisId;
              


              So, to me... It seems there is an issue in this part of the code? I have no idea what though!

              $i = 1;
              $level = $this->resource->getTVValue('levelsUp');
              $output = '<p>In snippet id = ' . $thisId . '</p>';
              foreach ($ids as $id => $value) {
                  $docIds[$i] = $value;
                  if ($value == $docId) {
                      $target = $docIds[$i - $level];
                      if (!empty($target)) {
                          $url = $modx->makeUrl($target, "", "", "full");
                          if (!empty($url)) {
                              $output .= '<p>URL: ' . $url;
                              // $modx->sendRedirect($url);
                          } else {
                              $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                          }
                             
                      } else {
                              $output .= return '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                      }
                  }
                  $i++;
              }
              
              [ed. note: dubbs last edited this post 9 years, 4 months ago.]
                • 8168
                • 1,118 Posts
                OK. Update... Worked out the PHP bugs and squashed them and now it's outputting the values we want... which is great... Bob - see code below for the version that is working. Could we now apply the redirect logic to make it all work?

                $thisId = $modx->resource->get('id');
                $ids = array_reverse($modx->getParentIds($thisId));
                $ids[] = $thisId;
                  
                $i = 1;
                // fixed here
                $level = $modx->resource->getTVValue('levelsUp');
                
                
                $output = '<p>In snippet id = ' . $thisId . '</p>';
                
                foreach ($ids as $id => $value) {
                    $docIds[$i] = $value;
                    if ($value == $docId) {
                        $target = $docIds[$i - $level];
                        if (!empty($target)) {
                            $url = $modx->makeUrl($target, "", "", "full");
                            if (!empty($url)) {
                                $output .= '<p>URL: ' . $url;
                                // $modx->sendRedirect($url);
                            } else {
                                $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                            }
                                
                        } else {
                // fixed here
                                $output .= '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                        }
                    }
                    $i++;
                }
                 
                $output .= '<br><pre>' . print_r($ids, true) . '</pre>';
                return $output;
                
                return $thisId;
                


                P.S. Values being outputed are:

                In snippet id = 67
                
                Target is empty
                Level is 2
                i is 1
                
                Array
                (
                    [0] => 0
                    [1] => 11
                    [2] => 12
                    [3] => 62
                    [4] => 67
                )
                


                So, the redirect function is not working, Why is Target empty? The array of ids seems to be right, level is right - something not working still? [ed. note: dubbs last edited this post 9 years, 4 months ago.]
                  • 3749
                  • 24,544 Posts
                  Change this:

                  if ($value == $docId) {


                  to this:
                  if ($value == $thisId) 
                  {

                  BTW, I think it can also be simplified to this:

                  $thisId = $modx->resource->get('id');
                  $level = $modx->resource->getTVValue('levelsUp');
                  $ids = array_reverse($modx->getParentIds($thisId));
                  $ids[] = $thisId;
                   
                  foreach ($ids as $key => $value) {
                      if ($value == $thisId) {
                          $target = $docIds[$key - $level];
                          if (!empty($target)) {
                              $url = $modx->makeUrl($target, "", "", "full");
                              if (!empty($url)) {
                                  $output .= '<p>URL: ' . $url;
                                  // $modx->sendRedirect($url);
                              } else {
                                  $output .= '<p>URL is empty<br>Level is ' . $level . 'target is ' . $target . '</p>';
                              }
                                   
                          } else {
                              $output .= '<p>Target is empty<br>Level is ' . $level . 'target is ' . $target . '</p>';
                          }
                      }
                  }
                    
                  $output .= '<br><pre>' . print_r($ids, true) . '</pre>';
                  return $output;
                   
                  return $thisId;
                    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
                    • 8168
                    • 1,118 Posts
                    Quote from: BobRay at Dec 16, 2014, 04:49 PM
                    Change this:

                    if ($value == $docId) {


                    to this:
                    if ($value == $thisId) 
                    {

                    BTW, I think it can also be simplified to this:

                    $thisId = $modx->resource->get('id');
                    $level = $modx->resource->getTVValue('levelsUp');
                    $ids = array_reverse($modx->getParentIds($thisId));
                    $ids[] = $thisId;
                     
                    foreach ($ids as $key => $value) {
                        if ($value == $thisId) {
                            $target = $docIds[$key - $level];
                            if (!empty($target)) {
                                $url = $modx->makeUrl($target, "", "", "full");
                                if (!empty($url)) {
                                    $output .= '<p>URL: ' . $url;
                                    // $modx->sendRedirect($url);
                                } else {
                                    $output .= '</p><p>URL is empty
                    Level is ' . $level . 'target is ' . $target . '</p>';
                                }
                                     
                            } else {
                                $output .= '<p>Target is empty
                    Level is ' . $level . 'target is ' . $target . '</p>';
                            }
                        }
                    }
                      
                    $output .= '
                    <pre>' . print_r($ids, true) . '</pre>';
                    return $output;
                     
                    return $thisId;

                    Bob - using the new code returns this:

                    Target is empty
                    Level is 2target is
                    
                    Array
                    (
                        [0] => 0
                        [1] => 11
                        [2] => 12
                        [3] => 62
                        [4] => 67
                    )
                    


                    So still not working to actually do any redirection... sorry!
                      • 8168
                      • 1,118 Posts
                      Target does not seem to be getting set...?