Example #1
0
	public static function AddLead($FORM_ID, $RESULT_ID, $arLink = null)
	{
		$FORM_ID = intval($FORM_ID);
		$RESULT_ID = intval($RESULT_ID);

		if ($FORM_ID <= 0 || $RESULT_ID <= 0)
			return false;

		if (!is_array($arLink))
		{
			$dbRes = CFormCrm::GetByFormID($FORM_ID);
			$arLink = $dbRes->Fetch();
		}

		if (!$arLink)
			return false;

		$arResultFields = array();
		$arAnswers = array();

		CFormResult::GetDataByID($RESULT_ID, array(), $arResultFields, $arAnswers);

		$ob = new CFormCrmSender($arLink['CRM_ID']);
		$arCrmF = $ob->GetFields();
		$arCrmFields = array();
		foreach ($arCrmF as $ar)
			$arCrmFields[$ar['ID']] = $ar;

		$arLeadFields = array();
		$dbRes = CFormCrm::GetFields($arLink['ID']);
		while ($arRes = $dbRes->Fetch())
		{
			if (intval($arRes['FIELD_ID']) > 0)
			{
				$bFound = false;
				foreach ($arAnswers as $sid => $arAnswer)
				{
					foreach ($arAnswer as $answer_id => $arAns)
					{
						if ($arAns['FIELD_ID'] == $arRes['FIELD_ID'])
						{
							$bFound = true;
							if ($arCrmFields[$arRes['CRM_FIELD']])
							{
								switch ($arCrmFields[$arRes['CRM_FIELD']]['TYPE'])
								{
									case 'enum':
									{
										if ($arAns['FIELD_TYPE'] == 'dropdown' || $arAns['FIELD_TYPE'] == 'checkbox' || $arAns['FIELD_TYPE'] == 'radio' || $arAns['FIELD_TYPE'] == 'multiselect')
										{
											$arLeadFields[$arRes['CRM_FIELD']] = $arAns['VALUE'];
										}
										else
										{
											$arLeadFields[$arRes['CRM_FIELD']] = $arAns['ANSWER_TEXT'];
										}
									}
									break;
									case 'boolean':
										$arLeadFields[$arRes['CRM_FIELD']] = 'Y';
									break;
									default:
										$arLeadFields[$arRes['CRM_FIELD']] = (strlen($arAns['USER_TEXT']) > 0
											? $arAns['USER_TEXT']
											: (
												strlen($arAns['ANSWER_TEXT']) > 0
												? $arAns['ANSWER_TEXT']
												: $arAns['VALUE']
											)
										);
								}
							}
						}
					}
				}

				if (!$bFound && $arCrmFields[$arRes['CRM_FIELD']] && $arCrmFields[$arRes['CRM_FIELD']]['TYPE'] == 'boolean')
				{
					$arLeadFields[$arRes['CRM_FIELD']] = 'N';
				}
			}
			elseif (strlen($arRes['FIELD_ALT']) > 0)
			{
				switch($arRes['FIELD_ALT'])
				{
					case 'RESULT_ID':
						$arLeadFields[$arRes['CRM_FIELD']] = $arResultFields['ID'];
					break;
					case 'FORM_SID':
						$arLeadFields[$arRes['CRM_FIELD']] = $arResultFields['SID'];
					break;
					case 'FORM_NAME':
						$arLeadFields[$arRes['CRM_FIELD']] = $arResultFields['NAME'];
					break;
					case 'SITE_ID':
						$arLeadFields[$arRes['CRM_FIELD']] = SITE_ID;
					break;
					case 'FORM_ALL':
						$arLeadFields[$arRes['CRM_FIELD']] = self::_getAllFormFields($FORM_ID, $RESULT_ID, $arAnswers);
					break;
					case 'FORM_ALL_HTML':
						$arLeadFields[$arRes['CRM_FIELD']] = self::_getAllFormFieldsHTML($FORM_ID, $RESULT_ID, $arAnswers);
					break;
				}
			}
		}

		$result = $ob->AddLead($arLeadFields);
		if ($result->code() != 201)
		{
			$GLOBALS['APPLICATION']->ThrowException($result->error(), $result->code());
			return false;
		}
		else
		{
			CFormResult::SetCRMFlag($RESULT_ID, 'Y');
			return $result->field('ID');
		}
	}
Example #2
0
    $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
}
CModule::IncludeModule('form');
$action = $_REQUEST['action'];
$result = '{"result":"error"}';
if (check_bitrix_sessid()) {
    switch ($action) {
        case 'get_fields':
        case 'check':
            $CRM_ID = intval($_REQUEST['ID']);
            if ($CRM_ID > 0) {
                $arAuth = null;
                if (strlen($_REQUEST['LOGIN']) > 0 && strlen($_REQUEST['PASSWORD']) > 0) {
                    $arAuth = array('LOGIN' => $_REQUEST['LOGIN'], 'PASSWORD' => $_REQUEST['PASSWORD']);
                }
                $link = new CFormCrmSender($CRM_ID, $arAuth);
                $arFields = $link->GetFields($_REQUEST['reload'] == 'Y');
                if (is_array($arAuth)) {
                    $authHash = $link->GetAuthHash();
                }
                if (is_array($arFields) && count($arFields) > 0) {
                    $result = '{"result":"ok","fields":' . CUtil::PhpToJsObject($arFields) . (is_array($arAuth) ? ',"auth_hash":"' . $authHash . '"' : '') . '}';
                } else {
                    $res = $link->GetLastResult();
                    if ($res) {
                        $result = '{"result":"error","error":"' . CUtil::JSEscape($res->field('error_message')) . '"}';
                    }
                }
            }
            break;
        case 'add_lead':