Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
Hello!

I have 2 questions.

First, is there a way to make sure the content of a specific field is unique in the database?
I know that users need unique emails for example.
But for regular entries using clonable pages, it is possible to do something like this?:

Code: Select all
<cms:editable name='contact_email' label="Email" type='text' unique_field='1' />


I know "unique_field" doesn't exist, but is there anything that would act abit like this?

The reason I would need this, is that there's a "Contact list" clonable pages section, and mulitple users can edit contacts, or add some in the admin panel. But it is important to make sure that no one adds a contact that already exist. And since there's going to be hundreds of contacts, the way to verify if it exists would be to check if the "contact_email" region content would already be saved in another entry. If it does, it should display a message that would say that it is not possible to save this entry with the content in the actual email region because it already exists.

2nd question is, is that possible to display entries from specific pages (clonable pages), from the logged in user only?
What I did right now is adding a dropdown region for each clonable pages, with each usernames as options, and when you create an entry, you have to select your username. Then, I activated a custom admin theme, and edited the "content_list_inner.html" file, for my specific page, and changed the <cms:pages...> part of the code for this :

Code: Select all
<cms:if k_user_name == "john-doe">
            <cms:pages
                masterpage       = k_template_name
                id               = k_selected_pageids
                folder           = k_selected_foldername
                orderby          = k_selected_orderby
                order            = k_selected_order
                custom_field     = 'author=John Doe'
                paginate         = '1'
                limit            = k_selected_limit
                show_unpublished = '1'
                show_future_entries = '1'
                page_name        = "<cms:if k_selected_exclude>NOT <cms:show k_selected_exclude /></cms:if>"
                start_on         = k_selected_start_on
                stop_before      = k_selected_stop_before
                base_link        = k_route_link
                token            = k_cur_token
            >
...


I have added a condition that checks if the current username is "john-doe", and if it is, I changed the "custom_field option for the masterpage to show only the ones where John Doe is selected in that dropdown region I talked about earlier. Thing is, I am using filters too with the addon available on the forum. Since I'm changing the custom_field value from "k_selected_custom_field" to "'author=John Doe'", I cannot user the filters, and I really need to be able to use filters.

Any solutions that would solve my issue?

Thanks a lot!
Hi,

Regarding the first issue, what you are looking for is a custom validator that checks for duplicate email before allowing the page to be saved.
Validators (e.g. 'email' etc.) are actually plain PHP functions so you can easily create one by adding an ordinary function to kfunctions.php.

For our purpose, try placing the following in that file -
Code: Select all
// custom email validator
function no_duplicate_mail( $field ){
    global $FUNCS, $CTX;

    $email = trim( $field->get_data() );
    $current_page_id = $field->page->id;

    if( strlen($email) ){

         // Create Couch script..
        $html = "<cms:pages masterpage='contact.php' id='NOT {$current_page_id}' custom_field='email=={$email}' show_future_entries='1' count_only='1' />";

        // Pass on the code to Couch for execution using the 'embed' function
        $count = $FUNCS->embed( $html, $is_code=1 );
        if( $count ){
            return KFuncs::raise_error( "Email already exists" );
        }
    }
}

N.B.: the code above has a hard-coded value of 'contact.php' for masterpage. Please make sure to change it to suit your use-case/

And with that in place, suppose the following was your original definition for the editable region -
Code: Select all
<cms:editable name='email' label='Email' validator='email' type='text' />

.. make it as follows to add our custom validator -
Code: Select all
<cms:editable name='email' label='Email' validator='email | no_duplicate_mail' type='text' />

Make sure to visit the modified template as super-admin for the change to persist.

And now no page should allow a duplicate email.

2. As for the second issue, I don't see any problem with your approach.
If your concern is about using the filter, I think it can be combined with your code as follows (notice the double-quotes) -
Code: Select all
custom_field     = "<cms:show k_selected_custom_field /> | author=John Doe"

Hope the reply helps.
Thank you so much KK.
It's crazy how everything seems possible with Couch if you dig deeper than what is in the documentation.
I'm actually working on a full blown CRM for a client, and at first I wasn't sure it would be possible with Couch, but it is!
Amazing support, as always.
Hi

I just used this piece of code and generalized it to use it on every editable. Filter name is 'unique':

Code: Select all
// custom validator unique value
function unique( $field ){
    global $FUNCS, $CTX;

    $benutzername = trim( $field->get_data() );
    $current_page_id = $field->page->id;
    $editable = $field->name;

    if( strlen($benutzername) ){

         // Create Couch script..
        $html = "<cms:pages masterpage=k_template_name id='NOT {$current_page_id}' custom_field='{$editable}=={$benutzername}' show_future_entries='1' count_only='1' />";

        // Pass on the code to Couch for execution using the 'embed' function
        $count = $FUNCS->embed( $html, $is_code=1 );
        if( $count ){
            return KFuncs::raise_error( "Dieser Wert existiert bereits" );
        }
    }
}


Regards,
Olliwalli
Oliver, your code will fail on checkboxes.
Indeed :-)

An it also does not work when using <cms:db_persist ... /> tag. It states that the editable field does not exists ...
6 posts Page 1 of 1