PostCreate, PostEdit and PostRemove logging to save on other database

PostCreate, PostEdit and PostRemove logging to save on other database

hyklhykl Posts: 48Questions: 19Answers: 5
edited December 2016 in Editor

I want to PostEdit to save logging on other database and no save to same database !

function logChange ($db, $action, $id, $values) {
            $db->insert('logs', array(
                'user'      => $_SESSION['user'],
                'action'          => $action,
                'values'       => json_encode($values),
                'row'         => $id,
                'date'         => date('d.m.Y')
            ));
}

...

->on('postCreate', function ($editor, $id, $values, $row) {
            logChange($editor->db(), 'create', $id, $values);
        })
        ->on('postEdit', function ($editor, $id, $values, $row) {
            logChange($editor->db(), 'edit', $id, $values);
        })
        ->on('postRemove', function ($editor, $id, $values) {
            logChange($editor->db(), 'delete', $id, $values);
        })
        ->process($_POST)
        ->json();

Please help me and thank you very much.

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Does the database connection held by $db have permission to write to the other database? If so, just use otherDb.logs as the table name.

    If it doesn't have permission and you can't add it, then you would need another database connection resource (probably best to use just a regular plain old PDO resource rather than the Editor database abstraction layer).

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5
    edited December 2016

    Now, again and clarity:
    Step by step:

    1) Datatables.php - code:

    <?php
    
    define("DATATABLES", true, true);
    
    if (version_compare(PHP_VERSION, "5.3.0", '<' )) {
        echo json_encode( array(
            "sError" => "Editor PHP libraries required PHP 5.3 or newer. You are ".
                "currently using ".PHP_VERSION.". PHP 5.3 and newer have a lot of ".
                "great new features that the Editor libraries take advantage of to ".
                "present an easy to use and flexible API."
        ));
        exit(0);
    }
    
    include( dirname(__FILE__).'/Bootstrap.php' );
    

    2) Bootstrap.php - code:

    <?php
    namespace DataTables;
    if (!defined('DATATABLES')) exit();
    
    if (!isset($sql_details1)) {
        include(dirname(__FILE__).'/config-datatabase1.php' );
    }
    
    if (!isset($sql_details2)) {
        include(dirname(__FILE__).'/config-datatabase2.php' );
    }
    
    spl_autoload_register( function ($class) {
        $a = explode("\\", $class);
        
        if ($a[0] !== "DataTables") {
            return;
        }
        
        if (count($a) === 2) {
            require( dirname(__FILE__).'/'.$a[1].'/'.$a[1].'.php');
        }
        else if ( count( $a ) === 3 ) {
            preg_match_all( "/[A-Z]+[^A-Z]*/", $a[2], $matches);
            $location = implode( '/', $matches[0]);
            
            require(dirname(__FILE__).'/'.$a[1].'/'.$location.'.php');
        }
    });
    
    $db1 = new Database($sql_details1);
    $db2 = new Database($sql_details2);
    

    3) config-database1.php - code:

    <?php if (!defined('DATATABLES')) exit();
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    $sql_details1 = array(
        "type" => "Mysql",
        "user" => "user_db1",
        "pass" => "***************",
        "host" => "db.oracle.com",
        "port" => "3306",
        "db"   => "name_db1", 
        "dsn"  => "charset=utf8"
    );
    

    4) config-database2.php - code:

    <?php if (!defined('DATATABLES')) exit();
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    $sql_details2 = array(
        "type" => "Mysql",
        "user" => "user_db2",
        "pass" => "***************",
        "host" => "db.oracle.com",
        "port" => "3306",
        "db"   => "name_db2", 
        "dsn"  => "charset=utf8"
    );
    

    5) staff-data.php - code:

    include("DataTables.php");
    
    session_start();
    
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    Editor::inst($db1,'table_name1')
            ->fields(
                Field::inst('id'),
                Field::inst('user_name'),
                Field::inst('title'),
                Field::inst('timestamp')
                    ->getFormatter(function ($val) {
                        return date('d.m.Y - H:i:s', $val);
                    }),
                Field::inst('session_id')
            )
            ->process($_POST)
            ->json();
    

    Where I add PDO and PDO for insert on PostEdit?

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Okay, so the $db2 variable is the connection you want to use for the logging? In which case you don't need a plain PDO connection (presumably you are happy to use the Editor Database class rather than a plain PDO reference).

    In which case you might use:

            ->on('postCreate', function ($editor, $id, $values, $row) use ( $db2 ) {
                logChange($db2, 'create', $id, $values);
            })
    

    Then your logChange function will be passed the $db2 variable.

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5

    I am happy.

    SOLVED !

  • hyklhykl Posts: 48Questions: 19Answers: 5

    I ask you:

    If click to data - open Editor form and logchange to save information, it is posible?

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    I'm afraid I don't quite understand the question. You are already using a logChange function to save the information. Is that not doing what you want?

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5

    Information storage is fine when the feature works when you create or after formation, to update or upgrade, when you delete or after removal. That's okay. I just want to, when I click to read such information, then the function logChange stores information such as reading events. Does it?

    PreCreate, PreEdit, PreRemove, PostCreate, PostEdit and PostRemove are all OK :-)

    I ask you, is possible maybe: PreRead? PostRead?

    I want logChange to save information for read row?

    You are understand?

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    I ask you, is possible maybe: PreRead? PostRead?

    As of 1.6 there are preGet and postGet methods which are documented here.

    I want logChange to save information for read row?

    That sounds like a lot of data storage!

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5

    I understand for lot of data storage, but I want to test. Afraid after test:

    ->on('postGet', function ($editor, $id, $values) use ($db2) {
                logChange($db2, 'reading', $id, $values);
            })
    

    Problem is: not working, table is showing: dialog error - tn/1

    Thank you for solve problem - "preGet or postGet"

    I have a version Editor 1.60.

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    What is the response from the server? Most likely there is an error being shown in it that would explain the issue.

    Allan

  • hyklhykl Posts: 48Questions: 19Answers: 5
    Answer ✓

    This is solved problem. This error was for small error of syntax.

This discussion has been closed.