/widgets/portlets/ShowAuditTrail.php

https://github.com/CoreyTisdale/YiiAuditTrail · PHP · 166 lines · 83 code · 15 blank · 68 comment · 4 complexity · 08cee5f91ef61ca1c07144385378710e MD5 · raw file

  1. <?php
  2. /**
  3. * ShowAuditTrail class file.
  4. *
  5. * ShowAuditTrail shows the audit trail for the current item. It
  6. * is configurable via the main config file to (hopefully)
  7. * suit your needs
  8. *
  9. * @author Corey Tisdale <corey[at]eyewantmedia[dot]com>
  10. * @link http://www.yiiframework.com/extension/audittrail/
  11. * @copyright Copyright &copy; 2010-2011 Eye Want Media, LLC
  12. * @package auditTrail
  13. */
  14. Yii::import('zii.widgets.CPortlet');
  15. require_once(realpath(dirname(__FILE__) . '/../../AuditTrailModule.php'));
  16. class ShowAuditTrail extends CPortlet
  17. {
  18. /**
  19. * @var CActiveRecord the model you want to use with this field
  20. */
  21. public $model;
  22. /**
  23. * @var boolean whether or not to show the widget
  24. */
  25. public $visible = true;
  26. /**
  27. * @var this allows you to override individual columns' display properties in the datagrid.
  28. * Column definitions should be indexed by column name, and the value should match the column
  29. * format of CDataGrid. For example:
  30. *
  31. * 'dataGridColumnsOverride' => array(
  32. * 'old_value' => array(
  33. * 'name' => 'old_value',
  34. * 'filter' => '',
  35. * ),
  36. * 'new_value' => array(
  37. * 'name' => 'new_value',
  38. * 'filter' => '',
  39. * ),
  40. * )
  41. *
  42. * Please do not specify a column if you do not wish to override the defaults of that column.
  43. * Also, please be careful when specifying a format for user_id, as special handling exists
  44. * to format the user name
  45. */
  46. public $dataGridColumnsOverride = array( );
  47. /**
  48. * @var AuditTrailModule static variable to hold the module so we don't have to instantiate it a million times to get config values
  49. */
  50. private static $__auditTrailModule;
  51. /**
  52. * Sets the title of the portlet
  53. */
  54. public function init() {
  55. $this->title = "Audit Trail For " . get_class($this->model) . " " . $this->model->id;
  56. parent::init();
  57. }
  58. /**
  59. * generates content of widget the widget.
  60. * This renders the widget, if it is visible.
  61. */
  62. public function renderContent()
  63. {
  64. if($this->visible) {
  65. $auditTrail = AuditTrail::model()->recently();
  66. $auditTrail->model = get_class($this->model);
  67. $auditTrail->model_id = $this->model->primaryKey;
  68. $columnFormat = $this->getColumnFormat();
  69. $this->widget('zii.widgets.grid.CGridView', array(
  70. 'id'=>'audit-trail-grid',
  71. 'dataProvider'=>$auditTrail->search(),
  72. 'columns'=> $this->getColumnFormat(),
  73. ));
  74. }
  75. }
  76. /**
  77. * Builds the label code we need to display usernames correctly
  78. * @return The code to be evaled to display the user info correctly
  79. */
  80. protected function getEvalUserLabelCode() {
  81. $userClass = $this->getFromConfigOrObject('userClass');
  82. $userNameColumn = $this->getFromConfigOrObject('userNameColumn');
  83. $userEvalLabel = ' ( ($t = '
  84. . $userClass
  85. . '::model()->findByPk($data->user_id)) == null ? "": $t->'
  86. . $userNameColumn
  87. . ' ) ';
  88. return $userEvalLabel;
  89. }
  90. /**
  91. * Returns the value you want to look up, either from the config file or a user's override
  92. * @var value The name of the value you would like to look up
  93. * @return the config value you need
  94. */
  95. protected function getFromConfigOrObject($value) {
  96. $at = Yii::app()->modules['auditTrail'];
  97. //If we can get the value from the config, do that to save overhead
  98. if( isset( $at[$value]) && !empty($at[$value] ) ) {
  99. return $at[$value];
  100. }
  101. //If we cannot get the config value from the config file, get it from the
  102. //instantiated object. Only instantiate it once though, its probably
  103. //expensive to do. PS I feel this is a dirty trick and I don't like it
  104. //but I don't know a better way
  105. if(!is_object(self::$__auditTrailModule)) {
  106. self::$__auditTrailModule = new AuditTrailModule(microtime(), null);
  107. }
  108. return self::$__auditTrailModule->$value;
  109. }
  110. /**
  111. * Gets final column format. Starts with default column format (specified in this method
  112. * and checks $this->dataGridColumnsOverride array to see if any columns need to use a
  113. * user specified format.
  114. * @return array The final format array, with any user specified formats taking precedent over defaults
  115. */
  116. protected function getColumnFormat() {
  117. $evalUserLabel = $this->getEvalUserLabelCode();
  118. $columnFormat = array();
  119. $defaultColumnFormat = array(
  120. 'old_value' => array(
  121. 'name' => 'old_value',
  122. 'filter' => '',
  123. ),
  124. 'new_value' => array(
  125. 'name' => 'new_value',
  126. 'filter' => '',
  127. ),
  128. 'action' => array(
  129. 'name' => 'action',
  130. 'filter'=> '',
  131. ),
  132. 'field' => array(
  133. 'name' => 'field',
  134. 'filter' => '',
  135. ),
  136. 'stamp' => array(
  137. 'name' => 'stamp',
  138. 'filter' => '',
  139. ),
  140. 'user_id' => array(
  141. 'name' => 'user_id',
  142. 'value'=>$evalUserLabel,
  143. 'filter'=> '',
  144. ),
  145. );
  146. foreach($defaultColumnFormat as $key => $format) {
  147. $columnFormat[] = isset($this->dataGridColumnsOverride[$key]) ? $this->dataGridColumnsOverride[$key] : $defaultColumnFormat[$key];
  148. }
  149. return $columnFormat;
  150. }
  151. }