AddConfigToQuickForm.php 4.21 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Civi\ActionProvider\Utils\UserInterface;

use \Civi\ActionProvider\Action\AbstractAction;

7 8
use CRM_ActionProvider_ExtensionUtil as E;

9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Helper class to add a configuration specification on to a quick form class.
 */
class AddConfigToQuickForm {
	
	/**
	 * BuildForm helper. Adds the elements to the form.
	 *
	 * @param \CRM_Core_Form $form
	 *   The form.
	 * @param AbstractAction $action
	 *   The action
21
   * @param $prefix
22
	 */
23
	public static function buildForm(\CRM_Core_Form $form, AbstractAction $action, $prefix=null) {
24 25 26 27 28 29 30
		// Check whether the actionProviderElementNames is already set if so get 
		// the variable and append the configuration for this action to it.
		// Because we dont want to overwrite the current actionProviderElementNames.	
		$elementNames = $form->get_template_vars('actionProviderElementNames');
		if (empty($elementNames)) {
			$elementNames = array();
		}
31 32 33 34
		if (!$prefix) {
		  $prefix = get_class($action);
    }
    $actionProviderElementDescriptions = array();
35 36
		$elementNames[$prefix] = array();
		foreach($action->getConfigurationSpecification() as $config_field) {
37
			$field_name = $prefix.$config_field->getName();
38 39 40
			if ($config_field->getDescription()) {
			  $actionProviderElementDescriptions[$field_name] = $config_field->getDescription();
      }
41 42 43 44
			$attributes = array(
				'class' => $prefix,
			);	
			
45 46
			if (!empty($config_field->getFkEntity())) {
				$attributes['entity'] = $config_field->getFkEntity();
47 48
        $attributes['placeholder'] = E::ts('- Select -');
        $attributes['select'] = array('minimumInputLength' => 0);
49 50 51 52 53 54 55
				if ($config_field->isMultiple()) {
					$attributes['multiple'] = true;
				}
				$form->addEntityRef($field_name, $config_field->getTitle(), $attributes, $config_field->isRequired());
				$elementNames[$prefix][] = $field_name;
			}else if (!empty($config_field->getOptions())) {
				$attributes['class'] .= ' crm-select2 huge';
56
				$attributes['placeholder'] = E::ts('- Select -');
57 58 59 60
				if ($config_field->isMultiple()) {
					$attributes['multiple'] = true;
					$options = $config_field->getOptions();
				} else {
61
					$options = $config_field->getOptions();
62 63 64 65 66 67 68
				}
				$form->add('select', $field_name, $config_field->getTitle(), $options, $config_field->isRequired(), $attributes);
				$elementNames[$prefix][] = $field_name;
			} else {
				$attributes['class'] .= ' huge';
				$form->add('text', $field_name, $config_field->getTitle(), $attributes, $config_field->isRequired());
				$elementNames[$prefix][] = $field_name;
69
			}
70

71 72
		}
		$form->assign('actionProviderElementNames', $elementNames);
73
		$form->assign('actionProviderElementDescriptions', $actionProviderElementDescriptions);
74 75 76 77 78 79 80 81 82
	}
	
	/**
	 * Returns the default values set by data or by the default value of the configuration specification.
	 * 
	 * @param AbtrsactAction $action
	 *   The action.
	 * @param array $data
	 *   The current configuration array
83
   * @param prefix
84 85
	 * @return array
	 */
86
	public static function setDefaultValues(AbstractAction $action, $data, $prefix=null) {
87
		$defaultValues = array();
88 89 90
    if (!$prefix) {
      $prefix = get_class($action);
    }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		foreach($action->getConfigurationSpecification() as $config_field) {
  		if (isset($data[$config_field->getName()])) {
  			$defaultValues[$prefix.$config_field->getName()] = $data[$config_field->getName()];
			} elseif (!empty($config_field->getDefaultValue())) {
				$defaultValues[$prefix.$config_field->getName()] = $config_field->getDefaultValue();
			}
		}
		return $defaultValues;
	}
	
	/**
	 * Returns the submitted configuration.
	 * 
	 * @param \CRM_Core_Form $form
	 *   The form.
	 * @param AbstractAction $action
	 *   The action
108
   * @param $prefix
109 110
	 * @return array
	 */
111 112 113 114
	public static function getSubmittedConfiguration(\CRM_Core_Form $form, AbstractAction $action, $prefix = null) {
    if (!$prefix) {
      $prefix = get_class($action);
    }
115
		$submitted_configuration = array();
116
		$submittedValues = $form->getVar('_submitValues');
117 118 119 120 121 122 123 124 125
		foreach($action->getConfigurationSpecification() as $config_field) {
  		if (isset($submittedValues[$prefix.$config_field->getName()])) {
  			$submitted_configuration[$config_field->getName()] = $submittedValues[$prefix.$config_field->getName()];
			}
		}
		return $submitted_configuration;
	}
		
}