<?php
/** 
 * @package     VikAppointments
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

VAPLoader::import('libraries.mvc.model');

/**
 * VikAppointments employee area location management model.
 *
 * @since 1.7
 */
class VikAppointmentsModelEmpeditlocation extends JModelVAP
{
	/**
	 * Basic save implementation.
	 *
	 * @param 	mixed  $data  Either an array or an object of data to save.
	 *
	 * @return 	mixed  The ID of the record on success, false otherwise.
	 */
	public function save($data)
	{
		$data = (array) $data;

		$auth = VAPEmployeeAuth::getInstance();

		JFactory::getApplication()->setUserState('vap.emparea.location.data', $data);

		// get location model
		$locationModel = JModelVAP::getInstance('location');

		// automatically assign to this employee
		$data['id_employee'] = $auth->id;

		// delegate save to location model
		$id = $locationModel->save($data);

		if (!$id)
		{
			// obtain error from model
			$error = $locationModel->getError();

			if ($error)
			{
				// propagate error
				$this->setError($error);
			}

			return false;
		}

		return $id;
	}

	/**
	 * Extend delete implementation to delete any related records
	 * stored within a separated table.
	 *
	 * @param   mixed    $ids  Either the record ID or a list of records.
	 *
	 * @return 	boolean  True on success, false otherwise.
	 */
	public function delete($ids)
	{
		$auth = VAPEmployeeAuth::getInstance();

		if (!$auth->isEmployee())
		{
			throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
		}

		$result = false;

		// only int values are accepted
		$ids = array_map('intval', (array) $ids);

		// get rid of those records that do not belong to this employee
		$ids = array_values(array_filter($ids, function($id) use ($auth)
		{
			// make sure the employee can manage this record
			return $auth->manageLocations($id);
		}));

		if (!$ids)
		{
			throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403);
		}

		// delegate delete to location model
		return JModelVAP::getInstance('location')->delete($ids);
	}
}
