Commit 753fab96 authored by RoSk0's avatar RoSk0

Added primary fields handling for contact types.

parent f365767e
......@@ -56,7 +56,10 @@ function crm_core_contact_type_form($form, &$form_state, $contact_type) {
'#required' => TRUE,
'#description' => t('The machine-readable name of this contact type. This name must contain only lowercase letters, numbers, and underscores, it must be unique.'),
'#attached' => array(
'js' => array(drupal_get_path('module', 'system') . '/system.js', $js_settings),
'js' => array(
drupal_get_path('module', 'system') . '/system.js',
$js_settings
),
),
);
}
......@@ -69,6 +72,31 @@ function crm_core_contact_type_form($form, &$form_state, $contact_type) {
'#rows' => 3,
);
$form['contact_type']['primary_fields_container'] = array(
'#type' => 'fieldset',
'#title' => t('Primary Fields'),
);
$default_primary_fields = array('email', 'address', 'phone');
$primary_fields = variable_get('crm_core_contact_default_primary_fields', $default_primary_fields);
$options = array();
if (isset($contact_type->type)) {
$instances = field_info_instances('crm_core_contact', $contact_type->type);
foreach ($instances as $instance) {
$options[$instance['field_name']] = $instance['label'];
}
}
foreach ($primary_fields as $primary_field) {
$form['contact_type']['primary_fields_container'][$primary_field] = array(
'#type' => 'select',
'#title' => t('Primary @field field', array('@field' => $primary_field)),
'#default_value' => empty($contact_type->primary_fields[$primary_field]) ? '' : $contact_type->primary_fields[$primary_field],
'#empty_value' => '',
'#empty_option' => t('--Please Select--'),
'#options' => $options,
);
}
$form['actions'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('form-actions')),
......@@ -94,7 +122,6 @@ function crm_core_contact_type_form($form, &$form_state, $contact_type) {
return $form;
}
/**
* Validation callback
*/
......@@ -109,7 +136,6 @@ function crm_core_contact_type_form_validate($form, &$form_state) {
}
}
/**
* Form submit handler: save a contact type.
*/
......@@ -117,6 +143,10 @@ function crm_core_contact_type_form_submit($form, &$form_state) {
$contact_type = $form_state['contact_type'];
$updated = !empty($contact_type->type);
// Saving primary fields selection.
$contact_type->primary_fields = $form_state['values']['contact_type']['primary_fields_container'];
unset($form_state['values']['contact_type']['primary_fields_container']);
foreach ($form_state['values']['contact_type'] as $key => $value) {
$contact_type->$key = $value;
}
......
......@@ -201,6 +201,11 @@ function crm_core_contact_schema() {
'default' => 0,
'size' => 'tiny',
),
'primary_fields' => array(
'description' => 'Serialized array of key-value pairs, where key is the primary field type and value is real field name used for this type.',
'type' => 'blob',
'serialize' => TRUE,
),
) + entity_exportable_schema_fields(),
'primary key' => array('id'),
'unique keys' => array(
......@@ -234,3 +239,14 @@ function crm_core_contact_update_7001() {
// Set unique key.
db_add_unique_key($table, 'type', array('type'));
}
/**
* Add field to contact type to store primary fields settings.
*/
function crm_core_contact_update_7002() {
db_add_field('crm_core_contact_type', 'primary_fields', array(
'description' => 'Serialized array of key-value pairs, where key is the primary field type and value is real field name used for this type.',
'type' => 'blob',
'serialize' => TRUE,
));
}
......@@ -4,9 +4,11 @@
* CRM Contact Type Entity Class.
*/
class CRMContactType extends Entity {
public $type;
public $label;
public $weight = 0;
public $primary_fields;
public function __construct($values = array()) {
parent::__construct($values, 'crm_core_contact_type');
......@@ -25,11 +27,13 @@ class CRMContactType extends Entity {
* special handling for contact objects.
*/
class CRMCoreContactTypeController extends EntityAPIControllerExportable {
public function create(array $values = array()) {
$values += array(
'name' => '',
'description' => '',
);
return parent::create($values);
}
......@@ -68,5 +72,4 @@ class CRMCoreContactTypeController extends EntityAPIControllerExportable {
// Reset cache entry, important for usecases like install procedure.
drupal_static_reset('crm_core_contact_types');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment