<commit_before><?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2015 Spring Signage Ltd
 *
 * This file (AuditTrailFactory.php) is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */


namespace Xibo\Factory;

use Xibo\Entity\AuditLog;
use Xibo\Service\LogServiceInterface;
use Xibo\Service\SanitizerServiceInterface;
use Xibo\Storage\StorageServiceInterface;

/**
 * Class AuditLogFactory
 * @package Xibo\Factory
 */
class AuditLogFactory extends BaseFactory
{
    /**
     * Construct a factory
     * @param StorageServiceInterface $store
     * @param LogServiceInterface $log
     * @param SanitizerServiceInterface $sanitizerService
     */
    public function __construct($store, $log, $sanitizerService)
    {
        $this->setCommonDependencies($store, $log, $sanitizerService);
    }

    /**
     * @return AuditLog
     */
    public function create()
    {
        return new AuditLog($this->getStore(), $this->getLog());
    }

    /**
     * @param array $sortOrder
     * @param array $filterBy
     * @return array
     */
    public function query($sortOrder = null, $filterBy = [])
    {
        $this->getLog()->debug('AuditLog Factory with filter: %s', var_export($filterBy, true));

        $entries = [];
        $params = [];

        $select = ' SELECT logId, logDate, user.userName, message, objectAfter, entity, entityId, auditlog.userId ';
        $body = 'FROM `auditlog` LEFT OUTER JOIN user ON user.userId = auditlog.userId WHERE 1 = 1 ';

        if ($this->getSanitizer()->getInt('fromTimeStamp', $filterBy) !== null) {
            $body .= ' AND `auditlog`.logDate >= :fromTimeStamp ';
            $params['fromTimeStamp'] = $this->getSanitizer()->getInt('fromTimeStamp', $filterBy);
        }

        if ($this->getSanitizer()->getInt('toTimeStamp', $filterBy) !== null) {
            $body .= ' AND `auditlog`.logDate < :toTimeStamp ';
            $params['toTimeStamp'] = $this->getSanitizer()->getInt('toTimeStamp', $filterBy);
        }

        if ($this->getSanitizer()->getString('entity', $filterBy) != null) {
            $body .= ' AND `auditlog`.entity LIKE :entity ';
            $params['entity'] = '%' . $this->getSanitizer()->getString('entity', $filterBy) . '%';
        }

        if ($this->getSanitizer()->getString('userName', $filterBy) != null) {
            $body .= ' AND `auditlog`.userName LIKE :userName ';
            $params['userName'] = '%' . $this->getSanitizer()->getString('userName', $filterBy) . '%';
        }

        if ($this->getSanitizer()->getString('message', $filterBy) != null) {
            $body .= ' AND `auditlog`.message LIKE :message ';
            $params['message'] = '%' . $this->getSanitizer()->getString('message', $filterBy) . '%';
        }

        if ($this->getSanitizer()->getInt('entityId', $filterBy) !== null) {
            $body .= ' AND `auditlog`.entityId = :entityId ';<commit_msg>OSX-510 fixes user filter on Audit Trail page<commit_after><?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2015 Spring Signage Ltd
 *
 * This file (AuditTrailFactory.php) is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */


namespace Xibo\Factory;

use Xibo\Entity\AuditLog;
use Xibo\Service\LogServiceInterface;
use Xibo\Service\SanitizerServiceInterface;
use Xibo\Storage\StorageServiceInterface;

/**
 * Class AuditLogFactory
 * @package Xibo\Factory
 */
class AuditLogFactory extends BaseFactory
{
    /**
     * Construct a factory
     * @param StorageServiceInterface $store
     * @param LogServiceInterface $log
     * @param SanitizerServiceInterface $sanitizerService
     */
    public function __construct($store, $log, $sanitizerService)
    {
        $this->setCommonDependencies($store, $log, $sanitizerService);
    }

    /**
     * @return AuditLog
     */
    public function create()
    {
        return new AuditLog($this->getStore(), $this->getLog());
    }

    /**
     * @param array $sortOrder
     * @param array $filterBy
     * @return array
     */
    public function query($sortOrder = null, $filterBy = [])
    {
        $this->getLog()->debug('AuditLog Factory with filter: %s', var_export($filterBy, true));

        $entries = [];
        $params = [];

        $select = ' SELECT logId, logDate, user.userName, message, objectAfter, entity, entityId, auditlog.userId ';
        $body = 'FROM `auditlog` LEFT OUTER JOIN user ON user.userId = auditlog.userId WHERE 1 = 1 ';

        if ($this->getSanitizer()->getInt('fromTimeStamp', $filterBy) !== null) {
            $body .= ' AND `auditlog`.logDate >= :fromTimeStamp ';
            $params['fromTimeStamp'] = $this->getSanitizer()->getInt('fromTimeStamp', $filterBy);
        }

        if ($this->getSanitizer()->getInt('toTimeStamp', $filterBy) !== null) {
            $body .= ' AND `auditlog`.logDate < :toTimeStamp ';
            $params['toTimeStamp'] = $this->getSanitizer()->getInt('toTimeStamp', $filterBy);
        }

        if ($this->getSanitizer()->getString('entity', $filterBy) != null) {
            $body .= ' AND `auditlog`.entity LIKE :entity ';
            $params['entity'] = '%' . $this->getSanitizer()->getString('entity', $filterBy) . '%';
        }

        if ($this->getSanitizer()->getString('userName', $filterBy) != null) {
            $body .= ' AND `user`.userName LIKE :userName ';
            $params['userName'] = '%' . $this->getSanitizer()->getString('userName', $filterBy) . '%';
        }

        if ($this->getSanitizer()->getString('message', $filterBy) != null) {
            $body .= ' AND `auditlog`.message LIKE :message ';
            $params['message'] = '%' . $this->getSanitizer()->getString('message', $filterBy) . '%';
        }

        if ($this->getSanitizer()->getInt('entityId', $filterBy) !== null) {
            $body .= ' AND `auditlog`.entityId = :entityId ';
{'fraction_non_alphanumeric': 0.1493193420306296, 'fraction_numerical': 0.002410663641520136, 'mean_word_length': 2.955692652832305, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7035189', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2082, 'n_words': 551}