• Translating snippet from Evo to Revo: Am I doing it right?#

  • charger15 Reply #1, 1 year, 10 months ago

    Reply
    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


  • splittingred Reply #2, 1 year, 10 months ago

    Reply
    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);
    



  • charger15 Reply #3, 1 year, 10 months ago

    Reply
    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.


  • PaulSuckling Reply #4, 1 year, 10 months ago

    Reply
    Change $modx to $this->modx (or stick global $modx; at the beginning of the method).

    EDIT: Corrected a mistake...


  • charger15 Reply #5, 1 year, 10 months ago

    Reply
    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.

    If this is becoming a bit too much of a php beginners thread, just tell me!


  • antsplace Reply #6, 1 year, 9 months ago

    Reply
    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...


  • BobRay Reply #7, 1 year, 9 months ago

    Reply
    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); 


  • xdom Reply #8, 1 year, 7 months ago

    Reply
    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


  • BobRay Reply #9, 1 year, 7 months ago

    Reply
    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).


  • xdom Reply #10, 1 year, 7 months ago

    Reply
    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