by
KK » Fri Mar 04, 2016 2:32 am
Hi Subhamoy,
Apologies for the delay in posting the solution.
Coming to which -
One solution to this problem could be to simply define *all* editable regions within the members template itself. So although many of the fields won't be applicable to 'employers', nevertheless those would be present in the template.
While offering the fields for editing through the databound form, we can test if it is the 'employer' account or the 'employee' and then show the extra fields only if it is the employee editing her record.
One potential problem with this approach could be if any of those extra fields is 'required' - this will crop up when the 'employer' is editing her record. Since we would have hidden the extra fields here, the required field will remain empty and the save operation will fail.
Although the problem can be solved using a workaround mentioned here -
viewtopic.php?f=4&t=9118, let us continue the tack you have taken i.e. use a separate template to hold data only for the employees.
We'll begin with a clonable template (say, named 'extra.php').
Define all the extra regions you'd want for the employees in this template.
For each employee page in the members template, we'll want to have a counterpart page in this new template.
This is a classical 'one-to-one' relation between pages.
Although we can use 'relation' feature of Couch to establish this relationship between the two templates (members and extra), we can actually get by without that additional hassle.
Since a page's name (k_page_name) is always a unique identifier, if we could ensure that for a page in member's template, we have a page with the *same name* in the extra template that would be sufficient to establish the relationship.
So, for example, suppose a member named 'john_doe' is logged in and is editing his profile.
We can check if a page named 'john_doe' is present in the extra template. If it is, we can offer that page for editing as DataBound Form.
What happens if no such page is present? (this will happen the first time a member edits her profile).
In that case, we'll set the 'mode' parameter of the Datbound Form to 'create'.
This will create a new page when the form is submitted. We'll just make sure that the name (and, although not essential, the title of the new page matches that of the logged-in member).
The following code does just that.
Add this form to the profile page (so we'll now have two forms on the profile page - the original will edit fields from the members template and the new form will edit those of the extra template).
- Code: Select all
<!-- find if the member has an associated page in the template for extra info -->
<cms:set my_form_mode = 'create' 'global' />
<cms:pages masterpage='extra.php' page_name=k_member_name limit='1' show_future_entries='1' >
<cms:set my_page_id = k_page_id 'global' />
<cms:set my_form_mode = 'edit' 'global' />
</cms:pages>
<cms:show my_form_mode />
<!-- this is regular databound-form showing fields from the extra template -->
<cms:form
masterpage='extra.php'
mode=my_form_mode
page_id=my_page_id
enctype="multipart/form-data"
method='post'
anchor='0'
>
<cms:if k_success >
<cms:db_persist_form
k_page_name = k_member_name
k_page_title = k_member_title
/>
<cms:if k_success >
<cms:set_flash name='success_msg' value='1' />
<cms:redirect k_page_link />
</cms:if>
</cms:if>
<cms:if k_error >
<font color='red'><cms:each k_error ><cms:show item /><br /></cms:each></font>
</cms:if>
<!-- an example extra field -->
<cms:input name='seminar_date' type='bound' /><br />
<input type="submit" name="submit" value="Save"/>
</cms:form>
Please note in the code above how we change the 'mode' of the form from 'create' to 'edit' if there is an existing page for the member and how we are explicitly setting the new page's name and title to match the member's info.
Hope it helps.