/
Module.php
412 lines (390 loc) · 12.9 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/* +***********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
* Contributor(s): YetiForce.com
* *********************************************************************************** */
/**
* Calendar Module Model Class.
*/
class Calendar_Module_Model extends Vtiger_Module_Model
{
/** {@inheritdoc} */
public $allowTypeChange = false;
/**
* Function returns the default view for the Calendar module.
*
* @return string
*/
public function getDefaultViewName()
{
return $this->getCalendarViewName();
}
/**
* Function returns the calendar view name.
*
* @return string
*/
public function getCalendarViewName()
{
$returnView = 'Calendar';
if ('Standard' !== $calendarView = App\Config::module('Calendar', 'CALENDAR_VIEW')) {
$returnView .= $calendarView;
}
return $returnView;
}
/**
* Function returns the url for Calendar view.
*
* @return string
*/
public function getCalendarViewUrl()
{
return 'index.php?module=' . $this->get('name') . '&view=' . $this->getCalendarViewName();
}
/**
* Function to check whether the module is summary view supported.
*
* @return bool
*/
public function isSummaryViewSupported()
{
return false;
}
/**
* {@inheritdoc}
*/
public function getSideBarLinks($linkParams)
{
$links = Vtiger_Link_Model::getAllByType($this->getId(), ['SIDEBARLINK', 'SIDEBARWIDGET'], $linkParams);
$links['SIDEBARLINK'][] = Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'SIDEBARLINK',
'linklabel' => 'LBL_CALENDAR_VIEW',
'linkurl' => $this->getCalendarViewUrl(),
'linkicon' => 'fas fa-calendar-alt',
]);
$links['SIDEBARLINK'][] = Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'SIDEBARLINK',
'linklabel' => 'LBL_RECORDS_LIST',
'linkurl' => $this->getListViewUrl(),
'linkicon' => 'fas fa-list',
]);
if (isset($linkParams['ACTION']) && 'Calendar' === $linkParams['ACTION'] && App\Config::module('Calendar', 'SHOW_LIST_BUTTON')) {
$links['SIDEBARLINK'][] = Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'SIDEBARLINK',
'linklabel' => 'LBL_CALENDAR_LIST',
'linkurl' => 'javascript:Calendar_Calendar_Js.getInstanceByView().goToRecordsList("' . $this->getListViewUrl() . '&viewname=All");',
'linkicon' => 'far fa-calendar-minus',
]);
}
if ($this->isPermitted('Kanban') && \App\Utils\Kanban::getBoards($this->getName(), true)) {
$links['SIDEBARLINK'][] = Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'SIDEBARLINK',
'linklabel' => 'LBL_VIEW_KANBAN',
'linkurl' => 'index.php?module=' . $this->getName() . '&view=Kanban',
'linkicon' => 'yfi yfi-kanban',
]);
}
return $links;
}
/**
* Function returns the url that shows Calendar Import result.
*
* @return string url
*/
public function getImportResultUrl()
{
return 'index.php?module=' . $this->getName() . '&view=ImportResult';
}
/**
* Function to get export query.
*
* @param mixed $focus
* @param mixed $where
*
* @return string query;
*/
public function getExportQuery($focus = '', $where = '')
{
return (new App\Db\Query())->select(['vtiger_activity.*', 'vtiger_crmentity.description', 'assigned_user_id' => 'vtiger_crmentity.smownerid', 'vtiger_activity_reminder.reminder_time'])
->from('vtiger_activity')
->innerJoin('vtiger_crmentity', 'vtiger_activity.activityid = vtiger_crmentity.crmid')
->leftJoin('vtiger_activity_reminder', 'vtiger_activity_reminder.activity_id = vtiger_activity.activityid')
->where(['vtiger_crmentity.deleted' => 0, 'vtiger_crmentity.smownerid' => App\User::getCurrentUserId()]);
}
/**
* Function to set event fields for export.
*/
public function setEventFieldsForExport()
{
$keysToReplace = ['taskpriority'];
$keysValuesToReplace = ['taskpriority' => 'priority'];
foreach ($this->getFields() as $fieldName => $fieldModel) {
if ($fieldModel->getPermissions()) {
if (!\in_array($fieldName, $keysToReplace)) {
$eventFields[$fieldName] = 'yes';
} else {
$eventFields[$keysValuesToReplace[$fieldName]] = 'yes';
}
}
}
$this->set('eventFields', $eventFields);
}
/**
* Function to set todo fields for export.
*/
public function setTodoFieldsForExport()
{
$keysToReplace = ['taskpriority', 'activitystatus'];
$keysValuesToReplace = ['taskpriority' => 'priority', 'activitystatus' => 'status'];
foreach ($this->getFields() as $fieldName => $fieldModel) {
if ($fieldModel->getPermissions()) {
if (!\in_array($fieldName, $keysToReplace)) {
$todoFields[$fieldName] = 'yes';
} else {
$todoFields[$keysValuesToReplace[$fieldName]] = 'yes';
}
}
}
$this->set('todoFields', $todoFields);
}
/**
* Function to get the url to view Details for the module.
*
* @param mixed $id
*
* @return string - url
*/
public function getDetailViewUrl($id)
{
return 'index.php?module=Calendar&view=' . $this->getDetailViewName() . '&record=' . $id;
}
/**
* Function to get Alphabet Search Field.
*/
public function getAlphabetSearchField()
{
return 'subject';
}
/**
* Function returns Calendar Reminder record models.
*
* @return \Calendar_Record_Model[]
*/
public static function getCalendarReminder()
{
$currentUserModel = Users_Record_Model::getCurrentUserModel();
$activityReminder = $currentUserModel->getCurrentUserActivityReminderInSeconds();
$recordModels = [];
if (!empty($activityReminder)) {
$time = date('Y-m-d H:i:s', strtotime("+$activityReminder seconds"));
$query = (new \App\Db\Query())
->select(['recordid', 'vtiger_activity_reminder_popup.datetime'])
->from('vtiger_activity_reminder_popup')
->innerJoin('vtiger_activity', 'vtiger_activity_reminder_popup.recordid = vtiger_activity.activityid')
->innerJoin('vtiger_crmentity', 'vtiger_activity_reminder_popup.recordid = vtiger_crmentity.crmid')
->where(['vtiger_crmentity.smownerid' => $currentUserModel->getId(), 'vtiger_crmentity.deleted' => 0, 'vtiger_activity.status' => self::getComponentActivityStateLabel('current')])
->andWhere(['or', ['and', ['vtiger_activity_reminder_popup.status' => Calendar_Record_Model::REMNDER_POPUP_ACTIVE], ['<=', 'vtiger_activity_reminder_popup.datetime', $time]], ['and', ['vtiger_activity_reminder_popup.status' => Calendar_Record_Model::REMNDER_POPUP_WAIT], ['<=', 'vtiger_activity_reminder_popup.datetime', date('Y-m-d H:i:s')]]])
->orderBy(['vtiger_activity_reminder_popup.datetime' => SORT_DESC])
->distinct()
->limit(\App\Config::module('Calendar', 'maxNumberCalendarNotifications', 20));
$dataReader = $query->createCommand()->query();
while ($recordId = $dataReader->readColumn(0)) {
$recordModels[] = Vtiger_Record_Model::getInstanceById($recordId, 'Calendar');
}
}
return $recordModels;
}
/**
* {@inheritdoc}
*/
public function getFieldsByType($type, bool $active = false): array
{
$restrictedField = ['picklist' => ['activitystatus', 'visibility', 'duration_minutes']];
if (!\is_array($type)) {
$type = [$type];
}
$fields = $this->getFields();
$fieldList = [];
foreach ($fields as $field) {
$fieldType = $field->getFieldDataType();
if (\in_array($fieldType, $type)) {
$fieldName = $field->getName();
if ('picklist' == $fieldType && \in_array($fieldName, $restrictedField[$fieldType])) {
} else {
$fieldList[$fieldName] = $field;
}
}
}
return $fieldList;
}
/**
* {@inheritdoc}
*/
public function getSettingLinks(): array
{
$currentUserModel = Users_Record_Model::getCurrentUserModel();
$settingLinks = [];
if ($currentUserModel->isAdminUser()) {
$settingLinks[] = [
'linktype' => 'LISTVIEWSETTING',
'linklabel' => 'LBL_EDIT_FIELDS',
'linkurl' => 'index.php?parent=Settings&module=LayoutEditor&sourceModule=' . $this->getName(),
'linkicon' => 'adminIcon-modules-fields',
];
$settingLinks[] = [
'linktype' => 'LISTVIEWSETTING',
'linklabel' => 'LBL_EDIT_PICKLIST_VALUES',
'linkurl' => 'index.php?parent=Settings&module=Picklist&view=Index&source_module=' . $this->getName(),
'linkicon' => 'adminIcon-fields-picklists',
];
}
return $settingLinks;
}
/**
* Function to get orderby sql from orderby field.
*
* @param mixed $orderBy
*/
public function getOrderBySql($orderBy)
{
if ('status' == $orderBy) {
return $orderBy;
}
return parent::getOrderBySql($orderBy);
}
public static function getCalendarTypes()
{
return App\Fields\Picklist::getValuesName('activitytype');
}
public static function getCalendarState($data = [])
{
if ($data) {
$activityStatus = $data['activitystatus'];
if (\in_array($activityStatus, self::getComponentActivityStateLabel('history'))) {
return false;
}
$dueDateTime = $data['due_date'] . ' ' . $data['time_end'];
$startDateTime = $data['date_start'] . ' ' . $data['time_start'];
$dates = ['start' => $startDateTime, 'end' => $dueDateTime, 'current' => null];
foreach ($dates as $key => $date) {
$date = new DateTimeField($date);
$userFormatedString = $date->getDisplayDate();
$timeFormatedString = $date->getDisplayTime();
$dBFomatedDate = DateTimeField::convertToDBFormat($userFormatedString);
$dates[$key] = strtotime($dBFomatedDate . ' ' . $timeFormatedString);
}
$activityStatusLabels = self::getComponentActivityStateLabel();
if (!empty($data['activitystatus']) && isset($activityStatusLabels[$data['activitystatus']])) {
$state = $activityStatusLabels[$data['activitystatus']];
} else {
$state = $activityStatusLabels['not_started'];
if ($dates['end'] > $dates['current'] && $dates['start'] < $dates['current']) {
$state = $activityStatusLabels['in_realization'];
} elseif ($dates['end'] > $dates['current']) {
$state = $activityStatusLabels['not_started'];
} elseif ($dates['end'] < $dates['current']) {
$state = $activityStatusLabels['overdue'];
}
}
return $state;
}
return false;
}
/**
* The function gets the labels for a given status field.
*
* @param string $key
*
* @return <Array>
*/
public static function getComponentActivityStateLabel($key = '')
{
$pickListValues = App\Fields\Picklist::getValuesName('activitystatus');
if (!\is_array($pickListValues)) {
return [];
}
$componentsActivityState = [];
foreach ($pickListValues as $value) {
switch ($value) {
case 'PLL_PLANNED':
$componentsActivityState['not_started'] = $value;
break;
case 'PLL_IN_REALIZATION':
$componentsActivityState['in_realization'] = $value;
break;
case 'PLL_COMPLETED':
$componentsActivityState['completed'] = $value;
break;
case 'PLL_POSTPONED':
$componentsActivityState['postponed'] = $value;
break;
case 'PLL_OVERDUE':
$componentsActivityState['overdue'] = $value;
break;
case 'PLL_CANCELLED':
$componentsActivityState['cancelled'] = $value;
break;
default:
break;
}
}
if ('current' == $key) {
$componentsActivityState = ['PLL_PLANNED', 'PLL_IN_REALIZATION', 'PLL_OVERDUE'];
} elseif ('history' == $key) {
$componentsActivityState = ['PLL_COMPLETED', 'PLL_POSTPONED', 'PLL_CANCELLED'];
} elseif ($key) {
return $componentsActivityState[$key];
}
return $componentsActivityState;
}
/**
* Import calendar rekords from ICS.
*
* @param string $filePath
*
* @throws \Exception
*
* @return array
*/
public function importICS(string $filePath)
{
$userId = \App\User::getCurrentUserRealId();
$lastImport = new ICalLastImport();
$lastImport->clearRecords($userId);
$eventModule = 'Events';
$todoModule = 'Calendar';
$totalCount = $skipCount = [$eventModule => 0, $todoModule => 0];
$calendar = \App\Integrations\Dav\Calendar::loadFromContent(file_get_contents($filePath));
foreach ($calendar->getRecordInstance() as $recordModel) {
$recordModel->set('assigned_user_id', $userId);
$recordModel->save();
$calendar->recordSaveAttendee($recordModel);
if ('VEVENT' === (string) $calendar->getComponent()->name) {
$module = $eventModule;
} else {
$module = $todoModule;
}
if ($recordModel->getId()) {
++$totalCount[$module];
$lastImport = new ICalLastImport();
$lastImport->setFields(['userid' => $userId, 'entitytype' => $this->getName(), 'crmid' => $recordModel->getId()]);
$lastImport->save();
} else {
++$skipCount[$module];
}
}
return ['events' => $totalCount[$eventModule] - $skipCount[$eventModule], 'skipped_events' => $skipCount[$eventModule], 'task' => $totalCount[$todoModule] - $skipCount[$todoModule], 'skipped_task' => $skipCount[$todoModule]];
}
/**
* {@inheritdoc}
*/
public function getLayoutTypeForQuickCreate(): string
{
return 'standard';
}
}