Advertisement
reenadak

Logging changes to a text file in MATE

Apr 15th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.12 KB | None | 0 0
  1. <?php
  2. require_once('DBC.php');
  3. require_once('Common.php');
  4. require_once('php/lang/LangVars-en.php');
  5. require_once('php/AjaxTableEditor.php');
  6.  
  7. /* http://www.mysqlajaxtableeditor.com */
  8. // ========== Script Settings =============
  9.  
  10. $p = isAdmin() ? "ADEFHIUOQSVX" : "";
  11. $str_admin = isAdmin() ? " - Logged in as Admin" : " - Not Admin - ";
  12.  
  13. define("tblMain", "fundflow");
  14. define("primaryCol", "id");
  15. define("sortOn", "id");
  16. define("ascOrDesc", "ASC");
  17.  
  18. define("displayNum", 100);
  19. define("displayNumInc", 100);
  20. define("orderByColumn", "tr_date");
  21. define("tableTitle", 'FundFlow ' . $str_admin);
  22.  
  23. define("permissions", $p);  //Available : ADEFHIOQSUVX
  24. define("addRowTitle", "Add ".tbleMain);
  25. define("editRowTitle", "Edit ".tbleMain);
  26.  
  27. define("addInPlace", true);
  28. define("editInPlace", true);
  29. define("viewQuery", false);
  30. define("paginationLinks", true);
  31.  
  32. class Blank extends Common
  33. {
  34.     var $Editor;
  35.     var $mateInstances = array('mate1_');
  36.    
  37.     protected function displayHtml()
  38.     {
  39.         $html = '
  40.             <br />
  41.                 <div class="mateAjaxLoaderDiv">
  42.                     <div id="ajaxLoader1"><img src="images/ajax_loader.gif" alt="Loading..." />
  43.                     </div>
  44.                 </div>
  45.             <br />
  46.             <br />
  47.                 <div id="'.$this->mateInstances[0].'information"></div>
  48.                 <div id="mateTooltipErrorDiv" style="display: none;"></div>
  49.                 <div id="'.$this->mateInstances[0].'titleLayer" class="mateTitleDiv"></div>        
  50.                 <div id="'.$this->mateInstances[0].'tableLayer" class="mateTableDiv"></div>
  51.                 <div id="'.$this->mateInstances[0].'updateInPlaceLayer" class="mateUpdateInPlaceDiv"></div>    
  52.                 <div id="'.$this->mateInstances[0].'recordLayer" class="mateRecordLayerDiv"></div>         
  53.                 <div id="'.$this->mateInstances[0].'searchButtonsLayer" class="mateSearchBtnsDiv"></div>
  54.                 <div style="align: center; valign: middle;"><div><center><a  href="http://mukeshdak.com/apps/fundflow/">FundFlow</a></center></div></div>
  55.             ';
  56.            
  57.         echo $html;
  58.        
  59.         // Set default session configuration variables here
  60.         $defaultSessionData['orderByColumn'] = orderByColumn;
  61.         $defaultSessionData['ascOrDesc'] = ascOrDesc;
  62.       //    $defaultSessionData['displayNum'] = displayNum;
  63.         //$defaultSessionData['displayNumInc'] = displayNumInc;
  64.         $defaultSessionData = base64_encode($this->Editor->jsonEncode($defaultSessionData));
  65.        
  66.         $javascript = '
  67.             <script type="text/javascript">
  68.                 var ' . $this->mateInstances[0] . ' = new mate("' . $this->mateInstances[0] . '");
  69.                 ' . $this->mateInstances[0] . '.setAjaxInfo({url: "' . $_SERVER['PHP_SELF'] . '", history: true});
  70.                 ' . $this->mateInstances[0] . '.init("' . $defaultSessionData . '");               
  71.             </script>';
  72.         echo $javascript;
  73.     }
  74.  
  75.  
  76.     function storeRow($col,$val,$row)
  77.     {
  78.        $_SESSION['old_row'] = $row;
  79.        return $val;
  80.     }
  81.  
  82.     function logEdit($val,$col,$row)
  83.     {
  84.        $changes = array();
  85.        foreach($row as $col => $val)
  86.        {
  87.           if(isset($_SESSION['old_row'][$col]) && $_SESSION['old_row'][$col] != $val)
  88.           {
  89.              $changes[] = $col.' changed from '.$_SESSION['old_row'][$col].' to '.$val;
  90.           }
  91.        }
  92.        if(count($changes) > 0)
  93.        {
  94.         // log changes
  95.         $log_file_name = basename(__FILE__, ".php")."_".date("Y-m").".csv" ;
  96.         $change_time = date("Y-m-d_H-i-s");
  97.         $file = fopen($log_file_name, "a");
  98.         $changes = "\n". $change_time." Changes by ".$_SESSION['oauth_user']['email']." for " . primaryCol . ": ". $_SESSION['old_row'][primaryCol] . " are ".implode(", ", $changes);
  99.         fwrite ($file, $changes);
  100.         fclose($file);// log changes
  101.        }
  102.     }
  103.    
  104.     protected function initiateEditor()
  105.     {
  106.         $tableColumns['id'] = array(
  107.             'display_text' => 'ID',
  108.             'perms' => 'TVQFSXO',
  109.             'view_fun' => array(&$this,'showDeleteOnEdit')
  110.         );
  111.        
  112.         $tableColumns['tr_name'] = array(
  113.             'display_text' => 'Transaction Name',
  114.             'perms' => 'AEVCTXQSHOF'           
  115.         );
  116.  
  117.         $tableColumns['tr_date'] = array(
  118.             'display_text' => 'Tr Date',
  119.             'perms' => 'AEFVCTXQSHO',
  120.             'display_mask' => 'date_format(tr_date,"%d %M %Y")',
  121.             'order_mask' => 'date_format(tr_date,"%Y-%m-%d %T")',
  122.             'calendar' => array('js_format' => 'dd MM yy')
  123.         );
  124.                
  125.         $tableColumns['tr_value'] = array(
  126.             'display_text' => 'Value',         
  127.             'perms' => 'AEVCTAXQSHOF',
  128.             'input_info' => 'size="32"',
  129.             'table_fun' => array(&$this,'limit_characters'),
  130.             'edit_fun' => array(&$this,'storeRow')
  131.             );
  132.        
  133.         $tableColumns['notes'] = array(
  134.             'display_text' => 'Notes',
  135.             'perms' => 'EVCTAXQSFHO',
  136.             'textarea' => array('cols' => 40, 'rows' => 3),
  137.             'sub_str' => 30
  138.         );
  139.        
  140.             $tableColumns['modified'] = array(
  141.             'display_text' => 'Modified',
  142.             'perms' => 'VCXQSHO',
  143.             'display_mask' => 'date_format(modified,"%d %M %Y")',
  144.             'calendar' => array('js_format' => 'dd MM yy')
  145.         );
  146.        
  147.         $errorFun = array(&$this,'logError');
  148.        
  149.         $this->Editor = new AjaxTableEditor(tblMain, primaryCol, $errorFun, permissions, $tableColumns);
  150.         $this->Editor->setConfig('tableInfo','cellpadding="1" cellspacing="1" align="center" class="mateTable"'); /* width="90%" */
  151.         $this->Editor->setConfig('orderByColumn', orderByColumn);
  152.         $this->Editor->setConfig('removeIcons', "CD");
  153.         $this->Editor->setConfig('tableTitle', tableTitle);
  154.         $this->Editor->setConfig('addRowTitle', addRowTitle);
  155.         $this->Editor->setConfig('editRowTitle', editRowTitle);
  156.         $this->Editor->setConfig('addInPlace', addInPlace);
  157.         $this->Editor->setConfig('editInPlace', editInPlace);
  158.         $this->Editor->setConfig('viewQuery', viewQuery);
  159.         $this->Editor->setConfig('paginationLinks', paginationLinks);
  160.         $this->Editor->setConfig('instanceName',$this->mateInstances[0]);
  161.         $this->Editor->setConfig('afterDeleteFun', array(&$this, 'refreshAfterDelete'));
  162.         $this->Editor->setConfig('afterEditFun',array(&$this,'logEdit'));
  163.     //  $this->Editor->setConfig('modifyRowSets',array(&$this,'changeBgColor'));
  164.         $this->Editor->setConfig('storageType','web');
  165.  
  166.     //  $userButtons[] = array('label' => 'Show Info', 'call_back_fun' => array(&$this,'testUserBtn'));
  167.     //  $this->Editor->setConfig('userButtons',$userButtons);
  168.         //$this->Editor->setConfig('escapeHtml',true);
  169.     }
  170.    
  171.     function __construct()
  172.     {
  173.         session_start();
  174.         ob_start();
  175.         $this->initiateEditor();
  176.         if(isset($_POST['json']))
  177.         {
  178.             if(ini_get('magic_quotes_gpc'))
  179.             {
  180.                 $_POST['json'] = stripslashes($_POST['json']);
  181.             }
  182.             $this->Editor->data = $this->Editor->jsonDecode($_POST['json'],true);
  183.             $this->Editor->setDefaults();
  184.             $this->Editor->main();
  185.         }
  186.         else if(isset($_GET['mate_export']))
  187.         {
  188.             $this->Editor->data['sessionData'] = $_GET['session_data'];
  189.             $this->Editor->setDefaults();
  190.             ob_end_clean();
  191.             header('Cache-Control: no-cache, must-revalidate');
  192.             header('Pragma: no-cache');
  193.             header('Content-type: application/x-msexcel');
  194.             header('Content-Type: text/csv');
  195.             header('Content-Disposition: attachment; filename="' . $this->Editor->tableName . '.csv"');
  196.             // Add utf-8 signature for windows/excel
  197.             echo chr(0xEF).chr(0xBB).chr(0xBF);
  198.             echo $this->Editor->exportInfo();
  199.             exit();
  200.         }
  201.         else
  202.         {
  203.             $this->allowIfLoggedIn();
  204.             $this->displayHeaderHtml();
  205.             $this->displayHtml();
  206.             $this->displayFooterHtml();
  207.         }
  208.     }
  209. }
  210. $page = new Blank();
  211. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement