SCENARIO
There are many a times requirements like the one mentioned here. As the scenario mentioned by here what user wants to achieve is a custom solution for the poject he is working on. As here, he wants to add 2 new buttons besides add. Similarly, there can be any such new functionalities that can be added to the core GC Library.
SOLUTION
(Here we will talk only about adding new buttons as expressed. The same can be replicated to add another functionalities. In near future, will share some of such functionalities that were added by me.)
One crude way is to alter the theme itself and add the buttons / functionality. It is a good practice if you wana replicate the same across all your site / application. Else, it is not. Other way around with the same pattern is, create a copy of the theme, rename it to something personal and use the same wherever the functionality is required.
Still, there is a simpler & better way around for the same.
(the following solution is to alter the base library to achieve the same solution. There is another mechanism too which i will like to share you in the next solution hence the base library remains intact and you can still achieve your own functionality with extended library version of yours.)
Open the GroceryCrud library file, find the following in the code ..
protected $callback_after_upload = null;
……there below the codes — add the following
protected $form_buttons = array();
……………….
Then u can find in for the following function ..
public function display_as($field_name, $display_as = null)
just before that or after – wherever you feel comfirtable, add the following
/**
*
* Allows user to add extra button to the add / edit form
* @param string $field
* @param string $mask
*/
public function form_buttons($button , $js_call, $class=null)
{
$this->form_buttons[$button] = array('js_call'=>$js_call, 'class'=>$class);
return $this;
}
There you are now set to go with adding the buttons …
Mind it – here i am adding the button and it being handled by a js call – if u wish to – add it with a direct link / url .. etc. There is also a class that is used here for styling. Add your own styling with it. This is a functionality used by me .. but you surely can add it your way around. Add your required params ..etc
Now post this…
Find the following…..
protected function showList()
……in here – add the following line
$data->buttons = $this->form_buttons;
What we have accomplished is – we captured the users buttons ….whatever set by him…and now we going to use them in the list. Similarly – if you want the same to be added in the add / edit form – u can achieve it the same way without tempering the functional needs.
There is 1 last thing now required to accomplish is making alterations in the view template of GC
Find the suitable location where u need to add the following buttons – and just add the same
<?php
if(isset($buttons) && is_array($buttons) && count($buttons) > 0) {
$n=1;
foreach ($buttons as $formName => $frmConfig) {
if(array_key_exists('class', $frmConfig)) {
$class = $frmConfig['class'];
} else {
$class = 'btn btn-large';
}
?>
<div class='form-button-box'>
<input class='<?php echo $class?>' type='button' onclick='<?php echo 'javascript:' . $frmConfig['js_call'] . '()'?>'
value='<?php echo $formName; ?>' id='user-button_<?php echo $n?>' />
</div>
<?php }
} ?>
now, how to use the same?
$crud->form_buttons('Button Name', 'YourJSCall', 'SomeClass');
The class here can be left blank.
The JS functionality can be added to a custom js file which then can be added using
$cud->set_js('path/to/customJSFile');
Happy GCing 🙂