<commit_before><?php
/**
 * Table Definition for folder.
 *
 * PHP version 5
 *
 * Copyright (C) Villanova University 2017.
 * Copyright (C) University of Freiburg 2014.
 * Copyright (C) The National Library of Finland 2015.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category VuBib
 * @package  Code
 * @author   <NAME> <<EMAIL>>
 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 *
 * @link https://
 */
namespace App\Db\Table;

use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Adapter\Adapter;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Sql;

/**
 * Table Definition for folder.
 *
 * @category VuBib
 * @package  Code
 * @author   <NAME> <<EMAIL>>
 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 *
 * @link https://
 */
class Folder extends \Zend\Db\TableGateway\TableGateway
{
    /**
     * Folder constructor.
     *
     * @param Adapter $adapter for db connection
     */
    public function __construct($adapter)
    {
        parent::__construct('folder', $adapter);
    }

    /**
     * Find folders with no parent.
     *
     * @return Paginator $paginatorAdapter folder records as paginator
     */
    public function findParent()
    {
        $select = $this->sql->select()->where(['parent_id' => null]);
        $paginatorAdapter = new DbSelect($select, $this->adapter);

        return new Paginator($paginatorAdapter);
    }

    /**
     * Export folders in a hierarchial way to a csv file.
     *
     * @return empty
     */
    public function exportClassification()
    {
        $fl = new self($this->adapter);
        $callback = function ($select) {
            $select->columns(['*']);
            $select->where('parent_id IS NULL');
        };
        $row = $this->select($callback)->toArray();
        $escaper = new \Zend\Escaper\Escaper('utf-8');
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename=test_export.csv');
        $file = fopen('php://output', 'w') or die('Unable to open file!');
        //add BOM to fix UTF-8 in Excel
        fputs($file, $bom = (chr(0xEF).chr(0xBB).chr(0xBF)));
        foreach ($row as $t):
            $content = $t['id'].' '.$escaper->escapeHtml($t['text_fr']).' ';
            fputcsv($file, array($content));
            $fl->getDepth($t['id'], $file, $content);
        endforeach;
        fflush($file);
        fclose($file);
        exit;
    }

    /**
     * Get the depth of each folder and write it to a file.
     *
     * @param Number $id      id of the folder
     * @param string $file    file to which folder hierarchy is to be written
     * @param string $content content to be written to file
     *
     * @return empty
     */
    public function getDepth($id, $file, $content)
    {
        $fl = new self($this->adapter);
        $escaper = new \Zend\Escaper\Escaper('utf-8');
        $con = $content;
        $current_parent_id = $id;
        $callback = function ($select) use ($current_parent_id) {
            $select->columns(['*']);
            $select->where->equalTo('parent_id', $current_parent_id);
        };
        $rc = $this->select($callback)->toArray();
        if (count($rc) != 0) {
            for ($i = 0; $i < count($rc); ++$i) {
                $con1 = ' '.$escaper->escapeHtml($rc[$i]['text_fr']).' ';
                fputcsv($file, array($con.$con1));
                $current_parent_id = $rc[$i]['id'];
                $fl->getDepth($current_parent_id, $file, $con.$con1);
            }
        }
    }

    /**
     * Get children of a parent folder.
     *
     * @param Number $parent parent id of a folder
     *
     * @return Array $rows folder child records
     */
    public function getChild($parent)
    {
        $callback = function ($select) use ($parent) {
            $select->columns(['*']);
            $select->where->equalTo('parent_id', $parent);
        };
        $rows = $this->select($callback)->toArray();
        return $rows;
    }

    /**
     * Get parent of a folder.
     *
     * @param Number $child id of a folder
     *
     * @return Array $row folder parent record
     */
    public function getParent($child)
    {
        $rowset = $this->select(array('id' => $child));
        $row = $rowset->current();

        return $row;
    }

