<?php

/**
 * Class file for decoding user audit logs.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfUserAuditHelper Class
 *
 * Class to provide decoding for user audit trails.
 *
 * @package clearFusionCMS
 */
class cfUserAuditHelper extends cfAuditHelper {

	/**
	 * Convert the raw data to a human readable message.
	 *
	 * @param array $data The audit data.
	 * @return string The message string.
	 */
	function getMessage($data) {
		$params = empty($data['params']) ? [] : json_decode($data['params'], true);

		$roleList = '';
		if(isset($params['roles']) && count($params['roles'])) {
			$roleList = '<strong>' . implode('</strong>, <strong>', $params['roles']) . '</strong>';
		}

		$permList = '';
		if(isset($params['perms']) && count($params['perms'])) {
			$permList = '<strong>' . implode('</strong>, <strong>', $params['perms']) . '</strong>';
		}

		$link = '';

		if(in_array($data['action'], [ cfUserAudit::NEWROLE, cfUserAudit::UPDATEROLE, cfUserAudit::DELROLE ])) {
			if(clearFusionCMS::getDb()->fetchOne('SELECT rolename FROM fl_roles WHERE roleid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'roles', [$data['cid']]) . '">' . __('Edit the Role', 'cfBackend') . '</a>';
			}
		}
		else {
			if(clearFusionCMS::getDb()->fetchOne('SELECT username FROM fl_users WHERE uid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'edituser', [$data['cid']]) . '">' . __('Edit the User', 'cfBackend') . '</a>';
			}
		}

		switch($data['action']) {
			case cfUserAudit::NEWUSER:
				$msg = sprintf(__('Created new user <strong>%1s</strong> with roles: %2s.', 'cfBackend'), $params['name'], $roleList) . $link;
				break;
			case cfUserAudit::UPDATEUSER:
				$msg = sprintf(__('Updated the user <strong>%1s</strong> with roles: %2s.', 'cfBackend'), $params['name'], $roleList) . $link;
				break;
			case cfUserAudit::DELUSER:
				$msg = sprintf(__('Deleted the user <strong>%s</strong>.', 'cfBackend'), $params['name']);
				break;
			case cfUserAudit::LOGIN:
				$msg = sprintf(__('<strong>%s</strong> logged in to the manager interface.', 'cfBackend'), $params['name']);
				break;
			case cfUserAudit::LOGOUT:
				$msg = sprintf(__('<strong>%s</strong> logged out of the manager interface.', 'cfBackend'), $params['name']);
				break;
			case cfUserAudit::LOGINFAILED:
				$msg = sprintf(__('<strong>%s</strong> failed to login to the manager interface.', 'cfBackend'), $params['name']);
				break;
			case cfUserAudit::NEWROLE:
				$msg = sprintf(__('Created new role <strong>%1s</strong> with permissions: %2s.', 'cfBackend'), $params['name'], $permList) . $link;
				break;
			case cfUserAudit::UPDATEROLE:
				$msg = sprintf(__('Updated role <strong>%1s</strong> with permissions: %2s.', 'cfBackend'), $params['name'], $permList) . $link;
				break;
			case cfUserAudit::DELROLE:
				$msg = sprintf(__('Deleted role <strong>%s</strong>.', 'cfBackend'), $params['name']) . $link;
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}

	/**
	 * @inheritdoc
	 */
	function hasAdditionalData($data) {
		return !in_array($data['action'], [ cfUserAudit::LOGIN, cfUserAudit::LOGOUT, cfUserAudit::LOGINFAILED ]);
	}
}
