Esempio n. 1
0
function GetCrmActivityCommunications($ID)
{
    $communications = CCrmActivity::GetCommunications($ID);
    $communicationData = array();
    if (is_array($communications)) {
        foreach ($communications as &$comm) {
            CCrmActivity::PrepareCommunicationInfo($comm);
            $datum = array('id' => $comm['ID'], 'type' => $comm['TYPE'], 'value' => $comm['VALUE'], 'entityId' => $comm['ENTITY_ID'], 'entityType' => CCrmOwnerType::ResolveName($comm['ENTITY_TYPE_ID']), 'entityTitle' => $comm['TITLE'], 'entityUrl' => CCrmOwnerType::GetShowUrl($comm['ENTITY_TYPE_ID'], $comm['ENTITY_ID']));
            if ($datum['type'] === 'PHONE' && CCrmSipHelper::checkPhoneNumber($datum['value'])) {
                $datum['enableSip'] = true;
            }
            $communicationData[] =& $datum;
            unset($datum);
        }
        unset($comm);
    }
    return array('DATA' => array('ID' => $ID, 'COMMUNICATIONS' => $communicationData));
}
Esempio n. 2
0
 function Fetch()
 {
     if ($result = parent::Fetch()) {
         if (array_key_exists('SETTINGS', $result)) {
             $result['SETTINGS'] = is_string($result['SETTINGS']) ? unserialize($result['SETTINGS']) : array();
         }
         if ($this->selectCommunications) {
             $result['COMMUNICATIONS'] = CCrmActivity::GetCommunications($result['ID']);
         }
     }
     return $result;
 }
