What Do You Need In The Xf2 Xengallery Version? Vote for your choices here: http://www.sonnb.com/threads/what-do-you-need-in-the-xf2-xengallery-version.1539/

Render Model

Discussion in 'XenGallery' started by dknife, Feb 14, 2015.

  1. dknife

    dknife YakTribe Gaming Authorized

    So I'm trying to display some latest albums with the main album photo on the home page of my website. I have the XenForo integration working for other functions and now I'm trying to figure out the models for the XenGallery.

    I have the Model_Album which I get the latest 5 albums by album date. I then get the content id's in the Model_Content (and for some reason 'limit' doesn't seem to work). I was then looking at Model_ContentData which has the actual file info of the content but I'm not sure where to go from there to actually pick the thumbnail size and render it.
     
  2. sonnb

    sonnb Administrator Staff Member

    Can you describe the full steps of the integration?
     
  3. dknife

    dknife YakTribe Gaming Authorized

    I'm not sure whether that really matters, nor how to describe it. I'm just trying to use the existing XenForo Model structure to access the XenGallery functions so I can go about displaying latest albums or pictures on my website outside of XenForo.

    For example from tests:

    Code:
    $gallerymodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Album');
    $contentmodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Content');
    $photomodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_ContentData');
    $albums = $gallerymodel->getAlbums(array(), array('order' => 'album_date', 'orderDirection' => 'desc', 'limit' => 5, 'offset' => 0));
    
    foreach ($albums as $album) {
    
        $content = $contentmodel->getContentsByAlbumId(array('album_id' => $album['album_id']),array('order' => 'content_date', 'orderDirection' => 'desc', 'limit' => 1, 'offset' => 0));
        foreach ($content as $photo) {
            $data = $photomodel->getDataByDataId($photo['content_id']);
            print_r($data);
        }
    }
    This gives me the array of data from ContentData Model for the first photo from each of the latest 5 albums. I just don't know where to go from there to actually render them using the XenGallery models
     
  4. dknife

    dknife YakTribe Gaming Authorized

    Does what I'm asking just make no sense?
     
  5. sonnb

    sonnb Administrator Staff Member

    Sorry for delay reply. I was on traditional new year vacation in home town.

    Since you have $contents in each album, you just need to display, eg:
    PHP:
    echo '<img src="' $photo['contentUrl'] . '" title="' $photo['title'] . '" />';
    Not sure if it is what you want to archive.
     
  6. dknife

    dknife YakTribe Gaming Authorized

    Content of getContentsByAlbumId:

    Code:
    Array ( [content_id] => 981 [content_type] => photo [content_data_id] => 1202 [album_id] => 75 [user_id] => 8970 [username] => YakTribe [title] => Bounty Hunter [description] => yaktribe competition 5 entry [position] => 0 [comment_count] => 0 [view_count] => 10 [tags] => 1 [tag_users] => a:1:{i:101;a:11:{s:7:"user_id";i:11144;s:8:"username";s:8:"Crowmire";s:12:"content_type";s:5:"photo";s:10:"content_id";i:981;s:14:"tagger_user_id";i:8970;s:15:"tagger_username";s:8:"YakTribe";s:9:"tag_state";s:8:"accepted";s:5:"tag_x";i:0;s:5:"tag_y";i:0;s:8:"tag_date";i:1424532928;s:6:"tag_id";i:101;}} [likes] => 2 [like_users] => a:2:{i:0;a:2:{s:7:"user_id";i:11054;s:8:"username";s:9:"trollmeat";}i:1;a:2:{s:7:"user_id";i:6424;s:8:"username";s:8:"grimwork";}} [latest_comment_ids] => [content_state] => visible [collection_id] => 0 [content_location] => [content_date] => 1424532928 [content_updated_date] => 1424532928 [content_privacy] => a:2:{s:10:"allow_view";s:8:"everyone";s:13:"allow_comment";s:8:"everyone";} [content_streams] => a:0:{} ) 
    Contents of getDataByDataId for those content:

    Code:
    Array ( [content_data_id] => 981 [content_type] => photo [temp_hash] => [unassociated] => 0 [file_size] => 217180 [file_hash] => 2605b79ca8c058da0f9775a3ed2a5b1a [extension] => jpg [duration] => 0 [is_animated] => 0 [width] => 1579 [height] => 537 [upload_date] => 1421129983 [large_width] => 1579 [large_height] => 537 [medium_width] => 300 [medium_height] => 102 [small_width] => 120 [small_height] => 120 [bdattachmentstore_engine] => [bdattachmentstore_options] => )
    Neither contain any reference to a URL.

    The photos seem to be stored in data/photos/[s|m|l|o]/ with a filename that's a combination of id, timestamp and hash? I tried content_data_id, dates and file_hash but none of the data from the arrays match up.
     
  7. sonnb

    sonnb Administrator Staff Member

    Try to use:
    PHP:
    $contents $contentmodel->getContentsByAlbumId(array('album_id' => $album['album_id']),array('order' => 'content_date''orderDirection' => 'desc''limit' => 1'offset' => 0'join' =>
    sonnb_XenGallery_Model_Content::FETCH_DATA sonnb_XenGallery_Model_Content::FETCH_PHOTO
    ));
    $contents $contentmodel->prepareContents($contents);
        foreach (
    $contents as $photo) {
        
    //Now $photo should contains URL
        
    }
     
  8. dknife

    dknife YakTribe Gaming Authorized

    Your code was very close and gave me one of the missing pieces (prepareContents) but the $contents seemed to be missing the required upload_date, file_hash and extension. There's probably some logical join in the fetchOptions that I couldn't figure out so I ended up cycling through the contents and adding the required data by using the ContentData model.

    Code:
    $gallerymodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Album');
    $contentmodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Content');
    $photomodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_ContentData');
    $albums = $gallerymodel->getAlbums(array(), array('order' => 'album_date', 'orderDirection' => 'desc', 'limit' => 5, 'offset' => 0));
    foreach ($albums as $album) {
        $contents = $contentmodel->getContentsByAlbumId(array('album_id' => $album['album_id']),array('order' => 'content_date', 'orderDirection' => 'desc', 'limit' => 1, 'offset' => 0, 'join' =>
        sonnb_XenGallery_Model_Content::FETCH_DATA | sonnb_XenGallery_Model_Content::FETCH_PHOTO
        ));
    
        $fixed = array();
        foreach ($contents as $key => $content) {
            $data = $photomodel->getDataByDataId($content['content_data_id']);
            $content['upload_date'] = $data['upload_date'];
            $content['file_hash'] = $data['file_hash'];
            $content['extension'] = $data['extension'];
            $fixed[$key] = $content;
        }
        $photos = $contentmodel->prepareContents($fixed,array('join' => sonnb_XenGallery_Model_Content::FETCH_DATA | sonnb_XenGallery_Model_Content::FETCH_PHOTO));
    
        foreach ($photos as $photo) {
            print_r($photo);exit;
           
        }
    
    }
    And that gave me the array with everything I need for the photos.

    Code:
    Array ( [content_id] => 981 [content_type] => photo [content_data_id] => 1202 [album_id] => 75 [user_id] => 8970 [username] => YakTribe [title] => Bounty Hunter [description] => yaktribe competition 5 entry [position] => 0 [comment_count] => 0 [view_count] => 10 [tags] => 1 [tag_users] => a:1:{i:101;a:11:{s:7:"user_id";i:11144;s:8:"username";s:8:"Crowmire";s:12:"content_type";s:5:"photo";s:10:"content_id";i:981;s:14:"tagger_user_id";i:8970;s:15:"tagger_username";s:8:"YakTribe";s:9:"tag_state";s:8:"accepted";s:5:"tag_x";i:0;s:5:"tag_y";i:0;s:8:"tag_date";i:1424532928;s:6:"tag_id";i:101;}} [likes] => 2 [like_users] => a:2:{i:0;a:2:{s:7:"user_id";i:11054;s:8:"username";s:9:"trollmeat";}i:1;a:2:{s:7:"user_id";i:6424;s:8:"username";s:8:"grimwork";}} [latest_comment_ids] => [content_state] => visible [collection_id] => 0 [content_location] => [content_date] => 1424532928 [content_updated_date] => 1424532928 [content_privacy] => Array ( [allow_view] => everyone [allow_comment] => everyone ) [content_streams] => a:0:{} [upload_date] => 1424532723 [file_hash] => 0e0d1b88d2a7610edcc2e948c25c8c75 [extension] => jpg [allow_view_html] => XenForo_Phrase Object ( [_phraseName:protected] => sonnb_xengallery_privacy_photo_view_everyone [_params:protected] => Array ( ) [_insertParamsEscaped:protected] => 1 [_phraseNameOnInvalid:protected] => 1 ) [canInlineMod] => 1 [canView] => 1 [canComment] => 1 [canEdit] => 1 [canDelete] => 1 [canLike] => 1 [canWatch] => 1 [canReport] => 1 [canPromote] => 1 [canUnPromote] => [canChangeOwner] => 1 [isDeleted] => [isModerated] => [isIgnored] => [likeUsers] => Array ( [0] => Array ( [user_id] => 11054 [username] => trollmeat ) [1] => Array ( [user_id] => 6424 [username] => grimwork ) ) [tagUsers] => Array ( [101] => Array ( [user_id] => 11144 [username] => Crowmire [content_type] => photo [content_id] => 981 [tagger_user_id] => 8970 [tagger_username] => YakTribe [tag_state] => accepted [tag_x] => 0 [tag_y] => 0 [tag_date] => 1424532928 [tag_id] => 101 ) ) [contentStreams] => Array ( ) [thumbnailUrl] => data/photos/m/1/1202-1424532723-e79e0aa3c64c7998ac7fa2dd2cd540be.jpg [url] => gallery/photos/bounty-hunter.981/ [medium_height] => 300 [contentUrl] => data/photos/l/1/1202-1424532723-181d14345ebba5c03fb34e97f1cc0e3c.jpg [thumbnailSmall] => data/photos/s/1/1202-1424532723-f816209e0148b3e27c485a463e1f261a.jpg [originalUrl] => )
     
  9. dknife

    dknife YakTribe Gaming Authorized

    I also realized in my original code (which would work to construct my own URLs) I was sending the wrong content id to the contentdata model, which is why the timestamp and hash I was getting didn't match what I was requesting.
     
  10. sonnb

    sonnb Administrator Staff Member

    Why you need upload_date, file_hash, extension? You already have contentUrl, no need to use other information.
     
  11. dknife

    dknife YakTribe Gaming Authorized

    You can't get ContentUrl without doing the join, and you can't do the join without the upload_date, file_hash and extension. The join has to be done on the prepareContents as well, which you didn't have in your code. That doesn't work without those 3 pieces of information.
     
  12. sonnb

    sonnb Administrator Staff Member

    sonnb_XenGallery_Model_Content::FETCH_DATA this join already fetched all information you need.
     
  13. dknife

    dknife YakTribe Gaming Authorized

    Your join is on getContentsByAlbumId but it needs to be on prepareContents to get those fields. And you can't do that join in prepareContents unless you have those other 3 fields in the $contents.
     
  14. sonnb

    sonnb Administrator Staff Member

    Haha. You are right, it is my fault. Please try this:

    PHP:
    $fetchOptions 
    array(
    'order' => 'content_date''orderDirection' => 'desc''limit' => 1'offset' => 0'join' =>
        
    sonnb_XenGallery_Model_Content::FETCH_DATA sonnb_XenGallery_Model_Content::FETCH_PHOTO
        
    );
    $contents $contentmodel->getContentsByAlbumId($album['album_id'], $fetchOptions);
    $contents$contentmodel->prepareContents($contents$fetchOptions);
     
  15. dknife

    dknife YakTribe Gaming Authorized

    You still can't do that as you don't have those 3 fields in $contents after getContentsByAlbumId.

    prepareContents gives you these errors when using the join in the fetch options

    Notice: Undefined index: upload_date in xxx/community/library/sonnb/XenGallery/Model/ContentData.php on line 523

    Notice: Undefined index: file_hash in xxx/community/library/sonnb/XenGallery/Model/ContentData.php on line 524

    Notice: Undefined index: extension in xxx/community/library/sonnb/XenGallery/Model/ContentData.php on line 525

    And so on for each of the 4 photo sizes

    getContentsByAlbumId with those fetchOptions gives you this array

    Code:
    Array ( [995] => Array ( [content_id] => 995 [content_type] => photo [content_data_id] => 1226 [album_id] => 76 [user_id] => 12024 [username] => hreikin [title] => My Hive [description] => [position] => 0 [comment_count] => 0 [view_count] => 9 [tags] => 0 [tag_users] => a:0:{} [likes] => 0 [like_users] => a:0:{} [latest_comment_ids] => [content_state] => visible [collection_id] => 0 [content_location] => [content_date] => 1425066745 [content_updated_date] => 1425066745 [content_privacy] => a:2:{s:10:"allow_view";s:8:"everyone";s:13:"allow_comment";s:8:"everyone";} [content_streams] => a:0:{} ) [996] => Array ( [content_id] => 996 [content_type] => photo [content_data_id] => 1227 [album_id] => 76 [user_id] => 12024 [username] => hreikin [title] => Memories [description] => [position] => 1 [comment_count] => 0 [view_count] => 8 [tags] => 0 [tag_users] => a:0:{} [likes] => 0 [like_users] => a:0:{} [latest_comment_ids] => [content_state] => visible [collection_id] => 0 [content_location] => [content_date] => 1425066745 [content_updated_date] => 1425066745 [content_privacy] => a:2:{s:10:"allow_view";s:8:"everyone";s:13:"allow_comment";s:8:"everyone";} [content_streams] => a:0:{} )
     
  16. sonnb

    sonnb Administrator Staff Member

    I still wrong.

    PHP:
    $fetchOptions =
    array(
    'order' => 'content_date''orderDirection' => 'desc''limit' => 1'offset' => 0'join' =>
        
    sonnb_XenGallery_Model_Content::FETCH_DATA sonnb_XenGallery_Model_Content::FETCH_PHOTO
        
    );
    $contents $contentmodel->getContentsByAlbumId($album['album_id'],  array(), $fetchOptions);
    $contents$contentmodel->prepareContents($contents$fetchOptions);
    It should be like this. Sorry for wasting your time :D
     
  17. dknife

    dknife YakTribe Gaming Authorized

    aha! Perfect, thank you @sonnb :)
     
  18. Dave

    Dave Authorized Member Authorized

    Can you please share the complete code for this to work in an external page?
    Thanks
     
  19. dknife

    dknife YakTribe Gaming Authorized

    First I use the xfloader I got from the XenForo forums

    Code:
    <?php
    
    // XenForo Authentication Loader
    
    define('XF_ROOT', '/path/to/xfroot/'); // set this (absolute path)!
    define('TIMENOW', time());
    define('SESSION_BYPASS', true); // if true: logged in user info and sessions are not needed
    
    require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
    
    XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
    
    XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
    XenForo_Application::set('page_start_time', TIMENOW);
    XenForo_Application::disablePhpErrorHandler();
    XenForo_Application::setDebugMode(false);
    
    
    XenForo_Session::startPublicSession();
    
    $visitor = XenForo_Visitor::getInstance();
    $threadModel = XenForo_Model::create('XenForo_Model_Thread');
    if ($visitor->getUserId())
    {
        // User array
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $userinfo = $userModel->getFullUserById($visitor->getUserId());
      
    }
    
    
    restore_error_handler();
    restore_exception_handler();
    
    ?>
    Then I use some basic code to pull the info into arrays for whatever use I wish

    Code:
               
    <?php
                $gallerymodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Album');
                $contentmodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Content');
                $photomodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_ContentData');
    
                // in this usage i'm pulling 3 albums randomly
                $albums = $gallerymodel->getAlbums(array(), array('order' => 'random', 'limit' => 3, 'offset' => 0));
    
                foreach ($albums as $album) {
    
                        // do something with albums here, I'm going to display 2 photos
    
                        $albumurl = preg_replace('/[^a-zA-Z0-9\-\s]+/','',strtolower($album['title']));
                        $albumurl = str_replace(' ','-',$albumurl);
                        $photo = array();
    
                        $fetchOptions =
                        array('order' => 'content_date', 'orderDirection' => 'desc', 'limit' => 2, 'offset' => 0, 'join' =>
                            sonnb_XenGallery_Model_Content::FETCH_DATA | sonnb_XenGallery_Model_Content::FETCH_PHOTO
                            );
                        $contents = $contentmodel->getContentsByAlbumId($album['album_id'],  array(), $fetchOptions);
                        $contents= $contentmodel->prepareContents($contents, $fetchOptions);
    
                        foreach ($contents as $photo) {
     
                               // do something with the photo(s) here
                               // eg <img src="forumpath/<?= $photo['thumbnailUrl']; ?>" alt="Photo Album by <?= stripslashes($photo['username']); ?>">
    
                        }
    
                }
    ?>
    
     
    sonnb likes this.
  20. Dave

    Dave Authorized Member Authorized

    @dknife Thanks, but i have added the code above and it just gives a blank page.
    Yes i have added my forum path ;)

    I will take a closer look to see for myself if possible, what could be causing this.
     

Share This Page