<?php

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

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

		// Get the info
		$name = isset($params['name']) ? $params['name'] : $data['cid'];
		$link = '';

		if(in_array($data['action'], [cfElementAudit::CREATESNIPPET, cfElementAudit::UPDATESNIPPET, cfElementAudit::DELETESNIPPET])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT snippetname FROM fl_snippets WHERE snippetid=?', $data['cid'])) {
				$name = $info['snippetname'];
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['snippet', $data['cid']]) . '">' . __('Edit the Snippet', 'cfBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [cfElementAudit::CREATETASK, cfElementAudit::UPDATETASK, cfElementAudit::DELETETASK])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT taskname FROM cf_tasksnippets WHERE taskid=?', $data['cid'])) {
				$name = $info['taskname'];
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['task', $data['cid']]) . '">' . __('Edit the Task', 'cfBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [cfElementAudit::CREATEFIELD, cfElementAudit::UPDATEFIELD, cfElementAudit::DELETEFIELD])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT placeholder FROM cf_customflds WHERE placeholder=?', $name)) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['field', $name, 'view']) . '">' . __('Edit the Field', 'cfBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [cfElementAudit::CREATECHUNK, cfElementAudit::UPDATECHUNK, cfElementAudit::DELETECHUNK])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT chunkname FROM fl_chunks WHERE istemplate=0 && chunkid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['chunk', $data['cid']]) . '">' . __('Edit the Chunk', 'cfBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [cfElementAudit::CREATETPL, cfElementAudit::UPDATETPL, cfElementAudit::DELETETPL])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT chunkname FROM fl_chunks WHERE istemplate=1 && chunkid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['template', $data['cid']]) . '">' . __('Edit the Template', 'cfBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [cfElementAudit::CREATECAT, cfElementAudit::UPDATECAT, cfElementAudit::DELETECAT])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT catname FROM fl_categories WHERE catid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['category', $data['cid']]) . '">' . __('Edit the Category', 'cfBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [cfElementAudit::CREATETHEME, cfElementAudit::UPDATETHEME, cfElementAudit::DELETETHEME])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT themename FROM fl_themes WHERE themeid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'elements', ['theme', $data['cid']]) . '">' . __('Edit the Theme', 'cfBackend') . '</a>';
			}
		}

		switch($data['action']) {
			case cfElementAudit::CREATESNIPPET:
				$msg = sprintf(__('Created a new snippet <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATESNIPPET:
				$msg = sprintf(__('Updated the content of snippet <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETESNIPPET:
				$msg = sprintf(__('Deleted snippet <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::CREATETASK:
				$msg = sprintf(__('Created a new task <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATETASK:
				$msg = sprintf(__('Updated the task <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETETASK:
				$msg = sprintf(__('Deleted task <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::CREATEFIELD:
				$msg = sprintf(__('Created a new custom field <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATEFIELD:
				$msg = sprintf(__('Updated the custom field <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETEFIELD:
				$msg = sprintf(__('Deleted the custom field <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::CREATECHUNK:
				$msg = sprintf(__('Created a new chunk <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATECHUNK:
				$msg = sprintf(__('Updated the chunk <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETECHUNK:
				$msg = sprintf(__('Deleted the chunk <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::CREATETPL:
				$msg = sprintf(__('Created a new template <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATETPL:
				$msg = sprintf(__('Updated the template <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETETPL:
				$msg = sprintf(__('Deleted the template <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::CREATECAT:
				$msg = sprintf(__('Created a new element category <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATECAT:
				$msg = sprintf(__('Updated the element category <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETECAT:
				$msg = sprintf(__('Deleted the element category <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::CREATETHEME:
				$msg = sprintf(__('Created a new theme <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::UPDATETHEME:
				$msg = sprintf(__('Updated the theme <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			case cfElementAudit::DELETETHEME:
				$msg = sprintf(__('Deleted the theme <strong>%s</strong>.', 'cfBackend'), $name) . $link;
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}
}
