<commit_before><?php

/**
 * Advanced OpenReports, SugarCRM Reporting.
 * @package Advanced OpenReports for SugarCRM
 * @copyright SalesAgility Ltd http://www.salesagility.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 AFFERO GENERAL PUBLIC LICENSE
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 *
 * @author SalesAgility <<EMAIL>>
 */


require_once("modules/AOW_WorkFlow/aow_utils.php");
require_once("modules/AOR_Reports/aor_utils.php");

class AOR_ReportsController extends SugarController
{

    protected function action_getModuleFields()
    {
        if (!empty($_REQUEST['aor_module']) && $_REQUEST['aor_module'] != '') {
            if (isset($_REQUEST['rel_field']) && $_REQUEST['rel_field'] != '') {
                $module = getRelatedModule($_REQUEST['aor_module'], $_REQUEST['rel_field']);
            } else {
                $module = $_REQUEST['aor_module'];
            }
            $val = !empty($_REQUEST['aor_value']) ? $_REQUEST['aor_value'] : '';
            echo getModuleFields($module, $_REQUEST['view'], $val);
        }
        die;

    }

    public function action_getVarDefs()
    {
        if ($_REQUEST['aor_module']) {
            $bean = BeanFactory::getBean($_REQUEST['aor_module']);
            echo json_encode((array)$bean->field_defs[$_REQUEST['aor_request']]);
            die();
        }
    }

    protected function action_getModuleTreeData()
    {
        if (!empty($_REQUEST['aor_module']) && $_REQUEST['aor_module'] != '') {
            ob_start();
            $data = getModuleTreeData($_REQUEST['aor_module']);
            ob_clean();
            echo $data;
        }
        die;
    }

    protected function action_getModuleRelationships()
    {
        if (!empty($_REQUEST['aor_module']) && $_REQUEST['aor_module'] != '') {
            echo getModuleRelationships($_REQUEST['aor_module']);
        }
        die;
    }

    protected function action_changeReportPage()
    {
        $offset = !empty($_REQUEST['offset']) ? $_REQUEST['offset'] : 0;
        if (!empty($this->bean->id)) {
            $this->bean->user_parameters = requestToUserParameters();
            echo $this->bean->build_group_report($offset, true);
        }

        die();
    }

    protected function action_getParametersForReport()
    {
        if (empty($_REQUEST['record'])) {
            echo json_encode(array());

            return;
        }
        $report = BeanFactory::getBean('AOR_Reports', $_REQUEST['record']);
        if (!$report) {
            echo json_encode(array());

            return;
        }
        if (empty($report->id)) {
            echo json_encode(array());

            return;
        }
        $conditions = getConditionsAsParameters($report);
        echo json_encode($conditions);
    }

    protected function action_getChartsForReport()
    {
        if (empty($_REQUEST['record'])) {
            echo json_encode(array());

            return;
        }
        $report = BeanFactory::getBean('AOR_Reports', $_REQUEST['record']);
        if (!$report) {
            echo json_encode(array());

            return;
        }
        $charts = array();
        foreach ($report->get_linked_beans('aor_charts', 'AOR_Charts') as $chart) {
            $charts[$chart->id] = $chart->name;
        }
        echo json_encode($charts);
    }

    protected function action_addToProspectList()
    {
        global $beanList;

        require_once('modules/Relationships/Relationship.php');
        require_once('modules/ProspectLists/ProspectList.php');

        $prospectList = new ProspectList();
        $prospectList->retrieve($_REQUEST['prospect_id']);

        $module = new $beanList[$this->bean->report_module]();

        $key = Relationship::retrieve_by_modules($this->bean->report_module, 'ProspectLists', $GLOBALS['db']);
        if (!empty($key)) {

            $sql = $this->bean->build_report_query();
            $result = $this->bean->db->query($sql);
            $beans = array();
            while ($row = $this->bean->db->fetchByAssoc($result)) {
                if (isset($row[$module->table_name . '_id'])) {
                    $beans[] = $row[$module->table_name . '_id'];
                }
            }
            if (!empty($beans)) {
                foreach ($prospectList->field_defs as $field => $def) {
                    if ($def['type'] == 'link' && !empty($def['relationship']) && $def['relationship'] == $key) {
                        $prospectList->load_relationship($field);
                        $prospectList->$field->add($beans);
                    }
                }
            }
        }
        die;
    }

    protected function action_chartReport()
    {
        $this->bean->build_report_chart(null, AOR_Report::CHART_TYPE_CHARTJS);

        die;
    }

    protected function action_export()
    {
        $this->bean->user_parameters = requestToUserParameters();
        $this->bean->build_report_csv();
        die;
    }

