Results 1 to 8 of 8

Thread: Adding an image in a news by code

  1. #1
    User
    Join Date
    11-24-13.
    Posts
    44

    Default Adding an image in a news by code

    Hi,
    I'm working on a script for importing wordpress posts to contao. I have the URL of the post image on wordpress (like http://www.domain.com/wp-content/upl...5/my-image.jpg), and I copied all /uploads/ file tree into /files/ in Contao.
    Now here is the request. Cycling on all wp posts I do an INSERT INTO tl_news. How can I set the image in this query? I see singleSRC but I don't understand what contains. Looking on other news in the db i can't find any path of images.
    I don't want to upload the images but using these already present in /files/uploads/.

    Thank you for your help!

    Davide

  2. #2
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Contao has a DB assisted file management system.

    After uploading the files synchronize your files in BE-File manager.

    Now you find your files in tl_files. Every file has a uuid.

    This uuid you have to put in field singleSRC in tl_news.

    In tl_news you find only datasets of a news main setting.

    Additional content elements of news you find in tl_content while filtering datasets by ptable="tl_news".
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  3. #3
    User
    Join Date
    11-24-13.
    Posts
    44

    Default

    Quote Originally Posted by Andreas View Post
    Contao has a DB assisted file management system.

    After uploading the files synchronize your files in BE-File manager.

    Now you find your files in tl_files. Every file has a uuid.

    This uuid you have to put in field singleSRC in tl_news.

    In tl_news you find only datasets of a news main setting.

    Additional content elements of news you find in tl_content while filtering datasets by ptable="tl_news".
    First of all thank you for your response.
    Since I don't upload any file, because I have copied /uploads/ from Wordpress to Contao /files/, I do that:

    PHP Code:
    \Dbafs::addResource($post_image_path); 
    And it works. But I have a doubt: what if I exec addResource() for the same image twice or more? Do I have to delete previous addResource? How?

    Thank you again

  4. #4
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Uploaded or copied the files to /files/ doesn't matter. You don't have to use Dbafs::addResource. Do the syncronization and your files are in tl_files. It's not possible to have duplicate files in one folder, the uuid of a file also considers the folder.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  5. #5
    User
    Join Date
    11-24-13.
    Posts
    44

    Default

    So the flow should be:

    1) copy /uploads/ under /files/uploads
    2) do System > File Manager > Synchronize
    3) run my import extension which it does something similar:

    PHP Code:
        $posts getPosts();

        foreach (
    $post in $posts
        {
            
    // Is this what you mean? 
            
    $objFile = \FilesModel::findByPath($post['image_path']);

            if (
    $objFile !== null)
            {
                
    $uuid $objFile->uuid;
            } 
            else 
            {
                
    $uuid '';
            }
            
    // end of this i think you mean :)

            
    $arrNews = array(
                
    // news data
                
    'singleSRC' => $uuid,        
            );

            
    // insert news
            
    $this->Database->prepare('INSERT INTO tl_news %s')->set($arrNews)->execute();
        } 
    Is this what you mean?

  6. #6
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Don't know what you are doing exactly. Just want to explain where you can find the pathes of your files (in tl_files) and that you have to write the uuid of a file into the singleSRC of tl_news or tl_content. But additionaly I think you have to generate a dataset in tl_content with ptable = tl_news as in tl_news you only find the teaser and other fields of a news main settings. The real content of a news you find in one or more datasets in tl_content.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  7. #7
    User
    Join Date
    11-24-13.
    Posts
    44

    Default

    Quote Originally Posted by Andreas View Post
    Don't know what you are doing exactly.
    I'm cycling an array of posts from wordpress and foreach one I create a record inside tl_news. For simplicity I don't use content elements but NewsSimple extension, that restores the old news behaviour.

    Quote Originally Posted by Andreas View Post
    Just want to explain where you can find the pathes of your files (in tl_files) and that you have to write the uuid of a file into the singleSRC of tl_news or tl_content.
    Ok this is what i do with

    PHP Code:
            // Is this what you mean?  
            
    $objFile = \FilesModel::findByPath($post['image_path']); 

            if (
    $objFile !== null
            { 
                
    $uuid $objFile->uuid
            }  
            else  
            { 
                
    $uuid ''
            } 
            
    // end of this i think you mean :) 
    Doing so seems it work, but I noted that on tl_news the fields size and imagemargin are empty. Are they mandatory? How can be populated?

  8. #8
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Ah, ok.

    You can ignore imagemargin as it's anyway a bad idea to have such a field in a CMS. Do styling with a css file.

    You need size (a serialized php array) when your original image has another size as you like to present it in FE. Contao then generates a thumb with the size you want in system/assets/images/ and shows this one in FE. If you set a value there (field size), Contao will do the rest for you if you once open the page with the image. Don't worry, Contao will not create the thumb if it already exists.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •