by
KK » Mon May 29, 2017 4:31 pm
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.