Jump to content


Stratos Provatopoulos

Member Since 07 Apr 2013
Offline Last Active Feb 11 2014 04:01 PM
-----

Topics I've Started

Edit extra fields in n_n_relations

07 April 2013 - 02:16 PM

Hello everybody,
 
So I am super excited about this library but I was really disappointed that it couldn't handle extra fields in n_n relations.
I decided to program this functionality and now I am sharing the results with you.
 
In order to use the extra fields functinality you need to pass one extra parameter to the set_relation_n_n function indicating that you want to edit the extra fields 

 

$crud->set_relation_n_n('actors', 'film_actor', 'actors', 'film_id', 'actor_id', 'name',null, null, TRUE);
 

 

 
You can also indicate that you don't want to edit one of the extra fields of the relation table by using the unset_edit_fields. You will however be required to prepend the name of the field you wish to exclude, with the string extra_field in order to avoid name conflicts

 

$crud->unset_edit_fields(array('extra_field_{relation_table_field_name}'));
 

 

 ie 

 

$crud->unset_edit_fields(array('extra_field_salary'))
 

 

 
By using the extra option a dropdown box will appear in your edit form that is populated from the selection_table (actors in the site example).
Each time you select one of the values in the dropdown a new tab will appear underneath that contans all the extra fields that are dynamically queried from the relation_table. If the tab already exists it makes that tab active.
The data types work as well but only for int, text, boolean, set, enum, date and datetime. No upload file (although I wanted to it proved hard and I am kinda tired for now) and no relations. 
I believe that if there are extra 1-n or n-n relations in the relation table it deserves to get a separate editing enviroment plus the complexity was simply too much for me.
 
That is pretty much it 
 
I am documenting the changes so if anyone is interested please check out my code
 
\assets\grocery_crud\themes\datatables\js\datatables-add.js
In the clear form I needed to trigger the change event of the <select> element
IMPORTANT! The changes currently work ONLY with the datatables theme. It shouldn't be hard however to make it happen for other themes as well
 
\assets\grocery_crud\js\jquery_plugins\config\jquery.tab.config.js
New file to handle the client-side tab initialization functionality 
 
\assets\grocery_crud\js\jquery_plugins\ui\jquery-ui-1.9.2.custom.min.js
I needed to add the tabs widget to the jQuery UI custom build. 
 
\assets\grocery_crud\css\ui\simple\jquery-ui-1.9.0.custom.min.css
Added a few classes for the tabs
 
\application\libraries\grocery_crud.php
Lots and lots of changes to support the new functionality. The changes (hopefully) will not interfere with the rest of the code
Most of the additions where here and some of the new lines of code are kind of ugly to read. If anyone is interested, this file could use some refactoring although I think that it should be done by someone who really understands and knows the rest of the code in order to make full use of it 
 
\application\models\grocery_crud_model.php
Added a couple of functions and modified the function that saved n_n relations and their data
 
\assets\grocery_crud\js\jquery_plugins\config\jquery.tine_mce.config.js
I had to put the config file in global scope in order to call it later on from my code. (There should be a better way to do this)
IMPORTANT! The changes currently work ONLY with tinymce. It should be refactored to work with any editor or at least not die.
 
\assets\grocery_crud\css\jquery_plugins\uniform\uniform.default.css
I had a strange bug where some of the uniform radio buttons got a display:none so this is pretty much a lousy hack to prevent this by doing .radio{display:inline-block !important}