private static function CheckFields($action, &$fields, $ID, $params = null) { global $DB; self::ClearErrors(); if (!(is_array($fields) && count($fields) > 0)) { self::RegisterError(array('text' => 'Fields is not specified.')); return false; } if ($action == 'ADD') { // Validation if (!isset($fields['OWNER_ID'])) { self::RegisterError(array('text' => 'OWNER_ID is not assigned.')); } if (!isset($fields['OWNER_TYPE_ID'])) { self::RegisterError(array('text' => 'OWNER_TYPE_ID is not assigned.')); } if (!isset($fields['TYPE_ID'])) { self::RegisterError(array('text' => 'TYPE_ID is not assigned.')); } elseif (!CCrmActivityType::IsDefined($fields['TYPE_ID'])) { self::RegisterError(array('text' => 'TYPE_ID is not supported.')); } if (!isset($fields['SUBJECT'])) { self::RegisterError(array('text' => 'SUBJECT is not assigned.')); } // if (!isset($fields['START_TIME'])) //is allowed for tasks // { // self::RegisterError(array('text' => 'START_TIME is not assigned.')); // } if (!isset($fields['RESPONSIBLE_ID'])) { self::RegisterError(array('text' => 'RESPONSIBLE_ID is not assigned.')); } if (!isset($fields['NOTIFY_TYPE'])) { $fields['NOTIFY_TYPE'] = CCrmActivityNotifyType::None; } if ($fields['NOTIFY_TYPE'] == CCrmActivityNotifyType::None) { $fields['NOTIFY_VALUE'] = 0; } elseif (!isset($fields['NOTIFY_VALUE'])) { self::RegisterError(array('text' => 'NOTIFY_VALUE is not assigned.')); } if (isset($fields['COMPLETED'])) { $completed = strtoupper(strval($fields['COMPLETED'])); if (!($completed == 'Y' || $completed == 'N')) { $completed = intval($fields['COMPLETED']) > 0 ? 'Y' : 'N'; } $fields['COMPLETED'] = $completed; } else { $fields['COMPLETED'] = 'N'; } if (isset($fields['CREATED'])) { unset($fields['CREATED']); } if (isset($fields['LAST_UPDATED'])) { unset($fields['LAST_UPDATED']); } $fields['~CREATED'] = $fields['~LAST_UPDATED'] = $DB->CurrentTimeFunction(); if (!isset($fields['AUTHOR_ID'])) { $currentUserId = CCrmPerms::GetCurrentUserID(); $fields['AUTHOR_ID'] = $currentUserId > 0 ? $currentUserId : $fields['RESPONSIBLE_ID']; } $fields['EDITOR_ID'] = $fields['AUTHOR_ID']; if (!isset($fields['END_TIME']) && isset($fields['START_TIME'])) { $fields['END_TIME'] = $fields['START_TIME']; } elseif (!isset($fields['START_TIME']) && isset($fields['END_TIME'])) { $fields['START_TIME'] = $fields['END_TIME']; } //DEADLINE --> if (isset($fields['DEADLINE'])) { unset($fields['DEADLINE']); } $typeID = intval($fields['TYPE_ID']); if ($typeID === CCrmActivityType::Task && isset($fields['END_TIME'])) { $fields['DEADLINE'] = $fields['END_TIME']; } elseif ($typeID !== CCrmActivityType::Task && isset($fields['START_TIME'])) { $fields['DEADLINE'] = $fields['START_TIME']; } if (!isset($fields['DEADLINE'])) { $fields['~DEADLINE'] = CCrmDateTimeHelper::GetMaxDatabaseDate(); } //<-- DEADLINE if (!isset($fields['ASSOCIATED_ENTITY_ID'])) { $fields['ASSOCIATED_ENTITY_ID'] = 0; } if (!isset($fields['PRIORITY'])) { $fields['PRIORITY'] = CCrmActivityPriority::Low; } if (!isset($fields['DIRECTION'])) { $fields['DIRECTION'] = CCrmActivityDirection::Undefined; } if (!isset($fields['DESCRIPTION_TYPE'])) { $fields['DESCRIPTION_TYPE'] = CCrmContentType::PlainText; } if (!isset($arFields['STORAGE_TYPE_ID'])) { $arFields['STORAGE_TYPE_ID'] = self::GetDefaultStorageTypeID(); } if (!isset($arFields['PARENT_ID'])) { $arFields['PARENT_ID'] = 0; } } else { $prevFields = is_array($params) && isset($params['PREVIOUS_FIELDS']) && is_array($params['PREVIOUS_FIELDS']) ? $params['PREVIOUS_FIELDS'] : null; if (!is_array($prevFields) && !self::Exists($ID, false)) { self::RegisterError(array('text' => "Could not find CrmActivity(ID = {$ID})")); } if (isset($fields['COMPLETED'])) { $completed = strtoupper(strval($fields['COMPLETED'])); if (!($completed == 'Y' || $completed == 'N')) { $completed = intval($fields['COMPLETED']) > 0 ? 'Y' : 'N'; } $fields['COMPLETED'] = $completed; } // Default settings if (isset($fields['CREATED'])) { unset($fields['CREATED']); } if (isset($fields['LAST_UPDATED'])) { unset($fields['LAST_UPDATED']); } $fields['~LAST_UPDATED'] = $DB->CurrentTimeFunction(); if (!isset($fields['EDITOR_ID'])) { $userID = isset($fields['AUTHOR_ID']) ? $fields['AUTHOR_ID'] : 0; if ($userID <= 0) { $userID = CCrmPerms::GetCurrentUserID(); } $fields['EDITOR_ID'] = $userID > 0 ? $userID : $fields['RESPONSIBLE_ID']; } unset($fields['AUTHOR_ID']); // TYPE_ID --> if (isset($fields['TYPE_ID'])) { unset($fields['TYPE_ID']); } // <-- TYPE_ID //DEADLINE --> if (isset($fields['DEADLINE'])) { unset($fields['DEADLINE']); } $typeID = isset($prevFields['TYPE_ID']) ? intval($prevFields['TYPE_ID']) : CCrmActivityType::Undefined; if ($typeID === CCrmActivityType::Task && isset($fields['END_TIME'])) { $fields['DEADLINE'] = $fields['END_TIME']; } elseif ($typeID !== CCrmActivityType::Task && isset($fields['START_TIME'])) { $fields['DEADLINE'] = $fields['START_TIME']; } //<-- DEADLINE } return self::GetErrorCount() == 0; }
$arResult['MODE'] = 'CREATE'; $typeID = $arParams['TYPE_ID'] = isset($arParams['TYPE_ID']) ? intval($arParams['TYPE_ID']) : CCrmActivityType::Undefined; if ($typeID <= 0 && isset($_REQUEST['type_id'])) { $typeID = $arParams['TYPE_ID'] = intval($_REQUEST['type_id']); } $ownerID = $arParams['OWNER_ID'] = isset($arParams['OWNER_ID']) ? intval($arParams['OWNER_ID']) : 0; if ($ownerID <= 0 && isset($_REQUEST['owner_id'])) { $ownerID = $arParams['OWNER_ID'] = intval($_REQUEST['owner_id']); } $ownerTypeName = $arParams['OWNER_TYPE'] = isset($arParams['OWNER_TYPE']) ? $arParams['OWNER_TYPE'] : ''; if ($ownerTypeName === '' && isset($_REQUEST['owner_type'])) { $ownerTypeName = $arParams['OWNER_TYPE'] = $_REQUEST['owner_type']; } $ownerTypeID = CCrmOwnerType::ResolveID($ownerTypeName); } if (!CCrmActivityType::IsDefined($typeID)) { ShowError(GetMessage('CRM_ACTIVITY_EDIT_TYPE_IS_NOT_SUPPORTED', array('#TYPE#' => $typeID))); return; } if ($ownerID > 0 && $ownerTypeID !== CCrmOwnerType::Undefined) { if (!CCrmActivity::CheckUpdatePermission($ownerTypeID, $ownerID, $userPerms)) { ShowError(GetMessage('CRM_PERMISSION_DENIED')); return; } } elseif (!CCrmPerms::IsAccessEnabled()) { ShowError(GetMessage('CRM_PERMISSION_DENIED')); return; } $arResult['TYPE_ID'] = $typeID; $arResult['OWNER_ID'] = $ownerID; $arResult['OWNER_TYPE_ID'] = $ownerTypeID;
public function createActivities(array $params) { $count = isset($params['COUNT']) ? (int) $params['COUNT'] : 0; if ($count <= 0) { return; } $typeID = isset($params['TYPE_ID']) ? (int) $params['TYPE_ID'] : \CCrmActivityType::Undefined; if (!\CCrmActivityType::IsDefined($typeID)) { return; } $ownerTypeID = isset($params['OWNER_TYPE_ID']) ? (int) $params['OWNER_TYPE_ID'] : \CCrmOwnerType::Undefined; if (!\CCrmOwnerType::IsDefined($ownerTypeID)) { return; } $ownerID = isset($params['OWNER_ID']) ? (int) $params['OWNER_ID'] : 0; if ($ownerID <= 0) { return; } $clientTypeID = isset($params['CLIENT_TYPE_ID']) ? (int) $params['CLIENT_TYPE_ID'] : \CCrmOwnerType::Undefined; $clientID = isset($params['CLIENT_ID']) ? (int) $params['CLIENT_ID'] : 0; if (!\CCrmOwnerType::IsDefined($clientTypeID) || $clientID <= 0) { $clientTypeID = $ownerTypeID; $clientID = $ownerID; } $commType = $typeID === \CCrmActivityType::Call ? 'PHONE' : ''; $values = array(); if ($commType === 'PHONE' || $commType === 'EMAIL') { $values = self::getEntityMultifieldValues($clientTypeID, $clientID, $commType); if (empty($values)) { return; } } $userIDs = isset($params['USER_IDS']) && is_array($params['USER_IDS']) ? $params['USER_IDS'] : array(); if (empty($userIDs)) { $userIDs[] = \CCrmSecurityHelper::GetCurrentUserID(); } $date = isset($params['DATE']) ? $params['DATE'] : null; if (!$date) { $date = $date = new Date(); } $maxDateOffset = isset($params['MAX_DATE_OFFSET']) ? (int) $params['MAX_DATE_OFFSET'] : 0; $dateTimeFormat = Date::convertFormatToPhp(FORMAT_DATETIME); $subjectPrefix = isset($params['SUBJECT_PREFIX']) ? $params['SUBJECT_PREFIX'] : ''; for ($i = 0; $i < $count; $i++) { $time = DateTime::createFromTimestamp($date->getTimestamp()); if ($maxDateOffset > 0) { $time->add(mt_rand(0, $maxDateOffset) . ' days'); } $time->setTime(mt_rand(8, 20), mt_rand(0, 59), 0); $siteTime = $time->format($dateTimeFormat); $fields = array('TYPE_ID' => $typeID, 'START_TIME' => $siteTime, 'END_TIME' => $siteTime, 'SUBJECT' => "{$subjectPrefix} ({$siteTime})", 'COMPLETED' => mt_rand(1, 10) % 2 !== 0 ? 'Y' : 'N', 'PRIORITY' => \CCrmActivityPriority::Medium, 'DESCRIPTION' => '', 'DESCRIPTION_TYPE' => \CCrmContentType::PlainText, 'LOCATION' => '', 'DIRECTION' => \CCrmActivityDirection::Outgoing, 'NOTIFY_TYPE' => \CCrmActivityNotifyType::None, 'RESPONSIBLE_ID' => self::getRandomItem($userIDs), 'OWNER_ID' => $ownerID, 'OWNER_TYPE_ID' => $ownerTypeID, 'BINDINGS' => array(array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID))); $ID = \CCrmActivity::Add($fields, false, true, array('REGISTER_SONET_EVENT' => true)); $comms = array(array('TYPE' => $commType, 'VALUE' => self::getRandomItem($values, ''), 'ENTITY_ID' => $clientID, 'ENTITY_TYPE_ID' => $clientTypeID)); \CCrmActivity::SaveCommunications($ID, $comms, $fields, false, false); } }
protected function innerAdd(&$fields, &$errors, array $params = null) { $ownerTypeID = isset($fields['OWNER_TYPE_ID']) ? intval($fields['OWNER_TYPE_ID']) : 0; $ownerID = isset($fields['OWNER_ID']) ? intval($fields['OWNER_ID']) : 0; $bindings = array(); if ($ownerTypeID > 0 && $ownerID > 0) { $bindings["{$ownerTypeID}_{$ownerID}"] = array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID); } $responsibleID = isset($fields['RESPONSIBLE_ID']) ? intval($fields['RESPONSIBLE_ID']) : 0; if ($responsibleID <= 0 && $ownerTypeID > 0 && $ownerID > 0) { $fields['RESPONSIBLE_ID'] = $responsibleID = CCrmOwnerType::GetResponsibleID($ownerTypeID, $ownerID); } if ($responsibleID <= 0) { $responsibleID = CCrmSecurityHelper::GetCurrentUserID(); } if ($responsibleID <= 0) { $errors[] = 'The field RESPONSIBLE_ID is not defined or invalid.'; return false; } $typeID = isset($fields['TYPE_ID']) ? intval($fields['TYPE_ID']) : CCrmActivityType::Undefined; if (!CCrmActivityType::IsDefined($typeID)) { $errors[] = 'The field TYPE_ID is not defined or invalid.'; return false; } if (!in_array($typeID, array(CCrmActivityType::Call, CCrmActivityType::Meeting, CCrmActivityType::Email), true)) { $errors[] = 'The activity type "' . CCrmActivityType::ResolveDescription($typeID) . ' is not supported in current context".'; return false; } $description = isset($fields['DESCRIPTION']) ? $fields['DESCRIPTION'] : ''; $descriptionType = isset($fields['DESCRIPTION_TYPE']) ? intval($fields['DESCRIPTION_TYPE']) : CCrmContentType::PlainText; if ($description !== '' && CCrmActivity::AddEmailSignature($description, $descriptionType)) { $fields['DESCRIPTION'] = $description; } $direction = isset($fields['DIRECTION']) ? intval($fields['DIRECTION']) : CCrmActivityDirection::Undefined; $completed = isset($fields['COMPLETED']) && strtoupper($fields['COMPLETED']) === 'Y'; $communications = isset($fields['COMMUNICATIONS']) && is_array($fields['COMMUNICATIONS']) ? $fields['COMMUNICATIONS'] : array(); $this->prepareCommunications($ownerTypeID, $ownerID, $typeID, $communications, $bindings); if (empty($communications)) { $errors[] = 'The field COMMUNICATIONS is not defined or invalid.'; return false; } if (($typeID === CCrmActivityType::Call || $typeID === CCrmActivityType::Meeting) && count($communications) > 1) { $errors[] = 'The only one communication is allowed for activity of specified type.'; return false; } if (empty($bindings)) { $errors[] = 'Could not build binding. Please ensure that owner info and communications are defined correctly.'; return false; } foreach ($bindings as &$binding) { if (!CCrmActivity::CheckUpdatePermission($binding['OWNER_TYPE_ID'], $binding['OWNER_ID'])) { $errors[] = 'Access denied.'; return false; } } unset($binding); $fields['BINDINGS'] = array_values($bindings); $fields['COMMUNICATIONS'] = $communications; $storageTypeID = $fields['STORAGE_TYPE_ID'] = CCrmActivity::GetDefaultStorageTypeID(); $fields['STORAGE_ELEMENT_IDS'] = array(); if ($storageTypeID === StorageType::WebDav) { $webdavElements = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS']) ? $fields['WEBDAV_ELEMENTS'] : array(); foreach ($webdavElements as &$element) { $elementID = isset($element['ELEMENT_ID']) ? intval($element['ELEMENT_ID']) : 0; if ($elementID > 0) { $fields['STORAGE_ELEMENT_IDS'][] = $elementID; } } unset($element); } elseif ($storageTypeID === StorageType::Disk) { $diskFiles = isset($fields['FILES']) && is_array($fields['FILES']) ? $fields['FILES'] : array(); if (empty($diskFiles)) { //For backward compatibility only $diskFiles = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS']) ? $fields['WEBDAV_ELEMENTS'] : array(); } foreach ($diskFiles as &$fileInfo) { $fileID = isset($fileInfo['FILE_ID']) ? (int) $fileInfo['FILE_ID'] : 0; if ($fileID > 0) { $fields['STORAGE_ELEMENT_IDS'][] = $fileID; } } unset($fileInfo); } if (!($ID = CCrmActivity::Add($fields))) { $errors[] = CCrmActivity::GetLastErrorMessage(); return false; } CCrmActivity::SaveCommunications($ID, $communications, $fields, false, false); if ($completed && $typeID === CCrmActivityType::Email && $direction === CCrmActivityDirection::Outgoing) { $sendErrors = array(); if (!CCrmActivityEmailSender::TrySendEmail($ID, $fields, $sendErrors)) { foreach ($sendErrors as &$error) { $code = $error['CODE']; if ($code === CCrmActivityEmailSender::ERR_CANT_LOAD_SUBSCRIBE) { $errors[] = 'Email send error. Failed to load module "subscribe".'; } elseif ($code === CCrmActivityEmailSender::ERR_INVALID_DATA) { $errors[] = 'Email send error. Invalid data.'; } elseif ($code === CCrmActivityEmailSender::ERR_INVALID_EMAIL) { $errors[] = 'Email send error. Invalid email is specified.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_FIND_EMAIL_FROM) { $errors[] = 'Email send error. "From" is not found.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_FIND_EMAIL_TO) { $errors[] = 'Email send error. "To" is not found.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_ADD_POSTING) { $errors[] = 'Email send error. Failed to add posting. Please see details below.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_SAVE_POSTING_FILE) { $errors[] = 'Email send error. Failed to save posting file. Please see details below.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_UPDATE_ACTIVITY) { $errors[] = 'Email send error. Failed to update activity.'; } else { $errors[] = 'Email send error. General error.'; } $msg = isset($error['MESSAGE']) ? $error['MESSAGE'] : ''; if ($msg !== '') { $errors[] = $msg; } } unset($error); return false; } } return $ID; }