⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Audit trail displayed on form view



jadin
  • profile picture
  • Member

Posted 29 January 2013 - 11:21 AM

Hello,

First off, great work. GroceryCrud is really making things remarkably easy.

I have a project with two tables "Sites" and "sectors", related by 'siteID' field. I wish to create an audit trail on both tables, so there is another table "audit" which will log changes from both tables. The idea is that when someone clicks "edit" on a record for the "sites" grid, below the form editing view the user gets another grid showing the change history for that particular Site (no editing).

As I understand from the documentation callbacks are suited to populate the audit table. What I have not figured out is how to get the view part in the edit form to also display a grid filtered on 'siteID'.

Any pointers would be appreciated.

davidoster
  • profile picture
  • Member

Posted 30 January 2013 - 09:26 AM

CodeIginter and hence GroceryCRUD let's you load multiple views on a single page, let's say via a controller function.
So for the view part you could have:

$crud->set_table('Sites');
$sites = $crud->render();
$this->_example_output($sites);

$crud2->set_table('audits');
// make sure to use unset_add/edit/delete here
$audits = $crud2->render();
$this->_example_output($audits);


And via callback_after_insert/update, http://www.grocerycr...ck_after_insert / http://www.grocerycr...ck_after_update
do the INSERT INTO on the audits table.

jadin
  • profile picture
  • Member

Posted 04 February 2013 - 11:07 AM

Hi, thanks for your reply. In fact I ended up using a different way all together for viewing the history. In each row I've added a custom "History" action. Once clicked it forwards the user to the history page which is just another read-only GroceryCrud table that should be filtered with the site ID of that particular site. My problem now is that the id of the row is passed in the url, but I can't figure how to use that in the controller. 

 

So basically I need to parse the url, and then use it in $crud->where('id');


jadin
  • profile picture
  • Member

Posted 04 February 2013 - 13:40 PM

Never mind, got it :)


jadin
  • profile picture
  • Member

Posted 05 February 2013 - 12:12 PM

Could someone let me know what is wrong with the code below. After the table loads, the rows appear for a brief moment before I get an empty table. A typical URL is something like this:

 

hostname/central/index.php/sites_tracker/data/sites_tracker_track_history?Siteid=4005

 

NOTE: I just checked, and this a problem only encountered with Chrome. Firefox and IE are handling it as they should.

 

Any ideas what might be causing this?

public function index(){
	
		
		//Get Site ID from URL
		$url = $_SERVER['REQUEST_URI'];
		$siteID = substr(parse_url($url, PHP_URL_QUERY), 7);
		// initialize groceryCRUD
                $crud = new grocery_CRUD();	
                //Set where condition
		$crud->where('siteID',$siteID);
		
                // set model
                $crud->set_model($this->cms_module_path().'/data/sites_tracker_track_history_model');
        
       
                // table name
                $crud->set_table('track_history');
        
                // set subject
                $crud->set_subject('History');

		
                $crud->unset_add();
                $crud->unset_edit();
        

		$crud->callback_before_insert(array($this,'before_insert'));
		$crud->callback_before_update(array($this,'before_update'));
		$crud->callback_before_delete(array($this,'before_delete'));
		$crud->callback_after_insert(array($this,'after_insert'));
		$crud->callback_after_update(array($this,'after_update'));
		$crud->callback_after_delete(array($this,'after_delete'));
		// callback for one to many detail columns
        
        
                // render
                $output = $crud->render();   
	
                $this->view($this->cms_module_path().'/data/sites_tracker_track_history_index', $output, 'sites_tracker_track_history');
        
    }

goFrendiAsgard
  • profile picture
  • Member

Posted 06 February 2013 - 07:42 AM

Maybe in your case $siteID is NULL.
How about doing this to ensure:
 

public function index(){
        
                
                //Get Site ID from URL
                $url = $_SERVER['REQUEST_URI'];
                $siteID = substr(parse_url($url, PHP_URL_QUERY), 7);
                
                // make sure the content of $siteID
                echo var_dump($siteID);

                // or you can also use this if you are using No-CMS:
                // $this->cms_show_variable($siteID);
    }

davidoster
  • profile picture
  • Member

Posted 07 February 2013 - 05:49 AM

Instead of these lines,

$url = $_SERVER['REQUEST_URI'];
$siteID = substr(parse_url($url, PHP_URL_QUERY), 7);

 

use these:

$url = $this->uri->segment(3);
$siteID = substr($url,strpos($url,'=')+1);

 

and let us know if this resolved it.


jadin
  • profile picture
  • Member

