• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.
  • Chevereto Support CLST

    Support response

    Support checklist

    • ⚠️ Got a Something went wrong message? Read this guide and provide the actual error. Do not skip this.
    • ✅ Confirm that the server meets the System Requirements
    • 🔥 Check for any available Hotfix - your issue could be already reported/fixed
    • 📚 Read documentation - It will be required to Debug and understand Errors for a faster support response

Album data extraction via username

j2k

Chevereto Member
How do I extrac a list of albums (with titles) from a user with a list of image URLs?

I need to use it for 3rd party script integration.

Other than going through direct MySQL queries, is there an easier approach using existing Chevereto libraries and classes?

Thanks.
 
You can use the built in CHV\DB, CHV\Albums or even CHV\List classes to achieve that.
 
I'm currently having the website privacy set in private mode allowing only registered users.

I'm building a custom route to extract album/imgage information via crontab wget execution.

However, the custom route was not executed and redirected to the login page.

To solve the problem, I modified the loader.php "website privacy mode" section to bypass the login check, and it works:
PHP:
// Website privacy mode
if(getSetting('website_privacy_mode') == 'private' and !Login::getUser()) {
        $allowed_requests = ['<name-of-custom-route>','api', 'login', 'logout', 'image', 'album', 'page', 'account', 'connect'];
        if(getSetting('enable_signups')) {
                $allowed_requests[] = 'signup';
        }
        if(!in_array($handler->request_array[0], $allowed_requests)) {
                G\redirect('login');
        }
}

I don't want to make modification of the core code loader.php so that I'll have to do custom modification every time I upgrade Chevereto.

While keeping the website private, is there another more elegant way to implement the custom route execution in private mode and bypass login authentication?
 
You only need to create a custom route https://chevereto.com/docs/routes and in this new custom route generate lets say a json output with all the data you want to get. You can use all the built in methods in all the system, no need to hack anything or use a parser.

The most complete way to get everything will be using class.listing.php here is an example:

app/routes/overrides/route.albums.php
PHP:
<?php
$route = function($handler) {
    try {
        $list = new CHV\Listing;
        $list->setType('albums');
        $list->setWhere('WHERE album_user_id = :user_id'); // Get albums that belong to the user_id = X
        $list->bind(':user_id', 1); // Bind to user_id = 1
        $list->setLimit(100); // how many results?
        $list->setSortType('date'); // date | size | views
        $list->setSortOrder('desc'); // asc | desc
        $list->exec();
        G\Render\json_output($list->output);
    } catch(Exception $e) {
        G\exception_to_error($e);
    }
};

So you only need to put something to avoid scrapping like ?key=<something> so if $_GET['key'] doesn't match you simply do a return $handler->issue404(). You can get creative working with that... Key element is that while you use routes you get all the stuff that you need to work with.

In addition to this, you can use the core DB get (CHV\DB::get) and inject your own stuff. You call use all the resulted handlers, fetchers, etc.

Hope it helps.
 
Thanks for the info Rodolfo, it does help me understand a little more about how things work.

Currently I'm modifying my requirement so that I don't care about which user the albums belong to.

I'm doing album data extraction on a category basis.

I'm not familiar with how to work with json, but I'm trying to figure it out for my data export once I got the query output to work.

Still, I'm confused about:

1. What does the : in front of a variable mean?
2. What does $list->bind do?

After hours of studying Chevereto and G\, I managed to modify and come up with the following section of code for my new custom route.

I find that $list->output_assoc stores that image urls from the query result I need in an album.

I'm structuring it so that every album belongs to one category.

Basically I'm trying to get all the image urls for each album (and its title).

Since album doesn't have category ID in its structure, I need to do some queries.

I'm looping through categories so that I can figure out which album belong to which category.

So far it pretty much work as I needed, where gives me the album title and image urls:
PHP:
var_dump($safe_html_album['name']);

foreach ($album_images as $album_image_url) {
    var_dump($album_image_url['image']['url']);
}

However, there's an issue I couldn't resolve.

If I logged in as admin, and execute the route, everything works.

If it's not logged in, I only get it to work for the first loop iteration of the category.

Further loops of the category iterations can only gives me the album title, and $list seems not being executed.

