by
KK » Tue Feb 25, 2014 8:48 pm
Thanks for the detailed description.
I think I get it now (hopefully

).
So the use-case goes like this -
a. A registered user visits a product page.
b. If the product is not in the list of products related to her, she sees a button saying 'Add this product'. Clicking the button adds the product to her list.
c. If the product is already in the list of products related to her, she sees a button saying 'Remove this product'. Clicking the button removes the product from her list.
Is that fine?
If so, I'll detail one possible solution below.
The solution revolves around the fact that we can set related-pages by specifying a comma-separated list of page ids. For example -
- Code: Select all
<cms:if k_success >
<cms:db_persist_form
user_products='324, 456, 709'
/>
</cms:if>
notice how in the code above we are providing a list of ids as values of the 'relation' field named 'user_products'.
Now, when a user is visiting a product page, the id of the product is well known (k_page_id).
If we were to set this id as follows
- Code: Select all
<cms:db_persist_form
user_products=k_page_id
/>
..it would work but it would remove *all other* previously related pages while making the current page the only related page.
That was the problem I had alluded to.
So, for our solution,
a. we need to first get a list of all pages (products) related to the user.
b. if the current page is already in the list, remove it from the list
c. if it is not in the list, add it to list
d. Now we can safely supply this list to cms:db_persist_form.
Following is a complete working code that does just this.
IMP:
1. You need to place this code in the page_view of products.php
2. the code assumes that 'my_user_id' contains the id of the logged-in user.- Code: Select all
<cms:if k_is_page>
<!-- save the id of the current product in a variable -->
<cms:set my_current_product=k_page_id 'global' />
<!-- get the list of ids of products related to this user -->
<cms:pages masterpage='users.php' id=my_user_id limit='1'>
<cms:set my_related_products="<cms:related_pages 'user_products' ids_only='1' />" 'global' />
</cms:pages>
<!-- use PHP to manipulate the list got above to include/exclude current page (i.e. product) -->
<cms:php>
global $CTX;
$my_current_product = $CTX->get( 'my_current_product' );
$my_related_products = $CTX->get( 'my_related_products' );
// is the current product (the page we are on) in the list of related products?
$arr_products = explode( ',', $my_related_products );
$key = array_search( $my_current_product, $arr_products );
if( $key !== false ){
// product already related.. remove it from list
unset( $arr_products[$key] );
$CTX->set( 'is_already_related', '1', 'global' );
}
else{
// not yet related.. add product to list
$arr_products[] = $my_current_product;
$CTX->set( 'is_already_related', '0', 'global' );
}
// set the revised list of ids in context
$my_related_products = trim( implode(',', $arr_products) );
$CTX->set( 'my_related_products', $my_related_products, 'global' );
</cms:php>
<!-- use databound form to persist the new list of related pages -->
<cms:form masterpage='users.php' mode='edit' page_id=my_user_id method='post' anchor='0'>
<cms:if k_success >
<cms:db_persist_form
user_products=my_related_products
/>
<cms:redirect k_page_link />
</cms:if>
<cms:if is_already_related >
<cms:input name="submit" type="submit" value="Remove product from my list" />
<cms:else />
<cms:input name="submit" type="submit" value="Add product to my list" />
</cms:if>
</cms:form>
</cms:if>
Please test out the code first as it is - modify it once you're sure everything is working fine.
Hope this helps.
Do let us know.
Thanks.