<?php

namespace Modules\Admin\ImportEntities\Model\Import;

class CompaniesBlock extends \Modules\Admin\ImportEntities\Model\ImportAbstract
{
	/**
	 *
	 * @var array
	 */
	protected $fields = ['idTina', 'blocked', 'reason'];


	public function process()
	{
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		$messagesFromImport = new \App\MessagesCollector();

		// LOOP THROUGH EACH CORPORATE ////////////////////////////////////////////////////////////
		foreach($this->fileData as $row) {
			if (strlen($row['blocked']) and in_array($row['blocked'], [1,0])) {
				try {
					\DB::startTransaction();

					$corp = \Entity\Corporate::update($row['idTina']);
					$corp->locked = $row['blocked'];
					if ($row['blocked']) {
					    $corp->lockedComments = $row['reason'];
					}
					$corp->save();

					\DB::commitTransaction();
					$messagesFromImport->addSuccess(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['blockedCompanies']['updateOk'], $row['idTina']));
				}
				catch (\App\Entity\Exception $e) {
					\DB::rollbackTransaction();
					$messagesFromImport->addWarning(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['blockedCompanies']['updateError'], $row['idTina']));
					$messagesFromImport->appendMessages($e->getMessagesCollector());
				}
			}
		}

		return $messagesFromImport;
	}
}