Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. models\Logs\Events.php
  5. */
  6.  
  7. class Logs_Events extends Kwf_Events_Subscriber
  8. {
  9. public function getListeners()
  10. {
  11. return array(
  12. array(
  13. 'event' => 'Kwf_Events_Event_Row_Updated',
  14. 'callback' => 'onUpdated'
  15. ),
  16. array(
  17. 'event' => 'Kwf_Events_Event_Row_Deleted',
  18. 'callback' => 'onDeleted'
  19. ),
  20. array(
  21. 'event' => 'Kwf_Events_Event_Row_Inserted',
  22. 'callback' => 'onInserted'
  23. ),
  24. );
  25. }
  26.  
  27. public function onUpdated(Kwf_Events_Event_Row_Updated $ev)
  28. {
  29. $object = $this->getObjectValues($ev->row);
  30. $message = $this->changeColumnsCheck($ev->row);
  31. if ($message) {
  32. $this->writeLog($ev->class, 'edited', $message, $object);
  33. } else $this->writeLog($ev->class, 're-saved', $object);
  34. }
  35.  
  36. public function onDeleted(Kwf_Events_Event_Row_Deleted $ev)
  37. {
  38. $object = $this->getObjectValues($ev->row);
  39. $this->writeLog($ev->class, 'deleted', NULL, $object);
  40. }
  41.  
  42. public function onInserted(Kwf_Events_Event_Row_Inserted $ev)
  43. {
  44. //prevent endless loop
  45. if ($ev->class != 'Logs') {
  46. $object = $this->getObjectValues($ev->row);
  47. $message = $this->changeColumnsCheck($ev->row);
  48. $this->writeLog($ev->class, 'created', $message, $object);
  49. }
  50. }
  51.  
  52. public function getObjectValues($row) {
  53.  
  54. //show only non-empty columns
  55. $valuesArray = array_filter($row->toArray());
  56.  
  57. $object = '';
  58. $lastKey = end(array_keys($valuesArray));
  59. foreach ($valuesArray as $key => $value) {
  60.  
  61. if ($key == $lastKey) {
  62.  
  63. $object .= $key . ': ' . $value;
  64. } else $object .= $key . ': ' . $value . "\n";
  65. }
  66.  
  67. return $object;
  68. }
  69.  
  70.  
  71. public function changeColumnsCheck($row)
  72. {
  73. //if we have changes
  74. $changedCols = $row->getDirtyColumns();
  75.  
  76. if ($changedCols) {
  77.  
  78. $message = '';
  79. $lastKey = end(array_keys($changedCols));
  80. foreach ($changedCols as $key => $col) {
  81.  
  82. $oldValue = $row->getCleanValue($col);
  83. $newValue = $row->$col;
  84.  
  85. if(empty($oldValue)) $oldValue = '(empty)';
  86. if(empty($newValue)) $newValue = '(empty)';
  87.  
  88. if ($key == $lastKey) {
  89.  
  90. $message .= $col . ': from ' . $oldValue . ' to ' . $newValue;
  91. } else $message .= $col . ': from ' . $oldValue . ' to ' . $newValue . "\n";
  92. }
  93.  
  94.  
  95. return $message;
  96. } else return false;
  97. }
  98.  
  99. public function writeLog($modelName, $messageType, $message = NULL, $object)
  100. {
  101. $user = Kwf_Registry::get('userModel')->getAuthedUser();
  102. $data = array(
  103. 'userId' => $user->id,
  104. 'userName' => $user->lastname . ' ' . $user->firstname,
  105. 'model' => $modelName,
  106. 'messageType' => $messageType,
  107. 'message' => $message,
  108. 'object' => $object
  109. );
  110.  
  111. $logsModel = Kwf_Model_Abstract::getInstance('Logs');
  112. $logsModel->createRow($data)->save();
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement