<?php

function uport_field($label, $fieldid, $fieldtype, $defaultvalue, $allvalues, $description, $newrow, $valarr, $fieldclass = "", $attributes = "", $dependency_class = "") {

    $fieldid = str_replace(".", "-", $fieldid);

    /*    $eventstr = "";
      if(sizeof($eventarr) > 0 && is_array($eventarr)){
      foreach($eventarr as $thisevent) {
      $eventstr .= $thisevent;
      }
      }
     */
    $finalvalue = "";
    $subfinalvalue = "";
    $oftype = "";

    if (strpos($fieldclass, 'livetitle') !== false) {
        $oftype = "livetitle";
    }
    if (strpos($fieldclass, 'livedesc') !== false) {
        $oftype = "livedesc";
    }
    if (strpos($fieldclass, 'liveslug') !== false) {
        $oftype = "liveslug";
    }
    if (strpos($fieldclass, 'livebut') !== false) {
        $oftype = "livebut";
    }
    if (strpos($fieldclass, 'livebuthover') !== false) {
        $oftype = "livebuthover";
    }
    if (strpos($fieldclass, 'livemaskbg') !== false) {
        $oftype = "livemaskbg";
    }
    if (strpos($fieldclass, 'liveoutbg') !== false) {
        $oftype = "liveoutbg";
    }
    if (strpos($fieldclass, 'liveportbg') !== false) {
        $oftype = "liveportbg";
    }
    if (strpos($fieldclass, 'livecatul') !== false) {
        $oftype = "livecatul";
    }
    if (strpos($fieldclass, 'livecatli') !== false) {
        $oftype = "livecatli";
    }
    if (strpos($fieldclass, 'livecatlihover') !== false) {
        $oftype = "livecatlihover";
    }
    if (strpos($fieldclass, 'liveshadow') !== false) {
        $oftype = "liveshadow";
    }
    if (strpos($fieldclass, 'liveborder') !== false) {
        $oftype = "liveborder";
    }
    if (strpos($fieldclass, 'liveprice') !== false) {
        $oftype = "liveprice";
    }
    if (strpos($fieldclass, 'liveabove') !== false) {
        $oftype = "liveabove";
    }


    if (isset($valarr[$fieldid])) {
        if ($fieldtype == "checkbox" || $fieldtype == "radiobox" || $fieldtype == "checkbox_single" || $fieldtype == "select_multi") {
            $finalvalue = explode("|", $valarr[$fieldid]);
            $finalvalue = array_unique($finalvalue);
            $finalvalue = array_filter($finalvalue);
        } else if ($fieldtype == "effectlist" || $fieldtype == "elementlist") {

            $finalvalue = explode("|", $valarr[$fieldid]);
            $finalvalue = array_unique($finalvalue);
            $finalvalue = array_filter($finalvalue);
            $subfinalvalue = explode("|", $valarr[$fieldid . "sub"]);
            $subfinalvalue = array_unique($subfinalvalue);
            $subfinalvalue = array_filter($subfinalvalue);
        } else {
            $finalvalue = $valarr[$fieldid];
        }
    } else {
        if ($fieldtype == "checkbox" || $fieldtype == "radiobox" || $fieldtype == "checkbox_single" || $fieldtype == "select_multi") {
            $finalvalue = explode("|", $defaultvalue);
            $finalvalue = array_unique($finalvalue);
            $finalvalue = array_filter($finalvalue);
        } else if ($fieldtype == "effectlist" || $fieldtype == "elementlist") {
            $finalvalue = explode("|", $valarr[$fieldid]);
            $finalvalue = array_unique($finalvalue);
            $finalvalue = array_filter($finalvalue);
            $subfinalvalue = explode("|", $valarr[$fieldid . "sub"]);
            $subfinalvalue = array_unique($subfinalvalue);
            $subfinalvalue = array_filter($subfinalvalue);
        } else {
            $finalvalue = $defaultvalue;
        }
    }



    $setclass = " ".$dependency_class." ";
    if ($fieldtype == "rowstart") {
        $setclass = " fieldset  " . $fieldclass . " ";
    }
    if ($fieldtype == "rowstart" || $newrow == "1") {
        echo "<tr class='" . $setclass . "'><td class='tit'><span>" . $label . "";
        if($fieldtype != "sortlist"){echo ":";}
        echo "</span></td><td class='" . $fieldtype . "'>";
    }


    if ($fieldtype == "label") {
        if ($newrow == "0") {
            echo "<div class='int_label_wrap'><div class='setval label'><span class='inlabel'>" . $label . "</span></div>";
        }
    }

    if ($fieldtype == "labelend") {
        if ($newrow == "0") {
            echo "</div>";
        }
    }


    if ($fieldtype == "text" || $fieldtype == "integer") {
        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }
        echo "<input ".$attributes." data-fieldid='".$fieldid."'  type='text' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' value='" . $finalvalue . "' class='uport_" . $fieldid . " " . $fieldclass . " '>";

        if ($description != "") {
            echo "<div class='descinfo'>$description</div>";
        }

        if ($newrow == "0") {
            echo "</div>";
        }
    }



    if ($fieldtype == "integer_label") {
        if ($newrow == "0") {
            echo "<div class='setval intlabel'>";
        }

        if($finalvalue == ""){$finalvalue = 0;}

        echo "<input type='text' data-fieldid='".$fieldid."'  name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' value='" . $finalvalue . "' class='uport_" . $fieldid . " " . $fieldclass . " intlabel'>";
        echo "<span>" . $label . "</span>";
        if ($newrow == "0") {
            echo "</div>";
        }
    }



    if ($fieldtype == "integer_multi") {
        echo "<div class='multi_wrap'><input type='text' data-fieldid='".$fieldid."'  name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' value='" . $finalvalue . "' class='small_input_text uport_" . $fieldid . " " . $fieldclass . " '>";
        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval multi_label'><span class='inlabel'>" . $label . "</span>";
        }
        if ($newrow == "0") {
            echo "</div>";
        }
        if ($description != "") {
            echo "<div class='descinfo'>$description</div>";
        }
        echo "</div>";
    }






    if ($fieldtype == "color") {
        if ($newrow == "0") {
            echo "<div class='setval'><span class='incolor'>" . $label . "</span>";
        }
        echo "<input type='text' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' value='" . $finalvalue . "' data-oftype='" . $oftype . "' class='my-color-field uport_" . $fieldid . " " . $fieldclass . " '>";
        if ($newrow == "0") {
            echo "</div>";
        }
    }




    if ($fieldtype == "sortlist") {
        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }
        echo "<div id='uport_" . $fieldid . "_dragdrop'></div><input data-fieldid='".$fieldid."'  type='hidden' 
        name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' value='" . $finalvalue . "' 
        class='uport_" . $fieldid . " " . $fieldclass . " '>";

        if ($description != "") {
            echo "<div class='descinfo'>$description</div>";
        }

        if ($newrow == "0") {
            echo "</div>";
        }
    }






    if ($fieldtype == "select") {

        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }

        echo "<select name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' data-fieldid='".$fieldid."' class='uport_" . $fieldid . " " . $fieldclass . " '>";
        $exp = explode("|", $allvalues);
        foreach ($exp as $val) {
            $expval = explode(":", $val);
            if (sizeof($expval) == 2) {
                $val = $expval[0];
                $disp = $expval[1];
            } else {
                $val = $disp = $expval[0];
            }
            if (trim($finalvalue) == trim($val)) {
                $sel = "selected";
            } else {
                $sel = "";
            }
            echo "<option " . $sel . " value='" . trim($val) . "'>" . trim($disp) . "</option>";
        }
        echo "</select>";
        if ($description != "") {
            echo "<div class='descinfo'>$description</div>";
        }
        if ($newrow == "0") {
            echo "</div>";
        }
    }


    if ($fieldtype == "select_multi_ctrl") {

        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }
        //echo $allvalues;
        //echo $finalvalue;
        echo "<select multiple style='min-height:200px;' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "' data-fieldid='".$fieldid."' class='uport_" . $fieldid . " " . $fieldclass . " '>";
        $exp = explode("|", $allvalues);
        foreach ($exp as $val) {
            if(trim($val) != ""){
            $expval = explode(":", $val);
            if (sizeof($expval) == 2) {
                $val = $expval[0];
                $disp = $expval[1];
            } else {
                $val = $disp = $expval[0];
            }
            if(in_array(trim($val), array_unique(array_filter(explode("|",$finalvalue))) )){
                $sel = "selected";
            } else {
                $sel = "";
            }
            echo "<option " . $sel . " value='" . trim($val) . "'>" . trim($disp) . "</option>";
        }
        }
        echo "</select>";
        if ($description != "") {
            echo "<div class='descinfo'>$description</div>";
        }
        if ($newrow == "0") {
            echo "</div>";
        }
    }



    if ($fieldtype == "select_multi") {
        $multival_str = "";
        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }

        echo "<select data-id='uport_" . $fieldid . "' data-fieldid='".$fieldid."'  class='" . $fieldclass . " '>";
        $exp = explode("|", $allvalues);
        foreach ($exp as $val) {
            $expval = explode(":", $val);
            if (sizeof($expval) == 2) {
                $val = $expval[0];
                $disp = $expval[1];
            } else {
                $val = $disp = $expval[0];
            }
            if (is_array($finalvalue) && in_array(trim($val), $finalvalue)) {
                $sel = "";
                $multival_str .= "<span class='multiselectval' data-field='uport_".$fieldid."' id='".trim($val)."'>".trim($disp)."<span>x</span></span>";
            } else {
                $sel = "";
            }
            echo "<option " . $sel . " value='" . trim($val) . "'>" . trim($disp) . "</option>";
        }
        echo "</select>"; 
        if ($description != "") {
            echo "<div class='descinfo largescreen'>$description</div>";
        }

        echo "<div class='clearfix'></div><input id='uport_" . $fieldid . "' name='uport_" . $fieldid . "' type='hidden' class=' uport_" . $fieldid . " ' value='|".$valarr[$fieldid]."|'>";
        echo "<div class=' multiselectval_wrap uport_" . $fieldid . "_tags '>".$multival_str."</div>";

        if ($description != "") {
            echo "<div class='descinfo smallscreen'>$description</div>";
        }

        if ($newrow == "0") {
            echo "</div>";
        }
    }






//print_r($finalvalue);
    if ($fieldtype == "checkbox" || $fieldtype == "checkbox_single") {
        if ($newrow == "0") {
            echo "<div class='setval'>";
        }

        if ($fieldtype == "checkbox" && trim($label) != "") {
            echo "<span class='inlabel'>" . $label . "</span>";
        }
//echo "<select name='uport_".$fieldid."' id='uport_".$fieldid."'>";
        $exp = explode("|", $allvalues);
        foreach ($exp as $val) {
            if (trim($val)) {
                $expval = explode(":", $val);
                if (sizeof($expval) == 2) {
                    $val = $expval[0];
                    $disp = $expval[1];
                } else {
                    $val = $disp = $expval[0];
                }
                if (is_array($finalvalue) && in_array(trim($val), $finalvalue)) {
                    $sel = "checked";
                } else {
                    $sel = "";
                }

                if ($fieldtype == "checkbox_single") {
                    $single = "checkbox_single";
                } else {
                    $single = "";
                }
                echo "<div class='pickdiv " . $single . "'><input type='checkbox' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "_" . trim($val) . "' value='" . trim($val) . "' " . $sel . "  class='uport_" . $fieldid . " " . $fieldclass . " '>
<label for='uport_" . $fieldid . "_" . trim($val) . "'>" . trim($disp) . "</label></div>";
//echo "<option $sel value='".trim($val)."'>".trim($disp)."</option>";
            }
        }
//echo "</select>";

        if ($description != "") {
            echo "<div class='clearboth'></div><div class='descinfo' style='padding-left:0px;'>$description</div>";
        }

        if ($newrow == "0") {
            echo "</div>";
        }
    }







    if ($fieldtype == "radiobox") {
        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }
        $exp = explode("|", $allvalues);
        foreach ($exp as $val) {
            if (trim($val)) {
                $expval = explode(":", $val);
                if (sizeof($expval) == 2) {
                    $val = $expval[0];
                    $disp = $expval[1];
                } else {
                    $val = $disp = $expval[0];
                }
                if (is_array($finalvalue) && in_array(trim($val), $finalvalue)) {
                    $sel = "checked";
                } else {
                    $sel = "";
                }
                echo "<div class='pickdiv'><input type='radio' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "_" . trim($val) . "' value='" . trim($val) . "' " . $sel . "  class='uport_" . $fieldid . " " . $fieldclass . " '>
<label for='uport_" . $fieldid . "_" . trim($val) . "'>" . trim($disp) . "</label></div>";
            }
        }

        if ($description != "") {
            echo "<div class='clearboth'></div><div class='descinfo' style='padding-left:0px;'>$description</div>";
        }
        if ($newrow == "0") {
            echo "</div>";
        }
    }





    if ($fieldtype == "effectlist" || $fieldtype == "elementlist") {
        //print_r($valarr);
        if ($newrow == "0" && trim($label) != "") {
            echo "<div class='setval'><span class='inlabel'>" . $label . "</span>";
        }
        $exp = explode("|", $allvalues);
        //print_r($exp);
        foreach ($exp as $v) {
            if (trim($v)) {
                $expval = explode(":", $v);
                if (sizeof($expval) == 2) {
                    $val = $expval[0];
                    $disp = $expval[1];
                } else {
                    $val = $disp = $expval[0];
                }

                $matches = array();
                preg_match_all('/(\[(.*?)\])/s', $disp, $matches);

                if (is_array($matches) && !empty($matches)) {

                    $subexp = explode("#", $matches[2][0]);
                    $sub = array();

                    foreach ($subexp as $subv) {

                        if (trim($subv)) {
                            $subexpval = explode(";", $subv);
                            if (sizeof($subexpval) == 2) {
                                $subval = $subexpval[0];
                                $subdisp = $subexpval[1];
                                $sub[$val][$subval] = $subdisp;
                            } else {
                                $subval = $subdisp = $subexpval[0];
                                $sub[$val][$subval] = $subdisp;
                            }
                        }
                    }
                }

                $dispexp = explode("[", $disp);
                $disp = $dispexp[0];

                //echo $val; echo $finalvalue;
                //print_r($finalvalue);
                if (is_array($finalvalue) && in_array(trim($val), $finalvalue)) {
                    $sel = "checked";
                    $substyle = "";
                    $selparent = $val;
                } else {
                    $sel = "";
                    $substyle = " style='display:none;'";
                    $selparent = "";
                }

                if ($fieldtype == "effectlist") {

                    echo "<div class='pickdiv'>";
                    echo "<input type='radio' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "_" . trim($val) . "' value='" . trim($val) . "' " . $sel . " data-effect='" . $val . "' class='uport_effect uport_" . $fieldid . " " . $fieldclass . " '>";
                    echo "<label for='uport_" . $fieldid . "_" . trim($val) . "'>" . trim($disp) . "</label>";
                    echo "</div>";

                    //print_r($subfinalvalue);
                    if (sizeof($sub) > 0) {
                        foreach ($sub[$val] as $sval => $sdisp) {
                            if (is_array($subfinalvalue) && in_array(trim($sval), $subfinalvalue) && $selparent == $val) {
                                $subsel = "checked";
                            } else {
                                $subsel = "";
                            }

                            echo "<div " . $substyle . " class='pickdiv uport_effect_sublist uport_effect_sublist_" . $val . "'>";
                            echo "<input type='radio' name='uport_" . $fieldid . "sub' id='uport_" . $fieldid . "_" . trim($val) . "_" . trim($sval) . "' value='" . trim($sval) . "' " . $subsel . "  data-parent-effect='" . $val . "' data-subeffect='" . $sval . "' class='uport_subeffect uport_subeffect_" . $val . "_" . $sval . " uport_subeffect_of_" . $val . " uport_" . $fieldid . "sub " . $fieldclass . " '>";
                            echo "<label for='uport_" . $fieldid . "_" . trim($val) . "_" . trim($sval) . "'>" . trim($sdisp) . "</label>";
                            echo "</div>";
                        }
                    }
                    echo "<div style='clear:both;width:100%'></div>";
                } else if ($fieldtype == "elementlist") {

                    echo "<div class='pickdiv'>";
                    echo "<input type='checkbox' name='uport_" . $fieldid . "' id='uport_" . $fieldid . "_" . trim($val) . "' value='" . trim($val) . "' " . $sel . " data-element='" . $val . "' class='uport_element uport_" . $fieldid . " " . $fieldclass . " '>";
                    echo "<label for='uport_" . $fieldid . "_" . trim($val) . "'>" . trim($disp) . "</label>";
                    echo "</div>";

                    //print_r($subfinalvalue);

                    if (sizeof($sub) > 0) {
                        foreach ($sub[$val] as $sval => $sdisp) {
                            if (is_array($subfinalvalue) && in_array($val . "_" . trim($sval), $subfinalvalue) && $selparent == $val) {
                                $subsel = "checked";
                            } else {
                                $subsel = "";
                            }

                            echo "<div " . $substyle . " class='pickdiv uport_element_sublist uport_element_sublist_" . $val . "'>";
                            echo "<input type='radio' name='uport_" . $fieldid . "_" . $val . "sub' id='uport_" . $fieldid . "_" . trim($val) . "_" . trim($sval) . "' value='" . trim($sval) . "' " . $subsel . "  data-parent-element='" . $val . "' data-subelement='" . $sval . "' data-textid='uport_" . $fieldid . "sub' class='uport_subelement uport_subelement_" . $val . "_" . $sval . " uport_subelement_of_" . $val . " " . $fieldclass . " '>";
                            echo "<label for='uport_" . $fieldid . "_" . trim($val) . "_" . trim($sval) . "'>" . trim($sdisp) . "</label>";
                            echo "</div>";
                        }
                    }
                    echo "<div style='clear:both;width:100%'></div>";
                }
            }
        }

        if ($fieldtype == "elementlist") {
            echo "<input type='hidden' name='uport_" . $fieldid . "sub' id='uport_" . $fieldid . "sub' value='" . $valarr[$fieldid . 'sub'] . "|' class='uport_" . $fieldid . "sub'>";
        }


        if ($description != "") {
            echo "<div class='clearboth'></div><div class='descinfo' style='padding-left:0px;'>$description</div>";
        }
        if ($newrow == "0") {
            echo "</div>";
        }
    }





    if ($newrow == "1") {
        if (trim($description) != "") {
            //echo "<div class='descinfo' style='clear:both;padding-left: 0px;'>$description</div>";
        }
        echo "</div></td></tr>";
    }



    if ($fieldtype == "rowend") {
        if (trim($description) != "") {
            echo "<div class='descinfo  " . $fieldclass . " ' style='clear:both;padding-left: 0px;'>$description</div>";
        }
        echo "</div></td></tr>";
    }
}

?>