Esempio n. 3
0
 private static function GetSubSelect($arLogFields, $bDecrement = false)
 {
     global $DB;
     $author_id = CCrmSecurityHelper::GetCurrentUserID();
     if ($author_id <= 0 && isset($arLogFields["USER_ID"])) {
         $author_id = intval($arLogFields["USER_ID"]);
     }
     if ($author_id <= 0) {
         return "";
     }
     $entityTypeID = CCrmLiveFeedEntity::ResolveEntityTypeID($arLogFields["ENTITY_TYPE"]);
     $entityID = $arLogFields["ENTITY_ID"];
     $arEntities = array();
     if ($entityTypeID == CCrmOwnerType::Activity) {
         if ($arActivity = CCrmActivity::GetByID($entityID)) {
             $entityTypeID = $arActivity["OWNER_TYPE_ID"];
             $entityID = $arActivity["OWNER_ID"];
             $entityName = CCrmOwnerType::ResolveName($entityTypeID);
             $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false);
             $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false);
             if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0) {
                 if (!array_key_exists($entityName, $arEntities)) {
                     $arEntities[$entityName] = array();
                 }
                 $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id);
             }
             $arCommunications = CCrmActivity::GetCommunications($arActivity["ID"]);
             foreach ($arCommunications as $arActivityCommunication) {
                 $entityTypeID = $arActivityCommunication["ENTITY_TYPE_ID"];
                 $entityID = $arActivityCommunication["ENTITY_ID"];
                 $entityName = CCrmOwnerType::ResolveName($entityTypeID);
                 $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false);
                 $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false);
                 if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0) {
                     if (!array_key_exists($entityName, $arEntities)) {
                         $arEntities[$entityName] = array();
                     }
                     $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id);
                 }
             }
         }
     } elseif ($entityTypeID == CCrmOwnerType::Invoice) {
         if ($arInvoice = CCrmInvoice::GetByID($entityID)) {
             $arBindings = array(CCrmOwnerType::Contact => $arInvoice["UF_CONTACT_ID"], CCrmOwnerType::Company => $arInvoice["UF_COMPANY_ID"], CCrmOwnerType::Deal => $arInvoice["UF_DEAL_ID"]);
             foreach ($arBindings as $entityTypeID => $entityID) {
                 if (intval($entityID) > 0) {
                     $entityName = CCrmOwnerType::ResolveName($entityTypeID);
                     $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false);
                     $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false);
                     if ($entityName && intval($responsible_id) > 0) {
                         if (!array_key_exists($entityName, $arEntities)) {
                             $arEntities[$entityName] = array();
                         }
                         $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id);
                     }
                 }
             }
         }
     } else {
         $entityName = CCrmOwnerType::ResolveName($entityTypeID);
         $bOpened = CCrmOwnerType::isOpened($entityTypeID, $entityID, false);
         $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false);
         if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0) {
             if (!array_key_exists($entityName, $arEntities)) {
                 $arEntities[$entityName] = array();
             }
             $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => $bOpened, "RESPONSIBLE_ID" => $responsible_id);
         }
     }
     if (intval($arLogFields["LOG_ID"]) > 0 && in_array($arLogFields["EVENT_ID"], array("crm_lead_message", "crm_deal_message", "crm_contact_message", "crm_company_message"))) {
         $dbRight = CSocNetLogRights::GetList(array(), array("LOG_ID" => $arLogFields["LOG_ID"]));
         while ($arRight = $dbRight->Fetch()) {
             if (preg_match('/^(' . CCrmLiveFeedEntity::Contact . '|' . CCrmLiveFeedEntity::Lead . '|' . CCrmLiveFeedEntity::Company . '|' . CCrmLiveFeedEntity::Deal . ')(\\d+)$/', $arRight["GROUP_CODE"], $matches)) {
                 $entityTypeID = CCrmLiveFeedEntity::ResolveEntityTypeID($matches[1]);
                 $entityID = $matches[2];
                 $entityName = CCrmOwnerType::ResolveName($entityTypeID);
                 $responsible_id = CCrmOwnerType::GetResponsibleID($entityTypeID, $entityID, false);
                 if (!array_key_exists($entityName, $arEntities)) {
                     $arEntities[$entityName] = array();
                 }
                 if (intval($entityID) > 0 && $entityName && intval($responsible_id) > 0 && !array_key_exists($entityTypeID . "_" . $entityID, $arEntities[$entityName])) {
                     $arEntities[$entityName][$entityTypeID . "_" . $entityID] = array("ENTITY_TYPE_ID" => $entityTypeID, "ENTITY_ID" => $entityID, "ENTITY_NAME" => $entityName, "IS_OPENED" => CCrmOwnerType::isOpened($entityTypeID, $entityID, false), "RESPONSIBLE_ID" => $responsible_id);
                 }
             }
         }
     }
     $arUserID = array();
     foreach ($arEntities as $entityName => $arTmp) {
         $sSql = "SELECT RL.RELATION, RP.ATTR \n\t\t\t\tFROM b_crm_role_relation RL \n\t\t\t\tINNER JOIN b_crm_role_perms RP ON RL.ROLE_ID = RP.ROLE_ID AND RP.ENTITY = '" . $entityName . "' AND RP.PERM_TYPE = 'READ'\n\t\t\t";
         $res = $DB->Query($sSql, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         while ($row = $res->Fetch()) {
             $user_id = false;
             switch ($row["ATTR"]) {
                 case BX_CRM_PERM_SELF:
                     foreach ($arTmp as $arEntity) {
                         $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tUA.USER_ID = " . intval($arEntity["RESPONSIBLE_ID"]) . "\n\t\t\t\t\t\t\t\tAND UA.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'";
                         $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
                         if (($arUser = $rsUser->Fetch()) && !in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) {
                             $arUserID[] = $arUser["USER_ID"];
                         }
                     }
                     break;
                 case BX_CRM_PERM_ALL:
                 case BX_CRM_PERM_CONFIG:
                     $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tUA.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'";
                     $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
                     while ($arUser = $rsUser->Fetch()) {
                         if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) {
                             $arUserID[] = $arUser["USER_ID"];
                         }
                     }
                     break;
                 case BX_CRM_PERM_OPEN:
                     foreach ($arTmp as $arEntity) {
                         if ($arEntity["IS_OPENED"]) {
                             $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tUA.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'";
                             $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
                             while ($arUser = $rsUser->Fetch()) {
                                 if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) {
                                     $arUserID[] = $arUser["USER_ID"];
                                 }
                             }
                         }
                     }
                     break;
                 case BX_CRM_PERM_DEPARTMENT:
                     foreach ($arTmp as $arEntity) {
                         $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\tINNER JOIN b_user_access UA1 ON \n\t\t\t\t\t\t\t\tUA1.USER_ID = " . intval($arEntity["RESPONSIBLE_ID"]) . "\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE LIKE 'D%'\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE NOT LIKE 'DR%'\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE = UA.ACCESS_CODE\n\t\t\t\t\t\t\tINNER JOIN b_user_access UA2 ON \n\t\t\t\t\t\t\t\tUA2.USER_ID = UA.USER_ID\n\t\t\t\t\t\t\t\tAND UA2.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'";
                         $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
                         while ($arUser = $rsUser->Fetch()) {
                             if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) {
                                 $arUserID[] = $arUser["USER_ID"];
                             }
                         }
                     }
                     break;
                 case BX_CRM_PERM_SUBDEPARTMENT:
                     foreach ($arTmp as $arEntity) {
                         $strSQL = "SELECT UA.USER_ID \n\t\t\t\t\t\t\tFROM b_user_access UA \n\t\t\t\t\t\t\tINNER JOIN b_user_access UA1 ON \n\t\t\t\t\t\t\t\tUA1.USER_ID = " . intval($arEntity["RESPONSIBLE_ID"]) . "\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE LIKE 'DR%'\n\t\t\t\t\t\t\t\tAND UA1.ACCESS_CODE = UA.ACCESS_CODE\n\t\t\t\t\t\t\tINNER JOIN b_user_access UA2 ON \n\t\t\t\t\t\t\t\tUA2.USER_ID = UA.USER_ID\n\t\t\t\t\t\t\t\tAND UA2.ACCESS_CODE = '" . $DB->ForSQL($row["RELATION"]) . "'";
                         $rsUser = $DB->Query($strSQL, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
                         while ($arUser = $rsUser->Fetch()) {
                             if (!in_array($arUser["USER_ID"], $arUserID) && $arUser["USER_ID"] != $author_id) {
                                 $arUserID[] = $arUser["USER_ID"];
                             }
                         }
                     }
                     break;
             }
         }
     }
     $strSubscription = "";
     $cnt = 0;
     foreach ($arEntities as $entityName => $arTmp) {
         foreach ($arTmp as $arEntity) {
             if ($cnt > 0) {
                 $strSubscription .= " OR ";
             }
             $strSubscription .= "\n\t\t\t\t\tEXISTS (\n\t\t\t\t\t\t\tSELECT S.USER_ID \n\t\t\t\t\t\t\tFROM " . CCrmSonetSubscription::TABLE_NAME . " S \n\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\tS.SL_ENTITY_TYPE = '" . CCrmLiveFeedEntity::GetByEntityTypeID($arEntity["ENTITY_TYPE_ID"]) . "'\n\t\t\t\t\t\t\t\tAND S.ENTITY_ID = " . intval($arEntity["ENTITY_ID"]) . "\n\t\t\t\t\t\t\t\tAND U.ID = S.USER_ID\n\t\t\t\t\t\t) ";
             $cnt++;
         }
     }
     $strReturn = "SELECT \n\t\t\tU.ID as ID\n\t\t\t," . ($bDecrement ? "-1" : "1") . " as CNT\n\t\t\t,'**' as SITE_ID\n\t\t\t,'CRM_**' as CODE,\n\t\t\t0 as SENT\n\t\tFROM b_user U \n\t\tWHERE\n\t\t\t(\n\t\t\t\tU.ID IN (SELECT USER_ID FROM b_user_access WHERE ACCESS_CODE = 'G1' AND USER_ID <> " . $author_id . ")\n\t\t\t\t" . (!empty($arUserID) ? " OR U.ID IN (" . implode(",", $arUserID) . ") " : "") . "\n\t\t\t)" . (strlen($strSubscription) > 0 || intval($arLogFields["LOG_ID"]) > 0 ? "\n\t\t\t\t\tAND\n\t\t\t\t\t(\n\t\t\t\t\t\t" . $strSubscription . (intval($arLogFields["LOG_ID"]) > 0 ? (strlen($strSubscription) > 0 ? " OR " : "") . " \n\t\t\t\t\t\t\t\tEXISTS (\n\t\t\t\t\t\t\t\t\tSELECT GROUP_CODE \n\t\t\t\t\t\t\t\t\tFROM b_sonet_log_right LR\n\t\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\t\tLR.LOG_ID = " . intval($arLogFields["LOG_ID"]) . " \n\t\t\t\t\t\t\t\t\t\tAND LR.GROUP_CODE = " . $DB->Concat("'U'", $DB->type == "MSSQL" ? "CAST(U.ID as varchar(17))" : "U.ID") . "\n\t\t\t\t\t\t\t\t) " : "") . "\n\t\t\t\t\t)\n\t\t\t\t\t" : "");
     return $strReturn;
 }
