<?php

/**
 * Class file for decoding site 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
 */

/**
 * cfSiteAuditHelper Class
 *
 * Class to provide decoding for site audit trails.
 *
 * @package clearFusionCMS
 */
class cfSiteAuditHelper 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);

		switch($data['action']) {
			case cfSiteAudit::CREATE:
				$msg = sprintf(__('Created a new site <strong>%1s</strong>.', 'cfBackend'), $params['name']);
				break;
			case cfSiteAudit::UPDATE:
				$msg = sprintf(__('Updated the site <strong>%1s</strong>.', 'cfBackend'), $params['name']);
				break;
			case cfSiteAudit::DELETE:
				$msg = sprintf(__('Deleted the site <strong>%s</strong>.', 'cfBackend'), $params['name']);
				break;
			case cfSiteAudit::SET403:
				$msg = sprintf(__('Set the 403 document to <strong>%1s</strong>, ID <strong>%2s</strong> .', 'cfBackend'), $params['path'], $params['id']);
				break;
			case cfSiteAudit::SET404:
				$msg = sprintf(__('Set the 404 document to <strong>%1s</strong>, ID <strong>%2s</strong> .', 'cfBackend'), $params['path'], $params['id']);
				break;
			case cfSiteAudit::SETHOME:
				$msg = sprintf(__('Set the home document to <strong>%1s</strong>, ID <strong>%2s</strong> .', 'cfBackend'), $params['path'], $params['id']);
				break;
			case cfSiteAudit::SETERROR:
				$msg = sprintf(__('Set the error document to <strong>%1s</strong>, ID <strong>%2s</strong> .', 'cfBackend'), $params['path'], $params['id']);
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}

	/**
	 * @inheritdoc
	 */
	function hasAdditionalData($data) {
		return in_array($data['action'], [ cfSiteAudit::UPDATE, cfSiteAudit::DELETE, cfSiteAudit::SET403, cfSiteAudit::SET404, cfSiteAudit::SETHOME, cfSiteAudit::SETERROR ]);
	}
}
