} else { if ($arID = $lAdmin->GroupAction()) { if ($_REQUEST['action_target'] == 'selected') { $arID = array(); $rsData = CVoteQuestion::GetList($VOTE_ID, $by, $order, $arFilter, $is_filtered); while ($arRes = $rsData->Fetch()) { $arID[] = $arRes['ID']; } } @set_time_limit(0); foreach ($arID as $ID) { $ID = intval($ID); if ($ID > 0) { switch ($_REQUEST['action']) { case "delete": if (!CVoteQuestion::Delete($ID)) { $lAdmin->AddGroupError(GetMessage("DELETE_ERROR"), $ID); } break; case "activate": case "deactivate": CVoteQuestion::setActive($ID, $_REQUEST['action'] == "activate"); break; } } } } } } $rsData = CVoteQuestion::GetList($VOTE_ID, $by, $order, $arFilter, $is_filtered); $rsData = new CAdminResult($rsData, $sTableID);
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"), ) ), ) ); */ }
public static function Delete($ID) { global $DB; $err_mess = CVote::err_mess() . "<br>Function: Delete<br>Line: "; $ID = intval($ID); if ($ID <= 0) { return false; } /***************** Event onBeforeVoteDelete *************************/ foreach (GetModuleEvents("vote", "onBeforeVoteDelete", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array(&$ID)) === false) { return false; } } /***************** /Event ******************************************/ @set_time_limit(1000); $DB->StartTransaction(); // delete questions CVoteQuestion::Delete(false, $ID); // delete vote images $strSql = "SELECT IMAGE_ID FROM b_vote WHERE ID = " . $ID . " AND IMAGE_ID > 0"; $z = $DB->Query($strSql, false, $err_mess . __LINE__); while ($zr = $z->Fetch()) { CFile::Delete($zr["IMAGE_ID"]); } // delete vote events $DB->Query("DELETE FROM b_vote_event WHERE VOTE_ID='{$ID}'", false, $err_mess . __LINE__); // delete vote $res = $DB->Query("DELETE FROM b_vote WHERE ID='{$ID}'", false, $err_mess . __LINE__); $DB->Commit(); /***************** Event onAfterVoteDelete *************************/ foreach (GetModuleEvents("vote", "onAfterVoteDelete", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($ID)); } /***************** /Event ******************************************/ return $res; }