<?php
namespace Modules\Partners\Customers\Profile;

class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{

	/**
	 * (non-PHPdoc)
	 *
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$entityCustomerProfile = \App\EntityPreference\Customer::getInstance($R['id']);

		$formData = [];
		$formData['id'] = $entityCustomerProfile->getId();
		$formData['farePreference'] = $entityCustomerProfile->farePreference;
		$formData['seatPreference'] = $entityCustomerProfile->seatPreference;
		$formData['mealPreference'] = $entityCustomerProfile->mealPreference;
		$formData['carRental'] = $entityCustomerProfile->carRental;
		$formData['accommodationPreference'] = explodeClean('|', $entityCustomerProfile->accommodationPreference);
		$formData['supplierServicesText'] = $entityCustomerProfile->supplierServicesText;

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 *
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		// Submit
		try {
			$closePopup = false;
			\DB::startTransaction();

			// save general preferences (select, radio, etc)
			foreach (['farePreference','seatPreference','mealPreference','carRental','accommodationPreference'] as $profileKey) {
				if ($profileKey == "accommodationPreference") {
					if (is_array($R['profile'][$profileKey])) {
						$thisProfileValue = '|' . implode('|', $R['profile'][$profileKey]) . '|';
					}
					else {
						$thisProfileValue = NULL;
					}
				}
				else {
					$thisProfileValue = $R['profile'][$profileKey];
				}

				\App\EntityPreference\Customer::getInstance($R['id'])->{$profileKey} = $thisProfileValue ?: null;
			}

			// save preferences text - customersPreferences - table
			\App\EntityPreference\Customer::getInstance($R['id'])->supplierServicesText = $R['supplierServicesText'] ?: null;

			// UNLINK/UPLOAD FILES ARRAY
			$filesWithPath = [];
			$unlinkFiles = [];

			foreach ($_FILES as $fileCode => $fileDetails) {

				// if file was NOT supposed to be kept
				if (! $R[$fileCode . '_keepFile']) {
					// unlink the file (if it exists)
					if ($R['files'][$fileCode]) {
						$unlinkFiles[] = $R['files'][$fileCode];
					}
					// new file added
					$uploadedFile = new \App\Form\UploadedFiles\File($fileCode);
					if ($uploadedFile->hasValidFile()) {
						$filesWithPath[$uploadedFile->getName()] = $uploadedFile->getTempName();
					}

				}
			}

			// Save customer files
			$customer = \Entity\Customer::update($R['id']);
			$customer->unlinkFiles = $unlinkFiles;
			$customer->filesWithPath = $filesWithPath;
			$customer->save();

			\DB::commitTransaction();
			$msgs->addSuccess($this->lang['messages']['profileServices_saveSuccess']);
			$closePopup = true;
		}
		catch (\App\Entity\Exception $e) {
			\DB::rollbackTransaction();
			$msgs->appendMessages($e->getMessagesCollector());
		}

		// render the view
		return $closePopup;
	}
}