From e869b07d94c53b7b185ed890e17332a646c95487 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 5 Feb 2014 16:44:54 -0800 Subject: [PATCH] CRM-14165 - Improve option page lookup --- CRM/Activity/Form/Activity.php | 4 +-- CRM/Contact/Form/Edit/Phone.php | 6 ++-- CRM/Core/DAO.php | 5 ++- CRM/Core/Form.php | 34 ++++++++++++------- CRM/Core/PseudoConstant.php | 26 ++++++++++++++ CRM/Utils/Api.php | 60 +++++++++++++++++++++++++++++++++ css/civicrm.css | 5 ++- js/Common.js | 2 +- 8 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 CRM/Utils/Api.php diff --git a/CRM/Activity/Form/Activity.php b/CRM/Activity/Form/Activity.php index 23af98507e..82ec0cf40b 100644 --- a/CRM/Activity/Form/Activity.php +++ b/CRM/Activity/Form/Activity.php @@ -695,7 +695,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { $this->addWysiwyg($field, $values['label'], $attribute, $required); } elseif ($values['type'] == 'select' && empty($attribute)) { - $this->addSelect('CRM_Activity_BAO_Activity', $field, array(), $required); + $this->addSelect($field, array(), $required); } elseif ($field != 'source_contact_id') { $this->add($values['type'], $field, $values['label'], $attribute, $required); @@ -712,7 +712,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { CRM_Campaign_BAO_Campaign::accessCampaign() ) { $buildEngagementLevel = TRUE; - $this->addSelect('CRM_Activity_BAO_Activity', 'engagement_level'); + $this->addSelect('engagement_level'); $this->addRule('engagement_level', ts('Please enter the engagement index as a number (integers only).'), 'positiveInteger' diff --git a/CRM/Contact/Form/Edit/Phone.php b/CRM/Contact/Form/Edit/Phone.php index a268f2cb5b..3fb2d97cd4 100644 --- a/CRM/Contact/Form/Edit/Phone.php +++ b/CRM/Contact/Form/Edit/Phone.php @@ -61,16 +61,16 @@ class CRM_Contact_Form_Edit_Phone { $form->applyFilter('__ALL__', 'trim'); //phone type select - $form->addElement('select', "phone[$blockId][phone_type_id]", ts('Phone'), CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id')); + $form->addSelect("phone[$blockId][phone_type_id]", array('data-api-entity' => 'phone', 'class' => 'six')); //main phone number with crm_phone class - $form->addElement('text', "phone[$blockId][phone]", ts('Phone'), array_merge(CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone'), array('class' => 'crm_phone twelve'))); + $form->add('text', "phone[$blockId][phone]", ts('Phone'), array_merge(CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone'), array('class' => 'crm_phone twelve'))); // phone extension $form->addElement('text', "phone[$blockId][phone_ext]", ts('Extension'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone_ext')); if (isset($form->_contactType) || $blockEdit) { //Block type select - $form->addElement('select', "phone[$blockId][location_type_id]", '', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id')); + $form->addSelect("phone[$blockId][location_type_id]", array('data-api-entity' => 'phone', 'class' => 'six')); //is_Primary radio $js = array('id' => 'Phone_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );'); diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 33346c19a1..54b0b484b1 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -929,8 +929,11 @@ FROM civicrm_domain * execute a query and get the single result * * @param string $query query to be executed + * @param array $params + * @param bool $abort + * @param bool $i18nRewrite + * @return string|null the result of the query if any * - * @return string the result of the query * @static * @access public */ diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index b6b573f7a3..028af24379 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -912,18 +912,24 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * @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); + function addSelect($name, $props = array(), $required = FALSE) { + if (!isset($props['data-api-entity'])) { + $props['data-api-entity'] = CRM_Utils_Api::getEntityName($this); } + if (!isset($props['data-api-field'])) { + $props['data-api-field'] = strrpos($name, '[') ? rtrim(substr($name, 1 + strrpos($name, '[')), ']') : $name; + } + $options = civicrm_api3($props['data-api-entity'], 'getoptions', array( + 'field' => $props['data-api-field'], + ) + ); + $options = $options['values']; if (!array_key_exists('placeholder', $props)) { $props['placeholder'] = $required ? ts('- select -') : ts('- none -'); } if (!empty($props['placeholder']) && empty($props['multiple'])) { $options = array('' => '') + $options; } - $bao = new $baoName; // Handle custom field if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) { list(, $id) = explode('_', $name); @@ -933,15 +939,19 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } // Core field else { - $meta = $bao->getFieldSpec($name); - $label = isset($props['label']) ? $props['label'] : ts($meta['title']); - if (!empty($meta['pseudoconstant']['optionGroupName'])) { - $props['data-option-group-url'] = 'civicrm/admin/options/' . $meta['pseudoconstant']['optionGroupName']; + $getFields = civicrm_api3($props['data-api-entity'], 'getfields'); + foreach($getFields['values'] as $uniqueName => $fieldSpec) { + if ( + $uniqueName === $props['data-api-field'] || + CRM_Utils_Array::value('name', $fieldSpec) === $props['data-api-field'] || + in_array($props['data-api-field'], CRM_Utils_Array::value('api.aliases', $fieldSpec, array())) + ) { + break; + } } + $label = isset($props['label']) ? $props['label'] : $fieldSpec['title']; + $props['data-option-group-url'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec); } - require_once 'api/api.php'; - $props['data-api-entity'] = _civicrm_api_get_entity_name_from_dao($bao); - $bao->free(); $props['class'] = isset($props['class']) ? $props['class'] . ' ' : ''; $props['class'] .= "crm-select2"; CRM_Utils_Array::remove($props, 'label'); diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 066a82cc35..30c01bd552 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -493,6 +493,32 @@ class CRM_Core_PseudoConstant { return CRM_Utils_Array::key($value, $values); } + /** + * Lookup the admin page at which a field's option list can be edited + * @param $fieldSpec + * @return string|null + */ + static function getOptionEditUrl($fieldSpec) { + // If it's an option group, that's easy + if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) { + return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName']; + } + // For everything else... + elseif (!empty($fieldSpec['pseudoconstant']['table'])) { + $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']); + if (!$daoName) { + return NULL; + } + // We don't have good mapping so have to do a bit of guesswork from the menu + list(, , , $ent) = explode('_', $daoName); + $sql = "SELECT path FROM civicrm_menu + WHERE page_callback LIKE '%CRM_Admin_Page_$ent%' + LIMIT 1"; + return CRM_Core_Dao::singleValueQuery($sql); + } + return NULL; + } + /** * DEPRECATED generic populate method * All pseudoconstant functions that use this method are also deprecated. diff --git a/CRM/Utils/Api.php b/CRM/Utils/Api.php new file mode 100644 index 0000000000..4d700e5e1d --- /dev/null +++ b/CRM/Utils/Api.php @@ -0,0 +1,60 @@ +