function VoteVoteEditFromArray($CHANNEL_ID, $VOTE_ID = false, $arFields = array(), $params = array()) { $CHANNEL_ID = intVal($CHANNEL_ID); if ($CHANNEL_ID <= 0 || empty($arFields)) { return false; } elseif (CVote::UserGroupPermission($CHANNEL_ID) <= 0) { return false; } $aMsg = array(); $params = is_array($params) ? $params : array(); $params["UNIQUE_TYPE"] = is_set($params, "UNIQUE_TYPE") ? intVal($params["UNIQUE_TYPE"]) : 20; $params["DELAY"] = is_set($params, "DELAY") ? intVal($params["DELAY"]) : 10; $params["DELAY_TYPE"] = is_set($params, "DELAY_TYPE") && in_array($params['DELAY_TYPE'], array("S", "M", "H", "D")) ? $params["DELAY_TYPE"] : "D"; $arVote = array(); $arQuestions = array(); $arFieldsQuestions = array(); $arFieldsVote = array("CHANNEL_ID" => $CHANNEL_ID, "AUTHOR_ID" => $GLOBALS["USER"]->GetID(), "UNIQUE_TYPE" => $params["UNIQUE_TYPE"], "DELAY" => $params["DELAY"], "DESCRIPTION_TYPE" => $params["DELAY_TYPE"]); if (!empty($arFields["DATE_START"])) { $arFieldsVote["DATE_START"] = $arFields["DATE_START"]; } if (!empty($arFields["DATE_END"])) { $arFieldsVote["DATE_END"] = $arFields["DATE_END"]; } if (!empty($arFields["TITLE"])) { $arFieldsVote["TITLE"] = $arFields["TITLE"]; } if (isset($arFields["ACTIVE"])) { $arFieldsVote["ACTIVE"] = $arFields["ACTIVE"]; } if (isset($arFields["NOTIFY"])) { $arFieldsVote["NOTIFY"] = $arFields["NOTIFY"]; } if (isset($arFields["URL"])) { $arFieldsVote["URL"] = $arFields["URL"]; } /************** Fatal errors ***************************************/ if (!CVote::CheckFields("UPDATE", $arFieldsVote)) { $e = $GLOBALS['APPLICATION']->GetException(); $aMsg[] = array("id" => "VOTE_ID", "text" => $e->GetString()); } elseif (intval($VOTE_ID) > 0) { $db_res = CVote::GetByID($VOTE_ID); if (!($db_res && ($res = $db_res->Fetch()))) { $aMsg[] = array("id" => "VOTE_ID", "text" => GetMessage("VOTE_VOTE_NOT_FOUND", array("#ID#", $VOTE_ID))); } elseif ($res["CHANNEL_ID"] != $CHANNEL_ID) { $aMsg[] = array("id" => "CHANNEL_ID", "text" => GetMessage("VOTE_CHANNEL_ID_ERR")); } else { $arVote = $res; $db_res = CVoteQuestion::GetList($arVote["ID"], $by = "s_id", $order = "asc", array(), $is_filtered); if ($db_res && ($res = $db_res->Fetch())) { do { $arQuestions[$res["ID"]] = $res + array("ANSWERS" => array()); } while ($res = $db_res->Fetch()); } $db_res = CVoteAnswer::GetListEx(array("ID" => "ASC"), array("VOTE_ID" => $arVote["ID"])); if ($db_res && ($res = $db_res->Fetch())) { do { $arQuestions[$res["QUESTION_ID"]]["ANSWERS"][$res["ID"]] = $res; } while ($res = $db_res->Fetch()); } } } if (!empty($aMsg)) { $e = new CAdminException(array_reverse($aMsg)); $GLOBALS["APPLICATION"]->ThrowException($e); return false; } /************** Fatal errors/***************************************/ if (!empty($arFieldsVote["TITLE"]) && !empty($arVote["TITLE"])) { $q = reset($arQuestions); if ($arVote["TITLE"] == substr($q["QUESTION"], 0, strlen($arVote["TITLE"]))) { unset($arFieldsVote["TITLE"]); } } /************** Check Data *****************************************/ // Questions $arFields["QUESTIONS"] = is_array($arFields["QUESTIONS"]) ? $arFields["QUESTIONS"] : array(); $iQuestions = 0; foreach ($arFields["QUESTIONS"] as $key => $arQuestion) { if ($arQuestion["DEL"] != "Y") { $arQuestion["ID"] = intval($arQuestion["ID"]); $arQuestion = array("ID" => $arQuestion["ID"] > 0 && is_set($arQuestions, $arQuestion["ID"]) ? $arQuestion["ID"] : false, "QUESTION" => trim($arQuestion["QUESTION"]), "ANSWERS" => is_array($arQuestion["ANSWERS"]) ? $arQuestion["ANSWERS"] : array()); $arAnswers = $arQuestion["ID"] > 0 ? $arQuestions[$arQuestion["ID"]]["ANSWERS"] : array(); foreach ($arQuestion["ANSWERS"] as $keya => $arAnswer) { $arAnswer["ID"] = intVal($arAnswer["ID"]); $arAnswer["MESSAGE"] = trim($arAnswer["MESSAGE"]); if (!empty($arAnswer["MESSAGE"]) && $arAnswer["DEL"] != "Y") { $arQuestion["ANSWERS"][$keya] = array("MESSAGE" => $arAnswer["MESSAGE"], "FIELD_TYPE" => $arAnswer["FIELD_TYPE"]); if ($arAnswer["ID"] > 0 && is_set($arAnswers, $arAnswer["ID"])) { $arQuestion["ANSWERS"][$keya]["ID"] = $arAnswer["ID"]; unset($arAnswers[$arAnswer["ID"]]); } } } } if ($arQuestion["DEL"] == "Y" || empty($arQuestion["QUESTION"]) || empty($arQuestion["ANSWERS"])) { if ($arQuestion["DEL"] != "Y" && !(empty($arQuestion["QUESTION"]) && empty($arQuestion["ANSWERS"]))) { $aMsg[] = array("id" => "QUESTION_" . $key, "text" => empty($arQuestion["QUESTION"]) ? GetMessage("VOTE_QUESTION_EMPTY", array("#NUMBER#" => $key)) : GetMessage("VOTE_ANSWERS_EMPTY", array("#QUESTION#" => $arQuestion["QUESTION"]))); } continue; } if ($arQuestion["ID"] > 0) { unset($arQuestions[$arQuestion["ID"]]); foreach ($arAnswers as $arAnswer) { $arQuestion["ANSWERS"][] = $arAnswer + array("DEL" => "Y"); } } $iQuestions++; $arFieldsQuestions[$key] = $arQuestion; } foreach ($arQuestions as $arQuestion) { $arFieldsQuestions[] = $arQuestion + array("DEL" => "Y"); } if (!empty($aMsg)) { $e = new CAdminException(array_reverse($aMsg)); $GLOBALS["APPLICATION"]->ThrowException($e); return false; } elseif (empty($arFieldsQuestions) && $VOTE_ID <= 0) { return true; } elseif ($params["bOnlyCheck"] == "Y") { return true; } /************** Check Data/*****************************************/ /************** Main actions with return ***************************/ if (empty($arFieldsVote["TITLE"])) { $q = reset($arFieldsQuestions); $arFieldsVote["TITLE"] = null; do { if ($q["DEL"] != "Y") { $arFieldsVote["TITLE"] = $q["QUESTION"]; break; } } while ($q = next($arFieldsQuestions)); reset($arFieldsQuestions); } if (empty($arVote)) { $arFieldsVote["UNIQUE_TYPE"] = $params["UNIQUE_TYPE"]; $arFieldsVote["DELAY"] = $params["DELAY"]; $arFieldsVote["DELAY_TYPE"] = $params["DELAY_TYPE"]; $arVote["ID"] = intval(CVote::Add($arFieldsVote)); } else { CVote::Update($VOTE_ID, $arFieldsVote); } if ($iQuestions > 0 && $arVote["ID"] > 0) { $iQuestions = 0; foreach ($arFieldsQuestions as $arQuestion) { if ($arQuestion["DEL"] == "Y") { CVoteQuestion::Delete($arQuestion["ID"]); continue; } elseif ($arQuestion["ID"] > 0) { $arQuestion["C_SORT"] = ($iQuestions + 1) * 10; CVoteQuestion::Update($arQuestion["ID"], $arQuestion); } else { $arQuestion["C_SORT"] = ($iQuestions + 1) * 10; $arQuestion["VOTE_ID"] = $arVote["ID"]; $arQuestion["ID"] = intVal(CVoteQuestion::Add($arQuestion)); if ($arQuestion["ID"] <= 0) { continue; } } $iQuestions++; $iAnswers = 0; foreach ($arQuestion["ANSWERS"] as $arAnswer) { if ($arAnswer["DEL"] == "Y") { CVoteAnswer::Delete($arAnswer["ID"]); continue; } if ($arAnswer["ID"] > 0) { $arAnswer["C_SORT"] = $iAnswers * 10; CVoteAnswer::Update($arAnswer["ID"], $arAnswer); } else { $arAnswer["QUESTION_ID"] = $arQuestion["ID"]; $arAnswer["C_SORT"] = ($iAnswers + 1) * 10; $arAnswer["ID"] = intVal(CVoteAnswer::Add($arAnswer)); if ($arAnswer["ID"] <= 0) { continue; } } $iAnswers++; } if ($iAnswers <= 0) { CVoteQuestion::Delete($arQuestion["ID"]); $iQuestions--; } } } if (intVal($arVote["ID"]) <= 0) { return false; } elseif ($iQuestions <= 0) { CVote::Delete($arVote["ID"]); return 0; } return $arVote["ID"]; /************** Actions/********************************************/ /* $arFields = array( "ID" => 345, "TITLE" => "test", "...", "QUESTIONS" => array( array( "ID" => 348, "QUESTION" => "test", "ANSWERS" => array( array( "ID" => 340, "MESSAGE" => "test"), array( "ID" => 0, "MESSAGE" => "test"), array( "ID" => 350, "DEL" => "Y", "MESSAGE" => "test") ) ), array( "ID" => 351, "DEL" => "Y", "QUESTION" => "test", "ANSWERS" => array( array( "ID" => 0, "MESSAGE" => "test"), array( "ID" => 478, "DEL" => "Y", "MESSAGE" => "test") ) ), array( "ID" => 0, "QUESTION" => "test", "ANSWERS" => array( array( "ID" => 0, "MESSAGE" => "test"), ) ), ) ); */ }
} else { $arIMAGE_ID = array(); } $arIMAGE_ID["del"] = ${"IMAGE_ID_del"}; $arIMAGE_ID["description"] = ${"IMAGE_ID_descr"}; $uniqSession = isset($_REQUEST['UNIQUE_TYPE_SESSION']) ? intval($_REQUEST['UNIQUE_TYPE_SESSION']) : 0; $uniqCookie = isset($_REQUEST['UNIQUE_TYPE_COOKIE']) ? intval($_REQUEST['UNIQUE_TYPE_COOKIE']) : 0; $uniqIP = isset($_REQUEST['UNIQUE_TYPE_IP']) ? intval($_REQUEST['UNIQUE_TYPE_IP']) : 0; $uniqID = isset($_REQUEST['UNIQUE_TYPE_USER_ID']) ? intval($_REQUEST['UNIQUE_TYPE_USER_ID']) : 0; $uniqIDNew = isset($_REQUEST['UNIQUE_TYPE_USER_ID_NEW']) ? intval($_REQUEST['UNIQUE_TYPE_USER_ID_NEW']) : 0; $uniqType = $uniqSession | $uniqCookie | $uniqIP | $uniqID | $uniqIDNew; $uniqType += 5; $arFields = array("CHANNEL_ID" => $_REQUEST["CHANNEL_ID"], "C_SORT" => intVal($_REQUEST["C_SORT"]), "ACTIVE" => $_REQUEST["ACTIVE"] == "Y" ? "Y" : "N", "DATE_START" => $_REQUEST["DATE_START"], "DATE_END" => $_REQUEST["DATE_END"], "TITLE" => $_REQUEST["TITLE"], "DESCRIPTION" => $_REQUEST["DESCRIPTION"], "DESCRIPTION_TYPE" => $_REQUEST["DESCRIPTION_TYPE"], "IMAGE_ID" => $arIMAGE_ID, "EVENT1" => $_REQUEST["EVENT1"], "EVENT2" => $_REQUEST["EVENT2"], "EVENT3" => $_REQUEST["EVENT3"], "UNIQUE_TYPE" => $uniqType, "DELAY" => $_REQUEST["DELAY"], "DELAY_TYPE" => $_REQUEST["DELAY_TYPE"], "TEMPLATE" => $_REQUEST["TEMPLATE"], "RESULT_TEMPLATE" => $_REQUEST["RESULT_TEMPLATE"], "NOTIFY" => $_REQUEST["NOTIFY"], "URL" => $_REQUEST["URL"]); $result = false; $arFields["IMAGE_ID"]["del"] = $_POST["IMAGE_ID_del"]; if (!CVote::CheckFields($ID > 0 ? "UPDATE" : "ADD", $arFields, $ID, array("CHECK_INTERSECTION" => "Y"))) { } elseif ($ID <= 0) { $arFields["AUTHOR_ID"] = $GLOBALS["USER"]->GetId(); $result = $ID = CVote::Add($arFields); } else { $result = CVote::Update($ID, $arFields); } if (!$result) { $e = $APPLICATION->GetException(); $message = new CAdminMessage(GetMessage("VOTE_GOT_ERROR"), $e); $bVarsFromForm = true; } else { if (isset($_REQUEST['COPYID']) && ($oldID = intval($_REQUEST['COPYID'])) > 0 && ($rCurrentVote = CVote::GetByID($oldID)) && ($arCurrentVote = $rCurrentVote->Fetch())) { global $DB; $newImageId = false; if (intval($arCurrentVote['IMAGE_ID']) > 0 && empty($arIMAGE_ID['name']) && $arIMAGE_ID['del'] != 'Y') {
public static function Update($ID, $arFields, $strUploadDir = false) { global $DB; $arBinds = array(); $strUploadDir = $strUploadDir === false ? "vote" : $strUploadDir; $ID = intVal($ID); if ($ID <= 0 || !CVote::CheckFields("UPDATE", $arFields, $ID)) { return false; } /***************** Event onBeforeVoteUpdate ************************/ foreach (GetModuleEvents("vote", "onBeforeVoteUpdate", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array(&$ID, &$arFields)) === false) { return false; } } /***************** /Event ******************************************/ if (empty($arFields)) { return false; } $arFields["~TIMESTAMP_X"] = $DB->GetNowFunction(); if (is_set($arFields, "DESCRIPTION")) { $arBinds["DESCRIPTION"] = $arFields["DESCRIPTION"]; } if (array_key_exists("IMAGE_ID", $arFields) && is_array($arFields["IMAGE_ID"]) && (!array_key_exists("MODULE_ID", $arFields["IMAGE_ID"]) || strlen($arFields["IMAGE_ID"]["MODULE_ID"]) <= 0)) { $arFields["IMAGE_ID"]["MODULE_ID"] = "vote"; } CFile::SaveForDB($arFields, "IMAGE_ID", $strUploadDir); $strUpdate = $DB->PrepareUpdateBind("b_vote", $arFields, $strUploadDir, false, $arBinds); if (!empty($strUpdate)) { $strSql = "UPDATE b_vote SET " . $strUpdate . " WHERE ID=" . $ID; $DB->QueryBind($strSql, $arBinds); } /***************** Event onAfterVoteUpdate *************************/ foreach (GetModuleEvents("vote", "onAfterVoteUpdate", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($ID, $arFields)); } /***************** /Event ******************************************/ return $ID; }
} InitBVar($find_id_exact_match); InitBVar($find_channel_exact_match); InitBVar($find_title_exact_match); InitBVar($find_description_exact_match); if (CheckFilter()) { $arFilter = array("ID" => $find_id, "ID_EXACT_MATCH" => $find_id_exact_match, "ACTIVE" => $find_active, "DATE_START_1" => $find_date_start_1, "DATE_START_2" => $find_date_start_2, "DATE_END_1" => $find_date_end_1, "DATE_END_2" => $find_date_end_2, "LAMP" => $find_lamp, "CHANNEL_ID" => $find_channel_id, "CHANNEL" => $find_channel, "CHANNEL_EXACT_MATCH" => $find_channel_exact_match, "TITLE" => $find_title, "TITLE_EXACT_MATCH" => $find_title_exact_match, "DESCRIPTION" => $find_description, "DESCRIPTION_EXACT_MATCH" => $find_description_exact_match, "COUNTER_1" => $find_counter_1, "COUNTER_2" => $find_counter_2); } if ($lAdmin->EditAction() && $VOTE_RIGHT >= "W" && check_bitrix_sessid()) { foreach ($FIELDS as $ID => $arFields) { if (!$lAdmin->IsUpdated($ID)) { continue; } $ID = intVal($ID); $arFieldsStore = array("ACTIVE" => $arFields['ACTIVE'], "C_SORT" => $arFields['C_SORT'], "TITLE" => $arFields['TITLE'], "CHANNEL_ID" => $arFields['CHANNEL_ID']); if (!CVote::CheckFields("UPDATE", $arFieldsStore, $ID, array("CHECK_INTERSECTION" => "Y"))) { $err = $GLOBALS['APPLICATION']->GetException(); $lAdmin->AddUpdateError($ID . ": " . $err->GetString(), $ID); } elseif (!CVote::Update($ID, $arFieldsStore)) { $lAdmin->AddUpdateError($ID . ": " . GetMessage("VOTE_SAVE_ERROR"), $ID); } } } // Group actions if (($arID = $lAdmin->GroupAction()) && $VOTE_RIGHT >= "W" && check_bitrix_sessid()) { if ($_REQUEST['action_target'] == 'selected') { $arID = array(); $rsData = CVote::GetList($by, $order, $arFilter, $is_filtered); while ($arRes = $rsData->Fetch()) { $arID[] = $arRes['ID']; }