How do I limit checklist options based on role? - laravel-backpack

I have a roles field.
if (!is('admin')) {
$this->crud->removeField('roles_permissions');
}
I want a user with the admin role to have access to all options in this checklist field. However, a user with a client role should have access to these roles but only specific ones.
I tried this:
if (is('client')) {
$this->crud->addField([
'label' => ucFirst(__('Roles')),
'name' => 'roles',
'type' => 'checklist',
'model' => 'Backpack\PermissionManager\app\Models\Role',
'attribute' => 'name',
]);
}
But how do I limit the options coming from the model?

You should be able to pass all options to the checklist field by using the options attribute:
$this->crud->addField([
'label' => ucFirst(__('Roles')),
'name' => 'roles',
'type' => 'checklist',
'model' => 'Backpack\PermissionManager\app\Models\Role',
'attribute' => 'name',
'options' => [
1 => 'smth',
2 => 'smth else',
]
]);

Related

Different value_options in Form Collection

I hava a Collection, in which a field User (a multiselect) depends on a previous Select, the Department. Therefore each User select contain a different "value_options".
How can I set different "value_options" when retrieving the form for each row of the Collection?
You have different options:
You create an API endpoint to retrieve form options
You make this into two different pages, the first one you choose the department and the second one you choose the user (ew)
You populate the form in server-side and you filter the selects on client side
I personally discourage the second option. Is there just to say that it is a possible solution, but NO.
The first option, the API, is interesting, but requires actually more work, especially if it is the only endpoint in your application.
The third option is the one I always use, since it requires the less code and it quite simple to implement.
In your form, you have your two elements:
$this->add([
'name' => 'department_id',
'type' => 'Select',
'attributes' => [
'id' => 'department_id'
],
'options' => [
'value_options' => [
1 => 'Marketing',
2 => 'IT',
3 => 'Logistic'
]
]
]);
$this->add([
'name' => 'user_id',
'type' => 'Select',
'attributes' => [
'id' => 'user_id',
'multiple' => true
],
'options' => [
'value_options' => [
[
'value' => 1,
'label' => 'John Doe - Marketing',
'attributes' => ['data-department-id' => 1]
],
[
'value' => 2,
'label' => 'Jane Doe - Marketing',
'attributes' => ['data-department-id' => 1]
],
[
'value' => 3,
'label' => 'Jack Doe - IT',
'attributes' => ['data-department-id' => 2]
],
[
'value' => 4,
'label' => 'Dana Doe - IT',
'attributes' => ['data-department-id' => 2]
],
[
'value' => 5,
'label' => 'Frank Doe - Logistic',
'attributes' => ['data-department-id' => 3]
],
[
'value' => 6,
'label' => 'Lara Doe - Logistic',
'attributes' => ['data-department-id' => 3]
]
]
]
]);
As you can see, all the users are put in the value_options. Keep in mind that this is just an example, you should use custom elements to populate this kind of selects ;)
Then, in your view, you render the elements:
<?= $this->formElement($this->form->get('department_id')); ?>
<?= $this->formElement($this->form->get('user_id')); ?>
And you finally add the JS code to handle the filter. Here I use jQuery, but it's not necessary to use it:
$(function () {
// Filter users when you load the page
filterUsers(false);
// Filter users when you change value on multiselect
$('#department_id').on('change', function () {
filterUsers(true);
});
});
function filterUsers(resetValue) {
var departmentId = $('#department_id').val();
// Remove previous value only when filter is changed
if (resetValue) {
$('#user_id').val(null);
}
// Disable all options
$('#user_id option').attr('disabled', 'disabled').attr('hidden', true);
// Enable only those that respect the criteria
$('#user_id option[data-department-id=' + departmentId + ']').attr('disabled', false).attr('hidden', false);
}
Final tip: don't forget to create and add to the form a Validator to check the couple department_id - user_id is correct, just to avoid (on my example) to accept Lara Doe (logistic) with IT department ;)

Magento 2 Get Category list in admin tab/main.php

I have created a custom module.Now I want to get categories in drop down in admin.
The file is on the following path,
app/code/vendor/theme/block/adminhtml/catbanner/edit/tab/Main.php
The html is for dropdown is,
$fieldset->addField(
'banner_category',
'select',
[
'label' => __('Select Category'),
'title' => __('Select Category'),
'name' => 'banner_category',
'required' => true,
'options' => \vendor\module\Block\Adminhtml\Catbanner\Grid::getOptionArray1(),
'disabled' => $isElementDisabled
]
);
I want the options to be populated by the categories.Kindly help how can i do that?
Use below code for fieldset
$fieldset->addField(
'category',
'select',
[
'name' => 'category',
'label' => __('Category'),
'id' => 'category',
'title' => __('Category'),
'values' => \vendor\module\Block\Adminhtml\Catbanner\Grid::getOptionArray1(),
'class' => 'category',
'required' => true,
]);
In your grid block use below code:
public function getOptionArray1()
{
$categoryCollection = $this->_categoryCollectionFactory->create()
->addAttributeToSelect(array('id','name'))
->addAttributeToFilter('is_active','1');
$options = array();
foreach($categoryCollection as $category){
$options[] = array(
'label' => $category->getName(),
'value' => $category->getId()
);
}
return $options;
}
Hope this will work for you...

