Newer
Older
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
<?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.
*************************************************************************************/
vimport ('~~/include/Webservices/Query.php');
class Calendar_Feed_Action extends Vtiger_BasicAjax_Action {
public function process(Vtiger_Request $request) {
try {
$result = array();
$start = $request->get('start');
$end = $request->get('end');
$type = $request->get('type');
$userid = $request->get('userid');
$color = $request->get('color');
$textColor = $request->get('textColor');
switch ($type) {
case 'Events': $this->pullEvents($start, $end, $result,$userid,$color,$textColor); break;
case 'Calendar': $this->pullTasks($start, $end, $result,$color,$textColor); break;
case 'Potentials': $this->pullPotentials($start, $end, $result, $color, $textColor); break;
case 'Contacts':
if($request->get('fieldname') == 'support_end_date') {
$this->pullContactsBySupportEndDate($start, $end, $result, $color, $textColor);
}else{
$this->pullContactsByBirthday($start, $end, $result, $color, $textColor);
}
break;
case 'Invoice': $this->pullInvoice($start, $end, $result, $color, $textColor); break;
case 'MultipleEvents' : $this->pullMultipleEvents($start,$end, $result,$request->get('mapping'));break;
case 'Project': $this->pullProjects($start, $end, $result, $color, $textColor); break;
case 'ProjectTask': $this->pullProjectTasks($start, $end, $result, $color, $textColor); break;
}
echo json_encode($result);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
protected function getGroupsIdsForUsers($userId) {
vimport('~~/include/utils/GetUserGroups.php');
$userGroupInstance = new GetUserGroups();
$userGroupInstance->getAllUserGroups($userId);
return $userGroupInstance->user_groups;
}
protected function queryForRecords($query, $onlymine=true) {
$user = Users_Record_Model::getCurrentUserModel();
if ($onlymine) {
$groupIds = $this->getGroupsIdsForUsers($user->getId());
$groupWsIds = array();
foreach($groupIds as $groupId) {
$groupWsIds[] = vtws_getWebserviceEntityId('Groups', $groupId);
}
$userwsid = vtws_getWebserviceEntityId('Users', $user->getId());
$userAndGroupIds = array_merge(array($userwsid),$groupWsIds);
$query .= " AND assigned_user_id IN ('".implode("','",$userAndGroupIds)."')";
}
// TODO take care of pulling 100+ records
return vtws_query($query.';', $user);
}
protected function pullEvents($start, $end, &$result, $userid = false,$color = null,$textColor = 'white') {
$dbStartDateOject = DateTimeField::convertToDBTimeZone($start);
$dbStartDateTime = $dbStartDateOject->format('Y-m-d H:i:s');
$dbStartDateTimeComponents = explode(' ', $dbStartDateTime);
$dbStartDate = $dbStartDateTimeComponents[0];
$dbEndDateObject = DateTimeField::convertToDBTimeZone($end);
$dbEndDateTime = $dbEndDateObject->format('Y-m-d H:i:s');
$currentUser = Users_Record_Model::getCurrentUserModel();
$db = PearDatabase::getInstance();
$moduleModel = Vtiger_Module_Model::getInstance('Events');
if($userid){
$focus = new Users();
$focus->id = $userid;
$focus->retrieve_entity_info($userid, 'Users');
$user = Users_Record_Model::getInstanceFromUserObject($focus);
$userName = $user->getName();
$queryGenerator = new QueryGenerator($moduleModel->get('name'), $user);
}else{
$queryGenerator = new QueryGenerator($moduleModel->get('name'), $currentUser);
}
$queryGenerator->setFields(array('subject', 'eventstatus', 'visibility','date_start','time_start','due_date','time_end','assigned_user_id','id','activitytype'));
$query = $queryGenerator->getQuery();
$query.= " AND vtiger_activity.activitytype NOT IN ('Emails','Task') AND ";
$hideCompleted = $currentUser->get('hidecompletedevents');
if($hideCompleted)
$query.= "vtiger_activity.eventstatus != 'HELD' AND ";
$query.= " ((concat(date_start, '', time_start) >= '$dbStartDateTime' AND concat(due_date, '', time_end) < '$dbEndDateTime') OR ( due_date >= '$dbStartDate'))";
$params = array();
if(empty($userid)){
$eventUserId = $currentUser->getId();
}else{
$eventUserId = $userid;
}
$params = array_merge(array($eventUserId), $this->getGroupsIdsForUsers($eventUserId));
$query.= " AND vtiger_crmentity.smownerid IN (". generateQuestionMarks($params).")";
$queryResult = $db->pquery($query, $params);
while($record = $db->fetchByAssoc($queryResult)){
$item = array();
$crmid = $record['activityid'];
$visibility = $record['visibility'];
$activitytype = $record['activitytype'];
$status = $record['eventstatus'];
$item['id'] = $crmid;
$item['visibility'] = $visibility;
$item['activitytype'] = $activitytype;
$item['status'] = $status;
if(!$currentUser->isAdminUser() && $visibility == 'Private' && $userid && $userid != $currentUser->getId()) {
$item['title'] = decode_html($userName).' - '.decode_html(vtranslate('Busy','Events')).'*';
$item['url'] = '';
} else {
$item['title'] = decode_html($record['subject']) . ' - (' . decode_html(vtranslate($record['eventstatus'],'Calendar')) . ')';
$item['url'] = sprintf('index.php?module=Calendar&view=Detail&record=%s', $crmid);
}
$dateTimeFieldInstance = new DateTimeField($record['date_start'] . ' ' . $record['time_start']);
$userDateTimeString = $dateTimeFieldInstance->getFullcalenderDateTimevalue($currentUser);
$dateTimeComponents = explode(' ',$userDateTimeString);
$dateComponent = $dateTimeComponents[0];
//Conveting the date format in to Y-m-d . since full calendar expects in the same format
$dataBaseDateFormatedString = DateTimeField::__convertToDBFormat($dateComponent, $currentUser->get('date_format'));
$item['start'] = $dataBaseDateFormatedString.' '. $dateTimeComponents[1];
$dateTimeFieldInstance = new DateTimeField($record['due_date'] . ' ' . $record['time_end']);
$userDateTimeString = $dateTimeFieldInstance->getFullcalenderDateTimevalue($currentUser);
$dateTimeComponents = explode(' ',$userDateTimeString);
$dateComponent = $dateTimeComponents[0];
//Conveting the date format in to Y-m-d . since full calendar expects in the same format
$dataBaseDateFormatedString = DateTimeField::__convertToDBFormat($dateComponent, $currentUser->get('date_format'));
$item['end'] = $dataBaseDateFormatedString.' '. $dateTimeComponents[1];
$item['className'] = $cssClass;
$item['allDay'] = false;
$item['color'] = $color;
$item['textColor'] = $textColor;
$item['module'] = $moduleModel->getName();
$result[] = $item;
}
}
protected function pullMultipleEvents($start, $end, &$result, $data) {
foreach ($data as $id=>$backgroundColorAndTextColor) {
$userEvents = array();
$colorComponents = explode(',',$backgroundColorAndTextColor);
$this->pullEvents($start, $end, $userEvents ,$id, $colorComponents[0], $colorComponents[1]);
$result[$id] = $userEvents;
}
}
protected function pullTasks($start, $end, &$result, $color = null,$textColor = 'white') {
$user = Users_Record_Model::getCurrentUserModel();
$db = PearDatabase::getInstance();
$moduleModel = Vtiger_Module_Model::getInstance('Calendar');
$userAndGroupIds = array_merge(array($user->getId()),$this->getGroupsIdsForUsers($user->getId()));
$queryGenerator = new QueryGenerator($moduleModel->get('name'), $user);
$queryGenerator->setFields(array('activityid','subject', 'taskstatus','activitytype', 'date_start','time_start','due_date','time_end','id'));
$query = $queryGenerator->getQuery();
$query.= " AND vtiger_activity.activitytype = 'Task' AND ";
$currentUser = Users_Record_Model::getCurrentUserModel();
$hideCompleted = $currentUser->get('hidecompletedevents');
if($hideCompleted)
$query.= "vtiger_activity.status != 'Completed' AND ";
$query.= " ((date_start >= ? AND due_date < ?) OR ( due_date >= ?))";
$params = array($start,$end,$start);
$params = array_merge($params, $userAndGroupIds);
$query.= " AND vtiger_crmentity.smownerid IN (".generateQuestionMarks($userAndGroupIds).")";
$queryResult = $db->pquery($query,$params);
while($record = $db->fetchByAssoc($queryResult)){
$item = array();
$crmid = $record['activityid'];
$item['title'] = decode_html($record['subject']) . ' - (' . decode_html(vtranslate($record['status'],'Calendar')) . ')';
$item['status'] = $record['status'];
$item['activitytype'] = $record['activitytype'];
$item['id'] = $crmid;
$dateTimeFieldInstance = new DateTimeField($record['date_start'] . ' ' . $record['time_start']);
$userDateTimeString = $dateTimeFieldInstance->getFullcalenderDateTimevalue();
$dateTimeComponents = explode(' ',$userDateTimeString);
$dateComponent = $dateTimeComponents[0];
//Conveting the date format in to Y-m-d . since full calendar expects in the same format
$dataBaseDateFormatedString = DateTimeField::__convertToDBFormat($dateComponent, $user->get('date_format'));
$item['start'] = $dataBaseDateFormatedString.' '. $dateTimeComponents[1];
$item['end'] = $record['due_date'];
$item['url'] = sprintf('index.php?module=Calendar&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$item['module'] = $moduleModel->getName();
$result[] = $item;
}
}
protected function pullPotentials($start, $end, &$result, $color = null,$textColor = 'white') {
$query = "SELECT potentialname,closingdate FROM Potentials";
$query.= " WHERE closingdate >= '$start' AND closingdate <= '$end'";
$records = $this->queryForRecords($query);
foreach ($records as $record) {
$item = array();
list ($modid, $crmid) = vtws_getIdComponents($record['id']);
$item['id'] = $crmid;
$item['title'] = decode_html($record['potentialname']);
$item['start'] = $record['closingdate'];
$item['url'] = sprintf('index.php?module=Potentials&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$result[] = $item;
}
}
protected function pullContacts($start, $end, &$result, $color = null,$textColor = 'white') {
$this->pullContactsBySupportEndDate($start, $end, $result, $color, $textColor);
$this->pullContactsByBirthday($start, $end, $result, $color, $textColor);
}
protected function pullContactsBySupportEndDate($start, $end, &$result, $color = null,$textColor = 'white') {
$query = "SELECT firstname,lastname,support_end_date FROM Contacts";
$query.= " WHERE support_end_date >= '$start' AND support_end_date <= '$end'";
$records = $this->queryForRecords($query);
foreach ($records as $record) {
$item = array();
list ($modid, $crmid) = vtws_getIdComponents($record['id']);
$item['id'] = $crmid;
$item['title'] = decode_html(trim($record['firstname'] . ' ' . $record['lastname']));
$item['start'] = $record['support_end_date'];
$item['url'] = sprintf('index.php?module=Contacts&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$result[] = $item;
}
}
protected function pullContactsByBirthday($start, $end, &$result, $color = null,$textColor = 'white') {
$db = PearDatabase::getInstance();
$user = Users_Record_Model::getCurrentUserModel();
$startDateComponents = split('-', $start);
$endDateComponents = split('-', $end);
$userAndGroupIds = array_merge(array($user->getId()),$this->getGroupsIdsForUsers($user->getId()));
$params = array($start,$end,$start,$end);
$params = array_merge($userAndGroupIds, $params);
$year = $startDateComponents[0];
$query = "SELECT firstname,lastname,birthday,crmid FROM vtiger_contactdetails";
$query.= " INNER JOIN vtiger_contactsubdetails ON vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid";
$query.= " INNER JOIN vtiger_crmentity ON vtiger_contactdetails.contactid = vtiger_crmentity.crmid";
$query.= " WHERE vtiger_crmentity.deleted=0 AND smownerid IN (". generateQuestionMarks($userAndGroupIds) .") AND";
$query.= " ((CONCAT('$year-', date_format(birthday,'%m-%d')) >= ?
AND CONCAT('$year-', date_format(birthday,'%m-%d')) <= ?)";
$endDateYear = $endDateComponents[0];
if ($year !== $endDateYear) {
$query .= " OR
(CONCAT('$endDateYear-', date_format(birthday,'%m-%d')) >= ?
AND CONCAT('$endDateYear-', date_format(birthday,'%m-%d')) <= ?)";
}
$query .= ")";
$queryResult = $db->pquery($query, $params);
while($record = $db->fetchByAssoc($queryResult)){
$item = array();
$crmid = $record['crmid'];
$recordDateTime = new DateTime($record['birthday']);
$calendarYear = $year;
if($recordDateTime->format('m') < $startDateComponents[1]) {
$calendarYear = $endDateYear;
}
$recordDateTime->setDate($calendarYear, $recordDateTime->format('m'), $recordDateTime->format('d'));
$item['id'] = $crmid;
$item['title'] = decode_html(trim($record['firstname'] . ' ' . $record['lastname']));
$item['start'] = $recordDateTime->format('Y-m-d');
$item['url'] = sprintf('index.php?module=Contacts&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$result[] = $item;
}
}
protected function pullInvoice($start, $end, &$result, $color = null,$textColor = 'white') {
$query = "SELECT subject,duedate FROM Invoice";
$query.= " WHERE duedate >= '$start' AND duedate <= '$end'";
$records = $this->queryForRecords($query);
foreach ($records as $record) {
$item = array();
list ($modid, $crmid) = vtws_getIdComponents($record['id']);
$item['id'] = $crmid;
$item['title'] = decode_html($record['subject']);
$item['start'] = $record['duedate'];
$item['url'] = sprintf('index.php?module=Invoice&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$result[] = $item;
}
}
/**
* Function to pull all the current user projects
* @param type $startdate
* @param type $actualenddate
* @param type $result
* @param type $color
* @param type $textColor
*/
protected function pullProjects($start, $end, &$result, $color = null,$textColor = 'white') {
$db = PearDatabase::getInstance();
$user = Users_Record_Model::getCurrentUserModel();
$userAndGroupIds = array_merge(array($user->getId()),$this->getGroupsIdsForUsers($user->getId()));
$params = array($start,$end,$start);
$params = array_merge($userAndGroupIds, $params);
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
$query = "SELECT projectname, startdate, targetenddate, crmid FROM vtiger_project";
$query.= " INNER JOIN vtiger_crmentity ON vtiger_project.projectid = vtiger_crmentity.crmid";
$query.= " WHERE vtiger_crmentity.deleted=0 AND smownerid IN (". generateQuestionMarks($userAndGroupIds) .") AND ";
$query.= " ((startdate >= ? AND targetenddate < ?) OR ( targetenddate >= ?))";
$queryResult = $db->pquery($query, $params);
while($record = $db->fetchByAssoc($queryResult)){
$item = array();
$crmid = $record['crmid'];
$item['id'] = $crmid;
$item['title'] = decode_html($record['projectname']);
$item['start'] = $record['startdate'];
$item['end'] = $record['targetenddate'];
$item['url'] = sprintf('index.php?module=Project&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$result[] = $item;
}
}
/**
* Function to pull all the current user porjecttasks
* @param type $startdate
* @param type $enddate
* @param type $result
* @param type $color
* @param type $textColor
*/
protected function pullProjectTasks($start, $end, &$result, $color = null,$textColor = 'white') {
$db = PearDatabase::getInstance();
$user = Users_Record_Model::getCurrentUserModel();
$userAndGroupIds = array_merge(array($user->getId()),$this->getGroupsIdsForUsers($user->getId()));
$params = array($start,$end,$start);
$params = array_merge($params, $userAndGroupIds);
$query = "SELECT projecttaskname, startdate, enddate, crmid FROM vtiger_projecttask";
$query.= " INNER JOIN vtiger_crmentity ON vtiger_projecttask.projecttaskid = vtiger_crmentity.crmid";
$query.= " WHERE vtiger_crmentity.deleted=0 AND ";
$query.= " ((startdate >= ? AND enddate < ?) OR ( enddate >= ?))";
$query.= " AND smownerid IN (". generateQuestionMarks($userAndGroupIds) .")";
$queryResult = $db->pquery($query, $params);
while($record = $db->fetchByAssoc($queryResult)){
$item = array();
$crmid = $record['crmid'];
$item['id'] = $crmid;
$item['title'] = decode_html($record['projecttaskname']);
$item['start'] = $record['startdate'];
$item['end'] = $record['enddate'];
$item['url'] = sprintf('index.php?module=ProjectTask&view=Detail&record=%s', $crmid);
$item['color'] = $color;
$item['textColor'] = $textColor;
$result[] = $item;
}
}