Sradesign Posted January 4, 2016 Posted January 4, 2016 Hi guys, I saw some post to track changes an specific filed or just show and instance of before and after save but what I want is a bit different, I have for example a form with 50 fields I want to send an email that shows only the fields that their value changed and what it was before and what it is now. How I can achieve this ? please give me your thoughts. Best Regards, Ali.M
Ivan Gretsky Posted January 4, 2016 Posted January 4, 2016 Please provide more details what data should be compared to form submission. If you need to compare your submitted data to some data saved in the regular fields, It seems to be a trivial task. Just get your data from $input and compare it to data from some $page and then send only fields that changed with wireMail.
Sradesign Posted January 5, 2016 Author Posted January 5, 2016 Yes Ivan is going to be compared with the data saved but since I have to get the data from the page one time and get it again after save for comparison I thought maybe there is a better way to do it cause for example some of my pages have hundreds of fields and maybe only one filed changed so I said to myself lets ask maybe there is a better way to save processing the data.
Ivan Gretsky Posted January 5, 2016 Posted January 5, 2016 I am not sure I understand what you are trying to implement... Let's guess you are trying to build a frontend edit form for non-admin users. The page edited has to saved after edit. But you do not want to save all fields - only those that have changed. But you still have to somehow determine, which fields changed. Probably, you can do it with javascript and only submit the fields changed. But I think it is not worth the effort in most cases.
Sradesign Posted January 5, 2016 Author Posted January 5, 2016 ok lets give you a practical example. You have users with some custom fields for user template. now when a user change his data how do you compare the new data with the old one please give me an example.
Wanze Posted January 5, 2016 Posted January 5, 2016 ProcessWire tracks changes on properties, so you could do something this: // After submitting your form with POST, set the new values to your user object $u = $users->get('myUser'); $u->of(false); $u->set('field1', $sanitizer->text($input->post->field1)); $u->set('field2', $sanitizer->text($input->post->field2)); $u->set('field3', $sanitizer->text($input->post->field3)); // Now assume only field2 and field3 changed $u->getChanges(); // --> Returns you array('field2', 'field3'); // If you need the old value(s) also $u->getChanges(true); $u->save(); Hope it helps! Cheers 2
Sradesign Posted January 5, 2016 Author Posted January 5, 2016 I checked it but I get empty array always see my code below: if($input->post->submitpiform) { if(!is_dir($upload_path)) { if(!wireMkdir($upload_path)) throw new WireException("No upload path!"); } // New wire upload $doc1_photo = new WireUpload('doc1'); // Reference field name in HTML form that uploads photos $doc1_photo->setMaxFiles(1); $doc1_photo->setOverwrite(true); $doc1_photo->setDestinationPath($upload_path); $doc1_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); $doc2_photo = new WireUpload('doc2'); // Reference field name in HTML form that uploads photos $doc2_photo->setMaxFiles(1); $doc2_photo->setOverwrite(true); $doc2_photo->setDestinationPath($upload_path); $doc2_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); $doc3_photo = new WireUpload('doc3'); // Reference field name in HTML form that uploads photos $doc3_photo->setMaxFiles(1); $doc3_photo->setOverwrite(true); $doc3_photo->setDestinationPath($upload_path); $doc3_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); $doc4_photo = new WireUpload('doc4'); // Reference field name in HTML form that uploads photos $doc4_photo->setMaxFiles(1); $doc4_photo->setOverwrite(true); $doc4_photo->setDestinationPath($upload_path); $doc4_photo->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); // execute upload and check for errors $files1 = $doc1_photo->execute(); $files2 = $doc2_photo->execute(); $files3 = $doc3_photo->execute(); $files4 = $doc4_photo->execute(); $usertitle = $input->post->usertitle; $fname = $input->post->fname; $lname = $input->post->lname; $dob = $input->post->dob; $country_of_residence = $input->post->country_of_residence; $country_of_citizenship = $input->post->country_of_citizenship; $address = $input->post->address; $address2 = $input->post->address2; $city = $input->post->city; $state = $input->post->state; $zipcode = $input->post->zipcode; $phone = $input->post->phone; $user->of(false); $user->usertitle = $usertitle; $user->fname = $fname; $user->lname = $lname; $user->dob = $dob; $user->country_of_residence = $country_of_residence; $user->country_of_citizenship = $country_of_citizenship; $user->address = $address; $user->address2 = $address2; $user->city = $city; $user->state = $state; $user->zipcode = $zipcode; $user->phone = $phone; $user->save(); $new_changes = $user->getChanges(); $string = join(',', $new_changes); $session->message(__($string)); foreach($files1 as $filename) { $user->doc1->removeAll(); $user->doc1->add($upload_path . $filename); $user->message("Added file 1 : $filename"); unlink($upload_path . $filename); } foreach($files2 as $filename) { $user->doc2->removeAll(); $user->doc2->add($upload_path . $filename); $user->message("Added file 2 : $filename"); unlink($upload_path . $filename); } foreach($files3 as $filename) { $user->doc3->removeAll(); $user->doc3->add($upload_path . $filename); $user->message("Added file 3 : $filename"); unlink($upload_path . $filename); } foreach($files4 as $filename) { $user->doc4->removeAll(); $user->doc4->add($upload_path . $filename); $user->message("Added file 4 : $filename"); unlink($upload_path . $filename); } $user->save(); $user->of(true); $fullname = $usertitle.' '.$fname.' '.$lname; // $mailadmin = $modules->get('WireMailSmtp'); // $mailadmin->sendSingle(true); // $mailadmin->to('sradesign.net@gmail.com', 'Ali Mahmoudi'); // $mailadmin->cc(array('ali@vinsonfinancials.com')); // $mailadmin->from = 'info@vinsonfinancials.com'; // $mailadmin->fromName = 'Vinson Financials'; // $mailadmin->subject('Test'); // $mailadmin->bodyHTML($fullname.' Account Details Have Been Updated Successfully.<br><br>Date of Birth: '.$dob.'<br>Country of Residence: '.$country_of_residence.'<br>Country of Citizenship: '.$country_of_citizenship.'<br>Address: '.$address.'<br>Address 2: '.$address2.'<br>City: '.$city.'<br>State: '.$state.'<br>Zip Code: '.$zipcode.'<br>Phone: '.$phone); // $numSent = $mailadmin->send(); // $mailuser = new wireMail(); // $mailuser->to($user->email, $fullname); // $mailuser->from = 'info@vinsonfinancials.com'; // $mailuser->fromName = 'Vinson Financials'; // $mailuser->subject('Test'); // $mailuser->bodyHTML('Your Account Details Have Been Updated Successfully.'); // $numSent = $mailuser->send(); $session->message(__('Personal Information Successfully Updated')); $this->session->redirect($this->page->url); }
LostKobrakai Posted January 5, 2016 Posted January 5, 2016 trackChanges does need to be active. Not sure if that's the case when creating a new user.
Sradesign Posted January 5, 2016 Author Posted January 5, 2016 I did turned it on but nothing changed I did used $user->setTrackChanges(true); inside the if for POST and outside also nothing changed.
Wanze Posted January 5, 2016 Posted January 5, 2016 Argh the problem is that calling Page::save() does reset the trackChanges by default. I corrected my example above. So you either need to get the changes before saving or save your user object like so: $user->save(array('resetTrackChanges' => false)); Cheers
Sradesign Posted January 5, 2016 Author Posted January 5, 2016 amazing thanks Wanze another thing how can we show the previous and also new value of the changed fields too?
Wanze Posted January 5, 2016 Posted January 5, 2016 To get the old value(s), you can call the getChanges method like this: // If you need the old value(s) also $u->getChanges(true); Looking at your code, I see that you are assigning input from POST directly, without sanitizing. Check out ProcessWire's $sanitizer API variable. Cheers 2
Sradesign Posted January 5, 2016 Author Posted January 5, 2016 Yes I have a reason for not sanitizing for now but thanks for checking the code and again thanks for the help.
Sradesign Posted January 7, 2016 Author Posted January 7, 2016 Wanze it will send me an array with empty value like this: if I use : $new_changes = $user->getChanges(true); $change_box = json_encode($new_changes); I get this: {"address":[null],"zipcode":[null]}
Wanze Posted January 13, 2016 Posted January 13, 2016 Hi Sradesign, Finally could look at the implementation, turned out that you must enable tracking by values explicitly like this: $p = $pages->get('/'); $p->setTrackChanges(Wire::trackChangesOn | Wire::trackChangesValues); $p->title = 'new value'; var_dump($p->getChanges(true)); die(); Worked for me then, hope it helps! Cheers 6
Sradesign Posted January 13, 2016 Author Posted January 13, 2016 Bravo that fixed my code completely. Below I put the complete code in case any one need it. $user->setTrackChanges(Wire::trackChangesOn | Wire::trackChangesValues); $usertitle = $input->post->usertitle; $fname = $input->post->fname; $lname = $input->post->lname; $user->of(false); $user->usertitle = $usertitle; $user->fname = $fname; $user->lname = $lname; $user->save(array('resetTrackChanges' => false)); $new_changes = $user->getChanges(true); foreach($new_changes as $row => $innerArray){ foreach($innerArray as $innerRow => $value){ $change_box .= $row . " : " .$value . "<br>"; } } echo $change_box;
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now