<commit_before><?php

namespace Reusables;

class Convert
{
    public static function keys($data, $identifier=null)
    {
        $testing=false;
        if (!$identifier) {
            $identifier = Data::getDefaultDataID($data);
        } else {
            $testing=true;
        }
        if ($testing) {
            exit(json_encode($data));
        }
        // exit( json_encode( $identifier ) );
        // $data = Data::get( $identifier );
        $options = Options::get($identifier);
        // exit( json_encode( $data ) );
        $convertkeys = Data::getValue($options, 'convert_keys');
        if ($convertkeys == "") {
            return $data;
        }
        if (!isset($convertkeys)) {
            $convertkeys = false;
        } else {
            $convertkeys = $convertkeys;
        }
        $convertdict = $data;
        if (isset($data['value'])) {
            $convertdict = $data['value'];
        }

        $sectionkeys = array_keys($convertdict);
        foreach ($sectionkeys as $k) {
            $k_no_table = $k;
            $k_arr = explode(".", $k);
            if (sizeof($k_arr) == 2) {
                $k_no_table = $k_arr[1];
            }
            // exit( json_encode( $convertkeys ) );
            // if( isset( $convertkeys[$k] ) ){ $convertdict[$convertkeys[$k]] = $convertdict[$k]; }
            if (isset($convertkeys[$k_no_table])) {
                if (is_array($convertkeys[$k_no_table])) {
                    foreach ($convertkeys[$k_no_table] as $ck) {
                        $convertdict[$ck] = $convertdict[$k];
                    }
                } else {
                    $convertdict[$convertkeys[$k_no_table]] = $convertdict[$k];
                }
            }
        }

        if (isset($data['value'])) {
            $data['value'] = $convertdict;
        } else {
            $data = $convertdict;
        }

        return $data;
    }

    public static function keysInTable($identifier, $post)
    {
        $data = Data::get($identifier);
        $options = Options::get($identifier);

        if (!isset($options['convert_keys'])) {
            $convertkeys = false;
        } else {
            $convertkeys = $options['convert_keys'];
        }

        $postkeys = array_keys($post);

        foreach ($postkeys as $k) {
            $k_no_table = $k;
            $k_arr = explode(".", $k);
            if (sizeof($k_arr) == 2) {
                $k_no_table = $k_arr[1];
            }
            if (isset($convertkeys[$k_no_table])) {
                if (is_array($convertkeys[$k_no_table])) {
                    foreach ($convertkeys[$k_no_table] as $ck) {
                        $post[$ck] = $post[$k];
                    }
                } else {
                    // exit( json_encode($post[$k]));
                    $post[$convertkeys[$k_no_table]] = $post[$k];
                }
                // $post[$convertkeys[$k] ]['key'] = $convertkeys[$k];
            }
        }

        return $post;
    }

    public static function convertDataForArray($identifier, $index)
    {
        $dict = self::retrieveDataWithID($identifier)['value'][$index];

        $allkeys = array_keys($dict);
        $returningdict = [];
        foreach ($allkeys as $k) {
            $returningdict[$k] = [ "data_id"=>$identifier, "key"=>$k, "index"=>$index ];
        }
        $returningdict['index'] = $index;

        return $returningdict;
    }

    public static function viewParamsToVars($identifier)
    {
        // Get view data, options, and tablename
        $viewdict = Data::get($identifier);<commit_msg>made it easier to add data to gallery view<commit_after><?php

namespace Reusables;

class Convert
{
    public static function keys($data, $identifier=null)
    {
        $testing=false;
        if (!$identifier) {
            $identifier = Data::getDefaultDataID($data);
        } else {
            $testing=true;
        }
        if ($testing) {
            exit(json_encode($data));
        }
        // exit( json_encode( $identifier ) );
        // $data = Data::get( $identifier );
        $options = Options::get($identifier);
        // exit( json_encode( $data ) );
        $convertkeys = Data::getValue($options, 'convert_keys');
        if ($convertkeys == "") {
            return $data;
        }
        if (!isset($convertkeys)) {
            $convertkeys = false;
        } else {
            $convertkeys = $convertkeys;
        }
        $convertdict = $data;
        if (isset($data['value'])) {
            $convertdict = $data['value'];
        }

        $sectionkeys = array_keys($convertdict);
        foreach ($sectionkeys as $k) {
            $k_no_table = $k;
            $k_arr = explode(".", $k);
            if (sizeof($k_arr) == 2) {
                $k_no_table = $k_arr[1];
            }
            // exit( json_encode( $convertkeys ) );
            // if( isset( $convertkeys[$k] ) ){ $convertdict[$convertkeys[$k]] = $convertdict[$k]; }
            if (isset($convertkeys[$k_no_table])) {
                if (is_array($convertkeys[$k_no_table])) {
                    foreach ($convertkeys[$k_no_table] as $ck) {
                        $convertdict[$ck] = $convertdict[$k];
                    }
                } else {
                    $convertdict[$convertkeys[$k_no_table]] = $convertdict[$k];
                }
            }
        }

        if (isset($data['value'])) {
            $data['value'] = $convertdict;
        } else {
            $data = $convertdict;
        }

        return $data;
    }

    public static function keysInTable($identifier, $post)
    {
        $data = Data::get($identifier);
        $options = Options::get($identifier);

        if (!isset($options['convert_keys'])) {
            $convertkeys = false;
        } else {
            $convertkeys = $options['convert_keys'];
        }
        $post = Convert::toViewTypeArray( $identifier, $post );
        $postkeys = array_keys($post);

        foreach ($postkeys as $k) {
            $k_no_table = $k;
            $k_arr = explode(".", $k);
            if (sizeof($k_arr) == 2) {
                $k_no_table = $k_arr[1];
            }
            if (isset($convertkeys[$k_no_table])) {
                if (is_array($convertkeys[$k_no_table])) {
                    foreach ($convertkeys[$k_no_table] as $ck) {
                        $post[$ck] = $post[$k];
                    }
                } else {
                    // exit( json_encode($post[$k]));
                    $post[$convertkeys[$k_no_table]] = $post[$k];
                }
                // $post[$convertkeys[$k] ]['key'] = $convertkeys[$k];
            }
        }

        return $post;
    }

    public static function toViewTypeArray( $identifier, $array )
    {
        $viewtype = strtolower(basename(Info::viewtype($identifier)));

        switch ($viewtype) {
          case 'gallery':
            return Convert::toGalleryArray( $identifier, $array );
            break;

          default:
            return $array;
            break;
        }
       return $array;
    }

    public static function toGalleryArray( $identifier, $array )
    {
      $new_array = [];
      if( is_string($array) ) {
        $new_array = ["imagepath"=>$array];
        return $new_array;
      }
      foreach ($array as $key => $value) {
        array_push($new_array, ["imagepath"=>$value]);
      }

      return $new_array;
    }

    public static function convertDataForArray($identifier, $index)
    {
        $dict = self::retrieveDataWithID($identifier)['value'][$index];

        $allkeys = array_keys($dict);
        $returningdict = [];
        foreach ($allkeys as $k) {
            $returningdict[$k] = [ "data_id"=>$identifier, "key"=>$k, "index"=>$index ];
        }
        $returningdict['index'] = $index;

        return $returningdict;
    }

    public static function viewParamsToVars($identifier)
    {
        // Get view data, options, and tablename
        $viewdict = Data::get($identifier);
{'fraction_non_alphanumeric': 0.15382697581650462, 'fraction_numerical': 0.0009972575417601596, 'mean_word_length': 1.6636786188579018, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5352574', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2281, 'n_words': 422}