Skip to content
Snippets Groups Projects
RawFieldOutputHandler.php 5.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • jaapjansma's avatar
    jaapjansma committed
    <?php
    /**
     * @author Jaap Jansma <jaap.jansma@civicoop.org>
     * @license AGPL-3.0
     */
    
    namespace Civi\DataProcessor\FieldOutputHandler;
    
    
    jaapjansma's avatar
    jaapjansma committed
    use Civi\DataProcessor\Exception\DataSourceNotFoundException;
    use Civi\DataProcessor\Exception\FieldNotFoundException;
    
    jaapjansma's avatar
    jaapjansma committed
    use CRM_Dataprocessor_ExtensionUtil as E;
    use Civi\DataProcessor\Source\SourceInterface;
    use Civi\DataProcessor\DataSpecification\FieldSpecification;
    
    
    jaapjansma's avatar
    jaapjansma committed
    class RawFieldOutputHandler extends AbstractFieldOutputHandler implements OutputHandlerSortable{
    
    jaapjansma's avatar
    jaapjansma committed
    
      /**
       * @var \Civi\DataProcessor\DataSpecification\FieldSpecification
       */
      protected $inputFieldSpec;
    
    
    jaapjansma's avatar
    jaapjansma committed
      /**
       * @var \Civi\DataProcessor\DataSpecification\FieldSpecification
       */
      protected $outputFieldSpec;
    
      /**
       * @var SourceInterface
       */
      protected $dataSource;
    
    jaapjansma's avatar
    jaapjansma committed
      /**
       * @return \Civi\DataProcessor\DataSpecification\FieldSpecification
       */
    
    jaapjansma's avatar
    jaapjansma committed
      public function getOutputFieldSpecification() {
        return $this->outputFieldSpec;
    
    jaapjansma's avatar
    jaapjansma committed
      }
    
    
    jaapjansma's avatar
    jaapjansma committed
      /**
    
    jaapjansma's avatar
    jaapjansma committed
       * @return \Civi\DataProcessor\DataSpecification\FieldSpecification
    
    jaapjansma's avatar
    jaapjansma committed
       */
    
    jaapjansma's avatar
    jaapjansma committed
      public function getSortableInputFieldSpec() {
        return $this->inputFieldSpec;
    
    jaapjansma's avatar
    jaapjansma committed
      }
    
      /**
       * Returns the data type of this field
       *
       * @return String
       */
      protected function getType() {
        return $this->inputFieldSpec->type;
      }
    
      /**
       * Initialize the processor
       *
       * @param String $alias
       * @param String $title
       * @param array $configuration
       * @param \Civi\DataProcessor\ProcessorType\AbstractProcessorType $processorType
       */
      public function initialize($alias, $title, $configuration) {
    
    jaapjansma's avatar
    jaapjansma committed
        $this->dataSource = $this->dataProcessor->getDataSourceByName($configuration['datasource']);
    
    jaapjansma's avatar
    jaapjansma committed
        if (!$this->dataSource) {
          throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
        }
    
    jaapjansma's avatar
    jaapjansma committed
        $this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
    
    jaapjansma's avatar
    jaapjansma committed
        if (!$this->inputFieldSpec) {
          throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
            1 => $title,
            2 => $configuration['field'],
            3 => $configuration['datasource']
          )));
        }
    
    jaapjansma's avatar
    jaapjansma committed
        $this->dataSource->ensureFieldInSource($this->inputFieldSpec);
    
    jaapjansma's avatar
    jaapjansma committed
    
        $this->outputFieldSpec = clone $this->inputFieldSpec;
        $this->outputFieldSpec->alias = $alias;
        $this->outputFieldSpec->title = $title;
    
    jaapjansma's avatar
    jaapjansma committed
        $this->outputFieldSpec->type = $this->getType();
    
    jaapjansma's avatar
    jaapjansma committed
      }
    
      /**
       * Returns the formatted value
       *
       * @param $rawRecord
       * @param $formattedRecord
       *
       * @return \Civi\DataProcessor\FieldOutputHandler\FieldOutput
       */
      public function formatField($rawRecord, $formattedRecord) {
        return new FieldOutput($rawRecord[$this->inputFieldSpec->alias]);
      }
    
    
    jaapjansma's avatar
    jaapjansma committed
      /**
       * Returns true when this handler has additional configuration.
       *
       * @return bool
       */
      public function hasConfiguration() {
        return true;
      }
    
      /**
       * When this handler has additional configuration you can add
       * the fields on the form with this function.
       *
       * @param \CRM_Core_Form $form
       * @param array $field
       */
      public function buildConfigurationForm(\CRM_Core_Form $form, $field=array()) {
        $fieldSelect = $this->getFieldOptions($field['data_processor_id']);
    
        $form->add('select', 'field', E::ts('Field'), $fieldSelect, true, array(
          'style' => 'min-width:250px',
    
          'class' => 'crm-select2 huge data-processor-field-for-name',
    
    jaapjansma's avatar
    jaapjansma committed
          'placeholder' => E::ts('- select -'),
        ));
        if (isset($field['configuration'])) {
          $configuration = $field['configuration'];
          $defaults = array();
          if (isset($configuration['field']) && isset($configuration['datasource'])) {
            $defaults['field'] = $configuration['datasource'] . '::' . $configuration['field'];
          }
          $form->setDefaults($defaults);
        }
      }
    
      /**
       * When this handler has configuration specify the template file name
       * for the configuration form.
       *
       * @return false|string
       */
      public function getConfigurationTemplateFileName() {
        return "CRM/Dataprocessor/Form/Field/Configuration/RawFieldOutputHandler.tpl";
      }
    
    
      /**
       * Process the submitted values and create a configuration array
       *
       * @param $submittedValues
       * @return array
       */
      public function processConfiguration($submittedValues) {
        list($datasource, $field) = explode('::', $submittedValues['field'], 2);
        $configuration['field'] = $field;
        $configuration['datasource'] = $datasource;
        return $configuration;
      }
    
      /**
       * Returns all possible fields
       *
       * @param $data_processor_id
       *
       * @return array
       * @throws \Exception
       */
      protected function getFieldOptions($data_processor_id) {
        $fieldSelect = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFieldsInDataSources($data_processor_id, array($this, 'isFieldValid'));
        return $fieldSelect;
      }
    
      /**
       * Callback function for determining whether this field could be handled by this output handler.
       *
       * @param \Civi\DataProcessor\DataSpecification\FieldSpecification $field
       * @return bool
       */
      public function isFieldValid(FieldSpecification $field) {
        return true;
      }