    protected function action_downloadPDF()
    {
        error_reporting(0);
        require_once('modules/AOS_PDF_Templates/PDF_Lib/mpdf.php');

        $d_image = explode('?', SugarThemeRegistry::current()->getImageURL('company_logo.png'));
        $graphs = $_POST["graphsForPDF"];
        $graphHtml = "<div class='reportGraphs' style='width:100%; text-align:center;'>";

        $chartsPerRow = $this->bean->graphs_per_row;
        $countOfCharts = count($graphs);
        if ($countOfCharts > 0) {
            $width = ((int)100 / $chartsPerRow);

            $modulusRemainder = $countOfCharts % $chartsPerRow;
<commit_msg>Disable php timeout while preparing report export<commit_after><?php

/**
 * Advanced OpenReports, SugarCRM Reporting.
 * @package Advanced OpenReports for SugarCRM
 * @copyright SalesAgility Ltd http://www.salesagility.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 AFFERO GENERAL PUBLIC LICENSE
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 *
 * @author SalesAgility <<EMAIL>>
 */


require_once("modules/AOW_WorkFlow/aow_utils.php");
require_once("modules/AOR_Reports/aor_utils.php");

class AOR_ReportsController extends SugarController
{

    protected function action_getModuleFields()
    {
        if (!empty($_REQUEST['aor_module']) && $_REQUEST['aor_module'] != '') {
            if (isset($_REQUEST['rel_field']) && $_REQUEST['rel_field'] != '') {
                $module = getRelatedModule($_REQUEST['aor_module'], $_REQUEST['rel_field']);
            } else {
                $module = $_REQUEST['aor_module'];
            }
            $val = !empty($_REQUEST['aor_value']) ? $_REQUEST['aor_value'] : '';
            echo getModuleFields($module, $_REQUEST['view'], $val);
        }
        die;

    }

    public function action_getVarDefs()
    {
        if ($_REQUEST['aor_module']) {
            $bean = BeanFactory::getBean($_REQUEST['aor_module']);
            echo json_encode((array)$bean->field_defs[$_REQUEST['aor_request']]);
            die();
        }
    }

    protected function action_getModuleTreeData()
    {
        if (!empty($_REQUEST['aor_module']) && $_REQUEST['aor_module'] != '') {
            ob_start();
            $data = getModuleTreeData($_REQUEST['aor_module']);
            ob_clean();
            echo $data;
        }
        die;
    }

    protected function action_getModuleRelationships()
    {
        if (!empty($_REQUEST['aor_module']) && $_REQUEST['aor_module'] != '') {
            echo getModuleRelationships($_REQUEST['aor_module']);
        }
        die;
    }

    protected function action_changeReportPage()
    {
        $offset = !empty($_REQUEST['offset']) ? $_REQUEST['offset'] : 0;
        if (!empty($this->bean->id)) {
            $this->bean->user_parameters = requestToUserParameters();
            echo $this->bean->build_group_report($offset, true);
        }

        die();
    }

    protected function action_getParametersForReport()
    {
        if (empty($_REQUEST['record'])) {
            echo json_encode(array());

            return;
        }
        $report = BeanFactory::getBean('AOR_Reports', $_REQUEST['record']);
        if (!$report) {
            echo json_encode(array());

            return;
        }
        if (empty($report->id)) {
            echo json_encode(array());

            return;
        }
        $conditions = getConditionsAsParameters($report);
        echo json_encode($conditions);
    }

    protected function action_getChartsForReport()
    {
        if (empty($_REQUEST['record'])) {
            echo json_encode(array());

            return;
        }
        $report = BeanFactory::getBean('AOR_Reports', $_REQUEST['record']);
        if (!$report) {
            echo json_encode(array());

            return;
        }
        $charts = array();
        foreach ($report->get_linked_beans('aor_charts', 'AOR_Charts') as $chart) {
            $charts[$chart->id] = $chart->name;
        }
        echo json_encode($charts);
    }

    protected function action_addToProspectList()
    {
        global $beanList;

        require_once('modules/Relationships/Relationship.php');
        require_once('modules/ProspectLists/ProspectList.php');

        $prospectList = new ProspectList();
        $prospectList->retrieve($_REQUEST['prospect_id']);

        $module = new $beanList[$this->bean->report_module]();

        $key = Relationship::retrieve_by_modules($this->bean->report_module, 'ProspectLists', $GLOBALS['db']);
        if (!empty($key)) {

            $sql = $this->bean->build_report_query();
            $result = $this->bean->db->query($sql);
            $beans = array();
            while ($row = $this->bean->db->fetchByAssoc($result)) {
                if (isset($row[$module->table_name . '_id'])) {
                    $beans[] = $row[$module->table_name . '_id'];
                }
            }
            if (!empty($beans)) {
                foreach ($prospectList->field_defs as $field => $def) {
                    if ($def['type'] == 'link' && !empty($def['relationship']) && $def['relationship'] == $key) {
                        $prospectList->load_relationship($field);
                        $prospectList->$field->add($beans);
                    }
                }
            }
        }
        die;
    }

    protected function action_chartReport()
    {
        $this->bean->build_report_chart(null, AOR_Report::CHART_TYPE_CHARTJS);

        die;
    }

    protected function action_export()
    {
        set_time_limit(0);
        $this->bean->user_parameters = requestToUserParameters();
        $this->bean->build_report_csv();
        die;
    }

    protected function action_downloadPDF()
    {
        error_reporting(0);
        require_once('modules/AOS_PDF_Templates/PDF_Lib/mpdf.php');

        $d_image = explode('?', SugarThemeRegistry::current()->getImageURL('company_logo.png'));
        $graphs = $_POST["graphsForPDF"];
        $graphHtml = "<div class='reportGraphs' style='width:100%; text-align:center;'>";

        $chartsPerRow = $this->bean->graphs_per_row;
        $countOfCharts = count($graphs);
        if ($countOfCharts > 0) {
            $width = ((int)100 / $chartsPerRow);

            $modulusRemainder = $countOfCharts % $chartsPerRow;

{'fraction_non_alphanumeric': 0.1420595038685491, 'fraction_numerical': 0.0034298476509531787, 'mean_word_length': 2.421943231441048, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4421085', 'n_tokens_mistral': 3630, 'n_tokens_neox': 3452, 'n_words': 715}