<?php

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

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

		$isAccess = preg_match('/^content\.(.*)/', $params['perm']);
		$perm = preg_replace('/^(content|ctntedt)\./', '', $params['perm']);

		$link = '';
		if($info = clearFusionCMS::getDb()->fetchRow('SELECT permission FROM fl_permissions WHERE permission=?', $params['perm'])) {
			$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'contentperms', [$params['perm']]) . '">' . __('Edit the Permission', 'cfBackend') . '</a>';
		}

		switch($data['action']) {
			case cfCtntPermAudit::CREATED:
				if($isAccess)
					$msg = sprintf(__('Created access permission <strong>%1s</strong> (<strong>%2s</strong>).', 'cfBackend'), $params['name'], $perm) . $link;
				else
					$msg = sprintf(__('Created edit permission <strong>%1s</strong> (<strong>%2s</strong>).', 'cfBackend'), $params['name'], $perm) . $link;
				break;
			case cfCtntPermAudit::UPDATED:
				if($isAccess)
					$msg = sprintf(__('Updated access permission <strong>%1s</strong> (<strong>%2s</strong>).', 'cfBackend'), $params['name'], $perm) . $link;
				else
					$msg = sprintf(__('Updated edit permission <strong>%1s</strong> (<strong>%2s</strong>).', 'cfBackend'), $params['name'], $perm) . $link;
				break;
			case cfCtntPermAudit::DELETED:
				if($isAccess)
					$msg = sprintf(__('Deleted access permission <strong>%1s</strong> (<strong>%2s</strong>).', 'cfBackend'), $params['name'], $perm) . $link;
				else
					$msg = sprintf(__('Deleted edit permission <strong>%1s</strong> (<strong>%2s</strong>).', 'cfBackend'), $params['name'], $perm) . $link;
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}
}
