⚠ 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 forum is read-only and soon will be archived. ⚠


Zalo

Member Since 27 Feb 2013
Offline Last Active Jul 31 2013 02:22 PM
-----

Topics I've Started

GC Multi Pourpuse Extension - Stackable Callbacks + field types + Extensible Configs

24 April 2013 - 12:57 AM

Hello to you all!

I wanted to share this extension I've made that might help you to speed things up.

 

Quick Documentation: 

 

function field_type_ext( $field, $type, [$extras]):
 - Basically is the same as field_type but let's you set your own types without editing the GC library. For now, the only new type is 'yes_no' that is a dropdown.
 The idea it's to provide a clean and easy way to include personalized and recurrent field_types for the community... maybe join all the field_type extensions and plugins in just one...
 
 
function set_soft_delete([$database_field], [$deteled_value]): 
 - Overrides the delete function with a soft delete function (by default: table column 'deleted' setted to 1).
   If $database_field is set, it uses that one instead of 'deleted'
   If $deleted_value is set, it uses that insead of 1.
 
 
function append_fields($field, [$field2, ...]):
 - Same as fields() from GC with the difference that this one adds the fields passed by parameter to the end of the fields list (auto-eliminates duplicates).
 
function append_add_fields($field, [$field2, ...]):
 - Same as add_fields() from GC with the difference that this one adds the fields passed by parameter to the end of the fields list (auto-eliminates duplicates).
 
function append_edit_fields($field, [$field2, ...]):
 - Same as edit_fields() from GC with the difference that this one adds the fields passed by parameter to the end of the fields list (auto-eliminates duplicates).
 
 
 
function prepend_fields($field, [$field2, ...]):
 - Same as fields() from GC with the difference that this one adds the fields passed by parameter to the beginning of the fields list (auto-eliminates duplicates).
 
function prepend_add_fields($field, [$field2, ...]):
 - Same as add_fields() from GC with the difference that this one adds the fields passed by parameter to the beginning of the fields list (auto-eliminates duplicates).
 
function prepend_edit_fields($field, [$field2, ...]):
 - Same as edit_fields() from GC with the difference that this one adds the fields passed by parameter to the beginning of the fields list (auto-eliminates duplicates).
 
 
 
function append_fields_after($field, $field2, [$field3, ...]):
 - Appends the field2, [field3, ...] fields to the field list (add+edit) after $field. If $field doesn't exist it just appends them at the end of the list (auto-eliminates duplicates).
 
function append_add_fields_after($field, [$field2, ...]):
 - Appends the field2, [field3, ...] fields to the field list (add) after $field. If $field doesn't exist it just appends them at the end of the list (auto-eliminates duplicates).
 
function append_edit_fields_after($field, [$field2, ...]):
 - Appends the field2, [field3, ...] fields to the field list (edit) after $field. If $field doesn't exist it just appends them at the end of the list (auto-eliminates duplicates).
 
 
 
function remove_fields($field, [$field2, ...]):
 - Removes the fields passed as parameters from the fields list (add+edit) (if they are setted).
 
function remove_add_fields($field, [$field2, ...]):
 - Removes the fields passed as parameters from the add fields list(if they are setted).
 
function remove_edit_fields($field, [$field2, ...]):
 - Removes the fields passed as parameters from the edit fields list (if they are setted).
 
 
 
function append_columns($columns, [$columns2, ...]):
 - Appends columns passed as parameters at the end of the existing columns list (auto-eliminates duplicates).
 
function prepend_columns($columns, [$columns2, ...]):
 - Appends columns passed as parameters at the beginning of the existing columns list (auto-eliminates duplicates).
 
function remove_columns($columns, [$columns2, ...]):
 - Removes the columns passed as parameters from the columns list (if they are setted).

 

 

--- Since Last Update ---
 
function callback_before_insert(array($this,'function_name'),[$override_all=0]):
- Same as GC original if override_all is set to 1. Else (default) adds the function to a callback array. The setted callbacks run one after the other in the order they where setted.
 
function callback_after_insert(array($this,'function_name'),[$override_all=0]):
- Same as GC original if override_all is set to 1. Else (default) adds the function to a callback array. The setted callbacks run one after the other in the order they where setted.
 
function callback_before_update(array($this,'function_name'),[$override_all=0]):
- Same as GC original if override_all is set to 1. Else (default) adds the function to a callback array. The setted callbacks run one after the other in the order they where setted.
 
function callback_after_update(array($this,'function_name'),[$override_all=0]):
- Same as GC original if override_all is set to 1. Else (default) adds the function to a callback array. The setted callbacks run one after the other in the order they where setted.
 
function callback_before_delete(array($this,'function_name'),[$override_all=0]):
- Same as GC original if override_all is set to 1. Else (default) adds the function to a callback array. The setted callbacks run one after the other in the order they where setted.
 