    /**
     * Get folders with no parent.
     *
     * @return Array $rows folder records with no parent
     */<commit_msg>Added order by for displaying classification levels<commit_after><?php
/**
 * Table Definition for folder.
 *
 * PHP version 5
 *
 * Copyright (C) Villanova University 2017.
 * Copyright (C) University of Freiburg 2014.
 * Copyright (C) The National Library of Finland 2015.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category VuBib
 * @package  Code
 * @author   <NAME> <<EMAIL>>
 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 *
 * @link https://
 */
namespace App\Db\Table;

use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Adapter\Adapter;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Expression;

/**
 * Table Definition for folder.
 *
 * @category VuBib
 * @package  Code
 * @author   <NAME> <<EMAIL>>
 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 *
 * @link https://
 */
class Folder extends \Zend\Db\TableGateway\TableGateway
{
    /**
     * Folder constructor.
     *
     * @param Adapter $adapter for db connection
     */
    public function __construct($adapter)
    {
        parent::__construct('folder', $adapter);
    }

    /**
     * Find folders with no parent.
     *
     * @return Paginator $paginatorAdapter folder records as paginator
     */
    public function findParent()
    {
        $select = $this->sql->select()->where(['parent_id' => null]);
        $paginatorAdapter = new DbSelect($select, $this->adapter);

        return new Paginator($paginatorAdapter);
    }

    /**
     * Export folders in a hierarchial way to a csv file.
     *
     * @return empty
     */
    public function exportClassification()
    {
        $fl = new self($this->adapter);
        $callback = function ($select) {
            $select->columns(['*']);
            $select->where('parent_id IS NULL');
        };
        $row = $this->select($callback)->toArray();
        $escaper = new \Zend\Escaper\Escaper('utf-8');
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename=test_export.csv');
        $file = fopen('php://output', 'w') or die('Unable to open file!');
        //add BOM to fix UTF-8 in Excel
        fputs($file, $bom = (chr(0xEF).chr(0xBB).chr(0xBF)));
        foreach ($row as $t):
            $content = $t['id'].' '.$escaper->escapeHtml($t['text_fr']).' ';
            fputcsv($file, array($content));
            $fl->getDepth($t['id'], $file, $content);
        endforeach;
        fflush($file);
        fclose($file);
        exit;
    }

    /**
     * Get the depth of each folder and write it to a file.
     *
     * @param Number $id      id of the folder
     * @param string $file    file to which folder hierarchy is to be written
     * @param string $content content to be written to file
     *
     * @return empty
     */
    public function getDepth($id, $file, $content)
    {
        $fl = new self($this->adapter);
        $escaper = new \Zend\Escaper\Escaper('utf-8');
        $con = $content;
        $current_parent_id = $id;
        $callback = function ($select) use ($current_parent_id) {
            $select->columns(['*']);
            $select->where->equalTo('parent_id', $current_parent_id);
        };
        $rc = $this->select($callback)->toArray();
        if (count($rc) != 0) {
            for ($i = 0; $i < count($rc); ++$i) {
                $con1 = ' '.$escaper->escapeHtml($rc[$i]['text_fr']).' ';
                fputcsv($file, array($con.$con1));
                $current_parent_id = $rc[$i]['id'];
                $fl->getDepth($current_parent_id, $file, $con.$con1);
            }
        }
    }

    /**
     * Get children of a parent folder.
     *
     * @param Number $parent parent id of a folder
     *
     * @return Array $rows folder child records
     */
    public function getChild($parent)
    {
        $callback = function ($select) use ($parent) {
            $select->columns(['*']);
            $select->where->equalTo('parent_id', $parent);
            //$select->order(new Expression('case when sort_order is null then 1 else 0 end, sort_order'),'text_fr');
            $select->order('sort_order');
            $select->order('text_fr');
        };
        $rows = $this->select($callback)->toArray();
        return $rows;
    }

    /**
     * Get parent of a folder.
     *
     * @param Number $child id of a folder
     *
     * @return Array $row folder parent record
     */
    public function getParent($child)
    {
        $rowset = $this->select(array('id' => $child));
        $row = $rowset->current();

        return $row;
    }

    /**
     * Get folders with no parent.
     *
     * @return Array $rows folder records with no parent
     */
{'fraction_non_alphanumeric': 0.14248779086469404, 'fraction_numerical': 0.007852149765393087, 'mean_word_length': 2.3399424368404222, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 74, 'https://': 4, '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': '1654843', 'n_tokens_mistral': 3168, 'n_tokens_neox': 3063, 'n_words': 916}