| 1 |
<?php |
| 2 |
namespace WeDevs\ERP; |
| 3 |
|
| 4 |
/** |
| 5 |
* ERP Log reporting class API |
| 6 |
* |
| 7 |
* @since 0.1 |
| 8 |
* |
| 9 |
* @package wp-erp |
| 10 |
*/ |
| 11 |
class Log { |
| 12 |
|
| 13 |
/** |
| 14 |
* Initializes the Log class |
| 15 |
* |
| 16 |
* Checks for an existing Log instance |
| 17 |
* and if it doesn't find one, creates it. |
| 18 |
*/ |
| 19 |
public static function instance() { |
| 20 |
static $instance = false; |
| 21 |
|
| 22 |
if ( ! $instance ) { |
| 23 |
$instance = new self(); |
| 24 |
} |
| 25 |
|
| 26 |
return $instance; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add a new log |
| 31 |
* |
| 32 |
* @since 0.1 |
| 33 |
* |
| 34 |
* @param array $data |
| 35 |
* |
| 36 |
* @return inserted_id |
| 37 |
*/ |
| 38 |
public function add( $data ) { |
| 39 |
return $this->insert_log( $data ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get logs base on criteria |
| 44 |
* |
| 45 |
* @since 0.1 |
| 46 |
* @since 1.2.0 Returns log count when $count param is true |
| 47 |
* |
| 48 |
* @param array $data |
| 49 |
* @param boolean $count |
| 50 |
* |
| 51 |
* @return object Collection of log |
| 52 |
*/ |
| 53 |
public function get( $args = array(), $count = false ) { |
| 54 |
global $wpdb; |
| 55 |
|
| 56 |
$defaults = array( |
| 57 |
'number' => 20, |
| 58 |
'offset' => 0, |
| 59 |
'no_object' => false, |
| 60 |
'orderby' => 'id', |
| 61 |
'order' => 'DESC' |
| 62 |
); |
| 63 |
|
| 64 |
$args = wp_parse_args( $args, $defaults ); |
| 65 |
$where = $results = []; |
| 66 |
|
| 67 |
$audits = new \WeDevs\ERP\Admin\Models\Audit_Log(); |
| 68 |
$audit_log = $audits->leftjoin( $wpdb->users, 'created_by', '=', $wpdb->users . '.ID' )->select( $wpdb->users.'.display_name', $wpdb->prefix . 'erp_audit_log.*' ); |
| 69 |
|
| 70 |
if ( isset( $args['component'] ) && ! empty( $args['component'] ) ) { |
| 71 |
$audit_log = $audit_log->where( 'component', $args['component'] ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( isset( $args['sub_component'] ) && ! empty( $args['sub_component'] ) ) { |
| 75 |
$audit_log = $audit_log->where( 'sub_component', $args['sub_component'] ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( isset( $args['data_id'] ) && ! empty( $args['data_id'] ) ) { |
| 79 |
$audit_log = $audit_log->where( 'data_id', $args['data_id'] ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( isset( $args['old_value'] ) && ! empty( $args['old_value'] ) ) { |
| 83 |
$audit_log = $audit_log->where( 'old_value', $args['old_value'] ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( isset( $args['new_value'] ) && ! empty( $args['new_value'] ) ) { |
| 87 |
$audit_log = $audit_log->where( 'new_value', $args['new_value'] ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( isset( $args['changetype'] ) && ! empty( $args['changetype'] ) ) { |
| 91 |
$audit_log = $audit_log->where( 'changetype', $args['changetype'] ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( isset( $args['created_by'] ) && ! empty( $args['created_by'] ) ) { |
| 95 |
$audit_log = $audit_log->where( 'created_by', (int)$args['created_by'] ); |
| 96 |
} |
| 97 |
|
| 98 |
// get item with date |
| 99 |
if ( isset( $args['start'] ) && isset( $args['end'] ) && ! empty( $args['start'] ) && ! empty( $args['end'] ) ) { |
| 100 |
$audit_log = $audit_log->where( 'created_at', '>=', $args['start'].' 00:00:00' ) |
| 101 |
->where( 'created_at', '<=', $args['end'].' 23:59:59' ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
$cache_key = 'erp-get-audit-log' . md5( serialize( $args ) ); |
| 106 |
$results = wp_cache_get( $cache_key, 'erp' ); |
| 107 |
$users = array(); |
| 108 |
|
| 109 |
if ( false === $results ) { |
| 110 |
$results = $audit_log->skip( $args['offset'] ) |
| 111 |
->take( $args['number'] ) |
| 112 |
->orderBy( $args['orderby'], $args['order'] ); |
| 113 |
|
| 114 |
$results = $results->get()->toArray(); |
| 115 |
|
| 116 |
$results = erp_array_to_object( $results ); |
| 117 |
|
| 118 |
wp_cache_set( $cache_key, $results, 'erp', HOUR_IN_SECONDS ); |
| 119 |
|
| 120 |
} else if ( $count ) { |
| 121 |
return $audit_log->skip( $args['offset'] )->take( $args['number'] )->count(); |
| 122 |
} |
| 123 |
|
| 124 |
return $results; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Insert a new log record |
| 129 |
* |
| 130 |
* @since 0.1 |
| 131 |
* |
| 132 |
* @param array $args |
| 133 |
* |
| 134 |
* @return integer [inserted id] |
| 135 |
*/ |
| 136 |
public function insert_log( $args ) { |
| 137 |
global $wpdb; |
| 138 |
|
| 139 |
$table = $wpdb->prefix . 'erp_audit_log'; |
| 140 |
|
| 141 |
$defaults = array( |
| 142 |
'component' => 'HRM', |
| 143 |
'sub_component' => '', |
| 144 |
'data_id' => null, |
| 145 |
'old_value' => '', |
| 146 |
'new_value' => '', |
| 147 |
'message' => '', |
| 148 |
'changetype' => 'add', |
| 149 |
'created_by' => '', |
| 150 |
'created_at' => current_time('mysql') |
| 151 |
); |
| 152 |
|
| 153 |
$formated = ['%s', '%s', '%d', '%s', '%s', '%s', '%s', '%d']; |
| 154 |
|
| 155 |
$fields = wp_parse_args( $args, $defaults ); |
| 156 |
|
| 157 |
do_action( 'erp_after_before_audit_log', $fields ); |
| 158 |
|
| 159 |
$id = $wpdb->insert( $table, $fields, $formated ); |
| 160 |
|
| 161 |
//$inserted = \WeDevs\ERP\Admin\Models\Audit_Log::create( $fields ); |
| 162 |
|
| 163 |
do_action( 'erp_after_insert_audit_log', $id, $fields ); |
| 164 |
|
| 165 |
return $id; |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|