<?php

/*===============================================*\
|| ############################################# ||
|| # JAKWEB.CH / Version 2.1.1                 # ||
|| # ----------------------------------------- # ||
|| # Copyright 2022 JAKWEB All Rights Reserved # ||
|| ############################################# ||
\*===============================================*/

// Check if the file is accessed only via index.php if not stop the script from running
if (!defined('JAK_ADMIN_PREVENT_ACCESS')) die('You cannot access this file directly.');

// Check if the user has access to this file
if (!jak_get_access("settings", $jakuser->getVar("permissions"), JAK_MAIN_OP)) jak_redirect(BASE_URL);

// enter number
$lic_nr = false;

// All the tables we need for this plugin
$errors = array();
$jaktable = 'ticketpriority';
$jaktable1 = 'customfields';
$jaktable2 = 'support_departments';
$jaktable3 = 'clients';
$jaktable4 = 'support_tickets';
$jaktable5 = 'translations';
$jaktable6 = 'ticketoptions';
$jaktable7 = 'support_status';
$jaktable8 = 'contacts';

// Call the language function
$lang_files = jak_get_lang_files(JAK_LANG);

// Now start with the plugin use a switch to access all pages
switch ($page1) {

  case 'ticketstatus':
  # code...
  switch ($page2) {
    case 'delete':
    # code...

    // Check if user exists and can be deleted
    if (is_numeric($page3) && jak_row_exist($page3, $opcacheid ,$jaktable7)) {

      $single_status = $jakdb->get($jaktable7, ["dorder"], ["AND" => ["id" => $page3, "opid" => $opcacheid], "ORDER" => ["dorder" => "ASC"]]);

      if ($single_status > 4) {

        // Now check how many languages are installed and do the dirty work
        $result = $jakdb->delete($jaktable7, ["AND" => ["id" => $page3, "opid" => $opcacheid]]);

        if ($result->rowCount() != 1) {
          $_SESSION["infomsg"] = $jkl['i'];
          jak_redirect($_SESSION['LCRedirect']);
        } else {

          // Now let us delete the user cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

          // Write the log file each time someone tries to login before
          JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 107, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

          $_SESSION["successmsg"] = $jkl['g14'];
          jak_redirect($_SESSION['LCRedirect']);
        }

      } else {

        $_SESSION["errormsg"] = $jkl['i3'];
        jak_redirect($_SESSION['LCRedirect']);

      }

    } else {

      $_SESSION["errormsg"] = $jkl['i3'];
      jak_redirect($_SESSION['LCRedirect']);
    }
    break;
    case 'edit':
        // Check if the option exists
    if (is_numeric($page3) && jak_row_exist($page3, $opcacheid, $jaktable7)) {

      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $jkp = $_POST;

        if (empty($jkp['jak_title'])) {
          $errors['e'] = $jkl['e2'];
        }

        if (count($errors) == 0) {

          $result = $jakdb->update($jaktable7, ["title" => $jkp['jak_title'],
            "class" => $jkp['jak_class'],
            "oponly" => $jkp['jak_oponly'],
            "reminder" => $jkp['jak_reminder'],
            "closed" => $jkp['jak_closed'],
            "dorder" => $jkp['jak_order'],
            "edited" => $jakdb->raw("NOW()")], ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
          
          if (!$result) {
            $_SESSION["infomsg"] = $jkl['i'];
            jak_redirect($_SESSION['LCRedirect']);
          } else {

            // Now let us delete the user cache file
            $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
            if (file_exists($cachestufffile)) {
              unlink($cachestufffile);
            }

            // Now we store the translations, that will be nasty
            if (isset($lang_files) && is_array($lang_files)) foreach($lang_files as $lf) {
              if (isset($jkp['title_'.$lf]) && !empty($jkp['title_'.$lf])) {
                    // Now check if the language already exist
                if ($jakdb->has($jaktable5, ["AND" => ["opid" => $opcacheid, "tstatus" => $page3, "lang" => $lf]])) {
                  $jakdb->update($jaktable5, ["title" => $jkp['title_'.$lf]], ["AND" => ["opid" => $opcacheid, "tstatus" => $page3, "lang" => $lf]]);
                } else {
                  $jakdb->insert($jaktable5, ["opid" => $opcacheid, "lang" => $lf, "tstatus" => $page3, "title" => $jkp['title_'.$lf], "time" => $jakdb->raw("NOW()")]);
                }
              }
                  // Delete the entry
              if (isset($jkp['deltrans_'.$lf]) && !empty($jkp['deltrans_'.$lf])) {
                $jakdb->delete($jaktable5, ["AND" => ["opid" => $opcacheid, "tstatus" => $page3, "lang" => $jkp['deltrans_'.$lf]]]);
              }
            }

            // Write the log file each time someone tries to login before
            JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 105, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);
          }
          
        // Output the errors
        } else {
          $errors = $errors;
        }
      }

      // Title and Description
      $SECTION_TITLE = $jkl["hd220"];
      $SECTION_DESC = "";

      // Get translations
      $JAK_PRIO_TRANSLATION = $jakdb->select($jaktable5, ["id", "lang", "title"], ["AND" => ["opid" => $opcacheid, "tstatus" => $page3]]);

      // Get the data
      $JAK_FORM_DATA = jak_get_data($page3, $opcacheid, $jaktable7);

      // Call the template
      $template = 'editticketstatus.php';

    } else {

      $_SESSION["errormsg"] = $jkl['i3'];
      jak_redirect($_SESSION['LCRedirect']);
    }

    break;
    default:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $jkp = $_POST;

      if (isset($jkp['corder']) && isset($jkp['real_status_id'])) {

        $dorders = $jkp['corder'];
        $depid = $jkp['real_status_id'];
        $dep = array_combine($depid, $dorders);
        $updatesql = '';       

        foreach ($dep as $key => $order) {
          $result = $jakdb->update($jaktable7, ["dorder" => $order], ["id" => $key]);
        }

        if (!$result) {
          $_SESSION["infomsg"] = $jkl['i'];
          jak_redirect($_SESSION['LCRedirect']);
        } else {

          // Now let us delete the user cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

          $_SESSION["successmsg"] = $jkl['g14'];
          jak_redirect($_SESSION['LCRedirect']);
        }

      }

      if (isset($_POST['insert_tstatus'])) {

        if (empty($jkp['jak_title'])) {
          $errors['e'] = $jkl['e2'];
        }

        if (count($errors) == 0) {

          // Get the next order
          $last = $jakdb->get($jaktable7, "dorder", ["opid" => $opcacheid,  "ORDER" => ["dorder" => "DESC"]]);
          $last = $last + 1;

          $jakdb->insert($jaktable7, ["opid" => $opcacheid,
            "title" => $jkp['jak_title'],
            "class" => $jkp['jak_class'],
            "oponly" => $jkp['jak_oponly'],
            "reminder" => $jkp['jak_reminder'],
            "closed" => $jkp['jak_closed'],
            "dorder" => $last,
            "edited" => $jakdb->raw("NOW()"),
            "created" => $jakdb->raw("NOW()")]);

          $lastid = $jakdb->id();

          if (!$lastid) {
            $_SESSION["infomsg"] = $jkl['i'];
            jak_redirect($_SESSION['LCRedirect']);
          } else {

            // Now let us delete the user cache file
            $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
            if (file_exists($cachestufffile)) {
              unlink($cachestufffile);
            }

              // Write the log file each time someone tries to login before
            JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 106, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);

            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);
          }

        // Output the errors
        } else {

          $errors = $errors;
        }

      }

    }

    // Get all support status
    $STATUS_ALL = $jakdb->select($jaktable7, ["id", "title", "class", "dorder"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);

    // Title and Description
    $SECTION_TITLE = $jkl["hd322"];
    $SECTION_DESC = "";

    // Include the javascript file for results
    $js_file_footer = 'js_sortable.php';

    // Call the template
    $template = 'ticketstatus.php';
    break;
  }
  
  break;
  case 'options':
    # code...
  switch ($page2) {
    case 'delete':
        # code...
        // Check if user exists and can be deleted
    if (is_numeric($page3) && jak_row_exist($page3, $opcacheid ,$jaktable6)) {
      
          // Now check how many languages are installed and do the dirty work
      $result = $jakdb->delete($jaktable6, ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
      
      if (!$result) {
        $_SESSION["infomsg"] = $jkl['i'];
        jak_redirect($_SESSION['LCRedirect']);
      } else {
        
          // Now let us delete the user cache file
        $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
        if (file_exists($cachestufffile)) {
          unlink($cachestufffile);
        }

          // Write the log file each time someone tries to login before
        JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 93, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
        
        $_SESSION["successmsg"] = $jkl['g14'];
        jak_redirect($_SESSION['LCRedirect']);
      }
      
    } else {
      
      $_SESSION["errormsg"] = $jkl['i3'];
      jak_redirect($_SESSION['LCRedirect']);
    }
    break;
    case 'edit':
        // Check if the option exists
    if (is_numeric($page3) && jak_row_exist($page3, $opcacheid ,$jaktable6)) {
      
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $jkp = $_POST;
        
        if (empty($jkp['jak_title'])) {
          $errors['e'] = $jkl['e2'];
        }
        
        if (count($errors) == 0) {

          $result = $jakdb->update($jaktable6, ["depid" => $jkp['jak_depid'],
            "title" => $jkp['jak_title'],
            "icon" => $jkp['jak_icon'],
            "oponly" => $jkp['jak_oponly'],
            "credits" => $jkp['credits'],
            "dorder" => $jkp['jak_order'],
            "edited" => $jakdb->raw("NOW()")], ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
          
          if (!$result) {
            $_SESSION["infomsg"] = $jkl['i'];
            jak_redirect($_SESSION['LCRedirect']);
          } else {
            
                // Now let us delete the user cache file
            $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
            if (file_exists($cachestufffile)) {
              unlink($cachestufffile);
            }

                // Now we store the translations, that will be nasty
            if (isset($lang_files) && is_array($lang_files)) foreach($lang_files as $lf) {
              if (isset($jkp['title_'.$lf]) && !empty($jkp['title_'.$lf])) {
                    // Now check if the language already exist
                if ($jakdb->has($jaktable5, ["AND" => ["opid" => $opcacheid, "toptionid" => $page3, "lang" => $lf]])) {
                  $jakdb->update($jaktable5, ["title" => $jkp['title_'.$lf]], ["AND" => ["opid" => $opcacheid, "toptionid" => $page3, "lang" => $lf]]);
                } else {
                  $jakdb->insert($jaktable5, ["opid" => $opcacheid, "lang" => $lf, "toptionid" => $page3, "title" => $jkp['title_'.$lf], "time" => $jakdb->raw("NOW()")]);
                }
              }
                  // Delete the entry
              if (isset($jkp['deltrans_'.$lf]) && !empty($jkp['deltrans_'.$lf])) {
                $jakdb->delete($jaktable5, ["AND" => ["opid" => $opcacheid, "toptionid" => $page3, "lang" => $jkp['deltrans_'.$lf]]]);
              }
            }

                // Write the log file each time someone tries to login before
            JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 91, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
            
            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);
          }
          
            // Output the errors
        } else {
          $errors = $errors;
        }
      }
      
          // Title and Description
      $SECTION_TITLE = $jkl["hd220"];
      $SECTION_DESC = "";
      
          // Get all departments
      $JAK_DEPARTMENTS = $jakdb->select($jaktable2, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);

          // Get translations
      $JAK_PRIO_TRANSLATION = $jakdb->select($jaktable5, ["id", "lang", "title"], ["AND" => ["opid" => $opcacheid, "toptionid" => $page3]]);
      
          // Get the data
      $JAK_FORM_DATA = jak_get_data($page3, $opcacheid, $jaktable6);

          // Call the template
      $template = 'editticketoptions.php';
      
    } else {
      
      $_SESSION["errormsg"] = $jkl['i3'];
      jak_redirect($_SESSION['LCRedirect']);
    }

    break;
    default:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $jkp = $_POST;

      if (isset($jkp['corder']) && isset($jkp['real_option_id'])) {

        $dorders = $jkp['corder'];
        $depid = $jkp['real_option_id'];
        $dep = array_combine($depid, $dorders);
        $updatesql = '';       

        foreach ($dep as $key => $order) {
          $result = $jakdb->update($jaktable6, ["dorder" => $order], ["id" => $key]);
        }

        if (!$result) {
          $_SESSION["infomsg"] = $jkl['i'];
          jak_redirect($_SESSION['LCRedirect']);
        } else {

              // Now let us delete the define cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/stuff.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

          $_SESSION["successmsg"] = $jkl['g14'];
          jak_redirect($_SESSION['LCRedirect']);
        }

      }

      if (isset($jkp['insert_ticketoptions'])) {
        
        if (empty($jkp['jak_title'])) {
          $errors['e'] = $jkl['e2'];
        }
        
        if (count($errors) == 0) {

                  // Get the next order
          $last = $jakdb->get($jaktable6, "dorder", ["opid" => $opcacheid, "ORDER" => ["dorder" => "DESC"]]);
          $last = $last + 1;

          $jakdb->insert($jaktable6, ["opid" => $opcacheid,
            "depid" => $jkp['jak_depid'],
            "title" => $jkp['jak_title'],
            "icon" => $jkp['jak_icon'],
            "oponly" => $jkp['jak_oponly'],
            "credits" => $jkp['credits'],
            "dorder" => $last,
            "edited" => $jakdb->raw("NOW()"),
            "created" => $jakdb->raw("NOW()")]);

          $lastid = $jakdb->id();

          if (!$lastid) {
            $_SESSION["infomsg"] = $jkl['i'];
            jak_redirect($_SESSION['LCRedirect']);
          } else {
            
                  // Now let us delete the user cache file
            $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
            if (file_exists($cachestufffile)) {
              unlink($cachestufffile);
            }

                // Write the log file each time someone tries to login before
            JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 92, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
            
            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);
          }
          
              // Output the errors
        } else {
          
          $errors = $errors;
        }

      }
      
    }

        // Get all departments
    $JAK_DEPARTMENTS = $jakdb->select($jaktable2, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
    
        // Get all responses
    $TOPTIONS_ALL = $jakdb->select($jaktable6, ["id", "title", "icon", "dorder"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
    
        // Title and Description
    $SECTION_TITLE = $jkl["hd224"];
    $SECTION_DESC = "";

        // Include the javascript file for results
    $js_file_footer = 'js_sortable.php';
    
        // Call the template
    $template = 'ticketoptions.php';
    break;
  }
  
  break;

  case 'form':

  switch ($page2) {
    case 'delete':
        # code...
        // Check if user exists and can be deleted
    if (is_numeric($page3) && jak_row_exist($page3, $opcacheid ,$jaktable1)) {

          // Get the data
      $getdata = jak_get_data($page3, $opcacheid, $jaktable1);
      
          // Now check how many languages are installed and do the dirty work
      $result = $jakdb->delete($jaktable1, ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
      
      if (!$result) {
        $_SESSION["infomsg"] = $jkl['i'];
        jak_redirect($_SESSION['LCRedirect']);
      } else {
        
          // Now let us delete the user cache file
        $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
        if (file_exists($cachestufffile)) {
          unlink($cachestufffile);
        }

          // Delete translations
        $jakdb->delete($jaktable5, ["AND" => ["opid" => $opcacheid, "customfieldid" => $page3]]);

          // Write the log file each time someone tries to login before
        JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 90, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
        
        $_SESSION["successmsg"] = $jkl['g14'];
        jak_redirect($_SESSION['LCRedirect']);
      }
      
    } else {
      
      $_SESSION["errormsg"] = $jkl['i3'];
      jak_redirect($_SESSION['LCRedirect']);
    }
    break;
    case 'edit':
        # code...
        // Check if the user exists
    if (is_numeric($page3) && jak_row_exist($page3, $opcacheid, $jaktable1)) {
      
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $jkp = $_POST;
        
        if (empty($jkp['jak_title'])) {
          $errors['e'] = $jkl['e2'];
        }

        if (isset($jkp['jak_fieldtype']) && ($jkp['jak_fieldtype'] == 2 || $jkp['jak_fieldtype'] == 3)) {
          if (empty($jkp['jak_field_html'])) {
            $errors['e2'] = $jkl['hd131'];
          }
        }
        
        if (count($errors) == 0) {

          $result = $jakdb->update($jaktable1, ["title" => $jkp['jak_title'],
            "depid" => $jkp['jak_depid'],
            "field_html" => $jkp['jak_field_html'],
            "fieldtype" => $jkp['jak_fieldtype'],
            "mandatory" => $jkp['jak_mandatory'],
            "onregister" => $jkp['jak_onregister'],
            "dorder" => $jkp['order']], ["AND" => ["id" => $page3, "opid" => $opcacheid]]);
          
          if (!$result) {
            $_SESSION["infomsg"] = $jkl['i'];
            jak_redirect($_SESSION['LCRedirect']);
          } else {
            
              // Now let us delete the user cache file
            $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
            if (file_exists($cachestufffile)) {
              unlink($cachestufffile);
            }

              // Now we store the translations, that will be nasty
            if (isset($lang_files) && is_array($lang_files)) foreach($lang_files as $lf) {
              if (isset($jkp['title_'.$lf]) && !empty($jkp['title_'.$lf])) {
                  // Now check if the language already exist
                if ($jakdb->has($jaktable5, ["AND" => ["opid" => $opcacheid, "customfieldid" => $page3, "lang" => $lf]])) {
                  $jakdb->update($jaktable5, ["title" => $jkp['title_'.$lf], "description" => $jkp['field_html_'.$lf]], ["AND" => ["opid" => $opcacheid, "customfieldid" => $page3, "lang" => $lf]]);
                } else {
                  $jakdb->insert($jaktable5, ["opid" => $opcacheid, "lang" => $lf, "customfieldid" => $page3, "title" => $jkp['title_'.$lf], "description" => $jkp['field_html_'.$lf], "time" => $jakdb->raw("NOW()")]);
                }
              }
                // Delete the entry
              if (isset($jkp['deltrans_'.$lf]) && !empty($jkp['deltrans_'.$lf])) {
                $jakdb->delete($jaktable5, ["AND" => ["opid" => $opcacheid, "customfieldid" => $page3, "lang" => $jkp['deltrans_'.$lf]]]);
              }
            }

              // Write the log file each time someone tries to login before
            JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 88, $page3, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
            
            $_SESSION["successmsg"] = $jkl['g14'];
            jak_redirect($_SESSION['LCRedirect']);
          }
          
          // Output the errors
        } else {
          $errors = $errors;
        }
        
      }
      
          // Title and Description
      $SECTION_TITLE = $jkl["hd153"];
      $SECTION_DESC = "";

          // Get translations
      $JAK_FIELD_TRANSLATION = $jakdb->select($jaktable5, ["id", "lang", "title", "description"], ["AND" => ["opid" => $opcacheid, "customfieldid" => $page3]]);
      
          // Get all departments
      $JAK_DEPARTMENTS = $jakdb->select($jaktable2, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
      
          // Get the data
      $JAK_FORM_DATA = jak_get_data($page3, $opcacheid, $jaktable1);

          // Call the template
      $template = 'editcustomfield.php';
      
    } else {
      
      $_SESSION["errormsg"] = $jkl['i3'];
      jak_redirect($_SESSION['LCRedirect']);
    }
    break;
    default:
        # code...

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['insert_customfield'])) {
      $jkp = $_POST;
      
      if (empty($jkp['jak_title'])) {
        $errors['e'] = $jkl['e2'];
      }
      
      if (empty($jkp['jak_slug']) || !preg_match('/^([a-z-_0-9]||[-_])+$/', $jkp['jak_slug'])) {
        $errors['e1'] = $jkl['e14'];
      }

      if (isset($jkp['jak_fieldtype']) && ($jkp['jak_fieldtype'] == 2 || $jkp['jak_fieldtype'] == 3)) {
        if (empty($jkp['jak_field_html'])) {
          $errors['e2'] = $jkl['hd131'];
        }
      }
      
      if (count($errors) == 0) {

              // Get the next order
        $last = $jakdb->get($jaktable1, "dorder", ["opid" => $opcacheid, "ORDER" => ["dorder" => "DESC"]]);
        $last = $last + 1;

        $jakdb->insert($jaktable1, ["opid" => $opcacheid,
          "title" => $jkp['jak_title'],
          "fieldlocation" => $jkp['jak_fieldloc'],
          "val_slug" => $jkp['jak_slug'],
          "depid" => $jkp['jak_depid'],
          "field_html" => $jkp['jak_field_html'],
          "fieldtype" => $jkp['jak_fieldtype'],
          "mandatory" => $jkp['jak_mandatory'],
          "onregister" => $jkp['jak_onregister'],
          "dorder" => $last,
          "active" => 1,
          "time" => $jakdb->raw("NOW()")]);

        $lastid = $jakdb->id();

        if (!$lastid) {
          $_SESSION["infomsg"] = $jkl['i'];
          jak_redirect($_SESSION['LCRedirect']);
        } else {
          
                // Now let us delete the user cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

              // Write the log file each time someone tries to login before
          JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 89, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
          
          $_SESSION["successmsg"] = $jkl['g14'];
          jak_redirect($_SESSION['LCRedirect']);
        }
        
            // Output the errors
      } else {
        
        $errors = $errors;
      }  
      
    }

        // Get all departments
    $JAK_DEPARTMENTS = $jakdb->select($jaktable2, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
    
        // Get all custom fields
    $CUSTOMF_ALL = jak_get_page_info($jaktable1, $opcacheid);
    
        // Title and Description
    $SECTION_TITLE = $jkl["hd148"];
    $SECTION_DESC = "";
    
        // Include the javascript file for results
    $js_file_footer = 'js_customfield.php';
    
        // Call the template
    $template = 'customfield.php';
    break;
  }

  break;
  case 'delete':
  
    // Check if user exists and can be deleted
  if (is_numeric($page2) && jak_row_exist($page2, $opcacheid ,$jaktable)) {

    
      // Now check how many languages are installed and do the dirty work
    $result = $jakdb->delete($jaktable, ["AND" => ["id" => $page2, "opid" => $opcacheid]]);
    
    if (!$result) {
      $_SESSION["infomsg"] = $jkl['i'];
      jak_redirect($_SESSION['LCRedirect']);
    } else {
      
      // Now let us delete the user cache file
      $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
      if (file_exists($cachestufffile)) {
        unlink($cachestufffile);
      }

      // Delete translations
      $jakdb->delete($jaktable5, ["AND" => ["opid" => $opcacheid, "priorityid" => $page3]]);

      // Write the log file each time someone tries to login before
      JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 87, $page2, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
      
      $_SESSION["successmsg"] = $jkl['g14'];
      jak_redirect($_SESSION['LCRedirect']);
    }
    
  } else {
    
    $_SESSION["errormsg"] = $jkl['i3'];
    jak_redirect($_SESSION['LCRedirect']);
  }
  
  break;
  case 'edit':
  
    // Check if the user exists
  if (is_numeric($page2) && jak_row_exist($page2, $opcacheid, $jaktable)) {
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $jkp = $_POST;
      
      if (empty($jkp['jak_title'])) {
        $errors['e'] = $jkl['e2'];
      }
      
      if (count($errors) == 0) {

        $result = $jakdb->update($jaktable, ["depid" => $jkp['jak_depid'],
          "title" => $jkp['jak_title'],
          "class" => $jkp['jak_class'],
          "oponly" => $jkp['jak_oponly'],
          "credits" => $jkp['credits'],
          "dorder" => $jkp['jak_order'],
          "edited" => $jakdb->raw("NOW()")], ["AND" => ["id" => $page2, "opid" => $opcacheid]]);
        
        if (!$result) {
          $_SESSION["infomsg"] = $jkl['i'];
          jak_redirect($_SESSION['LCRedirect']);
        } else {
          
            // Now let us delete the user cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

            // Now we store the translations, that will be nasty
          if (isset($lang_files) && is_array($lang_files)) foreach($lang_files as $lf) {
            if (isset($jkp['title_'.$lf]) && !empty($jkp['title_'.$lf])) {
                // Now check if the language already exist
              if ($jakdb->has($jaktable5, ["AND" => ["opid" => $opcacheid, "priorityid" => $page2, "lang" => $lf]])) {
                $jakdb->update($jaktable5, ["title" => $jkp['title_'.$lf]], ["AND" => ["opid" => $opcacheid, "priorityid" => $page2, "lang" => $lf]]);
              } else {
                $jakdb->insert($jaktable5, ["opid" => $opcacheid, "lang" => $lf, "priorityid" => $page2, "title" => $jkp['title_'.$lf], "time" => $jakdb->raw("NOW()")]);
              }
            }
              // Delete the entry
            if (isset($jkp['deltrans_'.$lf]) && !empty($jkp['deltrans_'.$lf])) {
              $jakdb->delete($jaktable5, ["AND" => ["opid" => $opcacheid, "priorityid" => $page2, "lang" => $jkp['deltrans_'.$lf]]]);
            }
          }

            // Write the log file each time someone tries to login before
          JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 85, $page2, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
          
          $_SESSION["successmsg"] = $jkl['g14'];
          jak_redirect($_SESSION['LCRedirect']);
        }
        
        // Output the errors
      } else {
        $errors = $errors;
      }
    }
    
      // Title and Description
    $SECTION_TITLE = $jkl["hd152"];
    $SECTION_DESC = "";
    
      // Get all departments
    $JAK_DEPARTMENTS = $jakdb->select($jaktable2, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);

      // Get translations
    $JAK_PRIO_TRANSLATION = $jakdb->select($jaktable5, ["id", "lang", "title"], ["AND" => ["opid" => $opcacheid, "priorityid" => $page2]]);
    
      // Get the data
    $JAK_FORM_DATA = jak_get_data($page2, $opcacheid, $jaktable);

      // Call the template
    $template = 'editticketpriority.php';
    
  } else {
    
    $_SESSION["errormsg"] = $jkl['i3'];
    jak_redirect($_SESSION['LCRedirect']);
  }
  
  break;
  default:
  
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $jkp = $_POST;

    if (isset($jkp['corder']) && isset($jkp['real_priority_id'])) {

      $dorders = $jkp['corder'];
      $depid = $jkp['real_priority_id'];
      $dep = array_combine($depid, $dorders);
      $updatesql = '';       

      foreach ($dep as $key => $order) {
        $result = $jakdb->update($jaktable, ["dorder" => $order], ["id" => $key]);
      }

      if (!$result) {
        $_SESSION["infomsg"] = $jkl['i'];
        jak_redirect($_SESSION['LCRedirect']);
      } else {

                 // Now let us delete the define cache file
        $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/stuff.php';
        if (file_exists($cachestufffile)) {
          unlink($cachestufffile);
        }

        $_SESSION["successmsg"] = $jkl['g14'];
        jak_redirect($_SESSION['LCRedirect']);
      }

    }

    if (isset($_POST['insert_priority']))  {
      
      if (empty($jkp['jak_title'])) {
        $errors['e'] = $jkl['e2'];
      }
      
      if (count($errors) == 0) {

              // Get the next order
        $last = $jakdb->get($jaktable, "dorder", ["opid" => $opcacheid, "ORDER" => ["dorder" => "DESC"]]);
        $last = $last + 1;

        $jakdb->insert($jaktable, ["opid" => $opcacheid,
          "depid" => $jkp['jak_depid'],
          "title" => $jkp['jak_title'],
          "class" => $jkp['jak_class'],
          "oponly" => $jkp['jak_oponly'],
          "credits" => $jkp['credits'],
          "dorder" => $last,
          "edited" => $jakdb->raw("NOW()"),
          "created" => $jakdb->raw("NOW()")]);

        $lastid = $jakdb->id();

        if (!$lastid) {
          $_SESSION["infomsg"] = $jkl['i'];
          jak_redirect($_SESSION['LCRedirect']);
        } else {
          
              // Now let us delete the user cache file
          $cachestufffile = APP_PATH.JAK_CACHE_DIRECTORY.'/opcache'.$opcacheid.'.php';
          if (file_exists($cachestufffile)) {
            unlink($cachestufffile);
          }

            // Write the log file each time someone tries to login before
          JAK_base::jakWhatslog('', $opcacheid, JAK_USERID, 0, 86, $lastid, (isset($_COOKIE['WIOgeoData']) ? $_COOKIE['WIOgeoData'] : ''), $jakuser->getVar("username"), $_SERVER['REQUEST_URI'], $ipa, $valid_agent);
          
          $_SESSION["successmsg"] = $jkl['g14'];
          jak_redirect($_SESSION['LCRedirect']);
        }
        
          // Output the errors
      } else {
        
        $errors = $errors;
      }

    }
    
  }

    // Get all departments
  $JAK_DEPARTMENTS = $jakdb->select($jaktable2, ["id", "title"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
  
    // Get all responses
  $PRIORITY_ALL = $jakdb->select($jaktable, ["id", "title", "class", "dorder"], ["opid" => $opcacheid, "ORDER" => ["dorder" => "ASC"]]);
  
    // Title and Description
  $SECTION_TITLE = $jkl["hd149"];
  $SECTION_DESC = "";
  
    // Include the javascript file for results
  $js_file_footer = 'js_sortable.php';
  
    // Call the template
  $template = 'ticketpriority.php';
}
?>
