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

require_once('include/Webservices/Extra/WebserviceExtra.php');

class WebserviceExtraTrans extends WebserviceExtra{
	/*
	 * function to show name of the extra module
	 */
	function get_listtype($module){
		return Array(
			'label'=>getTranslatedString($module,$module),
			'singular'=>getTranslatedString("SINGLE_".$module,$module),
		);
	}
	/*
	 * function to show fields of the extra module
	 */
	function describe(){
		$entity = parent::describe();
		$entity['fields'] = Array(
			0=>Array(
				'name' => 'old_status',
				'label' => 'Old Status',
				'mandatory' => 1,
				'type' => Array(
					'name' => 'string'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 1
			),
			1=>Array(
				'name' => 'new_status',
				'label' => 'New Status',
				'mandatory' => 1,
				'type' => Array(
					'name' => 'string'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 2
			),
			2=>Array(
				'name' => 'field',
				'label' => 'Field',
				'mandatory' => 1,
				'type' => Array(
					'name' => 'string'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 3
			),
			3=>Array(
				'name' => 'entity_id',
				'label' => 'Related To',
				'mandatory' => 1,
				'type' => Array(
					'refersTo'=>Array(
						'Accounts',
					),
					'name' => 'reference'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 4
			),
			4=>Array(
				'name' => 'userid',
				'label' => 'User',
				'mandatory' => 1,
				'type' => Array(
					'name' => 'owner'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 5
			),
			5=>Array(
				'name' => 'motivation',
				'label' => 'Motivation',
				'mandatory' => 0,
				'type' => Array(
					'name' => 'string'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 6
			),
			6=>Array(
				'name' => 'changetime',
				'label' => 'Change Time',
				'mandatory' => 0,
				'type' => Array(
					'name' => 'datetime'
					),
				'nullable' => 0,
				'editable' => 0,
				'sequence' => 7
			),				
			7=>Array(
				'name' => 'id',
				'label' => 'historyid',
				'mandatory' => '',
				'type' => Array(
					'name' => 'autogenerated'
					),
				'nullable' => '',
				'editable' => '',
				'default' => ''
			),				
		);
		return $entity;
	}
	/*
	 * function to map table fields to logical fields logical field => real field
	 */	
	function columnMapping(){
		return Array(
			'old_status'=>'old_status',
			'new_status'=>'new_status',
			'field'=>'field',
			'entity_id'=>'entity_id',
			'userid'=>'userid',
			'motivation'=>'motivation',
			'changetime'=>'changetime',
			'id'=>'historyid',
		);
	}
	/*
	 * function to return variables to construct the query:
	 * $function => Array with key: name of the field related to another module (not extra), value: name of related module
	 * $add_fields_arr => Array value: field to add to select statement on every query (used to extract related module name for $function parameter)
	 * $replace => subquery that will replace module name of the query
	 */
	function query_parameters(&$function,&$add_fields_arr,&$replace){
		global $table_prefix;
		$add_fields_arr[] = 'name';
		$function['entity_id'] = 'name';
		$replace = " (select h.*,t.name from tbl_s_transitions_history h
		inner join {$table_prefix}_tab t on t.tabid = h.tabid
		inner join {$table_prefix}_ws_entity e on e.name = t.name) t";
	}
	/*
	 * function to return variables to construct retrieve function:
	 * $params => Array that contains parameters with conditions
	 * $q => String query to retrieve records
	 * $fields => Array key: field, value: if fixed "function" -> look at function the id of the module related, else Integer: id of the module related
	 * $function => Array with key: name of the field related to another module (not extra), value: name of the field in select that contains name of related module
	 */
	function retrieve_parameters($id,$module_ids,&$params,&$q,&$fields,&$function){
		global $table_prefix;
		$q = "select * from (select h.*,t.name from tbl_s_transitions_history h
		inner join {$table_prefix}_tab t on t.tabid = h.tabid
		inner join {$table_prefix}_ws_entity e on e.name = t.name) t where historyid = ?";
		$params[] = $id;
		$fields['entity_id'] = 'function';
		$function['entity_id'] = 'name';
		$fields['userid'] = $module_ids['Users'];
	}
	/*
	 * function to return if a extra module is related to normal module
	 */
	function relatedto($module,&$rm){
		$relation = false;
		global $current_user,$table_prefix,$adb;
		if (vtlib_isModuleActive($this->name)){
			$transitions_obj = CRMEntity::getInstance($this->name);
			$transitions_obj->Initialize($module,$current_user->roleid);
			if ($transitions_obj->ismanaged_global()){
				//module related to transitions!
				$newrel = new ModuleRelation($module,$this->name, ModuleRelation::$TYPE_1TON);
				$newrel->fieldtable = $transitions_obj->history_table;
				$relres = $adb->pquery("select relation_id from {$table_prefix}_relatedlists where tabid = ? and related_tabid = ?", array(getTabid($module), getTabid($this->name)));
				if ($relres && $adb->num_rows($relres) == 1) {
					$newrel->relationid = $adb->query_result_no_html($relres, 0, 'relation_id');
				}
				// crmv@43864e
				$relation = $newrel;
				if ($saveCache) $rm->addRelationToCache($newrel);
			}
		}
		return $relation;
	}
	/*
	 * function to get related ids
	 */
	function getRelatedIds($module,$relmodule,$crmid, $start, $limit, $onlycount){
		global $current_user,$adb;
		$return_arr = Array();
		$transitions_obj = CRMEntity::getInstance($relmodule);
		$transitions_obj->Initialize($module,$current_user->roleid);
		if ($onlycount){
			$sql = "select count(*) as count from {$transitions_obj->history_table} where entity_id = ? and tabid = ? and field = ?";
			$params = Array($crmid,getTabId($transitions_obj->modulename),$transitions_obj->status_field);
			$res = $adb->pquery($sql,$params);
			if ($res){
				return $adb->query_result_no_html($res,0,'count');
			}
		}
		$sql = "select historyid from {$transitions_obj->history_table} where entity_id = ? and tabid = ? and field = ?";
		$params = Array($crmid,getTabId($transitions_obj->modulename),$transitions_obj->status_field);
		$res = $adb->pquery($sql,$params);
		$return_arr = Array();
		if ($res && $adb->num_rows($res)>0){
			while ($row = $adb->fetchByAssoc($res,-1,false)){
				$return_arr[] = $row['historyid'];
			}
		}
		return $return_arr;
	}
}
?>