How to get "Active Resources" outside of amember templates?

Discussion in 'Troubleshooting' started by superfactory, Feb 22, 2016.

  1. superfactory

    superfactory aMember Pro Customer

    Joined:
    Jan 17, 2009
    Messages:
    9
    We have created our own user dashboard that doesn't use the amember templates.

    We want to create our own version of "Active Resources", but cannot figure out how to get at the links.

    I have figured out how to pull active subscriptions, but not the associated URL.

    Can someone help?
  2. jenolan

    jenolan aMember Coder

    Joined:
    Nov 3, 2006
    Messages:
    493
    Have you tried something like Am_Di::getInstance()->resourceAccess->selectAllowedResources( $user_id );

    Then foreach the result as you desire
  3. superfactory

    superfactory aMember Pro Customer

    Joined:
    Jan 17, 2009
    Messages:
    9
    I get a script error when I try that.

    I've got a list of active products, but I can't get at the protected folder URL's of those products like active resources has.
  4. jenolan

    jenolan aMember Coder

    Joined:
    Nov 3, 2006
    Messages:
    493
    PHP:
    <?php
    $email 
    'jenolan@trexx.nampara';
    require_once 
    dirname(__FILE__) . '/amember/bootstrap.php';
    $user Am_Di::getInstance()->userTable->findFirstByEmail$email );
    $res Am_Di::getInstance()->resourceAccessTable->selectAllowedResources$user );
    print_rr$res );
    You then need to process each asset, you should have the user record already ;-)
  5. superfactory

    superfactory aMember Pro Customer

    Joined:
    Jan 17, 2009
    Messages:
    9
    Thank you. That gets me closer as I get resource id's.

    From there, what API call do I need to get resource links?
  6. jenolan

    jenolan aMember Coder

    Joined:
    Nov 3, 2006
    Messages:
    493
    Sigh I think you owe me a Kiva loan

    PHP:
    <?php
    define
    'REL_ROOT_URL''http://jlogica.dev/amember' );
    $email 'jenolan@trexx.nampara';
    require_once 
    dirname(__FILE__) . '/amember/bootstrap.php';
    $user Am_Di::getInstance()->userTable->findFirstByEmail$email );
    $res Am_Di::getInstance()->resourceAccessTable->selectAllowedResources$user );
    print_rr$res );
    $res Am_Di::getInstance()->resourceAccessTable
                
    ->getAllowedResources($userResourceAccess::USER_VISIBLE_TYPES);
    print_rr$res );
    echo 
    '<pre>';
    foreach( 
    $res AS $r )
    {
        if( 
    $l $r->renderLink() )
        {
            echo 
    htmlentities($l);
            if( @
    $r->desc )
            {
                echo 
    ' :: ';
                echo @
    $r->desc;
                echo 
    "\n";
            }
        }
    }
  7. superfactory

    superfactory aMember Pro Customer

    Joined:
    Jan 17, 2009
    Messages:
    9
    This is great thank you!

    Does anyone know if this info is supposed to be returned via: Am_Lite::getInstance()->getUserLinks()
  8. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    1,686
    To get this info via Am_Lite::getInstance()->getUserLinks() please add the following code to site.php ( http://www.amember.com/docs/Site.php_file ):
    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::AUTH_AFTER_LOGIN'mySaveLinks');
    Am_Di::getInstance()->hook->add('authSessionRefresh''mySaveLinks');

    function 
    mySaveLinks(Am_Event $e) {
        
    $resources $e->getDi()->resourceAccessTable->getAllowedResources($e->getDi()->userResourceAccess::USER_VISIBLE_TYPES);
        
    $links = array();
        foreach (
    $resources as $r)
        {
            
    $link $r->renderLink();
            if (
    $link)
                
    $links[] = $link;
        }
        
    $e->getDi()->session->amember_links $links;
    }

Share This Page