Beispiel #1
0
 /**
  * Process the request to perform relationship operations
  * @global Users Instance $currentUserModel
  * @global PearDataBase Instance $adb
  * @global String $currentModule
  * @param Vtiger_Request $request
  * @return boolean
  */
 public function process(Vtiger_Request $request)
 {
     $currentUserModel = Users_Record_Model::getCurrentUserModel();
     $response = new Vtiger_Response(true);
     $viewer = $this->getViewer($request);
     if ('find' == $this->getOperationArg($request)) {
         $this->skipConnection = true;
         // No need to connect to mailbox here, improves performance
         // Check if the message is already linked.
         $linkedto = MailManager_Relate_Action::associatedLink($request->get('_msguid'));
         // If the message was not linked, lookup for matching records, using FROM address
         if (empty($linkedto)) {
             $results = array();
             $modules = array();
             $allowedModules = $this->getCurrentUserMailManagerAllowedModules();
             foreach (self::$MODULES as $MODULE) {
                 if (!in_array($MODULE, $allowedModules)) {
                     continue;
                 }
                 $from = $request->get('_mfrom');
                 if (empty($from)) {
                     continue;
                 }
                 $results[$MODULE] = $this->lookupModuleRecordsWithEmail($MODULE, $from);
                 $describe = $this->ws_describe($MODULE);
                 $modules[$MODULE] = array('label' => $describe['label'], 'name' => textlength_check($describe['name']), 'id' => $describe['idPrefix']);
                 // If look is found in a module, skip rest. - for performance
                 //if (!empty($results[$MODULE])) break;
             }
             $viewer->assign('LOOKUPS', $results);
             $viewer->assign('MODULES', $modules);
         } else {
             $viewer->assign('LINKEDTO', $linkedto);
         }
         $viewer->assign('LINK_TO_AVAILABLE_ACTIONS', $this->linkToAvailableActions());
         $viewer->assign('ALLOWED_MODULES', $allowedModules);
         $viewer->assign('MSGNO', $request->get('_msgno'));
         $viewer->assign('FOLDER', $request->get('_folder'));
         $response->setResult(array('ui' => $viewer->view('Relationship.tpl', 'MailManager', true)));
     } else {
         if ('link' == $this->getOperationArg($request)) {
             $linkto = $request->get('_mlinkto');
             $foldername = $request->get('_folder');
             $connector = $this->getConnector($foldername);
             // This is to handle larger uploads
             $memory_limit = MailManager_Config_Model::get('MEMORY_LIMIT');
             ini_set('memory_limit', $memory_limit);
             $mail = $connector->openMail($request->get('_msgno'));
             $mail->attachments();
             // Initialize attachments
             $linkedto = MailManager_Relate_Action::associate($mail, $linkto);
             $viewer->assign('LINK_TO_AVAILABLE_ACTIONS', $this->linkToAvailableActions());
             $viewer->assign('ALLOWED_MODULES', $this->getCurrentUserMailManagerAllowedModules());
             $viewer->assign('LINKEDTO', $linkedto);
             $viewer->assign('MSGNO', $request->get('_msgno'));
             $viewer->assign('FOLDER', $foldername);
             $response->setResult(array('ui' => $viewer->view('Relationship.tpl', 'MailManager', true)));
         } else {
             if ('create_wizard' == $this->getOperationArg($request)) {
                 $moduleName = $request->get('_mlinktotype');
                 $parent = $request->get('_mlinkto');
                 $foldername = $request->get('_folder');
                 $connector = $this->getConnector($foldername);
                 $mail = $connector->openMail($request->get('_msgno'));
                 $formData = $this->processFormData($mail);
                 foreach ($formData as $key => $value) {
                     $request->set($key, $value);
                 }
                 $request->set('module', $moduleName);
                 // Delegate QuickCreate FormUI to the target view controller of module.
                 $quickCreateviewClassName = $moduleName . '_QuickCreateAjax_View';
                 if (!class_exists($quickCreateviewClassName)) {
                     $quickCreateviewClassName = 'Vtiger_QuickCreateAjax_View';
                 }
                 $quickCreateViewController = new $quickCreateviewClassName();
                 $quickCreateViewController->process($request);
                 // UI already sent
                 $response = false;
             } else {
                 if ('create' == $this->getOperationArg($request)) {
                     $linkModule = $request->get('_mlinktotype');
                     $parent = $request->get('_mlinkto');
                     $foldername = $request->get('_folder');
                     if (!empty($foldername)) {
                         // This is to handle larger uploads
                         $memory_limit = MailManager_Config_Model::get('MEMORY_LIMIT');
                         ini_set('memory_limit', $memory_limit);
                         $connector = $this->getConnector($foldername);
                         $mail = $connector->openMail($request->get('_msgno'));
                         $attachments = $mail->attachments();
                         // Initialize attachments
                     }
                     $linkedto = MailManager_Relate_Action::getSalesEntityInfo($parent);
                     $recordModel = Vtiger_Record_Model::getCleanInstance($linkModule);
                     $fields = $recordModel->getModule()->getFields();
                     foreach ($fields as $fieldName => $fieldModel) {
                         if ($request->has($fieldName)) {
                             $fieldValue = $request->get($fieldName);
                             $fieldDataType = $fieldModel->getFieldDataType();
                             if ($fieldDataType == 'time') {
                                 $fieldValue = Vtiger_Time_UIType::getTimeValueWithSeconds($fieldValue);
                             }
                             $recordModel->set($fieldName, $fieldValue);
                         }
                     }
                     switch ($linkModule) {
                         case 'Calendar':
                             $activityType = $recordModel->get('activitytype');
                             if (!$activityType) {
                                 $activityType = 'Task';
                             }
                             $recordModel->set('activitytype', $activityType);
                             //Start Date and Time values
                             $startTime = Vtiger_Time_UIType::getTimeValueWithSeconds($request->get('time_start'));
                             $startDateTime = Vtiger_Datetime_UIType::getDBDateTimeValue($request->get('date_start') . " " . $startTime);
                             list($startDate, $startTime) = explode(' ', $startDateTime);
                             $recordModel->set('date_start', $startDate);
                             $recordModel->set('time_start', $startTime);
                             //End Date and Time values
                             $endDate = Vtiger_Date_UIType::getDBInsertedValue($request->get('due_date'));
                             if ($activityType != 'Task') {
                                 $endTime = Vtiger_Time_UIType::getTimeValueWithSeconds($request->get('time_end'));
                                 $endDateTime = Vtiger_Datetime_UIType::getDBDateTimeValue($request->get('due_date') . " " . $endTime);
                                 list($endDate, $endTime) = explode(' ', $endDateTime);
                             } else {
                                 $endTime = '';
                             }
                             $recordModel->set('time_end', $endTime);
                             $recordModel->set('due_date', $endDate);
                             if ($parent) {
                                 if ($linkedto['module'] == 'Contacts') {
                                     $recordModel->set('contact_id', $parent);
                                 } else {
                                     $recordModel->set('parent_id', $parent);
                                 }
                             }
                             $recordModel->set('visibility', 'Public');
                             break;
                         case 'HelpDesk':
                             $from = $mail->from();
                             if ($parent) {
                                 if ($linkedto['module'] == 'Contacts') {
                                     $referenceFieldName = 'contact_id';
                                 } elseif ($linkedto['module'] == 'Accounts') {
                                     $referenceFieldName = 'parent_id';
                                 }
                             }
                             $recordModel->set($referenceFieldName, $this->setParentForHelpDesk($parent, $from));
                             break;
                         case 'ModComments':
                             $recordModel->set('assigned_user_id', $currentUserModel->getId());
                             $recordModel->set('commentcontent', $request->getRaw('commentcontent'));
                             $recordModel->set('userid', $currentUserModel->getId());
                             $recordModel->set('creator', $currentUserModel->getId());
                             $recordModel->set('related_to', $parent);
                             break;
                     }
                     try {
                         $recordModel->save();
                         // This condition is added so that emails are not created for Tickets and Todo without Parent,
                         // as there is no way to relate them
                         if (empty($parent) && $linkModule != 'HelpDesk' && $linkModule != 'Calendar') {
                             $linkedto = MailManager_Relate_Action::associate($mail, $recordModel->getId());
                         }
                         if ($linkModule === 'Calendar') {
                             // Handled to save follow up event
                             $followupMode = $request->get('followup');
                             //Start Date and Time values
                             $startTime = Vtiger_Time_UIType::getTimeValueWithSeconds($request->get('followup_time_start'));
                             $startDateTime = Vtiger_Datetime_UIType::getDBDateTimeValue($request->get('followup_date_start') . " " . $startTime);
                             list($startDate, $startTime) = explode(' ', $startDateTime);
                             $subject = $request->get('subject');
                             if ($followupMode == 'on' && $startTime != '' && $startDate != '') {
                                 $recordModel->set('eventstatus', 'Planned');
                                 $recordModel->set('subject', '[Followup] ' . $subject);
                                 $recordModel->set('date_start', $startDate);
                                 $recordModel->set('time_start', $startTime);
                                 $currentUser = Users_Record_Model::getCurrentUserModel();
                                 $activityType = $recordModel->get('activitytype');
                                 if ($activityType == 'Call') {
                                     $minutes = $currentUser->get('callduration');
                                 } else {
                                     $minutes = $currentUser->get('othereventduration');
                                 }
                                 $dueDateTime = date('Y-m-d H:i:s', strtotime("{$startDateTime}+{$minutes} minutes"));
                                 list($startDate, $startTime) = explode(' ', $dueDateTime);
                                 $recordModel->set('due_date', $startDate);
                                 $recordModel->set('time_end', $startTime);
                                 $recordModel->set('recurringtype', '');
                                 $recordModel->set('mode', 'create');
                                 $recordModel->save();
                             }
                         }
                         // add attachments to the tickets as Documents
                         if ($linkModule == 'HelpDesk' && !empty($attachments)) {
                             $relationController = new MailManager_Relate_Action();
                             $relationController->__SaveAttachements($mail, $linkModule, $recordModel);
                         }
                         $viewer->assign('MSGNO', $request->get('_msgno'));
                         $viewer->assign('LINKEDTO', $linkedto);
                         $viewer->assign('ALLOWED_MODULES', $this->getCurrentUserMailManagerAllowedModules());
                         $viewer->assign('LINK_TO_AVAILABLE_ACTIONS', $this->linkToAvailableActions());
                         $viewer->assign('FOLDER', $foldername);
                         $response->setResult(array('ui' => $viewer->view('Relationship.tpl', 'MailManager', true)));
                     } catch (Exception $e) {
                         $response->setResult(array('ui' => '', 'error' => $e));
                     }
                 } else {
                     if ('savedraft' == $this->getOperationArg($request)) {
                         $connector = $this->getConnector('__vt_drafts');
                         $draftResponse = $connector->saveDraft($request);
                         $response->setResult($draftResponse);
                     } else {
                         if ('saveattachment' == $this->getOperationArg($request)) {
                             $connector = $this->getConnector('__vt_drafts');
                             $uploadResponse = $connector->saveAttachment($request);
                             $response->setResult($uploadResponse);
                         } else {
                             if ('commentwidget' == $this->getOperationArg($request)) {
                                 $viewer->assign('LINKMODULE', $request->get('_mlinktotype'));
                                 $viewer->assign('PARENT', $request->get('_mlinkto'));
                                 $viewer->assign('MSGNO', $request->get('_msgno'));
                                 $viewer->assign('FOLDER', $request->get('_folder'));
                                 $viewer->view('MailManagerCommentWidget.tpl', 'MailManager');
                                 $response = false;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }
 /**
  * Process the request to perform relationship operations
  * @global Users Instance $current_user
  * @global PearDataBase Instance $adb
  * @global String $currentModule
  * @param Vtiger_Request $request
  * @return boolean
  */
 function process(Vtiger_Request $request)
 {
     global $current_user, $adb;
     $response = new Vtiger_Response(true);
     $viewer = $this->getViewer($request);
     if ('find' == $this->getOperationArg($request)) {
         $this->skipConnection = true;
         // No need to connect to mailbox here, improves performance
         // Check if the message is already linked.
         $linkedto = MailManager_Relate_Action::associatedLink($request->get('_msguid'));
         // If the message was not linked, lookup for matching records, using FROM address
         if (empty($linkedto)) {
             $results = array();
             $modules = array();
             $allowedModules = $this->getCurrentUserMailManagerAllowedModules();
             foreach (self::$MODULES as $MODULE) {
                 if (!in_array($MODULE, $allowedModules)) {
                     continue;
                 }
                 $from = $request->get('_mfrom');
                 if (empty($from)) {
                     continue;
                 }
                 $results[$MODULE] = $this->lookupModuleRecordsWithEmail($MODULE, $from);
                 $describe = $this->ws_describe($MODULE);
                 $modules[$MODULE] = array('label' => $describe['label'], 'name' => textlength_check($describe['name']), 'id' => $describe['idPrefix']);
                 // If look is found in a module, skip rest. - for performance
                 //if (!empty($results[$MODULE])) break;
             }
             $viewer->assign('LOOKUPS', $results);
             $viewer->assign('MODULES', $modules);
         } else {
             $viewer->assign('LINKEDTO', $linkedto);
         }
         $viewer->assign('LinkToAvailableActions', $this->linkToAvailableActions());
         $viewer->assign('AllowedModules', $allowedModules);
         $viewer->assign('MSGNO', $request->get('_msgno'));
         $viewer->assign('FOLDER', $request->get('_folder'));
         $response->setResult(array('ui' => $viewer->view('Relationship.tpl', 'MailManager', true)));
     } else {
         if ('link' == $this->getOperationArg($request)) {
             $linkto = $request->get('_mlinkto');
             $foldername = $request->get('_folder');
             $connector = $this->getConnector($foldername);
             // This is to handle larger uploads
             $memory_limit = MailManager_Config::get('MEMORY_LIMIT');
             ini_set('memory_limit', $memory_limit);
             $mail = $connector->openMail($request->get('_msgno'));
             $mail->attachments();
             // Initialize attachments
             $linkedto = MailManager_Relate_Action::associate($mail, $linkto);
             $viewer->assign('LinkToAvailableActions', $this->linkToAvailableActions());
             $viewer->assign('AllowedModules', $this->getCurrentUserMailManagerAllowedModules());
             $viewer->assign('LINKEDTO', $linkedto);
             $viewer->assign('MSGNO', $request->get('_msgno'));
             $viewer->assign('FOLDER', $foldername);
             $response->setResult(array('ui' => $viewer->view('Relationship.tpl', 'MailManager', true)));
         } else {
             if ('create_wizard' == $this->getOperationArg($request)) {
                 global $currentModule;
                 $moduleName = $request->get('_mlinktotype');
                 $parent = $request->get('_mlinkto');
                 $foldername = $request->get('_folder');
                 $connector = $this->getConnector($foldername);
                 $mail = $connector->openMail($request->get('_msgno'));
                 $formData = $this->processFormData($mail);
                 foreach ($formData as $key => $value) {
                     $request->set($key, $value);
                 }
                 $request->set('module', $moduleName);
                 // Delegate QuickCreate FormUI to the target view controller of module.
                 $quickCreateviewClassName = $moduleName . '_QuickCreateAjax_View';
                 if (!class_exists($quickCreateviewClassName)) {
                     $quickCreateviewClassName = 'Vtiger_QuickCreateAjax_View';
                 }
                 $quickCreateViewController = new $quickCreateviewClassName();
                 $quickCreateViewController->process($request);
                 // UI already sent
                 $response = false;
             } else {
                 if ('create' == $this->getOperationArg($request)) {
                     $linkModule = $request->get('_mlinktotype');
                     $parent = $request->get('_mlinkto');
                     $focus = CRMEntity::getInstance($linkModule);
                     // This is added as ModComments module has a bug that will not initialize column_fields
                     // Basically $currentModule is set to MailManager, so the fields are not set properly.
                     if (empty($focus->column_fields)) {
                         $focus->column_fields = getColumnFields($linkModule);
                     }
                     foreach ($focus->column_fields as $fieldname => $val) {
                         if ($request->has($fieldname)) {
                             $focus->column_fields[$fieldname] = $request->get($fieldname);
                         }
                     }
                     $foldername = $request->get('_folder');
                     if (!empty($foldername)) {
                         // This is to handle larger uploads
                         $memory_limit = MailManager_Config::get('MEMORY_LIMIT');
                         ini_set('memory_limit', $memory_limit);
                         $connector = $this->getConnector($foldername);
                         $mail = $connector->openMail($request->get('_msgno'));
                         $attachments = $mail->attachments();
                         // Initialize attachments
                     }
                     $linkedto = MailManager_Relate_Action::getSalesEntityInfo($parent);
                     switch ($linkModule) {
                         case 'Calendar':
                             if (empty($focus->column_fields['activitytype'])) {
                                 $focus->column_fields['activitytype'] = 'Task';
                             }
                             if (empty($focus->column_fields['due_date'])) {
                                 if (!empty($focus->column_fields['date_start'])) {
                                     $dateStart = getValidDBInsertDateValue($focus->column_fields['date_start']);
                                     $focus->column_fields['due_date'] = date("Y-m-d", strtotime(date("Y-m-d", strtotime($dateStart)) . " +1 day"));
                                 } else {
                                     $focus->column_fields['due_date'] = date('Y-m-d', strtotime("+1 day"));
                                 }
                             }
                             if (!empty($parent)) {
                                 if ($linkedto['module'] == 'Contacts') {
                                     $focus->column_fields['contact_id'] = $parent;
                                 } else {
                                     $focus->column_fields['parent_id'] = $parent;
                                 }
                             }
                             break;
                         case 'HelpDesk':
                             $from = $mail->from();
                             $focus->column_fields['parent_id'] = $this->setParentForHelpDesk($parent, $from);
                             break;
                         case 'ModComments':
                             $focus->column_fields['assigned_user_id'] = $current_user->id;
                             $focus->column_fields['creator'] = $current_user->id;
                             $focus->column_fields['related_to'] = $parent;
                             break;
                     }
                     try {
                         $focus->save($linkModule);
                         // This condition is added so that emails are not created for Tickets and Todo without Parent,
                         // as there is no way to relate them
                         if (empty($parent) && $linkModule != 'HelpDesk' && $linkModule != 'Calendar') {
                             $linkedto = MailManager_Relate_Action::associate($mail, $focus->id);
                         }
                         // add attachments to the tickets as Documents
                         if ($linkModule == 'HelpDesk' && !empty($attachments)) {
                             $relationController = new MailManager_Relate_Action();
                             $relationController->__SaveAttachements($mail, $linkModule, $focus);
                         }
                         $viewer->assign('MSGNO', $request->get('_msgno'));
                         $viewer->assign('LINKEDTO', $linkedto);
                         $viewer->assign('AllowedModules', $this->getCurrentUserMailManagerAllowedModules());
                         $viewer->assign('LinkToAvailableActions', $this->linkToAvailableActions());
                         $viewer->assign('FOLDER', $foldername);
                         $response->setResult(array('ui' => $viewer->view('Relationship.tpl', 'MailManager', true)));
                     } catch (Exception $e) {
                         $response->setResult(array('ui' => '', 'error' => $e));
                     }
                 } else {
                     if ('savedraft' == $this->getOperationArg($request)) {
                         $connector = $this->getConnector('__vt_drafts');
                         $draftResponse = $connector->saveDraft($request);
                         $response->setResult($draftResponse);
                     } else {
                         if ('saveattachment' == $this->getOperationArg($request)) {
                             $connector = $this->getConnector('__vt_drafts');
                             $uploadResponse = $connector->saveAttachment($request);
                             $response->setResult($uploadResponse);
                         } else {
                             if ('commentwidget' == $this->getOperationArg($request)) {
                                 $viewer->assign('LINKMODULE', $request->get('_mlinktotype'));
                                 $viewer->assign('PARENT', $request->get('_mlinkto'));
                                 $viewer->assign('MSGNO', $request->get('_msgno'));
                                 $viewer->assign('FOLDER', $request->get('_folder'));
                                 $viewer->view('MailManagerCommentWidget.tpl', 'MailManager');
                                 $response = false;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }