You are here

class FlexiformFCHandler in Flexiform 7

@file Contains the Field Comlete handler for the FlexiformWebform Module.

Hierarchy

Expanded class hierarchy of FlexiformFCHandler

4 string references to 'FlexiformFCHandler'
flexiform_webform_entity_info in flexiform_webform/flexiform_webform.module
Implements hook_entity_info().
flexiform_webform_flexiform_build_alter in flexiform_webform/flexiform_webform.module
Implements hook_flexiform_build_alter().
flexiform_webform_form in flexiform_webform/flexiform_webform.admin.inc
Generates the model type editing form.
flexiform_webform_form_flexiform_field_configure_form_alter in flexiform_webform/flexiform_webform.module
Implements hook_form_flexiform_field_configure_form_alter().

File

flexiform_webform/flexiform_webform.fc.inc, line 7
Contains the Field Comlete handler for the FlexiformWebform Module.

View source
class FlexiformFCHandler extends fcComplete {
  protected $ignored = array();

  /**
   * Get which form to use to calculate completeness.
   */
  public function getForm() {
    $webform = entity_load_single('flexiform_webform', $this->entity->webform);
    $form = '__none';
    if (!empty($webform->data['fc']['form'])) {
      $form = $webform->data['fc']['form'];
    }
    return $form;
  }

  /**
   * Calculate whether an element has been ignored.
   *
   * @return TRUE|FALSE
   *   True if the element is ignored.
   */
  public function elementIgnored($flexiform, $element_namespace) {
    if (isset($this->ignored[$element_namespace])) {
      return $this->ignored[$element_namespace];
    }
    $element = FlexiformElement::getElement($flexiform, $element_namespace);

    // Currently only support field api elements.
    if (!$element instanceof FlexiformElementField) {
      $this->ignored[$element_namespace] = TRUE;
      return TRUE;
    }
    $field = $element
      ->getField();
    $instance = $element
      ->getInstance();
    $settings = !empty($instance['settings']['fc']) ? $instance['settings']['fc'] : array();

    // Work out if this field needs to be included.
    $this->ignored[$element_namespace] = TRUE;
    if (!empty($settings['fc_include']) && $settings['fc_include'] == 'always') {
      $this->ignored[$element_namespace] = FALSE;
    }
    else {
      if (!empty($settings['fc_include']) && $settings['fc_include'] == 'entity_exists' && !empty($this->entities[$settings['fc_include__entity_exists']])) {
        $this->ignored[$element_namespace] = FALSE;
      }
      else {
        if (!empty($settings['fc_include']) && $settings['fc_include'] == 'element_complete') {
          $other_element = $settings['fc_include__element_complete'];
          $complete = $this
            ->getElementCompleteness($flexiform, $other_element);
          if ($complete === NULL) {
            $option = $settings['fc_include__element_complete_if_ignored'];
            if ($option == 'include') {
              $this->ignored[$element_namespace] = FALSE;
            }
            else {
              $this->ignored[$element_namespace] = TRUE;
            }
          }
          else {
            $this->ignored[$element_namespace] = !$complete;
          }
        }
      }
    }
    return $this->ignored[$element_namespace];
  }

  /**
   * Get Element completeness.
   */
  public function getElementCompleteness($flexiform, $element_namespace) {
    if ($this
      ->elementIgnored($flexiform, $element_namespace)) {
      return NULL;
    }
    if (isset($this->completeness[$element_namespace])) {
      return $this->completeness[$element_namespace];
    }
    $this->completeness[$element_namespace] = $this
      ->calculateElementCompleteness($flexiform, $element_namespace);
    return $this->completeness[$element_namespace];
  }

  /**
   * Calculate if a given element is complete.
   *
   * @return TRUE|FALSE|NULL
   *   True/False if the fields is complete/not complete. Null if the field is ignored.
   */
  public function calculateElementCompleteness($flexiform, $element_namespace) {

    // If we already know that this is ignored then return NULL;
    if ($this
      ->elementIgnored($flexiform, $element_namespace)) {
      return NULL;
    }
    $element = FlexiformElement::getElement($flexiform, $element_namespace);
    $element_settings = $flexiform->elements[$element_namespace];
    $field = $element
      ->getField();
    $instance = $element
      ->getInstance();
    $settings = !empty($instance['settings']['fc']) ? $instance['settings']['fc'] : array();

    // Get Necessary information.
    $entity = $this->entities[$element_settings['entity_namespace']];
    $entity_type = $flexiform->entities[$element_settings['entity_namespace']]['entity_type'];
    $field_name = $field['field_name'];

    // If the entity is empty and the field isn't allowed to be empty then
    // obviously this form isn't complete.
    if (empty($entity)) {
      if (!empty($settings['fc_allow_empty'])) {
        return TRUE;
      }
      return FALSE;
    }

    // Go through the field items if any are "not empty" then
    // we count that as complete (a bit simplistic but okay).
    $field_items = field_get_items($entity_type, $entity, $field_name);
    if (empty($field_items)) {
      if (!empty($settings['fc_allow_empty'])) {

        // Complex fields can be set so that they optionally
        // have content, and if they don't they must be
        // counted as complete
        return TRUE;
      }
      return FALSE;
    }

    // Choose the right plugin for the field type.
    $plugin = fc_get_plugin($field['type']);
    if ($function = ctools_plugin_get_function($plugin, 'completeness check')) {

      // Process the field to determine whether it's complete, normally we
      // just compare the number of complete field_items with the cardinality
      // but other fields might be more complex (like matrix fields).
      $cardinality = ctools_plugin_get_function($plugin, 'cardinality check');
      return $cardinality($function, $field_items, $instance, $field);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function completeness() {
    $form = $this
      ->getForm();
    if ($form == '__none') {
      return parent::completeness();
    }
    $flexiform = entity_load_single('flexiform', $form);
    if (!$flexiform) {
      throw new Exception(t('Completeness Calculation Failed: The flexiform %form does not exist.'));
    }

    // Get the Entities.
    $this->entities = $flexiform
      ->getBuilder($this->entity)
      ->getEntityManager()
      ->getEntities();
    $this->completeness = array();
    foreach ($flexiform->elements as $element_namespace => $element_settings) {
      $elementComplete = $this
        ->getElementCompleteness($flexiform, $element_namespace);
    }
    $count_fields = count($this->completeness);
    $complete_fields = count(array_filter($this->completeness));
    $this->complete = $count_fields == $complete_fields;
    $this->percentage = $count_fields ? (int) ($complete_fields * 100 / $count_fields) : 100;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
fcComplete::$complete protected property
fcComplete::$entity_type protected property
fcComplete::build public static function
fcComplete::debug public function
fcComplete::delete public function
fcComplete::export public function Export this class to a string.
fcComplete::fetch public function
fcComplete::load public static function
fcComplete::save public function
fcComplete::SKIP_IF_EXISTS constant
fcComplete::__construct protected function
fcComplete::__get public function
FlexiformFCHandler::$ignored protected property
FlexiformFCHandler::calculateElementCompleteness public function Calculate if a given element is complete.
FlexiformFCHandler::completeness public function Overrides fcComplete::completeness
FlexiformFCHandler::elementIgnored public function Calculate whether an element has been ignored.
FlexiformFCHandler::getElementCompleteness public function Get Element completeness.
FlexiformFCHandler::getForm public function Get which form to use to calculate completeness.