<?php
/*************************************
 * SPDX-FileCopyrightText: 2009-2020 Vtenext S.r.l. <info@vtenext.com> 
 * SPDX-License-Identifier: AGPL-3.0-only  
 ************************************/

abstract class WebserviceEntityOperation{
	protected $user;
	protected $log;
	protected $webserviceObject;
	protected $meta;
	/**
	 *
	 * @var PearDatabase
	 */
	protected $pearDB;

	protected static $metaCache = array();

	protected function __construct($webserviceObject,$user,$adb,$log){
		$this->user = $user;
		$this->log = $log;
		$this->webserviceObject = $webserviceObject;
		$this->pearDB = $adb;
	}

	public function create($elementType,$element){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation Create is not supported for this entity");
	}

	public function retrieve($id){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation Retrieve is not supported for this entity");
	}

	public function update($element){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation Update is not supported for this entity");
	}

	public function revise($element){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation Update is not supported for this entity");
	}

	public function delete($id){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation delete is not supported for this entity");
	}

	public function query($q){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation query is not supported for this entity");
	}

	public function describe($elementType){
		throw new WebServiceException(WebServiceErrorCode::$OPERATIONNOTSUPPORTED,
		"Operation describe is not supported for this entity");
	}

	function getFieldTypeDetails($webserviceField){
		global $upload_maxsize;
		$typeDetails = array();
		switch($webserviceField->getFieldDataType()){
			case 'reference': $typeDetails['refersTo'] = $webserviceField->getReferenceList();
				break;
			case 'picklistmultilanguage': //crmv@34374
			case 'multipicklist':
			case 'picklist':
				// crmv@78052
				$typeDetails["picklistValues"] = $webserviceField->getPicklistDetails();
				$typeDetails['defaultValue'] = $typeDetails["picklistValues"][0]['value'];
				if ($webserviceField->getUIType() == 300) {
					$typeDetails['linkedPicklistDetails'] = $webserviceField->getLinkedPicklistDetails();
				}
				// crmv@78052
				break;
			case 'file': $maxUploadSize = 0;
				$maxUploadSize = ini_get('upload_max_filesize');
				$maxUploadSize = strtolower($maxUploadSize);
				$maxUploadSize = explode('m',$maxUploadSize);
				$maxUploadSize = $maxUploadSize[0];
				if(!is_numeric($maxUploadSize)){
					$maxUploadSize = 0;
				}
				$maxUploadSize = $maxUploadSize * 1000000;
				if($upload_maxsize > $maxUploadSize){
					$maxUploadSize = $upload_maxsize;
				}
				$typeDetails['maxUploadFileSize'] = $maxUploadSize;
				break;
			// crmv@31780
			case 'currency':
				$currid = 1;
				if (!empty($this->user)) {
					$currid =$this->user->column_fields['currency_id'];
				}
				list($symb_name, $symb) = explode(' : ', getCurrencyName($currid), 2);
				$typeDetails['symbol'] = $symb;
				$typeDetails['symbol_name'] = $symb_name;
				break;
			// crmv@31780e
			case 'date': $typeDetails['format'] = $this->user->date_format;
		}
		return $typeDetails;
	}

	function isEditable($webserviceField){
		if(((int)$webserviceField->getDisplayType()) === 2 || strcasecmp($webserviceField->getFieldDataType(),"autogenerated")
			===0 || strcasecmp($webserviceField->getFieldDataType(),"id")===0 || $webserviceField->isReadOnly()) { // crmv@33097
			return false;
		}
		//uitype 70 is vte generated fields, such as (of vte_crmentity table) createdtime
		//and modified time fields.
		if($webserviceField->getUIType() ==  70 || $webserviceField->getUIType() ==  4){
			return false;
		}
		return true;
	}

	function getIdField($label){
		return array('name'=>'id','label'=>$label,'mandatory'=>false,'type'=>'id','editable'=>false,'type'=>
						array('name'=>'autogenerated'),'nullable'=>false,'default'=>"");
	}

	/**
	 * @return Intance of EntityMeta class.
	 *
	 */
	abstract public function getMeta();
	abstract protected  function getMetaInstance();

}

?>