Problems, need help? Have a tip or advice? Post it here.
23 posts Page 1 of 3
Okay i spoken with KK about a follow button on my website however i just cant get my head around it. I'm really struggling to get the concept correct and get things working.

Okay i'm currently creating a social side to my website where it allows content to be uploaded onto my website. Everything is going well via the databound forms and of course the new release of the members module. However when it comes to a more complex relation side i just cant make any headway.

What i'm looking to do.
Like on twitter a member can follow another member. Facebook has the pages ''like'' button. When you follow a user or like a page the content shows on your newsfeed. I want to create the same type of thing, follow. Allowing a member thats registered to follow another member. When you follow this member it'll show their content within the newsfeed page.

Users can post data onto our website with a databound form. The user can submit thease.

Cloneable page with a databound form
- status update.
- Article post.
- Product post.
- Photo upload.

Now to fetch and save the users submission to connect their profile i have set this within each of the databound forms above.

author=k_member_title
linkprofile=k_member_id

The members module has a page called ''index.php'' page. So that's where each user is created.

I'm unsure if to user the k_member_id within the code or what. where this code should be or where it shouldnt.

this is the code that KK pointed me towards.

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', 'global' );
        $my_related_products = $CTX->get( 'my_related_products', 'global' );
       
        // 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>
Is this the same requirement mentioned in viewtopic.php?f=3&t=8700 Simon?

From what I can tell, the code is meant to be placed in the page view of your members template (profile page). Perhaps the references to products is creating confusion...

Create an editable region in your members template:
Code: Select all
<cms:editable masterpage='members/index.php' name='following' type='relation' />

I edited the code to reflect your use case:
Code: Select all
<!-- save the id of the current user in a variable -->
<cms:set current_user_id=k_page_id 'global' />

<!-- get the list of ids of users followed by this user -->
<cms:pages masterpage='members/index.php' id=k_member_id limit='1'>
    <cms:set following="<cms:related_pages 'following' ids_only='1' />" 'global' />
</cms:pages>

<!-- use PHP to manipulate the list got above to include/exclude current page (i.e. user) -->
<cms:php>
    global $CTX;

    $current_user_id = $CTX->get( 'current_user_id', 'global' );
    $following = $CTX->get( 'following', 'global' );

    // is the current user (the page we are on) in the list of followed users?
    $arr_users = explode( ',', $following );
    $key = array_search( $current_user_id, $arr_users );
    if( $key !== false ){
        // user already following... remove it from list
        unset( $arr_users[$key] );
        $CTX->set( 'is_already_following', '1', 'global' );
    }
    else{
        // not yet following... add user to list
        $arr_users[] = $current_user_id;
        $CTX->set( 'is_already_following', '0', 'global' );
    }

    // set the revised list of ids in context
    $following = trim( implode(',', $arr_users) );
    $CTX->set( 'following', $following, 'global' );

</cms:php>

<!-- use databound form to persist the new list of followed users -->
<cms:form masterpage='members/index.php' mode='edit' page_id=k_member_id method='post' anchor='0'>
    <cms:if k_success >
        <cms:db_persist_form
            following=following
        />
        <cms:redirect k_page_link />
    </cms:if>

    <cms:if is_already_following >
        <cms:input name="submit" type="submit" value="Unfollow" />
    <cms:else />
        <cms:input name="submit" type="submit" value="Follow" />
    </cms:if>
</cms:form>
Hi cheesypoof, thank you so much for this. Its helped out massively to understand this function.

Now when i want to feed everything onto a newsfeed page. So i'm showing everything with this code on the main index.php page on the website outside the members part

Code: Select all
    <cms:query
    sql="  SELECT p.id pid, t.name tname
        FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
        inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
        on p.template_id = t.id
        WHERE (t.name='members/articles/index.php' or t.name='members/status/index.php' or t.name='members/images/index.php')
        AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
        AND NOT publish_date = '0000-00-00 00:00:00'
        ORDER BY publish_date desc;"
   
    >







   

<cms:pages  masterpage='members/articles/index.php' id=pid > 
<!------- Articles--------->
     <div id="news_main">
     <div id="news_posted"><div id="news_by_i"><img src="<cms:show profile />" /></div>
     <div id="news_by"><cms:show author /><br /><span class="style4">@<cms:show author /> </span></div>
     <div id="news_by_t"><abbr class="timeago" title="<cms:show k_page_date />"><cms:date k_page_date format='%d %B %Y, %r' locale='English' /></abbr></div><div id="news_by_t2"></div></div>
     <div id="news_main_img"> <img src="<cms:show s_img />"  /></div>
    <div id="news_title"><strong><cms:show art_title /></strong></div>
    <div id="news_text"><cms:excerpt count='190' truncate_chars='1'><cms:show art_information /></cms:excerpt></div>
     </div>