Esempio n. 4
0
 public static function PrepareActivityItem(&$item, &$params, $options = null)
 {
     if (!is_array($options)) {
         $options = array();
     }
     $itemID = intval($item['ID']);
     if (!isset($item['SUBJECT'])) {
         $item['SUBJECT'] = '';
     }
     if (!isset($item['DESCRIPTION'])) {
         $item['DESCRIPTION'] = '';
     }
     if (!isset($item['LOCATION'])) {
         $item['LOCATION'] = '';
     }
     $typeID = isset($item['TYPE_ID']) ? intval($item['TYPE_ID']) : CCrmActivityType::Undefined;
     $item['TYPE_ID'] = $typeID;
     $direction = isset($item['DIRECTION']) ? intval($item['DIRECTION']) : CCrmActivityDirection::Undefined;
     $item['DIRECTION'] = $direction;
     $priority = isset($item['PRIORITY']) ? intval($item['PRIORITY']) : CCrmActivityPriority::None;
     $item['PRIORITY'] = $priority;
     $item['IS_IMPORTANT'] = $priority === CCrmActivityPriority::High;
     $completed = isset($item['COMPLETED']) ? $item['COMPLETED'] === 'Y' : false;
     $item['COMPLETED'] = $completed ? 'Y' : 'N';
     if ($typeID === CCrmActivityType::Task) {
         $taskID = isset($item['ASSOCIATED_ENTITY_ID']) ? intval($item['ASSOCIATED_ENTITY_ID']) : 0;
         $item['SHOW_URL'] = $taskID > 0 && isset($params['TASK_SHOW_URL_TEMPLATE']) ? CComponentEngine::MakePathFromTemplate($params['TASK_SHOW_URL_TEMPLATE'], array('user_id' => isset($params['USER_ID']) ? $params['USER_ID'] : CCrmSecurityHelper::GetCurrentUserID(), 'task_id' => $taskID)) : '';
         $item['DEAD_LINE'] = isset($item['DEADLINE']) ? $item['DEADLINE'] : (isset($item['END_TIME']) ? $item['END_TIME'] : '');
     } else {
         if (isset($params['ACTIVITY_SHOW_URL_TEMPLATE'])) {
             $item['SHOW_URL'] = CComponentEngine::makePathFromTemplate($params['ACTIVITY_SHOW_URL_TEMPLATE'], array('activity_id' => $itemID));
         }
         $item['DEAD_LINE'] = isset($item['DEADLINE']) ? $item['DEADLINE'] : (isset($item['START_TIME']) ? $item['START_TIME'] : '');
     }
     //OWNER_TITLE
     $ownerTitle = '';
     $ownerID = isset($item['OWNER_ID']) ? intval($item['OWNER_ID']) : 0;
     $item['OWNER_ID'] = $ownerID;
     $ownerTypeID = isset($item['OWNER_TYPE_ID']) ? intval($item['OWNER_TYPE_ID']) : 0;
     $item['OWNER_TYPE_ID'] = $ownerTypeID;
     if ($ownerID > 0 && $ownerTypeID > 0) {
         $ownerTitle = CCrmOwnerType::GetCaption($ownerTypeID, $ownerID);
     }
     $item['OWNER_TITLE'] = $ownerTitle;
     //OWNER_SHOW_URL
     $ownerShowUrl = '';
     if ($ownerID > 0) {
         if ($ownerTypeID === CCrmOwnerType::Lead) {
             $ownerShowUrl = isset($params['LEAD_SHOW_URL_TEMPLATE']) ? CComponentEngine::makePathFromTemplate($params['LEAD_SHOW_URL_TEMPLATE'], array('lead_id' => $ownerID)) : '';
         } elseif ($ownerTypeID === CCrmOwnerType::Contact) {
             $ownerShowUrl = isset($params['CONTACT_SHOW_URL_TEMPLATE']) ? CComponentEngine::makePathFromTemplate($params['CONTACT_SHOW_URL_TEMPLATE'], array('contact_id' => $ownerID)) : '';
         } elseif ($ownerTypeID === CCrmOwnerType::Company) {
             $ownerShowUrl = isset($params['COMPANY_SHOW_URL_TEMPLATE']) ? CComponentEngine::makePathFromTemplate($params['COMPANY_SHOW_URL_TEMPLATE'], array('company_id' => $ownerID)) : '';
         } elseif ($ownerTypeID === CCrmOwnerType::Deal) {
             $ownerShowUrl = isset($params['DEAL_SHOW_URL_TEMPLATE']) ? CComponentEngine::makePathFromTemplate($params['DEAL_SHOW_URL_TEMPLATE'], array('deal_id' => $ownerID)) : '';
         }
     }
     $item['OWNER_SHOW_URL'] = $ownerShowUrl;
     //IS_EXPIRED
     if ($item['COMPLETED'] === 'Y') {
         $item['IS_EXPIRED'] = false;
     } else {
         $time = isset($item['DEAD_LINE']) ? MakeTimeStamp($item['DEAD_LINE']) : 0;
         $item['IS_EXPIRED'] = $time !== 0 && $time <= time() + CTimeZone::GetOffset();
     }
     $responsibleID = isset($item['RESPONSIBLE_ID']) ? intval($item['RESPONSIBLE_ID']) : 0;
     $item['RESPONSIBLE_ID'] = $responsibleID;
     $item['RESPONSIBLE_SHOW_URL'] = $responsibleID > 0 ? CComponentEngine::MakePathFromTemplate($params['USER_PROFILE_URL_TEMPLATE'], array('user_id' => $responsibleID)) : '';
     $item['RESPONSIBLE_FORMATTED_NAME'] = $responsibleID > 0 && isset($params['NAME_TEMPLATE']) ? CUser::FormatName($params['NAME_TEMPLATE'], array('LOGIN' => isset($item['RESPONSIBLE_LOGIN']) ? $item['RESPONSIBLE_LOGIN'] : '', 'NAME' => isset($item['RESPONSIBLE_NAME']) ? $item['RESPONSIBLE_NAME'] : '', 'LAST_NAME' => isset($item['RESPONSIBLE_LAST_NAME']) ? $item['RESPONSIBLE_LAST_NAME'] : '', 'SECOND_NAME' => isset($item['RESPONSIBLE_SECOND_NAME']) ? $item['RESPONSIBLE_SECOND_NAME'] : ''), true, false) : '';
     //COMMUNICATIONS
     if ($itemID > 0 && isset($options['ENABLE_COMMUNICATIONS']) && $options['ENABLE_COMMUNICATIONS'] && !isset($item['COMMUNICATIONS'])) {
         $item['COMMUNICATIONS'] = CCrmActivity::GetCommunications($itemID);
     }
     $storageTypeID = isset($item['STORAGE_TYPE_ID']) ? intval($item['STORAGE_TYPE_ID']) : CCrmActivityStorageType::Undefined;
     if ($storageTypeID === CCrmActivityStorageType::Undefined || !CCrmActivityStorageType::IsDefined($storageTypeID)) {
         $storageTypeID = CCrmActivity::GetDefaultStorageTypeID();
     }
     $item['STORAGE_TYPE_ID'] = $storageTypeID;
     $item['FILES'] = array();
     $item['DISK_FILES'] = array();
     $item['WEBDAV_ELEMENTS'] = array();
     if (isset($options['ENABLE_FILES']) && $options['ENABLE_FILES']) {
         CCrmActivity::PrepareStorageElementIDs($item);
         CCrmActivity::PrepareStorageElementInfo($item);
     }
 }