Posted 09 February 2013 - 13:36 PM

Hi,

 

Apologies for the late response, and thanks for your suggestions. The issue is not with siteID variable, as when I do an echo it appears correctly. Also, as stated firefox and IE are correctly loading the table with the where condition working as it should.Even in Chrome, the table initially loads correctly, but only later sort of "refreshes" and then all the rows appear.

 

The $this->uri->segment() function will not return anything after the '?' character. The third segment it returns is 'sites_tracker_track_history'.

 

I'm suspecting this has to do with the $this->view statement at the end. 


goFrendiAsgard
  • profile picture
  • Member

Posted 10 February 2013 - 10:07 AM

I think I know the problem.
So how about using 

 

$this->input->get('Siteid');
 

to get the siteID?


jadin
  • profile picture
  • Member

Posted 11 February 2013 - 12:02 PM

I think I know the problem.
So how about using 

 

$this->input->get('Siteid');
 

to get the siteID?

 

 

I tried this suggestion. It works exactly like before; google Chrome is still failing to filter.


goFrendiAsgard
  • profile picture
  • Member

Posted 14 February 2013 - 08:39 AM

Okay, how about using this url?


 

hostname/central/index.php/sites_tracker/data/sites_tracker_track_history/index?Siteid=4005
 

And, can you please show us what is the content of $siteID?

 


$siteID = $this->input->get('Siteid');
var_dump($siteID)

jadin
  • profile picture
  • Member

Posted 16 February 2013 - 10:29 AM

Okay, how about using this url?


 

hostname/central/index.php/sites_tracker/data/sites_tracker_track_history/index?Siteid=4005
 

 

 

Thank you so much :) This suggestion worked on all browsers


jadin
  • profile picture
  • Member

Posted 24 February 2013 - 11:54 AM

Thank you so much :) This suggestion worked on all browsers

 

 

Funny story, this suggestion is no longer working.

 

I have a pretty good idea what was/is happening though.

 

Here is the full scenario: On Chrome/Firefox and IE, the page loads correctly any number of times as long as the user doesn't click on the Grocery CRUD's refresh button (the Flexigrid one) once. Whenever that button is clicked, immediately the filtering in the where condition is cleared, and the entire table appears. More importantly, every time the page loads after that (in that particular browser), the flexigrid refreshes automatically when the page is finished loading, causing the filter to disappear at every page load after that. Clearing the cache resets the behavior.

 

Anyone has any ideas about that?


jadin
  • profile picture
  • Member

Posted 26 February 2013 - 09:14 AM

Hi,

 

Can no one assist with that issue? Using the search function, I can see some others had a similar issue with auto-refresh (/topic/653-problem-with-set-relation-where/) but not sure how the solution there applies in this case.


jadin
  • profile picture
  • Member

Posted 26 February 2013 - 09:35 AM

Removing everything else, if I hard code  the site ID as follows:

 

$siteID = $this->input->get('Siteid');

if ($siteID!=''){
            $crud->where('site_id','3700');
} 
 
 
I have the same problem (list auto-refreshes, and the where condition disappears). If on the other hand I do:
 
$siteID = $this->input->get('Siteid');

if ($siteID!='3704'){
             $crud->where('site_id','3700'); 
} 
 
The behavior is correct.
 
I don't understand why the if condition should have any effect like this...

goFrendiAsgard
  • profile picture
  • Member

Posted 26 February 2013 - 22:14 PM

Hi, Jadin
As far as I know, $this->input->get() will return FALSE (old version of CI) or NULL (new version of CI)
In this case, evaluate thing with !='' probably give an unexpected result.

So, how about this one?

$siteID = $this->input->get('Siteid');

if (!$siteID){
            $crud->where('site_id','3700');
} 

jadin
  • profile picture
  • Member

Posted 02 March 2013 - 11:50 AM

Thanks for the input. As I said in the beginning, the if condition executes just fine. The problem is when flexigrid executes an auto-refresh after the page loads; that's when the "where" condition malfunctions. Put simply, removing everything (along with the if statement) and just using:

 

$siteID = $this->input->get('Siteid');

$crud->where('History.site_id',$siteID);

 

1- The page first loads correctly (where statement is as it's supposed to be)

2- Flexigrid executes an autorefresh

3- All rows disappear 

 

When flexigrid does an autorefresh, it's as if it's setting Siteid to null, causing this:

 

$crud->where('History.site_id',$siteID);

 

to become this:

 

$crud->where('History.site_id', null);

 

Datatables works fine, and I'm using it for this particular table as a temporary fix, but I'd like to have consistency in the theme used.