<!------- Articles--------->   
</cms:pages>

<cms:pages  masterpage='members/status/index.php' id=pid > 
<!------- Articles--------->
     <div id="news_main">
     <div id="news_posted"><div id="news_by_i"><img src="<cms:show profile />" /></div>
     <div id="news_by"><cms:show author /><br /><span class="style4">@<cms:show author /> </span></div>
     <div id="news_by_t"><abbr class="timeago" title="<cms:show k_page_date />"><cms:date k_page_date format='%d %B %Y, %r' locale='English' /></abbr></div><div id="news_by_t2"></div></div>
     <div id="news_main_img"> <img src="<cms:show s_img />"  /></div>
    <div id="news_text"><cms:show status_text /></div>
     </div>
<!------- Articles--------->   
</cms:pages>   
 
   
</cms:query>


However i want to limit things by only showing the content by the users you are following, and your own content you have submitted

Thank you again
Simon, do you have a specific requirement for these three types of clonable pages (article, status, and image) that members can create to be separated into different templates? I ask this because I think it will introduce additional complexity to your solution, as demonstrated by the need to use the cms:query tag. That isn't to say it isn't possible with multiple templates and cms:query, but crafting such an SQL query is beyond my abilities.

Let's say that we have a clonable template named 'members/posts.php' or 'members/posts/index.php' that has three folders - articles, status, and images. All clonable pages created by members would be from this template and be placed in the appropriate folder to reflect the post type. The posts template would also have an author relation field like so (which would be filled with k_member_id on form submission):
Code: Select all
<cms:editable has='one' label='Author' masterpage='members/index.php' name='author' required='1' type='relation'/>

The code to list the posts of the members you are following would look like:
Code: Select all
<cms:pages masterpage='members/index.php' id=k_member_id limit='1'>
    <cms:set following_ids="<cms:related_pages field='following' ids_only='1'/>" scope='global'/>
</cms:pages>

<cms:pages masterpage='members/posts/index.php' custom_field="author=id(<cms:show k_member_id/>,<cms:show following_ids/>)">
    <cms:if k_page_foldername == 'articles'>
        ...
    <cms:else/>
        <cms:if k_page_foldername == 'status'>
            ...
        <cms:else/>
            <!-- images -->
        </cms:if>
    </cms:if>
</cms:pages>

This utilizes relation-aware improvements to the cms:pages tag which were added in v1.4.5RC1.

Do let us know if this makes sense and works as intended.
Hey,

I'm currently running three different clone able pages.
That have a databound form to submit the content. So

Members/status/index.php
Members/images/index.php
Members/articles/index.php

They all are submitted by the databound form saving the users data via the author.
And the content submitted.

So you advise that I run one template members/post/index.php and seperate each post by using folders? If so when submitting the data via databound what would need to be added to place into the folders depending on if their submitting status, images, articles ect
Correct - one template.

You would specify an additional parameter for the db_persist_form tag:
Code: Select all
k_page_folder_id="<cms:folders masterpage='members/post/index.php' root='articles' depth='1'><cms:show k_folder_id/></cms:folders>"

Nevertheless, I think you should wait to see what @KK has to say about the various implementation options.
Thanks cheesyproof.

I'll wait to see if Kk has an idea. I don't really want to run just the one template that's all.
Ill pop up this one back up in-case someone has a resolution :lol:
@Simon,

I totally agree with @cheesypoof - it'd be so much simpler if we combine the three separate templates into one. Folders would do just fine to make a separation between the contents.

While posting using DBF, we just need to specify the 'k_page_folderid' parameter to set the proper folder (again, @cheesypoof has provided the code for it).

I suggest you refactor your design to follow this method.

Let us know if you need any help with it.
Thanks.
Thanks KK and cheesypoof. I will go with the single template for all social submissions. Can i ask one more question. Using cheesypoof's code will this show ''by posted time'' Like the Quary?

When using the code above, it keeps coming up with
ERROR! Closing tag "pages" has no matching opening tag (line: 164 char: 5742)

Code: Select all
<cms:pages masterpage='members/index.php' id=k_member_id limit='1'>
    <cms:set following_ids="<cms:related_pages field='following' ids_only='1'/>" scope='global'/>
</cms:pages>

<cms:pages masterpage='members/posts/index.php' custom_field="author=id(<cms:show k_member_id/>,<cms:show following_ids/>)">
    <cms:if k_page_foldername == 'articles'>
        ...
    <cms:else/>
        <cms:if k_page_foldername == 'status'>
            ...
        <cms:else/>
            <!-- images -->
        <cms:if>
    </cms:if>
</cms:pages>
23 posts Page 1 of 3