Are you able to help me out with what might be wrong?

Here's the complete code of my custom route:
PHP:
<?php
$route = function($handler) {
    try {

        // Get categories
        $categories = CHV\DB::queryFetchAll("SELECT category_id, category_name, category_url_key FROM chv_categories");

        // Get all images in each category
        foreach ($categories as $category) {

            // Query fetch for all albums in each category
            $cat_id = $category['category_id'];
            $album_ids_per_cat = CHV\DB::queryFetchAll("SELECT DISTINCT(image_album_id) FROM chv_images WHERE image_category_id=$cat_id");
            var_dump($album_ids_per_cat);

            foreach ($album_ids_per_cat as $album_id) {
                $album_current = CHV\Album::getSingle($album_id['image_album_id']);
                $safe_html_album = G\safe_html($album_current);

                // List of images in album
                $list_params = CHV\Listing::getParams(); // Use CHV magic params

                $type = 'images';
                $where = 'WHERE image_album_id=:image_album_id';
                 
                $list = new CHV\Listing;
                $list->setType($type); // images | users | albums
                $list->setOffset($list_params['offset']);
                $list->setLimit('300');
                $list->setItemsPerPage($list_params['items_per_page']); // must
                $list->setSortType($list_params['sort'][0]); // date | size | views
                $list->setSortOrder('asc'); //asc sort order
                $list->setOwner($album_current["user"]["id"]);
                $list->setRequester(CHV\Login::getUser());
                $list->setWhere($where);
                $list->setPrivacy($album_current["privacy"]);
                $list->bind(":image_album_id", $album_current["id"]);
                $list->output_tpl = 'album/image';
                $list->exec();

                $album_images = $list->output_assoc;

                var_dump($safe_html_album['name']);

                foreach ($album_images as $album_image_url) {
                    var_dump($album_image_url['image']['url']);
                }

            } /* foreach $album_ids_per_cat END */
        } /* foreach $categories END */

    } catch(Exception $e) {
        G\exception_to_error($e);
    }
};
?>
 
1. What does the : in front of a variable mean?
All Chevereto DB methods use PDO so in this case ":" is used to bind parameters.

2. What does $list->bind do?
Is a handler for the default PDO bindling handler.

I'm not familiar with how to work with json, but I'm trying to figure it out for my data export once I got the query output to work.
JSON is an standard way used to transmit data. PHP can understand it using json_decode().

I'm looping through categories so that I can figure out which album belong to which category.
An album could have multiple images that belong to different categories. You shouldn't assign an album to any category.

Are you able to help me out with what might be wrong?
You are using things that you don't need. For example, you don't need to set the requester, you don't need to issue a query to get the available categories (look for app/loader.php), you don't need to set the privacy, etc. Everytime you remove things that you don't need then you will always have less trouble.

By the way, your code is doing multiple queries in a loop. Avoid that, it is a very expensive and discouraged practice.

Can you tell me which is the new requirement? Maybe I can help you if I properly understand what you want to achieve.
 
Last edited:
Hi Rodolfo,

Thanks for the detailed explanation.

I'm using Chevereto as an internal content management system or a custom project.

I understand an album can contain image from different categories, but for my specific project requirement, without a tag system in place, that's the best workaround I can come up with.

Simply put, the end result I want to achieve and my requirement is:

1. Using cronjob to execute the script in a 30 mins interval
2. Generate folder names using category url key
3. In each folder containing plain text file representing each album, in format of , <album_id>.txt
4. The format/structure of the plain txt file is like:
- first line is album title
- image urls in each new line (sorted by date asc. order)

That's it.

The other system I'm using is ready to accept input file in such a format, and I don't want to rewrite the whole system.

It's pretty similar to the idea of sitemap. I'm combining the sitemap mod with the default route.album.php to come up with a solution.

I understand my code was poorly written with expensive queries.

Are you able to help me get things done in the most efficient way?

Thanks.
 
Thanks Rodolfo.

I managed to rewrite the whole thing using CHV\DB only, and everything now works as expected.

Even though probably not the most efficient way, but it gets the job done.

I just now need to figure out how to export the result array into the files.

I must said the Chevereto script is really powerful and flexible.
 
Back
Top