<?php

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

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

		$link = '';
		if($info = clearFusionCMS::getDb()->fetchRow('SELECT typename FROM cf_contenttype WHERE id=?', $data['cid'])) {
			$link = ' <a href="' . clearFusionCMS::getController()->assemble('', 'contenttypes', [$data['cid']]) . '">' . __('Edit the Type', 'cfBackend') . '</a>';
		}

		switch($data['action']) {
			case cfCtntTypeAudit::CREATED:
				$msg = sprintf(__('Created content type <strong>%s</strong>.', 'cfBackend'), $params['name']) . $link;
				break;
			case cfCtntTypeAudit::UPDATED:
				$msg = sprintf(__('Updated content type <strong>%s</strong>.', 'cfBackend'), $params['name']) . $link;
				break;
			case cfCtntTypeAudit::DELETED:
				$msg = sprintf(__('Deleted content type <strong>%s</strong>.', 'cfBackend'), $params['name']) . $link;
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}
}