function callback_after_delete(array($this,'function_name'),[$override_all=0]):
- Same as GC original if override_all is set to 1. Else (default) adds the function to a callback array. The setted callbacks run one after the other in the order they where setted.
 
 
function callback_post_render(array($this,'function_name'),[$override_all=0]):
- Adds a callback function that runs right after the render() function.
 - Example: 
     
$gc->callback_post_render(array($this,'edit_body'));

public function edit_body($output) {
    $output->output.='Lorem ipsum dolor sit amet, 
        consectetur adipisicing elit, sed do 
        eiusmod tempor incididunt ut labore et 
        dolore magna aliqua.'; 
    //This just adds the text to the end of the generated html
    return $output;
}

 

-------------------------------------
 
Why should I use this?
 
So... why all this functions? The idea behind this is to allow the users to create some kind-of extensible configurations. I called them 'basic setups' but it's just a name.
Why a basic setup?
Let's say that almost all of the tables you use, have a creation date, a public field (if the content is or not visible in the public part of the site),a name and use soft_delete. 
 
You just go to your extension_grocery_crud library file (or another that inherits from it) and create something like this:
 
/* EXAMPLE OF BASIC SETUPS USE*/
 
    public function basic_gc_config($table_name, $content_public_name, $template='twitter-bootstrap'){
        $this->set_theme($template);
        
        $this->set_table($table_name)
            ->set_subject($content_public_name);
        $this->set_soft_delete();
        $this->columns('name','created','public');    
        $this->field_type_ext('public','yes_no');
        $this->required_fields('name');
        $this->fields(
            'name',
            'public'
        );
 
    }
 
(Don't worry it's in the attached file to)
 
So, when you go to your controller, you just call the library, make an instance (let's call it $c) and then $c->basic_gc_config('table_name', 'my_content');
After that you use the functions that the extension includes to add, move, remove, or adapt the columns and fields you need for that specific content type.
Of course, this is just a very minimal example of what you could pre-set in this "basic setups".
 
Also, fell completely free to add stuff to it, modify it, remove stuff from it and whatever you like. You may (if you want) post the modifications here or send them to my mail (greysama86@gmail.com - Only English or Spanish plz) and I promise to include them (as long as they are not repeated :P) in future updates.
You may also publish your on "basic setups" if you want.
 
I will try to update it at least once or twice a month (if there is something to update). 
 
---------------------------------------------------------------------------------
 
How can I add stuff?
 
Well, you can use anything available at the GC documentation. You should also be able to use many of the internal GC library functions and variables, since this is an extension to the library itself.
 
---------------------------------------------------------------------------------
 
Instalation:
 
Just copy this file in your libraries folder and call it on your controller after GC.
 
Example:

 

$this->load->library('grocery_crud');
$this->load->library('extension_grocery_CRUD');


$crud = new Extension_grocery_CRUD(); //This goes INSTEAD of the "new Grocery_CRUD();"

 

With this, you will have all the GC functions plus the ones listed above.

 

---------------------------------------------------------------------------------

 

GitHub Repository:
 

https://github.com/g...on_grocery_crud

 

---------------------------------------------------------------------------------

 
Hope you like this little extension. I am open to suggestions for any functionality you may want to add, modify or whatever.
 
Bye!
 
VERY IMPORTANT: This works "out of the box" with the last GitHub version of GC. If you want to use it with 1.3.3 (stable) you only need to change the $columns variable from private to protected on the GC library file. You may use this extension even without that, but all the columns related functions will not work and even crash :(

Set error message on callback insert/update

02 April 2013 - 11:25 PM

Quick question:

Is there a parameter or some variable/array/something you can set from a callback_before/after_insert/update where you can set a custom error message?

For example:

You have an article form... and you include some custom field. You check that its value it's ok with the function callback_before_insert/update. If it's not you just return false. Is there (already) a way to set the error message so you can tell the user WHY it failed?

 

Thanks in advance!


Adding New Field Types and extra data to the fields

27 February 2013 - 11:12 AM

Hello to you all!

 

First of all... i MUST say it: web-johnny... you are a genius! You have no idea how much I love this library!

 

That been said... I have 2 questions:

 

First: Is there a way (and how should I do it) to include more field types for the field_type function? If so... can that be done without changing the library file?

I also thought about creating another library file (like grocery_crud_extension) defining a function like extra_field_type (ideally with the same parameters as field_type) which would call some pre-defined callback_field depending on the field type you pass.

I would love some feed back about this idea, since I have been using GC for just a little while ('bout a month or so) and maybe there is a better way...

 

Second: It´s possible to add some extra data (html) to a default field? For ex.: You have a field named lastname... on the add/edit page, it has some automatic-attributes (id, etc.). Can you add some extra html attributes? Like a class for example...

I ask this because sometimes you may need to define different css styles for some fields in a form, but not others, or maybe running some js on just some of them.

Maybe there is something like the callback_field that let's you modify the default html instead of overwriting it? (like callback_after_insert but for a field... that would give you the default code...)

 

Thanks in advance!!

 

P.D.: If something sounds off... it's probably because my first language is not English, feel free to ask as many things and times as you need  :P