Important announcements from CouchCMS team
29 posts Page 2 of 3
A little update to get file uploaded from the front-end (securefile).
viewtopic.php?f=2&t=10417
Hi all,
we have now succeded in importing the .csv file. Now I wander how the updating process is working.
How can we update the database with a new updated csv file? If I import a new csv file with the same products (same product names and same fields), but updated, I get duplicate products.
Any idea?
Thanks
@Danno,
How can we update the database with a new updated csv file? If I import a new csv file with the same products (same product names and same fields), but updated, I get duplicate products
.
To prevent duplicate pages from being created, before actually creating a page we'll need to first check if the page does not already exist. The 'page_name' of a page is its unique ID so we can use it to do the check.

To help you, following is some example code. I'm continuing to use the 'cars.php' example used in the original post.
Code: Select all
<!-- database operation here -->

    <!-- set the following two variables to match your use-case -->
    <cms:set my_template_name = 'cars.php' />
    <cms:set my_page_title = "<cms:show _car_make/> <cms:show _car_model/>" />

    <!-- 1. convert the title into unique page_name -->
    <cms:php>
        global $CTX, $FUNCS;
        $name = $FUNCS->get_clean_url( "<cms:show my_page_title />" );
        $CTX->set( 'my_page_name', $name );
    </cms:php>

    <!-- 2. try to find a page by the name generated above -->
    <cms:set my_page_id = '' 'global' />
    <cms:pages masterpage=my_template_name page_name=my_page_name limit='1' show_future_entries='1'>
        <cms:set my_page_id=k_page_id  'global' />
    </cms:pages>

    <!-- 3. if 'my_page_id' is empty at this point, the page does not exist - so safe to create one now -->
    <cms:if my_page_id=''>

        <!-- CREATE HERE -->
        <cms:db_persist>
            ..
            ..
        </cms:db_persist>
    </cms:if>

Some notes on the code above -
It is meant to be added to the original code (just below the "<!-- database operation here -->" line there).
As I mentioned, we need the 'page_name' to search for duplicates. In the original code, we only have the 'page_title' (a combination of "<cms:show _car_make/> <cms:show _car_model/>") so the code above uses a tittle PHP to convert the 'title' to a unique 'name'.

Then we use <cms:pages> with the name to search for the page. If the page is *not* found, we continue with the original logic and use <cms:db_persist> to create a new page.

IMP:
To adapt the code above to your use case, please modify the top two lines and place values matching that of your use-case -
<!-- set the following two variables to match your use-case -->
<cms:set my_template_name = 'cars.php' />
<cms:set my_page_title = "<cms:show _car_make/> <cms:show _car_model/>" />

Hope it helps.
I'm sorry, I'm not really great with this. All the explanations are actually making it more difficult for me :?

Can I just get a straight up code example for how to bulk import cloned pages from the admin panel? To clarify, I need this as an option where the user uploads the import file (not a one-off import).

Thanks!
@BlueCaret, I concede this addon is a little advanced.
However, this is all that we have at the moment, I am afraid.

If you feel you won't be able to tailor it to suit your use-case, you may hire somebody to do that for you.
It is possible to produce data type relationships using this module?
Thanks :)
Jiwa wrote: It is possible to produce data type relationships using this module?
Thanks :)

Yes, db_persist tag supports it perfectly. It is written as relation_editable_name = related_page_id, for example, city = 121.
Thank you, Trendoman! It really helped :)
Hello KK, this addon is a very powerful weapon on the COUCH.
I understand version 2 opens up new possibilities - creating pages and folders for category.

How can we solve the following task: (creating the two folders if not found)

Each line in CSV file is a car modification. Each modification is classified into a folder (for car maker) and a subfolder (for car_model) like this:
Audi / Audi 100 (44, 44Q, C3) / Audi 100 (44, 44Q, C3) 1.8 65Kw

Here's the record in the CSV file:
car_make,car_model,car_modification,car_chassis,car_month_start,car_yers_start,car_month_end,car_yers_end,car_fuel,car_cylinders,car_valves,car_cubic,car_horsepower,kw,car_engine_code,code_drive
Audi;Audi 100 (44, 44Q, C3);Audi 100 (44, 44Q, C3) 1.8;седан;02;1986;07;1988;бензин;4;8;1781;88;65;SH;предно предаване

All the information is here,
- car_make - Couch must create (If it does not exist) folder in template with name Audi
- car_model - Couch must create (If it does not exist) sub folder (for folder Audi) with name Audi 100 (44, 44Q, C3)
- and finally - Couch must create (If it does not exist) page for this subfolder with name car_modification + car_yers_end

Thank you very much
Hi @orbital,

You are right, with v2.0, DataBound Forms, in addition to normal templates, can also work with 'folders' as well as 'users' and 'comments'. In fact, the admin-panel uses a single DBF for all of those entities ('couch/theme/_system/content_form.html' in case it interests you).

So, theoretically, we can now use a normal DBF (or <cms:db_persist> / <cms:db_persist_form> tags) on our front-end templates to work with folders, as per your use-case.

However, there is one little catch that needs to be taken care of before we do that.
The way things work in the admin-panel, the <cms:db_persist>/ <cms:db_persist_form> tags while working with folders assume that they (i.e. the folders) belong to the main template being edited i.e. if we are editing 'cars.php', any folder being added will go straight into cars.php.

In your case, you are likely to be using these tags from within the CSV importer template (e.g. import.php) but would want to create the folders in another template (e.g. cars.php) so we'll have to make a few adjustments (three to be precise) first.

1. Please place the following code in import.php (or whatever your importer template is named) just below the <cms:template> tag block -
Code: Select all
<cms:php>
    global $DB, $CTX, $PAGE, $ORIG_PAGE;
   
    $masterpage = 'cars.php'; // set this to the template that the folders belong to

    $rs = $DB->select( K_TBL_TEMPLATES, array('id'), "name='" . $DB->sanitize( $masterpage ). "'" );
    if( !count($rs) ){ die( "ERROR: \"".$masterpage."\" - masterpage does not exist" ); }

    $pg = new KWebpage( $rs[0]['id'] );
    if( $pg->error ){ die( "ERROR: " . $pg->err_msg ); }
   
    // save original page and replace it with the new one
    $ORIG_PAGE = $PAGE;
    $PAGE = $pg;
</cms:php>

In the code above, I am assuming your target template (where folders will be created) is 'cars.php' - if it is something different, please set the correct name here.

2. Just *before* the last line in the template i.e. <?php COUCH::invoke(); ?>, place the following -
Code: Select all
<cms:php>
    global $PAGE, $ORIG_PAGE;
   
    // restore original page
    $PAGE = $ORIG_PAGE;
</cms:php>


Ok, so now importer.php template will think it is cars.php.

We can now use <cms:db_persist> within it to work with folders but we'll run across one little issue - when the staggered importer template refreshes to.process the next batch of records, it is supposed to redirect to itself (refresh). And so it does - but since it now considers itself to be 'cars.php', it redirects to the cars template instead :)

The third and last change will rectify that.
3. Add the line highlighted below to the <cms:csv_reader> tag in your import template (I am assuming below it is 'import.php' - please use whatever is the correct name in your case).
<cms:csv_reader
file="<cms:show k_admin_path />addons/csv/cars.csv"
paginate='1'
limit='100'
prefix='_'
use_cache='0'
base_link="<cms:link 'import.php' />"
>

And that is it. We can now finally get going with creating the folders.

To make things a little easier for you, I have abstracted away all the Couch code that works with folders into a snippet named 'get_folder.html' attached below. Place extract it and place it within the 'snippets' folder of your Couch installation (by default this is 'couch/snippets' unless you have changed the location from couch/config.php).

Following is a sample usage of it -
Code: Select all
<cms:set folder_id = "<cms:embed 'get_folder.html' title='Audi Q3' ></cms:embed>" /> 

The code above will return back the ID of a folder titled 'Audi Q3'. If the folder does not exist, it will create one before returning its ID, so you can simply use it without worrying if the folder exists or not.

That should be sufficient for your requirements.
Just in case you need more help, following fuller example should be useful -
Code: Select all
<cms:set car_make = 'Audi' />
<cms:set car_model = 'Audi 100 (44, 44Q, C3)' />
<cms:set car_modification = 'Audi 100 (44, 44Q, C3) 1.8' />

<!-- get parent folder by title -->
<cms:set parent_folder_id = "<cms:embed 'get_folder.html' title=car_make ></cms:embed>" />

<!-- get child folder by title (notice parent_id) -->
<cms:set child_folder_id = "<cms:embed 'get_folder.html' title=car_model parent_id=parent_folder_id></cms:embed>" />

<!-- use the child_folder_id found above to create the actual page -->
<cms:db_persist
    _masterpage='cars.php'
    _mode='create'
   
    k_page_title=car_modification
    k_page_folder_id=child_folder_id
>   
    <cms:if k_error><strong style="color:red;">ERROR:</strong> <cms:show k_error/></cms:if>
</cms:db_persist>

Hope it helps.

Attachments

29 posts Page 2 of 3

Who is online

In total there are 3 users online :: 1 registered, 0 hidden and 2 guests
(based on users active over the past 5 minutes)

Users browsing this forum: Yahoo [Bot] and 2 guests