Test Setup Failed
Push — developer ( 9c2d40...d3e2a7 )
by Radosław
19:30
created

CRMEntity::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/* * *******************************************************************************
4
* The contents of this file are subject to the SugarCRM Public License Version 1.1.2
5
* ("License"); You may not use this file except in compliance with the
6
* License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
7
* Software distributed under the License is distributed on an  "AS IS"  basis,
8
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9
* the specific language governing rights and limitations under the License.
10
* The Original Code is:  SugarCRM Open Source
11
* The Initial Developer of the Original Code is SugarCRM, Inc.
12
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.;
13
* All Rights Reserved.
14
* Contributor(s): YetiForce S.A.
15
* ****************************************************************************** */
16
/* * *******************************************************************************
17
 * $Header: /advent/projects/wesat/vtiger_crm/vtigercrm/data/CRMEntity.php,v 1.16 2005/04/29 04:21:31 mickie Exp $
18
 * Description:  Defines the base class for all data entities used throughout the
19
 * application.  The base class including its methods and variables is designed to
20
 * be overloaded with module-specific methods and variables particular to the
21
 * module's base entity class.
22
 * ****************************************************************************** */
23
require_once 'include/utils/CommonUtils.php';
24
require_once 'include/fields/DateTimeField.php';
25
require_once 'include/fields/DateTimeRange.php';
26
require_once 'include/fields/CurrencyField.php';
27
include_once 'modules/Vtiger/CRMEntity.php';
28
require_once 'include/runtime/Cache.php';
29
require_once 'modules/Vtiger/helpers/Util.php';
30
require_once 'modules/PickList/DependentPickListUtils.php';
31
require_once 'modules/Users/Users.php';
32
require_once 'include/Webservices/Utils.php';
33
34
class CRMEntity
35
{
36
	/** @var string Table name */
37
	public $table_name = '';
38
	/** @var string Table index */
39 15
	public $table_index = '';
40
	/** @var array Mandatory table for supporting custom fields. */
41 15
	public $customFieldTable = [];
42 15
	/** @var string[] Mandatory for Saving, Include tables related to this module */
43
	public $tab_name = [];
44 5822
	/** @var array */
45
	public $list_fields_name = [];
46 5822
	/** @var array For Popup listview and UI type support */
47
	public $search_fields = [];
48
	/** @var array */
49 5822
	public $search_fields_name = [];
50 5798
	/** @var string[] For Popup window record selection */
51
	public $popup_fields = [];
52
	/** @var string Field name For Alphabetical search. */
53
	public $def_basicsearch_col = '';
54 50
	/** @var string[] */
55 1
	public $mandatory_fields = [];
56
	/** @var array Mandatory for saving, Include tablename and tablekey columnname here. */
57
	public $tab_name_index = [];
58
	/** @var string Default order by. */
59 1
	public $default_order_by = '';
60 1
	/** @var string Default sort order. */
61
	public $default_sort_order = '';
62
	/** @var string[] Tables join clause. */
63 50
	public $tableJoinClause = [
64 50
		'vtiger_entity_stats' => 'LEFT JOIN',
65 50
		'u_yf_openstreetmap' => 'LEFT JOIN',
66
		'u_yf_wapro_records_map' => 'LEFT JOIN',
67 50
	];
68
69
	/** @var array Column fields */
70
	public $column_fields = [];
71
	/** @var array Lock fields */
72
	protected $lockFields = [];
73
74
	/**
75
	 * Constructor which will set the column_fields in this object.
76
	 */
77
	public function __construct()
78
	{
79
		$this->column_fields = vtlib\Deprecated::getColumnFields(static::class);
80
	}
81
82
	/**
83
	 * Get module name.
84
	 *
85
	 * @return string
86
	 */
87
	public function getName(): string
88 18
	{
89
		return static::class;
90 18
	}
91
92
	/**
93
	 * Get CRMEntity instance.
94
	 *
95
	 * @param string $module
96
	 *
97
	 * @return self
98
	 */
99 18
	public static function getInstance(string $module)
100
	{
101 18
		if (is_numeric($module)) {
102
			$module = App\Module::getModuleName($module);
0 ignored issues
show
Bug introduced by
$module of type string is incompatible with the type integer expected by parameter $tabId of App\Module::getModuleName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
			$module = App\Module::getModuleName(/** @scrutinizer ignore-type */ $module);
Loading history...
103
		}
104
		if (\App\Cache::staticHas('CRMEntity', $module)) {
105
			return clone \App\Cache::staticGet('CRMEntity', $module);
106
		}
107 18
		// File access security check
108 18
		if (!class_exists($module)) {
109
			if (App\Config::performance('LOAD_CUSTOM_FILES') && file_exists("custom/modules/$module/$module.php")) {
110
				\vtlib\Deprecated::checkFileAccessForInclusion("custom/modules/$module/$module.php");
111
				require_once "custom/modules/$module/$module.php";
112 18
			} else {
113
				\vtlib\Deprecated::checkFileAccessForInclusion("modules/$module/$module.php");
114
				require_once "modules/$module/$module.php";
115
			}
116 18
		}
117 18
		$focus = new $module();
118 18
		if (method_exists($focus, 'init')) {
119 18
			$focus->init();
120 18
		}
121
		\App\Cache::staticSave('CRMEntity', $module, clone $focus);
122 18
		return $focus;
123 18
	}
124
125
	/**
126
	 * Retrieve record information of the module.
127
	 *
128 18
	 * @param int    $record - crmid of record
129
	 * @param string $module - module name
130 18
	 */
131 18
	public function retrieveEntityInfo(int $record, string $module)
132 18
	{
133 18
		if (!isset($record)) {
134 18
			throw new \App\Exceptions\NoPermittedToRecord('LBL_RECORD_NOT_FOUND');
135 18
		}
136 18
		if ($cachedModuleFields = \App\Field::getModuleFieldInfosByPresence($module)) {
137
			$query = new \App\Db\Query();
138
			$tabNameIndex = $this->tab_name_index; // copies-on-write
139
			$requiredTables = $columnClause = [];
140 18
			foreach ($cachedModuleFields as $fieldInfo) {
141
				if (isset($tabNameIndex[$fieldInfo['tablename']])) {
142
					if (!isset($requiredTables[$fieldInfo['tablename']])) {
143 18
						$requiredTables[$fieldInfo['tablename']] = $tabNameIndex[$fieldInfo['tablename']];
144 18
					}
145 18
					// Alias prefixed with tablename+fieldname to avoid duplicate column name across tables
146
					// fieldname are always assumed to be unique for a module
147 18
					$columnClause[] = $fieldInfo['tablename'] . '.' . $fieldInfo['columnname'] . ' AS ' . $this->createColumnAliasForField($fieldInfo);
148 18
				}
149
			}
150
			$columnClause[] = 'vtiger_crmentity.deleted';
151 18
			$query->select($columnClause);
152 18
			$query->from('vtiger_crmentity');
153 18
			if (isset($requiredTables['vtiger_crmentity'])) {
154
				unset($requiredTables['vtiger_crmentity']);
155 18
			}
156 18
			foreach ($requiredTables as $tableName => $tableIndex) {
157
				$query->leftJoin($tableName, "vtiger_crmentity.crmid = $tableName.$tableIndex");
158 18
			}
159 18
			$query->where(['vtiger_crmentity.crmid' => $record]);
160 18
			if ('' != $module) {
161 18
				$query->andWhere(['vtiger_crmentity.setype' => $module]);
162
			}
163
			$resultRow = $query->one();
164 18
			if (empty($resultRow)) {
165
				throw new \App\Exceptions\NoPermittedToRecord('ERR_RECORD_NOT_FOUND||' . $record);
166
			}
167 18
			foreach ($cachedModuleFields as $fieldInfo) {
168 18
				$fieldvalue = '';
169 18
				$fieldkey = $this->createColumnAliasForField($fieldInfo);
170
				// Note : value is retrieved with a tablename+fieldname as we are using alias while building query
171
				if (isset($resultRow[$fieldkey])) {
172
					$fieldvalue = $resultRow[$fieldkey];
173
				}
174
				if (120 === $fieldInfo['uitype']) {
175
					$fieldvalue = \App\Fields\SharedOwner::getById($record);
176 3
					if (\is_array($fieldvalue)) {
177
						$fieldvalue = implode(',', $fieldvalue);
178 3
					}
179
				}
180
				$this->column_fields[$fieldInfo['fieldname']] = $fieldvalue;
181 3
			}
182
		}
183
		$this->column_fields['record_id'] = $record;
184 3
		$this->column_fields['record_module'] = $module;
185
	}
186
187
	/**
188
	 * Get table join clause by table name.
189
	 *
190
	 * @param string $tableName
191
	 *
192
	 * @return string
193
	 */
194
	public function getJoinClause($tableName): string
195
	{
196
		if (strripos($tableName, 'rel') === (\strlen($tableName) - 3)) {
197
			return 'LEFT JOIN';
198
		}
199
		if (isset($this->tableJoinClause[$tableName])) {
200
			return $this->tableJoinClause[$tableName];
201
		}
202
		return 'INNER JOIN';
203
	}
204
205
	/**
206
	 * Function to get the relation tables for related modules.
207
	 *
208
	 * @param string $secModule - $secmodule secondary module name
209
	 *
210
	 * @return array returns the array with table names and fieldnames storing relations
211
	 *               between module and this module
212
	 */
213
	public function setRelationTables($secModule = false)
214
	{
215
		$relTables = [
216
			'Documents' => [
217
				'vtiger_senotesrel' => ['crmid', 'notesid'],
218
				$this->table_name => $this->table_index,
219
			],
220
			'OSSMailView' => [
221
				'vtiger_ossmailview_relation' => ['crmid', 'ossmailviewid'],
222
				$this->table_name => $this->table_index,
223
			],
224
		];
225
		if (false === $secModule) {
226
			return $relTables;
227
		}
228
		return $relTables[$secModule] ?? [];
229
	}
230
231
	/**
232
	 * Function to track when a new record is linked to a given record.
233
	 *
234
	 * @param mixed $crmId
235
	 */
236
	public static function trackLinkedInfo($crmId)
237
	{
238
		$currentTime = date('Y-m-d H:i:s');
239
		\App\Db::getInstance()->createCommand()->update('vtiger_crmentity', ['modifiedtime' => $currentTime, 'modifiedby' => \App\User::getCurrentUserId()], ['crmid' => $crmId])->execute();
240
	}
241
242
	/**
243
	 * Function to track when a record is unlinked to a given record.
244
	 *
245
	 * @param int $crmId
246
	 */
247
	public function trackUnLinkedInfo($crmId)
248
	{
249
		static::trackLinkedInfo($crmId);
250
	}
251
252
	/**
253
	 * Gets fields to locking record.
254
	 *
255
	 * @return array
256
	 */
257
	public function getLockFields()
258
	{
259
		return $this->lockFields;
260
	}
261
262
	/**
263
	 * Invoked when special actions are performed on the module.
264
	 *
265
	 * @param string $moduleName Module name
266
	 * @param string $eventType  Event Type (module.postinstall, module.disabled, module.enabled, module.preuninstall)
267
	 */
268
	public function moduleHandler($moduleName, $eventType)
269
	{
270
		if ($moduleName && 'module.postinstall' === $eventType) {
271
		} elseif ('module.disabled' === $eventType) {
272
		} elseif ('module.preuninstall' === $eventType) {
273
		} elseif ('module.preupdate' === $eventType) {
274
		} elseif ('module.postupdate' === $eventType) {
275
		}
276
	}
277
278
	/**
279
	 * Loading the system configuration.
280
	 *
281
	 * @return void
282
	 */
283
	protected function init(): void
284
	{
285
		$this->tab_name_index += ['u_yf_wapro_records_map' => 'crmid'];
286
	}
287
288
	/**
289
	 * Function returns the column alias for a field.
290
	 *
291
	 * @param array $fieldInfo - field information
292
	 *
293
	 * @return string field value
294
	 */
295
	protected function createColumnAliasForField(array $fieldInfo)
296
	{
297
		return strtolower($fieldInfo['tablename'] . $fieldInfo['fieldname']);
298
	}
299
}
300