<commit_before><?php
// Copyright (c) 2013 The Fajr authors (see AUTHORS).
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file in the project root directory.

namespace libfajr\data;

use libfajr\data\AIS2TableParser;
use libfajr\data\ComponentInterface;
use libfajr\trace\Trace;

/**
 * Trieda zastrešujúca tabuľku dát.
 *
 * @package    Libfajr
 */
class DataTable implements ComponentInterface
{
  /**
   * Name of the table in aisHTMLCode
   * @var string
   */
  private $dataViewName = null;

  /**
   * Definícia stĺpcov tabuľky
   * @var array(string)
   */
  private $definition = null;

  /**
   * Definition which the table must have
   * @var array(string)
   */
  private $controlDefinition = null;

  /**
   * Samotné dáta poparsované pri konštrukcii objektu.
   * @var array(array(string=>string))
   */
  private $data = null;

  /**
   * Selected rows
   * @var array(integer)
   */
  private $selectedRows = null;

  /**
   * Create a Table and set its dataViewName and definition
   *
   * @param string $dataViewName name of Table which we want to store here
   * @param Window $parent window where this component is used
   * @param array(string) $definition name of columns which table must have
   *                                  if not defined, no control on that will
   *                                  be done during updating the table and 
   *                                  definition will load from aisHTMLCode.
   */
  public function __construct($dataViewName, $definition = null)
  {
    $this->dataViewName = $dataViewName;
    if($definition != null){
      $this->controlDefinition = $definition;
    }else{
      $this->controlDefinition = array();
    }
    $this->selectedRows = array();
  }

  /**
   * Update Table from aisResponse
   *
   * @param DOMDocument $aisResponse AIS2 html parsed reply
   */
  public function updateComponentFromResponse(DOMDocument $aisResponse)
  {
    $element = $aisResponse->getElementById($dataViewName);
    $dom = new DOMDocument();
    $dom->appendChild($dom->importNode($element, true));
    // ok, now we have restricted document

    //informacia ci sa jedna o update, append...
    $element2 = $dom->getElementById("dataTabBodies");
    if ($element === null || $element2 === null) {
      throw new ParseException("Problem parsing ais2 response: Element not found");
    }
    

    //ak sa jedna len o scroll tak tam definicia tabulky nie je
    if($element2->getAttribute("dataSendType") == "update"){
      $this->definition = $this->getDefinitionFromDom($dom);
      $control = array_diff($this->controlDefinition,$this->definition);
      if($control != null){
        throw new ParseException("Table definition isn`t a subset of controlDefinition");
      }
    }

    $TData = $this->getTableDataFromDom($dom);
    
    // modifying data to better format
    assert(is_array($TData));
    $this->data = array();
    foreach ($TData as $rowKey=>$tableRow) {
      $myRow = array();
      $myRow['index'] = $rowKey;
      assert(count($this->definition) == count($tableRow));

      foreach($tableRow as $key=>$value) {
        assert(is_numeric($key));
        assert(isset($this->definition[$key]));
        $myRow[$this->definition[$key]] = $value;
      }

      $this->data[$rowKey] = $myRow;

    }
  
  }

  /**
   * Return data in the Table
   *
   * @returns array(array(string=>string)) all rows of the Table
   */
  public function getData()
  {
    return $this->data;
  }

  /**
   * Return Table definitions.
   *
   * @returns array(string) columns names.
   */
  public function getTableDefinition()
  {
    return $this->definition;
  }

  /**
   * Return one record from table
   *
   * @param string $index rowId of row, which we want to get
   * @returns array(string=>string) data in row $data[$index].
   */
  public function getRow($index)
  {
    return $this->data[$index];
  }

  /**
   * Returns changes on this table (selected rows)
   *
   * @return DOMDocument
   */
  public function getStateChanges()
  {
    $this->selectedRows = array_unique($this->selectedRows);   
  }

  /**
   * Add one row to selection
   *
   * @param string $index rowId of row, which we want to select
   */
  public function selectRow($index)
  {
    $this->selectedRows = $index;
  }

 /**
   * Select one record of table
   *
   * @param string $index rowId of row, which we want to select
   */
  public function selectSingleRow($index)
  {
    $this->clearSelection();
    $this->selectRow($index);
  }

  /**
   * Unselect actually selected row
   *
   */
  public function clearSelection()
  {
    $this->selectedRows = array();
  }

  /**
   * Get table definitions from DOMDocument
   *
   * @param $dom DOMDocument from ais2ResponseHtml
   * @returns array(string) Definition of table
   */
  private function getDefinitionFromDom(DOMDocument $dom)
  {
    $element = $dom->getElementById('dataTabColGroup');
    if ($element == null) {
      throw new ParseException("Can't find table headers");
    }
    $list = $element->getElementsByTagName('col');
    $columns = array();
    foreach ($list as $node) {
      assert($node->hasAttribute('shortname'));
      $columns[] = $node->getAttribute('shortname');
    }
    return $columns;
  }

