<commit_before><?php
App::uses('AppController','Controller');
App::uses('Participant','Model');
App::uses('History', 'Model');
App::uses('ProgramSetting', 'Model');
App::uses('Schedule', 'Model');
App::uses('VumiRabbitMQ', 'Lib');


class ProgramParticipantsController extends AppController
{

    public $uses = array('Participant', 'History');


    function constructClasses() 
    {
        parent::constructClasses();

        $options = array('database' => ($this->Session->read($this->params['program']."_db")));

        $this->Participant    = new Participant($options);
        $this->History        = new History($options);
        $this->ProgramSetting = new ProgramSetting($options);
        $this->Schedule       = new Schedule($options);
        $this->VumiRabbitMQ   = new VumiRabbitMQ();
    }


    function beforeFilter() 
    {
        parent::beforeFilter();
        //$this->Auth->allow('*');
    }

    
    public function index() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $participants = $this->paginate();
        $this->set(compact('participants'));        
    }


    protected function _notifyUpdateBackendWorker($workerName)
    {
        $this->VumiRabbitMQ->sendMessageToUpdateSchedule($workerName);
    }
    

    public function add() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $programUrl = $this->params['program'];
 
        if ($this->request->is('post')) {
            $this->Participant->create();
            if ($this->Participant->save($this->request->data)) {
                $this->_notifyUpdateBackendWorker($programUrl);
                $this->Session->setFlash(__('The participant has been saved.'),
                    'default',
                    array('class'=>'success-message')
                );
                $this->redirect(array(
                    'program' => $programUrl,  
                    'controller' => 'programParticipants',
                    'action' => 'index'
                    ));
            } else {
                $this->Session->setFlash(__('The participant could not be saved.'));
            }
        }        
    }

    
    public function edit() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $programUrl = $this->params['program'];
        $id         = $this->params['id'];
        
        $this->Participant->id = $id;
        if (!$this->Participant->exists()) {
            throw new NotFoundException(__('Invalid participant'));
        }
        $participant = $this->Participant->read();
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Participant->save($this->request->data)) {
                $this->Schedule->deleteAll(
                    array('participant-phone' => $participant['Participant']['phone']),
                    false
                    );
                $this->_notifyUpdateBackendWorker($programUrl);
                $this->Session->setFlash(__('The participant has been saved.'),
                    'default',
                    array('class'=>'success-message')
                );
                $this->redirect(array('program' => $programUrl, 'controller'=>'participants', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Participant->read(null, $id);
        } 
    }

    
    public function delete() 
    {
        $programUrl = $this->params['program'];
        $id         = $this->params['id'];
        
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Participant->id = $id;
        if (!$this->Participant->exists()) {
            throw new NotFoundException(__('Invalid participant:') . $id);
        }
        $participant = $this->Participant->read();
        if ($this->Participant->delete()) {
            $this->Schedule->deleteAll(
                array('participant-phone' => $participant['Participant']['phone']),
                false
                );
            $this->Session->setFlash(
                __('Participant and related schedule deleted'),
                'default',
                array('class'=>'success-message')
            );
            $this->redirect(
                array('program' => $programUrl,
                    'action' => 'index'
                    )
                );
        }
        $this->Session->setFlash(__('Participant was not deleted'));
        $this->redirect(
            array(
                'program' => $programUrl,
                'action' => 'index'
                )
            );
    }

    
    public function view() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $id = $this->params['id'];

        $this->Participant->id = $id;
        if (!$this->Participant->exists()) {
            throw new NotFoundException(__('Invalid participant'));
        }
        $participant = $this->Participant->read(null, $id);
        $histories   = $this->History->find(
            'participant', 
            array(
                'phone' => $participant['Participant']['phone']
                )
            );
        $this->set(compact('participant','histories'));
    }

    
    public function import()
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        require_once 'excel_reader2.php';
        //$data = new Spreadsheet_Excel_Reader("example.xls");

        $programName = $this->Session->read($this->params['program'].'_name');
        $programUrl  = $this->params['program'];

        if ($this->request->is('post')) {
            if (!$this->request->data['Import']['file']['error']) {
                $fileName = $this->request->data['Import']['file']["name"];
                $ext      = end(explode('.', $fileName));

                if (!($ext == 'csv') and !($ext == 'xls') and !($ext == 'xlsx')) {
                    $this->Session->setFlash('This file format is not supported');
                    return;
                }

                $filePath = WWW_ROOT . "files/" . $programUrl; 

                if (!file_exists(WWW_ROOT . "files/".$programUrl)) {
                    //echo 'create folder: ' . WWW_ROOT . "files/".$programUrl;
                    mkdir($filePath);
                    chmod($filePath, 0777);
                }
                                
                /** in case the file has already been created, 
                * the chmod function should not be called.
                */
                $wasFileAlreadyThere = false;
                if(file_exists($filePath . DS . $fileName)) {
                    $wasFileAlreadyThere = true;
                }

                copy($this->request->data['Import']['file']['tmp_name'],
                    $filePath . DS . $fileName);
                
                if(!$wasFileAlreadyThere) {
                    chmod($filePath . DS . $fileName, 0777);
                }
                
                if ($ext == 'csv') {
                    $entries = $this->processCsv($filePath, $fileName);
                } else if ($ext == 'xls' || $ext == 'xlsx') {
                    $entries = $this->processXls($filePath, $fileName);
                }
                $this->_notifyUpdateBackendWorker($programUrl);
            }
        } 
        $this->set(compact('entries'));
    }

    
    private function processCsv($filePath, $fileName)
    {
        $importedParticipants = fopen($filePath . DS . $fileName,"r");
        $entries              = array();
        $participant          = array();
        $count                = 0;

        while (!feof($importedParticipants)) {                    
            $entries[] = fgets($importedParticipants);
            if ($count > 0 && $entries[$count]) {
                $this->Participant->create();
                $entries[$count]      = str_replace("\n", "", $entries[$count]);
                $explodeLine          = explode(",", $entries[$count]);
                $participant['phone'] = $explodeLine[0];
                $participant['phone'] = $this->checkPhoneNumber($participant['phone']);
                $participant['name']  = $explodeLine[1];
                //print_r($participant);
                if ($this->Participant->save($participant)) {
                    $entries[$count] .= " insert ok"; 
                } else {
                    $entries[$count] .= " duplicated phone line ".($count+1);
                }
                
            }
            $count++; 
        }
        return $entries;
    }

    
    private function processXls($filePath, $fileName)
    {
        $data = new Spreadsheet_Excel_Reader($filePath . DS . $fileName);
        for ( $i = 2; $i <= $data->rowcount($sheet_index=0); $i++) {
            $participant['phone'] = $data->val($i,'A');
            $participant['phone'] = $this->checkPhoneNumber($participant['phone']);
            $participant['name']  = $data->val($i,'B');
            $this->Participant->create();
            //for view report
            $entries[$i] = $participant['phone'] . ','.$participant['name'];
            if ($this->Participant->save($participant)) {
                $entries[$i] .= " insert ok"; 
            } else {
                $entries[$i] .= " duplicated phone line ".$i;
            }
        }
        return $entries;
    }
    
   
    public function checkPhoneNumber($phoneNumber) 
    {
        $newPhoneNumber = preg_replace("/[^0-9]/", "", $phoneNumber);
        $newPhoneNumber = preg_replace("/^0/", "", $newPhoneNumber);
        return $newPhoneNumber;
    }
    
    
}
<commit_msg>fix bug redirection after edit<commit_after><?php
App::uses('AppController','Controller');
App::uses('Participant','Model');
App::uses('History', 'Model');
App::uses('ProgramSetting', 'Model');
App::uses('Schedule', 'Model');
App::uses('VumiRabbitMQ', 'Lib');