Prestashop Module : Add multi select dropdown

I'm working on a module and I would like to know how to add multiple dropdown with fields_options.
$this->fields_options = array(
'Test' => array(
'title' => $this->l('Test'),
'icon' => 'delivery',
'fields' => array(
'IXY_GALLERY_CREATION_OCCASION' => array(
'title' => $this->l('DropdownList'),
'type' => 'select',
'multiple' => true , // not working
'identifier' => 'value',
'list' => array(
1 => array('value' => 1, 'name' => $this->l('Test 1 ')),
2 => array('value' => 2, 'name' => $this->l('Test 2)'))
)
),
),
'description' =>'',
'submit' => array('title' => $this->l('Save'))
)
);
This is how I'm doin if you're meaning that :
$combo = $this->getAddFieldsValues();
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Title'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'nom_matiere',
'options' => array(
'query' => $combo[0],
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'name',
'options' => array(
'query' => $combo[1],
'id' => 'id_option',
'name' => 'name'
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('updateData'),
)
),
);
the answer are not correctly .. due its not only dfined the field in the database, also must capture and stored in special way the values, in this example i demostrate to store as "1,2,3,6,8" using a single field
THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
here i put only the most important parts..
as mentioned int he previous link, added a new fiel in the model definition, class and the table sql
this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
then in the helper form list array of inputs define a select as:
at the begining dont foget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'select',
'label' => $this->l('Select and employee'),
'name' => 'id_employee_tech',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'options' => array(
'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
'id' => 'id_employee',
'name' => 'firstname',
'default' => array(
'value' => '',
'label' => $this->l('ninguno')
)
)
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"

zendframework 2 form populating checkbox values from a database

I am using zendframework 2 and doctrine 2. I want to populate the values of my checkboxes from values in my database (dependence injection).
I got the technique from: https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md
This is my element (it works for select elements but not for checkboxes):
$this->add(array(
'type' => 'Zend\Form\Element\MultiCheckbox',
'name' => 'timesId',
'options' => array(
'label' => 'Please Select Your Availablity',
'value_options' => array(
'object_manager' => $this->getObjectManager(),
'target_class' => 'FormDependencies\Entity\AvailablityTimeTableList',
'property' => 'job',
),
),
'attributes' => array(
'value' => '1' //set checked to '1'
)
));
public function getObjectManager()
{
return $this->objectManager;
}
I cannot find the native doctrine 2 method for checkboxes.
The error message:
Fatal error: Cannot use object of type Doctrine\ORM\EntityManager as array
i have resolved it;
under type i needed to specify that its a :
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
the complete code:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'timesId',
'options' => array(
'label' => 'Please Select Your Availablity',
'object_manager' => $this->getObjectManager(),
'target_class' => 'FormDependencies\Entity\AvailablityTimeTableList',
'property' => 'times',
'empty_option' => '--- please choose ---'
),

How to replace a build in relationship field by a flexrelate field

For example in Opportunities and Cases there are relationship fields directly pointing to accounts. I have a custom module that integrates into those modules. I could add a separate relate field for the custom module, but what I want is a flexrelate allowing only Accounts and the Custom module. This works fine by setting a special options list in the parent_name. but results in a parent_type and parent_id field in db.
How do I configure the flexrelate so that on selecting accounts as parent_type it save to account_id and on selecting the custom module it saves to it's id field?
The options for the flexrelate:
$app_list_strings['parent_type_display_custom']['CustomMod'] = 'CustomMod';
$app_list_strings['parent_type_display_custom']['Accounts'] = 'Accounts';
$app_list_strings['record_type_display_custom']['CustomMod'] = 'CustomMod';
$app_list_strings['record_type_display_custom']['Accounts'] = 'Accounts';
The additional vardefs for Cases:
$dictionary["Case"]["fields"]["parent_type"] = array (
'name' => 'parent_type',
'type' => 'link',
'vname' => 'LBL_PARENT_TYPE',
'type' => 'parent_type',
'dbType' => 'varchar',
'group' => 'parent_name',
'options' => 'parent_type_display_custom',
'len' => '255',
);
$dictionary["Case"]["fields"]['parent_name'] =
array(
'name' => 'parent_name',
'parent_type' => 'record_type_display',
'type_name' => 'parent_type',
'id_name' => 'parent_id',
'vname' => 'LBL_RELATED_TO',
'type' => 'parent(_custom)',
'source' => 'non-db',
'options' => 'record_type_display_custom',
);
$dictionary["Case"]["fields"]['parent_id'] =
array(
'name' => 'parent_id',
'vname' => 'LBL_PARENT_ID',
'type' => 'id',
'required' => false,
'reportable' => true,
'comment' => 'eighter the Id of an account or a custommodule'
);
Does this SugarCRM developer blog post help?
http://developers.sugarcrm.com/wordpress/2011/05/16/howto-create-a-flex-relate-for-other-modules/