⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Problem with custom export model - please help me ! I am waiting for reply from last few days



Peter

Peter
  • profile picture
  • Member

Posted 03 November 2014 - 12:13 PM

Can Someone here correct me what's wrong with my code ? I am actually trying to create model instead of adding function in Grocery crud library, Please someone help me, I really don't know how to extend library functions using models.

 

 

My model custom_export_model.php

[peter@localhost models]$ pwd
/var/www/html/virtual/Development/DB/application/models

 

[peter@localhost models]$ cat custom_export_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Custom_export_model extends grocery_CRUD_model {

    function __construct() { parent::__construct(); }


    public function exportToCSV($state_info = null)
    {

        $data = $this->get_common_data();

        $data->order_by     = $this->order_by;
        $data->types         = $this->get_field_types();

        $data->list = $this->get_list();
        $data->list = $this->change_list($data->list , $data->types);
        $data->list = $this->change_list_add_actions($data->list);

        $data->total_results = $this->get_total_results();

        $data->columns                 = $this->get_columns();
        $data->primary_key             = $this->get_primary_key();

        @ob_end_clean();

        $this->_export_to_csv($data);

    }


    function _export_to_csv($data)
    {
        
        $string_to_export = "";
        foreach($data->columns as $column){
            $string_to_export .= '"'.$column->display_as.'"'.",";
        }
        $string_to_export .= "\n";

        foreach($data->list as $num_row => $row){
            foreach($data->columns as $column){
                $string_to_export .= '"'.$this->_trim_export_string($row->{$column->field_name}).'"'.",";
            }
            $string_to_export .= "\n";
        }

        // Convert to UTF-16LE and Prepend BOM
        $string_to_export = "\xFF\xFE" .mb_convert_encoding($string_to_export, 'UTF-16LE', 'UTF-8');

        $filename = "export-".date("Y-m-d_H:i:s").".csv";

        header('Content-type: application/vnd.ms-excel;charset=UTF-16LE');
        header('Content-Disposition: attachment; filename='.$filename);
        header("Cache-Control: no-cache");
        echo $string_to_export;
        die();
    }


}

My controller

 

Before calling load view and render function, I am doing this

 


        $crud->set_subject('testdata');

        $state = $crud->getState();
        $state_info = $crud->getStateInfo();
        if($state == 'export')
        {
            $crud->set_model('custom_export_model');
            $crud->basic_model->exportToCSV($state_info);
        }

        $output = $crud->render();
        $this->_example_output($output);

 

 

Thank you all