<?php

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

/**
 * propertyAuditHelper Class
 *
 * Class to provide decoding for property audit trails.
 *
 * @package clearFusionCMS\module\blog
 */
class propertyAuditHelper 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(in_array($data['action'], [propertyAudit::CREATEFEATURE, propertyAudit::UPDATEFEATURE, propertyAudit::DELETEFEATURE, propertyAudit::MOVEFEATURE])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT featureid FROM cf_property_features_' . clearFusionCMS::getSite() . ' WHERE featureid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'feature', ['edit', $data['cid']]) . '">' . __('Edit the Feature', 'propertyBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [propertyAudit::CREATEFIELD, propertyAudit::UPDATEFIELD, propertyAudit::DELETEFIELD])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT fldid FROM cf_property_flds_' . clearFusionCMS::getSite() . ' WHERE fldid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'fields', ['edit', $data['cid']]) . '">' . __('Edit the Field', 'propertyBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [propertyAudit::CREATERGNFIELD, propertyAudit::UPDATERGNFIELD, propertyAudit::DELETERGNFIELD])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT rgnfldid FROM cf_property_rgnflds_' . clearFusionCMS::getSite() . ' WHERE rgnfldid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'fields', ['edit', $data['cid']]) . '">' . __('Edit the Field', 'propertyBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [propertyAudit::CREATEREGION, propertyAudit::UPDATEREGION, propertyAudit::DELETEREGION, propertyAudit::MOVEREGION])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT regionid FROM cf_property_region_' . clearFusionCMS::getSite() . ' WHERE regionid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'region', ['edit', $data['cid']]) . '">' . __('Edit the Region', 'propertyBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [propertyAudit::CREATETYPE, propertyAudit::UPDATETYPE, propertyAudit::DELETETYPE, propertyAudit::MOVETYPE])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT typeid FROM cf_property_type_' . clearFusionCMS::getSite() . ' WHERE typeid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'type', ['edit', $data['cid']]) . '">' . __('Edit the Property Type', 'propertyBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [propertyAudit::CREATEPROPERY, propertyAudit::UPDATEPROPERTY, propertyAudit::DELETEPROPERTY])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT propertyid FROM cf_property_' . clearFusionCMS::getSite() . ' WHERE propertyid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'edit', [$data['cid']]) . '">' . __('Edit the Property', 'propertyBackend') . '</a>';
			}
		}
		else if(in_array($data['action'], [propertyAudit::CREATEGALLERY, propertyAudit::UPDATEGALLERY, propertyAudit::DELETEGALLERY])) {
			if($info = clearFusionCMS::getDb()->fetchRow('SELECT propertyid FROM cf_property_gal_' . clearFusionCMS::getSite() . ' WHERE galid=?', $data['cid'])) {
				$link = ' <a href="' . clearFusionCMS::getController()->assemble('property', 'gallery', [$info['propertyid'], 'edit', $data['cid']]) . '">' . __('Edit the Image', 'propertyBackend') . '</a>';
			}
		}

		switch($data['action']) {
			case propertyAudit::CREATEFEATURE:
				$msg = sprintf(__('Created a new property feature <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::UPDATEFEATURE:
				$msg = sprintf(__('Modified the property feature <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::DELETEFEATURE:
				$msg = sprintf(__('Deleted the property feature <strong>%s</strong>.', 'propertyBackend'), $title);
				break;
			case propertyAudit::MOVEFEATURE:
				$msg = sprintf(__('Moved the property feature <strong>%1s</strong> from <em>%2s</em> %3s <em>%4s</em>.', 'propertyBackend'), $title, $params['origPath'], $params['position'], $params['newPath']) . $link;
				break;

			case propertyAudit::CREATEFIELD:
				$msg = sprintf(__('Created a new property field <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::UPDATEFIELD:
				$msg = sprintf(__('Modified the property field <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::DELETEFIELD:
				$msg = sprintf(__('Deleted the property field <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;

			case propertyAudit::CREATERGNFIELD:
				$msg = sprintf(__('Created a new property region field <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::UPDATERGNFIELD:
				$msg = sprintf(__('Modified the property region field <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::DELETERGNFIELD:
				$msg = sprintf(__('Deleted the property region field <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;

			case propertyAudit::CREATEREGION:
				$msg = sprintf(__('Created a property region <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::UPDATEREGION:
				$msg = sprintf(__('Modified the property region <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::DELETEREGION:
				$msg = sprintf(__('Deleted the property region <strong>%s</strong>.', 'propertyBackend'), $title);
				break;
			case propertyAudit::MOVEREGION:
				$msg = sprintf(__('Moved the property region <strong>%1s</strong> from <em>%2s</em> %3s <em>%4s</em>.', 'propertyBackend'), $title, $params['origPath'], $params['position'], $params['newPath']) . $link;
				break;

			case propertyAudit::CREATETYPE:
				$msg = sprintf(__('Created a new property type <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::UPDATETYPE:
				$msg = sprintf(__('Modified the property type <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::DELETETYPE:
				$msg = sprintf(__('Deleted the property type <strong>%s</strong>.', 'propertyBackend'), $title);
				break;
			case propertyAudit::MOVETYPE:
				$msg = sprintf(__('Moved property type <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;

			case propertyAudit::CREATEPROPERY:
				$msg = sprintf(__('Created a new property <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::UPDATEPROPERTY:
				$msg = sprintf(__('Modified the property <strong>%s</strong>.', 'propertyBackend'), $title) . $link;
				break;
			case propertyAudit::DELETEPROPERTY:
				$msg = sprintf(__('Deleted the property <strong>%s</strong>.', 'propertyBackend'), $title);
				break;

			case propertyAudit::CREATEGALLERY:
				$msg = sprintf(__('Created gallery image <strong>%1s</strong> for property <strong>%2s</strong>.', 'propertyBackend'), $title, $params['property']) . $link;
				break;
			case propertyAudit::UPDATEGALLERY:
				$msg = sprintf(__('Updated gallery image <strong>%1s</strong> for property <strong>%2s</strong>.', 'propertyBackend'), $title, $params['property']) . $link;
				break;
			case propertyAudit::DELETEGALLERY:
				$msg = sprintf(__('Deleted gallery image <strong>%1s</strong> from property <strong>%2s</strong>.', 'propertyBackend'), $title, $params['property']) . $link;
				break;

			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}

	/**
	 * @inheritdoc
	 */
	function hasAdditionalData($data) {
		return !in_array($data['action'], [ propertyAudit::CREATEFEATURE, propertyAudit::MOVEFEATURE, propertyAudit::CREATEFIELD, propertyAudit::CREATERGNFIELD, propertyAudit::CREATEREGION, propertyAudit::MOVEREGION, propertyAudit::CREATETYPE, propertyAudit::MOVETYPE, propertyAudit::CREATEPROPERY, propertyAudit::CREATEGALLERY ]);
	}
}
