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

    I hope this is the right place to post my concern.

    I try to convert the Navigator snippet (http://modxcms.com/extras/package/?package=314) for Revo. I first tried to find all the place where I think I need to replace code. I then started translating the code, for example as follows:

    $doc = $this->modx->getPageInfo($id, 0, 'type,published,hidemenu,deleted' );


    was translated to

    $resource = $this->modx->getObject('modResource', $id);
    $doc = $resource->get('type,published,hidemenu,deleted');


    Is it correct this way?

    Now I’m stuck with more complex queries like in the following function:

      function GetTVArray()
      {
      // Gets an array of all template variables
        $table = $this->modx->getFullTableName('site_tmplvars');
        $tvs = $this->modx->db->select('name', $table);
        // TODO: make it so that it only pulls those that apply to the current template
        $dbfields = array();
        while ( $dbfield = $this->modx->db->getRow( $tvs ) )
        {
          $dbfields[] = $dbfield['name'];
        }
        return $dbfields;
      }


    If someone could help me with some advice about how to rewrite that for Revo, that would be great!

    Thanks!

    Daniel
      • 28215
      • 4,149 Posts
      Quote from: charger15 at Jul 21, 2010, 02:06 AM

      $doc = $this->modx->getPageInfo($id, 0, 'type,published,hidemenu,deleted' );

      $resource = $this->modx->getObject('modResource', $id);
      $doc = $resource->get('type,published,hidemenu,deleted');


      Almost, except change that 2nd line to:

      $doc = $resource->get(array('type','published','hidemenu','deleted'));


      And for the 2nd query, which looks like it’s just pulling all the TVs for a given template:

      /* assuming you have the ID of the template */
      $c = $modx->newQuery('modTemplateVar');
      $c->innerJoin('modTemplateVarTemplate','TemplateVarTemplates');
      $c->where(array(
          'TemplateVarTemplates.templateid' => $templateId,
      ));
      $c->sortby('TemplateVarTemplates.rank','ASC');
      $tvs = $modx->getObject('modTemplateVar',$c);
      
      $names = array();
      foreach ($tvs as $tv) {
         $names[] = $tv->get('name');
      }
      $names = array_unique($names);
      

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 4952
        • 13 Posts
        Thanks for your answer!

        I tried to convert the rest of the snippet but got the following error on line 596:

        Fatal error: Call to a member function newQuery() on a non-object in /public_html/assets/snippets/navigator/navigator.php on line 596

        On line 596 I replaced the EVO code

            $siblings = $this->modx->getAllChildren($parentId, 'menuindex', 'ASC', 'id');


        with this REVO code

            $c = $modx->newQuery('modResource');
            $c->where(array(
               'parent' => $parentId,
            ));
            $c->sortby('menuindex','ASC');
            $siblings = $modx->resource->getMany('Children',$c);



        Anyone seeing my mistake?

        I attached the snippet-code if anyone is interested in helping me. I’ve commented every change I made in the script.
          • 22851
          • 805 Posts
          Change [tt]$modx[/tt] to [tt]$this->modx[/tt] (or stick [tt]global $modx;[/tt] at the beginning of the method).

          EDIT: Corrected a mistake...
            YAMS: Yet Another Multilingual Solution for MODx
            YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
            Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
            • 4952
            • 13 Posts
            It did work with global $modx. Thank you for that! I now don’t get any php errors, but I also don’t get any value out of GetSiblingId().

            The code of the function:

              function GetSiblingId( $id )
              {
                global $modx;
                // Gets the next sibling.
                // Returns -1 if one doesn't exist
            
                $siblingId = -1;
            
                // If the document is the document root, then it has no siblings
                if ( $id == 0 )
                {
                  return -1;
                }
            
                // Get the parent document id
                $parentId = $this->GetParentId( $id );
            
                // Get the immediate siblings (and the current document)
                $c = $modx->newQuery('modResource');
                $c->where(array(
                   'parent' => $parentId,
                ));
                $c->sortby('menuindex','ASC');
                $siblings = $modx->resource->getMany(array('Children','id'),$c);
                echo "GetSiblingId: ";
                print_r($siblings);
            
                // Calculate the number of siblings
                $nSiblings = count( $siblings ) - 1;
            
                $currentIndex = -1;
                // Find the current document in the list of siblings
                foreach ( $siblings as  $index => $sibling )
                {
                  if ( $sibling['id'] == $id )
                  {
                    $currentIndex = $index;
                    break;
                  }
                }
                if ($currentIndex == -1)
                {
                  return $siblingId;
                }
            
                switch ( $this->rel )
                {
                  case 'prev':
                    if ( $currentIndex > 0 )
                    {
                      $siblingId = $siblings[$currentIndex-1]['id'];
                      return $siblingId;
                    }
                    break;
                  case 'next':
                    if ( $currentIndex < $nSiblings )
                    {
                      $siblingId = $siblings[$currentIndex+1]['id'];
                      return $siblingId;
                    }
                    break;
                  default:
                    return $siblingId;
                }
                return $siblingId;
              }


            The $siblings array inside GetSiblingId() prints out Array(), so no values, right? I then printed out the $parentId to check if it knows its parent but also didn’t get any value. The code of the related function GetParentId() looks like that:

              function GetParentId( $id )
              {
                // Gets the id of the parent.
                // If the document is the root, and so has no parent, -1 is returned
                if ( $id <= 0 ) {
                  return -1;
                }
                $resource = $this->modx->getObject('modResource', $id);
                $currentDoc = $resource->get('parent');
            
                return $currentDoc['parent'];
              }


            I first thought that return statement is wrong and changed it to return $currentDoc; but then got an Internal Server Error. huh


            If this is becoming a bit too much of a php beginners thread, just tell me! wink
              • 1841
              • 141 Posts
              Has anyone got any further with this snippet upgrade, This and a working migration tool is the only thing holding me back from the magnificence of Revolution...
                • 3749
                • 24,544 Posts
                Quote from: charger15 at Jul 22, 2010, 04:16 AM

                Thanks for your answer!

                I tried to convert the rest of the snippet but got the following error on line 596:

                Fatal error: Call to a member function newQuery() on a non-object in /public_html/assets/snippets/navigator/navigator.php on line 596

                On line 596 I replaced the EVO code

                    $siblings = $this->modx->getAllChildren($parentId, 'menuindex', 'ASC', 'id');


                with this REVO code

                    $c = $modx->newQuery('modResource');
                    $c->where(array(
                       'parent' => $parentId,
                    ));
                    $c->sortby('menuindex','ASC');
                    $siblings = $modx->resource->getMany('Children',$c);



                Anyone seeing my mistake?

                I attached the snippet-code if anyone is interested in helping me. I’ve commented every change I made in the script.

                If I’m understanding what you’re trying to do, you mean to ask for the siblings of the current resource with getMany(’Children’), but you’re asking for the current resource for its children instead, so the criteria make no sense.

                $modx->resource always points to the current resource, so you might want this:

                $parent = $modx->resource->getOne('Parent);
                $siblings = $parent->getMany('Children',$c);


                Then, all you’d need is the sortby criterion (no need to check the parent ID).
                Another way to go is to use your current criteria, but with:

                $modx->getCollection('modResource',$c); 
                  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
                  • 32822
                  • 18 Posts
                  Hi,

                  I’d be really interesting if you got this to work? I am relatively new to ModX so even though I just followed the thread and tried to debug it, I didn’t get it to work. Anyone?

                  Thanks
                    • 3749
                    • 24,544 Posts
                    Quote from: xdom at Sep 23, 2010, 03:14 PM

                    Hi,

                    I’d be really interesting if you got this to work? I am relatively new to ModX so even though I just followed the thread and tried to debug it, I didn’t get it to work. Anyone?

                    Thanks

                    xdom, can you post your snippet and let us know what output you want?

                    I’m going to guess that your problem is knowing what to do with the array returned from $parent->getMany(’Children’);

                    It returns an array of modResource objects (not IDs).
                      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
                      • 32822
                      • 18 Posts
                      Hi,

                      thanks for your reply - yes, indeed. I have gone through the code and debugged it for a while and got the massive array of modResource objects back. So have just followed the threat and made the suggested amendments. I just wondered if anyone has then taken it further to actually get the IDs out and make it work? Would have thought that’s a very useful snippet to allow cross-site in-page navigation.
                      Did you get it to work?

                      Thanks