Drupal7: real time link selected field to a text field in Drupal7 - drupal-7

I'm creating a webform to build up a subscription form, on Drupal 7.65
Goal
What I need to do is: to select a role from a list, and automatically to display the associated name of that role, in a text field.
As I said, the name should be displayed into a not modifiable text field just below it.
Suppose valid, the following list (key => value)
Field: Department
business_manager|Business Manager
hr_consultant|Human Resources
training_developer|Training Developer
and from the time going on, the associated names, are respectively
Options can appear into text field hr_business_partner
Steve Abc
Gertrude Def
Sven Hgj Klm
Thus when the trainee selects "Human Resources", the name of "Gertrude Def" should appear into the text field below the select one.
I've attached a mokup to better understand what I do need.
IMPORTANT
I can't put the names into the list as value, because the association can change but old records should keep the previously registered associations

You can use hook_form_alter() and add a new select field with the paired key value list you need to the webform. And then use javascript to update which field value gets shown in the HR Business Partner field on change, which by the way would also need to be added via your hook_form_alter. You could use a taxonomy to maintain a list of Departments/Business partners which would populate your department and business partners.
Write some javascript to dynamically update your original fields not added through the form_alter, on change. I would suggest making two textfields in your webform components which will hold the value from your form alter added fields. So that these values selected by the user gets saved in your form.
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_client_form_####"){
$form['#attached']['js'] = array(drupal_get_path('module','MODULENAME') . '/js/webform.js');
$form['hr_dept'] = array(
"#type" => "select",
"#options" => array("business_manager"=>"Business Manager", "hr_consultant"=>"Human Resources"),
);
$partners = taxonomy_get_tree(#); //the VID of the taxonomy
$list = array("0"=>"None"); //first option
foreach($partners as $tid => $partner){
$list[$partner->tid] = $partner->name;
}
$form['hr_partner'] = array(
'#type' => 'select',
'#options' => $list,
);
}
}
In your javascript file, /js/webform.js you can include all your logic to check for which value is selected on the Department field and then display the correct value in the Partners fields. At the same time, updating the original fields you've added as textfields in the webform components UI.

Related

Passing a variable as a foreign key

I am developing a simple CRM system.
User opens Customer index.ctp to see customer list
User selects Customer X and goes to view.ctp for that customer (e.g. is directed to Customers/View/5... i currently have a View button inside the table beside each row)
Customer X can have multiple addresses below in second table
User can click 'Add New' address which goes to add.ctp for Addresses
Customer ID is passed from Customers/view/X as the foreign key on Addresses/add
I have the first four steps working.
For number 5, I imagine there are a number of ways. Possibly by passing a variable across?
I want to add a new address, from the customer view, without selecting the customer a second time. On the Addresses/add.ctp I don't need the customer name or ID shown, just to end up as the foreign key somehow.
Adding the Customer id as a parameter for the CustomerAddresses/Add function.
In your Customer View you'll add the link $this->Html->link(__('New Address'), array('controller' => 'CustomerAddresses', 'action' => add, $this->data['Customer']['id'] )); given that you loaded $this->data with your Customer object on View.
In CustomerAddressesController:
public function add( $customer_id = null ) {
if( !$this->CustomerAddress->Customer->exists($customer_id)
&& empty($this->request->data['CustomerAddress']['customer_id']) ) {
// Redirect because customer_id does not exist, to avoid SOME security problems
$this->redirect($this->referer());
}
if( $this->request->is('post') ) {
// Whatever you need for your add function. Customer id will already be in $this->request->data
}
$this->set('customer_id', $customer_id);
}
And CustomerAddresses/add.ctp in your form add <input type="hidden" value="<?=$customer_id?>" />

How to create a lookup in CakePHP

I'm in the process of converting a 10 year old PHP application. After my boss hired a php consultant, he has set up a CakePHP application environment and we are learning as we go. (fun, I know). Also, I come from a javascript/sharepoint background and have not had a lot of php experience.
As a test, I created a basic address table with these fields: firstname, lastname, state, phonenumber. I've been using justice league members as names and other test data to populate my table. Baked it just fine, default bootstrap pages are working.
I decided I wanted to add a dropdown field called current status, and for now just to keep it simple I wanted the choices: alive, dead.I created the column in my address table.
I created a second table called statuses and pointed the status column in my first table to the status table, using the status id as the foriegn key.
Baked my new table and rebaked my old one.
The status drop down does not give my choices of dead or alive, If I click in the field I get an up or down arrow, and based on which one you click, it either increments or decrements by 1. So the first time I click it inserts a 0. If I go up or down, it adds or takes away one.
I'm not sure what I'm doing wrong, I'm guessing there is some additional code I need to add to the MVC?
ok, if this works, then a lot is working :-). Now to the following: set in the Status Model a query like this:
public function getStatus()
{
$opt = $this->Status->find('list', array(...));
return $opt;
}
Then get the list over to the Adress Controller like this:
$this->loadModel('Status');
$opt => $this->Status->getStatus();
$this->set('opt', $opt);
Now you are able to access the $opt in the view file.
Just delete this line in the view:
$opts = array('0' => __('dead'), '1' => __('alive'));
And it should work.
Keep it simple. Ad to your table this row (only to understand how it works): 'status' as typ "tinyint(1)". Then set this in your view file:
$opts = array('0' => __('dead'), '1' => __('alive'));
When you create the inputfield, do it like that:
echo $this->Form->input('Address.status', array('options' => $opts, 'label'
=> __('Status')));
This should work.