class ProgramParticipantsController extends AppController
{

    public $uses = array('Participant', 'History');


    function constructClasses() 
    {
        parent::constructClasses();

        $options = array('database' => ($this->Session->read($this->params['program']."_db")));

        $this->Participant    = new Participant($options);
        $this->History        = new History($options);
        $this->ProgramSetting = new ProgramSetting($options);
        $this->Schedule       = new Schedule($options);
        $this->VumiRabbitMQ   = new VumiRabbitMQ();
    }


    function beforeFilter() 
    {
        parent::beforeFilter();
        //$this->Auth->allow('*');
    }

    
    public function index() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $participants = $this->paginate();
        $this->set(compact('participants'));        
    }


    protected function _notifyUpdateBackendWorker($workerName)
    {
        $this->VumiRabbitMQ->sendMessageToUpdateSchedule($workerName);
    }
    

    public function add() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $programUrl = $this->params['program'];
 
        if ($this->request->is('post')) {
            $this->Participant->create();
            if ($this->Participant->save($this->request->data)) {
                $this->_notifyUpdateBackendWorker($programUrl);
                $this->Session->setFlash(__('The participant has been saved.'),
                    'default',
                    array('class'=>'success-message')
                );
                $this->redirect(array(
                    'program' => $programUrl,  
                    'controller' => 'programParticipants',
                    'action' => 'index'
                    ));
            } else {
                $this->Session->setFlash(__('The participant could not be saved.'));
            }
        }        
    }

    
    public function edit() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $programUrl = $this->params['program'];
        $id         = $this->params['id'];
        
        $this->Participant->id = $id;
        if (!$this->Participant->exists()) {
            throw new NotFoundException(__('Invalid participant'));
        }
        $participant = $this->Participant->read();
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Participant->save($this->request->data)) {
                $this->Schedule->deleteAll(
                    array('participant-phone' => $participant['Participant']['phone']),
                    false
                    );
                $this->_notifyUpdateBackendWorker($programUrl);
                $this->Session->setFlash(__('The participant has been saved.'),
                    'default',
                    array('class'=>'success-message')
                );
                $this->redirect(array('program' => $programUrl, 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Participant->read(null, $id);
        } 
    }

    
    public function delete() 
    {
        $programUrl = $this->params['program'];
        $id         = $this->params['id'];
        
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Participant->id = $id;
        if (!$this->Participant->exists()) {
            throw new NotFoundException(__('Invalid participant:') . $id);
        }
        $participant = $this->Participant->read();
        if ($this->Participant->delete()) {
            $this->Schedule->deleteAll(
                array('participant-phone' => $participant['Participant']['phone']),
                false
                );
            $this->Session->setFlash(
                __('Participant and related schedule deleted'),
                'default',
                array('class'=>'success-message')
            );
            $this->redirect(
                array('program' => $programUrl,
                    'action' => 'index'
                    )
                );
        }
        $this->Session->setFlash(__('Participant was not deleted'));
        $this->redirect(
            array(
                'program' => $programUrl,
                'action' => 'index'
                )
            );
    }

    
    public function view() 
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        $id = $this->params['id'];

        $this->Participant->id = $id;
        if (!$this->Participant->exists()) {
            throw new NotFoundException(__('Invalid participant'));
        }
        $participant = $this->Participant->read(null, $id);
        $histories   = $this->History->find(
            'participant', 
            array(
                'phone' => $participant['Participant']['phone']
                )
            );
        $this->set(compact('participant','histories'));
    }

    
    public function import()
    {
        $programTimezone = $this->ProgramSetting->find('programSetting', array('key' => 'timezone'));
        $this->set(compact('programTimezone'));
        
        require_once 'excel_reader2.php';
        //$data = new Spreadsheet_Excel_Reader("example.xls");

        $programName = $this->Session->read($this->params['program'].'_name');
        $programUrl  = $this->params['program'];

        if ($this->request->is('post')) {
            if (!$this->request->data['Import']['file']['error']) {
                $fileName = $this->request->data['Import']['file']["name"];
                $ext      = end(explode('.', $fileName));

                if (!($ext == 'csv') and !($ext == 'xls') and !($ext == 'xlsx')) {
                    $this->Session->setFlash('This file format is not supported');
                    return;
                }

                $filePath = WWW_ROOT . "files/" . $programUrl; 

                if (!file_exists(WWW_ROOT . "files/".$programUrl)) {
                    //echo 'create folder: ' . WWW_ROOT . "files/".$programUrl;
                    mkdir($filePath);
                    chmod($filePath, 0777);
                }
                                
                /** in case the file has already been created, 
                * the chmod function should not be called.
                */
                $wasFileAlreadyThere = false;
                if(file_exists($filePath . DS . $fileName)) {
                    $wasFileAlreadyThere = true;
                }

                copy($this->request->data['Import']['file']['tmp_name'],
                    $filePath . DS . $fileName);
                
                if(!$wasFileAlreadyThere) {
                    chmod($filePath . DS . $fileName, 0777);
                }
                
                if ($ext == 'csv') {
                    $entries = $this->processCsv($filePath, $fileName);
                } else if ($ext == 'xls' || $ext == 'xlsx') {
                    $entries = $this->processXls($filePath, $fileName);
                }
                $this->_notifyUpdateBackendWorker($programUrl);
            }
        } 
        $this->set(compact('entries'));
    }

    
    private function processCsv($filePath, $fileName)
    {
        $importedParticipants = fopen($filePath . DS . $fileName,"r");
        $entries              = array();
        $participant          = array();
        $count                = 0;

        while (!feof($importedParticipants)) {                    
            $entries[] = fgets($importedParticipants);
            if ($count > 0 && $entries[$count]) {
                $this->Participant->create();
                $entries[$count]      = str_replace("\n", "", $entries[$count]);
                $explodeLine          = explode(",", $entries[$count]);
                $participant['phone'] = $explodeLine[0];
                $participant['phone'] = $this->checkPhoneNumber($participant['phone']);
                $participant['name']  = $explodeLine[1];
                //print_r($participant);
                if ($this->Participant->save($participant)) {
                    $entries[$count] .= " insert ok"; 
                } else {
                    $entries[$count] .= " duplicated phone line ".($count+1);
                }
                
            }
            $count++; 
        }
        return $entries;
    }

    
    private function processXls($filePath, $fileName)
    {
        $data = new Spreadsheet_Excel_Reader($filePath . DS . $fileName);
        for ( $i = 2; $i <= $data->rowcount($sheet_index=0); $i++) {
            $participant['phone'] = $data->val($i,'A');
            $participant['phone'] = $this->checkPhoneNumber($participant['phone']);
            $participant['name']  = $data->val($i,'B');
            $this->Participant->create();
            //for view report
            $entries[$i] = $participant['phone'] . ','.$participant['name'];
            if ($this->Participant->save($participant)) {
                $entries[$i] .= " insert ok"; 
            } else {
                $entries[$i] .= " duplicated phone line ".$i;
            }
        }
        return $entries;
    }
    
   
    public function checkPhoneNumber($phoneNumber) 
    {
        $newPhoneNumber = preg_replace("/[^0-9]/", "", $phoneNumber);
        $newPhoneNumber = preg_replace("/^0/", "", $newPhoneNumber);
        return $newPhoneNumber;
    }
    
    
}

{'fraction_non_alphanumeric': 0.16568479478066117, 'fraction_numerical': 0.0018501387604070306, 'mean_word_length': 1.7026315789473685, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 314, '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': '1208632', 'n_tokens_mistral': 5675, 'n_tokens_neox': 5268, 'n_words': 885}