return $contexts;
}
+ /**
+ * @param $fieldName
+ * @return bool|array
+ */
+ function getFieldSpec($fieldName) {
+ $fields = $this->fields();
+ $fieldKeys = $this->fieldKeys();
+
+ // Support "unique names" as well as sql names
+ $fieldKey = $fieldName;
+ if (empty($fields[$fieldKey])) {
+ $fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
+ }
+ // If neither worked then this field doesn't exist. Return false.
+ if (empty($fields[$fieldKey])) {
+ return FALSE;
+ }
+ return $fields[$fieldKey];
+ }
+
/**
* SQL version of api function to assign filters to the DAO based on the syntax
* $field => array('IN' => array(4,6,9))
}
}
- function addSelect($name, $label, $prefix = NULL, $required = NULL, $extra = NULL, $select = '- select -') {
- if ($prefix) {
- $this->addElement('select', $name . '_id' . $prefix, $label,
- array(
- '' => $select) + CRM_Core_OptionGroup::values($name), $extra
- );
- if ($required) {
- $this->addRule($name . '_id' . $prefix, ts('Please select %1', array(1 => $label)), 'required');
- }
+ /**
+ * Adds a select based on field metadata
+ * TODO: This could be even more generic and widget type (select in this case) could also be read from metadata
+ * @param CRM_Core_DAO $baoName - string representing bao object
+ * @param $name
+ * @param array $props
+ * @param bool $required
+ * @throws CRM_Core_Exception
+ * @return HTML_QuickForm_Element
+ */
+ function addSelect($baoName, $name, $props = array(), $required = FALSE) {
+ $options = $baoName::buildOptions($name, 'create');
+ if ($options === FALSE) {
+ throw new CRM_Core_Exception('No option list for field ' . $name);
}
+ if (!array_key_exists('placeholder', $props)) {
+ $props['placeholder'] = $required ? ts('- select -') : ts('- none -');
+ }
+ if (!empty($props['placeholder']) && empty($props['multiple'])) {
+ $options = array('' => '') + $options;
+ }
+ // Handle custom field
+ if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
+ list(, $id) = explode('_', $name);
+ $label = isset($props['label']) ? $props['label'] : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
+ $props['data-option-group'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
+ }
+ // Core field
else {
- $this->addElement('select', $name . '_id', $label,
- array(
- '' => $select) + CRM_Core_OptionGroup::values($name), $extra
- );
- if ($required) {
- $this->addRule($name . '_id', ts('Please select %1', array(1 => $label)), 'required');
+ $bao = new $baoName;
+ $meta = $bao->getFieldSpec($name);
+ $bao->free();
+ // Todo: localize
+ $label = CRM_Utils_Array::value('label', $props, $meta['title']);
+ if (!empty($meta['pseudoconstant']['optionGroupName'])) {
+ $props['data-option-group'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $meta['pseudoconstant']['optionGroupName'], 'id', 'name');
}
}
+ $props['class'] = isset($props['class']) ? $props['class'] . ' ' : '';
+ $props['class'] .= "crm-select2";
+ return $this->add('select', $name, $label, $options, $required, $props);
}
/**
$val = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($val, CRM_Core_DAO::VALUE_SEPARATOR));
$field->setValue($val);
}
+ // TODO: we could fetch multiple values with array(IN => $val) but not all apis support this
foreach (explode(',', $val) as $v) {
$result = civicrm_api3($api['entity'], $api['action'], array('sequential' => 1, $api['key'] => $v));
if (!empty($result['values'])) {
}
}
+ if ($element->getType() == 'select' && $element->getAttribute('data-option-group')) {
+ $this->addOptionsEditLink($el, $element);
+ }
+
if (!empty($el['frozen'])) {
if ($element->getAttribute('data-api-params') && $element->getAttribute('data-entity-value')) {
$this->renderFrozenEntityRef($el, $element);
$el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
}
+
+ /**
+ * @param array $el
+ * @param HTML_QuickForm_element $field
+ */
+ function addOptionsEditLink(&$el, $field) {
+ if (CRM_Core_Permission::check('administer CiviCRM')) {
+ $el['html'] .= ' <a href="#" class="crm-edit-optionvalue-link" title="' . ts('Edit Options') . '" data-option-group="' . $field->getAttribute('data-option-group') . '"><span class="batch-edit"></span></a>';
+ }
+ }
}
// end CRM_Core_Form_Renderer
* - fresh boolean ignore cache entries and go back to DB
* @param String $context: Context string
*
- * @return Array on success, FALSE on error.
+ * @return Array|bool - array on success, FALSE on error.
*
* @static
*/
// Core field: load schema
$dao = new $daoName;
- $fields = $dao->fields();
- $fieldKeys = $dao->fieldKeys();
+ $fieldSpec = $dao->getFieldSpec($fieldName);
$dao->free();
-
- // Support "unique names" as well as sql names
- $fieldKey = $fieldName;
- if (empty($fields[$fieldKey])) {
- $fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
- }
// If neither worked then this field doesn't exist. Return false.
- if (empty($fields[$fieldKey])) {
+ if (empty($fieldSpec)) {
return FALSE;
}
- $fieldSpec = $fields[$fieldKey];
// If the field is an enum, explode the enum definition and return the array.
if (isset($fieldSpec['enumValues'])) {