From: Coleman Watts Date: Mon, 10 Feb 2014 18:01:57 +0000 (-0800) Subject: CRM-13966 CRM-14165 - cleanup CRM_Core_Form::addSelect function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=920600e1f8c144a9b6cf767ed57b428454a3222c;p=civicrm-core.git CRM-13966 CRM-14165 - cleanup CRM_Core_Form::addSelect function --- diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index 531afb43a5..0b45786e94 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -70,7 +70,7 @@ class CRM_Contact_Form_Edit_Address { $js = array('onChange' => 'checkLocation( this.id );'); } - $form->addSelect("address[$blockId][location_type_id]", array('data-api-entity' => 'address', 'class' => 'eight') + $js); + $form->addSelect("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js); if (!$inlineEdit) { $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );'); diff --git a/CRM/Contact/Form/Edit/Email.php b/CRM/Contact/Form/Edit/Email.php index 36694b6533..88fc7b20de 100644 --- a/CRM/Contact/Form/Edit/Email.php +++ b/CRM/Contact/Form/Edit/Email.php @@ -65,7 +65,7 @@ class CRM_Contact_Form_Edit_Email { $form->addRule("email[$blockId][email]", ts('Email is not valid.'), 'email'); if (isset($form->_contactType) || $blockEdit) { //Block type - $form->addSelect("email[$blockId][location_type_id]", array('data-api-entity' => 'email', 'class' => 'eight')); + $form->addSelect("email[$blockId][location_type_id]", array('entity' => 'email', 'class' => 'eight')); $multipleBulk = CRM_Core_BAO_Email::isMultipleBulkMail(); diff --git a/CRM/Contact/Form/Edit/IM.php b/CRM/Contact/Form/Edit/IM.php index 27ee664484..fa9945846b 100644 --- a/CRM/Contact/Form/Edit/IM.php +++ b/CRM/Contact/Form/Edit/IM.php @@ -59,10 +59,10 @@ class CRM_Contact_Form_Edit_IM { $form->applyFilter('__ALL__', 'trim'); //IM provider select - $form->addSelect("im[$blockId][provider_id]", array('data-api-entity' => 'im', 'class' => 'eight')); + $form->addSelect("im[$blockId][provider_id]", array('entity' => 'im', 'class' => 'eight')); //Block type select - $form->addSelect("im[$blockId][location_type_id]", array('data-api-entity' => 'im', 'class' => 'eight')); + $form->addSelect("im[$blockId][location_type_id]", array('entity' => 'im', 'class' => 'eight')); //IM box $form->addElement('text', "im[$blockId][name]", ts('Instant Messenger'), diff --git a/CRM/Contact/Form/Edit/Phone.php b/CRM/Contact/Form/Edit/Phone.php index c3eaac9af5..528deee03b 100644 --- a/CRM/Contact/Form/Edit/Phone.php +++ b/CRM/Contact/Form/Edit/Phone.php @@ -61,7 +61,7 @@ class CRM_Contact_Form_Edit_Phone { $form->applyFilter('__ALL__', 'trim'); //phone type select - $form->addSelect("phone[$blockId][phone_type_id]", array('data-api-entity' => 'phone', 'class' => 'eight')); + $form->addSelect("phone[$blockId][phone_type_id]", array('entity' => 'phone', 'class' => 'eight')); //main phone number with crm_phone class $form->add('text', "phone[$blockId][phone]", ts('Phone'), array_merge(CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone'), array('class' => 'crm_phone twelve'))); @@ -70,7 +70,7 @@ class CRM_Contact_Form_Edit_Phone { if (isset($form->_contactType) || $blockEdit) { //Block type select - $form->addSelect("phone[$blockId][location_type_id]", array('data-api-entity' => 'phone', 'class' => 'eight')); + $form->addSelect("phone[$blockId][location_type_id]", array('entity' => 'phone', 'class' => 'eight')); //is_Primary radio $js = array('id' => 'Phone_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );'); diff --git a/CRM/Contact/Form/Edit/Website.php b/CRM/Contact/Form/Edit/Website.php index 835d7b9d27..becc48956b 100644 --- a/CRM/Contact/Form/Edit/Website.php +++ b/CRM/Contact/Form/Edit/Website.php @@ -59,7 +59,7 @@ class CRM_Contact_Form_Edit_Website { $form->applyFilter('__ALL__', 'trim'); //Website type select - $form->addSelect("website[$blockId][website_type_id]", array('data-api-entity' => 'website', 'class' => 'eight')); + $form->addSelect("website[$blockId][website_type_id]", array('entity' => 'website', 'class' => 'eight')); //Website box $form->addElement('text', "website[$blockId][url]", ts('Website'), diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index efef6fdb17..b557289937 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -378,7 +378,7 @@ class CRM_Contact_Form_Search_Criteria { $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric'); $worldRegions = array('' => '') + CRM_Core_PseudoConstant::worldRegion(); - $form->addSelect('world_region', array('data-api-entity' => 'address', 'placeholder' => ts('- any -'), 'option_url' => NULL)); + $form->addSelect('world_region', array('entity' => 'address', 'placeholder' => ts('- any -'), 'option_url' => NULL)); // checkboxes for location type $location_type = array(); diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index dc2fbd6a8a..1b6f38b7a8 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -908,22 +908,25 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * 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 * Perhaps a method like $form->bind($name) which would look up all metadata for named field - * @param CRM_Core_DAO $baoName - string representing bao object - * @param $name - * @param array $props + * @param $name - field name to go on the form + * @param array $props - mix of html attributes and special properties, namely + * - entity (api entity name, can usually be inferred automatically from the form class) + * - field (field name - only needed if different from name used on the form) + * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link + * - placeholder - set to NULL to disable * @param bool $required * @throws CRM_Core_Exception * @return HTML_QuickForm_Element */ 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['entity'])) { + $props['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; + if (!isset($props['field'])) { + $props['field'] = strrpos($name, '[') ? rtrim(substr($name, 1 + strrpos($name, '[')), ']') : $name; } - $info = civicrm_api3($props['data-api-entity'], 'getoptions', array( - 'field' => $props['data-api-field'], + $info = civicrm_api3($props['entity'], 'getoptions', array( + 'field' => $props['field'], 'options' => array('metadata' => array('fields')) ) ); @@ -945,9 +948,9 @@ class CRM_Core_Form extends HTML_QuickForm_Page { else { foreach($info['metadata']['fields'] 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())) + $uniqueName === $props['field'] || + CRM_Utils_Array::value('name', $fieldSpec) === $props['field'] || + in_array($props['field'], CRM_Utils_Array::value('api.aliases', $fieldSpec, array())) ) { break; } @@ -955,9 +958,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $label = isset($props['label']) ? $props['label'] : $fieldSpec['title']; $props['data-option-group-url'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-group-url'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec); } - $props['class'] = isset($props['class']) ? $props['class'] . ' ' : ''; - $props['class'] .= "crm-select2"; - CRM_Utils_Array::remove($props, 'label', 'option_url'); + $props['class'] = (isset($props['class']) ? $props['class'] . ' ' : '') . "crm-select2"; + $props['data-api-entity'] = $props['entity']; + $props['data-api-field'] = $props['field']; + CRM_Utils_Array::remove($props, 'label', 'entity', 'field', 'option_url'); return $this->add('select', $name, $label, $options, $required, $props); }