<?php defined('BASEPATH') OR exit('No direct script access allowed');

require_once APPPATH . 'processors/Base_processor.php';

class TransFieldsProcessor extends Base_processor
{
	public function post_save(&$data, &$result)
	{
		// store custom fields data
		if (empty($data['fields']))
		{
			return;
		}

		$tmp_fields = array();

		foreach($data['fields'] as $key => $row)
		{
			if ($row["fName"] == "obtoken")
			{
				continue;
			}

			if (!empty($row['fId']) && !empty(TransFlag::$customFieldGrid["cf_" . $row['fId']]))
			{
				$row['fValue'] = json_encode(TransFlag::$customFieldGrid["cf_" . $row['fId']]);
				unset(TransFlag::$customFieldGrid["cf_" . $row['fId']]);
			}
			else if (!empty($row["isGrid"]))
			{
				unset($row['fValue']);
				continue;
			}

			if (empty($row['fValue']))
			{
				continue;
			}

			$row['accId'] = !empty($row['accId']) ? $row['accId'] : "";
			$row['accCode'] = !empty($row['accCode']) ? $row['accCode'] : "";
			$row['transId'] = $data["transId"];
			$row = $this->strip_fields($row, array('transId','fId','fName','fValue'));

			$tmp_fields[] = $row;
		}

		if (!empty($tmp_fields))
		{
			$this->CI->db->insert_batch('trans_fields', $tmp_fields);
		}
	}
}
