<?php

/**
 * Class file for decoding forms module 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\module\forms
 * @copyright Copyright (c) 2017 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

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

		$title = isset($params['title']) ? $params['title'] : '';

		$link = '';
		if($info = clearFusionCMS::getDb()->fetchRow('SELECT formid FROM cf_forms WHERE formid=?', $data['cid'])) {
			$link = ' <a href="' . clearFusionCMS::getController()->assemble('forms', 'editor', ['edit',$data['cid']]) . '">' . __('Edit the Form', 'formsBackend') . '</a>';
		}

		switch($data['action']) {
			case formsAudit::CREATE:
				$msg = sprintf(__('Created form <strong>%s</strong>.', 'formsBackend'), $title) . $link;
				break;
			case formsAudit::UPDATE:
				$msg = sprintf(__('Modified form <strong>%s</strong>.', 'formsBackend'), $title) . $link;
				break;
			case formsAudit::DELETE:
				$msg = sprintf(__('Deleted form <strong>%s</strong>.', 'formsBackend'), $title);
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}

	/**
	 * @inheritdoc
	 */
	function hasAdditionalData($data) {
		return !in_array($data['action'], [ formsAudit::CREATE ]);
	}
}
