Passed
Push — developer ( 37a4b6...cadaed )
by Mariusz
22:19 queued 06:19
created

EmailParser::getEmailsFromRoleModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace App;
4
5
/**
6
 * Email parser class.
7
 *
8
 * @package App
9
 *
10
 * @copyright YetiForce S.A.
11
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
12
 * @author    Mariusz Krzaczkowski <[email protected]>
13
 * @author    Radosław Skrzypczak <[email protected]>
14
 */
15
class EmailParser extends TextParser
16
{
17
	/** {@inheritdoc} */
18
	protected const BASE_FUNCTIONS = [
19
		'general', 'record', 'relatedRecord', 'sourceRecord', 'organization', 'employee', 'params', 'custom', 'userVariable', 'roleEmails'
20
	];
21
	private static $permissionToSend = [
22
		'Accounts' => 'emailoptout',
23
		'Contacts' => 'emailoptout',
24
		'Users' => 'emailoptout',
25
		'Leads' => 'noapprovalemails',
26
	];
27
	public $emailoptout = true;
28
29
	/**
30 1
	 * Check if this content can be used.
31
	 *
32 1
	 * @param \Vtiger_Field_Model $fieldModel
33 1
	 * @param string              $moduleName
34 1
	 *
35
	 * @return bool
36 1
	 */
37
	protected function useValue($fieldModel, $moduleName)
38 1
	{
39
		if ($this->emailoptout && isset(self::$permissionToSend[$moduleName])) {
40
			$checkFieldName = self::$permissionToSend[$moduleName];
41
			$permissionFieldModel = $this->recordModel->getModule()->getField($checkFieldName);
42
			return ($permissionFieldModel && $permissionFieldModel->isActiveField() && $this->recordModel->has($checkFieldName)) ? (bool) $this->recordModel->get($checkFieldName) : true;
43
		}
44
		return true;
45
	}
46 3
47
	/**
48 3
	 * Get content parsed for emails.
49 1
	 *
50
	 * @param bool $trim
51 2
	 *
52 2
	 * @return array|string
53 2
	 */
54 2
	public function getContent($trim = false)
55 2
	{
56
		if (!$trim) {
57 2
			return $this->content;
58 2
		}
59 2
		$emails = [];
60 2
		foreach (explode(',', $this->content) as $content) {
61
			$content = trim($content);
62 2
			if (empty($content) || '-' === $content) {
63
				continue;
64
			}
65 2
			if (strpos($content, '&lt;') && strpos($content, '&gt;')) {
66
				[$fromName, $fromEmail] = explode('&lt;', $content);
67
				$fromEmail = rtrim($fromEmail, '&gt;');
68
				$emails[$fromEmail] = $fromName;
69
			} else {
70
				$emails[] = $content;
71
			}
72
		}
73
		return $emails;
74
	}
75
76
	/** {@inheritdoc} */
77
	protected function relatedRecordsListPrinter(\Vtiger_RelationListView_Model $relationListView, \Vtiger_Paging_Model $pagingModel, int $maxLength): string
78
	{
79
		$relatedModuleName = $relationListView->getRelationModel()->getRelationModuleName();
80
		$rows = '';
81
		$fields = $relationListView->getHeaders();
82
		foreach ($relationListView->getEntries($pagingModel) as $relatedRecordModel) {
83
			foreach ($fields as $fieldName => $fieldModel) {
84
				if ($fieldModel && 'email' === $fieldModel->getFieldDataType() && $this->useValue($fieldModel, $relatedModuleName)) {
85
					$rows .= $relatedRecordModel->get($fieldName) . ',';
86
				}
87
			}
88
		}
89
		return rtrim($rows, ',');
90
	}
91
92
	/**
93
	 * Get users' emails belongs to role. Value 0 is for owner role level.
94
	 *
95
	 * @param string $params
96
	 *
97
	 * @return string
98
	 */
99
	public function roleEmails(string $params): string
100
	{
101
		if (!isset($this->recordModel) || !Privilege::isPermitted($this->moduleName)) {
102
			return '';
103
		}
104
		$recordOwnerId = $this->recordModel->get('assigned_user_id');
105
		if ('Groups' === Fields\Owner::getType($recordOwnerId)) {
0 ignored issues
show
introduced by
The condition 'Groups' === App\Fields\...getType($recordOwnerId) is always false.
Loading history...
106
			return '';
107
		}
108
		[$roleLevel] = array_pad(explode('|', $params, 1), 1, 0);
109
		$roleLevel = (int) $roleLevel;
110
		$usersEmails = [];
111
		$userModel = User::getUserModel($recordOwnerId);
112
		if (0 === $roleLevel) {
113
			$userRole = $userModel->getRole();
114
			$roleModel = \Settings_Roles_Record_Model::getInstanceById($userRole);
115
			$usersEmails = $this->getEmailsFromRoleModel($roleModel);
116
		} else {
117
			$userParentRoles = array_reverse($userModel->getParentRoles());
118
			$parentRolesKey = $roleLevel - 1;
119
			if (isset($userParentRoles[$parentRolesKey])) {
120
				$roleModel = \Settings_Roles_Record_Model::getInstanceById($userParentRoles[$parentRolesKey]);
121
				$usersEmails = $this->getEmailsFromRoleModel($roleModel);
122
			}
123
		}
124
		return implode(',', $usersEmails);
125
	}
126
127
	/**
128
	 * Get users' email belongs to role.
129
	 *
130
	 * @param Settings_Roles_Record_Model $roleModel
0 ignored issues
show
Bug introduced by
The type App\Settings_Roles_Record_Model was not found. Did you mean Settings_Roles_Record_Model? If so, make sure to prefix the type with \.
Loading history...
131
	 *
132
	 * @return array
133
	 */
134
	public function getEmailsFromRoleModel(\Settings_Roles_Record_Model $roleModel): array
135
	{
136
		$usersEmails = [];
137
		foreach ($roleModel->getUsers() as $userModel) {
138
			$usersEmails[] = $userModel->get('email1');
139
		}
140
		return $usersEmails;
141
	}
142
}
143