示例#1
0
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"), 
    					)
    				), 
    			)
    		);
    */
}
示例#2
0
 function Copy($ID, $newVoteID)
 {
     $err_mess = CAllVoteQuestion::err_mess() . "<br>Function: Copy<br>Line: ";
     $ID = intVal($ID);
     if ($ID <= 0) {
         return false;
     }
     $newVoteID = intVal($newVoteID);
     if ($newVoteID <= 0) {
         return false;
     }
     $res = CVoteQuestion::GetByID($ID);
     if (!($arQuestion = $res->Fetch())) {
         return false;
     }
     $arQuestion['VOTE_ID'] = $newVoteID;
     unset($arQuestion['ID']);
     $newQuestionID = CVoteQuestion::Add($arQuestion);
     if ($newQuestionID === false) {
         return false;
     }
     $state = true;
     $rAnswers = CVoteAnswer::GetList($ID);
     while ($arAnswer = $rAnswers->GetNext()) {
         $arAnswer['QUESTION_ID'] = $newQuestionID;
         unset($arAnswer['ID']);
         unset($arAnswer['~ID']);
         $state = $state && CVoteAnswer::Add($arAnswer) !== false;
     }
     if (!$state) {
         return $state;
     }
     CVoteQuestion::Reset($newQuestionID);
     return $newQuestionID;
 }
示例#3
0
 public static function Copy($ID, $newVoteID)
 {
     $ID = intVal($ID);
     if ($ID <= 0) {
         return false;
     }
     $newVoteID = intVal($newVoteID);
     if ($newVoteID <= 0) {
         return false;
     }
     $res = CVoteQuestion::GetByID($ID);
     if (!($arQuestion = $res->Fetch())) {
         return false;
     }
     $arQuestion['VOTE_ID'] = $newVoteID;
     unset($arQuestion['ID']);
     $newQuestionID = CVoteQuestion::Add($arQuestion);
     if ($newQuestionID === false) {
         return false;
     }
     $state = true;
     $rAnswers = CVoteAnswer::GetList($ID);
     while ($arAnswer = $rAnswers->Fetch()) {
         $arAnswer['QUESTION_ID'] = $newQuestionID;
         unset($arAnswer['ID']);
         $state = $state && CVoteAnswer::Add($arAnswer) !== false;
     }
     if (!$state) {
         return $state;
     }
     CVoteQuestion::Reset($newQuestionID);
     return $newQuestionID;
 }
示例#4
0
 $_FILES["IMAGE_ID"] = is_array($_FILES["IMAGE_ID"]) ? $_FILES["IMAGE_ID"] : array();
 $arFields = array("ACTIVE" => isset($_REQUEST["ACTIVE"]) ? $_REQUEST["ACTIVE"] : 'N', "VOTE_ID" => $VOTE_ID, "C_SORT" => $_REQUEST["C_SORT"], "QUESTION" => $_REQUEST["QUESTION"], "QUESTION_TYPE" => $_REQUEST["QUESTION_TYPE"], "IMAGE_ID" => $_FILES["IMAGE_ID"] + ($_REQUEST["IMAGE_ID_del"] == "Y" ? array("del" => "Y") : array()), "DIAGRAM" => isset($_REQUEST["DIAGRAM"]) ? $_REQUEST["DIAGRAM"] : 'N', "REQUIRED" => isset($_REQUEST["REQUIRED"]) ? $_REQUEST["REQUIRED"] : 'N', "DIAGRAM_TYPE" => $_REQUEST["DIAGRAM_TYPE"], "TEMPLATE" => $_REQUEST["TEMPLATE"], "TEMPLATE_NEW" => $_REQUEST["TEMPLATE_NEW"]);
 foreach ($_REQUEST["ANSWER"] as $pid) {
     if (intval($pid) <= 0) {
         continue;
     }
     $arAnswer = array("ID" => intVal($_REQUEST["ANSWER_ID_" . $pid]), "QUESTION_ID" => $ID, "ACTIVE" => $_REQUEST["ACTIVE_" . $pid] == 'Y' ? 'Y' : 'N', "C_SORT" => $_REQUEST["C_SORT_" . $pid], "MESSAGE" => $_REQUEST["MESSAGE_" . $pid] != ' ' ? trim($_REQUEST["MESSAGE_" . $pid]) : ' ', "FIELD_TYPE" => $_REQUEST["FIELD_TYPE_" . $pid], "FIELD_WIDTH" => intVal($_REQUEST["FIELD_WIDTH_" . $pid]), "FIELD_HEIGHT" => intVal($_REQUEST["FIELD_HEIGHT_" . $pid]), "FIELD_PARAM" => trim($_REQUEST["FIELD_PARAM_" . $pid]), "COLOR" => trim($_REQUEST["COLOR_" . $pid]));
     $arAnswersFields[$pid] = $arAnswer;
     if ($arAnswer["ID"] <= 0 && empty($arAnswer["MESSAGE"])) {
         unset($arAnswersFields[$pid]);
     }
 }
 if ($ID > 0) {
     $result = CVoteQuestion::Update($ID, $arFields);
 } else {
     $result = $ID = CVoteQuestion::Add($arFields);
 }
 $aMsg = array();
 if (!$result) {
     $bVarsFromForm = true;
 } else {
     foreach ($arAnswersFields as $pid => $arAnswer) {
         $bResult = true;
         $APPLICATION->ResetException();
         if ($_REQUEST["del_" . $pid] == "Y") {
             if ($arAnswer["ID"] > 0) {
                 CVoteAnswer::Delete($arAnswer["ID"]);
             }
             unset($arAnswersFields[$pid]);
         } elseif ($arAnswer["ID"] > 0) {
             $bResult = CVoteAnswer::Update($arAnswer["ID"], $arAnswer);