ci_bootstrap_3 icon indicating copy to clipboard operation
ci_bootstrap_3 copied to clipboard

On model db (Gcrud and Base_model)

Open buoncri opened this issue 8 years ago • 0 comments

Hi

I am working to have a "on module db" and while it's simple to config MY_model to have a second db which is stored in config/database.php (in future it can be a database configuration array to load with the model. (Simply passing it as the third parameter $this->load->model('model_name', '', $config); )

// Archiprogetto_model.php

class Archiprogetto_model extends MY_Model {
    public $_table = 'progetti';
    public $primary_key = 'id_proj';
    public $return_type = 'array';

    public function __construct() {
        // Call the CI_Model constructor
        parent::__construct();        

        $this->_database = $this->load->database('archiprogetti', TRUE);
    }

The temporary solution I found for Gcrud is to modify the Admin_controller to

<?php

/**
 * Base Controller for Admin module
 *  Added $database argument to generate_crud method and stored in 
 * $this->mModuleDb for using in render_crud method
 */
class Portale_Controller extends MY_Controller {

    protected $mUsefulLinks = array();

    // Grocery CRUD or Image CRUD
    protected $mCrud;
    protected $mCrudUnsetFields;
        protected $mModuleDb;

    // Constructor
    public function __construct()
    {
        parent::__construct();

        // only login users can access Admin Panel
        $this->verify_login();

                // carica il config di portale
                $tmp = $this->config->load('portale/portale.php', TRUE);

        // store site config values
        $this->mUsefulLinks = $tmp['portale']['menu_moduli'];
    }

    // Render template (override parent)
    protected function render($view_file, $layout = 'default')
    {
        // load skin according to user role
        $config = $this->mConfig['adminlte'];
        $this->mBodyClass = $config['body_class'][$this->mUserMainGroup];

        // additional view data
        $this->mViewData['useful_links'] = $this->mUsefulLinks;

        parent::render($view_file);
    }

    // Initialize CRUD table via Grocery CRUD library
    // Reference: http://www.grocerycrud.com/
    protected function generate_crud($table, $subject = '', $database = NULL)
    {
        // create CRUD object
        $this->load->library('Grocery_CRUD');                
        $crud = new grocery_CRUD();
        $crud->set_table($table);

        // auto-generate subject
        if ( empty($subject) )
        {
            $crud->set_subject(humanize(singular($table)));
        }

                // if there is a module db ...
                if ( $database != NULL) {
                    $this->mModuleDb = $database;
                }

        // load settings from: application/config/grocery_crud.php
        $this->load->config('grocery_crud');
        $this->mCrudUnsetFields = $this->config->item('grocery_crud_unset_fields');

        if ($this->config->item('grocery_crud_unset_jquery'))
            $crud->unset_jquery();

        if ($this->config->item('grocery_crud_unset_jquery_ui'))
            $crud->unset_jquery_ui();

        if ($this->config->item('grocery_crud_unset_print'))
            $crud->unset_print();

        if ($this->config->item('grocery_crud_unset_export'))
            $crud->unset_export();

        if ($this->config->item('grocery_crud_unset_read'))
            $crud->unset_read();

        foreach ($this->config->item('grocery_crud_display_as') as $key => $value)
            $crud->display_as($key, $value);

        // other custom logic to be done outside
        $this->mCrud = $crud;
        return $crud;
    }

    // Set field(s) to color picker
    protected function set_crud_color_picker()
    {
        $args = func_get_args();
        if(isset($args[0]) && is_array($args[0]))
        {
            $args = $args[0];
        }
        foreach ($args as $field)
        {
            $this->mCrud->callback_field($field, array($this, 'callback_color_picker'));
        }
    }

    public function callback_color_picker($value = '', $primary_key = NULL, $field = NULL)
    {
        $name = $field->name;
        return "<input type='color' name='$name' value='$value' style='width:80px' />";
    }

    // Append additional fields to unset from CRUD
    protected function unset_crud_fields()
    {
        $args = func_get_args();
        if(isset($args[0]) && is_array($args[0]))
        {
            $args = $args[0];
        }
        $this->mCrudUnsetFields = array_merge($this->mCrudUnsetFields, $args);
    }

    // Initialize CRUD album via Image CRUD library
    // Reference: http://www.grocerycrud.com/image-crud
    protected function generate_image_crud($table, $url_field, $upload_path, $order_field = 'pos', $title_field = '')
    {
        // create CRUD object
        $this->load->library('Image_crud');
        $crud = new image_CRUD();
        $crud->set_table($table);
        $crud->set_url_field($url_field);
        $crud->set_image_path($upload_path);

        // [Optional] field name of image order (e.g. "pos")
        if ( !empty($order_field) )
        {
            $crud->set_ordering_field($order_field);
        }

        // [Optional] field name of image caption (e.g. "caption")
        if ( !empty($title_field) )
        {
            $crud->set_title_field($title_field);
        }

        // other custom logic to be done outside
        $this->mCrud = $crud;
        return $crud;
    }

    // Render CRUD
    protected function render_crud()
    {
        // logic specific for Grocery CRUD only
        $crud_obj_name = strtolower(get_class($this->mCrud));
        if ($crud_obj_name==='grocery_crud')
        {
            $this->mCrud->unset_fields($this->mCrudUnsetFields);    
        }

                // if there is a module db ...
                if (!empty($this->mModuleDb)) {
                    // render with other db
                    $this->db = $this->load->database($this->mModuleDb, FALSE,TRUE);
                    $crud_data = $this->mCrud->render();
                    $this->db = $this->load->database('default', FALSE, TRUE);
                } else {
                    //render with default db
                    $crud_data = $this->mCrud->render();
                }

        // append scripts
        $this->add_stylesheet($crud_data->css_files, FALSE);
        $this->add_script($crud_data->js_files, TRUE, 'head');

        // display view
        $this->mViewData['crud_output'] = $crud_data->output;
        $this->render('crud');
    }
}

All improve ideas are welcome :-D

buoncri avatar Jul 05 '16 09:07 buoncri