How to insert into database Drupal Custom Fields

I retrieve content types with the following code
$form['ct'] = array(
'#type' => 'checkboxes',
'#options' => node_type_get_names(),
'#title' => t('Hangi tür içerikler hakkında bilgi almak istersiniz?'),
);
And It gives the following output
http://i.stack.imgur.com/TOfS6.png
And when I press Create new Account button, the POST Array is so :
http://i.stack.imgur.com/BtxhC.png
My Question is How can I insert into database these values and read?
There are two ways of saving this data and associates with user ID ($user->uid).
Adding a new field for users from 'admin/config/people/accounts/fields'. Say the new field name is 'ct' and it is a list type field. In hook form_alter you can assign this options to 'ct'. Drupal will take care of saving the data.
You can create separate table to keep this information and use db_insert function in hook hook_user_update. To retrieve this information you need to use db_query in hook_user_load.

how can I filter data cumulatively in CakePHP

I'm relatively new to CakePHP and object oriented programming. I have built apps in CakePHP before, but never to this extent, hence the reason I'm asking for help.
I have a large product management database, the user needs to be able to set filter criteria to find the closest product matches.
The products table contains category_id, manufacturer_id, type_id, status_id related to the respective tables.
I have a sidebar with dropdown boxes for each filter, and want all the filters to work together, for instance, if a manufacturer and product type is selected, display those filtered results, and change the status and category dropdowns to only contain options from the already filtered results.
In the past I have accomplished this by using a dynamic query setting conditions based on user input, appending where clauses each time a filter was applied.
so far I've only been able to get individually filtered results using
$this->Product->recursive = 0;
$mid = (isset($this->params['named']['mid'])) ? $this->params['named']['mid'] : '';
if(!empty($mid)){
$this->paginate = array(
'conditions' => array('Product.manufacturer_id' => $mid),
'limit' => 10
);
}
$products = $this->paginate('Product');
$this->set(compact('products'));
where 'mid' is the manufacturer id, and this is manually entering 'mid' in the url
How do I get cumulatively filter results if the user sets category_id, manufacturer_id, type_id, status_id or any combination? And what queries would I use to repopulate the dropdowns?
(Eventually I will use ajax, but for now assume I have 4 dropdown boxes with a submit button)
The long way would be:
var $conditions = array();
if (isset($this->params['named']['mid']) && !empty($this->params['named']['mid'])){
$conditions['Product.manufacturer_id'] = $this->params['named']['mid']);
}
if (isset($this->params['named']['catid']) && !empty($this->params['named']['catid'])){
$conditions['Product.category_id'] = $this->params['named']['catid']);
}
if (isset($this->params['named']['statid']) && !empty($this->params['named']['statid'])){
$conditions['Product.status_id'] = $this->params['named']['statid']);
}
$this->paginate['conditions'] = $conditions;
$this->paginate['limit'] = '10';
$this->set('products', $this->paginate('Product'));
Once you get the hang of that, you could put all your checks into a loop.

Salesforce: Picklist(multiple) with dynamic value

I have add custom field to Account with picklist(multiple) in Salesforce. But the values of picklist should be dynamically generated from other object. If not, is it possible to push values in picklist from native app(which is written in ruby)?
I dont think the standard controller supports dynamically adding the possible picklist (select list) values.
What you could do is add a text field instead of a picklist and create a custom page with visualforce. Use the standard controller with an extension for your code.
Create a new custom object for holding picklist values for a field (could be reused for other fields). Populate it with the possible picklist values.
In the page controller, load the values for that field.
In visualforce, display a picklist for the custom field and load the values from the controller.
Add an extra input field for manual insertion if desired.
On save, insert the value of the picklist (or input box) into the custom field.
A more detailed guide can be found here
Why dont't you use a normal SelectOption List in Apex?
public List<SelectOption> getMyPicklist()
{
List<SelectOption> options = new List<SelectOption>();
List<Account> acc = [ Select Id, Name From Account Limit 10 ];
for(Account a : acc){
options.add(new SelectOption(a.Id,a.Name));
}
return options;
}
I had to update picklist with values from my database (and not from some visualforce page). So I authenticated account using Databasedotcom gem and it provides nice Api to iteract with Salesforce Objects (both Standard and Custom).
client.authenticate :token => "my-oauth-token", :instance_url => "http://na1.salesforce.com" #=> "my-oauth-token"
account = client.materialize("Account")
account_to_be_updated = account.find(account_id) # here account is same as Instance of Activerecord
account_to_be_updated.my_picklist = [value1; value2; value3; value4]
account_to_be_updated.save

Resources