Forum for discussing general topics related to Couch.
9 posts Page 1 of 1
Hi everybody,

We have a project webpage (running couch 2.0) where most of the CRUD is done in the frontend (due to design freedom and inline editing).

For understanding our Page
Now we have a "Notes/Questions" field per project where the client can update notes/questions inline. We would like to toggle a database flag everytime a user edits that field, kind of like the red facebook notifications bubble. Then in the projects overview that red flag would mark every project with new notes. As soon a project manager reads the notes he can click a checkbox/button to change the flag to the state "read" meaning the notification would disappear again.

Problem
Generally we know how to program this... But we need to know (at runtime) when that notes field was saved inline to then send a second ajax request updating the flag. As the CKE editor is iniatilized on runtime i can't bind on any events... I also dont know of a possiblity to 'catch' ajax events in the couch backend.

Does anyone have any advice on how to achieve this? Thanks!

Regards Laduma
Can you also take time to explain why this is needed realtime? Once user makes edit, the field is saved to db, and before that happens, there might be multiple "edits", like add-delete image, change headings, mark word as italic, bold etc. Wouldn't that be too much of logging?
Well it doesnt have to be realtime... Just once the user clicks the save button in the CKE editor (Maybe i can somehow catch this - but the class of this cke wrapper is always random e.g. sometimes cke_297 sometimes cke_29 etc etc so i cant know which button to bind it to)...

Basically if user made changes to this specific field then show red flag on project... If field was read (there will be a checkbox/button for this) then remove the red flag again.

Thanks for helping!
I am sure there are php callbacks once editable field has been updated. Look into this if JS doesn\'t help!
When the 'save' button of inline editing is pressed, the AJAX call invokes Couch's regular PHP save() method on the page being edited. I think, this would be the best place to evaluate if the field in question has been modified and, if yes, set the flag.

It will require a bit of PHP to hook onto the save process and take the custom action.

Please let me know if this sounds ok and if you need any help with it.
That sounds great and would actually be pretty helpful with other problems of ours as well!

I see I could add an event listener in the kfunctions.php file (hopefully the kfunctions of our theme is also eligible for this? - To keep it all clean and ordered) and then add this functionality..?

Is there any documentation on these functions? Like for example what methods and variables i could use on the $page object and a list of events being fired?

I have a little snippet below that i found on the forum and now altered to what my needs would look like:

Code: Select all
$FUNCS->add_event_listener( 'page_saved', 'post_process_save' );

    function post_process_save( &$page, &$error_count ){
        var_dump($page);
       
        if( $page->tpl_name=='index.php' ){ // if this is the template we are interested in
            if( !$error_count ){// no errors at this point so the page is definitely going to be saved
               
                if($page->_fields['project_notes']){
                    $this->updateField('project_notes_flag', 1);
                }
            }
        }   
    }


Thank you!
@laduma, assuming your template has a type 'richtext' editable region named 'project_notes' and another of type 'checkbox' named 'project_notes_flag' -
Code: Select all
<cms:editable name='project_notes' label='Project Notes' type='richtext' />

<cms:editable
    name="project_notes_flag"
    label="Tracks the modified status of 'Project Notes' field above"
    opt_values='Modified=1'
    type='checkbox'
/>

Following code in kfunctions.php should automatically tick the checkbox whenever the text region is modified -
Code: Select all
$FUNCS->add_event_listener( 'page_presave', 'my_presave_handler', 10 );
function my_presave_handler( &$page ){
    global $FUNCS;

    if( $page->tpl_name == 'index.php' ){ // if this is the template we are interested in
        if( $page->_fields['project_notes']->modified ){ // if the field we are interested in has been modified

            // set the flag checkbox to '1'
            $page->_fields['project_notes_flag']->store_posted_changes( '1' );
        }
    }
}

The admin can manually toggle off the checkbox after inspection (if the text region gets modified again after this, the checkbox will get set automatically).

Would this help?
Seems perfect! Last question:

I'm assuming that the third parameter of add_event_listener is the access control?

Thanks for the help here, I have a million things i want to do, just opening the next question-post now! :)
I'm assuming that the third parameter of add_event_listener is the access control?

Actually it sets the 'priority' of the handler - higher the value, the greater it's priority (i.e. the earlier it would be executed if there are several handlers).

You can safely ignore it for now :)
9 posts Page 1 of 1