  /**<commit_msg>Oprava chyb, odstranenie controlDefinition<commit_after><?php
// Copyright (c) 2013 The Fajr authors (see AUTHORS).
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file in the project root directory.

namespace libfajr\data;

use libfajr\data\AIS2TableParser;
use libfajr\data\ComponentInterface;
use libfajr\trace\Trace;

/**
 * Trieda zastrešujúca tabuľku dát.
 *
 * @package    Libfajr
 */
class DataTable implements ComponentInterface
{
  /**
   * Name of the table in aisHTMLCode
   * @var string
   */
  private $dataViewName = null;

  /**
   * Definícia stĺpcov tabuľky
   * @var array(string)
   */
  private $definition = null;

  /**
   * Samotné dáta poparsované pri konštrukcii objektu.
   * @var array(array(string=>string))
   */
  private $data = null;

  /**
   * Selected rows
   * @var array(integer)
   */
  private $selectedRows = null;

  /**
   * Create a Table and set its dataViewName and definition
   *
   * @param string $dataViewName name of Table which we want to store here
   */
  public function __construct($dataViewName)
  {
    $this->dataViewName = $dataViewName;
    $this->selectedRows = array();
  }

  /**
   * Update Table from aisResponse
   *
   * @param DOMDocument $aisResponse AIS2 html parsed reply
   */
  public function updateComponentFromResponse(DOMDocument $aisResponse)
  {
    $element = $aisResponse->getElementById($dataViewName);
    $dom = new DOMDocument();
    $dom->appendChild($dom->importNode($element, true));
    // ok, now we have restricted document

    //informacia ci sa jedna o update, append...
    $element2 = $dom->getElementById("dataTabBodies");
    if ($element === null || $element2 === null) {
      throw new ParseException("Problem parsing ais2 response: Element not found");
    }
    

    //ak sa jedna len o scroll tak tam definicia tabulky nie je
    if($element2->getAttribute("dataSendType") == "update"){
      $this->definition = $this->getDefinitionFromDom($dom);
    }

    $tdata = $this->getTableDataFromDom($dom);
    
    // use column name as array key instead of column index
    assert(is_array($tdata));
    $this->data = array();
    foreach ($tdata as $rowKey=>$tableRow) {
      $myRow = array();
      $myRow['index'] = $rowKey;
      assert(count($this->definition) == count($tableRow));

      foreach($tableRow as $key=>$value) {
        assert(is_numeric($key));
        assert(isset($this->definition[$key]));
        $myRow[$this->definition[$key]] = $value;
      }

      $this->data[$rowKey] = $myRow;

    }
  
  }

  /**
   * Return data in the Table
   *
   * @returns array(array(string=>string)) all rows of the Table
   */
  public function getData()
  {
    return $this->data;
  }

  /**
   * Return Table definitions.
   *
   * @returns array(string) columns names.
   */
  public function getTableDefinition()
  {
    return $this->definition;
  }

  /**
   * Return one record from table
   *
   * @param integer $index rowId of row, which we want to get
   * @returns array(string=>string) data in row $data[$index].
   */
  public function getRow($index)
  {
    if ($index < 0 || $index >= count($this->data)) return null;
    return $this->data[$index];
  }

  /**
   * Returns changes on this table (selected rows)
   *
   * @return string XML in string
   */
  public function getStateChanges()
  {
    $this->selectedRows = array_unique($this->selectedRows);   
  }

  /**
   * Add one row to selection
   *
   * @param integer $index rowId of row, which we want to select
   */
  public function selectRow($index)
  {
    $this->selectedRows[] = $index;
  }

 /**
   * Select one record of table
   *
   * @param integer $index rowId of row, which we want to select
   */
  public function selectSingleRow($index)
  {
    $this->clearSelection();
    $this->selectRow($index);
  }

  /**
   * Unselect every selected row.
   *
   */
  public function clearSelection()
  {
    $this->selectedRows = array();
  }

  /**
   * Get table definitions from DOMDocument
   *
   * @param $dom DOMDocument from ais2ResponseHtml
   * @returns array(string) Definition of table
   */
  private function getDefinitionFromDom(DOMDocument $dom)
  {
    $element = $dom->getElementById('dataTabColGroup');
    if ($element == null) {
      throw new ParseException("Can't find table headers");
    }
    $list = $element->getElementsByTagName('col');
    $columns = array();
    foreach ($list as $node) {
      assert($node->hasAttribute('shortname'));
      $columns[] = $node->getAttribute('shortname');
    }
    return $columns;
  }

  /**
{'fraction_non_alphanumeric': 0.13027707808564232, 'fraction_numerical': 0.0023173803526448365, 'mean_word_length': 2.8532608695652173, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2752648', 'n_tokens_mistral': 3062, 'n_tokens_neox': 3000, 'n_words': 842}