Esempio n. 5
0
 function __CrmActivityListRenderGadgetItem(&$item, $displayClient, $editorID, $prefix)
 {
     static $ownerDataCache = array();
     $typeID = isset($item['~TYPE_ID']) ? intval($item['~TYPE_ID']) : CCrmActivityType::Undefined;
     $direction = isset($item['~DIRECTION']) ? intval($item['~DIRECTION']) : CCrmActivityDirection::Undefined;
     $deadline = isset($item['~DEADLINE']) ? $item['~DEADLINE'] : '';
     $completed = (isset($item['~COMPLETED']) ? $item['~COMPLETED'] : 'N') === 'Y';
     $expired = !$completed && $deadline !== '' && MakeTimeStamp($deadline) <= ($now = time() + CTimeZone::GetOffset());
     $subject = isset($item['~SUBJECT']) ? htmlspecialcharsbx($item['~SUBJECT']) : $item['~ID'];
     $descr = isset($item['~DESCRIPTION']) ? htmlspecialcharsbx($item['~DESCRIPTION']) : '';
     $priority = isset($item['~PRIORITY']) ? intval($item['~PRIORITY']) : CCrmActivityPriority::None;
     $js = "BX.CrmActivityEditor.viewActivity('{$editorID}', {$item['~ID']}, { 'enableInstantEdit':false, 'enableEditButton':false });";
     $containerClassName = 'crm-gadg-block crm-gadg-to-do';
     $typeTitle = '';
     if ($typeID === CCrmActivityType::Meeting) {
         $containerClassName .= ' crm-gadg-meeting';
         $typeTitle = htmlspecialcharsbx(GetMessage('CRM_ACTION_TYPE_MEETING'));
     } elseif ($typeID === CCrmActivityType::Call) {
         if ($direction === CCrmActivityDirection::Outgoing) {
             $containerClassName .= ' crm-gadg-call-outgoing';
             $typeTitle = htmlspecialcharsbx(GetMessage('CRM_ACTION_TYPE_CALL_OUTGOING'));
         } else {
             $containerClassName .= ' crm-gadg-call-incoming';
             $typeTitle = htmlspecialcharsbx(GetMessage('CRM_ACTION_TYPE_CALL_INCOMING'));
         }
     } elseif ($typeID === CCrmActivityType::Email) {
         if ($direction === CCrmActivityDirection::Outgoing) {
             $containerClassName .= ' crm-gadg-email-outgoing';
             $typeTitle = htmlspecialcharsbx(GetMessage('CRM_ACTION_TYPE_EMAIL_OUTGOING'));
         } else {
             $containerClassName .= ' crm-gadg-email-incoming';
             $typeTitle = htmlspecialcharsbx(GetMessage('CRM_ACTION_TYPE_EMAIL_INCOMING'));
         }
     } elseif ($typeID === CCrmActivityType::Task) {
         $containerClassName .= ' crm-gadg-task';
         $typeTitle = htmlspecialcharsbx(GetMessage('CRM_ACTION_TYPE_TASK'));
     }
     if (!$expired && $priority === CCrmActivityPriority::High) {
         $containerClassName .= ' crm-gadg-to-do-important';
     }
     if ($expired) {
         $containerClassName .= ' crm-gadg-red';
     }
     echo '<div class="', $containerClassName, '">';
     echo '<a href="#" onclick="', $js, ' return false;" class="crm-to-do-type" title="', $typeTitle, '"></a>';
     echo '<div class="crm-gadg-title">';
     echo '<a href="#" class="crm-gadg-link" title="', $subject, '" onclick="', $js, ' return false;" >', $subject, '</a>', '<span class="crm-gadg-title-deadline"> ', $deadline !== '' ? CCrmComponentHelper::TrimDateTimeString(FormatDate('FULL', MakeTimeStamp($deadline))) : '';
     if ($priority === CCrmActivityPriority::High) {
         echo '<span class="crm-gadg-to-do-important-icon" title="', htmlspecialcharsbx(GetMessage('CRM_ACTION_IMPORTANT')), '"></span>';
     }
     echo '</span>';
     if ($expired) {
         echo '<span class="crm-gadg-title-status"> ', htmlspecialcharsbx(GetMessage('CRM_ACTION_EXPIRED')), '</span>';
     } elseif ($completed) {
         echo '<span class="crm-gadg-title-status"> ', htmlspecialcharsbx(GetMessage('CRM_ACTION_COMPLETED')), '</span>';
     }
     echo '</div>';
     if ($descr !== '') {
         echo '<div class="crm-gadg-text">', $descr, '</div>';
     }
     echo '<div class="crm-gadg-footer">';
     $ownerTypeID = isset($item['OWNER_TYPE_ID']) ? intval($item['OWNER_TYPE_ID']) : 0;
     $ownerID = isset($item['OWNER_ID']) ? intval($item['OWNER_ID']) : 0;
     $referenceCaption = '';
     $referenceHtml = '';
     if ($ownerTypeID > 0 && $ownerID > 0) {
         if ($ownerTypeID === CCrmOwnerType::Lead) {
             $referenceCaption = htmlspecialcharsbx(GetMessage('CRM_ACTION_REFERENCE_LEAD'));
             $key = "{$ownerTypeID}_{$ownerID}";
             if (!(isset($ownerDataCache[$key]) && isset($ownerDataCache[$key]['TITLE']))) {
                 $dbRes = CCrmLead::GetListEx(array(), array('ID' => $ownerID), false, false, array('TITLE'));
                 if ($arRes = $dbRes->Fetch()) {
                     $ownerDataCache[$key] = array('TITLE' => $arRes['TITLE']);
                 }
             }
             $referenceHtml = CCrmViewHelper::PrepareEntityBaloonHtml(array('ENTITY_TYPE_ID' => CCrmOwnerType::Lead, 'ENTITY_ID' => $ownerID, 'PREFIX' => uniqid("{$prefix}_"), 'TITLE' => $ownerDataCache[$key]['TITLE'], 'CLASS_NAME' => 'crm-gadg-link'));
         } elseif ($ownerTypeID === CCrmOwnerType::Deal) {
             $referenceCaption = htmlspecialcharsbx(GetMessage('CRM_ACTION_REFERENCE_DEAL'));
             $key = "{$ownerTypeID}_{$ownerID}";
             if (!(isset($ownerDataCache[$key]) && isset($ownerDataCache[$key]['TITLE']))) {
                 $dbRes = CCrmDeal::GetListEx(array(), array('ID' => $ownerID), false, false, array('TITLE'));
                 if ($arRes = $dbRes->Fetch()) {
                     $ownerDataCache[$key] = array('TITLE' => $arRes['TITLE']);
                 }
             }
             $referenceHtml = CCrmViewHelper::PrepareEntityBaloonHtml(array('ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $ownerID, 'PREFIX' => uniqid("{$prefix}_"), 'TITLE' => $ownerDataCache[$key]['TITLE'], 'CLASS_NAME' => 'crm-gadg-link'));
         }
     }
     if ($referenceHtml !== '') {
         echo '<div class="crm-gadg-footer-row">';
         echo '<span class="crm-gadg-footer-left">', $referenceCaption, ':</span>';
         echo '<span class="crm-gadg-footer-right">', $referenceHtml, '</span>';
         echo '</div>';
     }
     if ($displayClient) {
         //Looking for first contact or company
         $clientHtml = '';
         $commLoaded = isset($item['COMMUNICATIONS_LOADED']) ? $item['COMMUNICATIONS_LOADED'] : true;
         if ($commLoaded && is_array($item['COMMUNICATIONS'])) {
             $comms = $item['COMMUNICATIONS'];
         } else {
             //Communications are disabled. Try to load first 3 communications to resolve client
             $comms = CCrmActivity::GetCommunications($item['~ID'], 3);
         }
         foreach ($comms as &$comm) {
             $commOwnerTypeID = isset($comm['ENTITY_TYPE_ID']) ? intval($comm['ENTITY_TYPE_ID']) : 0;
             $commOwnerID = isset($comm['ENTITY_ID']) ? intval($comm['ENTITY_ID']) : 0;
             if ($commOwnerTypeID <= 0 || $commOwnerID <= 0) {
                 continue;
             }
             $settings = isset($comm['ENTITY_SETTINGS']) ? $comm['ENTITY_SETTINGS'] : array();
             if ($commOwnerTypeID === CCrmOwnerType::Company) {
                 $clientHtml = CCrmViewHelper::PrepareClientBaloonHtml(array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $commOwnerID, 'PREFIX' => uniqid("{$prefix}_"), 'TITLE' => isset($settings['COMPANY_TITLE']) ? $settings['COMPANY_TITLE'] : '', 'CLASS_NAME' => 'crm-gadg-link'));
                 break;
             } elseif ($commOwnerTypeID === CCrmOwnerType::Contact) {
                 $clientHtml = CCrmViewHelper::PrepareClientBaloonHtml(array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $commOwnerID, 'PREFIX' => uniqid("{$prefix}_"), 'NAME' => isset($settings['NAME']) ? $settings['NAME'] : '', 'LAST_NAME' => isset($settings['LAST_NAME']) ? $settings['LAST_NAME'] : '', 'SECOND_NAME' => isset($settings['SECOND_NAME']) ? $settings['SECOND_NAME'] : '', 'CLASS_NAME' => 'crm-gadg-link'));
                 break;
             }
         }
         unset($comm);
         if ($clientHtml !== '') {
             echo '<div class="crm-gadg-footer-row">';
             echo '<span class="crm-gadg-footer-left">', htmlspecialcharsbx(GetMessage('CRM_ACTION_CUSTOMER')), ':</span>';
             echo '<span class="crm-gadg-footer-right">', $clientHtml, '</span>';
             echo '</div>';
         }
     }
     echo '</div>';
     echo '</div>';
 }