<commit_before><?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reports extends Easol_Controller {

    /**
     * default constructor
     */
    public function __construct(){
        parent::__construct();
    }

    protected function accessRules(){
        return [
           // "index"     =>  ['@'],
           "index"     =>  ['System Administrator','Data Administrator'],
        ];
    }

    /**
     * index action
     */
    public function index()
	{

        $this->load->model('entities/easol/Easol_Report');
        $this->load->model('entities/easol/Easol_DashboardConfiguration');
        $report = new Easol_Report();
        if($this->input->post('dashboardConf')){
            //print_r($this->input->post('dashboardConf'));
            foreach($this->input->post('dashboardConf') as $roleId => $conf){
                $dashConf= (new Easol_DashboardConfiguration())->findOne(['RoleTypeId'=>$roleId,'EducationOrganizationId' => Easol_Authentication::userdata('SchoolId')]);
                if($dashConf==null){
                    $dashConf = new Easol_DashboardConfiguration();
                    $dashConf->RoleTypeId = $roleId;
                    $dashConf->EducationOrganizationId = Easol_Authentication::userdata('SchoolId');


                } else{
                    $dashConf = (new Easol_DashboardConfiguration())->hydrate($dashConf);

                }
                $dashConf->LeftChartReportId = $conf['left'];
                $dashConf->RightChartReportId = $conf['right'];
                $dashConf->BottomTableReportId = $conf['bottom'];
                $dashConf->save();
            }
            $this->session->set_flashdata('message_dash_conf', 'Dashboard Configuration Saved');

            return redirect(site_url("reports/index#dashConf"));

        }

		$this->render("index",['reports' => $report->hydrate($report->findAll()->result())]);
	}

    /**
     * index action
     */
    public function create()
    {
        $this->load->model('entities/easol/Easol_Report');

        $model= new Easol_Report();
        //die(print_r($this->input->post('access[access]')));
        if($this->input->post('report') && $model->populateForm($this->input->post('report'))){

                if ($this->form_validation->run() != FALSE) {
                    if($model->save()){

                        $this->load->model('entities/easol/Easol_ReportAccess');

                        foreach($this->input->post('access[access]') as $access){
                            $displayAccess = new Easol_ReportAccess();
                            $displayAccess->ReportId = $model->ReportId;
                            $displayAccess->RoleTypeId = $access;
                            $displayAccess->save();
                        }
                        $this->session->set_flashdata('message', 'New Report Added : '. $model->ReportName);
                        $this->session->set_flashdata('type', 'success');

                        $this->easol_logs->Log( [
                            'Description'=>'Flex Report (create)',
                            'Data'=>["ModelId"=>$model->ReportId]
                        ]);
                        

                        return redirect(site_url("reports/index"));

                    }
                }

        }

       // $report->ReportName = "Report";
       /* echo $report->ReportName."sdd";
        echo $report->ReportName."sdd"; */
        //$report->save();
        $this->render("create",['model' => $model]);
    }


    /**
     * @param null | int $id
     * @throws Exception
     */
    public function edit($id=null){
        if($id==null)
            throw new \Exception("Invalid report Id");

        $this->load->model('entities/easol/Easol_Report');

        $model= new Easol_Report();
        $model= $model->hydrate($model->findOne($id));

        // die(print_r($model));
        //die(print_r($this->input->post('access[access]')));
        if($this->input->post('report') && $model->populateForm($this->input->post('report'))) {

            if ($this->form_validation->run() != FALSE) {
                $arrOldValue = $this->stdToArray($model->findOne($id));
                if($model->save()){

                    $this->load->model('entities/easol/Easol_ReportAccess');
/*
                    foreach($this->input->post('access[access]') as $access){
                        $displayAccess = new Easol_ReportAccess();
                        $displayAccess->ReportId = $model->ReportId;
                        $displayAccess->RoleTypeId = $access;
                        $displayAccess->save();
                    }
*/

                    $aRoles=[];
                    foreach($model->getAccessTypes() as $role){
                        if(!in_array($role->RoleTypeId,$this->input->post('access[access]'))){
                            $this->db->delete('EASOL.ReportAccess', array('ReportId' => $model->ReportId,'RoleTypeId'=>$role->RoleTypeId));
                        }
                        $aRoles[] = $role->RoleTypeId;
                    }

                    foreach($this->input->post('access[access]') as $access){
                        if(in_array($access,$aRoles))
                            continue;
                        $displayAccess = new Easol_ReportAccess();
                        $displayAccess->ReportId = $model->ReportId;
                        $displayAccess->RoleTypeId = $access;
                        $displayAccess->save();
                    }


                    $this->session->set_flashdata('message', 'Report Updated Successfully : '. $model->ReportName);
                    $this->session->set_flashdata('type', 'success');

                    unset($arrOldValue['ReportId'], $arrOldValue['CreatedBy'], $arrOldValue['CreatedOn'], $arrOldValue['UpdatedBy'], $arrOldValue['UpdatedOn'], $arrOldValue['SchoolId']);
                    $this->easol_logs->Log( [
                        'Description'=>'Flex Report (Update)',
                        'Data'=>["ModelId"=>$model->ReportId, 'OldValue'=>json_encode($this->stdToArray($arrOldValue)),'NewValue'=>json_encode($this->input->post('report'))]
                    ]);

                    return  redirect(site_url("reports/edit/".$model->ReportId));

                }
            }
        }
        // $report->ReportName = "Report";
        /* echo $report->ReportName."sdd";
         echo $report->ReportName."sdd"; */
        //$report->save();
        $this->render("edit",['model' => $model]);
<commit_msg>Fixed create/edit process to skip access insertion/edition if there's no selected access<commit_after><?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reports extends Easol_Controller {

    /**
     * default constructor
     */
    public function __construct(){
        parent::__construct();
    }

    protected function accessRules(){
        return [
           // "index"     =>  ['@'],
           "index"     =>  ['System Administrator','Data Administrator'],
        ];
    }

    /**
     * index action
     */
    public function index()
	{

        $this->load->model('entities/easol/Easol_Report');
        $this->load->model('entities/easol/Easol_DashboardConfiguration');
        $report = new Easol_Report();
        if($this->input->post('dashboardConf')){
            //print_r($this->input->post('dashboardConf'));
            foreach($this->input->post('dashboardConf') as $roleId => $conf){
                $dashConf= (new Easol_DashboardConfiguration())->findOne(['RoleTypeId'=>$roleId,'EducationOrganizationId' => Easol_Authentication::userdata('SchoolId')]);
                if($dashConf==null){
                    $dashConf = new Easol_DashboardConfiguration();
                    $dashConf->RoleTypeId = $roleId;
                    $dashConf->EducationOrganizationId = Easol_Authentication::userdata('SchoolId');


                } else{
                    $dashConf = (new Easol_DashboardConfiguration())->hydrate($dashConf);

                }
                $dashConf->LeftChartReportId = $conf['left'];
                $dashConf->RightChartReportId = $conf['right'];
                $dashConf->BottomTableReportId = $conf['bottom'];
                $dashConf->save();
            }
            $this->session->set_flashdata('message_dash_conf', 'Dashboard Configuration Saved');

            return redirect(site_url("reports/index#dashConf"));

        }

		$this->render("index",['reports' => $report->hydrate($report->findAll()->result())]);
	}

    /**
     * index action
     */
    public function create()
    {
        $this->load->model('entities/easol/Easol_Report');

        $model= new Easol_Report();
        //die(print_r($this->input->post('access[access]')));
        if($this->input->post('report') && $model->populateForm($this->input->post('report'))){

                if ($this->form_validation->run() != FALSE) {
                    if($model->save()){

                        $this->load->model('entities/easol/Easol_ReportAccess');

                        if (is_array($this->input->post('access[access]'))) {
                            foreach ($this->input->post('access[access]') as $access) {
                                $displayAccess             = new Easol_ReportAccess();
                                $displayAccess->ReportId   = $model->ReportId;
                                $displayAccess->RoleTypeId = $access;
                                $displayAccess->save();
                            }
                        }
                        $this->session->set_flashdata('message', 'New Report Added : '. $model->ReportName);
                        $this->session->set_flashdata('type', 'success');

                        $this->easol_logs->Log( [
                            'Description'=>'Flex Report (create)',
                            'Data'=>["ModelId"=>$model->ReportId]
                        ]);
                        

                        return redirect(site_url("reports/index"));

                    }
                }

        }

       // $report->ReportName = "Report";
       /* echo $report->ReportName."sdd";
        echo $report->ReportName."sdd"; */
        //$report->save();
        $this->render("create",['model' => $model]);
    }


    /**
     * @param null | int $id
     * @throws Exception
     */
    public function edit($id=null){
        if($id==null)
            throw new \Exception("Invalid report Id");

        $this->load->model('entities/easol/Easol_Report');

        $model= new Easol_Report();
        $model= $model->hydrate($model->findOne($id));

        // die(print_r($model));
        //die(print_r($this->input->post('access[access]')));
        if($this->input->post('report') && $model->populateForm($this->input->post('report'))) {

            if ($this->form_validation->run() != FALSE) {
                $arrOldValue = $this->stdToArray($model->findOne($id));
                if($model->save()){

                    $this->load->model('entities/easol/Easol_ReportAccess');
/*
                    foreach($this->input->post('access[access]') as $access){
                        $displayAccess = new Easol_ReportAccess();
                        $displayAccess->ReportId = $model->ReportId;
                        $displayAccess->RoleTypeId = $access;
                        $displayAccess->save();
                    }
*/

                    $aRoles=[];
                    foreach($model->getAccessTypes() as $role){
                        if(!in_array($role->RoleTypeId,$this->input->post('access[access]'))){
                            $this->db->delete('EASOL.ReportAccess', array('ReportId' => $model->ReportId,'RoleTypeId'=>$role->RoleTypeId));
                        }
                        $aRoles[] = $role->RoleTypeId;
                    }

                    if (is_array($this->input->post('access[access]'))) {
                        foreach ($this->input->post('access[access]') as $access) {
                            if (in_array($access, $aRoles))
                                continue;
                            $displayAccess             = new Easol_ReportAccess();
                            $displayAccess->ReportId   = $model->ReportId;
                            $displayAccess->RoleTypeId = $access;
                            $displayAccess->save();
                        }
                    }


                    $this->session->set_flashdata('message', 'Report Updated Successfully : '. $model->ReportName);
                    $this->session->set_flashdata('type', 'success');

                    unset($arrOldValue['ReportId'], $arrOldValue['CreatedBy'], $arrOldValue['CreatedOn'], $arrOldValue['UpdatedBy'], $arrOldValue['UpdatedOn'], $arrOldValue['SchoolId']);
                    $this->easol_logs->Log( [
                        'Description'=>'Flex Report (Update)',
                        'Data'=>["ModelId"=>$model->ReportId, 'OldValue'=>json_encode($this->stdToArray($arrOldValue)),'NewValue'=>json_encode($this->input->post('report'))]
                    ]);

                    return  redirect(site_url("reports/edit/".$model->ReportId));

                }
            }
        }
        // $report->ReportName = "Report";
        /* echo $report->ReportName."sdd";
         echo $report->ReportName."sdd"; */
        //$report->save();
        $this->render("edit",['model' => $model]);

{'fraction_non_alphanumeric': 0.1646130008116284, 'fraction_numerical': 0.0, 'mean_word_length': 1.729359645590012, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 269, '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': '911028', 'n_tokens_mistral': 3672, 'n_tokens_neox': 3463, 'n_words': 461}