添加后,Grocery Crud默认值

时间:2014-05-04 12:11:03

标签: codeigniter grocery-crud

如果用户没有选择要上传的图片,我需要添加默认图片。 用于加载图像的字段可选。 我该怎么做?

我使用此控制器。

public function news() {
        $cur_user_id['user_id'] = $this->ion_auth->user()->row()->user_id;
        $this->db->update('news', $cur_user_id);

        $crud = new grocery_CRUD();
        $crud->set_table('news')
                ->display_as('title', 'Заголовок')
                ->display_as('content', 'Текст')
                ->display_as('date', 'Дата')
                ->display_as('urlpict', 'Изображение')
                ->display_as('hide', 'Отображение')
                ->display_as('tags', 'Теги')
                ->display_as('add_new_tags', 'Добавить новые теги')
                ->display_as('menu_id', 'Раздел меню');
        $crud->unset_columns('user_id');
        $crud->required_fields('title', 'content', 'date', 'hide');
        //relation
        $crud->set_relation('menu_id', 'my_menu', 'name');
        $crud->set_relation_n_n('tags', 'tag2art', 'tags', 'art_id', 'tag_id', 'tag');
        //configuration for uploading 
        $this->config->set_item('grocery_crud_file_upload_allow_file_types', 'gif|jpeg|jpg|png');
        $crud->set_field_upload('urlpict', 'assets/uploads/images');
        //callback function

        $crud->callback_after_upload(array($this, 'func_callback_after_upload'));

        $state = $crud->getState();
        $state_info = $crud->getStateInfo();

        if ($state == 'add') {
            $crud->fields('title', 'content', 'date', 'urlpict', 'hide', 'tags', 'add_new_tags', 'menu_id');
            $crud->callback_add_field('add_new_tags', array($this, 'add_field_callback_1'));
        } else {
            $crud->unset_fields('add_new_tags', 'views', 'user_id');
        }
        $output = $crud->render();
        $this->load->view('administrator/news', $output);
    }

这是我的callback_function

function func_callback_after_upload($uploader_response, $field_info, $files_to_upload) 
{

    $config['image_library'] = 'gd2';
    $config['source_image'] = $field_info->upload_path . '/' . $uploader_response[0]->name;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE; 
    $config['width'] = 175;
    $config['height'] = 175;

    $this->load->library('image_lib', $config); 
    $this->image_lib->resize(); 
      return true;
}

1 个答案:

答案 0 :(得分:0)

矿山解决方案 而是$crud->callback_after_update(array($this, 'default_img')); 我使用了$crud->callback_after_insert(array($this, 'default_img'));

我的控制器

function default_img($post_array, $primary_key) {
        if (!$this->input->post('urlpict')) {
            $default_img = array(
                "id" => $primary_key,
                "urlpict" => "news_default.png"
            );

            $this->db->update('news', $default_img, array('id' => $primary_key));
            return true;
        }
    }