Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

Vtiger_Mail_Action::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Mail action class.
5
 *
6
 * @package Action
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 * @author    Radosław Skrzypczak <[email protected]>
12
 */
13
class Vtiger_Mail_Action extends \App\Controller\Action
14
{
15
	use \App\Controller\ExposeMethod;
16
17
	/**
18
	 * Function to check permission.
19
	 *
20
	 * @param \App\Request $request
21
	 *
22
	 * @throws \App\Exceptions\NoPermitted
23
	 * @throws \App\Exceptions\NoPermittedToRecord
24
	 */
25
	public function checkPermission(App\Request $request)
26
	{
27
		$moduleModel = \Vtiger_Module_Model::getInstance($request->getModule());
28
		if (!$moduleModel || !$moduleModel->isPermitted('MassComposeEmail') || !App\Config::main('isActiveSendingMails') || !App\Mail::getDefaultSmtp()) {
0 ignored issues
show
introduced by
$moduleModel is of type Vtiger_Module_Model, thus it always evaluated to true.
Loading history...
29
			throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED', 406);
30
		}
31
		if (!$request->isEmpty('sourceRecord') && !\App\Privilege::isPermitted($request->getByType('sourceModule', 2), 'DetailView', $request->getInteger('sourceRecord'))) {
32
			throw new \App\Exceptions\NoPermittedToRecord('ERR_NO_PERMISSIONS_FOR_THE_RECORD', 406);
33
		}
34
	}
35
36
	/**
37
	 * Construct.
38
	 */
39
	public function __construct()
40
	{
41
		parent::__construct();
42
		$this->exposeMethod('sendMails');
43
	}
44
45
	/**
46
	 * Send mails.
47
	 *
48
	 * @param \App\Request $request
49
	 *
50
	 * @return void
51
	 */
52
	public function sendMails(App\Request $request): void
53
	{
54
		$moduleName = $request->getModule();
55
		$field = $request->getByType('field', 'Alnum');
56
		$sourceModule = $request->getByType('sourceModule', 'Alnum');
57
		$sourceRecord = $request->getInteger('sourceRecord');
58
		$result = false;
59
		if (!$request->isEmpty('template') && !empty($field)) {
60
			$params = [
61
				'template' => $request->getInteger('template'),
62
				'massMailNotes' => $request->getForHtml('mailNotes'),
63
			];
64
			$emails = [];
65
			foreach ($this->getQuery($request)->each() as $row) {
66
				if (isset($emails[$row[$field]])) {
67
					$emails[$row[$field]][] = $row['id'];
68
				} else {
69
					$emails[$row[$field]] = [$row['id']];
70
				}
71
			}
72
			foreach ($emails as $email => $ids) {
73
				$id = current($ids);
74
				if (isset(\App\TextParser::$sourceModules[$sourceModule]) && \in_array($moduleName, \App\TextParser::$sourceModules[$sourceModule])) {
75
					$extraParams = [
76
						'moduleName' => $sourceModule,
77
						'recordId' => $sourceRecord,
78
						'sourceModule' => $moduleName,
79
						'sourceRecord' => $id,
80
					];
81
				} else {
82
					$extraParams = [
83
						'moduleName' => $moduleName,
84
						'recordId' => $id,
85
						'sourceModule' => $sourceModule,
86
						'sourceRecord' => $sourceRecord,
87
					];
88
				}
89
				$params['to'] = $email;
90
				$params['emailIds'] = $ids;
91
				$result = \App\Mailer::sendFromTemplate(array_merge($params, $extraParams));
92
				if (!$result) {
93
					break;
94
				}
95
			}
96
		}
97
		$response = new Vtiger_Response();
98
		$response->setResult($result);
99
		$response->emit();
100
	}
101
102
	/**
103
	 * Get query instance.
104
	 *
105
	 * @param \App\Request $request
106
	 *
107
	 * @return \App\Db\Query
108
	 */
109
	public function getQuery(App\Request $request): App\Db\Query
110
	{
111
		$moduleName = $request->getModule();
112
		$sourceModule = $request->getByType('sourceModule', 2);
113
		if ($sourceModule) {
114
			$cvId = $request->isEmpty('cvId', true) ? 0 : $request->getByType('cvId', 'Alnum');
115
			$parentRecordModel = Vtiger_Record_Model::getInstanceById($request->getInteger('sourceRecord'), $sourceModule);
116
			$listView = Vtiger_RelationListView_Model::getInstance($parentRecordModel, $moduleName, $request->getInteger('relationId'), $cvId);
117
		} else {
118
			$listView = Vtiger_ListView_Model::getInstance($moduleName, $request->getByType('viewname', 2));
119
		}
120
		if (!$request->isEmpty('searchResult', true)) {
121
			$listView->set('searchResult', $request->getArray('searchResult', 'Integer'));
122
		}
123
		$searchKey = $request->getByType('search_key');
124
		$operator = $request->getByType('operator');
125
		$searchValue = $request->getByType('search_value', 'Text');
126
		if (!empty($searchKey) && !empty($searchValue)) {
127
			$listView->set('operator', $operator);
128
			$listView->set('search_key', $searchKey);
129
			$listView->set('search_value', App\Condition::validSearchValue($searchValue, $listView->getQueryGenerator()->getModule(), $searchKey, $operator));
130
		}
131
		$searchParams = App\Condition::validSearchParams($listView->getQueryGenerator()->getModule(), $request->getArray('search_params'));
132
		if (!empty($searchParams) && \is_array($searchParams)) {
133
			$transformedSearchParams = $listView->getQueryGenerator()->parseBaseSearchParamsToCondition($searchParams);
134
			$listView->set('search_params', $transformedSearchParams);
135
		}
136
		if ($advancedConditions = $request->has('advancedConditions') ? $request->getArray('advancedConditions') : []) {
137
			$listView->set('advancedConditions', \App\Condition::validAdvancedConditions($advancedConditions));
138
		}
139
		if ($sourceModule) {
140
			$queryGenerator = $listView->getRelationQuery(true);
0 ignored issues
show
Bug introduced by
The method getRelationQuery() does not exist on Vtiger_ListView_Model. ( Ignorable by Annotation )

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

140
			/** @scrutinizer ignore-call */ 
141
   $queryGenerator = $listView->getRelationQuery(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
		} else {
142
			$listView->loadListViewCondition();
0 ignored issues
show
Bug introduced by
The method loadListViewCondition() does not exist on Vtiger_RelationListView_Model. ( Ignorable by Annotation )

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

142
			$listView->/** @scrutinizer ignore-call */ 
143
              loadListViewCondition();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
			$queryGenerator = $listView->getQueryGenerator();
144
		}
145
		$moduleModel = $queryGenerator->getModuleModel();
0 ignored issues
show
Bug introduced by
The method getModuleModel() does not exist on App\Db\Query. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

145
		/** @scrutinizer ignore-call */ 
146
  $moduleModel = $queryGenerator->getModuleModel();
Loading history...
146
		$baseTableName = $moduleModel->get('basetable');
147
		$baseTableId = $moduleModel->get('basetableid');
148
		$queryGenerator->setFields(['id', $request->getByType('field', 'Alnum')]);
0 ignored issues
show
Bug introduced by
The method setFields() does not exist on App\Db\Query. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

148
		$queryGenerator->/** @scrutinizer ignore-call */ 
149
                   setFields(['id', $request->getByType('field', 'Alnum')]);
Loading history...
149
		$queryGenerator->addCondition($request->getByType('field', 'Alnum'), '', 'ny');
0 ignored issues
show
Bug introduced by
The method addCondition() does not exist on App\Db\Query. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

149
		$queryGenerator->/** @scrutinizer ignore-call */ 
150
                   addCondition($request->getByType('field', 'Alnum'), '', 'ny');
Loading history...
150
		$selected = $request->getArray('selected_ids', 2);
151
		if ($selected && 'all' !== $selected[0]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selected of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
152
			$queryGenerator->addNativeCondition(["$baseTableName.$baseTableId" => $selected]);
0 ignored issues
show
Bug introduced by
The method addNativeCondition() does not exist on App\Db\Query. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

152
			$queryGenerator->/** @scrutinizer ignore-call */ 
153
                    addNativeCondition(["$baseTableName.$baseTableId" => $selected]);
Loading history...
153
		}
154
		$excluded = $request->getArray('excluded_ids', 2);
155
		if ($excluded) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $excluded of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
156
			$queryGenerator->addNativeCondition(['not in', "$baseTableName.$baseTableId" => $excluded]);
157
		}
158
		return $queryGenerator->createQuery();
0 ignored issues
show
Bug introduced by
The method createQuery() does not exist on App\Db\Query. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

158
		return $queryGenerator->/** @scrutinizer ignore-call */ createQuery();
Loading history...
159
	}
160
}
161